<!--

	/*
	 * Autor: Ricardo José Ramalho Moreira ( ricardojrm@gmail.com )
	 * Licença: GNU
	 * Nota: O uso desse script é totalmente gratuito sobre a impossição de não alterar
	 * os créditos do mesmo. Aqueles que corrigirem bugs ou melhorarem algo no código, por
	 * favor, me contate por e-mail relatando as modifações, bem como seu nome e e-mail oficial
	 * para que seja dada os devidos créditos.
	 *
	 * Atenciosamente,
	 * Ricardo José Ramalho Moreira
	 * Programador WEB
	 *
	 * Descrição:
	 *
	 * Esta função é a função principal que valida todos os campos de um formulário.
	 * Sua ideía é baseada na pseudo propriedade 'validar' de cada elemento do formulário.
	 * Quando você insere essa propriedade em algum elemento dentro do formulário alvo,
	 * ela se encarregará de descobrir qual o tipo do elemento, e baseado na opção passada
	 * como parametro, realizará a sua validação.
	 *
 	 * Versão: 1.4
	 *
	 * Changelog:	21/09/2005 - Adicionada a função mascara( src , mask ).
	 *				31/08/2005 - Corrigido bug da função FormatarTelefone( event , this ).
	 *				Agora sua máscara está mais precisa para números de 7 e 8 dígitos.
	 *
	 */
 
	 /* Formatação para qualquer mascara */
	function mascara( src , mask ) {
		var i = src.value.length;
		var saida = mask.substring( 0 , 1 );
		var texto = mask.substring( i )
		if ( texto.substring( 0 , 1 ) != saida ) { src.value += texto.substring( 0 , 1 ); }
	}

	function FormatarCEP( campo , teclapres ) {
			FormatarCep( campo , teclapres );
	}

	function FormatarPorcentagem( campo , teclapres ) {
		
		var tecla = teclapres.keyCode;
		tammax = campo.maxLength;
		pos = 2;
		//alert( tecla );
		vr = campo.value;
		vr = vr.replace( ".", "" );
		vr = vr.replace( /^0+/g, "" );
		//while ( vr.match( "%" ) ) { vr = vr.replace( "%", "" ); }
		tam = vr.length ;
		//alert(vr);
		if ( tam < tammax && tecla != 8 ) tam = vr.length + 1;

		if ( tecla == 8 ) tam = tam - 1;
		
		if ( tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) {
			if ( tam < 1 ) campo.value = "0.0" + vr;
			if ( tam <= 2 ) campo.value = "0." + vr;
			if ( tam > pos && tam < tammax ) campo.value = vr.substr( 0, tam - pos ) + '.' + vr.substr( tam - pos, tam );
		}
	
	}
	
	function FormatarCep( campo , teclapres ) {
		
		var tecla = teclapres.keyCode;
		tammax = campo.maxLength;
		pos = 3;
		//if ( tecla != 8 && tecla != 9 && tecla != 35 && tecla != 36 && tecla != 37 && tecla != 38 && tecla != 39 && tecla != 40 && ( tecla < 96 || tecla > 105 ) ) teclapres.returnValue = false;
		//else {
			vr = campo.value;
			vr = vr.replace( "-", "" );
			tam = vr.length ;
			if ( tam < tammax && tecla != 8 ) tam = vr.length + 1;

			if ( tecla == 8 ) tam = tam - 1;
			
			if ( tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) {
				if ( tam <= 2 ) campo.value = vr;
				if ( tam > pos && tam < tammax ) campo.value = vr.substr( 0, tam - pos ) + '-' + vr.substr( tam - pos, tam );
			}
		//}
		
	}
	
	function FormataData(campo,tammax,pos,teclapres){
		var tecla = teclapres.keyCode;
		
		if ( tecla != 8 && tecla < 96 || tecla > 105 ) teclapres.returnValue = false;
		else {

			vr = campo.value;
			vr = vr.replace( ".", "" );
			tam = vr.length ;
			if ( tam < tammax && tecla != 8 ) tam = vr.length + 1;

			if ( tecla == 8 ) tam = tam - 1;
			
			if ( tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) {
			if ( tam <= 2 ) campo.value = vr;
			if ( tam > pos && tam < tammax ) campo.value = vr.substr( 0, tam - pos ) + '.' + vr.substr( tam - pos, tam );
			}
		}
	}
	
	function FormataDinheiro( campo , teclapres ) {
		var tecla = teclapres.keyCode;
		tammax = campo.maxLength;
		pos = 2;
		if ( campo.value.length == tammax && tecla != 8 ) { teclapres.returnValue = false; }
		//if ( tecla != 8 && tecla != 9 && tecla < 96 || tecla > 105 ) { teclapres.returnValue = false; }
		//else {

			vr = campo.value;
			vr = vr.replace( ",", "" );
			vr = vr.replace( "R$ ", "" );
			vr = vr.replace( /^0+/g, "" );
			tam = vr.length ;
			if ( tam < tammax && tecla != 8 ) tam = vr.length + 1;

			if ( tecla == 8 ) tam = tam - 1;
			if ( tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) {
				if ( tecla == 8 && tam == -1 ) { teclapres.returnValue = false; }
				if ( tecla == 8 && tam == 0 ) { campo.value = "R$ 0,00" + vr; }
				if ( tecla == 8 && tam == 1 ) { campo.value = "R$ 0,0" + vr; }
				if ( tecla != 8 && tam <= 0 ) { campo.value = "R$ 0,00"; }
				if ( tecla != 8 && tam == 1 ) { campo.value = "R$ 0,0" + vr; }
				if ( tam == 2 ) { campo.value = "R$ 0," + vr; }
				if ( tam > pos && tam < tammax ) { campo.value = "R$ " + vr.substr( 0, tam - pos ) + ',' + vr.substr( tam - pos, tam ); }
			}
		//}
	}
	
	// Mascará para números telefónicos.
	function FormatarTelefone( param_evento , param_campo ) {
		var tecla = param_evento.keyCode;
		var temp = param_campo.value.replace(/-/g,"");
		var total = temp.length;
		var agree = new Array( "37" , "38" , "39" , "40" );
		if ( tecla == 8 ) { total -= 1; }
		//alert( tecla );
		if ( tecla != 16 && tecla != 9 && total > 2 ) {
			if ( tecla == 8 && total == 3 ) { param_campo.value = temp.substr( 0 , 4 ); }
			else if ( total < 7 || tecla == 8 && total == 7 ) { param_campo.value = temp.substr( 0 , 3 ) + "-" + temp.substr( 3 , total ); }
			else { param_campo.value = temp.substr( 0 , 4 ) + "-" + temp.substr( 4 , total ); }
		}
	}
	
	function FormatarCPF( campo , teclapres ) {
		var tecla = teclapres.keyCode;
		//alert(tecla);
		tammax = campo.maxLength;
		pos = 2;
		// Comentei estas linhas para deixar a responsabilidade
		// de verificar se o elemento digitado é um dígito.
		//if ( tecla != 8 && tecla != 9 && tecla != 35 && tecla != 36 && tecla != 37 && tecla != 38 && tecla != 39 && tecla != 40 && ( tecla < 96 || tecla > 105 ) ) teclapres.returnValue = false;
		//else {
			vr = campo.value;
			vr = vr.replace( "-", "" );
			tam = vr.length ;
			if ( tam < tammax && tecla != 8 ) tam = vr.length + 1;

			if ( tecla == 8 ) tam = tam - 1;
			
			if ( tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) {
				if ( tam <= 2 ) campo.value = vr;
				if ( tam > pos && tam < tammax ) campo.value = vr.substr( 0, tam - pos ) + '-' + vr.substr( tam - pos, tam );
			}
		//}
	}
	
//-->



function Limpar(valor, validos) {
// retira caracteres invalidos da string
var result = "";
var aux;
for (var i=0; i < valor.length; i++) {
aux = validos.indexOf(valor.substring(i, i+1));
if (aux>=0) {
result += aux;
}
}
return result;
}

//Formata número tipo moeda usando o evento onKeyDown

function Formata(campo,tammax,teclapres,decimal) {
var tecla = teclapres.keyCode;
vr = Limpar(campo.value,"0123456789");
tam = vr.length;
dec=decimal

if (tam < tammax && 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 <= dec )
{ campo.value = vr ; }

if ( (tam > dec) && (tam <= 5) ){
campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 6) && (tam <= 8) ){
campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
}
if ( (tam >= 9) && (tam <= 11) ){
campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 12) && (tam <= 14) ){
campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 15) && (tam <= 17) ){
campo.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 ) ;}
} 

}