﻿var CodCidade = 0;
var DivCadastro = gE("DivCadastro");

var DivClubeAmigo = gE("DivClubeAmigo");
if (DivClubeAmigo != null) {gE("Origem").value = DivClubeAmigo.innerHTML;}

var DivEmail = gE("DivEmail");
if (DivEmail != null) {gE("Email").value = DivEmail.innerHTML;}

function gE(id){return document.getElementById(id)}

function enviar(){
    var form = new Form();
    form.Form = gE("FormCadastro");
	form.PostUrl = "Usuario/Services/InserirAtualizar.aspx";
	form.callback = callback;

	if (gE("CEP").value == "00000-000")
	    gE("CEP").value = "";

	form.AdicionarValidacao("Perfil", "Preencha o campo \"Perfil\".", "vazio");
	if ((gE("Perfil").value == "0" || gE("Perfil").value == "2" || gE("Perfil").value == "5") && (gE("Crea").value == ""))
	{ alert("Preencha o campo \"CREA/ADB\"."); return false; }
	form.AdicionarValidacao("Nome", "Preencha o campo \"Nome\".", "vazio");
	form.AdicionarValidacao("Sobrenome", "Preencha o campo \"Sobrenome\".", "vazio");
	form.AdicionarValidacao("Email", "Preencha o campo \"E-mail\" com um endereço válido.", "email");
	form.AdicionarValidacao("Sexo", "Preencha o campo \"Sexo\".", "vazio");
	form.AdicionarValidacao("Nascimento", "Preencha o campo \"Nascimento\" com uma data válida.", "data");
	form.AdicionarValidacao("Cpf", "Preencha o campo \"CPF\" com um número válido.", "cpf");
	form.AdicionarValidacao("Endereco", "Preencha o campo \"Endereço\".", "vazio");
	form.AdicionarValidacao("Numero", "Preencha o campo \"Número\".", "vazio");
	form.AdicionarValidacao("CEP", "Preencha o campo \"CEP\" com o formato \"00000-000\".", "cep");
	form.AdicionarValidacao("CodEstado", "Preencha o campo \"Estado\".", "vazio");
	form.AdicionarValidacao("CodCidade", "Preencha o campo \"Cidade\".", "vazio");
	form.AdicionarValidacao("DddTelefone", "Preencha o campo \" DDD do Telefone\".", "vazio");
	form.AdicionarValidacao("Telefone", "Preencha o campo \"Telefone\".", "vazio");
               
	if (form.DoPost())
	    gE("btEnviarCadastro").disabled = true;
    else if (gE("Nascimento").value == "")
        gE("Nascimento").value = "(dd/mm/aaaa)";
}

function enviarBlogs() {
    var form = new Form();
    form.Form = gE("FormCadastro");
	form.PostUrl = "Usuario/Services/InserirAtualizarBlogs.aspx";
	form.callback = callback;
	
    if (gE("OptInAceito").checked == false){
        alert("Para participar da promoção você precisa aceitar o contrato.");
        return;
    } 
              
	if (form.DoPost())
	    gE("btEnviarCadastro").disabled = true;
}	
function enviaColorTest() {
    var formCT = new Form();
    formCT.Form = gE("FormCadastro");
	formCT.PostUrl = "ColorTest/Services/InserirAtualizar.aspx";
	formCT.callback = callbackCT;
	
	if (formCT.DoPost())
	    gE("btEnviarCadastro").disabled = true;
}
function limparCampos(){
    if (gE("Nome") != null) 
        gE("Nome").value = "";
    if (gE("Sobrenome") != null)
        gE("Sobrenome").value = "";
    if (gE("Email") != null)
        gE("Email").value = "";
    if(document.forms["FormCadastro"].Sexo[0] != null)
        document.forms["FormCadastro"].Sexo[0].checked = false;
    if(document.forms["FormCadastro"].Sexo[1] != null)
        document.forms["FormCadastro"].Sexo[1].checked = false;
    if (gE("Nascimento") != null)
        gE("Nascimento").value = "dd/mm/aaaa";
    if (gE("Rg") != null)
        gE("Rg").value = "";
    if (gE("Cpf") != null)
	    gE("Cpf").value = "00000000000";        
    if (gE("Endereco") != null)
        gE("Endereco").value = "";
    if (gE("Numero") != null)
        gE("Numero").value = "";
    if (gE("Complemento") != null)
        gE("Complemento").value = "";
    if (gE("CodEstado") != null)
        gE("CodEstado").selectedIndex = 0;
    if (gE("CodCidade") != null)
        gE("CodCidade").selectedIndex = 0;
    if (gE("DddTelefone") != null)
        gE("DddTelefone").value = "";
    if (gE("Telefone") != null)
        gE("Telefone").value = "";
    if (gE("DddCelular") != null)
        gE("DddCelular").value = "";
    if (gE("Celular") != null)
        gE("Celular").value = "";
    if (gE("Perfil") != null)
        gE("Perfil").selectedIndex = 0;
    if (gE("OptInInternet") != null)
        gE("OptInInternet").checked = true;
    if (gE("OptInCelular") != null)
        gE("OptInCelular").checked = true;
    if (gE("OptInCorreio") != null)
        gE("OptInCorreio").checked = true;
}
function voltar() {
    gE("FormCadastro").style.display = "block";
	gE("divRespostaCadastro").style.display = "none";
	gE("SpanRespostaCadastro").innerHTML = "";
}

function PopularCidade(ComboEstado) {
    var formCidade = new Form();
    formCidade.Form = gE("FormCadastro");
    formCidade.PostUrl = "Usuario/Services/Cidades.aspx?CodEstado=" + ComboEstado.options[ComboEstado.selectedIndex].value + "&crm=1";

    var combo = gE("CodCidade");
    for (var i = combo.options.length - 1; i >= 0; i--)
        combo.options[i] = null;
    var opt = new Option("Aguarde...", "");
    combo.options[combo.options.length] = opt;
	
	formCidade.callback = callbackCidade;
	formCidade.DoPost();
}

function callbackCidade() {
    if (http.readyState == 4) {
        var combo = gE("CodCidade");
        for(var i=combo.options.length-1;i>=0;i--)
            combo.options[i] = null;
        var cidades = http.responseXML.getElementsByTagName("Cidade");        
        combo.options[0] = new Option("Selecione","");
        for(var i=0;i<cidades.length;i++) {
            var opt = new Option(cidades[i].childNodes[0].nodeValue,cidades[i].attributes[0].value);
            
            if (opt.value == CodCidade)
                opt.selected = true;
            combo.options[combo.options.length] = opt;
        }
    }
}
function callbackCT() {
	if (http.readyState == 4) {
		var xml = http.responseXML;
		if (xml.getElementsByTagName("Erro").length > 0) {
		    alert("Atenção. "+ xml.getElementsByTagName("Erro")[0].childNodes[0].nodeValue);
		} else if (xml.getElementsByTagName("ErroSistema").length > 0) {
		    alert("Ocorreu um erro no sistema, contate o administrador e forneça a seguinte mensagem: \""+ xml.getElementsByTagName("ErroSistema")[0].childNodes[0].nodeValue +"\"");
		} else {
			gE("FormCadastro").style.display = "none";
			gE("lblDesc").style.display = "none";
    		gE("divRespostaCadastro").style.display = "block";
		
		    gE("rEndereco").innerHTML = gE("Endereco").value + " " + gE("Complemento").value;
		    gE("rCidade").innerHTML = gE("CodCidade").options[gE("CodCidade").selectedIndex].text;
		    gE("rEstado").innerHTML = gE("CodEstado").options[gE("CodEstado").selectedIndex].text;
		    gE("rCEP").innerHTML = gE("CEP").value;			
		    limparCampos();
		}
		gE("btEnviarCadastro").disabled = false;
	}
}
function callback() {
	if (http.readyState == 4) {
		var xml = http.responseXML;
		if (xml.getElementsByTagName("Erro").length > 0) {
            alert("Atenção. "+ xml.getElementsByTagName("Erro")[0].childNodes[0].nodeValue);
		} else if (xml.getElementsByTagName("ErroSistema").length > 0) {
		    alert("Ocorreu um erro no sistema, contate o administrador e forneça a seguinte mensagem: \""+ xml.getElementsByTagName("ErroSistema")[0].childNodes[0].nodeValue +"\"");
		} else {
		    gE("FormCadastro").style.display = "none";
		    gE("divRespostaCadastro").style.display = "block";		
		    gE("SpanRespostaCadastro").innerHTML = xml.getElementsByTagName("Sucesso")[0].childNodes[0].nodeValue;
		    limparCampos();
		}
		gE("btEnviarCadastro").disabled = false;
	}
}
function callbackLoad() {
    if (http.readyState == 4) {
        if (http.responseXML.getElementsByTagName("CodEstado").length == 0)
            return;
        var CodEstado = http.responseXML.getElementsByTagName("CodEstado")[0].childNodes[0].nodeValue;
        CodCidade = http.responseXML.getElementsByTagName("CodCidade")[0].childNodes[0].nodeValue;
        var combo = gE("CodEstado");
        for(var i=0;i<combo.options.value;i++) {
            if (combo.options[i].value == CodEstado) {
                combo.selectedIndex = i;
                break;
            }
        }
        PopularCidade(gE("CodEstado"));
    }
}

function escolheProfissao() {
    if(document.getElementById("Perfil").value == 0 || document.getElementById("Perfil").value == 5 || document.getElementById("Perfil").value == 2) {
        document.getElementById("lbCrea").style.display = "";
        document.getElementById("Crea").style.display = "";
    }
    else {
        document.getElementById("lbCrea").style.display = "none";
        document.getElementById("Crea").style.display = "none";
    }
}

function mascaraNumero(obj,evento) {
    ascii = (evento.keyCode) ? evento.keyCode : evento.which;
    if(ascii == 8 || ascii == 9 || ascii == 13 || ascii == 116 || ascii == 37 || ascii == 39 || ascii == 36 || ascii == 35 || ascii == 46)
        return true;
    if (/\D/.test(String.fromCharCode(ascii)))
        return false;
    return true;
}