//---------------------------------------------------------------------------------------------------------
// Por Alexandre Rocha  -  alexandressrocha@yahoo.com.br
//---------------------------------------------------------------------------------------------------------
// Nome:	LibFormataDados.js
// 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).
//---------------------------------------------------------------------------------------------------------
// FormataCPF			Máscara para CPF (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; 
	} 
}

//---------------------------------------------------------------------------------------------------------








//
//-----------------------------------------------------------------------------
// Constantes
var vbCr = "\r";
var vbLf = "\n";
var vbCrLf = vbCr+vbLf;
var vbTab = "\t";

function Left(s, n){
	if(n>s.length)
		n=s.length;
		
	return s.substring(0, n);
}
function Right(s, n){
	var t=s.length;
	if(n>t)
		n=t;
		
	return s.substring(t-n, t);
}
function Mid(s, n, c){
	
	var numargs=Mid.arguments.length;
	
	// Si sólo se pasan los dos primeros argumentos
	if(numargs<3)
		c=s.length-n+1;
		
	if(c<1)
		c=s.length-n+1;
	if(n+c >s.length)
		c=s.length-n+1;
	if(n>s.length)
		return "";
		
	return s.substring(n-1,n+c-1);
}
function LTrim(s){
	var i=0;
	var j=0;
	
	for(i=0; i<=s.length-1; i++)
		if(s.substring(i,i+1) != ' '){
			j=i;
			break;
		}
	return s.substring(j, s.length);
}
function RTrim(s){

	var j=0;
	
	for(var i=s.length-1; i>-1; i--)
		if(s.substring(i,i+1) != ' '){
			j=i;
			break;
		}
	return s.substring(0, j+1);
}
function Trim(s){

	return LTrim(RTrim(s));
}
function InStr(n, s1, s2){

	var numargs=InStr.arguments.length;
	
	if(numargs<3)
		return n.indexOf(s1)+1;
	else
		return s1.indexOf(s2, n)+1;
}
function RInStr(n, s1, s2){

	var numargs=RInStr.arguments.length;
	
	if(numargs<3)
		return n.lastIndexOf(s1)+1;
	else
		return s1.lastIndexOf(s2, n)+1;
}
function Space(n){

	var t="";
	
	for(var i=1; i<=n; i++)
		t=t+" ";
	
	return t;
}
function jString(n, c){

	var t="";
	
	for(var i=1; i<=n; i++)
		t=t+c;
	return t;
}
function UCase(s){

	return s.toUpperCase();
}
function LCase(s){

	return s.toLowerCase();
}
function Len(s){

	return s.length;
}
function StrReverse(s){

	var i=s.length;
	var t="";
	
	while(i>-1){
		t=t+ s.substring(i,i+1);
		i--;
	}
	return t;
}

//-----------------------------------------------------------------------------

function FormataCPF(pForm,pCampo,pTeclaPres){
	var wTecla, wVr, wTam;
	var pTamMax = 11;
	var pPos1 = 8;
	var pPos2 = 5;
	var pPosTraco = 2;
				  
	wTecla = pTeclaPres.keyCode;
	wVr = pForm[pCampo].value;
	wVr = wVr.toString().replace( "-", "" );
	wVr = wVr.toString().replace( ".", "" );
	wVr = wVr.toString().replace( ".", "" );
	wVr = wVr.toString().replace( "/", "" );
	wTam = wVr.length ;

	if (wTam < pTamMax && wTecla != 8) { 
		wTam = wVr.length + 1 ; 
	}

	if (wTecla == 8 ) { 
		wTam = wTam - 1 ; 
	}
				   
	if ( wTecla == 8 || wTecla == 88 || wTecla >= 48 && wTecla <= 57 || wTecla >= 96 && wTecla <= 105 ){
		if ( wTam <= 2 ){
			pForm[pCampo].value = wVr ;
		}
		if (wTam > pPosTraco && wTam <= pTamMax) {
			wVr = wVr.substr(0, wTam - pPosTraco) + '-' + wVr.substr(wTam - pPosTraco, wTam);
		}
		if ( wTam == pTamMax){
			wVr = wVr.substr( 0, wTam - pPos1 ) + '.' + wVr.substr(wTam - pPos1, 3) + '.' + wVr.substr(wTam - pPos2, wTam);
		}
		pForm[pCampo].value = wVr;				 
	}
}



//
//-----------------------------------------------------------------------------
// Fim
//-----------------------------------------------------------------------------
//

