﻿/********************************************************************************/
/* Variáveis globais                                                            */
/*------------------------------------------------------------------------------*/
/*                                                                              */
if (document.layers){
	document.captureEvents(Event.KEYDOWN | Event.KEYUP);
	document.onkeydown = PTATMascaraValorTecla;
	document.onkeyup   = PTATMascaraValorExibe;
}
/*                                                                              */
/*------------------------------------------------------------------------------*/
/********************************************************************************/



/********************************************************************************/
/* Função: PTATMascaraValor                                                       */
/*------------------------------------------------------------------------------*/
/* Autor:                                                                       */
/*   Nome: Frank Gindri Brandolff                                               */
/*   Email: frank@pta.com.br                                                    */
/*   Data: 01/10/2001                                                           */
/*------------------------------------------------------------------------------*/
/* Descrição: Classe responsável por criar o objeto MascaraValor.               */
/*------------------------------------------------------------------------------*/
/* Parâmetros: Nome: o nome da variável que receberá o objeto do tipo           */
/*                   PTATMascaraValor deverá obrigatiramente ser passado como		*/
/*                   uma string neste parâmetro.								*/
/*------------------------------------------------------------------------------*/
/* Retorno: Objeto MascaraValor.												*/
/*------------------------------------------------------------------------------*/
/* Dependências: Nenhum.                                                        */
/*------------------------------------------------------------------------------*/
/*                                                                              */
function PTATMascaraValor(Nome, SepMilhar, SepDecimal, TotMilhar, TotDecimal, ValorEntrada, ExibeSepMilhar){

	//propriedades
	this.Nome        = Nome;
	this.Milhar      = '';
	this.Decimal     = '';
	this.sepMilhar   = SepMilhar;
	this.sepDecimal  = SepDecimal;
	this.totMilhar   = TotMilhar;
	this.totDecimal  = TotDecimal;
	this.Valor       = '';
	this.Qtde        = 0;
	this.Objeto      = null;
	this.ehDecimal   = false;
	this.exibeMilhar = ExibeSepMilhar;
	this.Form        = '';

	var Auxiliar     = '';
	var i = 0;
	var j = 0;

	if (ValorEntrada.length>0){
		while (ValorEntrada.charAt(i)!=this.sepDecimal && i<ValorEntrada.length){
			if ((ValorEntrada.charCodeAt(i)>=48) && (ValorEntrada.charCodeAt(i)<=57)) this.Milhar += ValorEntrada.charAt(i);
			i++;
		}
		if (i+1<ValorEntrada.length){
			this.ehDecimal = true;
			for (j = i+1; j < ValorEntrada.length; j++){
				if ((ValorEntrada.charCodeAt(j)>=48) && (ValorEntrada.charCodeAt(j)<=57)) this.Decimal += ValorEntrada.charAt(j);
			}
		}
	}

	//métodos
	this.Exibe      = PTATMascaraValorExibe;
	this.Tecla      = PTATMascaraValorTecla;
	this.Limpa      = PTATMascaraValorLimpa;
	this.SetValores = PTATMascaraValorSetValores;
	
	return this;
}


/*                                                                              */
/*------------------------------------------------------------------------------*/
/********************************************************************************/

function PTATMascaraValorSetValores(TotMilhar, TotDecimal, ValorEntrada){
	this.totMilhar   = TotMilhar;
	this.totDecimal  = TotDecimal;
	this.Objeto = (this.Objeto == null) ? document.getElementById(this.Nome) : this.Objeto;

	if (ValorEntrada.length>0){
		while (ValorEntrada.charAt(i)!=this.sepDecimal && i<ValorEntrada.length){
			if ((ValorEntrada.charCodeAt(i)>=48) && (ValorEntrada.charCodeAt(i)<=57)) this.Milhar += ValorEntrada.charAt(i);
			i++;
		}
		if (i+1<ValorEntrada.length){
			this.ehDecimal = true;
			for (j = i+1; j < ValorEntrada.length; j++){
				this.Decimal += ValorEntrada.charAt(j);
			}
		}
	}
  this.Objeto.value = '';
  this.Objeto.size  = this.totMilhar + this.totDecimal + 3 + parseInt((this.totMilhar/3),10);
  this.Objeto.maxLength = this.totMilhar + this.totDecimal + 5 + parseInt((this.totMilhar/3),10);
}




function PTATMascaraValorLimpa(){
	this.Objeto = (this.Objeto == null) ? document.getElementById(this.Nome) : this.Objeto;
	this.Objeto.value = this.Mascara;
	this.Valor = '';
}




/********************************************************************************/
/* Função: PTATMascaraValorExibe                                                  */
/*------------------------------------------------------------------------------*/
/* Autor:                                                                       */
/*   Nome: Frank Gindri Brandolff                                               */
/*   Email: frank@pta.com.br                                                    */
/*   Data: 01/10/2001                                                           */
/*------------------------------------------------------------------------------*/
/* Descrição: Método destinado a exibir os valores digitados juntamente com sua */
/*            máscara.                                                          */
/*------------------------------------------------------------------------------*/
/* Parâmetros: Texto: input text que recebeu o evento.                          */
/*             Objeto: o objeto do tipo máscara que manipula os eventos do input*/
/*                     text.                                                    */
/*------------------------------------------------------------------------------*/
/* Retorno: Nenhum.                                                             */
/*------------------------------------------------------------------------------*/
/* Dependências: Nenhum.                                                        */
/*------------------------------------------------------------------------------*/
/*                                                                              */
function PTATMascaraValorExibe(){
	var conteudo, qtdvalor, saida;
	var i, cont;
	saida = '';
	conteudo = '';
	this.Objeto = (this.Objeto == null) ? document.getElementById(this.Nome) : this.Objeto;

	for (i = this.Decimal.length; i>=0; i--){
		if (i<this.totDecimal) conteudo += this.Decimal.charAt(i);
	}
	if (this.ehDecimal) conteudo += this.sepDecimal;
	cont = (this.totMilhar > this.Milhar.length) ? this.Milhar.length : this.totMilhar;
	for (i = cont-1; i>=0; i--){
		conteudo += ((((cont-1-i) % 3)==0)&&(i<cont-1)&&(this.exibeMilhar)) ? this.sepMilhar + this.Milhar.charAt(i) : this.Milhar.charAt(i);
	}
	for (i=conteudo.length-1; i>=0; i--){
		saida += conteudo.charAt(i);
	}
	this.Objeto.value = saida;
	this.Objeto.select();
}
/*                                                                              */
/*------------------------------------------------------------------------------*/
/********************************************************************************/



/********************************************************************************/
/* Função: PTATMascaraValorTecla                                                  */
/*------------------------------------------------------------------------------*/
/* Autor:                                                                       */
/*   Nome: Frank Gindri Brandolff                                               */
/*   Email: frank@pta.com.br                                                    */
/*   Data: 01/10/2001                                                           */
/*------------------------------------------------------------------------------*/
/* Descrição: Função que trata a entrada de dados, filtra as teclas pressiona-  */
/*            das para aceitar apenas números. Verifica se a tecla backspace    */
/*            foi pressionada para excluir o último número digitado na proprie- */
/*            dade Valor. Verifica se a tecla delete foi pressionada para esva- */
/*            ziar a propriedade Valor.                                         */
/*------------------------------------------------------------------------------*/
/* Parâmetros: e: o evento realizado no input text da máscara.                  */
/*             Objeto: o objeto do tipo máscara que manipula os eventos do input*/
/*                     text.                                                    */
/*------------------------------------------------------------------------------*/
/* Retorno: Nenhum.                                                             */
/*------------------------------------------------------------------------------*/
/* Dependências: Nenhum.                                                        */
/*------------------------------------------------------------------------------*/
/*                                                                              */
function PTATMascaraValorTecla (e){
	var whichCode;
	this.Objeto = (this.Objeto == null) ? document.getElementById(this.Nome) : this.Objeto;

	
	if (document.layers){
		whichCode = e.which;
	}
	else{
		whichCode = (window.Event) ? e.which : e.keyCode;
	}

	var key;
	
	if (((whichCode >= 48) && (whichCode <= 57)) || ((whichCode >= 96) && (whichCode <= 105))){
		//a tecla pressionada é um número
		if (whichCode > 95){
			key = String.fromCharCode(whichCode-48);
		}
		else{
			key = String.fromCharCode(whichCode);
		}
		
		if (!this.ehDecimal){
			if (this.totMilhar>this.Milhar.length) this.Milhar += key;
		}
		else{
			if (this.totDecimal>this.Decimal.length) this.Decimal += key;
		}
	}
	else{
		//se a tecla pressionada não for um numero
		switch (whichCode){
			case 8://Tecla backspace
				//apagar o último caracter;
				if (!this.ehDecimal)
					this.Milhar  = this.Milhar.substring(0,this.Milhar.length-1);
				else
					this.Decimal = this.Decimal.substring(0,this.Decimal.length-1);
					if (this.Decimal.length==0) this.ehDecimal=false;
			break;
			case 46://tecla delete
				//apaga todo o conteúdo da máscara;
				this.Milhar  = '';
				this.Decimal = '';
				this.ehDecimal = false;
			break;
			case 110:
			case 188://virgula
		
				if (","==this.sepDecimal){
					if (!this.ehDecimal && this.totDecimal > 0 && this.Milhar!=''){
						this.ehDecimal = true;
					}
				}
			break;
			case 194:
			case 190://ponto
				if ("."==this.sepDecimal){
					if (!this.ehDecimal && this.totDecimal > 0 && this.Milhar!=''){
						this.ehDecimal = true;
					}
				}
			break;
		}
	}	
	
	return 0;
}
/*                                                                              */
/*------------------------------------------------------------------------------*/
/********************************************************************************/
