function ajax(){
	try{
		xmlhttp = new XMLHttpRequest();
	}
	catch(ee){
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(E){
				xmlhttp = false;
			}
		}
	}
	return xmlhttp;
}


function url_encode(str) {
	var hex_chars = "0123456789ABCDEF";
	var noEncode = /^([a-zA-Z0-9\_\-\.])$/;
	var n, strCode, hex1, hex2, strEncode = "";

	for(n = 0; n < str.length; n++){
		if (noEncode.test(str.charAt(n))){
			strEncode += str.charAt(n);
		}
		else{
			strCode = str.charCodeAt(n);
			hex1 = hex_chars.charAt(Math.floor(strCode / 16));
			hex2 = hex_chars.charAt(strCode % 16);
			strEncode += "%" + (hex1 + hex2);
		}
	}
	return strEncode;
}

function url_decode(str) {
	var n, strCode, strDecode = "";

	for (n = 0; n < str.length; n++) {
		if (str.charAt(n) == "%") {
			strCode = str.charAt(n + 1) + str.charAt(n + 2);
			strDecode += String.fromCharCode(parseInt(strCode, 16));
			n += 2;
		} else if (str.charAt(n) == "+") {
			strCode = str.charAt(n);
			strDecode += " ";
		} else {
			strDecode += str.charAt(n);
		}
	}
	return strDecode;
}


// evento para fechar suggest ao clicar fora dele
$(document).ready(function(){
	$(document).bind('mousedown',function(e){
		e=e||window.event;

		var tgt = e.scrElement||e.target;           

		if($("#suggestionsVag").css("display") != "none"){
			if($(tgt).parents('#suggestionsVag').length == 0 && $(tgt).attr('id') != 'suggestionsVag'){
				$("#suggestionsVag").hide();
			}
		}
		if($("#suggestionsVagLoc").css("display") != "none"){
			if($(tgt).parents('#suggestionsVagLoc').length == 0  && $(tgt).attr('id') != 'suggestionsVagLoc'){
				$("#suggestionsVagLoc").hide();
			}
		}
		if($("#suggestionsCur").css("display") != "none" && $(tgt).attr('id') != 'suggestionsCur'){
			if($(tgt).parents('#suggestionsCur').length == 0){
				$("#suggestionsCur").hide();
			}
		}
		if($("#suggestionsCurLoc").css("display") != "none"  && $(tgt).attr('id') != 'suggestionsCurLoc'){
			if($(tgt).parents('#suggestionsCurLoc').length == 0){
				$("#suggestionsCurLoc").hide();
			}
		}
	});
});

function KeyPressAscCode(objEvent,atual,fieldInput,fieldDiv,enter,valueId) {
	var Code;

	if (navigator.appName == 'Microsoft Internet Explorer') {
		Code = event.keyCode;
		var shift = false;
		if( Code == 16 ){
			shift = true;
		}
	}
	else if(navigator.appName == 'Netscape'){
		Code = objEvent.keyCode;
		shift = objEvent.shiftKey;
	}

	if( Code == 40 ){
		var div = atual+1;
		if( document.getElementById(fieldDiv).style.display == '' && eval("document.getElementById('div"+div+"')") ){
			var field = eval("document.getElementById('link"+div+"')");
//			if( atual>=0 ){
//				eval("document.getElementById('link"+atual+"').style.color = '#000066';");
//				eval("document.getElementById('link"+atual+"').style.background = '#ffffff';");
//			}
//			eval("document.getElementById('link"+div+"').style.color = '#0000FF';");
//			eval("document.getElementById('link"+div+"').style.background = '#e9e9e9';");
			field.focus();
		}
		return false;
	}
	else if( Code == 38){
		var div = atual-1;
		if( atual == 0 ){
			document.getElementById(fieldInput).focus();
			document.getElementById(fieldInput).value=contentOriginal;
//			eval("document.getElementById('link"+atual+"').style.color = '#000066';");
//			eval("document.getElementById('link"+atual+"').style.background = '#ffffff';");
//			eval("document.getElementById('link"+div+"').style.background = '#c9c9c9';");
		}
		else if( document.getElementById(fieldDiv).style.display == '' && eval("document.getElementById('div"+div+"')") ){
			var field = eval("document.getElementById('link"+div+"')");
//			if( atual >= 0 ){
//				eval("document.getElementById('link"+atual+"').style.color = '#000066';");
//				eval("document.getElementById('link"+atual+"').style.background = '#ffffff';");
//			}
//			eval("document.getElementById('link"+div+"').style.color = '#0000FF';");
//			eval("document.getElementById('link"+div+"').style.background = '#c9c9c9';");
			field.focus();
		}
		return false;
	}
	else if( Code == 9 ){
		document.getElementById(fieldDiv).style.display='none';
		document.getElementById(fieldInput).value=contentOriginal;
	}
	else if( Code == 13 && enter == 1 ){
		$("#suggestions").hide();
		if( atual != '-1' ){
			return false;
		}
	}
}



function ObjetivoClass(){

	this.getSuggest       = getSuggest;
	this.preencheForm     = preencheForm;
	this.preencheTempForm = preencheTempForm;
	this.unsetObjective   = unsetObjective;

	function getSuggest(fieldBox, suggestionBox, type, action,keyWord, e){
		var suggests = "";
		e=e||window.event;


		if( navigator.appName == "Microsoft Internet Explorer" ){
			var version = navigator.appVersion.split(";");
			var versionNumber = version[1].split(" ");
			var versionAbsolute = versionNumber[2].split(".");
			/*if( versionAbsolute[0] == 6 ){
				document.getElementById("expansive").style.height = "130px";
			}*/
		}

		contentOriginal = keyWord;
		if( keyWord.length > 1 && e.keyCode != 13){
			document.getElementById(suggestionBox).style.display='';
			var xmlhttp = ajax();
			xmlhttp.open("GET", "/funcoesLocais.php?action="+action+"&type="+type+"&keyWord="+url_encode(keyWord),true);
			//Executada quando o navegador obtiver o código
			xmlhttp.onreadystatechange=function(){
				if (xmlhttp.readyState==4){
					//Lê o texto
					var retorno = url_decode(xmlhttp.responseText);
					if( retorno.length > 0 ){
						document.getElementById(suggestionBox).style.display='';
					}
					else{
						document.getElementById(suggestionBox).style.display='none';
					}
					if( retorno.indexOf("::") > 0 ){
						var words = retorno.split("::");
						if( words.length >=6 ){
							document.getElementById(suggestionBox).style.height='144px';
						}
						else{
							document.getElementById(suggestionBox).style.height='75px';
						}
						for( var i=0; i < words.length; i++ ){
							if( words[i].indexOf("##") > 0 ){
								var varsTemp = words[i].split("##");
								var idLocale = varsTemp[0];
								words[i] = varsTemp[1];
								suggests += "<li id='div"+i+"'><a href='#' id='link"+i+"' onClick=\"ObjClass.preencheForm('"+fieldBox+"','"+suggestionBox+"','"+words[i]+"','"+idLocale+"');return false;\" onKeyDown=\"return KeyPressAscCode(event,"+i+",'"+fieldBox+"','"+suggestionBox+"',0,'"+idLocale+"');\" onFocus=\"ObjClass.preencheTempForm('"+fieldBox+"','"+words[i]+"');\">"+words[i]+"</a></li>";
							}
							else if( words[i].indexOf("|||") > 0 ){
								var varsTemp = words[i].split("|||");
								var total = varsTemp[1];
								words[i] = varsTemp[0];
								if( type == 'vag' ){
									if( total == 1 ){
										var vagasTxt = "vaga";
									}
									else{
										var vagasTxt = "vagas";
									}
								}
								else{
									if( total == 1 ){
										var vagasTxt = 'currículo';
									}
									else{
										var vagasTxt = 'curriculos';
									}
								}
//								suggests += "<li id='div"+i+"'><a href='#' id='link"+i+"' onClick=\"ObjClass.preencheForm('"+fieldBox+"','"+suggestionBox+"','"+words[i]+"','"+idLocale+"');return false;\" onKeyDown=\"return KeyPressAscCode(event,"+i+",'"+fieldBox+"','"+suggestionBox+"',0,'"+idLocale+"');\" onFocus=\"ObjClass.preencheTempForm('"+fieldBox+"','"+words[i]+"');\">"+words[i]+" <span class='boxBuscaNum txt11'>"+total+" "+vagasTxt+"</span></a></li>";
								suggests += "<li id='div"+i+"'><a href='#' id='link"+i+"' onClick=\"ObjClass.preencheForm('"+fieldBox+"','"+suggestionBox+"','"+words[i]+"','"+idLocale+"');return false;\" onKeyDown=\"return KeyPressAscCode(event,"+i+",'"+fieldBox+"','"+suggestionBox+"',0,'"+idLocale+"');\" onFocus=\"ObjClass.preencheTempForm('"+fieldBox+"','"+words[i]+"');\">"+words[i]+"</a></li>";
							}
							else{
								suggests += "<li id='div"+i+"'><a href='#' id='link"+i+"' onClick=\"ObjClass.preencheForm('"+fieldBox+"','"+suggestionBox+"','"+words[i]+"');return false;\" onKeyDown=\"return KeyPressAscCode(event,"+i+",'"+fieldBox+"','"+suggestionBox+"',0);\" onFocus=\"ObjClass.preencheTempForm('"+fieldBox+"','"+words[i]+"');\">"+words[i]+"</a></li>";
							}
						}
					}
					else{
						document.getElementById(suggestionBox).style.height='20px';
						if( retorno.indexOf("##") > 0 ){
							var varsTemp = retorno.split("##");
							var idLocale = varsTemp[0];
							retorno = varsTemp[1];
							suggests += "<li id='div0'><a href='#' id='link0' onClick=\"ObjClass.preencheForm('"+fieldBox+"','"+suggestionBox+"','"+retorno+"','"+idLocale+"'); return false;\" onKeyDown=\"return KeyPressAscCode(event,"+i+",'"+fieldBox+"','"+suggestionBox+"',0,'"+idLocale+"');\" onFocus=\"ObjClass.preencheTempForm('"+fieldBox+"','"+retorno+"');\">"+retorno+"</a></li>";
						}
						else if( retorno.indexOf("|||") > 0 ){
							var varsTemp = retorno.split("|||");
							var total = varsTemp[1];
							retorno = varsTemp[0];
							if( type == 'vag' ){
								if( total == 1 ){
									var vagasTxt = "vaga";
								}
								else{
									var vagasTxt = "vagas";
								}
							}
							else{
								if( total == 1 ){
									var vagasTxt = 'currículo';
								}
								else{
									var vagasTxt = 'currículos';
								}
							}
//							suggests += "<li id='div"+i+"'><a href='#' id='link"+i+"' onClick=\"ObjClass.preencheForm('"+fieldBox+"','"+suggestionBox+"','"+retorno+"');return false;\" onKeyDown=\"return KeyPressAscCode(event,"+i+",'"+fieldBox+"','"+suggestionBox+"',0);\" onFocus=\"ObjClass.preencheTempForm('"+fieldBox+"','"+retorno+"');\">"+retorno+" <span class='boxBuscaNum txt11'>"+total+" "+vagasTxt+"</span></a></li>";
							suggests += "<li id='div"+i+"'><a href='#' id='link"+i+"' onClick=\"ObjClass.preencheForm('"+fieldBox+"','"+suggestionBox+"','"+retorno+"');return false;\" onKeyDown=\"return KeyPressAscCode(event,"+i+",'"+fieldBox+"','"+suggestionBox+"',0);\" onFocus=\"ObjClass.preencheTempForm('"+fieldBox+"','"+retorno+"');\">"+retorno+"</a></li>";
						}
						else{
							suggests += "<li id='div0'><a href='#' id='link0' onClick=\"ObjClass.preencheForm('"+fieldBox+"','"+suggestionBox+"','"+retorno+"'); return false;\" onKeyDown=\"return KeyPressAscCode(event,"+i+",'"+fieldBox+"','"+suggestionBox+"',0);\" onFocus=\"ObjClass.preencheTempForm('"+fieldBox+"','"+retorno+"');\">"+retorno+"</a></li>";
						}
					}
					document.getElementById(suggestionBox).innerHTML=suggests;
					
					$("#"+suggestionBox+" li, #"+suggestionBox+" li a").mouseover(function(){
						var id = $(this).attr("id").match(/^link[0-9]+$/) ? $(this).parent("li").attr("id") : $(this).attr("id");
//						$("#"+suggestionBox+" > li").css("background-color", "#FFFFFF");
//						$("#"+suggestionBox+" > li > a").css("background-color", "#FFFFFF");
//						$("#"+suggestionBox+" > li > a").css("color", "#000066");
//						$("#"+id).css("background-color", "navy");
//						$("#"+id+" > a").css("background-color", "navy");
//						$("#"+id+" > a").css("color", "#FFFFFF");
					});
					$("#"+suggestionBox+" li, #"+suggestionBox+" li a").focus(function(){
						var id = $(this).attr("id").match(/^link[0-9]+$/) ? $(this).parent("li").attr("id") : $(this).attr("id");
//						$("#"+suggestionBox+" > li").css("background-color", "#FFFFFF");
//						$("#"+suggestionBox+" > li > a").css("background-color", "#FFFFFF");
//						$("#"+suggestionBox+" > li > a").css("color", "#000066");
//						$("#"+id).css("background-color", "navy");
//						$("#"+id+" > a").css("background-color", "navy");
//						$("#"+id+" > a").css("color", "#FFFFFF");
					});
				}
			}
			
			
			xmlhttp.send(null);
			
		}
		else{
			document.getElementById(suggestionBox).style.display='none';
		}
	}

	function preencheForm(buscaField, suggestionBox, value, id){
		value = value.replace(/ *\([0-9\.]+\)$/,"")
		document.getElementById(buscaField).value=value;
		document.getElementById(suggestionBox).style.display='none';
		contentOriginal = value;
		if( Number(id) > 0 ){
			var valueValida = " "+value;
			if( valueValida.indexOf('ESTADO') > 0 ){
				if( buscaField == 'textSearchJobsLoc' ){
					document.getElementById('estado_idJob').value=id;
				}
				else{
					document.getElementById('estado_idCur').value=id;
				}
			}
			else if( valueValida.indexOf('PAÍS') > 0 ){
				if( buscaField == 'textSearchJobsLoc' ){
					document.getElementById('pais_idJob').value=id;
				}
				else{
					document.getElementById('pais_idCur').value=id;
				}
			}
			else{
				if( buscaField == 'textSearchJobsLoc' ){
					document.getElementById('cidade_idJob').value=id;
				}
				else{
					document.getElementById('cidade_idCur').value=id;
				}
			}
		}
	}

	function preencheTempForm(buscaField, value){
		value = value.replace(/ *\([0-9\.]+\)$/,"")
		document.getElementById(buscaField).value=value;
	}

function unsetObjective(line){
		var table = document.getElementById("objectiveList");
		var lines = table.getElementsByTagName("tr");
		for( var i=0; i < lines.length; i++ ){
			if( lines[i].id == line ){
				table.deleteRow(i);
				objTotal = objTotal-1;
			}
		}
		document.getElementById('textSearchJobs').disabled=false;
	}
}

var ObjClass         = new ObjetivoClass();

