//variavel global
var boolIsProduct54 = (document.location.pathname.indexOf('salario/') > 0 ? true : false);
var funcReturn = '';
var openEngine = true;
var objFields;
var valIni;

var serverCatho = setServerCatho();
var pathProjectEngine = setPathProjectEngine();


function setPathProjectEngine(paramUrl){
	if(typeof(paramUrl) == 'undefined' || paramUrl == ''){
		arrayObjScripts = window.parent.document.getElementsByTagName("script"); 
		
		strPathScriptJs = '';
		for(i=0; i < arrayObjScripts.length; i++){
			strPathScriptJs = arrayObjScripts[i].src;
			if(strPathScriptJs.indexOf('engine/js') >= 0){
				strPathScriptJs = strPathScriptJs.substring(0, strPathScriptJs.indexOf('engine/js'))
				break;
			}
		}
		if(boolIsProduct54 == true && serverCatho == 'SEGURO'){
			strPathScriptJs = strPathScriptJs.substring(strPathScriptJs.indexOf('://'));
			strPathScriptJs = strPathScriptJs.replace('://', '');
			strPathScriptJs = '../../..'+strPathScriptJs.substring(strPathScriptJs.indexOf('/'));
		}
		if(strPathScriptJs == '')
			strPathScriptJs = URL_WWW;
	}else{
		strPathScriptJs = paramUrl;
	}
	pathProjectEngine = strPathScriptJs;
	
	return pathProjectEngine;
}

function setServerCatho(nameServer){
	if(typeof(nameServer) == 'undefined' || nameServer == ''){
		host = document.location.host;
		if(host.indexOf('www3') >= 0){
			serverCatho = 'CONLINE';
		}else if(host.indexOf('seguro') >= 0){
			serverCatho = 'SEGURO';
		}else if(host.indexOf('www3') >= 0){			
			serverCatho = 'CATHO';
		}		
	}else{		
		serverCatho = nameServer; 
	}	
	return serverCatho;
}

function ready(fields, fReturn) {
	objFields = fields;
	for (var field in objFields) {

		$j("#"+field).focus( function() {
			dadoIni(this);
		});

		$j("#"+field).blur( function() {
			dadoFim(this);
		});
	}
	if (fReturn != 'undefined' || fReturn != '') {
		funcReturn = fReturn;
	}
}

function dadoIni(obj) {
	valIni = $j(obj).val();
}

function dadoFim(obj) {
	if(valIni != $j(obj).val()) {
		searchDados(obj);
	}
}

function searchDados(element) {
	var ckCnpj = false;;
	var ckFone = false;
	var cnpj = '';	
	var executeAjax = false;
	var param = "";
	
	for (field in objFields) {
		if (field == 'cnpj5') {
			ckCnpj = true;			
		} else if (field == 'usuario_ddd_com' || field == 'usuario_fone_com') {
			ckFone = true;
		} else {
			if ($j("#"+objFields[field]).val() != '' && $j("#"+objFields[field]).val() != 'undefined') {
				executeAjax = true;
				param += "&"+objFields[field]+"="+$j("#"+field).val();
			}
		}
	}
	if (ckCnpj) {
		cnpj = $j('#cnpj1').val()+'.'+$j('#cnpj2').val()+'.'+$j('#cnpj3').val()+'/'+$j('#cnpj4').val()+'-'+$j('#cnpj5').val();
	}
	if (cnpj.length == '18') {
		param += "&cnpj="+cnpj;
		executeAjax = true;
	}
	if (ckFone) {
		if ($j('#usuario_ddd_com').val() != '' && $j('#usuario_fone_com').val() != '') {
			param += "&ddd="+$j('#usuario_ddd_com').val()+"&fone="+$j('#usuario_fone_com').val();
			executeAjax = true;
		}
	}
	if ($j(element).val() == '') {
		executeAjax = false;
	}
	if (objFields[element.id] == 'usuario_ddd_com' || objFields[element.id] == 'usuario_fone_com') {
		if ($j("#usuario_ddd_com").val() == '' || $j("#usuario_fone_com").val() == '') {
			executeAjax = false;
		}
	}

	if (executeAjax && openEngine) {
		ExecuteStateReturnFunction(pathProjectEngine+'engine/action/AutoComplete.php', 'openPopup', 'Search', param);
	}
}

function openPopup(ajaxReturn) {
	strTypeOf = new String(typeof(ajaxReturn));
	if(strTypeOf != 'undefined'){
		if (ajaxReturn['empId'].length > 0 && openEngine == true) {
			var param = '';
			for (var i=0; i < ajaxReturn['empId'].length; i++) {
				param += '&empId[]='+ajaxReturn['empId'][i];
			}
			if (serverCatho == 'SEGURO') {
				if (URL_SEGURO != undefined && URL_SEGURO != '' && URL_SEGURO != null) {
					togglePopupWBP(URL_SEGURO+'produtos/engine/action/AutoComplete.php?state=DisplayDadosEmpresa&'+param, 600, 360);
				} else {
					togglePopupWBP('https://seguro.catho.com.br/produtos/engine/action/AutoComplete.php?state=DisplayDadosEmpresa&'+param, 600, 360);
				}
			} else {
				togglePopupWBP(pathProjectEngine+'engine/action/AutoComplete.php?state=DisplayDadosEmpresa&'+param, 600, 360);
			}
		}

	}
}

function getData(empId) {
	ExecuteStateReturnFunction(pathProjectEngine+'engine/action/AutoComplete.php', 'popDados', 'GetEmpresa', '&empId='+empId);
}

function popDados(ajaxReturn) {
	var empId = ajaxReturn[0]['emp_id'];
	if ($j("#desc_produto_id").val() == '49' && empId != '' && empId != undefined) {
		ExecuteStateReturnFunction(pathProjectEngine+'engine/action/AutoComplete.php', 'verifyProduct', 'verifyProduct', '&empId='+empId);
	}
	
	var empresa = ajaxReturn[0];
	if (funcReturn == '' || funcReturn == undefined || funcReturn == 'undefined') {
		$j("#empresa_razaosocial").val(empresa['razaosocial']);
		$j("#empresa_ramo_id").val(empresa['ramo_id']);
		$j("#cnpj1").val(empresa['cnpj'].substr(0, 2));
		$j("#cnpj2").val(empresa['cnpj'].substr(3, 3));
		$j("#cnpj3").val(empresa['cnpj'].substr(7, 3));
		$j("#cnpj4").val(empresa['cnpj'].substr(11, 4));
		$j("#cnpj5").val(empresa['cnpj'].substr(16, 2));
		if (empresa['tipoemp'] == 'R') {
			$j("#empresa_tipoemp_c").attr('checked', true);
		}
		if (empresa['tipoemp'] == 'C') {
			$j("#empresa_tipoemp_r").attr('checked', true);
		}
		$j("#empresa_end").val(empresa['end']);
		$j("#empresa_end_numero").val(empresa['end_numero']);
		$j("#empresa_end_comp").val(empresa['end_comp']);
		$j("#cep1").val(empresa['cep'].substr(0, 5));
		$j("#cep2").val(empresa['cep'].substr(6, 3));
		$j("#empresa_estado_id").val(empresa['estado_id']);
		$j("#empresa_cidade_id").val(empresa['cidade_id']);
		$j("#emp_id").val(empresa['emp_id']);
	} else {
		eval(funcReturn+"(empresa);");
	}
	openEngine = false;
	togglePopupWBP();
}

function verifyProduct(ajaxReturn) {
	if (ajaxReturn[0] == true) {
		alert('Essa empresa já possui o serviço de Busca de Currículos.');
	}
}

function cancelEngine() {
	openEngine = false;
	togglePopupWBP();
}
