// JavaScript Document

//Validacao de Email
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
<!-- Begin
function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

alert("O campo E-mail foi digitado incorretamente (verifique @ e .)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("O nome do usuário do campo E-mail contém caracteres inválidos.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("O nome do domínio do campo E-mail contém caracteres inválidos.");
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

alert("O nome do usuário do campo E-mail não está correto.");
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("O endereço destino do IP do campo E-mail não está correto!");
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("O nome do domínio do campo E-mail não é válido.");
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("O endereço do e-mail deve terminar com um domínio conhecido ou com duas letras indicando o país.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
alert("Esse endereço de e-mail não contém um hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}
//  End -->
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


//Validacao de Data
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function fValidarData( pDia, pMes, pAno )
{
	var vbBissexto ;
	
		
	// Verifica se os parametros sao do tipo numerico
	if(isNaN( pDia ))
	{
		alert ("O campo Data não esta preenchido de forma correta.");
		return false ;		
	}
	if(isNaN( pMes ))
	{
		alert ("O campoData não esta preenchido de forma correta.");
		return false ;
	}
	if(isNaN( pAno ))
	{
		alert ("O campo Data não esta preenchido de forma correta.");
		return false ;
	} 
	
	// Verifica se foi informado dia 31 para um mes que nao tem 31
	if((pMes==4 || pMes==6 || pMes==9 || pMes==11) && pDia==31)
	{
		alert ("O campo Data não esta preenchido de forma correta.");
		return false ;
	}	
	
	// Verifica se o ano e Bissexto
	vbBissexto = (pAno % 4 == 0 && (pAno % 100 != 0 || pAno % 400 == 0));	
	if (pMes == 2)
	{		
		if (pDia > 29 || (pDia == 29 && !vbBissexto))
		{
			alert ("O campo Data não esta preenchido de forma correta.");
			return false ;
		}	
	}		
	
	// Valida a quantidade MAXIMA de DIA e MES
	if( ( pMes > 12 ) || ( pDia > 31 ) )
	{
		alert ("O campo Data não esta preenchido de forma correta.");
		return false ;
	}		
	return true ;
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

//Validacao de CEP
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function fValidarCEP(num) 
{
	var rg_ver = num ;
	var valid = "0123456789"
	var ok = "yes";
	var temp;

	for (var i=0; i<rg_ver.length; i++) 
	{
		temp = "" + rg_ver.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		alert("O campo CEP não foi preenchido corretamente.");
		return false
	}
	else
	{
		return true
	}
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

//Confirma Exclusão
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function fValidarExcluir()
{
	var vsRetorno;
	while( true )
	{
		vsRetorno = prompt("Para excluir o cliente digite EXCLUIR", "NÃO EXCLUIR" );
	
		if( vsRetorno == null )
		{
			return false;
		}			
		else if( (vsRetorno != "NÃO EXCLUIR" ) && ( vsRetorno != "EXCLUIR" ) )
		{
			alert("Digite EXCLUIR corretamente e tente de novo.") ;
		}
		else if(vsRetorno == "NÃO EXCLUIR")
		{
			return false ; 
		}
		else
		{
			return true ;
		}
	}
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

//Somente Números
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function fSomenteNumeros(num) 
{
	var rg_ver = num ;
	var valid = "0123456789"
	var ok = "yes";
	var temp;

	for (var i=0; i<rg_ver.length; i++) 
	{
		temp = "" + rg_ver.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		return false
	}
	else
	{
		return true
	}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//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); 
  } 
 } 

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); 
      } 
    } 
  }
  
//Formata REAIS
<!-- Início da Função FormataReais --> 
function FormataReais(fld, milSep, decSep, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13) return true;
key = String.fromCharCode(whichCode);  // Valor para o código da Chave
if (strCheck.indexOf(key) == -1) return false;  // Chave inválida
len = fld.value.length;
for(i = 0; i < len; i++)
if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
aux = '';
for(; i < len; i++)
if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
aux += key;
len = aux.length;
if (len == 0) fld.value = '';
if (len == 1) fld.value = '0'+ decSep + '0' + aux;
if (len == 2) fld.value = '0'+ decSep + aux;
if (len > 2) {
aux2 = '';
for (j = 0, i = len - 3; i >= 0; i--) {
if (j == 3) {
aux2 += milSep;
j = 0;
}
aux2 += aux.charAt(i);
j++;
}
fld.value = '';
len2 = aux2.length;
for (i = len2 - 1; i >= 0; i--)
fld.value += aux2.charAt(i);
fld.value += decSep + aux.substr(len - 2, len);
}
return false;
}
//Fim da Função FormataReais -->
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//Contar Caracter

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function fContarCaracter(texto,contator,qtdemax)
{
	
	if (texto.value.length > qtdemax)
	{
		texto.value = texto.value.substring(0, qtdemax);
	}
	else 
	{
		contator.value = qtdemax - texto.value.length;
	}
	if (contator.value <= 0)
	{
		alert("Limite máximo de caracteres.");
		texto.length = texto.length - 1;
	}
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

function fPopUp(jvsURL,jvsNome,jvsWidth,jvsHeight)
{
window.open(jvsURL,jvsNome,'toolbar=no,width=' + jvsWidth + ',height=' + jvsHeight + ',scrollbars=no,resizable=no');
}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
<!-- // para browsers sem suporte a java script

function check_cpf(StrCPF)
{
	retorno = true;
	x = 0;
	soma = 0;
	dig1 = 0;
	dig2 = 0;
	texto = "";
	StrCPF1="";
	len = StrCPF.length;
	x = len -1;
	for (var i=0; i <= len - 3; i++)
	{
		y = StrCPF.substring(i,i+1);
		soma = soma + ( y * x);
		x = x - 1;
		texto = texto + y;
	}
	dig1 = 11 - (soma % 11);
	if (dig1 == 10) dig1=0 ;
	if (dig1 == 11) dig1=0 ;
	StrCPF1 = StrCPF.substring(0,len - 2) + dig1 ;
	x = 11; soma=0;
	for (var i=0; i <= len - 2; i++)
	{
		soma = soma + (StrCPF1.substring(i,i+1) * x);
		x = x - 1;
	}
	dig2= 11 - (soma % 11);
	if (dig2 == 10) dig2=0;
	if (dig2 == 11) dig2=0;
	if ((dig1 + "" + dig2) == StrCPF.substring(len,len-2))
	{
		// alert ("Número do CPF Válido !");
		return true;
	}
	alert ("Número do CPF Inválido !");
	return false;
}


function check_cgc (StrCGC)
{
	var varFirstChr = StrCGC.charAt(0);
	var vlMult,vlControle,s1, s2 = "";
	var i,j,vlDgito,vlSoma = 0;
	for ( var i=0; i<=13; i++ )
	{
	
		var c = StrCGC.charAt(i);
		if( ! (c>="0")&&(c<="9") )
		{
			alert("Número do CGC Inválido !");
			return false;
		}
		if( c!=varFirstChr ) 
		{ 
			vaCharCGC = true; 
		}
	}
	if( ! vaCharCGC )
	{
	alert("Número do CGC Inválido !");
	return false ;
	}


	s1 = StrCGC.substring(0,12);
	s2 = StrCGC.substring(12,15);
	vlMult = "543298765432";
	vlControle = "";
	for ( j=1; j<3; j++ )
	{
	
		vlSoma = 0;
		for ( i=0; i<12; i++ )
		{
			vlSoma += eval( s1.charAt(i) )* eval( vlMult.charAt(i) );
		}
		if( j == 2 )
		{
			vlSoma += (2 * vlDgito); 
		}
		vlDgito = ((vlSoma*10) % 11);
		if( vlDgito == 10 )
		{
			vlDgito = 0; 
		}
		vlControle = vlControle + vlDgito;
		vlMult = "654329876543";
	}
	if( vlControle != s2 )
	{
		alert("Número do CGC Inválido !");
		return false;
		return false;
	}
	else
	{
	return true;
	}

}


function validaCGC_CPF(cpf_cgc)
{

	var StrData = cpf_cgc;
	var retorno;
	retorno = true;
	
	var CGCPat = /^(\d{2}).(\d{3}).(\d{3})\/(\d{4})-(\d{2})/;
	var CGCPat2 = /^(\d{14})/;
	var CPFPat = /^(\d{3}).(\d{3}).(\d{3})-(\d{2})/;
	var CPFPat2 = /^(\d{11})/;
	
	var matchCGCArray = StrData.match(CGCPat);
	var matchCGCArray2 = StrData.match(CGCPat2);
	var matchCPFArray = StrData.match(CPFPat);
	var matchCPFArray2 = StrData.match(CPFPat2);
	
	if (matchCGCArray == null && matchCGCArray2 == null && matchCPFArray == null && matchCPFArray2 == null)
	{
		cpfalert = 'O número do CPF deve ser informado incluindo-se os dois dígitos verificadores. Não são necessários zeros à esquerda.\nExemplo: 000.000.000-00 ou 00000000000\n\n';
		cgcalert = 'O número do CGC deve ser informado incluindo-se os 14 dígitos. Não são necessários zeros à esquerda.\nExemplo: 00.000.000\/0000-00 ou 00000000000000';
		alert('Vc deve fornecer um CGC ou um CPF valido\n\n' + cpfalert + cgcalert);
		retorno = false;
	}
	else if(matchCGCArray != null)
	{
		StrData = matchCGCArray[1] + matchCGCArray[2] + matchCGCArray[3] +
		matchCGCArray[4] + matchCGCArray[5] ;
		retorno = check_cgc(StrData);
	}
	else if(matchCGCArray2 != null)
	{
		StrData = matchCGCArray2[1];
		retorno = check_cgc(StrData);
	}
	else if(matchCPFArray != null)
	{
		StrData = matchCPFArray[1] + matchCPFArray[2] + matchCPFArray[3] +
		matchCPFArray[4];
		retorno = check_cpf(StrData);
	}
	else if(matchCPFArray2 != null)
	{
		StrData = matchCPFArray2[1];
		retorno = check_cpf(StrData);
	}
	return retorno;
	
}

//-->
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
