//MÁSCARA DE NÚMEROS

//EXEMPLO DE ULTILIZAÇÃO
//onkeypress="return(Mascara(this, '99/99/9999', event))"

function Mascara(objeto, sMask, evtKeyPress)
{
	
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	
	if(document.all)
	{ // Internet Explorer
		nTecla = evtKeyPress.keyCode;
	} 
	else if(document.layers)
	{ // Nestcape
		nTecla = evtKeyPress.which;
	} 
	else
	{
		nTecla = evtKeyPress.which;
		if (nTecla == 8)
		{
			return true;
		}
	}

    sValue = objeto.value;

    // Limpa todos os caracteres de formatação que
    // já estiverem no campo.
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( " ", "" );
    fldLen = sValue.length;
    mskLen = sMask.length;

    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;

    while (i <= mskLen)
	{
      bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
      bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

      if (bolMask)
	  {
        sCod += sMask.charAt(i);
        mskLen++;
	  }
      else
	  {
        sCod += sValue.charAt(nCount);
        nCount++;
      }

      i++;
    }

    objeto.value = sCod;

    if (nTecla != 8)
	{ // backspace
      if (sMask.charAt(i-1) == "9")
	  { // apenas números...
        return ((nTecla > 47) && (nTecla < 58));
	  }
      else
	  { // qualquer caracter...
        return true;
      }
    }
    else
	{
      return true;
    }
}
  
  
//MASCARA DE VALORES

//EXEMPLO DE ULTILIZAÇÃO
//onKeyDown="MascaraArea(this,Area);" onKeyPress="MascaraArea(this,Area);" onKeyUp="MascaraArea(this,Area);"
//onKeyDown="MascaraArea(this,Valor);" onKeyPress="MascaraArea(this,Valor);" onKeyUp="MascaraArea(this,Valor);"
	
	/*Função Pai de Mascaras*/
    function MascaraArea(valor,funcao)
	{
        valor_objeto=valor
        valor_funcao=funcao
        setTimeout("ExecutaMascara()",1)
    }
    
    /*Função que Executa os objetos*/
    function ExecutaMascara()
	{
        valor_objeto.value=valor_funcao(valor_objeto.value)
    }
    	
	/*Função que padroniza Area*/
    function Area(valor)
	{
        valor=valor.replace(/\D/g,"") 
		valor=valor.replace(/(\d)(\d{9})$/,"$1.$2")
		valor=valor.replace(/(\d)(\d{6})$/,"$1.$2")
        valor=valor.replace(/(\d)(\d{3})$/,"$1.$2") 
        return valor 
    }
	
	function Valor(valor)
	{
		valor=valor.replace(/\D/g,"") //Removalore tudo o que não é dígito
		valor=valor.replace(/^([0-9]{3}\.?){3}-[0-9]{2}$/,"$1.$2");
		valor=valor.replace(/(\d)(\d{8})$/,"$1.$2")
		valor=valor.replace(/(\d)(\d{5})$/,"$1.$2")
		valor=valor.replace(/(\d)(\d{2})$/,"$1,$2") //Coloca ponto antes dos 2 últimos digitos
		return valor
    }

