function Mascara(objeto, evt, mask) {
    var LetrasU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var LetrasL = 'abcdefghijklmnopqrstuvwxyz';
    var Letras = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    var Numeros = '0123456789';
    var Fixos = '().-:/ ';
    var Charset = " !\"#$%&\"()*+,-./0123456789:;<=>?   @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_/`abcdefghijklmnopqrstuvwxyz{|}~";

    evt = (evt) ? evt : (window.event) ? window.event : "";

    var value = objeto.value;

    if (evt) {

        var ntecla = (evt.which) ? evt.which : evt.keyCode;
   
        tecla = Charset.substr(ntecla - 32, 1);
   
        if (ntecla < 32) return true;
   
        var tamanho = value.length;
   
        if (tamanho >= mask.length) return false;
        var pos = mask.substr(tamanho,1);
        while (Fixos.indexOf(pos) != -1) {
            value += pos;
            tamanho = value.length;
            if (tamanho >= mask.length) return false;
            pos = mask.substr(tamanho,1);
        }
       
        switch (pos) {
            //case '#' : if (Numeros.indexOf(tecla) == -1) return false; break;
            case '#' : if (!somenteNumeros(evt)) return false; break;
            case 'A' : if (LetrasU.indexOf(tecla) == -1) return false; break;
            case 'a' : if (LetrasL.indexOf(tecla) == -1) return false; break;
            case 'Z' : if (Letras.indexOf(tecla) == -1) return false; break;
            case '*' : objeto.value = value; return true; break;
            default : return false; break;
        }

    }
    objeto.value = value;
    return true;
} 

function somenteNumeros(e) {
var key;
if(window.event) {
  key = e.keyCode;
}
else if(e.which) {
   key = e.which;
}
 
key = (e.which) ? e.which : e.keyCode;
 
if (key==13 || key == 9){
   return true;
}
 
if (key!=8 || key < 48 || key > 57) return (((key > 47) && (key < 58)) || (key==8));

{
    return true;
}
}


function MaskData(objeto, evt) 
{
  return Mascara(objeto, evt, '##/##/####');
}  
function hiddenCPF()
{
 var form = document.forms['form1'];
 var tr = document.getElementById('trCnpj');
 if(form.cpf.value != ''){
    form.cnpj.value = '';
    tr.style.display = 'none';
 }
 if(form.cpf.value == '')
 {
  tr.style.display = '';
 }
}

function hiddenCNPJ()
{
 var form = document.forms['form1'];
 var tr = document.getElementById('trCpf');
 if(form.cnpj.value != ''){
    form.cpf.value = '';
    tr.style.display = 'none';
 }
 if(form.cnpj.value == '')
 {
  tr.style.display = '';
 }
}
function MaskCPF(objeto, evt) 
{ 

 return Mascara(objeto, evt, '###.###.###-##');   
}  

function MaskCNPJ(objeto, evt)
{     
 return Mascara(objeto, evt, '##.###.###/####-##');  
}

function validarCPF (CPF) 
{
   CPF = LimpaFormatoStr(CPF);
    
    if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
        CPF == "22222222222" ||    CPF == "33333333333" || CPF == "44444444444" ||
        CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
        CPF == "88888888888" || CPF == "99999999999")
        return false;
    soma = 0;
    for (i=0; i < 9; i ++)
        soma += parseInt(CPF.charAt(i)) * (10 - i);
    resto = 11 - (soma % 11);
    if (resto == 10 || resto == 11)
        resto = 0;
    if (resto != parseInt(CPF.charAt(9)))
        return false;
    soma = 0;
    for (i = 0; i < 10; i ++)
        soma += parseInt(CPF.charAt(i)) * (11 - i);
    resto = 11 - (soma % 11);
    if (resto == 10 || resto == 11)
        resto = 0;
    if (resto != parseInt(CPF.charAt(10)))
        return false;
    return true;
}

function validarCNPJ(CNPJ) 
{
   //CNPJ = LimpaFormatoStr(CNPJ);
   erro = new String;
   if (CNPJ.length < 18) erro += "É necessario preencher corretamente o número do CNPJ! \n\n";
   if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
   if (erro.length == 0) erro += "É necessário preencher corretamente o número do CNPJ! \n\n";

   }
   //substituir os caracteres que não são números
   if(document.layers && parseInt(navigator.appVersion) == 4){
            x = CNPJ.substring(0,2);
            x += CNPJ. substring (3,6);
            x += CNPJ. substring (7,10);
            x += CNPJ. substring (11,15);
            x += CNPJ. substring (16,18);
                       CNPJ = x;
               } else {
                       CNPJ = CNPJ. replace (".","");
                       CNPJ = CNPJ. replace (".","");
                       CNPJ = CNPJ. replace ("-","");
                       CNPJ = CNPJ. replace ("/","");
               }
               var nonNumbers = /\D/;
               if (nonNumbers.test(CNPJ)) erro += "A verificação de CNPJ suporta apenas números! \n\n";
               var a = [];
               var b = new Number;

               var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
               for (i=0; i<12; i++){
                       a[i] = CNPJ.charAt(i);
                       b += a[i] * c[i+1];
 }
               if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
               b = 0;
               for (y=0; y<13; y++) {
                       b += (a[y] * c[y]);
               }
               if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
               if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
                       erro +="Dígito verificador com problema!";
               }
               if (erro.length > 0){
                       return false;
               } else {
               }
               return true;
}

function LimpaFormatoStr(strnoformat)
{
    strnoformat = strnoformat.replace(",","");
    strnoformat = strnoformat.replace("/","");
    strnoformat = strnoformat.replace("-","");
    strnoformat = strnoformat.replace(".","");
    strnoformat = strnoformat.replace(".","");    
    return strnoformat;
}

function verificaData(campo){

    if (campo.value == '')
        return true;
   
    Data = campo.value;
    var dma = -1;
    var data = Array(3);
    var ch = Data.charAt(0);
    var valor = 0;
    for(i=0; i < Data.length && (( ch >= '0' && ch <= '9' ) || ( ch == '/' && i != 0 ) ); ) {
        data[++dma] = '';
        if(ch!='/' && i != 0) valor = 1;
       
        if(i != 0 ) ch = Data.charAt(++i);
       
        if(ch=='0') ch = Data.charAt(++i);
        while( ch >= '0' && ch <= '9' ){
            data[dma] += ch;
            ch = Data.charAt(++i);
        }
    }
    if(ch!='') valor = 1;
    if(data[0] == '' || isNaN(data[0]) || parseInt(data[0]) < 1) valor = 1;
    if(data[1] == '' || isNaN(data[1]) || parseInt(data[1]) < 1 || parseInt(data[1]) > 12) valor = 1;
    if(data[2] == '' || isNaN(data[2]) || ((parseInt(data[2]) < 0 || parseInt(data[2]) > 99 ) && (parseInt(data[2]) < 1900 || parseInt(data[2]) > 9999))) valor = 1;
    if(data[2] < 50) data[2] = parseInt(data[2]) + 2000;
    else if(data[2] < 100) data[2] = parseInt(data[2]) + 1900;

    switch(parseInt(data[1])){
        case 2: { if(((parseInt(data[2])%4!=0 || (parseInt(data[2])%100==0 && parseInt(data[2])%400!=0)) && parseInt(data[0]) > 28) || parseInt(data[0]) > 29 ) valor = 1; break; }
        case 4: case 6: case 9: case 11: { if(parseInt(data[0]) > 30) valor = 1; break;}
        default: { if(parseInt(data[0]) > 31) valor = 1;}
    }
   
    if (valor == 1) {
        alert('Data inválida!');
        campo.value = '';
        campo.focus();
        return false;
    }
    return true;
} 

function valida() 
{
var teste = true;
 if (document.forms[0].dataNascimento.value == '' ) {
    alert('Por favor, informe a Data de Nascimento.');
    document.forms[0].dataNascimento.focus();   
    teste = false;
    return false;    
  }
 
 if(verificaData(document.forms[0].dataNascimento)==false)
 {
    teste = false;
    return false;
 }
 
  if(document.forms[0].email.value == '' ) {
    alert('Por favor, informe um E-mail.');
    document.forms[0].email.focus();     
    teste = false;
    return false;
  }
  if (document.forms[0].cpf.value != '') {
   if (!validarCPF(document.forms[0].cpf.value)) {
        alert('Por favor, informe um CPF válido.');
        document.forms[0].cpf.focus();  
        teste = false;
        return false;
    }   
  } 
  if (document.getElementById('trCpf').style.display == '' && document.forms[0].cpf.value == '') {   
        alert('Por favor, informe um CPF.');
        document.forms[0].cpf.focus();     
        teste = false;
        return false;
  }
  if( document.forms[0].cnpj.value != '') {
      if (!validarCNPJ(document.forms[0].cnpj.value)) {
        alert('Por favor, informe um CNPJ válido.');
        document.forms[0].cnpj.focus();   
        teste = false;
        return false;
    }
   }
   if(document.getElementById('trCnpj').style.display == '' && document.forms[0].cnpj.value == '') {      
        alert('Por favor, informe um CNPJ.');
        document.forms[0].cnpj.focus();  
        teste = false;
        return false;
   } 
    if(teste == true)
    {    
     return true;  
    }
}

function validaParceiro() 
{
var teste = true;
 if (document.forms[0].dataNascimento.value == '' ) {
    alert('Por favor, informe a Data de Nascimento.');
    document.forms[0].dataNascimento.focus();   
    teste = false;
    return false;    
  }
 
 if(verificaData(document.forms[0].dataNascimento)==false)
 {
    teste = false;
    return false;
 }
 
  if(document.forms[0].email.value == '' ) {
    alert('Por favor, informe um E-mail.');
    document.forms[0].email.focus();     
    teste = false;
    return false;
  }

  if( document.forms[0].cnpj.value != '') {
      if (!validarCNPJ(document.forms[0].cnpj.value)) {
        alert('Por favor, informe um CNPJ válido.');
        document.forms[0].cnpj.focus();   
        teste = false;
        return false;
    }
   }
   
   if(document.getElementById('trCnpj').style.display == '' && document.forms[0].cnpj.value == '') {      
        alert('Por favor, informe um CNPJ.');
        document.forms[0].cnpj.focus();  
        teste = false;
        return false;
   } 
   
    if(teste == true)
    {    
     return true;  
    }
}



