function ValidarCNPJ(ObjCnpj){
        var cnpj = ObjCnpj.value;
        var valida = new Array(6,5,4,3,2,9,8,7,6,5,4,3,2);
        var dig1= new Number;
        var dig2= new Number;
        
        exp = /\.|\-|\//g
        cnpj = cnpj.toString().replace( exp, "" ); 
        var digito = new Number(eval(cnpj.charAt(12)+cnpj.charAt(13)));
                
        for(i = 0; i<valida.length; i++){
                dig1 += (i>0? (cnpj.charAt(i-1)*valida[i]):0);  
                dig2 += cnpj.charAt(i)*valida[i];       
        }
        dig1 = (((dig1%11)<2)? 0:(11-(dig1%11)));
        dig2 = (((dig2%11)<2)? 0:(11-(dig2%11)));
        
        if(((dig1*10)+dig2) != digito)
               {  
                alert('CNPJ Invalido!');
                ObjCnpj.focus();
                }
}

function fone(obj,prox) {
switch (obj.value.length) {
        case 1:
                obj.value = "(" + obj.value;
                break;
        case 3:
                obj.value = obj.value + ")";
                break;  
        case 8:
                obj.value = obj.value + "-";
                break;  
        case 13:
                prox.focus();
                break;
}
}
function formata_data(obj,prox) {
switch (obj.value.length) {
        case 2:
                obj.value = obj.value + "/";
                break;
        case 5:
                obj.value = obj.value + "/";
                break;
        case 10:
                prox.focus();
                break;
}
}

function formata_cep(obj,prox) {
switch (obj.value.length) {
        case 2:
                obj.value = obj.value + ".";
                break;
        case 6:
                obj.value = obj.value + "-";
                break;
        case 10:
                prox.focus();
                break;
}
}


function Apenas_Numeros(caracter)
{
  var nTecla = 0;
  if (document.all) {
          nTecla = caracter.keyCode;
  } else {
          nTecla = caracter.which;
  }
  if ((nTecla> 47 && nTecla <58)
  || nTecla == 8 || nTecla == 127
  || nTecla == 0 || nTecla == 9  // 0 == Tab
  || nTecla == 13) { // 13 == Enter
          return true;
  } else {
          return false;
  }
}
function validaCPF(cpf) 
 {
   erro = new String;
 
        if (cpf.value.length == 11)
        {       
                        cpf.value = cpf.value.replace('.', '');
                        cpf.value = cpf.value.replace('.', '');
                        cpf.value = cpf.value.replace('-', '');
 
                        var nonNumbers = /\D/;
        
                        if (nonNumbers.test(cpf.value)) 
                        {
                                        erro = "A verificacao de CPF suporta apenas números!"; 
                        }
                        else
                        {
                                        if (cpf.value == "00000000000" || 
                                                        cpf.value == "11111111111" || 
                                                        cpf.value == "22222222222" || 
                                                        cpf.value == "33333333333" || 
                                                        cpf.value == "44444444444" || 
                                                        cpf.value == "55555555555" || 
                                                        cpf.value == "66666666666" || 
                                                        cpf.value == "77777777777" || 
                                                        cpf.value == "88888888888" || 
                                                        cpf.value == "99999999999") {
                                                        
                                                        erro = "Número de CPF inválido!"
                                        }
        
                                        var a = [];
                                        var b = new Number;
                                        var c = 11;
 
                                        for (i=0; i<11; i++){
                                                        a[i] = cpf.value.charAt(i);
                                                        if (i < 9) b += (a[i] * --c);
                                        }
        
                                        if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
                                        b = 0;
                                        c = 11;
        
                                        for (y=0; y<10; y++) b += (a[y] * c--); 
        
                                        if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
        
                                        if ((cpf.value.charAt(9) != a[9]) || (cpf.value.charAt(10) != a[10])) {
                                                erro = "Número de CPF inválido.";
                                                
                                        }
                        }
        }
        else
        {
                if(cpf.value.length == 0)
                        return false
                else
                        erro = "Número de CPF inválido.";
        }
        if (erro.length > 0) {
                        alert(erro);
                        
                        return false;
        }
             
        return true;    
 }
 
 //envento onkeyup
 function maskCPF(CPF) {
        var evt = window.event;
        kcode=evt.keyCode;
        if (kcode == 8) return;
        if (CPF.value.length == 3) { CPF.value = CPF.value + '.'; }
        if (CPF.value.length == 7) { CPF.value = CPF.value + '.'; }
        if (CPF.value.length == 11) { CPF.value = CPF.value + '-'; }
 }
 
 // evento onBlur
 function formataCPF(CPF)
 {
        with (CPF)
        {
                value = value.substr(0, 3) + '.' + 
                                value.substr(3, 3) + '.' + 
                                value.substr(6, 3) + '-' +
                                value.substr(9, 2);
        }
 }
 function retiraFormatacao(CPF)
 {
        with (CPF)
        {
                value = value.replace (".","");
                value = value.replace (".","");
                value = value.replace ("-","");
                value = value.replace ("/","");
        }
 }



String.prototype.extenso = function(c){
    var ex = [
        ["zero", "um", "dois", "três", "quatro", "cinco", "seis", "sete", "oito", "nove", "dez", "onze", "doze", "treze", "quatorze", "quinze", "dezesseis", "dezessete", "dezoito", "dezenove"],
        ["dez", "vinte", "trinta", "quarenta", "cinqüenta", "sessenta", "setenta", "oitenta", "noventa"],
        ["cem", "cento", "duzentos", "trezentos", "quatrocentos", "quinhentos", "seiscentos", "setecentos", "oitocentos", "novecentos"],
        ["mil", "milhão", "bilhão", "trilhão", "quadrilhão", "quintilhão", "sextilhão", "setilhão", "octilhão", "nonilhão", "decilhão", "undecilhão", "dodecilhão", "tredecilhão", "quatrodecilhão", "quindecilhão", "sedecilhão", "septendecilhão", "octencilhão", "nonencilhão"]
    ];
    var a, n, v, i, n = this.replace(c ? /[^,\d]/g : /\D/g, "").split(","), e = " e ", $ = "real", d = "centavo", sl;
    for(var f = n.length - 1, l, j = -1, r = [], s = [], t = ""; ++j <= f; s = []){
        j && (n[j] = (("." + n[j]) * 1).toFixed(2).slice(2));
        if(!(a = (v = n[j]).slice((l = v.length) % 3).match(/\d{3}/g), v = l % 3 ? [v.slice(0, l % 3)] : [], v = a ? v.concat(a) : v).length) continue;
        for(a = -1, l = v.length; ++a < l; t = ""){
            if(!(i = v[a] * 1)) continue;
            i % 100 < 20 && (t += ex[0][i % 100]) ||
            i % 100 + 1 && (t += ex[1][(i % 100 / 10 >> 0) - 1] + (i % 10 ? e + ex[0][i % 10] : ""));
            s.push((i < 100 ? t : !(i % 100) ? ex[2][i == 100 ? 0 : i / 100 >> 0] : (ex[2][i / 100 >> 0] + e + t)) +
            ((t = l - a - 2) > -1 ? " " + (i > 1 && t > 0 ? ex[3][t].replace("ão", "ões") : ex[3][t]) : ""));
        }
        a = ((sl = s.length) > 1 ? (a = s.pop(), s.join(" ") + e + a) : s.join("") || ((!j && (n[j + 1] * 1 > 0) || r.length) ? "" : ex[0][0]));
        a && r.push(a + (c ? (" " + (v.join("") * 1 > 1 ? j ? d + "s" : (/0{6,}$/.test(n[0]) ? "de " : "") + $.replace("l", "is") : j ? d : $)) : ""));
    }
    return r.join(e);
}
