//FORMATA CPF
function FormataCPF(e,form,valor,nome)
{
	var tecla,Str;
	if (valor.length == 3){	Str='document.'+form+'.'+nome+'.value = document.'+form+'.'+nome+'.value + "." ;';eval(Str);}
	if (valor.length == 7){	Str='document.'+form+'.'+nome+'.value = document.'+form+'.'+nome+'.value + "." ;';eval(Str);}
	if (valor.length == 11){Str='document.'+form+'.'+nome+'.value = document.'+form+'.'+nome+'.value + "-" ;';eval(Str);}
	
	if(document.all)tecla = event.keyCode;
	else return true;
	if(tecla > 47 && tecla < 58 )	return true;
	if(tecla == 13 )return true;	
	return false;
}

//FORMATA CNPJ
function FormataCNPJ(e,form,valor,nome)
{
	var tecla,Str;
	if (valor.length == 8){	Str='document.'+form+'.'+nome+'.value = document.'+form+'.'+nome+'.value + "/" ;';eval(Str);}
	if (valor.length == 13){	Str='document.'+form+'.'+nome+'.value = document.'+form+'.'+nome+'.value + "-" ;';eval(Str);}
	
	if(document.all)tecla = event.keyCode;
	else return true;
	if(tecla > 47 && tecla < 58 )	return true;
	if(tecla == 13 )return true;	
	return false;
}

//FORMATA DATA
function FormataDATA(e,form,valor,nome)
{
	var tecla,Str;
	if (valor.length == 2){	Str='document.'+form+'.'+nome+'.value = document.'+form+'.'+nome+'.value + "/" ;';eval(Str);}
	if (valor.length == 5){	Str='document.'+form+'.'+nome+'.value = document.'+form+'.'+nome+'.value + "/" ;';eval(Str);}
	
	if(document.all)tecla = event.keyCode;
	else return true;
	if(tecla > 47 && tecla < 58 )	return true;
	if(tecla == 13 )return true;	
	return false;
}


//FORMATA CEP
function FormataCEP(e,form,valor,nome)
{
	var tecla,Str;
	if (valor.length == 5){	Str='document.'+form+'.'+nome+'.value = document.'+form+'.'+nome+'.value + "-" ;';eval(Str);}
	
	if(document.all)tecla = event.keyCode;
	else return true;
	if(tecla > 47 && tecla < 58 )	return true;
	if(tecla == 13 )return true;	
	return false;
}
 // ISNUMBERSTRING
function isNumberString (InString)  
{
	if(InString.length==0) 
		return (false);
	RefString="1234567890";
	for (Count=0; Count < InString.length; Count++)  {
		TempChar= InString.substring (Count, Count+1);
		if (RefString.indexOf (TempChar, 0)==-1)  
			return (false);
	}
	return (true);
}

//ISDATE
function isDate (Valor)
{
	// Verifica de possui 10 caracteres
	if(Valor.length!=10) 
		return (false);
	// Verifica de colocou as barras		
	if(Valor.charAt(2)!='/' | Valor.charAt(5)!='/') 
		return (false);
	// Verifica de colocou todos os números		
	if(!isNumberString(Valor.charAt(0)+Valor.charAt(1)+Valor.charAt(3)+Valor.charAt(4)+Valor.charAt(6)+Valor.charAt(7)+Valor.charAt(8)+Valor.charAt(9)))	
		return (false);
	// Verifica dia 01 até 31      
	if (parseInt(Valor.charAt(0)+Valor.charAt(1)) < 1 | parseInt(Valor.charAt(0)+Valor.charAt(1)) > 31) 
		if (Valor.charAt(0)+Valor.charAt(1)!='08' & Valor.charAt(0)+Valor.charAt(1)!='09' )
			return (false);
	// Verifica mês 01 até 12
	if (parseInt(Valor.charAt(3)+Valor.charAt(4)) < 1 | parseInt(Valor.charAt(3)+Valor.charAt(4)) > 12) 
		if (Valor.charAt(3)+Valor.charAt(4)!='08' & Valor.charAt(3)+Valor.charAt(4)!='09' )
			return (false);	
			
	return (true);
}

// VALIDA_CPF
function Valida_CPF(valor)
{  
	
	Mult1 = 10;
	Mult2 = 11;
	dig1 = 0;
	dig2 = 0;
	svalor = String(valor);
        
	if (
		svalor == '00000000000'
		|| svalor == '11111111111'
		|| svalor == '22222222222'
		|| svalor == '33333333333'
		|| svalor == '44444444444'
		|| svalor == '55555555555'
		|| svalor == '66666666666'
		|| svalor == '77777777777'
		|| svalor == '88888888888'
		|| svalor == '99999999999'
		 )	
	  return(false);
  
	for (i = 1; i<=9; i++) {		
		dig1 = dig1 + (parseInt(valor.charAt(i-1)) * Mult1) ;
		Mult1--
  }

	for (i = 1 ;i<=10 ;i++) {		
		dig2 = dig2 + (parseInt(valor.charAt(i-1)) * Mult2 );
		Mult2--
	}

	dig1 = (dig1 * 10) % 11;
	dig2 = (dig2 * 10) % 11;

	if (dig1 == 10)
		dig1 = 0;              
           
	if (dig2 == 10)
		dig2 = 0;

	if (dig1 != parseInt( valor.charAt(10-1) ) )
		return(false);
		
	if (dig2 != parseInt( valor.charAt(11-1) ) )
		return(false);

	return(true);  

}

// VALIDA_CGC
function Valida_CGC(VALOR)
{

	Mult1 = "543298765432";
	Mult2 = "6543298765432";
	dig1 = 0;
	dig2 = 0;

	for (i = 1;i<=12;i++)
		dig1 = dig1 + ( parseInt(VALOR.charAt(i-1)) * parseInt( Mult1.charAt(i-1) ) );

	for (i = 1; i<=13; i++)	  
		dig2 = dig2 + ( parseInt(VALOR.charAt(i-1)) * parseInt( Mult2.charAt(i-1) ) );
	
	dig1 = (dig1 * 10) % 11;
	dig2 = (dig2 * 10) % 11;

	if (dig1 == 10) 
		dig1 = 0;
		
	if (dig2 == 10) 
		dig2 = 0;

	if (dig1 != parseInt( VALOR.charAt(13-1) ))
	   return(false);
		
	if (dig2 != parseInt( VALOR.charAt(14-1) ))
		return(false);

	return(true);
}
 
// ALLTRIM
function allTrim(InString) {
		var LoopCtrl=true;

		while (LoopCtrl) {
			if (InString.indexOf("  ") != -1) {
				Temp = InString.substring(0, InString.indexOf("  "));
				InString = Temp + InString.substring(InString.indexOf("  ")+1,InString.length)
			}
			else LoopCtrl = false;
		}
		if (InString.substring(0, 1) == " ")
			InString = InString.substring(1, InString.length);
			
		if (InString.substring (InString.length-1) == " ")
			InString = InString.substring(0, InString.length-1);
			
   return (InString)
}
