//----------------------------------------------------------
function ajaxCarregaCidade(uf) {
	
	var php = "ajax/ajaxCarregaCidade.php";
	var parametros = 'uf='+ $F('UF');
	
	if($('carregaCidade'))	$('carregaCidade').style.display = 'block';
	
	var ajaxNews = new Ajax.Request(php,
									{
										method: 'post',
										parameters: parametros,
										onComplete: retornoCarregaCidade
									});
	
	
}

//----------------------------------------------------------
function retornoCarregaCidade(retorno) {
		
	resposta = retorno.responseXML;
	retorno = resposta.getElementsByTagName( 'cidade' );
	
	document.getElementById('CIDADE_ID').options.length = 1;
	
	for(i=0; i < retorno.length; i++) {
		
		id 		= retorno[i].getElementsByTagName( 'id' );
		id		= id[0].firstChild.nodeValue;
		nome	= retorno[i].getElementsByTagName( 'nome' );
		nome	= nome[0].firstChild.nodeValue;
		
		novaOp 			= document.createElement('option');
		novaOp.value	= id;
		novaOp.text  	= nome;
		document.getElementById('CIDADE_ID').options.add(novaOp);
	}
	
	if($('carregaCidade'))	$('carregaCidade').style.display = 'none';
	
}

//----------------------------------------------------------
function newsSubmit(){
	
	var php = "ajax/submitNewsletter.php";
	var parametros = 'nome='+ $F('nome_cadastro') + '&email='+ $F('email_menu');
	var ajaxNews = new Ajax.Request(php,
									{
										method: 'post',
										parameters: parametros,
										onComplete: retornoNews
									});
	
}

//----------------------------------------------------------
function retornoNews(retorno){
	
	resposta = retorno.responseText;
		
	if(resposta.indexOf('newsOk') != -1)
		alert("Cadastro realizado com sucesso. Em breve novidades da Agafarma para você!");
	
	if(resposta.indexOf('newsJaExiste') != -1)
		alert("Seu e-mail já faz parte do nosso banco de dados.");

	if(resposta.indexOf('newsErro') != -1)
		alert("Ocorreu um erro ao cadastrar seu e-mail.\nTente novamente mais tarde.");
			
}

//----------------------------------------------------------
function Init(){
	// SUBMETE O FORM PELO ID DO SUBMIT (ajax não obstrutivo)
	Event.observe("submit_news","click",function(e){ checkNews() });
	
}

Event.observe(window, "load", Init, false);

//----------------------------------------------------------
function ajaxLojas(){
	
	if($F('sel_cidades') != '') {
		var php = "ajax/ajaxLojas.php";
		var parametros = 'cidade_id='+ $F('sel_cidades')+'&bairro='+ $F('sel_bairros');
		
		if($('fPopular').checked == true)
			parametros = parametros + '&fPopular=S';
		if($('fManipulacao'))
			if($('fManipulacao').checked == true)
				parametros = parametros + '&fManipulacao=S';
		
		var ajaxNews = new Ajax.Request(php,
										{
											method: 'post',
											parameters: parametros,
											onComplete: retornoLojas
										});
	}
}

//----------------------------------------------------------
function retornoLojas(retorno) {
	resposta = retorno.responseText;
	document.getElementById('cidadeDados').innerHTML = resposta;
}

//----------------------------------------------------------
function limpaForm() {
	document.formDadosPessoais.reset();
	$('btn_editar').style.display 	= 'none';
	$('btn_limpar').style.display 	= 'none';
	$('btn_add').style.display 		= 'block';
}

//----------------------------------------------------------
function ajaxFormacao(acao){
	
	if($('retorno_id'))
		$('retorno_id').style.display = 'none';
		
	if(validaFormacao()) {
		var php = "index.php?on=trabalhe&in=ajax&etapa=formacao&ac="+acao;
		var parametros = 'GRAU_INSTRUCAO='+ $F('GRAU_INSTRUCAO')+'&INSTITUICAO='+$F('INSTITUICAO')+'&CURSO_ID='+ $F('CURSO_ID')+'&SEMESTRE_SERIE='+$F('SEMESTRE_SERIE');
		
		if(acao == 'editar')
			parametros = parametros + '&formacao_id='+$F('formacao_id');
	
		var ajaxNews = new Ajax.Request(php,
									{
										method: 'post',
										parameters: parametros,
										onComplete: retornoFormacao
									});
	}
}

//----------------------------------------------------------
function ajaxFormacaoExcluir(formacao_id){

	if($('retorno_id'))
		$('retorno_id').style.display = 'none';
	
	if(confirm('Deseja Excluir?')) {
				
		var php = "index.php?on=trabalhe&in=ajax&etapa=formacao&ac=excluir";
		var parametros = 'formacao_id='+ formacao_id;
		
		var ajaxNews = new Ajax.Request(php,
									{
										method: 'post',
										parameters: parametros,
										onComplete: retornoFormacao
									});
	}
}

//----------------------------------------------------------
function retornoFormacao(retorno) {
	resposta = retorno.responseText;
	$('mostraCursos').innerHTML = resposta;
	$('formacao_id').value = '';
	limpaForm();
}

//----------------------------------------------------------
function ajaxCarregaFormacao(formacao_id) {
	
	if($('retorno_id'))
		$('retorno_id').style.display = 'none';
		
	var php = "index.php?on=trabalhe&in=ajax&etapa=formacao&ac=carregar";
	var parametros = 'formacao_id='+formacao_id;
	var ajaxNews = new Ajax.Request(php,
								{
									method: 'post',
									parameters: parametros,
									onComplete: retornoCarregaFormacao
								});

}

//----------------------------------------------------------
function retornoCarregaFormacao(retorno) {
	resposta = retorno.responseText;
	
	dados = resposta.split(";");
	
	$('GRAU_INSTRUCAO').value 	= dados[0];
	$('INSTITUICAO').value		= dados[1];
	$('CURSO_ID').value			= dados[2];
	$('SEMESTRE_SERIE').value	= dados[3];
	$('formacao_id').value		= dados[4];
	
	$('btn_editar').style.display  	= 'block';
	$('btn_limpar').style.display  	= 'block';
	$('btn_add').style.display 		= 'none';
	
}
//----------------------------------------------------------
function ajaxExperienciaExcluir(experiencia_id){

	if($('retorno_id'))
		$('retorno_id').style.display = 'none';
	
	if(confirm('Deseja Excluir?')) {
				
		var php = "index.php?on=trabalhe&in=ajax&etapa=experiencia&ac=excluir";
		var parametros = 'experiencia_id='+ experiencia_id;
		
		var ajaxNews = new Ajax.Request(php,
									{
										method: 'post',
										parameters: parametros,
										onComplete: retornoExperiencia
									});
	}
}


//----------------------------------------------------------
function ajaxExperiencia(acao){
	
	if($('retorno_id'))
		$('retorno_id').style.display = 'none';
	
	if($('PRIMEIRO_EMPREGON').checked == true)
		primeiroEmprego = 'N';
	else
		primeiroEmprego = 'S';
		
		
	if(validaExperiencia()) {
		var php = "index.php?on=trabalhe&in=ajax&etapa=experiencia&ac="+acao;
		var parametros = 'EMPRESA='+ $F('EMPRESA')+'&AGAFARMA='+$F('AGAFARMA')+'&RAMO_ID='+$F('RAMO_ID')+'&CIDADE_ID='+ $F('CIDADE_ID')+'&UF='+$F('UF')+'&DDD='+$F('DDD')+'&TELEFONE='+$F('TELEFONE')+'&DATA_ADMISSAO='+$F('DATA_ADMISSAO')+'&DATA_DEMISSAO='+$F('DATA_DEMISSAO')+'&ULTIMO_SALARIO='+$F('ULTIMO_SALARIO')+'&ULTIMO_CARGO='+$F('ULTIMO_CARGO')+'&TAREFAS='+$F('TAREFAS')+'&PRIMEIRO_EMPREGO='+primeiroEmprego;
		
		if(acao == 'editar')
			parametros = parametros + '&experiencia_id='+$F('experiencia_id');
		
		var ajaxNews = new Ajax.Request(php,
									{
										method: 'post',
										parameters: parametros,
										onComplete: retornoExperiencia
									});
	}
}

//----------------------------------------------------------
function retornoExperiencia(retorno) {
	resposta = retorno.responseText;
	$('experienciasCad').innerHTML = resposta;
	$('experiencia_id').value = '';
	limpaForm();
	$('PRIMEIRO_EMPREGON').checked = 'true';
}


//----------------------------------------------------------
function ajaxCarregaExperiencia(experiencia_id) {
	
	if($('retorno_id'))
		$('retorno_id').style.display = 'none';
		
	var php = "index.php?on=trabalhe&in=ajax&etapa=experiencia&ac=carregar";
	var parametros = 'experiencia_id='+experiencia_id;
	var ajaxNews = new Ajax.Request(php,
								{
									method: 'post',
									parameters: parametros,
									onComplete: retornoCarregaExperiencia
								});

}

//----------------------------------------------------------
function retornoCarregaExperiencia(retorno) {
	resposta = retorno.responseText;
	
	dados = resposta.split(";");
	
	$('EMPRESA').value 			= dados[0];
	$('AGAFARMA').value			= dados[1];
	$('RAMO_ID').value			= dados[2];
	$('UF').value				= dados[4];
	$('CIDADE_ID').value 		= dados[3];
	$('DDD').value				= dados[5];
	$('TELEFONE').value			= dados[6];
	$('DATA_ADMISSAO').value	= dados[7];
	$('DATA_DEMISSAO').value	= dados[8];
	$('ULTIMO_SALARIO').value	= dados[9];
	$('ULTIMO_CARGO').value		= dados[10];
	$('TAREFAS').value			= dados[11];
	$('experiencia_id').value	= dados[12];
	
	$('btn_editar').style.display  	= 'block';
	$('btn_limpar').style.display  	= 'block';
	$('btn_add').style.display 		= 'none';

	ajaxCarregaCidade(dados[4]);
	
	window.setTimeout(function() { $('CIDADE_ID').value = dados[3];},1000);
	window.setTimeout(function() { $('CIDADE_ID').value = dados[3];},2000);
	
}

//----------------------------------------------------------
function retornoCarregaCep(retorno) {
	
	function carregaCidade(retorno) {
		resposta = retorno.responseXML;
		retorno = resposta.getElementsByTagName( 'cidade' );
		
		document.getElementById('CIDADE_ID').options.length = 1;
		
		for(i=0; i < retorno.length; i++) {
			
			id 		= retorno[i].getElementsByTagName( 'id' );
			id		= id[0].firstChild.nodeValue;
			nome	= retorno[i].getElementsByTagName( 'nome' );
			nome	= nome[0].firstChild.nodeValue;
			
			novaOp 			= document.createElement('option');
			novaOp.value	= id;
			novaOp.text  	= nome;
			document.getElementById('CIDADE_ID').options.add(novaOp);
		}
		
		// MONTA CIDADE	
		if(cidade != 0) {
			for ( var i=0; i < $('CIDADE_ID').length; i++ )
				if ( cidade.toUpperCase() == $('CIDADE_ID').options[i].text )
					$('CIDADE_ID').selectedIndex = i; 
		}
		
		if(rua != 0) {
			$('ENDERECO_NOME').value = rua.toUpperCase();
		} else
			$('ENDERECO_NOME').value = '';	
		
		if(tipo != 0) {
			if(tipo.toUpperCase() == 'AVENIDA')	$('ENDERECO_TIPO').selectedIndex = 0;		
			if(tipo.toUpperCase() == 'RUA')		$('ENDERECO_TIPO').selectedIndex = 1;		
			if(tipo.toUpperCase() == 'ACESSO')	$('ENDERECO_TIPO').selectedIndex = 2;		
			if(tipo.toUpperCase() == 'ALAMEDA')	$('ENDERECO_TIPO').selectedIndex = 3;		
			if(tipo.toUpperCase() == 'BECO')	$('ENDERECO_TIPO').selectedIndex = 4;		
			if(tipo.toUpperCase() == 'ESTRADA')	$('ENDERECO_TIPO').selectedIndex = 5;		
			if(tipo.toUpperCase() == 'PRAÇA')	$('ENDERECO_TIPO').selectedIndex = 6;		
			if(tipo.toUpperCase() == 'RODOVIA')	$('ENDERECO_TIPO').selectedIndex = 7;		
			if(tipo.toUpperCase() == 'TRAVESSA')$('ENDERECO_TIPO').selectedIndex = 8;		
			if(tipo.toUpperCase() == 'PASSGEM')	$('ENDERECO_TIPO').selectedIndex = 1;		
			if(tipo.toUpperCase() == 'PASSEIO')	$('ENDERECO_TIPO').selectedIndex = 1;		
			if(tipo.toUpperCase() == 'LARGO')	$('ENDERECO_TIPO').selectedIndex = 1;		
			
		} else
			$('ENDERECO_TIPO').selectedIndex = 1;
			
		if(bairroIni != 0) {
			$('BAIRRO').value = bairroIni.toUpperCase();
		} else
			$('BAIRRO').value = '';
			
		$('ENDERECO_NUMERO').focus();
	}

	
	
	cepXML		= retorno.responseXML;
	ceps		= cepXML.getElementsByTagName( 'cep' );
	
	estado 		= ceps[0].getElementsByTagName( 'estado' );
	estado 		= estado[0].firstChild.nodeValue;
	cidade 		= ceps[0].getElementsByTagName( 'cidade' );
	cidade 		= cidade[0].firstChild.nodeValue;
	rua 		= ceps[0].getElementsByTagName( 'nome' );
	rua 		= rua[0].firstChild.nodeValue;
	tipo 		= ceps[0].getElementsByTagName( 'tipo' );
	tipo		= tipo[0].firstChild.nodeValue;
	bairroIni	= ceps[0].getElementsByTagName( 'bairroIni' );
	bairroIni	= bairroIni[0].firstChild.nodeValue;
	
	//MONTA O ESTADO
	if(estado != 0) {
		for ( var i=0; i < $('UF').length; i++ )
			if ( estado == $('UF').options[i].value )
				$('UF').selectedIndex = i;
		
		
				var parametros = 'uf='+ $F('UF');
				var ajaxCidade = new Ajax.Request("ajax/ajaxCarregaCidade.php",
									{
										method: 'post',
										parameters: parametros,
										onComplete: carregaCidade		
									});	
				
	}
	else
		$('UF').selectedIndex = 0;
	
		
	
	
}

//----------------------------------------------------------
function ajaxCarregaCep(valor) {
	
	var php = "ajax/ajaxCep.php";
	var parametros = 'cep='+valor;
	var ajaxNews = new Ajax.Request(php,
								{
									method: 'post',
									parameters: parametros,
									onComplete: retornoCarregaCep
								});

}

//----------------------------------------------------------
function ajaxCep(valor){
	if(valor.length > 9)
		ajaxCarregaCep(valor);
	else
	return false;
}
//----------------------------------------------------------
function moverCidade(origem,divDestino,acao) {
	 	 
	var php 		= "index.php?on=trabalhe&in=ajax&etapa=addCidade&acao="+acao;
	var cidade 		= new Array();
	var comboOrigem = document.getElementById(origem);
	var divDestino 	= document.getElementById(divDestino);
	var CIDADE_ID 	= '';
	var separador 	= '';
	
	for (i = 0, j = 0; i < comboOrigem.options.length; i++) {
    	if(comboOrigem.options[i].selected && comboOrigem.options[i].value != "") {
    		CIDADE_ID = CIDADE_ID + separador + comboOrigem.options[i].value ;
			separador = ';';
			cidade[j] = i;
			j++;
    	}
    }
    
	/*REVERTE A ARRAY PARA REMOVER DE BAIXO PARA CIMA*/
    cidade.reverse();
    
	if(CIDADE_ID != '') {
		/*REMOVE OS INDEXS DA ORIGEM*/
		for (i = 0; i <= cidade.length -1; i++)
			comboOrigem.remove(cidade[i]);
		
		var ajaxCidade = new Ajax.Request(php,
							{
								method: 'post',
								parameters: 'CIDADE_ID='+CIDADE_ID,
								onComplete: function(retorno) { divDestino.innerHTML = retorno.responseText; }
							});	
	}
}

//-----------------------------------------------
function excluiFoto() {
	
	var php 	  = "index.php?on=trabalhe&in=ajax&etapa=excluiFoto";
	var parametros = '';
	var ajaxNews = new Ajax.Request(php,
								{
									method: 'post',
									parameters: parametros,
									onComplete: function retornoFoto(retorno) {
										
													alert(retorno.responseText);
													document.getElementById('excluirFoto').style.display = "none";
													document.getElementById('thumbnails').innerHTML ="<img src='resources/images/sem_imagem.jpg' >";	
													document.getElementById('divFileProgressContainer').innerHTML = "";	
									}
								});
	
}