﻿function ValidaHora(custom, args) {
    var Horas = parseInt(args.Value.split(':')[0]);
    var Minutos = parseInt(args.Value.split(':')[1]);

    if (Horas > 23 || Minutos > 59) {
        args.IsValid = false;
    }
}

function validaEMAIL(custom, args) {

    var testeEmail = new RegExp(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/);
    OKEmail = testeEmail.test(args.Value);

    if (args.Value == "") {
        args.IsValid = true;
        return;
    }
    if (!OKEmail) {
        args.IsValid = false;
    }
}

function validaCampoPreenchido_CPF(custom, args) {
    if (args.Value.indexOf('_') == -1) {
        validarCPF(custom, args)
    }
    else {
        args.IsValid = true;
    }

}

function ValidaCEP(custom, args) {
    var cep = args.Value;
    cep = remove(cep, '_');
    cep = remove(cep, '-');
    args.IsValid = cep.length == 8;
}

function ValidaSenha(custom, args) {
    if (args.Value.length < 6) {
        args.IsValid = false;
        alert(custom.errormessage);
    }
}

function ValidaTamanhoCampo(tamanho, campo) {
    if (campo.value.length >= tamanho) {
        campo.value = campo.value.substring(0, tamanho);
    }
}

function validarCNPJ(valor, args) {
    var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
    digitos_iguais = 1;
    cnpj = args.Value;

    cnpj = remove(cnpj, ".");
    cnpj = remove(cnpj, "-");
    cnpj = remove(cnpj, "/");

    if (remove(cnpj, "_") == '') {
        return true;
    }

    if (cnpj.length < 14 && cnpj.length < 15) {
        args.IsValid = false;
        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)) {
            args.IsValid = false;
            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)) {
            args.IsValid = false;
            return false;
        }
        return true;
    }
    else {
        args.IsValid = false;
        return false;
    }
}

function validarCPF(valor, args) {

    
    var cpf = args.Value;
    var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;

    cpf = remove(cpf, ".");
    cpf = remove(cpf, "-");

    if (remove(cpf, "_") == '') {
        return true;
    }

    if (cpf.length > 11) {
        args.IsValid = false;
        return false;
    }

    if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	              cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	              cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	              cpf == "88888888888" || cpf == "99999999999") {
        args.IsValid = false;
        return false;
    }

    soma = 0;
    for (i = 0; i < 9; i++)
        soma += parseInt(cpf.charAt(i)) * (10 - i);
    resto = 11 - (soma % 11);
    if (resto == 10 || resto == 11)
        resto = 0;
    if (resto != parseInt(cpf.charAt(9))) {
        args.IsValid = false;
        return false;
    }
    soma = 0;
    for (i = 0; i < 10; i++)
        soma += parseInt(cpf.charAt(i)) * (11 - i);
    resto = 11 - (soma % 11);
    if (resto == 10 || resto == 11)
        resto = 0;
    if (resto != parseInt(cpf.charAt(10))) {
        args.IsValid = false;
        return false;
    }
    return true;
}

function remove(str, sub) {
    i = str.indexOf(sub);
    r = "";
    if (i == -1) return str;
    r += str.substring(0, i) + remove(str.substring(i + sub.length), sub);
    return r;
}


function ValidaTelefones(custom, args) {
    if (args.Value == "(__) ____-____") {
        args.IsValid = true;
    }
    else if(args.Value.indexOf('_') != -1) {
        args.IsValid = false ;
    }
}

function FormataCampo(tipo, Campo) {

    var Masc = ''
    var Barrar = 0
    var Negativo = ''

    if (window.event.keyCode == 9) { Barrar = 1 } // Tab
    if (window.event.keyCode == 16) { Barrar = 1 } // shift
    if (window.event.keyCode == 33) { Barrar = 1 } // pg up
    if (window.event.keyCode == 34) { Barrar = 1 } // pg down
    if (window.event.keyCode == 35) { Barrar = 1 } // End
    if (window.event.keyCode == 36) { Barrar = 1 } // Home
    if (window.event.keyCode == 37) { Barrar = 1 } // seta para esquerda
    if (window.event.keyCode == 38) { Barrar = 1 } // seta para cima
    if (window.event.keyCode == 39) { Barrar = 1 } // seta para direita
    if (window.event.keyCode == 40) { Barrar = 1 } // seta para baixo
    if (Barrar == 1) { return false; }



    var Valor = Campo.value.replace(' ', '')
    if (Valor.indexOf('-') >= 0) {
        Negativo = '-';
    }

    Valor = Valor.replace(':', '')
    Valor = Valor.replace('/', '')
    Valor = Valor.replace('.', '')
    Valor = Valor.replace(',', '')
    Valor = Valor.replace('(', '')
    Valor = Valor.replace(')', '')
    Valor = Valor.replace(' ', '')
    Valor = Valor.replace('_', '')
    Valor = Valor.replace('-', '')


    for (dig = 0; dig < 20; dig++) {
        Masc = Masc + '_';
        Valor = Valor.replace('_', '');
    }

    var Num = '0123456789'
    var Letras = 'abcdefghijklmnopqrstuvwxyz'
    var Txt = ''

    for (i = 0; i < Valor.length; i++) {
        var Erro = 1
        for (c = 0; c < Num.length; c++) {
            if (Valor.substring(i, (i + 1)) == Num.substring(c, (c + 1))) {
                Erro = 0
            }
        }
        if (Erro == 0) {
            Txt = Txt + Valor.substring(i, (i + 1))
        }
    }
    var Chr = Txt.length

    switch (tipo) {

        case 1: //hora = 00:00
            Txt = Txt.substring(0, 4);
            Txt = Masc.substring(0, (4 - Txt.length)) + Txt;
            Txt = Txt.substring(0, 2) + ':' + Txt.substring(2, 4);
            break;

        case 2: //hora = 00:00:00
            Txt = Txt.substring(0, 6);
            Txt = Masc.substring(0, (6 - Txt.length)) + Txt;
            Txt = Txt.substring(0, 2) + ':' + Txt.substring(2, 4) + ':' + Txt.substring(4, 6);
            break;

        case 3: //data = 00/00/00
            Txt = Txt.substring(0, 6);
            Txt = Masc.substring(0, (6 - Txt.length)) + Txt;
            Txt = Txt.substring(0, 2) + '/' + Txt.substring(2, 4) + '/' + Txt.substring(4, 6);
            break;

        case 4: //data = 00/00/0000
            Txt = Txt.substring(0, 8);
            Txt = Masc.substring(0, (8 - Txt.length)) + Txt;
            Txt = Txt.substring(0, 2) + '/' + Txt.substring(2, 4) + '/' + Txt.substring(4, 8);
            break;

        case 5: //cnpj = 00.000.000/0000-00
            Txt = Txt.substring(0, 14);
            Txt = Masc.substring(0, (14 - Txt.length)) + Txt;
            Txt = Txt.substring(0, 2) + '.' + Txt.substring(2, 5) + '.' + Txt.substring(5, 8) + '/' + Txt.substring(8, 12) + '-' + Txt.substring(12, 14);
            break;

        case 6: //cpf = 000.000.000-00
            Txt = Txt.substring(0, 11);
            Txt = Masc.substring(0, (11 - Txt.length)) + Txt;
            Txt = Txt.substring(0, 3) + '.' + Txt.substring(3, 6) + '.' + Txt.substring(6, 9) + '-' + Txt.substring(9, 11)
            break;

        case 7: //IE = 000.000.000.000
            Txt = Txt.substring(0, 12);
            Txt = Masc.substring(0, (12 - Txt.length)) + Txt;
            Txt = Txt.substring(0, 3) + '.' + Txt.substring(3, 6) + '.' + Txt.substring(6, 9) + '.' + Txt.substring(9, 12)
            break;

        case 8: //valor = 000.000.000.000,00
            ChrV = Txt.length
            Valor = Txt

            if (ChrV == 4 && (Valor.substring(0, 1) == '0' || Valor.substring(0, 1) == '00')) {
                Campo.value = Valor.substring(1, (Valor.length))
                FormataCampo(11, Campo);
                return false
            }

            Txt = Valor.substring((ChrV - 5), (ChrV - 2)) + ',' + Valor.substring((ChrV - 2), ChrV)
            if (ChrV >= 6) { Txt = Valor.substring((ChrV - 8), (ChrV - 5)) + '.' + Txt }
            if (ChrV >= 9) { Txt = Valor.substring((ChrV - 11), (ChrV - 8)) + '.' + Txt }
            if (ChrV >= 12) { Txt = Valor.substring((ChrV - 14), (ChrV - 11)) + '.' + Txt }
            break;

        case 9: //Telefone = (00) 0000-0000
            Txt = Txt.substring(0, 10);
            Txt = Masc.substring(0, (10 - Txt.length)) + Txt;
            Txt = '(' + Txt.substring(0, 2) + ') ' + Txt.substring(2, 6) + '-' + Txt.substring(6, 10)
            break;

        case 10: //Cep = 00000-000
            Txt = Txt.substring(0, 8);
            Txt = Masc.substring(0, (8 - Txt.length)) + Txt;
            Txt = Txt.substring(0, 5) + '-' + Txt.substring(5, 8)
            break;

        case 11: //Decimal= 000000000000,00
            ChrV = Txt.length
            Valor = Txt

            if (ChrV == 4 && (Valor.substring(0, 1) == '0' || Valor.substring(0, 1) == '00')) {
                Campo.value = Valor.substring(1, (Valor.length))
                FormataCampo(11, Campo);
                return false
            }

            Txt = Valor.substring((ChrV - 5), (ChrV - 2)) + ',' + Valor.substring((ChrV - 2), ChrV)
            if (ChrV >= 6) { Txt = Valor.substring((ChrV - 8), (ChrV - 5)) + Txt }
            if (ChrV >= 9) { Txt = Valor.substring((ChrV - 11), (ChrV - 8)) + Txt }
            if (ChrV >= 12) { Txt = Valor.substring((ChrV - 14), (ChrV - 11)) + Txt }
            if (ChrV >= 15) { Txt = Valor.substring((ChrV - 17), (ChrV - 14)) + Txt }
            break;


        case 12: //Decimal= 0000000000,0000
            ChrV = Txt.length
            Valor = Txt

            if (ChrV == 6 && (Valor.substring(0, 1) == '0' || Valor.substring(0, 1) == '00')) {
                Campo.value = Negativo + Valor.substring(1, (Valor.length))
                FormataCampo(12, Campo);
                return false
            }

            Txt = Valor.substring((ChrV - 7), (ChrV - 4)) + ',' + Valor.substring((ChrV - 4), ChrV)
            if (ChrV >= 9) { Txt = Valor.substring((ChrV - 10), (ChrV - 7)) + Txt }
            if (ChrV >= 11) { Txt = Valor.substring((ChrV - 13), (ChrV - 10)) + Txt }
            if (ChrV >= 14) { Txt = Valor.substring((ChrV - 16), (ChrV - 13)) + Txt }
            if (ChrV >= 17) { Txt = Valor.substring((ChrV - 19), (ChrV - 16)) + Txt }
            Txt = Negativo + Txt
            break;

    }

    if (Chr > 0) {
        Campo.value = Txt
    } else {
        Campo.value = ''
    }

}

function Valida(tipo, foco, Campo) {

    var Msg = ''
    var Valor = Campo.value.replace(' ', '')

    if (Valor != '') {
        switch (tipo) {

            case 1:
                Valor = Valor.split(':')
                if (Valor[0] > 23) { Msg = 'No Campo de horario, a hora nao pode ser maior que 23 horas.'; }
                if (Valor[1] > 59) { Msg = 'No Campo de horario, os minutos nao podem ser maior que 59 minutos.'; }
                if (Valor[0] == ' ') { Msg = 'Informe a hora'; }
                break;

            case 2:
                Valor = Valor.split(':')
                if (Valor[0] > 23) { Msg = 'No Campo de horario, a hora nao pode ser maior que 23 horas.'; }
                if (Valor[1] > 59) { Msg = 'No Campo de horario, os minutos nao podem ser maior que 59 minutos.'; }
                if (Valor[2] > 59) { Msg = 'No Campo de horario, os segundos nao podem ser maior que 59 segundos.'; }
                if (Valor[0] == ' ') { Msg = 'Informe a hora'; }
                if (Valor[1] == ' ') { Msg = 'Informe os minutos'; }
                break;

            case 3:
                Valor = Valor.split('/')
                if (Valor[0] == 0) { Msg = 'informe o dia.' }
                if (Valor[1] == 0) { Msg = 'informe o mes.' }
                if (Valor[0] > 30 && (Valor[1] == 4 || Valor[1] == 6 || Valor[1] == 9 || Valor[1] == 11)) { Msg = 'Data inválida, este mes pode ir somente até o dia 30.' }
                if (Valor[0] > 28 && Valor[1] == 2 && Valor[2] <= 94) { Msg = 'Data inválida, este mes pode ir somente até o dia 28.' }
                if (Valor[0] > 29 && Valor[1] == 2 && Valor[2] >= 95) { Msg = 'Data inválida, este mes pode ir somente até o dia 29.' }
                if (Valor[1] > 12) { Msg = 'Mes inválido, os deve ser informado de 1~12.' }
                break;

            case 4:
                Valor = Valor.split('/')
                Data = new Date()
                Ano = Data.getYear()
                if (Valor[0] == 0) { Msg = 'informe o dia.' }
                if (Valor[1] == 0) { Msg = 'informe o mes.' }
                if (Valor[2] == 0) { Msg = 'informe o ano.' }
                if (Valor[0] > 30 && (Valor[1] == 4 || Valor[1] == 6 || Valor[1] == 9 || Valor[1] == 11)) { Msg = 'Data inválida, este mes pode ir somente até o dia 30.' }
                if (Valor[0] > 28 && Valor[1] == 2 && Valor[2] <= 94) { Msg = 'Data inválida, este mes pode ir somente até o dia 28.' }
                if (Valor[0] > 29 && Valor[1] == 2 && Valor[2] >= 95) { Msg = 'Data inválida, este mes pode ir somente até o dia 29.' }
                if (Valor[1] > 12) { Msg = 'Mes inválido, os deve ser informado de 1~12.' }
                if (Valor[2] > Ano) { Msg = 'Ano inválido, o ano nao pode ser maior que o ano atual.' }
                if (Valor[2] < 1900) { Msg = 'Ano inválido, o ano nao pode ser menor que 1900.' }
                break;

            case 8:
                if (Valor.length <= 3) {
                    Zeros = '000'
                    Valor = Valor.replace(',', '')
                    Valor = Zeros.substring(0, (3 - Valor.length)) + Valor
                    Campo.value = Valor //insere novamente o valor de centavos com zero ou zeros a frente. Ex : xx vai pra 0,xx ou 0,0x  
                    FormataCampo(8, Campo); //formata o Campo nvamente
                }
                break;

            case 9:
                Valor = Valor.replace('-', '');
                Valor = Valor.replace('(', '');
                Valor = Valor.replace(')', '');
                Valor = Valor.replace(' ', '');
                if (Valor.length < 10) { Msg = 'Telefone inválido.'; }
                break;

            case 10:
                Valor = Valor.replace('-', '');
                Valor = Valor.replace(' ', '');
                if (Valor.length < 8 || Valor < 1000) { Msg = 'CPF inválido.'; }
                break;

            case 11:
                if (Valor.length <= 3) {
                    Zeros = '000'
                    Valor = Valor.replace(',', '')
                    Valor = Zeros.substring(0, (3 - Valor.length)) + Valor
                    Campo.value = Valor //insere novamente o valor de centavos com zero ou zeros a frente. Ex : xx vai pra 0,xx ou 0,0x  
                    FormataCampo(11, Campo); //formata o Campo nvamente
                }
                break;

            case 12:

                var Minimo = 4
                var Zeros = '00000'
                Valor = Valor.replace(',', '')
                Valor = Valor.replace('.', '')

                if (Valor.indexOf('-') >= 0) {
                    Minimo = 5
                    Zeros = '-00000'
                }

                if (Valor.length <= Minimo) {
                    Valor = Valor.replace(',', '')
                    Valor = Zeros.substring(0, (Minimo - Valor.length)) + Valor
                    Campo.value = Valor //insere novamente o valor de centavos com zero ou zeros a frente. Ex : xx vai pra 0,xx ou 0,0x  
                    FormataCampo(12, Campo); //formata o Campo nvamente
                }
                break;

        }

        if (Msg != '') {
            Campo.value = '';
            if (foco == 1) { Campo.focus(); }
            return false
        }
    }
}

function MostraOculta(barra, div) {
    var divAtual = document.getElementById(div);

    if (divAtual.style.display == 'none') {
        barra.innerHTML = barra.innerHTML.replace('+', '-');
        divAtual.style.display = 'block';
    } else {
        barra.innerHTML = barra.innerHTML.replace('-', '+');
        divAtual.style.display = 'none';
    }
}

function SetaAba(Tipo, ID) {
    for (i = 1; i < 50; i++) {
        var Aba = document.getElementById(Tipo + 'Abas_' + i)
        var DivAba = document.getElementById(Tipo + 'Divs_' + i)
        if (Aba) {
            Aba.className = 'abaBranca1';
            DivAba.style.display = 'none';
        } else {
            i = 50
        }
    }

    var Aba = document.getElementById(Tipo + 'Abas_' + ID)
    var DivAba = document.getElementById(Tipo + 'Divs_' + ID)
    if (Aba) {
        Aba.className = 'abaAzul1';
        DivAba.style.display = 'block';
    }

}



function Navegacao(Pag,ID, Parametro ){

    var Url = ''

    switch(Pag){
        case 'Negociacao' : 
            switch( parseInt(ID) ){
                case 1 : Url = 'Negociacao_segmentacao_1.aspx' ; break ;
                case 2 : Url = 'Negociacao_followup.aspx'      ; break ;
                case 3 : Url = 'Negociacao_agendamento.aspx'   ; break ;
                case 4 : Url = 'Negociacao_pipeline.aspx'      ; break ;
            }
            ; break ;
            
    }
    
    if(Url != '' ){
        if(Parametro){
            Url = Url + '?' + Parametro
        }
        window.location.href = Url ;
    }
    
}