/*

<form name="form1">
<br><br>CEP:
<input type="text" name="cep" onkeypress="MascaraCep(form1.cep);"
 maxlength="10" onblur="ValidaCep(form1.cep)">
<br><br>DATA:
<input type="text" name="data" onkeypress="MascaraData(form1.data);"
 maxlength="10" onblur= "ValidaDataform1.data);">
<br><br>Telefone: 
<input type="text" name="tel" onkeypress="MascaraTelefone(form1.tel);" 
maxlength="14"  onblur="ValidaTelefone(form1.tel);">
<br><br>CPF:
<input type="text" name="cpf" onblur="ValidarCPF(form1.cpf);" 
onkeypress="MascaraCPF(form1.cpf);" maxlength="14">
<br><br>CNPJ:
<input type="text" name="cnpj" onkeypress="MascaraCNPJ(form1.cnpj);" 
maxlength="18" onblur="ValidarCNPJ(form1.cnpj);">
</form>

*/



// Javascript Document
//adiciona mascara de cnpj
function MascaraCNPJ(cnpj,event){
        if(mascaraInteiro(event)==false){
                event.returnValue = false;
        }       
        return formataCampo(cnpj, '00.000.000/0000-00', event);
}
//adiciona mascara de numero
function MascaraNumero(numero,event){
                if(mascaraInteiro(event)==false){
                event.returnValue = false;
        }       
        return formataCampo(numero, '0000000', event);
}

//adiciona mascara de cep
function MascaraCep(cep,event){
                if(mascaraInteiro(event)==false){
                event.returnValue = false;
        }       
        return formataCampo(cep, '00000-000', event);
}

//adiciona mascara de data
function MascaraData(data,event){
        if(mascaraInteiro(event)==false){
                event.returnValue = false;
        }       
        return formataCampo(data, '00/00/0000', event);
}

//adiciona mascara ao telefone
function MascaraTelefone(tel,event){  
        if(mascaraInteiro(event)==false){
                event.returnValue = false;
        }       
        return formataCampo(tel, '(00) 0000-0000', event);
}

//adiciona mascara ao CPF
function MascaraCPF(cpf,event){
        if(mascaraInteiro(event)==false){
                event.returnValue = false;
        }       
        return formataCampo(cpf, '000.000.000-00', event);
}

//valida telefone
function ValidaTelefone(tel){
        exp = /\(\d{2}\)\ \d{4}\-\d{4}/
        if(!exp.test(tel.value))
            return false;
        else return true;
}

//valida CEP
function ValidaCep(cep){
        exp = /\d{2}\d{3}\-\d{3}/
        if(!exp.test(cep.value))
            return false;
        else return true;           
}

//valida data
function ValidaData(data){
        exp = /\d{2}\/\d{2}\/\d{4}/
        if(!exp.test(data.value))
            return false;
        else return true;                   
}

function ValidarCPF (Objcpf) 
{                           
  var cpf = Objcpf.value;   
  exp = /\.|\-/g
  cpf = cpf.toString().replace( exp, "" ); 
  
  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;
  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;

}


//valida o CPF digitado
function ValidarCPF2(Objcpf){
        var cpf = Objcpf.value;
        exp = /\.|\-/g
        cpf = cpf.toString().replace( exp, "" ); 
        var digitoDigitado = eval(cpf.charAt(9)+cpf.charAt(10));
        var soma1=0, soma2=0;
        var vlr =11;
        
        for(i=0;i<9;i++){
                soma1+=eval(cpf.charAt(i)*(vlr-1));
                soma2+=eval(cpf.charAt(i)*vlr);
                vlr--;
        }       
        soma1 = (((soma1*10)%11)==10 ? 0:((soma1*10)%11));
        soma2=(((soma2+(2*soma1))*10)%11);
        
        var digitoGerado=(soma1*10)+soma2;
        if(digitoGerado!=digitoDigitado) 
            return false;
        else return true;      
}

//valida numero inteiro com mascara
function mascaraInteiro(event){

        if (event.keyCode < 48 || event.keyCode > 57){
                event.returnValue = false;
                return false;
        }
        return true;
}

//valida o CNPJ digitado
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
        if (cnpj != undefined) {
          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)  
            return false;
        else return true;
                
}

//formata de forma generica os campos
function formataCampo(campo, Mascara, evento) { 
        var boleanoMascara; 
        
        var Digitato = evento.keyCode;
        exp = /\-|\.|\/|\(|\)| /g
        campoSoNumeros = campo.value.toString().replace( exp, "" ); 
   
        var posicaoCampo = 0;    
        var NovoValorCampo="";
        var TamanhoMascara = campoSoNumeros.length;; 
        
        if (Digitato != 8) { // backspace 
                for(i=0; i<= TamanhoMascara; i++) { 
                        boleanoMascara  = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".")
                                                                || (Mascara.charAt(i) == "/")) 
                        boleanoMascara  = boleanoMascara || ((Mascara.charAt(i) == "(") 
                                                                || (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " ")) 
                        if (boleanoMascara) { 
                                NovoValorCampo += Mascara.charAt(i); 
                                  TamanhoMascara++;
                        }else { 
                                NovoValorCampo += campoSoNumeros.charAt(posicaoCampo); 
                                posicaoCampo++; 
                          }              
                  }      
                campo.value = NovoValorCampo;
                  return true; 
        }else { 
                return true; 
        }
}


function valida(theForm,tipo) {

    theForm = $(theForm);
   var retorno = 0;
   
   document.getElementById("alertaEmail").innerHTML = "";
   document.getElementById("alertaSenha").innerHTML = "";
   document.getElementById("alertaConfirmaSenha").innerHTML = "";
   document.getElementById("alertaEndereco").innerHTML = "";  
   document.getElementById("alertaNumero").innerHTML = ""; 
   document.getElementById("alertaBairro").innerHTML = ""; 
   document.getElementById("alertaCidade").innerHTML = "";
   document.getElementById("alertaEstado").innerHTML = "";
   document.getElementById("alertaCep").innerHTML = "";
   
   
   if (tipo == 'pf') {

    document.getElementById("alertaNascimento").innerHTML = "";
    document.getElementById("alertaNome").innerHTML = "";
    document.getElementById("alertaCpf").innerHTML = "";
    document.getElementById("alertaResidencial").innerHTML = "";
    
    if (!ValidaCep(theForm.cep)) {
  	    document.getElementById("alertaCep").innerHTML = "CEP inválido.";
        theForm.cep.focus();
	      retorno = 1;
    }
   
    if (!ValidaTelefone(theForm.telefone1)) {
  	    document.getElementById("alertaResidencial").innerHTML = "Você precisa informar um telefone para contato!";
        theForm.telefone1.focus();
	      retorno = 1;
    }
   
    if (ValidarCPF(theForm.cpf) == false) {
  	    document.getElementById("alertaCpf").innerHTML = "Você precisa informar um cpf válido!";
        theForm.cpf.focus();
	      retorno = 1;
    }  
   
    
  if (theForm.nome.value == ""){
  	  document.getElementById("alertaNome").innerHTML = "Você precisa informar o seu nome";
      theForm.nome.focus();
	  retorno = 1;
      }
  
  if (theForm.cpf.value == ""){
  	  document.getElementById("alertaCpf").innerHTML = "Você precisa informar um cpf válido!";
      theForm.cpf.focus();
      retorno = 1;
      }	
    
	  
  if (theForm.telefone1.value == ""){
      document.getElementById("alertaResidencial").innerHTML = "Você precisa informar um telefone para contato!";
      theForm.telefone1.focus();
      retorno = 1;
      }	
      
      
	  
  if (theForm.diaNascimento.value == "" || theForm.mesNascimento.value == "" || theForm.anoNascimento.value == ""){
  	  document.getElementById("alertaNascimento").innerHTML = "Informe uma data de nascimento valida";
      retorno = 1; 
	  }
  }
   else {
   
    document.getElementById("alertaRazaoSocial").innerHTML = "";
    document.getElementById("alertaCnpj").innerHTML = "";
    document.getElementById("alertaInscricaoEstadual").innerHTML = "";
    document.getElementById("alertaTelefoneComercial").innerHTML = "";
    
   //// PESSOA JURIDICA
   
    if (!ValidaCep(theForm.cep)) {
  	    document.getElementById("alertaCep").innerHTML = "CEP inválido.";
        theForm.cep.focus();
	      retorno = 1;
    }
   
    if (!ValidaTelefone(theForm.telefone1)) {
  	    document.getElementById("alertaTelefoneComercial").innerHTML = "Telefone inválido.";
        theForm.telefone1.focus();
	      retorno = 1;
    }

    if (!ValidarCNPJ(theForm.cnpj)) {
  	    document.getElementById("alertaCnpj").innerHTML = "CNPJ inválido.";
        theForm.cnpj.focus();
	      retorno = 1;
    }
  
    
  if (theForm.nome.value == ""){
  	  document.getElementById("alertaRazaoSocial").innerHTML = "Você precisa informar a sua Razão Social";
      theForm.nome.focus();
	  retorno = 1;
      }
  
  if (theForm.cnpj.value == ""){
  	  document.getElementById("alertaCnpj").innerHTML = "Você precisa informar um cnpj válido!";
      theForm.cnpj.focus();
      retorno = 1;
      }	
      
	  
  if (theForm.telefone1.value == ""){
      document.getElementById("alertaTelefoneComercial").innerHTML = "Você precisa informar um telefone para contato!";
      theForm.telefone1.focus();
      retorno = 1;
      }	
   }
   
	  
  if (theForm.email.value == ""){
  	  document.getElementById("alertaEmail").innerHTML = "Você precisa informar um E-mail para login!";
      theForm.email.focus();
      retorno = 1;
      }
	  	
  if (theForm.email.value.indexOf('@', 0) == -1 ||
      theForm.email.value.indexOf('.', 0) == -1) {
	  document.getElementById("alertaEmail").innerHTML = "O E-mail informado é inválido. Informe o e-mail correto.";
      theForm.email.focus(); 
      retorno = 1;
      }
	 
  if (theForm.senha.value == ""){
  	  document.getElementById("alertaSenha").innerHTML = "Você precisa informar uma senha!";
      theForm.senha.focus();
      retorno = 1;
      }	
	 
  if (theForm.confirmaSenha.value == ""){
  	  document.getElementById("alertaConfirmaSenha").innerHTML = "Você precisa confirmar a senha!";
      theForm.confirmaSenha.focus();
      retorno = 1;
      }	

  if (theForm.confirmaSenha.value != theForm.senha.value){
  	  document.getElementById("alertaConfirmaSenha").innerHTML = "Confirmação de senha não confere!";
      theForm.confirmaSenha.focus();
      retorno = 1;
      }	

  if (theForm.cep.value == ""){
   	  document.getElementById("alertaCep").innerHTML = "Informe um cep!";
      theForm.cep.focus();
      retorno = 1;
      }	
	  
  if (theForm.endereco.value == ""){
   	  document.getElementById("alertaEndereco").innerHTML = "Você precisa informar o endereço!";
      theForm.endereco.focus();
      retorno = 1;
      }	
	
  if (theForm.numero.value == ""){
   	  document.getElementById("alertaNumero").innerHTML = "Você precisa informar um numero de logradouro!";
      theForm.numero.focus();
      retorno = 1;
      }	
	
  if (theForm.bairro.value == ""){
   	  document.getElementById("alertaBairro").innerHTML = "Você precisa informar um bairro!";
      theForm.bairro.focus();
      retorno = 1;
      }	
	
  if (theForm.estado_id.options[theForm.estado_id.selectedIndex].value == ""){
   	  document.getElementById("alertaEstado").innerHTML = "Você precisa informar um estado!";
      theForm.estado_id.focus();
	  retorno = 1;
	  }
	  
  if (theForm.cidade_id.options[theForm.cidade_id.selectedIndex].value == ""){
   	  document.getElementById("alertaCidade").innerHTML = "Você precisa informar uma cidade!";
      theForm.cidade_id.focus();
      retorno = 1;
      }	
    	   
  if (retorno == 1) {
      return (false);
  } 	 
  else
    return (true);
}

