//Estas funções são de validação de campos 

function valida_campo(tipo, campo){
		switch(tipo)
		{
			case 0: //CAMPO ACEITA ASPAS SIMPLES E DUPLAS - EVENTO: onKeyPress
			{
				if(event.keyCode == 39 || event.keyCode == 34)
					event.returnValue = false;
				break;
			}
			case 1: //CAMPO ACEITA SOMENTE NUMERO - EVENTO: onKeyPress
			{
			
				if(event.keyCode <= 45 || event.keyCode > 57 || event.keyCode == 46 || event.keyCode == 47 || event.keyCode == 42 || event.keyCode == 39 || event.keyCode == 34)
				event.returnValue = false;
				break;
			}
			case 2: //CAMPO ACEITA SOMENTE TEXTO - EVENTO: onKeyPress
			{
				if (event.keyCode > 47 && event.keyCode < 58 || event.keyCode == 39 || event.keyCode == 34)
					event.returnValue = false;
				break;
			}
			case 3: //CAMPO ACEITA SOMENTE EMAIL - EVENTO: onChange
			{

				if (campo.value!=''){
					if(!isEmail(campo.value)){
						alert('Digite o seu E-MAIL corretamente');
						event.returnValue = false;
						campo.select();
					}
				}
				break;		
			}
			case 4: //CAMPO ACEITA SOMENTE DATA - EVENTO: onChange
			{
				if (campo.value!=''){
					if(!validaData(campo.value)){
						event.returnValue = false;
						campo.select();
					}
				}
				break;		
			}
			case 5: //CAMPO ACEITA SOMENTE CPF - EVENTO: onChange
			{
			//alert("oi");
				if (campo.value!=''){
					if(!isCpf(campo.value)){
						alert('Digite o seu CPF corretamente');
						event.returnValue = false;
						campo.select();
					}
				}
				break;
			}
			
		}
	}
	
	
	
function isCpf(valor){
//valor = document.cadastro.cpf.value; 
 //alert(valor.length);
 if (valor.length==11)
  {var aux = 0;
   for (var i=0; i<9; i++)
   {
    aux = aux + parseInt(valor.substr(i,1)) * parseInt(i+1);
   } 

   var resto = aux%11
   if (resto==10) resto=0;
   if(resto==valor.substr(9,1))
   {
     //alert("Ok - Check 01");
     var aux = 0;
     for (var i=1; i<10; i++)
     {
      aux = aux + parseInt(valor.substr(i,1)) * parseInt(i);
     } 

     var resto = aux%11
     if (resto==10) resto=0;
     if(resto==valor.substr(10,1))
     {
      //alert("Ok - Check 02");
      return true;
     }
     else
     {
      return false;
     }

   }
   else
   {
    return false;
   }
  }
}	
	
	

//esta é a função que testa o email
function isEmail(qual)
{
confirma = false;
var reTipo = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
confirma = reTipo.test(qual);
if(confirma != true)
{
return false;
}
else
{
return true;
}
}

 //esta função é a de formatação de cnpj
function isNUMB(c) 
 { 
 if((cx=c.indexOf(","))!=-1) 
  { 
  c = c.substring(0,cx)+"."+c.substring(cx+1); 
  } 
 if((parseFloat(c) / c != 1)) 
  { 
  if(parseFloat(c) * c == 0) 
   { 
   return(1); 
   } 
  else 
   { 
   return(0); 
   } 
  } 
 else 
  { 
  return(1); 
  } 
 } 

 //esta função limpa o valor do CNPJ
function LIMP(c) 
 { 
 while((cx=c.indexOf("-"))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf("/"))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf(","))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf("."))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf("("))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf(")"))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf(" "))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 return(c); 
 } 

function VerifyCNPJ(CNPJ) 
 { 
 CNPJ = LIMP(CNPJ); 
 if(isNUMB(CNPJ) != 1) 
  { 
  return(0); 
  } 
 else 
  { 
  if(CNPJ == 0) 
   { 
   return(0); 
   } 
  else 
   { 
   g=CNPJ.length-2; 
   if(RealTestaCNPJ(CNPJ,g) == 1) 
    { 
    g=CNPJ.length-1; 
    if(RealTestaCNPJ(CNPJ,g) == 1) 
     { 
     return(1); 
     } 
    else 
     { 
     return(0); 
     } 
    } 
   else 
    { 
    return(0); 
    } 
   } 
  } 
 } 
function RealTestaCNPJ(CNPJ,g) 
 { 
 var VerCNPJ=0; 
 var ind=2; 
 var tam; 
 for(f=g;f>0;f--) 
  { 
  VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind; 
  if(ind>8) 
   { 
   ind=2; 
   } 
  else 
   { 
   ind++; 
   } 
  } 
  VerCNPJ%=11; 
  if(VerCNPJ==0 || VerCNPJ==1) 
   { 
   VerCNPJ=0; 
   } 
  else 
   { 
   VerCNPJ=11-VerCNPJ; 
   } 
 if(VerCNPJ!=parseInt(CNPJ.charAt(g))) 
  { 
  return(0); 
  } 
 else 
  { 
  return(1); 
  } 
 } 
  

  function FormataCGC(Formulario, Campo, TeclaPres) 
  { 
    var tecla = TeclaPres.keyCode; 
    var strCampo; 
    var vr; 
    var tam; 
    var TamanhoMaximo = 14; 
  
    eval("strCampo = document." + Formulario + "." + Campo); 
  
    vr = strCampo.value; 
    vr = vr.replace("/", ""); 
    vr = vr.replace("/", ""); 
    vr = vr.replace("/", ""); 
    vr = vr.replace(",", ""); 
    vr = vr.replace(".", ""); 
    vr = vr.replace(".", ""); 
    vr = vr.replace(".", ""); 
    vr = vr.replace(".", ""); 
    vr = vr.replace(".", ""); 
    vr = vr.replace(".", ""); 
    vr = vr.replace(".", ""); 
    vr = vr.replace("-", ""); 
    vr = vr.replace("-", ""); 
    vr = vr.replace("-", ""); 
    vr = vr.replace("-", ""); 
    vr = vr.replace("-", ""); 
    tam = vr.length; 

    if (tam < TamanhoMaximo && 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) 
      { 
        strCampo.value = vr; 
      } 
       if ((tam > 2) && (tam <= 6)) 
       { 
         strCampo.value = vr.substr(0, tam - 2) + '-' + vr.substr(tam - 2, tam); 
       } 
       if ((tam >= 7) && (tam <= 9)) 
       { 
         strCampo.value = vr.substr(0, tam - 6) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam); 
      } 
       if ((tam >= 10) && (tam <= 12)) 
       { 
         strCampo.value = vr.substr(0, tam - 9) + '.' + vr.substr(tam - 9, 3) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam); 
      } 
       if ((tam >= 13) && (tam <= 14)) 
       { 
         strCampo.value = vr.substr(0, tam - 12) + '.' + vr.substr(tam - 12, 3) + '.' + vr.substr(tam - 9, 3) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam); 
      } 
       if ((tam >= 15) && (tam <= 17)) 
       { 
         strCampo.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 TESTA(valor) 
 { 
 if(VerifyCNPJ(valor) == 1) 
  { 
  return true; 
  } 
 else 
  { 
  alert("CNPJ não é válido!");
  return false;
  } 
 } 
 
 // função de formatação de CEP
 
 function FormataCep(Formulario, Campo, TeclaPres) 
  { 
    var tecla = TeclaPres.keyCode; 
    var strCampo; 
    var vr; 
    var tam; 
    var TamanhoMaximo = 8; 
	eval("strCampo = document." + Formulario + "." + Campo); 
	vr = strCampo.value; 
    tam = vr.length;
    eval("strCampo = document." + Formulario + "." + Campo); 
  
    vr = strCampo.value; 
        if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105) 
    { 
      if (tam <= 5) 
      { 
        strCampo.value = vr; 
      } 
       if ((tam > 5) && (tam <= 6)) 
       { 
         strCampo.value = vr.substr(0, tam - 1) + '-' + vr.substr(tam - 1, tam); 
       } 
    } 
  } 

  // FUNCAO DE FORMATACAO DE TELEFONE
  function FormataTel(Formulario, Campo, TeclaPres) 
  { 
    var tecla = TeclaPres.keyCode; 
    var strCampo; 
    var vr; 
    var tam; 
    var TamanhoMaximo = 9; 
	eval("strCampo = document." + Formulario + "." + Campo); 
	vr = strCampo.value; 
    tam = vr.length;
    eval("strCampo = document." + Formulario + "." + Campo); 
  
    vr = strCampo.value; 
        if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105) 
    { 
      if (tam <= 4) 
      { 
        strCampo.value = vr; 
      } 
       if ((tam > 4) && (tam <= 5)) 
       { 
         strCampo.value = vr.substr(0, tam - 1) + ' ' + vr.substr(tam - 1, tam); 
       } 
    } 
  } 