// Trecho global de script
// Retorna os caracteres disponíveis numa caixa de texto
function TextArea_onKeyUp (strFieldName, strTarget, iMaxSize)
{
	if (iMaxSize <= eval("document.frmNavegador." + strFieldName + ".value.length"))
	{
		alert("Este campo pode conter no máximo " + iMaxSize + " caracteres!");
		eval("document.frmNavegador." + strFieldName + ".value = document.frmNavegador." + 
			strFieldName + ".value.substring (0, " + iMaxSize + ");");
		eval("document.frmNavegador." + strTarget + ".value = 0;");
	}
	else
	{
		eval("document.frmNavegador." + strTarget + ".value = " + iMaxSize + 
			"- document.frmNavegador." + strFieldName + ".value.length;");
	}
}

// Adiciona o método trim() ao objeto String
String.prototype.trim = function()
{
	// Usa regular expression para mudar a string substituindo os espaços por ""
	return this.replace (/(^\s*)|(\s*$)/g, "");
}

// Adiciona espaços à direita de uma string
function String_AddSpace (strText, iSize)
{
	var ct = iSize - strText.length;
	
	for (i = 0; i < ct; i++)
	{
		strText = strText + " ";
	}
	return(strText); 
}
// Foi alterada para atender as necessidades de formatação de data
// Depois deve ser criada uma função a parte para isso
function String_AddChar (strText, iSize, strChar, fToLeft)
{
	var ct;
	
	if (strText == "")
		return(strText); 
		
	ct = iSize - strText.length;
	
	for (i = 0; i < ct; i++)
	{
		if (fToLeft)
		{
			strText = strChar + strText;
		}
		else
		{
			strText = strText + strChar;
		}
	}
	return(strText); 
}

// Adiciona caracteres à direita ou esquerda de uma string
function String_AddChar_BK (strText, iSize, strChar, fToLeft)
{
	var ct;
	
	if (strText == "")
	{
		strText = "";
		ct = 0;
	}
	else
	{
		ct = iSize - strText.length;
	}

	for (i = 0; i < ct; i++)
	{
		if (fToLeft)
		{
			strText = strChar + strText;
		}
		else
		{
			strText = strText + strChar;
		}
	}
	return(strText); 
}

function FormatNumber(strNumero, precisao)
{
	var astrNumero;

	strNumero = String(strNumero);
	if (strNumero.indexOf(".") == -1)
	{
		strNumero = poe_ponto_e_virgulas(String_AddChar (strNumero, strNumero.length + precisao, '0', false));
		return(strNumero);
	}
	else
	{
		astrNumero = strNumero.split(".");
		if (astrNumero[1].length > precisao)
		{
			strNumero = poe_ponto_e_virgulas(astrNumero[0] + astrNumero[1].slice(0,precisao));
			return(strNumero);
		}
		else
		{
			strNumero = poe_ponto_e_virgulas(astrNumero[0] +  String_AddChar (astrNumero[1], precisao, '0', false));
			return(strNumero);
		}
	}
}

//Chega se o número passado tem vírgula como separador de 
//casas decimais
function ChecaVirgula(strNumero)
{
	var i;
	
	//quando recebe null no parâmetro chega a configuração
	//do Cliente
	if (strNumero == null)
	{
		strNumero = String (Number(1.5));
	}
	
	for (i = strNumero.length-1; i>=0; --i)
	{
		if (strNumero.charAt(i)== ",")
		{
			return(true);
		}	
		if (strNumero.charAt(i)== ".")
		{
			return(false);
		}	
	}
	
}

//Prepara o Número de acordo com a configuracao do servidor
function PreparaNumero(strNumero)
{
	var fVirgulaParametro;
	var strNumeroOld;
	fVirgulaParametro = ChecaVirgula(strNumero);
	
	if (fVirgulaParametro == false)
	{
		do
		{
			strNumeroOld = strNumero;
			strNumero = strNumero.replace(",","");
		}while(strNumeroOld != strNumero);
		return (strNumero);
	}	
	else
	{
		do
		{
			strNumeroOld = strNumero;
			strNumero = strNumero.replace(".","");
		}while(strNumeroOld != strNumero);
		return (strNumero.replace(",","."));
	}
}

// Substitui ' por \' ou " por \". Utilizar especialmente em eval()'s
function PreparaStringFromDB (strTexto, fAspas)
{
	var objRegExp;
	var ret;
	
	if (fAspas == true)
	{
		objRegExp = new RegExp ('"',"g");  // Cria um objeto "regular expression" para procurar todas as ocorrências.
		ret = strTexto.replace (objRegExp, '\\"');
	}
	else
	{
		objRegExp = new RegExp ("'","g");  // Cria um objeto "regular expression" para procurar todas as ocorrências.
		ret = strTexto.replace (objRegExp, "\\'");
	}

	return ret;
}

//-----------------------------------------------------
function ExibeValor(retorno)
{   
   var ValorTotal;
   var ValorEntrada;
   var TemPonto;
   var ValorCalculoEntrada;
   var ValorCalculo;

   // Formata Valor do carro
   ValorTotal = String(document.FormName.valor_veic1.value);   
   

   ValorCalculo = tira_ponto_e_virgulas(ValorTotal);

   // Formata Valor de entrada
   if (retorno == 'v')
      {
	   ValorEntrada = tira_ponto_e_virgulas(String(document.FormName.valor_entrada.value));
      }
   else
      {
	   ValorEntrada = tira_ponto_e_virgulas(String(document.FormName.valor_financ.value));
      }   

   ValorCalculoEntrada = ValorEntrada;
   
						
   if (Number(ValorCalculo) >= Number(ValorCalculoEntrada))
	{
	Valor_Financiado = (Number(ValorCalculo) - Number(ValorCalculoEntrada));
	}
	else
	{
		if (navigator.appName.indexOf("Netscape") == -1)
			{
			alert("Valor de entrada maior que valor do veículo!");
			Valor_Financiado = 0;
			}
		else
			{
			Valor_Financiado = 0;
			}
	
	}

   // Exibir Valor


   if (retorno == 'v')
      {
      document.FormName.valor_financ.value = poe_ponto_e_virgulas(Valor_Financiado);
      }
   else
      {
      document.FormName.valor_entrada.value = poe_ponto_e_virgulas(Valor_Financiado);
      }
}

function tira_ponto_e_virgulas(texto)
{	
		var txttmp;
		var i;
		var tam;
		tam = texto.length -1;
		txttmp="";
		i = 0;
		while (i <= tam)
			{if (texto.charAt(i) != "." && texto.charAt(i) != "," && texto.charAt(i) != " ")
							{txttmp = txttmp + texto.charAt(i)}
							i++}
		return txttmp;
}

function poe_ponto_e_virgulas(texto)
{
	var txttmp
	var i;
	var tam;
	var pos;
	var fVirgulaParametro;
	fVirgulaParametro = ChecaVirgula(null);
	texto = "" + texto + "";
	i = texto.length -1 ;
	txttmp="";
	pos = 0;
	var consiste; 
	consiste = "0123456789";
	
		if (fVirgulaParametro)
		{
		while (i >= 0)
			{if (consiste.indexOf(texto.charAt(i)) >= 0)
				{
				if (pos == 2)
					{txttmp = "," + txttmp}
					else
						if (pos == 5 || pos == 8 || pos == 11 || pos == 14)
								{txttmp = "." + txttmp }
					
				pos++
				txttmp = texto.charAt(i) + txttmp;
				}
				i--}
		}
		else
		{
				while (i >= 0)
			{if (consiste.indexOf(texto.charAt(i)) >= 0)
				{
				if (pos == 2)
					{txttmp = "." + txttmp}
					else
						if (pos == 5 || pos == 8 || pos == 11 || pos == 14 || pos == 18)
								{txttmp = "," + txttmp }
					
				pos++
				txttmp = texto.charAt(i) + txttmp;
				}
				i--}
		}
	
	return txttmp;
}

function poe_ponto(texto)
{var txttmp
		var i;
		var tam;
		var pos;
		var fVirgulaParametro;
		fVirgulaParametro = ChecaVirgula(null);
		texto = "" + texto + "";
		i = texto.length -1 ;
		txttmp="";
		pos = 0;
		var consiste; 
		consiste = "0123456789";
		if (fVirgulaParametro)
		{
				while (i >= 0)
			{if (consiste.indexOf(texto.charAt(i)) >= 0)
				{
				if (pos == 2)
					{txttmp = "." + txttmp}
				pos++
				txttmp = texto.charAt(i) + txttmp;
				}
				i--}
		}		
	return txttmp;
}

function formata_currency(conteudo, caracter)
{
var Browser

if (navigator.appName.indexOf("Netscape") > -1)
		{Browser = "N"}
else
		{Browser = "IE"}
		
if (Browser == "N")
	{
	if (caracter < 48 || caracter > 57)
	{conteudo.value = poe_ponto_e_virgulas(conteudo.value)
	return}
}


var temporario;
temporario = conteudo.value;

temporario = tira_ponto_e_virgulas(temporario);

var ultimo_caracter;
var consiste ;
consiste = "0123456789";


if (Browser == "N")
{	
ultimo_caracter = temporario.substring(0,1)
temporario = temporario.substring(1,temporario.length) + ultimo_caracter
}
else
{
ultimo_caracter = temporario.substring(temporario.length-1,temporario.length)
}


if (consiste.indexOf(ultimo_caracter) >= 0 )
{temporario = poe_ponto_e_virgulas(temporario)
conteudo.value = temporario
conteudo.focus()
}

else
{
temporario = temporario.substring(0,temporario.length-1)
conteudo.value = poe_ponto_e_virgulas(temporario)
conteudo.focus()}
}
//-----------------------------------------------------