//---------------------------------------------------------------------------------------------------------
// Nome:		LibFormataDados
// Descrição:	Arquivo de biblioteca de funções para Formatação de Dados.
//---------------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------------
// Descrição das funções disponíveis neste arquivo:
//---------------------------------------------------------------------------------------------------------
// Nome						Descrição
//---------------------------------------------------------------------------------------------------------
// SomentePreco				Permite digitar números e vírgula (KeyPress).
//---------------------------------------------------------------------------------------------------------
// FormataPreco				Formata preço para padrão brasileiro (OnBlur).
//---------------------------------------------------------------------------------------------------------
// SomenteNumero			Permite digitar somente números (KeyPress).
//---------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------
// Duas funções para permitir digitar no campo somente valor monetário.

// Exemplo: 
// <input type="text" name="txtValor" OnKeyPress="SomentePreco(this);" OnBlur="FormataPreco(this);">

// Permite digitar números e vírgula.
// Coloque no evento KeyPress(verifica os caracteres durante a digitação).
function SomentePreco(oCampo)
{

	var sPreco = oCampo.value;
	sPreco = sPreco.toString()

	// Permite digitar somente números e vírgula.
	var charCode = (navigator.appName == "Netscape") ? event.which : event.keyCode;
    if (charCode < 48 || charCode > 57)
    {
		if (charCode != 44 && charCode != 45)
		{
			event.returnValue=false;
			return false;
		}
	}

	// Se foi digitado vírgula.	
	if (charCode == 44)
	{	
		// Não permitir digitar vírgula no início.
		if (sPreco.indexOf(",") == -1 && (sPreco.length < 1))
		{
			event.returnValue=false; 
		}

		// Não permite digitar mais que uma vírgula.
		if (sPreco.lastIndexOf(",") != -1)
		{
			event.returnValue=false;
		}
		
		if (sPreco.indexOf(",") != sPreco.lastIndexOf(","))
		{
			event.returnValue=false;
		}

	}
		
	// Se foi digitado traço.
	if (charCode == 45)
	{

		// Permitir digitar traço somente no início.
		if ((sPreco.lastIndexOf("-")) != -1 || (sPreco.length > 0))
		{
			event.returnValue=false; 
		}

		// Não permite digitar mais que um sinal de menor.
		
		if (sPreco.lastIndexOf("-") != -1)
		{
			event.returnValue=false;
		}

		if (sPreco.indexOf("-") != sPreco.lastIndexOf("-"))
		{
			event.returnValue=false;
		}
		
	}
		
	oCampo.value = sPreco;	

}

// Formata preço para padrão brasileiro.
// Coloque no evento KeyPress(verifica os caracteres durante a digitação).
function FormataPreco(oCampo)
{
	// Formata número para preço.	
	numero = oCampo.value;

	if ((numero==null) || (numero==''))
	{
		numero = 0,00;
		oCampo.value = numero;
	}

	sVirgula = numero.toString()
	posicao_virgula = sVirgula.indexOf(",")
	if (posicao_virgula!= -1)
	{
		numero = sVirgula.replace("," , ".");
	}

	numero_formatado = (parseFloat(numero) * 1000)/10;
	numero_formatado = parseInt(numero_formatado);
	numero_ultimo_digito = numero * 1000;
	numero_ultimo_digito = parseInt(numero_ultimo_digito);

	string_numero = numero_ultimo_digito.toString();
	ultimo_digito = string_numero.substring(string_numero.length-1,string_numero.length);

	if (ultimo_digito>=5)
	{
		numero_formatado = numero_formatado + 1;
	}
	
	numero_formatado = numero_formatado / 100;
	oCampo.value = parseFloat(numero_formatado);
	oCampo.value = oCampo.value.replace("." , ",");
	
}
//---------------------------------------------------------------------------------------------------------

// Permite digitar somente números.
// Ex.: <input type="text" name="txtNumero" OnKeyPress="SomenteNumero();">
// Utilizar no evento OnKeyPress.
// Testado: 29 Jul 2002

function SomenteNumero() 
{	
    if (event.keyCode < 48 || event.keyCode > 57)
    {    
		event.returnValue=false; 
	} 
}

//---------------------------------------------------------------------------------------------------------