//*****************************************************************************
//Objetivo:					Encapsular funcionalidades do Site
//Autor:					- Marcelo Pacheco (7COMm)
//Data:						- 19/04/2006
//Autor:					
//Data Ultima alteração:	
//Motivo:					
//*****************************************************************************
//*****************************************************************************
// DECLARAÇÃO DAS VARIÁVEIS
//*****************************************************************************
//Variáveis Globais
// ----------------------------------------------------------------------------
// Objetivo  : Validar Email
// Premissas : Nenhuma
// Entradas  : strEmail - Email recebido para validação
// Retorno   : true ou false
// ----------------------------------------------------------------------------
function ValidarEmail(strEmail)
{
	try
    {
		//var strPadrao = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i; // Regra da expressão regular
		var strPadrao = /^([\&\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/i;
		var objReg = new RegExp(strPadrao); // Objeto Expressão Regular
		var blnCondicao // Variavel auxiliar true or false
	    
		objReg.exec(strPadrao)
		objReg.global == true;
		if (!(objReg.test(strEmail)))
		{   
			throw "Email Inválido";
		}
		blnCondicao = true;
	}
	catch(objEx)
	{
		blnCondicao = false;
	}
	finally
	{
		return blnCondicao
	}
}
// ----------------------------------------------------------------------------
// Objetivo  : Validar Número
// Premissas : Nenhuma
// Entradas  : strNumero - valor recebido para validação
// Retorno   : true ou false
// ----------------------------------------------------------------------------
function ValidarNumero(strNumero)
{
	try
    {
		var strPadrao = /[\D]/; // Regra da expressão regular
		var objReg = new RegExp(strPadrao); // Objeto Expressão Regular
		var blnCondicao // Variavel auxiliar true or false
	    
		objReg.exec(strPadrao)
		objReg.global == true;
		if ((objReg.test(strNumero)))
		{   
			throw "Valor Inválido";
		}
		blnCondicao = true;
	}
	catch(objEx)
	{
		blnCondicao = false;
	}
	finally
	{
		return blnCondicao
	}
}
//============================================================================================
// objetivo........: Verifica se numero do CNPJ é valido
// Premissas.......:
// Entradas........: nenhuma
// Retorno.........: true - numero valido / false - numero invalido
//============================================================================================
function ValidaCNPJ(intCNPJ)
{
   var intCNPJLocal ;		//Numero do CNPJ
   var intRespostaLocal ;	//Resposta Local
   var intDigito1 ;			//Digito Verificador 1
   var intDigito2 ;			//Digito Verificador 2
   var intCont ;			//Contador
   var intSoma ;			//Soma
 
   intCNPJLocal = intCNPJ ;
 
  if ((intCNPJLocal.length < 14) || (intCNPJLocal == "00000000000000"))
  {
    intRespostaLocal = false ;
  }
  else
  {
	intRespostaLocal = true ;
  }
 
  if (intRespostaLocal == true)
  {
		intSoma = 0 ;
 
		for (intCont = 1; intCont <= 12; intCont++) 
		{
			if (intCont < 5)
			{
				intSoma = intSoma + (intCNPJLocal.substring(intCont-1,intCont) * (6-intCont))
			}
			else
			{
				intSoma = intSoma + (intCNPJLocal.substring(intCont-1,intCont) * (14-intCont))
			}
		}
 
		intDigito1 = 11 - (intSoma % 11) ;
		if (intDigito1 > 9)
		{
			intDigito1 = 0 ;
		}
 
		intSoma = 0 ;
   
		for (intCont = 1; intCont <= 13; intCont++)
		{
			if (intCont < 6)
		    {
				intSoma = intSoma + (intCNPJLocal.substring(intCont-1,intCont) * (7-intCont)) ;
		    }
		    else
		    {
				intSoma = intSoma + (intCNPJLocal.substring(intCont-1,intCont) * (15-intCont)) ;
		    }
		}
 
		intDigito2 = 11 - (intSoma % 11) ;
		if (intDigito2 > 9)
		{
			intDigito2 = 0 ;
		}
 
		if ((intDigito1 == intCNPJLocal.substr(12,1)) && (intDigito2 == intCNPJLocal.substr(13,1)))
		{
			intRespostaLocal = true
		}
		else 
		{
		    intRespostaLocal = false
		}
 
  }
    return intRespostaLocal
}

/******************************************************************************
/ Objetivo  : Validar se o CPF é válido
/ Premissas : Nenhuma
/ Entradas  : strCPF CPF do usuário
/ Retorno   : true or false
/******************************************************************************/
function ValidaCPF(strCPF)
{
	var strCharCPF = false;				//Caracter do CPF
	var strFirstChr = strCPF.charAt(0);	//Primeiro caracter
	var blnCondicao = true				// variavel boleana de controle
	var intCont							// Variavel Auxiliar utilizada no laço for
	var strDigitoVerificador			// Digito Verificador
	var	intSoma							// Recebe soma do modulo 11
	var strCaracter
	try
	{
		for ( var intCont=0; intCont<=10; intCont++ )
		{
			strCaracter = strCPF.charAt(intCont);
			if( ! ((strCaracter>="0")&&(strCaracter<="9")) ) 
			{	
				throw "false";
			}
			if( strCaracter!=strFirstChr ) 
				strCharCPF = true;
		}
		if( ! strCharCPF ) {
			throw "false";
		}
		intSoma=0;
		for ( intCont=0; intCont<9; intCont++ ) { 
			intSoma += (10-intCont) * ( eval(strCPF.charAt(intCont)) ); 
		}
		strDigitoVerificador = 11-(intSoma % 11);
		if ( (intSoma % 11) < 2 ) 
			strDigitoVerificador = 0;
		if ( eval(strCPF.charAt(9)) != strDigitoVerificador ) { 
			throw "false";
		}
		intSoma=0; 
		for ( intCont=0; intCont<9; intCont++ ) { 
			intSoma += (11-intCont) * ( eval(strCPF.charAt(intCont)) ); 
		} 
		intSoma += 2 * ( eval(strCPF.charAt(9)) ); 
		strDigitoVerificador = 11-(intSoma % 11); 
		if ( (intSoma % 11) < 2 ) 
			strDigitoVerificador = 0; 
		if ( eval(strCPF.charAt(10)) != strDigitoVerificador ) { 
			throw "false"; 
		}
	}
	catch(objEx)
	{
		 blnCondicao = false
	}
	finally
	{
		return blnCondicao 
	}
}	
