/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|| Script language: JavaScript
|| Funcao para validacao de mascara pre-determinada
|| 
|| Ex:
|| <input type="text" onkeypress="mask(this,'000.000.000.000:000/00-00',1)">
|| Author: Leonardo Marchini Loureiro - Brazil
|| leonardo@loureiro.as
|| Modified: may 11, 2004
||
|| this		= recebe o campo
|| formato	= formato da mascara
|| conteudo	= 1 - Só Numeros; 2 - Só Letras; 3 - Numeros e Letras; 4 - Alpha numerico(Qualquer caracter)
*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function mask(campo,formato,conteudo,evento){

 	var i, j,k;
 	var tamanho		= formato.length;
 	
 	var Elementos = new Array(".","/","-",":","(",")",",","+","%","$"," ");
 	var tam = Elementos.length;
 	var posElementos = new Array(tam);
 	var auxElementos = new Array(tam); 	
 	for(k=0;k<tam;k++){
 		posElementos[k]=new Array(tamanho);
 		auxElementos[k] = formato;
 	}
 	
	var tecla = 0;
	if(evento!=null) tecla = evento.keyCode;
	else tecla = event.keyCode;
	var dados = campo.value;
	campo.maxLength = tamanho;	
	
	/*
	if(! (tecla==32 					||//ESPAÇO	
		tecla==61						|| //+
		(tecla>=48 && tecla<=57)        || //NUMEROS
		(tecla>= 96 && tecla <= 105)    || //NUM PAD NUMEROS
		(tecla>= 106 && tecla <= 122)   || 
		(tecla>= 65 && tecla <= 90)
		 || tecla==0	)	)		return;
			
	switch (conteudo){
		case 1: // Verifica se soh podem ser entrados valores numericos
			if (!(	(tecla>=48 && tecla<=57) || (tecla>= 96 && tecla <= 105)	)) {
				campo.value=dados.substring(0,campo.value.length-1);
				return;
			}
			break;
			
		case 2: // Somente Letras
			if (!(  (tecla>= 96 && tecla <= 122) || (tecla>= 65 && tecla <= 90))){
				campo.value=dados.substring(0,campo.value.length-1);
				return;
			}
			break;
		case 3: // Letras e numeros
			if (! ((tecla>=48 && tecla<=57) || (tecla>= 96 && tecla <= 122) 
				|| (tecla>= 65 && tecla<= 90)) ){
				campo.value=dados.substring(0,campo.value.length-1);
				return;
			}
			break;
	}*/
	
 // ---------------------------------------- PEGA A FORMATACAO DA MASCARA -----------------------------------
 	for (i=0;i<tamanho;i++){
 		
 		for(k=0;k<tam;k++)
 			posElementos[k][i] = auxElementos[k].indexOf(Elementos[k]); 	

 		for(k=0;k<tam;k++)
 			auxElementos[k] = auxElementos[k].substring(posElementos[k][i]+1,tamanho); 	
 		
 		if (i > 0){
 			for(k=0;k<tam;k++)
 				posElementos[k][i]=posElementos[k][i]+posElementos[k][i-1];
			for(k=0;k<tam;k++)
 				posElementos[k][i]=posElementos[k][i]+1; 				
 		}
 		
 				
	// ---------------------------------------- APLICA A FORMATACAO DA MASCARA -----------------------------------
		//tecla = keycode(keyPress);
		if (tecla != 8 && tecla != 45 && tecla != 46 && tecla != 47 && tecla != 58){
		
			for(k=0;k<tam;k++){
 				if(campo.value.length == posElementos[k][i]){ 				
 					campo.value = campo.value + Elementos[k];
					campo.focus();
				}
 			}
 		}
 	}  		
}
