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); 
  } 
} 
 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 checa_cnpj(CNPJ)
{  
  if(VerifyCNPJ(CNPJ) == 1) 
    return(true); 
  else 
    return(false);
}

function checar_email()
{
  if (/^\w+([\-]?\w+)*@\w+([\-]?\w+)*(\.\w{2,3})+$/.test(document.form1.email.value))
  {
    return (true)
  }
  
  alert("Esse e-mail não é válido.")
  document.form1.email.focus();
  
  return (false)
}

function valida_cep()
{
  var valid = "0123456789";
  var hyphencount = 0;

 field = document.form1.cep1.value+document.form1.cep2.value
 
  for (var i=0; i < field.length; i++)
  {
    temp = "" + field.substring(i, i+1);
    if (valid.indexOf(temp) == "-1")
    {
      alert("CEP Inválido");
      document.form1.cep1.focus();
      return (false);
    }
  }
  return (true);
}
  
/**********************************************************************
CADASTRO AREA RESTRITA
**********************************************************************/

function checa_formulario()
{
  if (document.form1.empresa.value == "")
  {
    alert("Preencha o campo \"Empresa\".");
    document.form1.empresa.focus();
    return (false);
  }
  if (document.form1.cnpj.value == "")
  {
    alert("Preencha o campo \"CNPJ\".");
    document.form1.cnpj.focus();
    return (false);
  }
  if (document.form1.ie.value == "")
  {
    alert("Preencha o campo \"I.E.\".");
    document.form1.ie.focus();
    return (false);
  }
  if (document.form1.endereco.value == "")
  {
    alert("Preencha o campo \"Endereço\".");
    document.form1.endereco.focus();
    return (false);
  }
  if (document.form1.numero.value == "")
  {
    alert("Preencha o campo \"Número\".");
    document.form1.numero.focus();
    return (false);
  }
  if (document.form1.bairro.value == "")
  {
    alert("Preencha o campo \"Bairro\".");
    document.form1.bairro.focus();
    return (false);
  }
  if (document.form1.cep1.value == "")
  {
    alert("Preencha o campo \"CEP\".");
    document.form1.cep1.focus();
    return (false);
  }
  if (document.form1.cep2.value == "")
  {
    alert("Preencha o campo \"CEP\".");
    document.form1.cep2.focus();
    return (false);
  }
  if (document.form1.cidade.value == "")
  {
    alert("Preencha o campo \"Cidade\".");
    document.form1.cidade.focus();
    return (false);
  }
  if (document.form1.estado.value == "")
  {
   alert("Preencha o campo \"Estado\".");
   document.form1.estado.focus();
    return (false);
  }
  if (document.form1.ddd1.value == "") {
    alert("Preencha o DDD que pertence ao campo \"Fone\".");
    document.form1.ddd1.focus();
    return (false);
  }
  if (document.form1.telefone1.value == "")
  {
    alert("Preencha o campo \"Telefone\".");
    document.form1.telefone1.focus();
    return (false);
  }
  if ((document.form1.telefone2.value == "") && (document.form1.ddd2.value != ""))
  {
    alert("Preencha o campo \"Telefone\".");
    document.form1.telefone2.focus();
    return (false);
  }
  if ((document.form1.telefone2.value != "") && (document.form1.ddd2.value == ""))
  {
    alert("Preencha o DDD que pertence ao campo \"Fone\".");
    document.form1.ddd2.focus();
    return (false);
  }
  if (document.form1.email.value == "")
  {
    alert("Preencha o campo \"E-Mail\".");
    document.form1.email.focus();
    return (false);
  }
  if (document.form1.contato.value == "")
  {
    alert("Preencha o campo \"Contato\".");
    document.form1.contato.focus();
    return (false);
  }
  if (document.form1.usuario.value == "")
  {
    alert("Preencha o campo \"Usuário\".");
    document.form1.usuario.focus();
    return (false);
  }
  if (document.form1.senha.value == "")
  {
    alert("Preencha o campo \"Senha\".");
    document.form1.senha.focus();
    return (false);
  }
  if (document.form1.conf_senha.value == "")
  {
    alert("Preencha o campo \"Confima a senha\".");
    document.form1.conf_senha.focus();
    return (false);
  }  
  return (true);
}
	
function checar_senha()
{
  if(document.form1.senha.value != document.form1.conf_senha.value)
	{
	  alert("O campo \"Senha\" não confere com o campo de \"Confirmação!\".");
	  document.form1.senha.focus()
	  return (false)
  }
  return (true)
}
	
function todas_verificacoes()
{
	var retorno1, retorno2, retorno3, retorno4, retorno5
  	
	retorno1 = checa_formulario()
	if(retorno1 == true)
	{
	  retorno2 = checa_cnpj(document.form1.cnpj.value)	  
	  if(retorno2 == true)
	  {
	    retorno3 = valida_cep() 
	    if(retorno3 == true)
	    {
	      retorno4 = checar_email() 
	      if(retorno4 == true)
	      {
	        retorno5 = checar_senha() 
	        if(retorno5 == true)
	        {
	          return (true) 
	        }
	        else
	        {
	        	return (false)
	        }
	      }
	      else
	      {
	      	return (false)
	      }
	    }
	    else
	    {
	    	return (false)
	    }
	  }
	  else
	  {
	  	alert("CNPJ Inválido.");
	  	document.form1.cnpj.focus();
	  	return (false)
	  }
	}
	else
	{
		return (false)
  }
}

/**********************************************************************
ESQUECEU A SENHA
**********************************************************************/

function todas_verificacoes_esqueceu_senha()
{
	var retorno1, retorno2
	  	
	retorno1 = checa_cnpj(document.form1.cnpj.value)
	if(retorno1 == true)
	{
	  retorno2 = checar_email()
	  if(retorno2 == true)
	  {
	    return (true)
	  }
	  else
	  {
	    return (false)
	  }
	}
	else
	{
	  alert("CNPJ não é válido!"); 
    document.form1.cnpj.focus(); 
    return(false);
	}
}


/**********************************************************************
GERADOR DE SENHA
**********************************************************************/

/*-----------------------------------------------------------------------*/

function validacao()
{ 
	  var Modelo  = document.form1.Modelo.value;
	  var NSerial = document.form1.NSerial.value;
	  var CNPJ    = document.form1.CNPJ.value;
	  var IE      = document.form1.IE.value;
	  
	  if (Modelo == "")
	  {
	    alert("Selecione o modelo da IF.");
	    document.form1.Modelo.focus();
	    return (false);
	  }
	  else if (NSerial == "")
	  {
	    alert("Digite o Número Serial da IF.");
	    document.form1.NSerial.focus();
	    return (false);
	  }
	  else if (NSerial.length < 20)
	  {
	    alert("O número de série deve possuir 20 caracteres.");
	    document.form1.NSerial.focus();
	    return (false);
	  }
	  else if (!checa_cnpj(CNPJ))
	 	{
	    alert("CNPJ não é válido!"); 
      document.form1.CNPJ.focus(); 
      return(false);
	  }
	  else if (IE == "")
	  {
	    alert("Digite a Incrição Estadual.");
	    document.form1.IE.focus();
	    return (false);
	  }
	  else
	    return true;
}
