var mascaras = Mascaras = {
	AZ: /[A-Z]/i,
	Acentos: /[À-ÿ]/i,
	Num: /[0-9]/,
	login: "abcdefghijklmnopqrstuvxwyz0123456789-_",
	carregar: function(parte){
		parte = parte||document;
		var Tags = ['input','textarea'];
		for(var z=0;z<Tags.length;z++){
			Inputs=parte.getElementsByTagName(Tags[z]);
			for(var i=0;i<Inputs.length;i++)
				if(('button,image,submit,reset').indexOf(Inputs[i].type.toLowerCase())==-1)
					this.aplicar(Inputs[i]);
		}
	},
	aplicar: function(campo){
		var tipo = campo.getAttribute('tipo');
		var sel = (campo.type == "select-one");
		if (!tipo || sel){
			var keydownpress = function(e){ return Mascaras.backspace(e?e:event);};
			if (sel && isIE){
				Eventos.adiciona(campo, 'keydown', keydownpress);
			}else if (sel){
				Eventos.adiciona(campo, 'keypress', keydownpress);
			}
			keydownpress = null;
			return;
		}
		var orientacao = campo.getAttribute('orientacao');
		var mascara = campo.getAttribute('mascara');
		var negativo = campo.getAttribute('negativo');
		if (tipo.toLowerCase() == "ip"){
			var keypress = function(e){ return IP.keyPress(e?e:event, campo) }
			Eventos.adiciona(campo, 'keypress', keypress);
			keypress = null;
			return;
		}else if (tipo.toLowerCase() == "decimal"){
			orientacao = "esquerda";
			var casasdecimais = campo.getAttribute('casasdecimais');
			var tamanho = campo.getAttribute('maxLength');
			if (!tamanho || tamanho > 50)
				var tamanho = 20;
			if (!casasdecimais)
				casasdecimais = 2;
			campo.setAttribute("mascara", this.geraMascaraDecimal(tamanho, casasdecimais));
			campo.setAttribute("tipo", "numerico");
			campo.setAttribute("orientacao", orientacao);
		}else if (tipo.toLowerCase() == "inteiromilhar"){
			var tamanho = campo.getAttribute('maxLength');
			if (!tamanho || tamanho > 50)
				var tamanho = 20;
			var masc = "";
			var cont = 0;
			for (var i=0; i< tamanho; i++){
				masc = "#" + masc;
				cont++;
				if (cont == 3){
					masc = "." + masc;
					cont = 0;
				}
			}
			campo.setAttribute("orientacao", "esquerda");
			campo.setAttribute("mascara", masc);
			campo.setAttribute("tipo", "numerico");
		}else if (tipo.toLowerCase() == "temperatura"){
			var keypress = function(e){ return Temperatura.onKeyPress(e?e:event); };
			Eventos.adiciona(campo, 'keypress', keypress);
			var change = function(e){ Temperatura.onchange(campo);};
			Eventos.adicionaInicio(campo, 'change', change);
			keypress = change = null;
			return;
		}else if (tipo.toLowerCase() == "placa"){
			campo.setAttribute("mascara", "###-####");
			var blur = function(e){ ValPlaca.valida(campo);};
			Eventos.adicionaInicio(campo, 'blur', blur);
			blur = null;
			tipo = "caracter";
		}else if (tipo.toLowerCase() == "percentual"){
			var casasdecimais = campo.getAttribute('casasdecimais');
			if (!casasdecimais)
				casasdecimais = 0;
			campo.style.textAlign = "right";
			var keypress = function(e){ return Percentual.onKeyPress(e?e:event, casasdecimais); };
			Eventos.adiciona(campo, 'keypress', keypress);
			var change = function(e){ Percentual.onblur(campo, casasdecimais); };
			Eventos.adicionaInicio(campo, 'change', change);
			keypress = change = null;
			return;
		}else if (tipo.toLowerCase() == "moeda"){
			var casasdecimais = campo.getAttribute('casasdecimais');
			if (!casasdecimais)
				casasdecimais = 0;
			campo.style.textAlign = "right";
			var keypress = function(e){ return Moeda.onKeyPress(e?e:event, casasdecimais); };
			Eventos.adiciona(campo, 'keypress', keypress);
			var change = function(e){ Moeda.onblur(campo, casasdecimais); };
			Eventos.adicionaInicio(campo, 'change', change);
			keypress = change = null;
			return;
		}
		if (orientacao && orientacao.toLowerCase() == "esquerda") campo.style.textAlign = "right";
		if (mascara) campo.setAttribute("maxLength", mascara.length + (negativo ? 1 : 0));
		if (tipo){
			var keypress = function(e){ return Mascaras.onkeypress(e?e:event, campo);};
			Eventos.adiciona(campo, 'keypress', keypress);
			var keyup = function(e){ return Mascaras.onkeyup(e?e:event, campo);};
			Eventos.adiciona(campo, 'keyup', keyup);
			keypress = keyup = null;
		}
		campo.setAttribute("snegativo", ((campo.value).substr(0,1) == "-" ? "s" : "n"));
	},
	onkeypress: function(e, campo){
		var KeyCode = isIE ? event.keyCode : e.which;
		if (campo.getAttribute('readonly'))
			return false;
		var maxlength = campo.getAttribute('maxlength');
		if (KeyCode == 0) return true;
		Char = String.fromCharCode(KeyCode);
		var valor = campo.value;
		var mascara = campo.getAttribute('mascara');
		if (KeyCode != 8){
			var tipo = campo.getAttribute('tipo').toLowerCase();
			var negativo = 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" && 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 == "login" && this.login.indexOf(Char) == -1) return false;
			}
		}
		if (this.selecao(campo).length > 0 && KeyCode != 0)
			return true;
		if (mascara){
			this.aplicarMascara(campo, valor);
			return false;
		}
		return true;
	},
	onkeyup: function(e, campo){
		KeyCode = isIE ? event.keyCode : e.which;
		if (campo.getAttribute('readonly'))
			return;
		if(KeyCode == 13) return;
		if (KeyCode != 9 && KeyCode != 16 && KeyCode != 109){
			valor = campo.value;
			if (KeyCode == 8 && !isIE) {
				valor = valor.substr(0,valor.length-1);
				this.aplicarMascara(campo, valor);
				return false;
			}
			this.aplicarMascara(campo, valor);
		}
	},
	aplicarMascara: function(campo, valor){
		var mascara = campo.getAttribute('mascara');
		if (!mascara) return;
		var negativo = campo.getAttribute('negativo');
		var snegativo = campo.getAttribute('snegativo');
		if (negativo && valor.substr(0,1) == "-") 
			valor = valor.substr(1,valor.length-1);
		var 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, "");
		}
		var retorno = "";
		if (orientacao != "esquerda"){
			var 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;
	},
	selecao: function(campo){
		if (isIE)
			return document.selection.createRange().text;
		else
			return (campo.value).substr(campo.selectionStart, (campo.selectionEnd - campo.selectionStart));
	},
	formataValor: function (valor, decimais){
		valor = valor.split('.');
		if (valor.length == 1) valor[1] = "";
		for(var i=valor[1].length;i<decimais;i++)
			valor[1] += "0"; 
		valor[1] = valor[1].substr(0,2);
		return (valor[0] + "." + valor[1]);
	},
	backspace: function(e, campo){
		var KeyCode = isIE ? event.keyCode : e.which;
		if (isIE && event.keyCode == 8)
			event.keyCode = 0;
		return (KeyCode != 8);
	}
};