/*
	MASCARAS - 3 PARÂMETROS: CAMPO, MÁSCARA, EVENTO PASSADOS ATRAVÉS DO EVENTO onKeyPress	
	EX:  <input type="text" onKeyPress="return formata(this, '§§§.§§§.§§§-§§', event)">
	O CARACTER '§' DEFINE QUE SÓ SERÁ PERMITIDO NÚMEROS
	O CARACTER '!' DEFINE QUE É PERMITIDO QUALQUER CARACTER
	
	
telefone: onKeyPress="return formata(this, '(§§) §§§§-§§§§', event);"  maxlength="14"
data:     onKeyPress="return formata(this, '§§/§§/§§§§', event);"      maxlength="10"
hora:     onKeyPress="return formata(this, '§§:§§', event);"           maxlength="5"
cpf:      onKeyPress="return formata(this, '§§§.§§§.§§§-§§', event);"  maxlength="14"
cep:      onKeyPress="return formata(this, '§§.§§§-§§', event);"       maxlength="9"

*/

function formata(campo, mask, evt) {
 
 if(document.all) { // Internet Explorer
    key = evt.keyCode; }
    else{ // Nestcape
       key = evt.which;
     }

 string = campo.value;  
 i = string.length;

 if (key != 8 && key != 0) {
  if (mask.charAt(i) == '§') {
	return (key > 47 && key < 58);
  } 
  else {
       if (mask.charAt(i) == '!') {  
	   		return true;
		}
   		for (c = i; c < mask.length; c++) {
         	if (mask.charAt(c) != '§' && mask.charAt(c) != '!')
        		campo.value = campo.value + mask.charAt(c);
      		else if (mask.charAt(c) == '!'){
                return true;
       		}
			else {
         		return (key > 47 && key < 58);
          	}
       	}
    }
  }
}