﻿function ValidaFormulario2(Form, IndexCampoValida, Executar) {
    var frm = document.forms[Form].elements;
    array = IndexCampoValida.split(',');
    var Verifica = 0;

    for (i = 0; i < array.length; i++) {
        var Index = array[i];
        //alert(frm[Index].id+" "+Index+" "+frm[Index].type+" "+frm[Index].name);        
        if (frm[Index].type == "select-one") {
            if (frm[Index].value == 0 || frm[Index].selectedIndex == 0) {
                frm[Index].className = 'fundo_dropdownlist_erro';
                Verifica++;
            }
            else {
                frm[Index].className = 'fundo_dropdownlist_normal';
            }
        }
        else {
            if (frm[Index].value == "") {
                frm[Index].className = 'normal';
                Verifica++;
            }
            else {
                frm[Index].className = 'anormal';
            }
        }
    }

    if (Verifica != 0) {
        Alerta('Os Campos em Vermelhos são de Preenchimento Obrigatórios!');
        return false;
    }
    if (Verifica == 0) {
        if (document.getElementById(Executar).disabled == 1) {
            document.getElementById(Executar).disabled = 0;
        }

        document.getElementById(Executar).click();
        return true;
    }
}
function ValidaFormulario(Form, IndexCampoValida, Executar) {
    var frm = document.forms[Form].elements;
    array = IndexCampoValida.split(',');
    var Verifica = 0;
    var Cph = 'ctl00$cphPrincipal$';

    for (i = 0; i < array.length; i++) {
        var Index = Cph + array[i];
        //alert(frm[Index].id+" "+Index+" "+frm[Index].type+" "+frm[Index].name);        
        if (frm[Index].type == "select-one") {
            if (frm[Index].value == 0 || frm[Index].selectedIndex == 0) {
                frm[Index].className = 'fundo_dropdownlist_erro';
                Verifica++;
            }
            else {
                frm[Index].className = 'fundo_dropdownlist_normal';
            }
        }
        else {
            if (frm[Index].value == "") {
                frm[Index].className = 'normal';
                Verifica++;
            }
            else {
                frm[Index].className = 'anormal';
            }
        }
    }

    if (Verifica != 0) {
        Alerta('Os Campos em Vermelhos são de Preenchimento Obrigatórios!');
        return false;
    }
    if (Verifica == 0) {
        if (document.getElementById(Executar).disabled == 1) {
            document.getElementById(Executar).disabled = 0;
        }

        document.getElementById(Executar).click();
        return true;
    }
}
function SetFocus(txtCampo)//seta o campo e limpa
{
    document.getElementById(txtCampo).value = "";
    return document.getElementById(txtCampo).focus();
}
//function SoNumeros()//Aceita apenas números
//{    
//    var tecla = window.event.keyCode;
//    var teclaAux = String.fromCharCode(tecla);
//    if (!((teclaAux >= "0") && (teclaAux <= "9"))) {
//        if (tecla != "13") {
//            window.event.keyCode = 0;
//        }
//    }
//}


function SoNumeros(event)//Aceita apenas números
{
    var tecla;
     
    tecla = (window.event) ? event.keyCode : event.which;
    
    var teclaAux = String.fromCharCode(tecla);

    if (!((teclaAux >= 0) && (teclaAux <= 9)) || teclaAux == ' ') {
        if (tecla == 13 || tecla == 8) {
            return true;
        } else {

            if (window.event) {
                window.event.keyCode = 0;

            }
            event.returnValue = false;
            return false;
        }
    }
}



function SoValores()//Aceita apenas Valores
{
    var tecla = window.event.keyCode;
    tecla = String.fromCharCode(tecla);
    if (!((tecla >= "0") && (tecla <= "9") || (tecla == ",") || (tecla == "."))) {
        window.event.keyCode = 0;
    }
}


//Função para validar caracteres de RG
function ValidaRg() {
    var tecla = window.event.keyCode;
    tecla = String.fromCharCode(tecla);
    if (!((tecla >= "0") && (tecla <= "9") || (tecla == "-") || (tecla == ".") || (tecla.toUpperCase() == "X"))) {
        window.event.keyCode = 0;
    }

}

function MaskCNPJ(txtCampo)//passa o campo CNPJ(this)
{
    var tecla = window.event.keyCode;
    if (!(tecla >= 45 && tecla <= 57)) {
        return false;
    }
    if (tecla != "8" && tecla != "37" && tecla != "39") {
        txtCampo.value = txtCampo.value.replace(/\D/g, "").replace(/(\d{2})(\d)/, "$1.$2").replace(/(\d{3})(\d)/, "$1.$2").replace(/\.(\d{3})(\d)/, ".$1/$2").replace(/(\d{3})(\d{1,2})$/, "$1-$2");
        if (txtCampo.value.length >= 18) {
            txtCampo.value = txtCampo.value.substring(0, 18);
            return false;
        }
        else return txtCampo.value;
    }
}
function MaskCPF(txtCampo)//passa o campo CPF(this)//Não mexer... Ass.Mateus dia 31/08
{
    SoNumeros();
    var tecla = window.event.keyCode;
    if (!(tecla >= 45 && tecla <= 57)) {
        return false;
    }
    if (tecla != "8" && tecla != "37" && tecla != "39") {
        txtCampo.value = txtCampo.value.replace(/\D/g, "").replace(/(\d{3})(\d)/, "$1.$2").replace(/(\d{3})(\d)/, "$1.$2").replace(/(\d{3})(\d{1,2})$/, "$1-$2");
        if (txtCampo.value.length >= 14) {
            txtCampo.value = txtCampo.value.substring(0, 13);
            return false;
        }
        else return txtCampo.value;
    }
}
function CPFCNPJ(txtCampo, rdbCNPJ)//passa o campo documento(this) e campo RadioButton
{
    var tecla = window.event.keyCode;
    if (!(tecla >= 45 && tecla <= 57)) {
        return false;
    }
    if ((rdbCNPJ.checked) && (tecla != "8" && tecla != "37" && tecla != "39")) {
        txtCampo.value = txtCampo.value.replace(/\D/g, "").replace(/(\d{2})(\d)/, "$1.$2").replace(/(\d{3})(\d)/, "$1.$2").replace(/\.(\d{3})(\d)/, ".$1/$2").replace(/(\d{3})(\d{1,2})$/, "$1-$2");
        if (txtCampo.value.length >= 18) {
            txtCampo.value = txtCampo.value.substring(0, 18);
            return false;
        }
        else return txtCampo.value;
    }
    else if (tecla != "8" && tecla != "37" && tecla != "39") {
        txtCampo.value = txtCampo.value.replace(/\D/g, "").replace(/(\d{3})(\d)/, "$1.$2").replace(/(\d{3})(\d)/, "$1.$2").replace(/(\d{3})(\d{1,2})$/, "$1-$2");
        if (txtCampo.value.length >= 14) {
            txtCampo.value = txtCampo.value.substring(0, 14);
            return false;
        }
        else return txtCampo.value;
    }
}


//Função para Máscara de Campo Data
function MascaraData(ValCampo, NmCampo) {
    var data = "";
    data = data + ValCampo;
    var tecla = event.keyCode;

    tecla = String.fromCharCode(tecla);
    if (!((tecla >= "0") && (tecla <= "9"))) {
        window.event.keyCode = 0;
    }

    if (data.length == 2) {
        data = data + "/";
    }
    if (data.length == 5) {
        data = data + '/';
    }
    NmCampo.value = data.substr(0, 9);
}

function CamposPesquisasSeg(RadioNome, RadioCpf, RadioMatricula, CampoPesquisa) {
    var RdbNome = document.getElementById(RadioNome);
    var RdbCpf = document.getElementById(RadioCpf);
    var RdbMatricula = document.getElementById(RadioMatricula);
    var Campo = document.getElementById(CampoPesquisa);
    Campo.value = Campo.value.substring(0, 100);
    var tecla = event.keyCode;

    tecla = String.fromCharCode(tecla);

    if (RdbCpf.checked) {
        //MascaraCpf(Campo.value, Campo); 
        MaskCPF(Campo);
        //Campo.value = Campo.value.substring(0, 13);  

        /*if(Campo.value.length == 13)
        {                        
        Campo.value = Campo.value + tecla;            
        document.getElementById('ctl00_cphBotao_btnBuscaSegurado').focus();                        
        }*/
    }
}

function VerificaCpfCnpj() {
    var Verifica = document.getElementById('ctl00$cphPrincipal$TIPO');
    var Campo = document.getElementById('ctl00$cphPrincipal$txtDocumento');

    if (Verifica.value == 'rdbTipoCNPJ' && Verifica.checked) {
        MascaraCnpj(Campo.value, Campo);
    }
    else {
        MascaraCpf(Campo.value, Campo);
    }
}

function VerificaCaracterCpfCnpj() {
    var Verifica = document.getElementById('ctl00$cphPrincipal$TIPO');
    var Campo = document.getElementById('ctl00$cphPrincipal$txtDocumento');

    if (Verifica.value == 'rdbTipoCNPJ' && Verifica.checked) {
        if (Campo.value.length == 18) {
            ProximoCampo('ctl00$cphPrincipal$txtDocumento', 18, 'ctl00$cphPrincipal$txtNomeClinica', '');
        }
    }
    else {
        if (Campo.value.length == 14) {
            ValidaCpf(Campo, 'ctl00$cphPrincipal$txtNomeClinica')
        }
    }

    //if(this.value.length==14)  ValidaCpf(this, 'txtTel')
}

//Função para Máscara de Campo CPF
function MascaraCpf(ValCampo, NmCampo) {
    var Cpf = "";
    Cpf = Cpf + ValCampo;

    var tecla = event.keyCode;

    tecla = String.fromCharCode(tecla);
    if (!((tecla >= "0") && (tecla <= "9"))) {
        window.event.keyCode = 0;
    }

    if (Cpf.length == 3 || Cpf.length == 7) {
        Cpf = Cpf + ".";
    }
    if (Cpf.length == 11) {
        Cpf = Cpf + "-";
    }

    NmCampo.value = Cpf;
}

//Função para Máscara de Campo Cep
function MascaraCep(ValCampo, NmCampo) {
    var Cep = "";
    Cep = Cep + ValCampo;

    var tecla = event.keyCode;

    tecla = String.fromCharCode(tecla);
    if (!((tecla >= "0") && (tecla <= "9"))) {
        window.event.keyCode = 0;
    }

    if (Cep.length == 5) {
        Cep = Cep + '-';
    }
    NmCampo.value = Cep;
}

//Função para Máscara de Campo CNPJ
function MascaraCnpj(ValCampo, NmCampo) {
    var Cnpj = "";
    Cnpj = Cnpj + ValCampo;

    var tecla = event.keyCode;

    tecla = String.fromCharCode(tecla);
    if (!((tecla >= "0") && (tecla <= "9"))) {
        window.event.keyCode = 0;
    }

    if (Cnpj.length == 2 || Cnpj.length == 6) {
        Cnpj = Cnpj + ".";
    }
    if (Cnpj.length == 10) {
        Cnpj = Cnpj + "/";
    }
    if (Cnpj.length == 15) {
        Cnpj = Cnpj + "-";
    }
    NmCampo.value = Cnpj;
}

//Função para Máscara de Campo Telefone
function MascaraTel(ValCampo, NmCampo, DDD) {
    var Tel = "";
    Tel = Tel + ValCampo;

    var tecla = event.keyCode;

    tecla = String.fromCharCode(tecla);
    if (!((tecla >= "0") && (tecla <= "9"))) {
        window.event.keyCode = 0;
    }

    if (DDD != '') {
        if (Tel.length == 0) {
            Tel = Tel + "(";
        }
        document.getElementById('FlagDDD').value = '1';
    }
    else {
        if (Tel.length == 4) {
            Tel = Tel + "-";
        }
    }
    NmCampo.value = Tel;
}



//Função para Validar CPF
function ValidaCpf(Campo, Proximo) {

    var POSICAO, I, SOMA, DV, DV_INFORMADO;
    var DIGITO = new Array(10);
    DV_INFORMADO = Campo.value.substring(0, 3) + Campo.value.substring(4, 7) + Campo.value.substring(8, 11);

    for (I = 0; I <= 8; I++) {
        DIGITO[I] = DV_INFORMADO.substr(I, 1);
    }

    POSICAO = 10;
    SOMA = 0;
    for (I = 0; I <= 8; I++) {
        SOMA = SOMA + DIGITO[I] * POSICAO;
        POSICAO = POSICAO - 1;
    }

    DIGITO[9] = SOMA % 11;
    if (DIGITO[9] < 2) {
        DIGITO[9] = 0;
    }
    else {
        DIGITO[9] = 11 - DIGITO[9];
    }

    POSICAO = 11;
    SOMA = 0;
    for (I = 0; I <= 9; I++) {
        SOMA = SOMA + DIGITO[I] * POSICAO;
        POSICAO = POSICAO - 1;
    }

    DIGITO[10] = SOMA % 11;
    if (DIGITO[10] < 2) {
        DIGITO[10] = 0;
    }
    else {
        DIGITO[10] = 11 - DIGITO[10];
    }

    DV = DIGITO[9] * 10 + DIGITO[10];
    if (DV != Campo.value.substring(12, 14)) {
        alert('CPF inválido');
        Campo.value = '';
        Campo.focus();
    }
    else {
        var CampoProx = document.getElementById(Proximo).focus();
    }
}

function ValidaCnpj(Campo, Proximo) {

    var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais, cnpj = Campo.value.replace(/\D+/g, '');
    digitos_iguais = 1;

    if (cnpj.length != 14) {
        if (cnpj.length != 0) {
            alert('CNPJ inválido');
            return false;
        }
        else {
            return false;
        }
    }

    for (i = 0; i < cnpj.length - 1; i++) {
        if (cnpj.charAt(i) != cnpj.charAt(i + 1)) {
            digitos_iguais = 0;
            break;
        }
    }

    if (!digitos_iguais) {
        tamanho = cnpj.length - 2
        numeros = cnpj.substring(0, tamanho);
        digitos = cnpj.substring(tamanho);
        soma = 0;
        pos = tamanho - 7;
        for (i = tamanho; i >= 1; i--) {
            soma += numeros.charAt(tamanho - i) * pos--;
            if (pos < 2) {
                pos = 9;
            }
        }

        resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;

        if (resultado != digitos.charAt(0)) {
            alert('CNPJ inválido');
            return false;
        }

        tamanho = tamanho + 1;
        numeros = cnpj.substring(0, tamanho);
        soma = 0;
        pos = tamanho - 7;

        for (i = tamanho; i >= 1; i--) {
            soma += numeros.charAt(tamanho - i) * pos--;
            if (pos < 2) {
                pos = 9;
            }
        }

        resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;

        if (resultado != digitos.charAt(1)) {
            alert('CNPJ inválido');
            return false;
        }
    }
    else {
        alert('CNPJ inválido');
        return false;
    }

    document.getElementById(Proximo).focus();
    return true;
}

function FormataMoeda(controle) {
    controle = "ctl00$cphPrincipal$" + controle;
    valor = document.getElementById(controle).value;
    array = valor.indexOf(",");
    if (array < 1 && document.getElementById(controle).value != "") {
        document.getElementById(controle).value += ",00";
    }
}

function BloqueiaLetra() {
    var tecla = event.keyCode;

    tecla = String.fromCharCode(tecla);
    if (!((tecla >= "0") && (tecla <= "9")) && (tecla != ",")) {
        window.event.keyCode = 0;
    }
}

//Função para Validar Data
function ValidaData(NmCampo, Proximo) {
    var dDia;
    var dMes;
    var dAno;
    var Ano = NmCampo.value.substring(6, 10);
    var Mes = NmCampo.value.substring(3, 5);
    var Dia = NmCampo.value.substring(0, 2);
    var date = new Date();

    date.setFullYear(Ano, Mes - 1, Dia);

    dDia = (date.getDate() == Dia);
    dMes = (date.getMonth() == Mes - 1);
    dAno = (date.getFullYear() == Ano);


    if (dDia && dMes && dAno) {
        if (Proximo != '') {
            document.getElementById(Proximo).focus();
        }
    }
    else {
        if (NmCampo.value != '__/__/____') {
            NmCampo.value = '';
            alert('Data Inválida');
        }
    }

}

//Função para Abrir PopUp
function Abrir(Url, Widt, Heig, Scroll) {

    var pix = (screen.width - Widt) / 2;
    window.open(Url, "Janela", "status=no, scrollbars=" + Scroll + ", width=" + Widt + ", height= " + Heig + ", top=20, left=" + pix).focus();
    return false;

}


// Função que valida a data com tres campos (WebService) Data Nascimento
function VerificaDataWSNasc(_ano) {
    var dia = document.getElementById('ctl00_ContentPlaceHolder01_ucFormularioCadastro1_txtdia_nascimento').value;
    var mes = document.getElementById('ctl00_ContentPlaceHolder01_ucFormularioCadastro1_txtmes_nascimento').value;
    var ano = _ano;

    var bissexto = 0;

    if (dia > 0 && dia < 32 && mes > 0 && mes < 13) {
        if ((ano > 1900) && (ano < 2100)) {
            switch (mes) {
                case '01':
                case '03':
                case '05':
                case '07':
                case '08':
                case '10':
                case '12':
                    if (dia <= 31) {
                        return true;
                    }
                    break

                case '04':
                case '06':
                case '09':
                case '11':
                    if (dia <= 30) {
                        return true;
                    }
                    break
                case '02':
                    /* Validando ano Bissexto / fevereiro / dia */
                    if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)) {
                        bissexto = 1;
                    }
                    if ((bissexto == 1) && (dia <= 29)) {
                        return true;
                    }
                    if ((bissexto != 1) && (dia <= 28)) {
                        return true;
                    }
                    break
            }
        }
    }
    alert("Data inválida");
    return false;
}


// Função que valida a data com tres campos (WebService) Data Fundacao
function VerificaDataWSFund(dia, mes, ano) {

    var bissexto = 0;

    if (dia > 0 && dia < 32 && mes > 0 && mes < 13) {
        if ((ano > 1900) && (ano < 2100)) {
            switch (mes) {
                case '01':
                case '03':
                case '05':
                case '07':
                case '08':
                case '10':
                case '12':
                    if (dia <= 31) {
                        return true;
                    }
                    break

                case '04':
                case '06':
                case '09':
                case '11':
                    if (dia <= 30) {
                        return true;
                    }
                    break
                case '02':
                    /* Validando ano Bissexto / fevereiro / dia */
                    if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)) {
                        bissexto = 1;
                    }
                    if ((bissexto == 1) && (dia <= 29)) {
                        return true;
                    }
                    if ((bissexto != 1) && (dia <= 28)) {
                        return true;
                    }
                    break
            }
        }
    }
    alert("Data inválida");
    return false;
}

// Função que valida a data com tres campos
function VerificandoData(dia, mes, ano) {

    var bissexto = 0;

    var myDate = new Date();
    myDate.setFullYear(ano, mes-1, dia);
    var today = new Date();
    if (myDate > today) {        
        return false;
    }

    if (dia > 0 && dia < 32 && mes > 0 && mes < 13) {
        if ((ano > 1900) && (ano < 2100)) {
            switch (mes) {
                case '01':
                case '03':
                case '05':
                case '07':
                case '08':
                case '10':
                case '12':
                    if (dia <= 31) {
                        return true;
                    }
                    break

                case '04':
                case '06':
                case '09':
                case '11':
                    if (dia <= 30) {
                        return true;
                    }
                    break
                case '02':
                    /* Validando ano Bissexto / fevereiro / dia */
                    if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)) {
                        bissexto = 1;
                    }
                    if ((bissexto == 1) && (dia <= 29)) {
                        return true;
                    }
                    if ((bissexto != 1) && (dia <= 28)) {
                        return true;
                    }
                    break
            }
        }


    }
  
    return false;
}
function VerificarAceito(source, args) {

    var dia = document.getElementById('ctl00_ContentPlaceHolder01_ucFormularioCadastro1_txtdia_fundacao').value;
    var mes = document.getElementById('ctl00_ContentPlaceHolder01_ucFormularioCadastro1_txtmes_fundacao').value;
    var ano = document.getElementById('ctl00_ContentPlaceHolder01_ucFormularioCadastro1_txtano_fundacao').value;

//alert(document.getElementById('ctl00_ContentPlaceHolder01_ucRegulamento1_chkAceito').checked);
    args.IsValid = VerificandoData(dia, mes, ano);

}

function validarDtaEmissao(oSrc, args) 
{
    var dia = document.getElementById('ctl00_ContentPlaceHolder01_ucFormularioCadastro1_txtdia_emissao').value;
    var mes = document.getElementById('ctl00_ContentPlaceHolder01_ucFormularioCadastro1_txtmes_emissao').value;
    var ano = document.getElementById('ctl00_ContentPlaceHolder01_ucFormularioCadastro1_txtano_emissao').value;
    return args.IsValid = VerificandoData(dia, mes, ano);
}


function validarDtaNasc(oSrc, args) {
    var dia = document.getElementById('ctl00_ContentPlaceHolder01_ucFormularioCadastro1_txtdia_nascimento').value;
    var mes = document.getElementById('ctl00_ContentPlaceHolder01_ucFormularioCadastro1_txtmes_nascimento').value;
    var ano = document.getElementById('ctl00_ContentPlaceHolder01_ucFormularioCadastro1_txtano_nascimento').value;
    return args.IsValid = VerificandoData(dia, mes, ano);
}

function validarSobrenome(oSrc, args) {
    var nome = args.Value.trim();
    if (nome.indexOf(" ") == -1) {
        return args.IsValid = false;
    }
    else {
        return args.IsValid = true;
    }    
}


