﻿function validaForm() {
	var form = document.forms["aspnetForm"];

	var intCountForm=0;
	
	arrForms['FORM_OBJECT'][0] = 'document.aspnetForm';
	var objForm=arrForms['FORM_OBJECT'][0];
	
	arrForms['FORM_FIELDS'][0] = new Array();
	arrForms['FORM_FIELDS_TYPE'][0] = new Array();
	arrForms['FORM_ERROR_MESSAGES'][0] = new Array();
	
	arrForms['FORM_FIELDS'][0][intCountForm] = 'nome';
	arrForms['FORM_FIELDS_TYPE'][0][intCountForm] = 'text';
	arrForms['FORM_ERROR_MESSAGES'][0][intCountForm] = 'Nome.';
	
	intCountForm++;
	arrForms['FORM_FIELDS'][0][intCountForm] = 'sexo';
	arrForms['FORM_FIELDS_TYPE'][0][intCountForm] = 'checked';
	arrForms['FORM_ERROR_MESSAGES'][0][intCountForm] = 'Sexo.';
	
	/*
	intCountForm++;
	arrForms['FORM_FIELDS'][0][intCountForm] = 'ctl00_cphConteudo_estado';
	arrForms['FORM_FIELDS_TYPE'][0][intCountForm] = 'selected';
	arrForms['FORM_ERROR_MESSAGES'][0][intCountForm] = 'Estado.';
	
	intCountForm++;
	arrForms['FORM_FIELDS'][0][intCountForm] = 'cidade';
	arrForms['FORM_FIELDS_TYPE'][0][intCountForm] = 'selected';
	arrForms['FORM_ERROR_MESSAGES'][0][intCountForm] = 'Cidade.';
	*/

	intCountForm++;
	arrForms['FORM_FIELDS'][0][intCountForm] = 'email';
	arrForms['FORM_FIELDS_TYPE'][0][intCountForm] = 'email';
	arrForms['FORM_ERROR_MESSAGES'][0][intCountForm] = 'E-mail.';
	
	intCountForm++;
	arrForms['FORM_FIELDS'][0][intCountForm] = 'cpf';
	arrForms['FORM_FIELDS_TYPE'][0][intCountForm] = 'cpf';
	arrForms['FORM_ERROR_MESSAGES'][0][intCountForm] = 'CPF.';
	

	intCountForm++;
	arrForms['FORM_FIELDS'][0][intCountForm] = 'nickname';
	arrForms['FORM_FIELDS_TYPE'][0][intCountForm] = 'text';
	arrForms['FORM_ERROR_MESSAGES'][0][intCountForm] = 'Nickname.';
	
	intCountForm++;
	arrForms['FORM_FIELDS'][0][intCountForm] = 'senha';
	arrForms['FORM_FIELDS_TYPE'][0][intCountForm] = 'text';
	arrForms['FORM_ERROR_MESSAGES'][0][intCountForm] = 'Senha.';			
	

	intCountForm++;
	arrForms['FORM_FIELDS'][0][intCountForm] = 'termos_uso';
	arrForms['FORM_FIELDS_TYPE'][0][intCountForm] = 'checked';
	arrForms['FORM_ERROR_MESSAGES'][0][intCountForm] = 'Termos de Uso.';

	
	if (checkForm(0)){
        http = getHTTPObject();

        if (form.senha.value != form.confirmar_senha.value){
            alert("A senha não foi confirmada corretamente.");
            return;
        }

        if (form.cpf.value != '' && strNomeUsuario == ""){
	        http.open("GET","../interfaces/checkCpf.aspx?txtCpf="+ form.cpf.value,false);
	        http.send(null);
	        var feedback = parseInt(http.responseText);
	        if (feedback == 1) {
		        alert("O CPF preenchido já existe em nosso cadastro.");
		        return;
	        }
	    }

        if (form.email.value != '' && strNomeUsuario == ""){
	        http.open("GET","../interfaces/buscaPorEmail.aspx?txtEmail="+ form.email.value,false);
	        http.send(null);
	        var feedback = parseInt(http.responseText);
	        if (feedback == 1) {
		        alert("O e-mail preenchido já existe em nosso cadastro.");
		        return;
	        }
	    }

	    var strPost = "";
	    for(var i=0;i<form.elements.length;i++) {
		    if (form.elements[i].type == "checkbox" || form.elements[i].type == "radio") {
			    if (!form.elements[i].checked)
				    continue;
		    }else if(form.elements[i].name.substring(0,2) == "__"){
		        continue;
		    }
		    if (form.elements[i].name.indexOf("estado") >= 0)
		        strPost += "estado="+ escape(form.elements[i].value) +"&";
		    else 
		        strPost += form.elements[i].name +"="+ escape(form.elements[i].value) +"&";
	    }

        http.open("POST", "../interfaces/cadastro.aspx", false);
	    http.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
        http.send(strPost);
        
        if (http.readyState == 4) {
		    var feedback = http.responseText;
		    document.getElementById("boxFeedback").style.display = "none";

		    var msg = "";
		    var img = "";
		    switch(feedback) {
			    case "0":
				    msg = "Não foi possível efetuar o seu cadastro.<br>Por favor, tente novamente mais tarde.";
				    img = "../images/img_top_erro.gif";
				    break;
				case "5":
				    msg = "Parabéns! Seu cadastro foi atualizado com<br /> sucesso!";
				    img = "../images/img_top_confirmacao.gif";
				    break;
			    default:
				    msg = "Parabéns! Seu cadastro foi efetuado com<br /> sucesso!";
				    img = "../images/img_top_confirmacao.gif";
		    }
		    document.getElementById("masterCadastro").style.display = "none";
		    document.getElementById("boxFeedback").style.display = "";
		    document.getElementById("MsgRetorno").innerHTML = msg;
		    document.getElementById("imgFeedback").src = img;
		    //
		    acessarTagPredicta('hellmanns_cadastro_feedback');
        }
    }
}
var isIE;
function LoadUsuario() {
    url = "../interfaces/DadosUsuario.aspx";
    if (window.ActiveXObject){
        isIE = true;
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async=false;
        xmlDoc.load(url);
        PopularForm();
    }
    else if (document.implementation && document.implementation.createDocument){
        xmlDoc=document.implementation.createDocument("","",null);
        xmlDoc.load(url);
        xmlDoc.onload = PopularForm;
        isIE = false;
    }
}

function PopularForm() {
    var xml = xmlDoc.getElementsByTagName("Cliente");
    xml = xml[0];
    
    document.getElementById("nome").value = xml.getElementsByTagName("Nome")[0].childNodes[0].nodeValue;
    document.getElementById("dia").value = xml.getElementsByTagName("DiaNascimento")[0].childNodes[0].nodeValue;
    document.getElementById("mes").value = xml.getElementsByTagName("MesNascimento")[0].childNodes[0].nodeValue;
    document.getElementById("ano").value = xml.getElementsByTagName("AnoNascimento")[0].childNodes[0].nodeValue;
    
    document.getElementById(xml.getElementsByTagName("Sexo")[0].childNodes[0].nodeValue).checked = true;
    
    var CodEstado = xml.getElementsByTagName("Estado")[0].childNodes[0].nodeValue;
    var comboEstado = document.getElementById("ctl00_cphConteudo_estado");
    for(var i=0;i<comboEstado.options.length;i++) {
        if (comboEstado.options[i].value == CodEstado) {
            comboEstado.selectedIndex = i;
            break;
        }
    }
    Selecionar();
    
    var CodCidade = xml.getElementsByTagName("Cidade")[0].childNodes[0].nodeValue;
    var comboCidade = document.getElementById("cidade");
    for(var i=0;i<comboCidade.options.length;i++) {
        if (comboCidade.options[i].value == CodCidade) {
            comboCidade.selectedIndex = i;
            break;
        }
    }
    
    document.getElementById("email").value = xml.getElementsByTagName("Email")[0].childNodes[0].nodeValue;
    document.getElementById("cpf").value = xml.getElementsByTagName("Cpf")[0].childNodes[0].nodeValue;
    document.getElementById("nickname").value = xml.getElementsByTagName("Nickname")[0].childNodes[0].nodeValue;
    document.getElementById("newsEmail").checked = xml.getElementsByTagName("OptEmail")[0].childNodes[0].nodeValue == "S" ? true : false;
    document.getElementById("termos_uso").checked = false;
}