﻿function ObterHttp() {
	var http = false;
	try { http = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) {
		try { http = new ActiveXObject("Microsoft.XMLHTTP"); }
		catch (E) { http = false; }
	}
	if (!http && typeof XMLHttpRequest!='undefined') { http = new XMLHttpRequest(); }
	return http;
}

function PopularCidade(Combo,EstadoSel,CidadeSel) {
	var http = ObterHttp();	
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			Combo = document.getElementById(Combo);
			var Cidades = http.responseXML.getElementsByTagName("Cidade");

			for(var i=Combo.options.length-1;i>=0;i--)
				Combo.options[i] = null;
			
			Combo.options[Combo.options.length] = new Option("-- Selecione --","0");
			
			for(var i=0;i<Cidades.length;i++) {
				var opt = new Option(Cidades[i].getElementsByTagName("Nome")[0].childNodes[0].nodeValue,Cidades[i].getElementsByTagName("Cod")[0].childNodes[0].nodeValue);
				Combo.options[Combo.options.length] = opt;
				if(CidadeSel){
					if (Cidades[i].getElementsByTagName("Cod")[0].childNodes[0].nodeValue == CidadeSel)
						Combo.selectedIndex = i + 1;
				}
			}
		}
	}
	http.open("GET","services/Cidades.aspx?CodEstado=" + EstadoSel, true);
	http.send(null);
}

function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}
function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}
function cpf(v){
    v=v.replace(/\D/g,"")
    v=v.replace(/(\d{3})(\d)/,"$1.$2")
    v=v.replace(/(\d{3})(\d)/,"$1.$2")
    v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2")
    return v
}
function telefone(v){
    v=v.replace(/\D/g,"")
    v=v.replace(/^(\d\d)(\d)/g,"($1) $2")
    v=v.replace(/(\d{4})(\d)/,"$1-$2")
    return v
}
function cep(v){
    v=v.replace(/D/g,"")
    v=v.replace(/^(\d{5})(\d)/,"$1-$2")
    return v
}
function nascimento(v){
    v=v.replace(/\D/g,"")
    v=v.replace(/(\d{2})(\d)/,"$1/$2")
    v=v.replace(/(\d{2})(\d)/,"$1/$2")
	return v
}
function rg(v){
	v=v.replace(/\W/g,"")
    v=v.replace(/(\w{2})(\w)/,"$1.$2")
    v=v.replace(/(\w{3})(\w)/,"$1.$2")
    v=v.replace(/(\w{3})(\w)/,"$1-$2")
	return v
}
function soNumeros(v){
    return v.replace(/\D/g,"")
}

function ValidarSexo(source,arguments){
    var sexoFem = document.getElementById("rbSexoF").checked;
    var sexoMas = document.getElementById("rbSexoM").checked;
    
    if(sexoFem == false && sexoMas == false)
        arguments.IsValid = false;
    else
        arguments.IsValid = true;
}

function ValidarProjeto(source,arguments){
    var ProjetoS = document.getElementById("rbPossuiProjetoSPart4").checked;
    var ProjetoN = document.getElementById("rbPossuiProjetoNPart4").checked;
    
    if(ProjetoS == false && ProjetoN == false)
        arguments.IsValid = false;
    else
        arguments.IsValid = true;
}

function ValidaCPF(source,arguments){
    var cpf = arguments.Value;
    var multiplicador1 = [ 10, 9, 8, 7, 6, 5, 4, 3, 2 ];
    var multiplicador2 = [ 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 ];
    var tempCpf;
    var digito;
    var soma;
    var resto;

    cpf = cpf.replace(/^\s*/, "").replace(/\s*$/, "");
    cpf = cpf.replace(".", "");
    cpf = cpf.replace("-", "");
    cpf = cpf.replace(".", "");

    if(cpf.length != 11 || cpf == "00000000000")
        arguments.IsValid = false;

    tempCpf = cpf.substring(0, 9);
    tempCpf = tempCpf.split("");
    
    soma = 0;
    for (var i = 0; i < 9; i++)
        soma += parseInt(tempCpf[i]) * multiplicador1[i];

    resto = soma % 11;
    if (resto < 2) resto = 0;
    else resto = 11 - resto;

    digito = resto;
    tempCpf[9] = digito;

    soma = 0;
    for (var j = 0; j < 10; j++)
        soma += parseInt(tempCpf[j]) * multiplicador2[j];

    resto = soma % 11;
    if (resto < 2) resto = 0;
    else resto = 11 - resto;

    digito = String(digito) + String(resto);

    if(cpf.substring(9) != digito)
        arguments.IsValid = false;
}

function ValidaCPFClientSide(cpf){
    var multiplicador1 = [ 10, 9, 8, 7, 6, 5, 4, 3, 2 ];
    var multiplicador2 = [ 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 ];
    var tempCpf;
    var digito;
    var soma;
    var resto;

    cpf = cpf.replace(/^\s*/, "").replace(/\s*$/, "");
    cpf = cpf.replace(".", "");
    cpf = cpf.replace("-", "");
    cpf = cpf.replace(".", "");

    if(cpf.length != 11 || cpf == "00000000000")
        return false;

    tempCpf = cpf.substring(0, 9);
    tempCpf = tempCpf.split("");
    
    soma = 0;
    for (var i = 0; i < 9; i++)
        soma += parseInt(tempCpf[i]) * multiplicador1[i];

    resto = soma % 11;
    if (resto < 2) resto = 0;
    else resto = 11 - resto;

    digito = resto;
    tempCpf[9] = digito;

    soma = 0;
    for (var j = 0; j < 10; j++)
        soma += parseInt(tempCpf[j]) * multiplicador2[j];

    resto = soma % 11;
    if (resto < 2) resto = 0;
    else resto = 11 - resto;

    digito = String(digito) + String(resto);

    if(cpf.substring(9) != digito)
        return false;
}

function ValidaTermos(source,arguments){
    var checkBoxTermo = document.getElementById("cbTermo").checked;
    var checkBoxRegulamento = document.getElementById("cbRegulamento").checked;
    var checkBoxDeclaracao = document.getElementById("cbDeclaracao").checked;
    
    if(checkBoxTermo == false || checkBoxRegulamento == false || checkBoxDeclaracao == false)
        arguments.IsValid = false;
}

function ValidaCombo(source,arguments){

    if(document.getElementById("rbPossuiProjetoNPart4").checked)
    {
        arguments.IsValid = true;
        return;
    }        
    
    var comboSelecionado = arguments.Value;
    if(comboSelecionado == "0")
        arguments.IsValid = false;
}

function ValidaGrupo(source,arguments){

    if(document.getElementById("rbPossuiProjetoNPart4").checked)
    {
        arguments.IsValid = true;
        return;
    }    
    if(document.getElementById("txtNomeGrupo").value == "")
        arguments.IsValid = false;
    else
        arguments.IsValid = true;
}

function ValidaInstituicao(source,arguments){

    if(document.getElementById("rbPossuiProjetoNPart4").checked)
    {
        arguments.IsValid = true;
        return;
    }    
    if(document.getElementById("txtInstituicao").value == "")
        arguments.IsValid = false;
    else
        arguments.IsValid = true;
}

function ValidaReponsavel(source,arguments){
    var idadeDigitada = document.getElementById("txtNascimento").value;
    
    if(idadeDigitada.length > 9){
        var idade = VerificaIdade(idadeDigitada);
        
        if(idade == true){
            document.getElementById("boxResponsavel").style.display = "none";
        }else{
            document.getElementById("boxResponsavel").style.display = "block";
            if(document.getElementById("txtResponsavel").value == "")
                arguments.IsValid = false;
        }
    }
}

function ValidaReponsavelClientSide(id){
    var idadeDigitada = document.getElementById("txtNascimentoPart" + id).value;
    
    if(idadeDigitada.length > 9){
        var idade = VerificaIdade(idadeDigitada);
        
        if(idade == true){
            document.getElementById("boxResponsavelPart" + id).style.display = "none";
        }else{
            document.getElementById("boxResponsavelPart" + id).style.display = "block";
            if(document.getElementById("txtResponsavelPart" + id).value == "")
                return false;
        }
    }
}

function VerificaIdade(idade){
    var dataAtual = new Date();
    var diaAtual = dataAtual.getDate().toString();
        diaAtual = (diaAtual.length == 1) ? "0" + diaAtual : diaAtual;
    var mesAtual = (dataAtual.getMonth() + 1).toString();
        mesAtual = (mesAtual.length == 1) ? "0" + mesAtual : mesAtual;
    var anoAtual = dataAtual.getFullYear();

    var splitIdade = idade.split("/");
    
    var dataAtual = anoAtual + mesAtual + diaAtual;
    var dataPessoa = splitIdade[2] + splitIdade[1] + splitIdade[0];
    var idade = parseInt(dataAtual) - parseInt(dataPessoa);

    if(idade > 180000) return true;
    else return false;
}

function montaParticipantes(valor){
    for(var i = 1; i < 5; i++)
        document.getElementById("boxPart" + i).style.display = "none";
    
    for(var j = 1; j <= valor; j++)
        document.getElementById("boxPart" + j).style.display = "block";
}

function openFormPart(id){
    document.getElementById("formPart" + id).style.display = "block";
}

function validaParticipantes(form){
    QtdParticipantes = document.getElementById("ddlParticipantes").value;
    var msnErro = new Array("","","","","");
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    var erData = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;

    for(var i = 1; i <= QtdParticipantes; i++){
        ge("imgNome" + i).style.display = "none";
        ge("imgSobrenome" + i).style.display = "none";
        ge("imgEmail" + i).style.display = "none";
        ge("imgEmail" + i).style.display = "none";
        ge("imgSexo" + i).style.display = "none";
        ge("imgResponsavel" + i).style.display = "none";
        ge("imgNascimento" + i).style.display = "none";
        ge("imgResponsavel" + i).style.display = "none";
        ge("imgRG" + i).style.display = "none";
        ge("imgCPF" + i).style.display = "none";
        ge("imgCPF" + i).style.display = "none";
        ge("imgMatricula" + i).style.display = "none";
        ge("imgEndereco" + i).style.display = "none";
        ge("imgNumero" + i).style.display = "none";
        ge("imgCEP" + i).style.display = "none";
        ge("imgEstado" + i).style.display = "none";
        ge("imgCidade" + i).style.display = "none";
        ge("imgPart" + i).style.display = "none";
    }
    
    for(var i = 1; i <= QtdParticipantes; i++){
        if(ge("txtNomePart" + i).value == ""){  msnErro[i] = msnErro[i] + "<br />- Inserir o nome do Participante"; ge("imgNome" + i).style.display = "inline"; }
        if(ge("txtSobrenomePart" + i).value == ""){ msnErro[i] = msnErro[i] + "<br />- Inserir o sobrenome do Participante"; ge("imgSobrenome" + i).style.display = "inline"; }
        if(ge("txtEmailPart" + i).value == ""){ msnErro[i] = msnErro[i] + "<br />- Inserir o email do Participante"; ge("imgEmail" + i).style.display = "inline"; }
	        else if(!er.test(ge("txtEmailPart" + i).value)){    msnErro[i] = msnErro[i] + "<br />- Email incorreto"; ge("imgEmail" + i).style.display = "inline"; }
        if(ge("rbSexoMPart" + i).checked == false && ge("rbSexoFPart" + i).checked == false){  msnErro[i] = msnErro[i] + "<br />- Escolha o sexo do Participante"; ge("imgSexo" + i).style.display = "inline"; }
        if(ge("txtNascimentoPart" + i).value == ""){    msnErro[i] = msnErro[i] + "<br />- Inserir o nascimento do Participante"; ge("imgNascimento" + i).style.display = "inline"; }
            else if(!erData.test(ge("txtNascimentoPart" + i).value)) { msnErro[i] = msnErro[i] + "<br />- Inserir data correta"; ge("imgNascimento" + i).style.display = "inline"; }
            else if(ValidaReponsavelClientSide(i) == false){  msnErro[i] = msnErro[i] + "<br />- Coloque o nome do responsável"; ge("imgResponsavel" + i).style.display = "inline"; }
        if(ge("txtRGPart" + i).value == ""){    msnErro[i] = msnErro[i] + "<br />- Inserir o RG do Participante"; ge("imgRG" + i).style.display = "inline"; }
        if(ge("txtCPFPart" + i).value == ""){   msnErro[i] = msnErro[i] + "<br />- Inserir o CPF do Participante"; ge("imgCPF" + i).style.display = "inline"; }
            else if(ValidaCPFClientSide(ge("txtCPFPart" + i).value) == false){  msnErro[i] = msnErro[i] + "<br />- CPF Inválido"; ge("imgCPF" + i).style.display = "inline"; }
        if(ge("txtMatriculaPart" + i).value == ""){ msnErro[i] = msnErro[i] + "<br />- Inserir a matrícula do Participante"; ge("imgMatricula" + i).style.display = "inline"; }
        if(ge("txtEnderecoPart" + i).value == ""){  msnErro[i] = msnErro[i] + "<br />- Inserir o endereço do Participante"; ge("imgEndereco" + i).style.display = "inline"; }
        if(ge("txtNumeroPart" + i).value == ""){    msnErro[i] = msnErro[i] + "<br />- Inserir o número do endereço do Participante"; ge("imgNumero" + i).style.display = "inline"; }
        if(ge("txtCEPPart" + i).value == ""){   msnErro[i] = msnErro[i] + "<br />- Inserir o CEP do Participante"; ge("imgCEP" + i).style.display = "inline"; }
        if(ge("ddlEstadoPart" + i).value == "0"){   msnErro[i] = msnErro[i] + "<br />- Selecione o estado do Participante"; ge("imgEstado" + i).style.display = "inline"; }
        if(ge("ddlCidadePart" + i).value == "0"){   msnErro[i] = msnErro[i] + "<br />- Selecione a cidade do Participante"; ge("imgCidade" + i).style.display = "inline"; }
    
        document.getElementById("mnsErroPart" + i).innerHTML = msnErro[i];
        if(msnErro[i] != "") ge("imgPart" + i).style.display = "inline";
    }

    if(msnErro[1] == "" && msnErro[2] == "" && msnErro[3] == "" && msnErro[4] == "") return true;
    else return false;
}

function ge(id){
    return document.getElementById(id);
}