/*
Adicionar ao txtbox
onkeypress="return validaTecla('an',this, event);"
*/
function isNum(caractere) 
{ 
	var strValidos = "0123456789";
	if (strValidos.indexOf(caractere) == -1 ) return false; 
	return true;
} 

function isAlfa(caractere) 
{ 
	var strValidos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	if (strValidos.indexOf(caractere) == -1 ) return false; 
	return true;
} 

function isAlfaNum(caractere) 
{ 
	var strValidos = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	if (strValidos.indexOf(caractere) == -1 ) return false; 
	return true;
} 

function valida_tecla(tipo,campo, event) 
{ 
	var BACKSPACE= 8;
	var key;
	var tecla; 
	CheckTAB=true; 
  
	if(navigator.appName.indexOf("Netscape")!= -1)
  		tecla= event.which; 
	else 
		tecla= event.keyCode;   
		
	key = String.fromCharCode(tecla); 
  
	if (tecla == 13) return false; 
	
	if (tecla == BACKSPACE) return true;

	if (tipo == 'n') return (isNum(key));
	if (tipo == 'a') return (isAlfa(key));
	if (tipo == 'an') return (isAlfaNum(key));
}

// Validação de e-mail
function valida_email(campo)
{
}

// Form
function valida_txtbox(obj)
{
   	if (obj.value.length == 0)
	{
   		return false;
	}
   	else 
	{
   		return true;
   	}
}

// Limite de caracteres

/**
Colcar no Textbox os seguintes parâmetros:
onkeydown="limite_char(this)" onblur="limite_char(this)" onkeyup="limite_char(this)"
e para mostrar o contador, é só criar um objeto com o ID = ch_restante
*/
var strErro="Você ultrapassou o limite de caracteres.";
function limite_char(obj, limite)
{
	intLng=obj.value.length;
	if(intLng>limite)
	{
		obj.value=obj.value.substring(0,limite)
	}
	else
	{
		document.getElementById('ch_restante').innerHTML=(limite-obj.value.length);
	}
}

// Validação de e-mails
function valida_email(obj) {
	prim = obj.value.indexOf("@")
	if(prim < 2 || obj.value.indexOf("@",prim + 1) != -1 || obj.value.indexOf(".") < 1 || obj.value.indexOf(" ") != -1 || obj.value.indexOf(".@") > 0 || obj.value.indexOf("@.") > 0 || obj.value.indexOf(".com.br.") > 0 || obj.value.indexOf("/") > 0 || obj.value.indexOf("[") > 0 || obj.value.indexOf("]") > 0 || obj.value.indexOf("(") > 0 || obj.value.indexOf(")") > 0 || obj.value.indexOf("..") > 0) {
		alert("Formato de e-mail inválido.");
		return false;
	}
	return true;
}
//********************************************************
/*
Verificação de CIC / CGC
Devem ser passados como parametros:
campo = valor a ser pesquisado
empresa = se true, verifica CGC, senao CPF
*/
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 );
}

// 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 );
}

//ps: campo é a string com o cpf/cnpj
function validaCpfCgc(campo)
{
	if (campo == "")
	{
			return false ;
	}
	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(campo);

	if ( vlr.length == 14 ) // 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 ) );

		if (! rt ) 
		{
				//alert("Verifique o CNPJ digitado!");
				//alert(campo);
				return false;
		}
		
	}
	else // Pessoa física
	{
		if ( vlr.length == 11 ) // 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 ) );

			if (! rt ) 
			{
				//alert("Verifique o CPF digitado!");
				return false;
			}
	}
	else // quantidade de digitos errada cpf->11 e cgc->14 
	{
		if ( campo != "" )
		{
				//alert("Verifique o CPF/CGC digitado!");
				return false;
		}
	}
	}
	return true;		
}	
//********************************************************
//********************************************************

function validaCPF(form) {

	if (!validaCpfCgc(form.value))
		return false;	
	else
		return true;
}

function showClientes(reg)
{
	l=10;
	t=10;
	regiao = (reg == 'N' ? 'norte' : (reg == 'NE' ? 'nordeste' : (reg == 'CO' ? 'centro-oeste' : (reg == 'SE' ? 'sudeste' : 'sul'))));
	mostrascroll = reg == 'SE' ? 'yes' : 'no';
	
	window.open('../clientes/'+regiao+'.htm',"","resizable=no,toolbar=no,status=no,menubar=no,scrollbars="+mostrascroll+",width=400,height=450,center=yes,left="+l+",top="+t);
}
