﻿function checkNavegador (){
  try{
	return new XMLHttpRequest();
  }catch(ee){
    try{
	  return new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
	  try{
		 return new ActiveXObject("Microsoft.XMLHTTP");
	   }catch(E){
		 return false;
	   }
   }
  }
	
}


function limpaVarAut(){
	$("#cxAutUserErroLogin").css("background","");
	$("#cxAutUserErroLogin").html("");
//	$("#cxAutUserErroLogin").css("height","");
}

/*
	Autentica usuario ( Meus Pedidos)
*/
function autenticaUserMp(){
	var email = $("#email").val();
	var senha = $("#senha").val();
	
	limpaVarAut(); // Limpa as divs e seus retornos 
	
	if (email.length == 0) {
	  alert(CkLoginALERTcmpemail);
	  $("#email").focus();
	  return false;
	}
	
	if (!checkEmail(email)) {
	  alert(CkLoginALERTcmpemailinc); 
	  $("#email").focus();
	  return false;
	}

	// Caso seja queira se logar 
	if (senha.length == 0) {
	  alert(VldPwdALERTcamposenha);
	  $("#senha").focus();
	  return false;
	}
	
	// Inicia a consulta no servidor 
	xmlhttp = checkNavegador();
	xmlhttp.open("POST", "./e-commerce/actions/autenticaUser2.php", true);	
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
	xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
	xmlhttp.setRequestHeader("Pragma", "no-cache");
	xmlhttp.onreadystatechange = function (){
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200) {
					var retXml = xmlhttp.responseXML.getElementsByTagName('main');
					var resultado = xmlhttp.responseText;
//					alert(xmlhttp.responseText)
					if (resultado == '1'){
						location.replace("./e-commerce/minha_conta/index.php");
					} else {
						$("#cxAutUserErroLogin").css("background","#FFFF99");
						$("#cxAutUserErroLogin").css("color","#CC0000");
						$("#cxAutUserErroLogin").css("height","18px");
						$("#cxAutUserErroLogin").html('<center><font size="2">E-mail ou Senha inválidos!</font></center>');
					}
				} else {
				   alert("Problemas ao carregar o arquivo.");
				}
			}
	};
	xmlhttp.send("email="+email+"&senha="+senha);
}

function esqueciMinhaSenha(){
	if ( validaCampos(document.frmEsqueciSenha) == true ){
		$("#cxRespErro").css("background","#FFFF99");
		$("#cxRespErro").css("color","#CC0000");
		$("#cxRespErro").css("height","18px");
		// Inicia a consulta no servidor 
		xmlhttp = checkNavegador();
		xmlhttp.open("POST", "../actions/EsqueciMinhaSenha.php", true);	
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
		xmlhttp.setRequestHeader("Pragma", "no-cache");
		xmlhttp.onreadystatechange = function (){
				if (xmlhttp.readyState == 1) 
					$("#cxRespErro").html("<center><font size='2'>Aguarde processando as informações!</font></center>");
				if (xmlhttp.readyState == 4) {
					if (xmlhttp.status == 200) {
					//	alert(xmlhttp.responseText)
						$("#cxRespErro").html('<center><font size="2">'+xmlhttp.responseText+'</font></center>');
					} else {
					   alert("Problemas ao carregar o arquivo.");
					}
				}
		};
		xmlhttp.send("email="+document.getElementById("email").value);
	}
}

function alteraEmailCliente(){
	var email_atual = $("#email_atual").val();
	var novo_email 	= $("#novo_email").val();
	var senha_atual	= $("#senha_atual").val();
	
	limpaVarAut(); // Limpa as divs e seus retornos 
	
	if (email_atual.length == 0) {
	  alert(CkLoginALERTcmpemail);
	  $("#email_atual").focus();
	  return false;
	}
	
	if (!checkMail(email_atual)) {
	  alert(CkLoginALERTcmpemailinc); 
	  $("#email_atual").focus();
	  return false;
	}

	if (novo_email.length == 0) {
	  alert(CkLoginALERTcmpNewEmail);
	  $("#novo_email").focus();
	  return false;
	}
	
	if (!checkMail(novo_email)) {
	  alert(CkLoginALERTcmpemailinc); 
	  $("#novo_email").focus();
	  return false;
	}

	// Caso seja queira se logar 
	if (senha_atual.length == 0) {
	  alert(VldPwdALERTcamposenha);
	  $("#senha_atual").focus();
	  return false;
	}
	
	$("#cxAutUserErroLogin").css("background","#FFFF99");
	$("#cxAutUserErroLogin").css("color","#CC0000");
	$("#cxAutUserErroLogin").css("height","18px");

	// Inicia a consulta no servidor 
	xmlhttp = checkNavegador();
	xmlhttp.open("POST", "../actions/AlterarMyInfo.php", true);	
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
	xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
	xmlhttp.setRequestHeader("Pragma", "no-cache");
	xmlhttp.onreadystatechange = function (){
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200) {
					$("#cxAutUserErroLogin").html(xmlhttp.responseText);
				} else {
				   alert("Problemas ao carregar o arquivo.");
				}
			}
	};
	xmlhttp.send("act=1&email_atual="+email_atual+"&novo_email="+novo_email+"&senha_atual="+senha_atual);
}


function alterarSenhaCliente(){
	var senha_atual 	= $("#senha_atual").val();
	var nova_senha 		= $("#nova_senha").val();
	var conf_nova_senha	= $("#conf_nova_senha").val();
	
	limpaVarAut(); // Limpa as divs e seus retornos 
	
	if (senha_atual.length == 0) {
	  alert(VldTrocaPwdALERTcamposenhaatual);
	  $("#senha_atual").focus();
	  return false;
	}
	
	if (nova_senha.length == 0) {
	  alert(VldPwdALERTcampoNovaSenha);
	  $("#nova_senha").focus();
	  return false;
	}

// Caso seja queira se logar 
	if (conf_nova_senha.length == 0) {
	  alert(VldTrocaPwdALERTcampoConfSenha);
	  $("#conf_nova_senha").focus();
	  return false;
	}
	
	// Caso seja queira se logar 
	if (nova_senha != conf_nova_senha) {
	  alert(VldTrocaPwdALERTsenhadif);
	  $("#conf_nova_senha").focus();
	  return false;
	}

	$("#cxAutUserErroLogin").css("background","#FFFF99");
	$("#cxAutUserErroLogin").css("color","#CC0000");
	$("#cxAutUserErroLogin").css("height","18px");

	// Inicia a consulta no servidor 
	xmlhttp = checkNavegador();
	xmlhttp.open("POST", "../actions/AlterarMyInfo.php", true);	
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
	xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
	xmlhttp.setRequestHeader("Pragma", "no-cache");
	xmlhttp.onreadystatechange = function (){
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200) {
					$("#cxAutUserErroLogin").html(xmlhttp.responseText);
					$("#senha_atual").val("");
					$("#nova_senha").val("");
					$("#conf_nova_senha").val("");
				} else {
				   alert("Problemas ao carregar o arquivo.");
				}
			}
	};
	xmlhttp.send("act=2&senha_atual="+senha_atual+"&nova_senha="+nova_senha);
}
