/*** INICIO DO CODIGO DAS VALIDAÇÕES **

     Verificação de CPF / CNPJ
     Devem ser passados como parametros:
     campo = valor a ser pesquisado
     empresa = se true, verifica CNPJ, senao CPF
*/
function verificarCpfCnpj(vlr, empresa)
{
	var intNumero, intMais, i, intResto, strCampo, strCaracter, strConf;
	var intDig1, intDig2, dblDivisao, rt, intSoma, intSoma1, intSoma2, intInteiro;
	var lngSoma, lngInteiro, strCGC; // somente para o caso de CGC
	
	vlr = SoNumero( vlr );

	if ( empresa ) // Pessoa jurídica
	{
		vlr = TransCGC( vlr );
		intSoma = 0;
		intSoma1 = 0;
		intSoma2 = 0;
		intNumero = 0;
		intMais = 0;
		strCGC = vlr.substr( vlr.length-7, 4 );
		strCampo = vlr.substr( 4, 4 );
		strCampo += strCGC;

		for ( i=1; i<9; i++ )
		{
			strCaracter = strCampo.substr( strCampo.length - i );
			intNumero = strCaracter.substr( 0, 1 );
			intMais = intNumero * ( i+1 );
			intSoma1 += intMais;
		}
		strCampo = vlr.substr( 0, 4 );
		
		for ( i=1; i<5; i++ )
		{
			strCaracter = strCampo.substr( strCampo.length - i );
			intNumero = strCaracter.substr( 0, 1 );
			intMais = intNumero * ( i+1 );
			intSoma2 += intMais;
		}
		
		intSoma = intSoma1 + intSoma2;
		dblDivisao = intSoma / 11;
		intInteiro = parseInt( dblDivisao ) * 11;
		intResto = intSoma - intInteiro;
		intDig1 = ( intResto == 0 || intResto == 1 ) ? 0 : 11 - intResto;

		intSoma = 0;
		intSoma1 = 0;
		intSoma2 = 0;
		intNumero = 0;
		intMais = 0;
		strCGC = vlr.substr( vlr.length-7, 4 );
		strCampo = vlr.substr( 5, 3 );
		strCampo += ( strCGC + intDig1 );

		for ( i=1; i<9; i++ )
		{
			strCaracter = strCampo.substr( strCampo.length - i );
			intNumero = strCaracter.substr( 0, 1 );
			intMais = intNumero * ( i+1 );
			intSoma1 += intMais;
		}
		strCampo = vlr.substr( 0, 5 );
		
		for ( i=1; i<6; i++ )
		{
			strCaracter = strCampo.substr( strCampo.length - i );
			intNumero = strCaracter.substr( 0, 1 );
			intMais = intNumero * ( i+1 );
			intSoma2 += intMais;
		}

		intSoma = intSoma1 + intSoma2;
		dblDivisao = intSoma / 11;
		intInteiro = parseInt( dblDivisao ) * 11;
		intResto = intSoma - intInteiro;
		intDig2 = ( intResto == 0 || intResto == 1 ) ? 0 : 11 - intResto;

		strConf = intDig1.toString() + intDig2.toString();
		rt = ( strConf == vlr.substr( vlr.length - 2 ) );
	}
	else // Pessoa física
	{
		lngSoma = 0;
		intNumero = 0;
		intMais = 0;
		strCampo = vlr.substr( 0, 9 );

		for ( i=1; i<10; i++ )
		{
			strCaracter = strCampo.substr( strCampo.length - i );
			intNumero = parseInt( strCaracter.substr( 0, 1 ) );
			intMais = intNumero * ( i+1 );
			lngSoma += intMais;
		}
		dblDivisao = lngSoma / 11;
		lngInteiro = parseInt( dblDivisao ) * 11;
		intResto = lngSoma - lngInteiro;
		intDig1 = ( intResto == 0 || intResto == 1 ) ? 0 : 11 - intResto;
		strCampo += intDig1;
		
		lngSoma = 0;
		intNumero = 0;
		intMais = 0;
		for ( i=1; i<11; i++ )
		{
			strCaracter = strCampo.substr( strCampo.length - i );
			intNumero = parseInt( strCaracter.substr( 0, 1 ) );
			intMais = intNumero * ( i+1 );
			lngSoma += intMais;
		}
		
		dblDivisao = lngSoma / 11;
		lngInteiro = parseInt( dblDivisao ) * 11;
		intResto = lngSoma - lngInteiro;
		intDig2 = ( intResto == 0 || intResto == 1 ) ? 0 : 11 - intResto;
		strCampo += intDig2;
		strConf = intDig1.toString() + intDig2.toString();
		rt = ( strConf == vlr.substr( vlr.length - 2 ) );
	}
	return rt;
}
// Transforma o CGC para a rotina corretamente, os numeros devem ser filtrados antes
function TransCGC( vlr )
{
	var x, y, z;
	x = vlr.substr( 0, 8 ); y = vlr.substr( 8, 4 ); z = vlr.substr( 12, 2 );
	return ( x + '/' + y + '-' + z );
}
/*
Retira todas as letras e outros caracteres de uma string devolvendo
apenas numeros 
*/
function SoNumero( v )
{
	var x, ret;
	ret = '';
	for ( x=0; x<=v.length; x++ )
		ret += v.substr( x, 1 ) >= '0' && v.substr( x, 1 ) <= '9' ? v.substr( x, 1 ) : '';
	return ( ret );
} 
// ** FIM DO CODIGO DAS VALIDAÇÕES **
