// JavaScript Document
function val_cpf(numero1) {
    dig_1 = 0;
    dig_2 = 0;
    controle_1 = 10;
    controle_2 = 11;
    lsucesso = 1;
    numero = numero1.value;
    numero = numero.replace(".","");
    numero = numero.replace(".","");
    numero = numero.replace(".","");
    numero = numero.replace("-","");
    if (numero.length != 11) {
        return false;
    }
    else {
        for ( i=0 ; i < 9 ; i++) {
            dig_1 = dig_1 + parseInt(numero.substring(i, i+1) * controle_1);
            controle_1 = controle_1 - 1;
        }
    
        resto = dig_1 % 11;
        dig_1 = 11 - resto;
        if ((resto == 0) || (resto == 1)) {
            dig_1 = 0;
        }
        for ( i=0 ; i < 9 ; i++) {
            dig_2 = dig_2 + parseInt(numero.substring(i, i + 1) * controle_2);
            controle_2 = controle_2 - 1;
        }
        dig_2 = dig_2 + 2 * dig_1;
        resto = dig_2 % 11;
        dig_2 = 11 - resto;
        if ((resto == 0) || (resto == 1)) {
            dig_2 = 0;
        }
        dig_ver = (dig_1 * 10) + dig_2;
        if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length))) {
            return false;
        }
    }
    return true;
}

function val_email(Field) {
    if (!Field.value) {
        return false;
    }
    if ((Field.value.indexOf("@") == -1) || (Field.value.indexOf(".") == -1) || (Field.value.indexOf(" ") != -1) || (Field.value.length < 6)) {
        return false;
    } else {
        return true;
    }
}   

function ValidDate(data) {
    if (!data) {
        return false;
    }
    var v_dia;
    var v_mes;
    var v_ano;
    v_dia = data.substr(0,2);
    v_mes = data.substr(3,2);
    v_ano = data.substr(6,4);

    if (!valida_inteiro(v_dia) || !valida_inteiro(v_mes) || !valida_inteiro(v_ano)){
        return false;
    }
    if (v_dia.length < 1) {
        return false;
    }
    if (v_mes.length < 1) {
        return false;
    }
    if (v_ano.length < 4) {
        return false;
    }
    if (((v_ano < 1900) || (v_ano > 2079)) && (v_ano.length != 0)) {
        return false;
    }
    if (v_dia > 31) {
        return false;
    }
    if (v_mes > 12) {
        return false;
    }
    if (v_dia == "31") {
        if ((v_mes == "04") || (v_mes == "06") || (v_mes == "09") || (v_mes == "11")) {
            return false;
        }
    }
    if (v_mes == "02") {
        if (!(v_ano%4)) {
            if (v_dia > 29) {
                return false;
            }
        } else if (v_dia > 28) {
            return false;
        }
    }
    //o -if- abaixo testa se algum campo foi preenchido e outro deixado em branco deixando a data incompleta
    if (((v_dia != "") || (v_mes != "") || (v_ano != "")) && ((v_dia == "") || (v_mes == "") || (v_ano == ""))) {
        return false;
    }
    return true;
}

function verifica_branco(parametro)   // FUNCAO PARA VERIFICAÇÃO DE CAMPOS NÃO PREENCHIDOS
// OU PREENCHIDOS APENAS COM ESPAÇOS EM BRANCO
{
    teste_parametro = "false"; //variavel para teste de espacos em branco
    tamanho_parametro = parametro.length;
    if (tamanho_parametro != 0)
    {
        for (i = 0; i < tamanho_parametro; i++)
        {
            if (parametro.charAt(i) != " ")

            {
                teste_parametro = "true"; /*existe caracter diferente de branco*/
            }
        }
        if (teste_parametro == "false")  //todos os caracteres digitados são brancos
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        return false;
    }
		
}


function valida_inteiro(parametro)  //FUNCAO PARA VALIDACAO DE NÚMEROS INTEIROS, E ESPAÇOS EM BRANCO
{
    if (parametro.length != 0)
    {
        if (!verifica_branco(parametro))
        {
            return true;
        }
		
        teste_ponto = "false";
        tamanho_parametro = parametro.length;
		
        if (isNaN(parametro)) //valor digitado não é numérico
        {
            return false;
        }
        else //valor digitado é um numérico válido
        {
			
            for (k = 0; k < tamanho_parametro; k++)
            {
                if ((parametro.charAt(k) == '.') || (parametro.charAt(k) == '-') || (parametro.charAt(k) == '+'))

                {
                    teste_ponto = "true"; /*existe caracter ponto*/
                }
            }
			
            if (teste_ponto == "true") //encontrou caracter ponto(numero real)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
    else
    {
        return true;
    }
	
}

function centro(p,n,l,a,v1,v2,v3,v4,v5,v6,v7,v8,v9){
    var l = l;
    var a = a;
    var t = (screen.height/2)-(a/2);
    var d = (screen.width/2)-(l/2);
    window.open(p,n,'width='+l+',height='+a+',top='+t+',left='+d+',scrollbars='+v1+',menubar='+v2+',directories='+v3+',location='+v4+',copyhistory='+v5+',status='+v6+',toolbar='+v7+',maximized='+v8+',resizable='+v9+'');
}
function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

    if(document.all) { // Internet Explorer
        nTecla = evtKeyPress.keyCode;
    }
    else if(document.layers) { // Nestcape
        nTecla = evtKeyPress.which;
    }

    sValue = objForm[strField].value;
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( " ", "" );
    fldLen = sValue.length;
    mskLen = sMask.length;
    
    
    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;

    while (i <= mskLen) {
        bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == ":") || (sMask.charAt(i) == "/"))
        bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

        if (bolMask) {
            sCod += sMask.charAt(i);
            mskLen++;
        }
        else {
            sCod += sValue.charAt(nCount);
            nCount++;
        }

        i++;
    }

    objForm[strField].value = sCod;

    if (nTecla != 8) {
        if (sMask.charAt(i-1) == "9") {
            return ((nTecla > 47) && (nTecla < 58));
        }
        else {
            return true;
        }
    }
    else {
        return true;
    }
}

//StrReplace 
function strreplace(str, char_busca, char_substr) {
    cnt = str.length;
    var str_novo = '';
    for (var vdigpos = 0; vdigpos < cnt; vdigpos++ ) {
        vdig = str.substr(vdigpos,1);
        if (vdig == char_busca) {
            str_novo += char_substr;
        }
        else {
            str_novo += vdig;
        }
    }
    return str_novo;
}

//valida cpf
function chkcpf(vcic){
    var cic_old = vcic.value;
    vcic.value = strreplace(vcic.value, '.', '');
    vcic.value = strreplace(vcic.value, '-', '');
    expr  = new RegExp("0{11}|1{11}|2{11}|3{11}|4{11}|5{11}|6{11}|7{11}|8{11}|9{11}");
    if (vcic.value.match(expr)){
        vcic.value = cic_old;
        vcic.select();
        vcic.focus();
        alert('C.P.F. Inválido');
        return false;
    }
    if (isNaN(vcic.value) || vcic.value.length != 11){
        vcic.value = cic_old;
        vcic.select();
        vcic.focus();
        alert("Valor do C.P.F. Inválidos");
        return false;
    }
    for (var vdigpos = 10; vdigpos < 12; vdigpos++ ){
        var vdig = 0;
        var vpos = 0;
        for (var vfator = vdigpos;vfator >= 2; vfator-- ){
            vdig = eval(vdig + vcic.value.substr(vpos,1) * vfator);
            vpos++;
        }
        vdig  = eval(11 -(vdig % 11)) < 10 ? eval(11 - vdig % 11) : 0;
        if (vdig != eval(vcic.value.substr(vdigpos-1,1))) {
            vcic.value = cic_old;
            vcic.select();
            vcic.focus();
            alert('C.P.F. Inválido');
            return false;
        }
    }
    return true;
}

//validação de cnpj
function chkcnpj(vcnpj){
    var cnpj_old = vcnpj.value;
    vcnpj.value = strreplace(vcnpj.value, '.', '');
    vcnpj.value = strreplace(vcnpj.value, '/', '');
    vcnpj.value = strreplace(vcnpj.value, '-', '');
    if (isNaN(vcnpj.value) || vcnpj.value.length != 14){
        vcnpj.value = cnpj_old;
        vcnpj.select();
        vcnpj.focus();
        alert("C.N.P.J. Inválido !");
        return false;
    }
    for (var vdigpos = 13; vdigpos < 15; vdigpos++ ){
        var vdig = 0;
        var vpos = 0;
        for (var vfator = vdigpos - 8 ;vfator >= 2; vfator-- ){
            vdig = eval(vdig + vcnpj.value.substr(vpos,1) * vfator);
            vpos++;
        }
        for (var vfator = 9 ;vfator >= 2; vfator-- ){
            vdig = eval(vdig + vcnpj.value.substr(vpos,1) * vfator);
            vpos++;
        }
        vdig  = eval(11 -(vdig % 11)) < 10 ? eval(11 - vdig % 11) : 0;
        if (vdig != eval(vcnpj.value.substr(vdigpos-1,1))) {
            vcnpj.value = cnpj_old;
            vcnpj.select();
            vcnpj.focus();
            alert("C.N.P.J. Inválido !");
            return false;
        }
    }
    return true;
}


function Integer()
{
    tecla = event.keyCode;
    Vchar = String.fromCharCode(tecla);
    er = /^[0-9]$/;
    if (!er.test(Vchar))
        event.returnValue = false;
}

// JavaScript Document

function valida(campo,msg,tipo)
{
    if (!tipo) tipo=0;
	
    if (campo.value=='')
    {
        alert('Favor informar '+msg+' !');
		
        if (tipo==0)
            campo.focus();
			
        return false;
    }
	
    return true;
}


function FormatarCEP(campo)
{
    tecla = event.keyCode;
    Vchar = String.fromCharCode(tecla);
    size = campo.value.length;
    er = /^[0-9]$/;
    if (!er.test(Vchar))
        event.returnValue = false;
    else
    {
        if (size == 5)
            campo.value += '-';
        if (size > 9)
            event.returnValue = false;
    }
}

function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

    if(document.all) { // Internet Explorer
        nTecla = evtKeyPress.keyCode;
    }
    else if(document.layers) { // Nestcape
        nTecla = evtKeyPress.which;
    }

    if (evtKeyPress.keyCode != 8) {

        sValue = objForm[strField].value;
    
        // Limpa todos os caracteres de formatação que
        // já estiverem no campo.
        sValue = sValue.toString().replace( "-", "" );
        sValue = sValue.toString().replace( "-", "" );
        sValue = sValue.toString().replace( ".", "" );
        sValue = sValue.toString().replace( ".", "" );
        sValue = sValue.toString().replace( ":", "" );
        sValue = sValue.toString().replace( "/", "" );
        sValue = sValue.toString().replace( "/", "" );
        sValue = sValue.toString().replace( "(", "" );
        sValue = sValue.toString().replace( "(", "" );
        sValue = sValue.toString().replace( ")", "" );
        sValue = sValue.toString().replace( ")", "" );
        sValue = sValue.toString().replace( " ", "" );
        sValue = sValue.toString().replace( " ", "" );
        fldLen = sValue.length;
        mskLen = sMask.length;

        i = 0;
        nCount = 0;
        sCod = "";
        mskLen = fldLen;

        while (i <= mskLen) {
            bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == ":") || (sMask.charAt(i) == "/"))
            bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

            if (bolMask) {
                sCod += sMask.charAt(i);
                mskLen++;
            }
            else {
                sCod += sValue.charAt(nCount);
                nCount++;
            }

            i++;
        }

        objForm[strField].value = sCod;

    }

    if (nTecla != 8) { // backspace
        if (sMask.charAt(i-1) == "9") { // apenas números...
            return ((nTecla > 47) && (nTecla < 58));
        } // números de 0 a 9
        else { // qualquer caracter...
            return true;
        }
    }
    else {
        return true;
    }
}

function MM_preloadImages() { //v3.0
    var d=document;
    if(d.images){
        if(!d.MM_p) d.MM_p=new Array();
        var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
        for(i=0; i<a.length; i++)
            if (a[i].indexOf("#")!=0){
                d.MM_p[j]=new Image;
                d.MM_p[j++].src=a[i];
            }
    }
}

function Integer()
{
    tecla = event.keyCode;
    Vchar = String.fromCharCode(tecla);
    er = /^[0-9]$/;
    if (!er.test(Vchar))
        event.returnValue = false;
}

function PreencheHidden(campo,valor)
{
    campo.value = valor;
}

function roundFloat(num)
{
    var negativo = num<0;
    if(negativo) num = -num;
    var teste = new String(num);
	
    decimal = (teste.length)-teste.indexOf(".");

    if(teste.indexOf(".")==-1)
    {
        teste = teste + ".00";
    }
    else if(teste.length-teste.indexOf(".")>2)
    {
        //arredonda o valor de teste para cima (round up) a partir da terceira casa decimal
        c = teste.substring(teste.indexOf(".")+3, teste.indexOf(".")+4);	//terceiro depois da virgula
			
        b = teste.substring(teste.indexOf(".")+2, teste.indexOf(".")+3); //segundo depois da virgula
			
        a = teste.substring(teste.indexOf(".")+1, teste.indexOf(".")+2); //primeiro depois da virgula
		
        if(b==9)
        {
            if(c>4)
            {
                a++;
                b=0;
            }
        }
        else
        {
            temp = b+"."+c;
            b = Math.round(temp);
        }
		
        if(a==10)
        {
            decimal = teste.substring(0, teste.indexOf(".")); //primeiro depois da virgula
            decimal++;
            teste = decimal+".00";
            return teste;
        }

							
        teste = teste.substring(0, teste.indexOf(".")+1)+a+b;
    }
    else if((teste.length)-teste.indexOf(".")==2)
    {
        teste = teste + "0";
    }
	
    if(negativo) return "-" + teste;
    return teste;
}

function strToNum(campo)
{
    var vr = campo
    vr = vr.replace( "/", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ",", "." );
	
    return vr;
}

function FormatarValor(tammax,teclapres)
{
    var tecla = teclapres.keyCode;
    Vchar = String.fromCharCode(tecla);
    er = /^[0-9]/;
    if (!er.test(Vchar))
        event.returnValue = false;
    vr = event.srcElement.value;
    vr = vr.replace( "/", "" );
    vr = vr.replace( ",", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( "-", "" );
    if (vr.length > 0)
    {
        vr = parseInt(vr,10);
    }
    vr = '' + vr
    tam = vr.length;
    if (tam < tammax && tecla != 8)
    {
        tam = vr.length + 1;
    }
    if (tecla == 8)
    {
        tam = tam - 1;
    }
    if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105)
    {
        if ( tam <= 2 )
        {
            if (tam == 1)
            {
                event.srcElement.value = "0,0" + vr ;
            }
            else if (tam == 2)
            {
                event.srcElement.value = "0," + vr ;
            }
            else
            {
                event.srcElement.value = "" ;
            }
        }
        if ((tam > 2) && (tam <= 5))
        {
            event.srcElement.value = vr.substr(0, tam - 2) + ',' + vr.substr(tam - 2, tam);
        }
        if ((tam >= 6) && (tam <= 8))
        {
            event.srcElement.value = vr.substr(0, tam - 5) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
        }
        if ((tam >= 9) && (tam <= 11))
        {
            event.srcElement.value = vr.substr(0, tam - 8 ) + '.' + vr.substr(tam - 8, 3) + '.' + vr.substr(tam - 5, 3 ) + ',' + vr.substr(tam - 2, tam) ;
        }
        if ((tam >= 12) && (tam <= 14))
        {
            event.srcElement.value = vr.substr(0, tam - 11) + '.' + vr.substr(tam - 11, 3 ) + '.' + vr.substr(tam - 8, 3) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam) ;
        }
        if ((tam >= 15) && (tam <= 17))
        {
            event.srcElement.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr(tam - 11, 3) + '.' + vr.substr(tam - 8, 3) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam) ;
        }
    }
}


function numToStrvalue(numero)
{
    var num = numero.replace(/\./g,'');
    tam = num.toString().length
 
    isNegative = (num.toString().substring(0,1)=='-')
    num = num.replace(/([a-z])|([A-Z])|(\,)|(\-)/g,'')
    num = num.replace(' ','')
    tam = num.toString().length;
 
    if (tam > 0)
    {
        num = parseInt(num,10);

    }
    tam = num.toString().length;
 
    if (tam > 2)
    {
        dec = num.toString().substring(tam-2,tam)
        num = num.toString().substring(0,tam-2)
 	 	
        if(!isNaN(num))
        {
            num = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1.').split('').reverse().join('').replace(/^[\.]/,'');
            if (isNegative)
                return '-'+num+','+dec;
		  
            else
                return num+','+dec;
        }
        else if(num.toString().substring(tam-1,tam)=='-')
        {
            return '-'+numero.substring(0,numero.length-1);
        }
        else
        {
            return numero.substring(0,numero.length-1);
        }
    }
    else
    {
        if (tam == 1)
        {
            if (isNegative)
            {
                return "-0,0" + num ;

            }
            else
                return "0,0" + num ;
        }
        else if (tam == 2)
        {
            if (isNegative)
                return  "-0," + num ;
            else
                return  "0," + num ;
        }
        else
        {
            return  "" ;
        }
    }
}

function format (obj, decimal) {
    //decimal  - the number of decimals after the digit from 0 to 3
    //-- Returns the passed number as a string in the xxx,xxx.xx format.
    anynum=eval(obj);
    divider =10;
    switch(decimal){
        case 0:
            divider =1;
            break;
        case 1:
            divider =10;
            break;
        case 2:
            divider =100;
            break;
        default:  	 //for 3 decimal places
            divider =1000;
    }

    workNum=Math.abs((Math.round(anynum*divider)/divider));

    workStr=""+workNum

    if (workStr.indexOf(".")==-1){
        workStr+="."
    }

    dStr=workStr.substr(0,workStr.indexOf("."));
    dNum=dStr-0
    pStr=','+workStr.substr((workStr.indexOf(".")+1))

    while (pStr.length-1< decimal){
        pStr+="0"
    }

    if(pStr ==',') pStr ='';

    //--- Adds a comma in the thousands place.
    if (dNum>=1000) {
        dLen=dStr.length
        dStr=parseInt(""+(dNum/1000))+"."+dStr.substring(dLen-3,dLen)
    }

    //-- Adds a comma in the millions place.
    if (dNum>=1000000) {
        dLen=dStr.length
        dStr=parseInt(""+(dNum/1000000))+"."+dStr.substring(dLen-7,dLen)
    }
    retval = dStr + pStr
    //-- Put numbers in parentheses if negative.
    if (anynum<0) {
        retval="("+retval+")";
    }

	  
    //You could include a dollar sign in the return value.
    //retval =  "$"+retval
    return retval;
}
function FormatarData(campo)
{
	
    if (campo.value.length==10)
        campo.value = '';
	
    tecla = event.keyCode;
    Vchar = String.fromCharCode(tecla);
    size = campo.value.length;
    er = /^[0-9]$/;
    if (!er.test(Vchar))
        event.returnValue = false;
    else
    {
        if (size == 2 || size == 5)
            campo.value += '/';
		
        if (size > 9)
            event.returnValue = false;
    }
}

function FormatarDataCartao(campo)
{
	
    if (campo.value.length==7)
        campo.value = '';
	
    tecla = event.keyCode;
    Vchar = String.fromCharCode(tecla);
    size = campo.value.length;
    er = /^[0-9]$/;
    if (!er.test(Vchar))
        event.returnValue = false;
    else
    {
        if (size == 2)
            campo.value += '/';
		
        if (size > 6)
            event.returnValue = false;
    }
}


function adicionarDias(data, dias)
{
    novaData = new Date(data.getTime() + (dias * 24 * 60 * 60 * 1000));

    var ano=novaData.getFullYear();
    var mes=novaData.getMonth();
    var dia=novaData.getDate();

    if (mes==0)
        mes = 12;

    if (mes<=9)
        mes = '0'+mes;

    return dia+'/'+mes+'/'+ano;

}
  

function ValidarData(data)
{

    data = data.replace(/[^0-9]/g,"");
    if (data.length != 8)
        return false;
    if (isNaN(data))
        return false;
    var dia = data.substr(0,2);
    var mes = data.substr(2,2);
    var ano = data.substr(4,4);
		
    if ((dia<1) || (dia>31))
        return false;
    if (((mes==1) || (mes==3) || (mes==5) || (mes==7) || (mes==8) || (mes==10) || (mes==12)) && (dia>31))
        return false;
    if (((mes==4) || (mes==6) || (mes==9) || (mes==11)) && (dia>30))
        return false;
    if ((mes > 12) || (mes<1))
        return false;
    if ((mes==2) && (isAnoBisexto(ano)) && (dia>29))
        return false;
    else if ((mes==2) && (dia>28))
        return false;
    else if (ano < 1900 || ano > 2078) //como data eh do tipo smalldatetime nao eh permitido salvar uma ano anterior a 1900, por restricoes do banco
        return false;
		
		
    return true;
}

function isAnoBisexto(ano)
{
    if ((ano%100==0) && (ano%400==0))
        return true;
    else if ((ano%4)==0)
        return true;
    return false;
}


function FormatarCPF(campo)
{
    tecla = event.keyCode;
    Vchar = String.fromCharCode(tecla);
    size = campo.value.length;
    er = /^[0-9]$/;
    if (!er.test(Vchar))
        event.returnValue = false;
    else
    {
        if (size == 3 || size == 7)
            campo.value += '.';
        if (size == 11)
            campo.value += '-';
        if (size > 13)
            event.returnValue = false;
    }
}


function ValidarCPF(cpf)
{
    er = /$1{11}^/;
    if (er.test(cpf))
        return false;
    cpf = cpf.replace(/[^0-9]/g,"");
    if (cpf.length!=11)
        return false;
    var c = cpf.substr(0,9);
    var dv = cpf.substr(9,2);
    var d1 = 0;
    for (i = 0; i < 9; i++)
        d1 += c.charAt(i) * (10-i);
    if (d1 == 0)
        return false;
    d1 = 11 - (d1%11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(0) != d1)
        return false;
    d1 *= 2;
    for (i = 0; i < 9; i++)
        d1 += c.charAt(i) * (11-i);
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1=0;
    if (dv.charAt(1) != d1)
        return false;
    return true;
}

//onkeypress="javascript:evita_letra(event);"
function evita_letra(tecla) {
    if (tecla.keyCode < 43 || tecla.keyCode > 57)
        tecla.returnValue = false;
}

//JQUERY

//valida cpf
function val_cpfJquery(element){
    vcic = element;
    var cic_old = vcic.value;
    vcic.value = strreplace(vcic.value, '.', '');
    vcic.value = strreplace(vcic.value, '-', '');
    expr  = new RegExp("0{11}|1{11}|2{11}|3{11}|4{11}|5{11}|6{11}|7{11}|8{11}|9{11}");
    if (vcic.value.match(expr)){
        vcic.value = cic_old;
        //alert('C.P.F. Inválido');
        return false;
    }
    if (isNaN(vcic.value) || vcic.value.length != 11){
        vcic.value = cic_old;
        //alert("Valor do C.P.F. Inválidos");
        return false;
    }
    for (var vdigpos = 10; vdigpos < 12; vdigpos++ ){
        var vdig = 0;
        var vpos = 0;
        for (var vfator = vdigpos;vfator >= 2; vfator-- ){
            vdig = eval(vdig + vcic.value.substr(vpos,1) * vfator);
            vpos++;
        }
        vdig  = eval(11 -(vdig % 11)) < 10 ? eval(11 - vdig % 11) : 0;
        if (vdig != eval(vcic.value.substr(vdigpos-1,1))) {
            vcic.value = cic_old;
            //alert('C.P.F. Inválido');
            return false;
        }
    }
    vcic.value = cic_old;
    return true;
}

//validação de cnpj
function chkcnpjJquery(element){
    vcnpj = element;
    var cnpj_old = vcnpj.value;
    vcnpj.value = strreplace(vcnpj.value, '.', '');
    vcnpj.value = strreplace(vcnpj.value, '/', '');
    vcnpj.value = strreplace(vcnpj.value, '-', '');
    if (isNaN(vcnpj.value) || vcnpj.value.length != 14){
        vcnpj.value = cnpj_old;
        //alert("C.N.P.J. Inválido !");
        return false;
    }
    for (var vdigpos = 13; vdigpos < 15; vdigpos++ ){
        var vdig = 0;
        var vpos = 0;
        for (var vfator = vdigpos - 8 ;vfator >= 2; vfator-- ){
            vdig = eval(vdig + vcnpj.value.substr(vpos,1) * vfator);
            vpos++;
        }
        for (var vfator = 9 ;vfator >= 2; vfator-- ){
            vdig = eval(vdig + vcnpj.value.substr(vpos,1) * vfator);
            vpos++;
        }
        vdig  = eval(11 -(vdig % 11)) < 10 ? eval(11 - vdig % 11) : 0;
        if (vdig != eval(vcnpj.value.substr(vdigpos-1,1))) {
            vcnpj.value = cnpj_old;
            //alert("C.N.P.J. Inválido !");
            return false;
        }
    }
    vcnpj.value = cnpj_old;
    return true;
}
