// JavaScript Document
//AJAX Funcs
function ajaxXML() {
	if (window.XMLHttpRequest) {
  		// code for IE7+, Firefox, Chrome, Opera, Safari
  		return new XMLHttpRequest();
  	}
	else if (window.ActiveXObject) {
  		// code for IE6, IE5
  		return new ActiveXObject("Microsoft.XMLHTTP");
  	}
	else {
		return false;
  	}
}

var shLicence = true;
function showLicence(id) {
	var id;
	if (shLicence) {
		xmlhttp = ajaxXML();
		if (xmlhttp) {
			var url = "?main=txt&id=licence&sid="+Math.random();
			xmlhttp.onreadystatechange = function() {
											if (xmlhttp.readyState == 4) { document.getElementById(id).innerHTML = xmlhttp.responseText; shLicence = false; }
										};
			xmlhttp.open('GET',url,true);
			xmlhttp.send(null);
		}
		else {
			window.open('?main=txt&id=licence');
		}
	}
	else { document.getElementById(id).innerHTML = ''; shLicence = true; }
}

var shLicence = true;
function loadProfile(id,fid) {
	var id,fid;
	//if (shLicence) {
		xmlhttp = ajaxXML();
		if (xmlhttp) {
			var url = "user/loadprofile.php?id="+id+"&fid="+fid+"&sid="+Math.random();
			xmlhttp.onreadystatechange = function() {
											if (xmlhttp.readyState == 4) {
												document.getElementById('profileExpose').innerHTML = '<style type="text/css">.viewRowContent {width:800px;margin-left:41px;} #viewContent table th { text-indent:0px; padding-left:30px; } #viewContent { margin-left:0px; position:relative; left:-5px; } .viewRowBorder {width:800px;margin-left:0px;}</style>';
												document.getElementById('profileExpose').innerHTML += xmlhttp.responseText;
												shLicence = false;
											}
										};
			xmlhttp.open('GET',url,true);
			xmlhttp.send(null);
		}
		else {
			window.open('?main=view&uid='+id);
		}
	//}
	//else { document.getElementById('profileExpose').innerHTML = ''; shLicence = true; }
}
function createFolder(user_id,name,del,enc) {
	var user_id;
	var nameRaw = document.getElementById('folderName').value;
	name = nameRaw.replace(/[^А-Яа-яA-Za-z0-9-_]/,'');
	if (name != '') {
		xmlhttp = ajaxXML();
		if (xmlhttp) {
			if (enc == true || enc == 1) {
				name = encodeURIComponent(name);
			}
			var url = "user/folders.php?act=create&id="+user_id+"&folder="+name+((del == true)?'&del=yes&':'&')+"sid="+Math.random();
			xmlhttp.onreadystatechange = function() {
											if (xmlhttp.readyState == 4) {
												//document.getElementById('profileExpose').innerHTML = xmlhttp.responseText;
												if (xmlhttp.responseText == 'del') {
													if (confirm('Директорията вече съществува! Желаете ли да я изтриете?')) {
														createFolder(user_id,name,true,enc);
													}
												}
												else if (xmlhttp.responseText.match(/\|/) || xmlhttp.responseText.match(/,/) || xmlhttp.responseText == '') {
													alert('Папка '+name+' беше изтрита!');
													document.getElementById('Mfavourites').click();
												}
												else {
													//alert(xmlhttp.responseText);
												}
												refreshFolders(user_id);
												document.getElementById('folderName').value = '';
											}
										};
			xmlhttp.open('GET',url,true);
			xmlhttp.send(null);
		}
	}
	else {
		document.getElementById('folderName').value = '';
		alert('Моля посочете име на новата директория!');
	}
}
function refreshFolders(user_id) {
	var user_id;
	xmlhttp = ajaxXML();
	if (xmlhttp) {
		var url = "user/folders.php?act=refresh&id="+user_id+"&sid="+Math.random();
		xmlhttp.onreadystatechange = function() {
										if (xmlhttp.readyState == 4) {
											d = new dTree('d');
											d.add(0,-1,'Предпочитани кандидати');
											var asd = xmlhttp.responseText;
											res = asd.split('|');
											for (a=0;a<res.length;a++) {
												ress = res[a].split(',');
												if (ress.length < 4) {
													d.add(ress[0],ress[1],ress[2]);
												}
												else {
													d.add(ress[0],ress[1],ress[2],ress[3],ress[4])
												}
											}
											if (document.getElementById('foldersExpose')) {
												document.getElementById('foldersExpose').innerHTML = d;
											}
											refreshSFolders(user_id);
										}
									};
		xmlhttp.open('GET',url,true);
		xmlhttp.send(null);
	}
}
function renFolder(user_id) {
	var user_id;
	var fid = document.getElementById('renCat').value;
	var fnameRaw = document.getElementById('newFolderName').value;
	fname = fnameRaw.replace(/[^А-Яа-яA-Za-z0-9-_]/,'');
	if (fid != '' && fname != '') {
		xmlhttp = ajaxXML();
		if (xmlhttp) {
			var url = "user/folders.php?act=rename&id="+user_id+"&fid="+fid+"&folder="+fname+"&sid="+Math.random();
			xmlhttp.onreadystatechange = function() {
											if (xmlhttp.readyState == 4) {
												//alert(xmlhttp.responseText);
												refreshFolders(user_id);
												document.getElementById('newFolderName').value = '';
											}
										};
			xmlhttp.open('GET',url,true);
			xmlhttp.send(null);
		}
	}
	else {
		alert('Моля изберете категория от списъка и посочете новото й име!');
	}
}
function refreshSFolders(user_id) {
	var user_id;
	xmlhttp = ajaxXML();
	if (xmlhttp) {
		var url = "user/folders.php?act=srefresh&id="+user_id+"&sid="+Math.random();
		xmlhttp.onreadystatechange = function() {
										if (xmlhttp.readyState == 4) {
											if (document.getElementById('spanfolders')) {
												document.getElementById('spanfolders').innerHTML = xmlhttp.responseText;
											}
											else if (document.getElementById('currentFolders')) {
												document.getElementById('currentFolders').innerHTML = xmlhttp.responseText;
											}
										}
									};
		xmlhttp.open('GET',url,true);
		xmlhttp.send(null);
	}
}
function addFavourite(id,name,enc) {
	var id,name;
	fid = document.getElementById('renCat').value;
	if (fid != '') {
		xmlhttp = ajaxXML();
		if (xmlhttp) {
			if (enc == true || enc == 1) {
				name = encodeURIComponent(name);
			}
			var url = "user/folders.php?act=addfav&id="+id+"&name="+name+"&fid="+fid+"&sid="+Math.random();
			xmlhttp.onreadystatechange = function() {
											if (xmlhttp.readyState == 4) {
												//alert(xmlhttp.responseText);
												alert('Кандидатът е успешно добавен!');
											}
										};
			xmlhttp.open('GET',url,true);
			xmlhttp.send(null);
		}
	}
	else {
		alert('Моля изберете папка, в която да бъде добавен '+name);
	}
}
function remFavourite(fav,fid,eid) {
	var fav,fid,eid;
	xmlhttp = ajaxXML();
	if (xmlhttp) {
		var url = "user/folders.php?act=remfav&fav="+fav+"&fid="+fid+"&sid="+Math.random();
		xmlhttp.onreadystatechange = function() {
										if (xmlhttp.readyState == 4) {
											//alert(xmlhttp.responseText);
											//refreshFolders(eid);
											document.getElementById('Mfavourites').click();
										}
									};
		xmlhttp.open('GET',url,true);
		xmlhttp.send(null);
	}
}
function viewProfile(url) {
	document.getElementById('viewProf').src = url;
}

function sanitize(id,value,county,enc) {
	val = document.getElementById(id).value;
	document.getElementById(id).value = val.replace(/[^А-я\.\s]/gi,'');
	vals = document.getElementById(id).value;
	if (vals != '') {
		loadSuggestions(vals,county,enc);
	}
}
function loadSuggestions(city,county,enc) {
	countySplit = county.split('|');
	county = countySplit[0];
	xmlhttp = ajaxXML();
	if (xmlhttp) {
		if (enc == true || enc == 1) {
			city = encodeURIComponent(city);
		}
		var url = "user/suggestions.php?city="+city+"&county="+county+"&sid="+Math.random();
		xmlhttp.onreadystatechange = function() {
										if (xmlhttp.readyState == 4) {
											//alert(xmlhttp.responseText);
											document.getElementById('city').innerHTML = xmlhttp.responseText;
										}
									};
		xmlhttp.open('GET',url,true);
		xmlhttp.send(null);
	}
}
function ucwords(str) {
// Uppercase the first character of every word in a string
// 
// version: 909.322
// discuss at: http://phpjs.org/functions/ucwords
// +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
// +   improved by: Waldo Malqui Silva
// +   bugfixed by: Onno Marsman
// *     example 1: ucwords('kevin van zonneveld');
// *     returns 1: 'Kevin Van Zonneveld'
// *     example 2: ucwords('HELLO WORLD');
// *     returns 2: 'HELLO WORLD'
return (str+'').replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase( ); } );
}