
/* VALIDA Dados */  
// ******************* inicio  *********************
		

function Validate(form) 

	{

	/* valida CREA*/
		if (document.FormExclVincIdent.CREA.value.length != 12)
		  {
		    alert("ATENÇÃO!\n\nFavor, informar CREA com 12 posições.");
		    document.FormExclVincIdent.CREA.focus();
		    return (false);
		  }
	
		if  (document.FormExclVincIdent.CREA.value.substring(0,1) == 6  ||  document.FormExclVincIdent.CREA.value.substring(0,1) == 9)
		   {
		   alert("ATENÇÃO!\n\nFunção não disponível para Crea Negativo ou Genérico.");
		   document.FormExclVincIdent.CREA.focus();
		   return (false);
           }
		
		
		if  (document.FormExclVincIdent.CREA.value.substring(0,1) != 3  && document.FormExclVincIdent.CREA.value.substring(0,1) != 5 && document.FormExclVincIdent.CREA.value.substring(0,1) != 6 && document.FormExclVincIdent.CREA.value.substring(0,1) != 9)
		   {
		   alert("ATENÇÃO!\n\nCREA inválido.");
		   document.FormExclVincIdent.CREA.focus();
		   return (false);
           }


	/* valida CPF*/
      if ( !VerifyCPF( document.FormExclVincIdent.cpf_Responsavel, "CPF do Responsável", true, 11, true ) )
         {
           document.FormExclVincIdent.cpf_Responsavel.focus();
		   return (false);	
		 }

      
	/* valida NOME */     
       if ( !VerifyAlfa( document.FormExclVincIdent.nome_Responsavel, "Nome do Responsavel", true, 4 ) )
          {
          document.FormExclVincIdent.nome_Responsavel.focus();
          return (false);
          }


	/* valida cnpj inscrição */
	if (document.FormExclVincIdent.cnpj.value == "")
		{		
		alert("ATENÇÃO!\n\nFavor, informar o CNPJ/CEI.");
		document.FormExclVincIdent.cnpj.focus();
		return false;
		}
	else
		{
		tamanho = (document.FormExclVincIdent.cnpj.value.length);
		if (tamanho == 14)							
			{   	
			if (!IsCgcCei(document.FormExclVincIdent.cnpj.value))
				{
				alert("ATENÇÃO!\n\nInscrição CNPJ inválido.");
				document.FormExclVincIdent.cnpj.focus();
				return false;
				}
			}
		else
		    {	
			if (tamanho == 12)
				{
				if (!Valida_CEI(document.FormExclVincIdent.cnpj.value))
					{
					alert("ATENÇÃO!\n\nInscrição CEI inválido.");
					document.FormExclVincIdent.cnpj.focus();
					return false;
					}
				}
			else
				{
				alert("ATENÇÃO!\n\nInscrição CNPJ/CEI inválido.");
				document.FormExclVincIdent.cnpj.focus();
				return false;
				}
			}  
		}	
        

		/* valida CEI Vinculado */
		if (document.FormExclVincIdent.ceivinc.value != "")
		   {
		   if (!Valida_CEI(document.FormExclVincIdent.ceivinc.value))
		      {
			  alert("ATENÇÃO!\n\nInscrição CEI Vinculado inválido.");
			  document.FormExclVincIdent.ceivinc();
			  return false;
		      }
		   }


		/* valida PIS */
		pis=document.FormExclVincIdent.pis.value;
	
		if (pis=="" || pis==null)
		{
			alert("ATENÇÃO!\n\nFavor, informar pis com 11 posições.");
			document.FormExclVincIdent.pis.focus();
			return false;
		}
		else
		{
			if (!ChecaPIS(pis))
				{
				alert("ATENÇÃO!\n\n PIS inválido.")
				document.FormExclVincIdent.pis.focus()
				return false;
				}
		}


   return true;
	
	}		

// ******************* fim  *********************




/* funcoes chamada pela funcao Validade*/



 function VerifyCPF( field, fieldName, obrig, minLen, acSpace )
{
  
  
  if ( field.value == "" )
     if ( obrig == true ) 
        {
        alert("ATENÇÃO!\n\nFavor, informar o " + fieldName + "." );
        document.FormExclVincIdent.cpf_Responsavel.focus();
        return false;
        }
     else {
        return true;
		  }	
  if ( field.value.length < minLen )
     {
     alert("ATENÇÃO!\n\nPor favor, informar o campo " + fieldName + " com no mínimo " + minLen + " posições." );
     document.FormExclVincIdent.cpf_Responsavel.focus();
     return false;
     } 
     
     
  
  var checkOK = "0123456789";
  if ( acSpace )
     checkOK = checkOK + " ";
  var checkStr = field.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
    {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
      {
      allValid = false;
      break;
      }
    }

  if (!allValid)
  {
     alert("ATENÇÃO!\n\nCPF do responsável inválido." );
     document.FormExclVincIdent.cpf_Responsavel.focus();
     return false;
  }   
     
     
  /*
/* Checa DV do CPF do Responsável      
/**/
      var cpf = new Array();
      var d1,d2, algarismo, soma, res;
      
      for(var i=0;i<11;i++){
		if(i==10){
			cpf[i] = document.FormExclVincIdent.cpf_Responsavel.value.substring(i);
		}
		else
		{
			cpf[i] = document.FormExclVincIdent.cpf_Responsavel.value.substring(i,i+1);
		}
      }
      
      algarismo = 10;
      soma = 0;
      
      for(var i=0;i<=8;i++){
		soma = soma + (cpf[i] * algarismo--);
      }
      
      res = soma % 11;
      
      if(res<2){
		d1 = 0;
      }
      else{
		d1 = 11 - res;
      }
      
      algarismo = 11;
      soma = 0;
      
      for(var i=0;i<=9;i++){
		soma = soma + (cpf[i] * algarismo--);
      }
      
      res = soma % 11;
      
      if(res<2){
		d2 = 0;
      }
      else{
		d2 = 11 - res;
      }

      if(d1!=cpf[9] || d2!=cpf[10]){

        alert("ATENÇÃO!\n\nCPF do responsável inválido." );
		document.FormExclVincIdent.cpf_Responsavel.focus();
		return false;
      }
   
    return true; 
    
}	     




		
		// field -> campo
	   	// fieldName -> nome
	   	// obrig -> campo obrigatorio
	   	// minLen -> tamanho minimo
function VerifyAlfa( field, fieldName, obrig, minLen )
	   	{
	   	 
	   	  if ( field.value == "" )
				    
	       {
	   	     alert("ATENÇÃO!\n\nFavor, informar o Nome do Responsável.");
	   	     return false;
	   	   }
	      if ( field.value.length < minLen )
	   		   {
	   		   alert("ATENÇÃO!\n\nPor favor, informar o campo " + fieldName + " com no mínimo " + minLen + " posições." );
	   		   return false;
	   		   } 
	   	  		 
	   	//teste ***  var checkOK = " !" + '"' + "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
//	   	  var checkOK = " \0123456789ABCDE'FGHIJLMNO\PQRSTUVXWYZabcdefghijlmnopqrstuvwyz:;<=>?!@#$%*[^()_-+=}]{\\\\|/";
//	   	  var checkStr = field.value;
//	   	  var allValid = true;
//	   	  for (i = 0;  i < checkStr.length;  i++)
//	   	    {
//	   	    ch = checkStr.charAt(i);
//	   	    for (j = 0;  j < checkOK.length;  j++)
//	   	      if (ch == checkOK.charAt(j))
//	   	        break;
//	   	    if (j == checkOK.length)
//	   	      {
//	   	      allValid = false;
//	   	      break;
//	   	      }
//	   	    }
//
//	   	  if (!allValid)
//	   	  {
//	   	     alert("ATENÇÃO!\n\nEste campo não aceita caracteres acentuados, E comercial e cê-cedilha." );
//	   	     return false;
//	   	  }
//
    return true;
  
  }
	
// funcoes chamadas para validaçoes cnpj e cei vinculado
	function IsCgcCei( CgcCei ) 
{
        strTxtCgc = CgcCei;
        iLenTxtCgc =  strTxtCgc.length;
        
        val_cgc = 0;
           
        if(iLenTxtCgc == 14)
        {
          val_cgc = valida_CGC(strTxtCgc);
        }
		else
		{
		  if(iLenTxtCgc == 12)
		  {
		    if(isNaN(CgcCei))
		    {
		      return false;
		    }
		    else
		    {
		      val_cgc = 1;
		    }
		  }
		  else
		  {
		    return false;
		  }
		}

        valCgcZero = parseFloat( strTxtCgc );
        if ( valCgcZero == 0 )
           return false;
                
        if(val_cgc == 1){
          return true;
          } 
        else 
          {
          return false;
          }

}

function valida_CGC(strNum)
{
        strMul = "6543298765432"
        iValido = 1
        if(strNum.length != 14)
          {
          return(0)
          }
        iSoma = 0
        strNum_base = strNum.substring(0,12) 
        iLenNum_base = strNum_base.length - 1
        iLenMul = strMul.length - 1
        for(i=0;i<12;i++)
           {
           iSoma = iSoma + parseInt(strNum_base.substring((iLenNum_base-i),(iLenNum_base-i)+1),10) * parseInt(strMul.substring((iLenMul-i),(iLenMul-i)+1),10)
           }
        
        iSoma = 11 - (iSoma - Math.floor(iSoma/11) * 11)
        if(iSoma == 11 || iSoma == 10)
          {
          iSoma = 0
          }
        
        strNum_base = strNum_base + iSoma 
        iSoma = 0
        iLenNum_base = strNum_base.length - 1
        for(i=0;i<13;i++)
           {
           iSoma = iSoma + parseInt(strNum_base.substring((iLenNum_base-i),(iLenNum_base-i)+1),10) * parseInt(strMul.substring((iLenMul-i),(iLenMul-i)+1),10)
           }
        
        iSoma = 11 - (iSoma - Math.floor(iSoma/11) * 11)
        if(iSoma == 11 || iSoma == 10)
          {
          iSoma = 0
          }
        
        strNum_base = strNum_base + iSoma 
        if(strNum != strNum_base)
          {
          return(0)
          }
        
        return(1)
}
		







function Valida_CEI(cei)
{
cei_tmp= "";
for (i = 0; (i <= cei.length ); i++) 
{ 
p = cei.charAt(i,1) ;
if (p != " ") 
{ 
cei_tmp+=p;
}
}
cei_tmp = "000000000000" + cei_tmp;
nPosIni = cei_tmp.length - 12;
cei_tmp = cei_tmp.substring(nPosIni,cei_tmp.length);
var cDigito = ""; 
var ceidig1 = cei_tmp.substring(0,1); 
var ceidig2 = cei_tmp.substring(1,2);
var ceidig3 = cei_tmp.substring(2,3);
var ceidig4 = cei_tmp.substring(3,4);
var ceidig5 = cei_tmp.substring(4,5);
var ceidig6 = cei_tmp.substring(5,6);
var ceidig7 = cei_tmp.substring(6,7);
var ceidig8 = cei_tmp.substring(7,8);
var ceidig9 = cei_tmp.substring(8,9);
var ceidig10 = cei_tmp.substring(9,10);
var ceidig11 = cei_tmp.substring(10,11); 
var ceidig12 = cei_tmp.substring(11,12); 
var soma = 0; 

soma = ceidig1 + ceidig2; 
if ((soma ==0 ))

return false;

if ((ceidig11 == 1) || (ceidig11 == 2) || (ceidig11 == 3) || (ceidig11 == 4) || (ceidig11 == 5))
return false;


soma =0;
soma += ceidig1 * 7;
soma += ceidig2 * 4;
soma += ceidig3 * 1;
soma += ceidig4 * 8;
soma += ceidig5 * 5;
soma += ceidig6 * 2;
soma += ceidig7 * 1;
soma += ceidig8 * 6;
soma += ceidig9 * 3;
soma += ceidig10 * 7;
soma += ceidig11 * 4;
var soma2 = "" + soma;

var somauni = soma2.substring((soma2.length - 1),soma2.length);
var somadez = soma2.substring((soma2.length - 2),(soma2.length - 1));

var somauni1 = parseInt(somauni);
var somadez1= parseInt(somadez);

soma = somauni1 + somadez1 ;
soma2 = "" + soma;
somauni = soma2.substring((soma2.length - 1),soma2.length);
if ( somauni == 0)
soma=0
else
soma = 10 - parseInt(somauni);



if (soma != ceidig12)
	{
	return false;
	}


return true;

}



		
		
		
function ValidaNumero() {
  var key = event.keyCode;
  if (key == 9)
    return
  var numKeys = ((key >= 48 && key <= 57) || (key >= 96 && key <= 105)) && !event.shiftKey && !event.altKey ;
  var sysKeys = (key == 8 || key == 13 || key == 9 || (key >= 35 && key <= 46));
  if (!numKeys && !sysKeys)
    event.returnValue = false;
}
		

function LimpaCampos()
{
  var form = document.FormExclVincIdent
  form.CREA.value = ""
  form.cpf_Responsavel.value = ""
  form.nome_Responsavel.value = ""
  form.cnpj.value = ""
  form.ceivinc.value = ""
  form.pis.value = ""
}












function ChecaPIS(pis)
{

	
	
	
	var ftap="3298765432";
	var total=0;
	var i;
	var resto=0;
	var numPIS=0;
	var strResto="";
		
		
	total=0;
	resto=0;
	numPIS=0;
	strResto="";

	numPIS=pis;
		
	for(i=0;i<=9;i++)
	{
		resultado = (numPIS.slice(i,i+1))*(ftap.slice(i,i+1));
		total=total+resultado;
	}
	

	resto = (total % 11)
	
	if (resto != 0)
	{
		resto=11-resto;
	}
	
	if (resto==10 || resto==11)
	{
		strResto=resto+"";
		resto = strResto.slice(1,2);
	}
	

		
	
	if (resto!=(numPIS.slice(10,11)))
	{
		return false;
	}
	
	return true;
}












