﻿
$(document).ready(function() {
     
    $('.tabDataFundacao').autotabindex();
    $('.tabDataEmissao').autotabindex();
    $('.tabDataNasc').autotabindex();
    $('.tabCep').autotabindex();
    //$('.tabCep2').autotabindex();

    $('.lnk-tooltip').mouseover(function() {
        var posTooltip = $(this).offset();
        var txtTooltip = $(this).text();
        var x = Math.ceil(txtTooltip.length / 2);

        $('<div class="tooltip"><span class="tooltip-top" /><p>' + txtTooltip + '</p></div>').css('top', posTooltip.top - x - 20).css('left', posTooltip.left - 25).appendTo('body').show();

    }).mouseout(function() {
        $('.tooltip').hide();
    });
});

function Scroll() {
    //Scroll Bar
    var scroller = null;
    var scrollbar = null;
    window.onload = function() {
    scroller = new jsScroller(document.getElementById("Scroller-1"), 400, 200);
        scrollbar = new jsScrollbar(document.getElementById("Scrollbar-Container"), scroller, false);
    }
}

function noCopy(teclapress) {
    if (navigator.appName == "Netscape") { tecla = teclapress.which; }
    else { tecla = teclapress.keyCode; }

    var ctrl = teclapress.ctrlKey;

    if (ctrl && tecla == 67) { return false; }
    if (ctrl && tecla == 86) { return false; }
}

function Load() {
    //Font Replace
    Cufon.replace('h3,h2,h4,.btn-stnd a,.title-menu,.cuffon_h2,.title');

    //PNG Fix Resource
    //DD_belatedPNG.fix('div, a');

    //Scroll Bar
    //var scroller = null;
    //var scrollbar = null;
    //window.onload = function() {
        //scroller = new jsScroller(document.getElementById("Scroller-1"), 400, 200);
        //scrollbar = new jsScrollbar(document.getElementById("Scrollbar-Container"), scroller, false);
}



function LimparCnpjCPF() {

    document.getElementById('ctl00_ContentPlaceHolder01_txtCnpjCpf').value = '';
    document.getElementById('ctl00_ContentPlaceHolder01_txtCnpjCpf').focus();

    return false;
}


function SetarClasseCNPJ(txt, cpf, cnpj) {

    if (document.getElementById(cnpj).checked)
        txt.className = 'text-box ipt-cnpj';

    if (document.getElementById(cpf).checked)
        txt.className = 'text-box ipt-cpf';


    (function($) {
        // call setMask function on the document.ready event
        $(function() { 
            $('.ipt-cpf').setMask('cpf');
            $('.ipt-cnpj').setMask('cnpj');
            $('#ctl00_ContentPlaceHolder01_ucFormularioCadastro1_txtcep_digito').setMask('cepDig');
            
        }
    );
    })(jQuery);

    return false;

}



//----------------------FUNÇÃO PARA A VALIDAÇÃO DE CNPJ E CPF ------------------------------------

function validarCNPJ_CPF(oSrc, args) {

    var cnpj_cpf = args.Value;
    cnpj_cpf = cnpj_cpf.replace('.', '');
    cnpj_cpf = cnpj_cpf.replace('.', '');
    cnpj_cpf = cnpj_cpf.replace('/', '');
    cnpj_cpf = cnpj_cpf.replace('-', '');

    if (cnpj_cpf.length <= 11) {
        return args.IsValid = validaCPF(cnpj_cpf);

    }
    else {
        return args.IsValid = valida_cnpj(cnpj_cpf);
    }

}


//--------------------------------------FORMATA CPF OU CNPJ DE ACORDO COM RADIO --------------------

function aplicar_mascara_cpf_cnpj_radio(campo) {


    var opcao = document.getElementById('ctl00_ContentPlaceHolder01_rblTipoPessoa').getAttribute('checked');
                                    
    //verificamos qual radio esta checado para aplicar a marcara correta
    if (opcao) {
       
        //neste caso mascaramos o cnpj
        marcaraCNPJ(campo)
    }
    else {
        //neste caso mascaramos o cpf
        mascaraCPF(campo)
    }


}



//----------------------------------------- Valida CPF --------------------------------------------//

function validarCPF(oSrc, args) {
    return args.IsValid = validaCPF(args.Value);
}


function validaCPF(cpf) {

    cpf = cpf.replace('.', '');
    cpf = cpf.replace('.', '');
    cpf = cpf.replace('-', '');


    if (cpf.length != 11 || cpf == "12345678909" || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
        return 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)))
        return 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)))
        return false;

    return true;

}


//----------------------------------------- Valida CNPJ --------------------------------------------//

function validarCNPJ(oSrc, args) {

    return args.IsValid = valida_cnpj(args.Value);

}

function valida_cnpj(cnpj) {

    cnpj = cnpj.replace("/", "");
    cnpj = cnpj.replace("/", "");
    cnpj = cnpj.replace("/", "");
    cnpj = cnpj.replace(",", "");
    cnpj = cnpj.replace(".", "");
    cnpj = cnpj.replace(".", "");
    cnpj = cnpj.replace(".", "");
    cnpj = cnpj.replace(".", "");
    cnpj = cnpj.replace(".", "");
    cnpj = cnpj.replace(".", "");
    cnpj = cnpj.replace(".", "");
    cnpj = cnpj.replace("-", "");
    cnpj = cnpj.replace("-", "");
    cnpj = cnpj.replace("-", "");
    cnpj = cnpj.replace("-", "");
    cnpj = cnpj.replace("-", "");

    var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
    digitos_iguais = 1;
    if (cnpj.length < 14 && cnpj.length < 15)
        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)) {
            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)) {
            return false;
        }
        return true;
    }
    else
        return false;
}


//Mascara para a formatação de CPF

function mascaraCPF(campo) {

    var vr = campo.value;
    vr = vr.replace(/\//g, "");
    vr = vr.replace(/-/g, "");
    vr = vr.replace(/\./g, "");
    var tam = vr.length;
    if (tam <= 3) {
        campo.value = vr;
    }
    if ((tam > 3) && (tam <= 6)) {
        campo.value = vr.substring(0, 3) + '.' + vr.substring(3, tam);
    }
    if ((tam >= 7) && (tam <= 9)) {
        campo.value = vr.substring(0, 3) + '.' + vr.substring(3, 6) + '.' + vr.substring(6, tam);
    }
    if (tam > 10) {
        campo.value = vr.substring(0, 3) + '.' + vr.substring(3, 6) + '.' + vr.substring(6, 9) + '-' + vr.substring(9, 11);
    }

}


//Mascara para a formatação de cnpj    
function marcaraCNPJ(campo) {

    var vr = campo.value;
    vr = vr.replace(/\//g, "");
    vr = vr.replace(/-/g, "");
    vr = vr.replace(/\./g, "");
    var tam = vr.length;
    if (tam <= 2) {
        campo.value = vr;
    }
    if ((tam > 2) && (tam <= 5)) {
        campo.value = vr.substring(0, 2) + '.' + vr.substring(2, tam);
    }
    if ((tam >= 5) && (tam <= 8)) {
        campo.value = vr.substring(0, 2) + '.' + vr.substring(2, 5) + '.' + vr.substring(5, tam);
    }
    if ((tam >= 8) && (tam <= 11)) {
        campo.value = vr.substring(0, 2) + '.' + vr.substring(2, 5) + '.' + vr.substring(5, 8) + '/' + vr.substring(8, tam);
    }
    if (tam == 12) {
        campo.value = vr.substring(0, 2) + '.' + vr.substring(2, 5) + '.' + vr.substring(5, 8) + '/' + vr.substring(8, tam);
    }
    if (tam >= 13) {
        campo.value = vr.substring(0, 2) + '.' + vr.substring(2, 5) + '.' + vr.substring(5, 8) + '/' + vr.substring(8, 12) + '-' + vr.substring(12, 14);
    }

}


//auto tab
jQuery.fn.autotabindex = function() {
    var keypressed = null;
   
    jQuery(this).click(function() {
        $(this).select();
    });
    jQuery(this).keyup(function(e) {
        if (window.event) keycode = window.event.keyCode;
        else if (e) keycode = e.which;
        keypressed = keycode;
        switch (keypressed) {
            // ignore the following keys   
            case 9: // tab
                return false;
            case 16: // shift
                return false;
            case 20: // capslock
                return false;
            default: // any other keyup actions will trigger
                //alert(keypressed);
                var maxlength = jQuery(this).attr('maxlength'); // get maxlength value
                var inputlength = jQuery(this).val().length; // get the length of the text
                if (inputlength >= maxlength) { // if the text is equal of more than the max length
                    next = parseInt($(this).attr("tabindex")) + 1;
                    end = next + 5;
                    for (next; next < end; next++) {

                        if ($("[tabindex='" + next + "']").is(':visible')) {
                            
                            $("[tabindex='" + next + "']").select();
                            $("[tabindex='" + next + "']").focus();
                            break
                        }
                    }
                }
        }
    });
}

//Funcao: MascaraMoeda
//-----------------------------------------------------
function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true;
    var t = new String(objTextBox.value);
    if (whichCode == 8) {
        objTextBox.value = t.substring(0, t.length - 1);
    }
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

//------------------------------------- Valida Data -------------------------------------------

function validarData(oSrc, args) {
    return args.IsValid = validaData(args.Value);
}

function validaData(_data) {
    if (_data.length < 10) {
        return false;
    }

    var dia = _data.substr(0, 2);
    var mes = _data.substr(3, 2);
    var ano = _data.substr(6, 4);

    if (dia > 0 && dia < 32 && mes > 0 && mes < 13 && ano > 1900) {
        data = new Date(ano, mes - 1, dia);
        if (!isDate(ano, mes, dia)) {
            return false;
        }
        if (data >= new Date()) {
            return false;
        }
    }
    else {
        return false;
    }
    return true;
}


function getYear(d) {
    return (d < 1000) ? d + 1900 : d;
}

function isDate(year, month, day) {

    // month argument must be in the range 1 - 12
    month = month - 1; // javascript month range : 0- 11
    var tempDate = new Date(year, month, day);
    if ((getYear(tempDate.getYear()) == year) && (month == tempDate.getMonth()) && (day == tempDate.getDate()))
        return true;
    else
        return false
}

/*
function maskPedOnLine() { 


        (function($) {
            // call setMask function on the document.ready event
            $(function() {
                $('.ipt-cpf').setMask('cpf');
                $('.ipt-cnpj').setMask('cnpj');
                $('.datejv').setMask('date');
                $('#ctl00_ContentPlaceHolder01_ucFormularioCadastro1_txtcep_digito').setMask('cepDig');
            }
        );
        })(jQuery);

}
*/

function redirectServico(opcao){
    var lnkMundo = document.getElementById('slcServico');
    if(lnkMundo.value == 'Default.aspx') { return false; }
    else window.open(opcao,'_blank');
}



// Listar as categorias de Noticias
function ListarCategorias(categoriaID) {
    window.location.href = 'noticias.aspx?categoriaID=' + categoriaID;   
}
