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 FormataReal(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 ) ;
		}
	} 
}	
function formatCurrency(num) 
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+'.'+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + ',' + cents);
}		
function criaMascara(_RefObjeto, _Modelo)
{
	
	var valorAtual = _RefObjeto.value;
	var valorNumerico = '';
	var nIndexModelo = 0;
	var nIndexString = 0;
	var valorFinal = '';
	var adicionarValor = true;
	
	// limpa a string valor atual para verificar
	// se todos os caracteres são números
	for (i=0;i<_Modelo.length;i++)
	{
		if (_Modelo.substr(i,1) != '#')
		{
			valorAtual = valorAtual.replace(_Modelo.substr(i,1),'');
		}
	}
	 
	// verifica se todos os caracteres são números
	for (i=0;i<valorAtual.length;i++)
	{
		if (!isNaN(parseFloat(valorAtual.substr(i,1))))
		{
			valorNumerico = valorNumerico + valorAtual.substr(i,1);
		}
	}
	 
	// aplica a máscara ao campo informado usando
	// o modelo de máscara informado no script
	for (i=0;i<_Modelo.length;i++)
	{
	 
		if (_Modelo.substr(i,1) == '#')
		{
			if (valorNumerico.substr(nIndexModelo,1) != '')
			{
				valorFinal = valorFinal + valorNumerico.substr(nIndexModelo,1);
				nIndexModelo++;nIndexString++;
			}
			else 
			{
					adicionarValor = false;
			}
		}
	 	else 
		{
				if (adicionarValor && valorNumerico.substr(nIndexModelo,1) != '')
				{
				valorFinal = valorFinal + _Modelo.substr(nIndexString,1)
				nIndexString++;
				}
		}
	}
	 
	_RefObjeto.value = valorFinal
	 
}


