﻿function trim(str) {
    return str.replace(/^\s+|\s+$/g, "");
}

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 + ' error');
            else if (attCN != null)
                parent.setAttribute('className', attCN + ' error');
        }
        else {
            if (attC != null)
                parent.setAttribute('class', attC.split(' ')[0]);
            else if (attCN != null)
                parent.setAttribute('className', attCN.split(' ')[0]);
        }
    }
    catch(e)
    {
    }
}

function RequeridoModuloBusca(sender, args) {
    var obj = document.getElementById(sender.controltovalidate);
    args.IsValid = (trim(obj.value) != '') && (obj.value != 'Digite a palavra');
    
    MudarClasse(obj, args);
}



function Requerido(sender, args) {
    var obj = document.getElementById(sender.controltovalidate);

    if (obj.id.indexOf('ddl') > -1) {
        if (isNaN(obj.value))
            args.IsValid = obj.value != '';
        else
            args.IsValid = obj.value > 0;
    }
    else {
        args.IsValid = (trim(obj.value) != '');
    }

    MudarClasse(obj, args);
}

function RequeridoSenhaCadastro(sender, args) {
    var obj = document.getElementById(sender.controltovalidate);
    var objConfirma = document.getElementById('li3');

    for (i = 0; i < objConfirma.childNodes.length; i++) {
        if (objConfirma.childNodes[i].id != null && objConfirma.childNodes[i].id.indexOf('txtSenhaConfirma') > -1) {
            args.IsValid = (obj.value == objConfirma.childNodes[i].value && obj.value != '');

            MudarClasse(obj, args);
            MudarClasse(objConfirma.childNodes[i], args);

            break;
        }
    }
}

function RequeridoSenhaCadastrado(sender, args) {
    var obj = document.getElementById(sender.controltovalidate);
    var objConfirma = document.getElementById('li3');

    for (i = 0; i < objConfirma.childNodes.length; i++) {
        if (objConfirma.childNodes[i].id != null && objConfirma.childNodes[i].id.indexOf('txtSenhaConfirma') > -1) {
            args.IsValid = (obj.value == objConfirma.childNodes[i].value);

            MudarClasse(obj, args);
            MudarClasse(objConfirma.childNodes[i], args);

            break;
        }
    }
}

function RequeridoCEP(sender, args) {
    var obj = document.getElementById(sender.controltovalidate);
    var valor = trim(obj.value);

    if (obj.id.indexOf('CEP1') > -1)
        args.IsValid = (isInteger(valor) && valor.length == 5);
    else if (obj.id.indexOf('CEP2') > -1)
        args.IsValid = (isInteger(valor) && valor.length == 3);
    else
        args.IsValid = (trim(obj.value) != '');

    MudarClasse(document.getElementById('div5'), args);
}

function RequeridoNumero(sender, args) {
    var obj = document.getElementById(sender.controltovalidate);
    var valor = trim(obj.value);

    args.IsValid = (isInteger(valor) && valor.length > 0);

    MudarClasse(obj, args);
}

function RequeridoCPF(sender, args) {
    var obj = document.getElementById(sender.controltovalidate);

    args.IsValid = validaCpfCnpj(obj);

    MudarClasse(obj, args);
}

function RequeridoData(sender, args) {
    var obj = document.getElementById(sender.controltovalidate);

    args.IsValid = isDate(trim(obj.value));

    MudarClasse(obj, args);
}

function validaCpfCnpj(obj) {
    var i;
    var retorno = true;

    cpf = obj.value.replace(".", "");
    cpf = cpf.replace(".", "");
    cpf = cpf.replace("-", "");
    cpf = cpf.replace("/", "");

    if (cpf.length == 11) {
        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, i, resultado, pos, tamanho, digitos_iguais, cnpj;
        cnpj = cpf;
        digitos_iguais = 1;
        if (cnpj.length < 14 && cnpj.length < 15)
            retorno = 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))
                retorno = 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))
                retorno = false;
        }
        else
            retorno = false;
    }

    return retorno;
}

function MudarClasse_old(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);

    var browser = navigator.userAgent.toLowerCase();

    if (!args.IsValid) {
        if (browser.indexOf("firefox") > 0) {
            parent.setAttribute('class', parent.getAttribute('class') + ' error');
        }
        else {
            parent.setAttribute('className', parent.getAttribute('className') + ' error');
        }
    }
    else {
        if (browser.indexOf("firefox") > 0) {
            parent.setAttribute('class', parent.getAttribute('class').split(' ')[0]);
        }
        else {
            parent.setAttribute('className', parent.getAttribute('className').split(' ')[0]);
        }
    }
}