function jsAjax(){
	if (!$Id('frmLogin')){ //Verifica se o formulário já foi carregado.
		setTimeout('jsAjax()', 500); //Aguarda 0,5 segundos e chama a função novamente.
		return false;
	}

	
	
	

	jsAjaxGerenciarSenha();
}
jsAjax();

function jsAcao(oAcao, codigo){
	var sAcao				= (oAcao.value ? oAcao.value : oAcao)	//Verifica se é objeto ou texto.
	
	var edEmail				= $Id('edEmail');
	var nmCliente			= $Id('nmCliente');
	var dtNascimento		= $Id('dtNascimento');
	var dmSexo				= $Id('dmSexo');
	var nuCpf				= $Id('nuCpf');
	var dsRg				= $Id('dsRg');
	

	

	switch (sAcao.toLowerCase()){
		
		case 'cancelar':
			jsCarregando(0);
			
			edEmail.value='';
			nmCliente.value='';
			dtNascimento.value='';
			dmSexo.value='';
			nuCpf.value='';
			dsRg.value='';
			
		
		
			break;
			
		case 'salvar':
			
		
			var Email			= edEmail.value;
			var Cliente			= nmCliente.value;
			var Data			= dtNascimento.value;
			var Sexo			= dmSexo.value;
			var Cpf				= nuCpf.value;
			var Rg				= dsRg.value;
			
			
			jsAjaxClientesSalvar(Email,Cliente,Data,Sexo,Cpf,Rg);
			
			
			break;
		
	}
}

function jsAjaxGerenciarSenha()
{
	
	
	
	
	var xmlhttp = Ajax();
	
	if(xmlhttp){
		jsCarregando(1);
		//Abre a url
		
		
		xmlhttp.open('GET', '/bin/AjaxEsqueciSenha.php?op=1' , true);

		//Executada quando o navegador obtiver o código
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4){
				//Lê o texto
				var texto = new String(xmlhttp.responseText);

				//Desfaz o urlencode
				texto=texto.replace(/\+/g," ");
				texto=unescape(texto);
				
				//alert(texto);

				//Retorna o resultado;
				
				$Id('ulSenha').innerHTML=texto;	
				
				$Id('btLogin').title="Enviar";
				$Id('btLogin').value="Ok";
				addEvent($Id('btLogin'),'click',jsBotaoSalvar);
				jsFocaInput();
				jsInputs();

					
										
					
					
					
				
				jsCarregando(0);
			}
		}
		xmlhttp.send(null)
	}
	
}



function jsAjaxClientesSalvar(edEmail,nmCliente,dtNascimento,dmSexo,nuCpf,dsRg)
{
	
	
	
	//Validação Javascript
	var bedEmail		= (edEmail=="oculto" || edEmail.search(/^[a-zA-Z0-9_.-]{2,}@[a-zA-Z0-9_.-]+\.([a-zA-Z]{2,4})$/) == 0);
	var bnmCliente		= (nmCliente=="oculto" || nmCliente.search(/^[A-Za-zÀ-ú ]{3,80}$/gi) == 0);
	var bdtNascimento	= (dtNascimento=="oculto" || dtNascimento.search(/^((((0?[1-9]|1\d|2[0-8])\/(0?[1-9]|1[0-2]))|((29|30)\/(0?[13456789]|1[0-2]))|(31\/(0?[13578]|1[02])))\/((19|20)?\d\d))$|((29\/0?2\/)((19|20)?(0[48]|[2468][048]|[13579][26])|(20)?00))$/gi) == 0);
	var bnuCpf			= (nuCpf=="oculto" || validarCPF(nuCpf)==1);
	var bdsRg			= (dsRg=="oculto" || dsRg!="");
	var bdmSexo			= (dmSexo=="oculto" || dmSexo.search(/^[mf]{1}/) == 0);
	
	
	var xmlhttp = Ajax();
	
	if(xmlhttp && bedEmail && bnmCliente && bdtNascimento && bnuCpf && bdsRg && bdmSexo){
		
		
		jsCarregando(1,"Validando seus dados...");
		//Abre a url
		
		
		xmlhttp.open('GET', '/bin/AjaxEsqueciSenha.php?op=2&edEmail=' + edEmail+ '&nmCliente=' + nmCliente+ '&dtNascimento=' + dtNascimento+ '&nuCpf=' + nuCpf+ '&dsRg=' + dsRg+ '&dmSexo=' + dmSexo , true);

		//Executada quando o navegador obtiver o código
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4)
			{
				//Lê o texto
				var texto = new String(xmlhttp.responseText);

				//Desfaz o urlencode
				texto=texto.replace(/\+/g," ");
				texto=unescape(texto);
				
				//alert(texto);
				
				div = texto.split("<>");
				
				texto = div[0];
				dados = div[1];
				if(dados)
				{
				div = dados.split("*");
				email = div[0];
				nome  = div[1];
				}
				
				//alert(texto);
				
				//Retorna o resultado;
				if (texto.search('alteracaoOk')!=-1)
				{
				
					//alert("Ok");
					$Id('frmLogin').style.display='none';
					jsAcao('cancelar');		//Limpa os campos;
					//setTimeout('self.location=\'finalizar.php\'', 5);
										
					if (!$Id('dvCadastroOk'))
					{
						oCadastro		= document.createElement('div');
						oCadastro.id	= 'dvCadastroOk';
						oCadastro.innerHTML='<h5>ALTERAÇÃO CONCLUÍDO!!</h5><br/><p>'+nome+', \nSeu cadastro foi alterado com sucesso, foi enviado um email para <Br/><strong>('+email+')</strong>, siga as instruçoes para ativar o seu registro.<br/><br/><strong>Agradecemos e aguardamos a sua compra.</strong><br/>  </p>';
						$Id('dvInterna').appendChild(oCadastro);
					}
					
					setTimeout('self.location=\''+urlSiteOk+'/Login.html\'', 7000);
				
				}
				else
				{
				
					
					var saida = 'ATENÇAO:\n';			
					if (texto.search('stAtivo')!=-1)
						saida += '\nAtivaçao: O seu cadastro não está ativado entre em contato para solucionar o problema.';
					if (texto.search('emailInvalido')!=-1)
						saida += '\nE-mail inválido!';
					if (texto.search('dsInvalido')!=-1)
					{
						saida += '\nOs dados não conferem.';
						$Id('edEmail').focus();
					}
									
					alert(saida);
				}
				
					
									
					
					
				

				jsCarregando(0);
			}
		}
		xmlhttp.send(null)
	}
	else{
		var saida = 'Erro de validação:\n';

		
		if (!bdmSexo){
			saida += '\nSexo: ' + $Id('dmSexo').title;
			$Id('dmSexo').focus();
		}
		if (!bdtNascimento){
			saida += '\nData de Nascimento: ' + $Id('dtNascimento').title;
			$Id('dtNascimento').focus();
		}
		if (!bnuCpf){
			saida += '\nCPF: ' + $Id('nuCpf').title;
			$Id('nuCpf').focus();
		}
		if (!bdsRg){
			saida += '\nRG: ' + $Id('dsRg').title;
			$Id('dsRg').focus();
		}
		
		if (!bnmCliente){
			saida += '\nNome: ' + $Id('nmCliente').title;
			$Id('nmCliente').focus();
		}
		if (!bedEmail){
			saida += '\nE-mail: ' + $Id('edEmail').title;
			$Id('edEmail').focus();
		}

		
		
		alert(saida);
	}
}

