﻿/************************************************************************
*
*Decription: Preencher campo somente com caracteres numéricos
*Author: Eliamar Tani
*
************************************************************************/
function SomenteNumero(e) {
    var key = window.event ? e.keyCode : e.which;
    var keychar = String.fromCharCode(key);
    var vrRetorno = false;

    if (key != 8 && key != 0) {
        goodChars = "0123456789";
        if (goodChars.indexOf(keychar) != -1) {
            vrRetorno = true;
        }
    }
    else {
        vrRetorno = true;
    }
    return vrRetorno;
}

/************************************************************************
*
*Decription: Verificar formatação do email com expressão regular
*Author: Eliamar Tani
*
************************************************************************/
function verificarEmail(sender, args) {
    var er = /^[\w!#$%&amp;'*+\/=?^`{|}~-]+(\.[\w!#$%&amp;'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
    var email = document.getElementById(sender.controltovalidate);
   
    if (email.value != '') {
        if (typeof (email) == "string") {
            if (er.test(email.value))
                args.IsValid = true;
            else
                args.IsValid = false;
        }
        else if (typeof (email) == "object") {
            if (er.test(email.value))
                args.IsValid = true;
            else
                args.IsValid = false;    
        }
        else {
            args.IsValid = false;
        }
    }
    else {
        args.IsValid = false;
    }

    MudarClasse(email, args);
}


/************************************************************************
*
*Decription: Verificar se o campo foi preenchido
*Author: Eliamar Tani
*
************************************************************************/
function verificarCampo(sender, args) {
    var obj = document.getElementById(sender.controltovalidate);
    args.IsValid = trim(obj.value) != '';
    MudarClasse(obj, args);
}


/************************************************************************
*
*Decription: Verificar se o campo CEP foi preenchido
*Author: Pedro Oliveira
*
************************************************************************/
function verificarCep(sender, args) {
    var obj = document.getElementById(sender.controltovalidate);
    args.IsValid = trim(obj.value).length == 9;
    MudarClasse(obj, args);
}

/************************************************************************
*
*Decription: Verificar se as senhas estão iguais 1
*Author: Eliamar Tani
*
************************************************************************/
function validarSenha(sender, args) {
    var senha1 = document.getElementById('ctl00_ContentPlaceHolder1_txtSenha');
    var senha2 = document.getElementById('ctl00_ContentPlaceHolder1_txtSenhaConfirma');

    if (trim(senha1.value) != '' && trim(senha2.value) != '') {
        if (senha1.value == senha2.value)
            args.IsValid = true;
        else
            args.IsValid = false;
    }
    else {
        args.IsValid = false;
    }

    MudarClasse(senha1, args);
    MudarClasse(senha2, args);
}

function validarSenha1(sender, args) {
   
    var senha1 = document.getElementById('ctl00_ContentPlaceHolder1_AtualizaDados1_txtSenha');
    var senha2 = document.getElementById('ctl00_ContentPlaceHolder1_AtualizaDados1_txtSenhaConfirma');

    if (trim(senha1.value) != '' && trim(senha2.value) != '') {
        if (senha1.value == senha2.value)
            args.IsValid = true;
        else
            args.IsValid = false;
    }
    else {
        args.IsValid = false;
    }

    MudarClasse(senha1, args);
    MudarClasse(senha2, args);
}

function validarSenha2(sender, args) {
    
    var senha1 = document.getElementById('ctl00_ContentPlaceHolder1_DadosPessoaFisica1_txtSenha');
    var senha2 = document.getElementById('ctl00_ContentPlaceHolder1_DadosPessoaFisica1_txtSenhaConfirma');
    
    if (trim(senha1.value) != '' && trim(senha2.value) != '') {
        if (senha1.value == senha2.value)
            args.IsValid = true;
        else
            args.IsValid = false;
    }
    else {
        args.IsValid = false;
    }

    MudarClasse(senha1, args);
    MudarClasse(senha2, args);
}


/************************************************************************
*
*Decription: Verificar se as senhas estão iguais 2
*Author: Eliamar Tani
*
************************************************************************/
function verificarSenha(sender, args) {
    var senha1 = document.getElementById('ctl00_ContentPlaceHolder1_txtSenha');
    var senha2 = document.getElementById('ctl00_ContentPlaceHolder1_txtSenhaConfirma');

    if (trim(senha1.value) != '' && trim(senha2.value) != '') {
        if (senha1.value == senha2.value)
            args.IsValid = true;
        else
            args.IsValid = false;
    }
    else {
        if (trim(senha1.value) == '' && trim(senha2.value) == '')
            args.IsValid = true;
        else
            args.IsValid = false;
    }

    MudarClasse(senha1, args);
    MudarClasse(senha2, args);
}

function verificarSenha1(sender, args) {

    var senha1 = document.getElementById('ctl00_ContentPlaceHolder1_AtualizaDados1_txtSenha');
    var senha2 = document.getElementById('ctl00_ContentPlaceHolder1_AtualizaDados1_txtSenhaConfirma');

    if (trim(senha1.value) != '' && trim(senha2.value) != '') {
        if (senha1.value == senha2.value)
            args.IsValid = true;
        else
            args.IsValid = false;
    }
    else {
        if (trim(senha1.value) == '' && trim(senha2.value) == '')
            args.IsValid = true;
        else
            args.IsValid = false;
    }

    MudarClasse(senha1, args);
    MudarClasse(senha2, args);
}

function verificarSenha2(sender, args) {

    var senha1 = document.getElementById('ctl00_ContentPlaceHolder1_DadosPessoaFisica1_txtSenha');
    var senha2 = document.getElementById('ctl00_ContentPlaceHolder1_DadosPessoaFisica1_txtSenhaConfirma');
    
    if (trim(senha1.value) != '' && trim(senha2.value) != '') {
        if (senha1.value == senha2.value)
            args.IsValid = true;
        else
            args.IsValid = false;
    }
    else {
        if (trim(senha1.value) == '' && trim(senha2.value) == '')
            args.IsValid = true;
        else
            args.IsValid = false;
    }

    MudarClasse(senha1, args);
    MudarClasse(senha2, args);
}

/************************************************************************
*
*Decription: Função trim em javascript
*Author: Pedro Oliveira
*
************************************************************************/
function trim(str) {
    return str.replace(/^\s+|\s+$/g, "");
}

/************************************************************************
*
*Decription: Mudar Classe de campos obrigatórios
*Author: Eliamar Tani
*
************************************************************************/
function MudarClasse(obj, args) {
    if (obj == null) return;
    if (obj.id == null) return;

    var myID = obj.id;
    if (document.getElementById(obj.id + '_wrapper') != null) myID = obj.id + '_wrapper';
    var parent = document.getElementById(myID);

    try {
        var attC = parent.getAttribute('class');
        var attCN = parent.getAttribute('className');
        if (!args.IsValid) {
            if (attC != null) {
                parent.setAttribute('class', attC + ' ' + attC + '_erro');
            }
            else if (attCN != null) {
                parent.setAttribute('className', attCN + ' ' + attCN + '_erro');
            }
        }
        else {
            if (attC != null)
                parent.setAttribute('class', attC.split(' ')[0]);
            else if (attCN != null)
                parent.setAttribute('className', attCN.split(' ')[0]);
        }
    }
    catch (e) {
    }
}


/************************************************************************
*
*Decription: Formata Data em um textbox
*Author: Matheus Moreno
*
************************************************************************/
function FormataData(e, obj) {
    //usar no evento keypress
    //bloqueia caracteres alfa e coloca as barras nas posições        

    var key = window.event ? e.keyCode : e.which;
    var keychar = String.fromCharCode(key);
    var vrRetorno = false;

    //Backspace e Tab
    if (key != 8 && key != 0) {
        goodChars = "0123456789";
        if (goodChars.indexOf(keychar) != -1) {
            if ((obj.value.length == 2 || obj.value.length == 5) && key != 8) {
                obj.value += "/";
            }

            vrRetorno = true;
        }
    }
    else {
        vrRetorno = true;
    }

    return vrRetorno;
}


/************************************************************************
*
*Decription: Formata Telefone
*Author: Matheus Moreno
*
************************************************************************/
function FormataTelefone(e, obj) {
    //usar no evento keypress
    //bloqueia caracteres alfa e coloca as barras nas posições

    var key = window.event ? e.keyCode : e.which;
    var keychar = String.fromCharCode(key);
    var vrRetorno = false;

    if (key != 8 && key != 0) {
        goodChars = "0123456789";
        if (goodChars.indexOf(keychar) != -1) {
            if (obj.value.length == 1)
                obj.value = "(" + obj.value;
            if (obj.value.length == 3)
                obj.value += ") ";
            if (obj.value.length == 9)
                obj.value += "-";

            vrRetorno = true;
        }
    }
    else {
        vrRetorno = true;
    }

    return vrRetorno;
}

/************************************************************************
*
*Decription: Formata CEP em um textbox
*Author: Matheus Moreno
*
************************************************************************/
function FormataCEP(e, obj) {
    //usar no evento keypress
    //bloqueia caracteres alfa e coloca as barras nas posições

    var key = window.event ? e.keyCode : e.which;
    var keychar = String.fromCharCode(key);
    var vrRetorno = false;

    if (key != 8 && key != 0) {
        goodChars = "0123456789";
        if (goodChars.indexOf(keychar) != -1) {
            if (obj.value.length == 5)
                obj.value += "-";

            vrRetorno = true;
        }
    }
    else {
        vrRetorno = true;
    }

    return vrRetorno;
}

/************************************************************************
*
*Decription: Formata CPF 
*Author: Camila Foltran
*
************************************************************************/
function FormataCPF(e, obj) {
    //usar no evento keypress
    //bloqueia caracteres alfa e coloca as barras nas posições

    var key = window.event ? e.keyCode : e.which;
    var keychar = String.fromCharCode(key);
    var vrRetorno = false;

    if (key != 8 && key != 0) {
        goodChars = "0123456789";
        if (goodChars.indexOf(keychar) != -1) {

            if ((obj.value.length == 3 || obj.value.length == 7) && key != 8)
            { obj.value += "."; }
            if ((obj.value.length == 11) && key != 8)
            { obj.value += "-"; }

            vrRetorno = true;
        }
    }
    else {
        vrRetorno = true;
    }
    return vrRetorno;
}

/************************************************************************
*
*Decription: Formata CNPJ/CPF de com o tipo pessoa passado
*Author: Luiz Fernando Mascarenhas
*
************************************************************************/
function FormataCNPJ(e, obj) {
    //usar no evento keypress
    //bloqueia caracteres alfa e coloca as barras nas posições    
    var key = window.event ? e.keyCode : e.which;
    var keychar = String.fromCharCode(key);
    var vrRetorno = false;


    if (key != 8 && key != 0) {
        goodChars = "0123456789";
        if (goodChars.indexOf(keychar) != -1) {

            if ((obj.value.length == 2 || obj.value.length == 6) && key != 8)
            { obj.value += "."; }
            if ((obj.value.length == 10) && key != 8)
            { obj.value += "/"; }
            if ((obj.value.length == 15) && key != 8)
            { obj.value += "-"; }

            vrRetorno = true;
        }
    }
    else {
        vrRetorno = true;
    }

    return vrRetorno;
}

/************************************************************************
*
*Decription: função q não permite ctrl+c, ctrl+v 
*Author: Camila Foltran
*
************************************************************************/
function desabilitaCtrlKeyCombinacao(e) {
    //lista as combinações CTRL + key que queremos desabilitar:
    var forbiddenKeys = new Array('c', 'v');
    var key;
    var isCtrl;

    if (window.event) {
        key = window.event.keyCode;   //IE
        if (window.event.ctrlKey)
            isCtrl = true;
        else
            isCtrl = false;
    }
    else {
        key = e.which;    //firefox
        if (e.ctrlKey)
            isCtrl = true;
        else
            isCtrl = false;
    }

    // se ctrl é pressionado, verifica se outro key (tecla) é proibida
    if (isCtrl) {
        for (i = 0; i < forbiddenKeys.length; i++) {
            //case-insensitive comparation
            if (forbiddenKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase()) {
                alert('Key combination CTRL + '
                                        + String.fromCharCode(key)
                                        + 'has been disabled.');
                return false;
            }
        }
    }
    return true;
}

/************************************************************************
*
*Decription: Função para validar data
*Author: Eliamar Tani
*
************************************************************************/
function validarData(sender, args) {
    var bissexto = 0;
    var dtAniversario = document.getElementById(sender.controltovalidate);
    var data = dtAniversario.value;
    var tam = data.length;
    var retorno = false;
    if (tam == 10 && tam > 0) {
        var dia = data.substr(0, 2);
        var mes = data.substr(3, 2);
        var ano = data.substr(6, 4);
        if ((ano > 1900) || (ano < 2100)) {
            switch (mes) {
                case '01':
                case '03':
                case '05':
                case '07':
                case '08':
                case '10':
                case '12':
                    if (dia <= 31) {
                        retorno = true;
                    }
                    break;
                case '04':
                case '06':
                case '09':
                case '11':
                    if (dia <= 30) {
                        retorno = true;
                    }
                    break;
                case '02':
                    if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)) {
                        bissexto = 1;
                    }
                    if ((bissexto == 1) && (dia <= 29)) {
                        retorno = true;
                    }
                    if ((bissexto != 1) && (dia <= 28)) {
                        retorno = true;
                    }
                    break;
            }
        }
    }
    args.IsValid = retorno;
    MudarClasse(dtAniversario, args);
}

/************************************************************************
*
*Decription: Função para validar Cpf/Cnpj com alteração de classe
*Author: Eliamar Tani
*
************************************************************************/
function validaCpfCnpj(sender, args) {
    var retorno = true;

    obj = document.getElementById(sender.controltovalidate);

    cpf = obj.value.replace(".", "");
    cpf = cpf.replace(".", "");
    cpf = cpf.replace("-", "");
    cpf = cpf.replace("/", "");

    if (cpf.length == 11) {
        var i;

        if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
            retorno = false;

        add = 0;
        for (i = 0; i < 9; i++)
            add += parseInt(cpf.charAt(i)) * (10 - i);

        rev = 11 - (add % 11);

        if (rev == 10 || rev == 11)
            rev = 0;

        if (rev != parseInt(cpf.charAt(9)))
            retorno = false;

        add = 0;
        for (i = 0; i < 10; i++)
            add += parseInt(cpf.charAt(i)) * (11 - i);

        rev = 11 - (add % 11);

        if (rev == 10 || rev == 11)
            rev = 0;

        if (rev != parseInt(cpf.charAt(10)))
            retorno = false;

    }
    else {
        var numeros, digitos, soma, x, resultado, pos, tamanho, digitos_iguais, cnpj;
        cnpj = cpf;
        digitos_iguais = 1;
        if (cnpj.length < 14 && cnpj.length < 15)
            retorno = false;
        for (x = 0; x < cnpj.length - 1; x++)
            if (cnpj.charAt(x) != cnpj.charAt(x + 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 (x = tamanho; x >= 1; x--) {
                soma += numeros.charAt(tamanho - x) * pos--;
                if (pos < 2)
                    pos = 9;
            }

            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;

            if (resultado != digitos.charAt(0))
                retorno = false;

            tamanho = tamanho + 1;
            numeros = cnpj.substring(0, tamanho);
            soma = 0;
            pos = tamanho - 7;

            for (x = tamanho; x >= 1; x--) {
                soma += numeros.charAt(tamanho - x) * pos--;
                if (pos < 2)
                    pos = 9;
            }

            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;

            if (resultado != digitos.charAt(1))
                retorno = false;
        }
        else
            retorno = false;
    }

    args.IsValid = retorno;
    MudarClasse(obj, args);
}


/************************************************************************
*
*Decription: Função para validar Cpf/Cnpj sem alteração de classe
*Author: Eliamar Tani
*
************************************************************************/
function validaCpfCnpj2(sender, args) {
    var retorno = true;

    obj = document.getElementById(sender.controltovalidate);

    cpf = obj.value.replace(".", "");
    cpf = cpf.replace(".", "");
    cpf = cpf.replace("-", "");
    cpf = cpf.replace("/", "");

    if (cpf.length == 11) {
        var i;

        if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
            retorno = false;

        add = 0;
        for (i = 0; i < 9; i++)
            add += parseInt(cpf.charAt(i)) * (10 - i);

        rev = 11 - (add % 11);

        if (rev == 10 || rev == 11)
            rev = 0;

        if (rev != parseInt(cpf.charAt(9)))
            retorno = false;

        add = 0;
        for (i = 0; i < 10; i++)
            add += parseInt(cpf.charAt(i)) * (11 - i);

        rev = 11 - (add % 11);

        if (rev == 10 || rev == 11)
            rev = 0;

        if (rev != parseInt(cpf.charAt(10)))
            retorno = false;

    }
    else {
        var numeros, digitos, soma, x, resultado, pos, tamanho, digitos_iguais, cnpj;
        cnpj = cpf;
        digitos_iguais = 1;
        if (cnpj.length < 14 && cnpj.length < 15)
            retorno = false;
        for (x = 0; x < cnpj.length - 1; x++)
            if (cnpj.charAt(x) != cnpj.charAt(x + 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 (x = tamanho; x >= 1; x--) {
                soma += numeros.charAt(tamanho - x) * pos--;
                if (pos < 2)
                    pos = 9;
            }

            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;

            if (resultado != digitos.charAt(0))
                retorno = false;

            tamanho = tamanho + 1;
            numeros = cnpj.substring(0, tamanho);
            soma = 0;
            pos = tamanho - 7;

            for (x = tamanho; x >= 1; x--) {
                soma += numeros.charAt(tamanho - x) * pos--;
                if (pos < 2)
                    pos = 9;
            }

            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;

            if (resultado != digitos.charAt(1))
                retorno = false;
        }
        else
            retorno = false;
    }

    args.IsValid = retorno;
}
