// JavaScript Document

/******************************************************************************************************************************/
// JavaScript MASCARAS
Mascaras = {

	IsIE: navigator.appName.toLowerCase().indexOf('microsoft')!=-1,
	AZ: /[A-Z]/i,
	Acentos: /[À-ÿ]/i,
	Num: /[0-9]/,

	carregar: function(parte){
		var Tags = ['input','textarea'];
		if (typeof parte == "undefined") parte = document;
		for(var z=0;z<Tags.length;z++){
			Inputs=parte.getElementsByTagName(Tags[z]);
			for(var i=0;i<Inputs.length;i++)
				if(('button,image,hidden,submit,reset').indexOf(Inputs[i].type.toLowerCase())==-1)
					this.aplicar(Inputs[i]);
		}
	},

	aplicar: function(campo){
		tipo = campo.getAttribute('tipo');
		if (!tipo || campo.type == "select-one") return;
		orientacao = campo.getAttribute('orientacao');
		mascara = campo.getAttribute('mascara');

		if(tipo.toLowerCase() == "numerico" && eval(campo.getAttribute('negativo'))){
			orientacao = "esquerda";
			tamanho = campo.getAttribute('maxLength');
			if (!tamanho || tamanho > 50)
				tamanho = 10;

			if(!mascara)
				campo.setAttribute("mascara", this.geraMascaraNumerico(tamanho));
			campo.setAttribute("orientacao", orientacao);
		}
		if (tipo.toLowerCase() == "decimal"){
			orientacao = "esquerda";
			casasdecimais = campo.getAttribute('casasdecimais');
			tamanho = campo.getAttribute('maxLength');
			if (!tamanho || tamanho > 50)
				tamanho = 10;
			if (!casasdecimais)
				casasdecimais = 2;

			campo.setAttribute("mascara", this.geraMascaraDecimal(tamanho, casasdecimais));
			campo.setAttribute("orientacao", orientacao);
		}
		if (orientacao && orientacao.toLowerCase() == "esquerda") campo.style.textAlign = "right";
		if (mascara) campo.setAttribute("maxLength", mascara.length);
		if (tipo){
			//  campo.onkeypress 	= function(e){ return Mascaras.onkeypress(e?e:event); };
			campo.onkeydown 	= function(e){ return Mascaras.onkeydown(e?e:event); };
			campo.onkeyup 		= function(e){ Mascaras.onkeyup(e?e:event, campo) };
			campo.onfocus		= function(e){ Mascaras.onfocus(e?e:event, campo) };
			campo.onfocusout 	= function(e){ Mascaras.onfocusout(e?e:event, campo) };
		}
		campo.setAttribute("snegativo", ((campo.value).substr(0,1) == "-" ? "s" : "n"));
	},

	onkeydown: function(e){
		KeyCode = this.IsIE ? event.keyCode : e.which;
		if (KeyCode == 9) return true;
			campo =  this.IsIE ? event.srcElement : e.target;

		campo.setAttribute("snegativo", ((campo.value).substr(0,1) == "-" ? "s" : "n"));
		readonly = campo.getAttribute('readonly');
		tipo = campo.getAttribute('tipo').toLowerCase();

		if (readonly) return;
		maxlength = campo.getAttribute('maxlength');
		selecao = this.selecao(campo);

		if (selecao.length > 0 && KeyCode != 0){
			campo.value = ""; return true;
		}

		if (KeyCode == 0) return true;
		var Char = "";

		if(KeyCode < 96 || KeyCode > 105){
			Char = String.fromCharCode(KeyCode);
		}else{
			switch(KeyCode){ 
				case 96: 
					Char = "0";
					break;
				case 97: 
					Char = "1";
					break;
				case 98: 
					Char = "2";
					break;
				case 99: 
					Char = "3";
					break;
				case 100: 
					Char = "4";
					break;
				case 101: 
					Char = "5";
					break;
				case 102: 
					Char = "6";
					break;
				case 103: 
					Char = "7";
					break;
				case 104: 
					Char = "8";
					break;
				case 105: 
					Char = "9";
					break;
			}
		}

		valor = campo.value;
		mascara = campo.getAttribute('mascara');
		if (KeyCode != 8){
			negativo = eval(campo.getAttribute('negativo'));
			if(negativo && KeyCode == 45){
				snegativo = campo.getAttribute('snegativo');
				snegativo = (snegativo == "s" ? "n" : "s");
				campo.setAttribute("snegativo", snegativo);
			}else{
				valor += Char
				if ((tipo == "numerico" || tipo == "decimal") && Char.search(this.Num) == -1) return false;
				if (KeyCode != 32 && tipo == "caracter" && Char.search(this.AZ) == -1 && Char.search(this.Acentos) == -1) return false;
			}
		}
		if (tipo == "decimal"){
			this.formatValue(campo, valor);
			valor = campo.value;
			this.aplicarMascara(campo, valor);
			return false;
		}else{
			if (mascara){
				this.aplicarMascara(campo, valor);
				return false;
			}
		}
		return true;
	},

	onkeyup: function(e, campo){
		KeyCode = this.IsIE ? event.keyCode : e.which;
		if (KeyCode != 9 && KeyCode != 16 && KeyCode != 109){
			valor = campo.value;

			if (KeyCode == 8) valor = valor.substr(0,valor.length-1);
	
			if (tipo == "decimal" && campo.value !=""){
				this.formatValue(campo, valor);
				valor = campo.value;
			} 
	
			this.aplicarMascara(campo, valor);
		}
	},

	onfocus: function(e, campo){
		cpfcnpj = eval(campo.getAttribute('cpfcnpj'));
		if(cpfcnpj){
			valor = campo.value;
			valor = ReplaceAll(valor,".","");
			valor = ReplaceAll(valor,"-","");
			valor = ReplaceAll(valor,"/","");
			campo.setAttribute("mascara","##############");
			this.aplicarMascara(campo, valor);
			campo.focus();
		}
		cpf = eval(campo.getAttribute('cpf'));
		if(cpf){
			valor = campo.value;
			valor = ReplaceAll(valor,".","");
			valor = ReplaceAll(valor,"-","");
			campo.setAttribute("mascara","###########");
			this.aplicarMascara(campo, valor);
			campo.focus();
		}
	},

	onfocusout: function(e, campo){
		valor = campo.value;
		cpfcnpj = eval(campo.getAttribute('cpfcnpj'));
		if(cpfcnpj){
			tamanho = campo.getAttribute('maxLength');
			if(valor.length == 11){
				campo.setAttribute("mascara","###.###.###-##");
			}
			if(valor.length == 14){
				campo.setAttribute("mascara","##.###.###/####-##");
			}
			this.aplicarMascara(campo, valor);
		}
		cpf = eval(campo.getAttribute('cpf'));
		if(cpf){
			tamanho = campo.getAttribute('maxLength');
			if(valor.length == 11){
				campo.setAttribute("mascara","###.###.###-##");
			}
			this.aplicarMascara(campo, valor);
		}
	},

	aplicarMascara: function(campo, valor){
		mascara = campo.getAttribute('mascara');
		if (!mascara) return;

		negativo = eval(campo.getAttribute('negativo'));
		snegativo = campo.getAttribute('snegativo');

		if (negativo && valor.substr(0,1) == "-") 
			valor = valor.replace("-","");

		orientacao = campo.getAttribute('orientacao');
		var i = 0;
		for(i=0;i<mascara.length;i++){
			caracter = mascara.substr(i,1);
			if (caracter != "#") valor = valor.replace(caracter, "");
		}
		retorno = "";
		if (orientacao != "esquerda"){
			contador = 0;
			for(i=0;i<mascara.length;i++){
				caracter = mascara.substr(i,1);
				if (caracter == "#"){
					retorno += valor.substr(contador,1);
					contador++;
				}else
					retorno += caracter;
				if(contador >= valor.length) break;
			}
		}else{
			contador = valor.length-1;
			for(i=mascara.length-1;i>=0;i--){
				if(contador < 0) break;
				caracter = mascara.substr(i,1);
				if (caracter == "#"){
					retorno = valor.substr(contador,1) + retorno;
					contador--;
				}else
					retorno = caracter + retorno;
			}
		}

		if (negativo && snegativo == "s")
			retorno = "-" + retorno;
		campo.value = retorno;
	},

	geraMascaraDecimal: function(tam, decimais){
		var retorno = ""; var contador = 0; var i = 0;
		decimais = parseInt(decimais);
		for (i=0;i<(tam-(decimais+1));i++){
			retorno = "#" + retorno;
			contador++;
			if (contador == 3){
				retorno = "." + retorno;
				contador=0;
			}
		}
		retorno = retorno + ",";
		for (i=0;i<decimais;i++) retorno += "#";
		return retorno;
	},

	geraMascaraNumerico: function(tam){
		var retorno = ""; var i = 0;
		for (i=0;i<tam;i++){
			retorno = "#" + retorno;
		}
		return retorno;
	},

	selecao: function(campo){
		if (this.IsIE)
			return document.selection.createRange().text;
		else
			return (campo.value).substr(campo.selectionStart, (campo.selectionEnd - campo.selectionStart));
	},

	formatValue: function(campo, num) {
		decimal = campo.getAttribute("casasdecimais");
		fator = "1"
		for (var i=0; i<decimal; i++){
			fator = fator + "0";
		}

		while(num.indexOf(".") != -1)
			num = num.replace(".","")

		num = num.replace(",","")
		num = num.toString().replace(/\$|\,/g,'');

		if(isNaN(num))
			num = "0";

		if ((num.length) <= decimal){
			cents = num;
			for (var i=num.length; i<=decimal; i++)
				if(cents.length < decimal) 
					cents = "0" + cents;			
			num = "0";
		}
		
		sign = (num == (num = Math.abs(num)));
//		num = Math.floor(num*fator+0.50000000001);
//		cents = num%fator;
//		num = Math.floor(num/fator).toString();
//		cents = cents.toString()

//		for (var i=0; i<=decimal; i++)
//			if(cents.length < decimal) 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));

		campo.value = (((sign)?'':'-') + num + ',' + cents);
	}

};

//***********************************************************************
