var NlFormat = {
	cpf : function(_componet) {
		var value = _componet.value;
		// Remove tudo o que não é dígito
		value = String(value).replace(/\D/g, "").replace(/^0+/, "");
		if (value.length > 0) {
			// Completa com '0' a esquerda
			value = NlUtil.lpad(value, 11, '0');
			value = value.substring(0, 11);
			reCpf = /(\d{3})(\d{3})(\d{3})(\d{2})$/;
			value = value.replace(reCpf, "$1.$2.$3-$4");
		}
		_componet.value = value;
	},

	cnpj : function(_componet) {
		var value = _componet.value;
		// Remove tudo o que não é dígito
		value = String(value).replace(/\D/g, "").replace(/^0+/, "");
		if (value.length > 0) {
			// Completa com '0' a esquerda
			value = NlUtil.lpad(value, 14, '0');
			reCnpj = /(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/;
			value = value.replace(reCnpj, "$1.$2.$3/$4-$5");
		}
		_componet.value = value;
	},

	cep : function(_componet) {
		var value = _componet.value;
		// Remove tudo o que não é dígito
		value = String(value).replace(/\D/g, "").replace(/^0+/, "");
		if (value.length > 0) {
			// Completa com '0' a esquerda
			value = NlUtil.lpad(value, 8, '0');
			value = value.replace(/^(\d{5})(\d)/, "$1-$2");
		}
		_componet.value = value;
	},

	date : function(_componet) {
		var value = _componet.value;
		value = new String(value.replace(/[^\d\/,\.\s-;:]/g, ""));

		// Data corrida EX 12082008 ou 120808
		var expCorrida;
		var expSeparador;
		if (value.length <= 2) {
			expCorrida = /^(\d+)$/;
			expSeparador = /^(\d+)[\/,\.\s-;:]?$/;
		} else {
			expCorrida = /^(\d{2})(\d{2})?(\d{2,4})?$/;
			expSeparador = /^(\d{1,2})[\/,\.\s-;:](\d{1,2})?[\/,\.\s-;:]?(\d{1,4})?$/;
		}

		// Data separador EX 12/8/2008 ou 12,8,8 ou 12.2.2008 ou ainda 12,2/08

		var _arrD;

		if ((_arrD = expCorrida.exec(value)) != null
				|| (_arrD = expSeparador.exec(value)) != null) {
			var dt = new Date();
			var _y = _arrD[3] ? NlFormat._getYear(_arrD[3]) : dt.getFullYear();
			var _m = (_arrD[2] ? _arrD[2] - 1 : dt.getMonth()) + 1;
			var _d = _arrD[1];
			var _dia = ("00" + _d).slice(-2);
			var _mes = ("00" + _m).slice(-2);
			var _ano = ("0000" + _y).slice(-4);

			_componet.value = new String(_dia + "/" + _mes + "/" + _ano);
		}
	},

	monthYear : function(_component) {
		var value = _component.value;
		value = new String(value.replace(/[^\d\/,\.\s-;:]/g, ""));

		// Verifica o tamanho
		if (value.length != 0) {
			if (value.length <= 2) {
				expCorrida = /^(\d+)$/;
				expSeparador = /^(\d+)[\/,\.\s-;:]?$/;
			} else {
				expCorrida = /^(\d{2})(\d{2,4})?$/;
				expSeparador = /^(\d{1,2})[\/,\.\s-;:]?(\d{1,4})?$/;
			}

			var _res;
			if ((_res = expCorrida.exec(value)) != null
					|| (_res = expSeparador.exec(value)) != null) {
				var dt = new Date();
				var _m;
				var _y;
				// Verifica se o mês é válido
				var numMonth = new Number(_res[1]);
				// Atribui um valor válido caso não seja
				_res[1] = (numMonth < 1) ? "01" : (numMonth > 12) ? "12"
						: numMonth.toString();

				// Busca mês e ano
				if (_res.length == 2) {
					_m = ("00" + _res[1]).slice(-2);
					_y = dt.getFullYear();
				} else {
					_m = ("00" + _res[1]).slice(-2);
					_y = ("0000" + NlFormat._getYear(_res[2])).slice(-4);
				}

				// Seta no componente
				_component.value = _m + "/" + _y;
			} else {
				_component.value = "";
			}
		} else {
			_component.value = "";
		}
	},

	dateTime : function(_componet, _segundos) {
		var value = _componet.value;
		value = new String(value.replace(/[^\d\/,\.\s-;:]/g, ""));
		// Data hora corrida com segundos EX 12082008133036 ou 120808133042

		var expCorrida;
		var expSeparador;
		if (value.length <= 2) {
			expCorrida = /^(\d+)$/;
			expSeparador = /^(\d+)[\/,\.\s-;:]?$/;
		} else {
			if (value.length <= 8) {
				expCorrida = /^(\d{2})(\d{2})?(\d{2,4})?$/;
			} else {
				expCorrida = /^(\d{2})(\d{2})(\d{4})(\d{2})?(\d{2})?(\d{2})?$/;
			}
			expSeparador = /^(\d{1,2})[\/,\.\s-;:](\d{1,2})?[\/,\.\s-;:]?(\d{1,4})?[\/,\.\s-;:]?(\d{1,2})?[\/,\.\s-;:]?(\d{1,2})?[\/,\.\s-;:]?(\d{1,2})?$/;
		}

		var _arrD;

		if ((_arrD = expCorrida.exec(value)) != null
				|| (_arrD = expSeparador.exec(value)) != null) {
			var dt = new Date();

			var _y = _arrD[3] ? NlFormat._getYear(_arrD[3]) : dt.getFullYear();
			var _m = (_arrD[2] ? _arrD[2] - 1 : dt.getMonth()) + 1;
			var _d = _arrD[1];

			var _h = _arrD[4] ? _arrD[4] : 0;
			var _n = _arrD[5] ? _arrD[5] : 0;
			var _s = _arrD[6] ? _arrD[6] : 0;

			var _dia = ("00" + _d).slice(-2);
			var _mes = ("00" + _m).slice(-2);
			var _ano = ("0000" + _y).slice(-4);
			var _hora = ("00" + _h).slice(-2);
			var _min = ("00" + _n).slice(-2);
			var _sec = ("00" + _s).slice(-2);

			if (_segundos) {
				_componet.value = new String(_dia + "/" + _mes + "/" + _ano
						+ " " + _hora + ":" + _min + ":" + _sec);
			} else {
				_componet.value = new String(_dia + "/" + _mes + "/" + _ano
						+ " " + _hora + ":" + _min);
			}
		}
	},

	time : function(_componet, _segundos) {
		var value = _componet.value;
		value = value.replace(/[^\d\/,\.\s-;:]/g, "");

		// Hora corrida EX 153027
		var expCorrida = /^(\d{1,2})(\d{2})?(\d{2})?$/;

		// Hora separador EX 13:30:14 ou 13,30,14 ou 13.30,14 ou 13,30:17
		var expSeparador = /^(\d{1,2})[\/,\.\s-;:]?(\d{1,2})?[\/,\.\s-;:]?(\d{1,2})?$/;

		var _arrD;

		if ((_arrD = expCorrida.exec(value)) != null
				|| (_arrD = expSeparador.exec(value)) != null) {
			var _hora = ("00" + _arrD[1]).slice(-2);
			var _min = _arrD[2] ? ("00" + _arrD[2]).slice(-2) : "00";
			var _sec = _arrD[3] ? ("00" + _arrD[3]).slice(-2) : "00";
			if (_segundos) {
				_componet.value = new String(_hora + ":" + _min + ":" + _sec);
			} else {
				_componet.value = new String(_hora + ":" + _min);
			}
		}

	},

	_getYear : function(_year) {
		var _y = new Number(_year);

		if (_y < 100) {
			var _hoje = new Date();
			var _yh = new Number((new String(_hoje.getFullYear())).slice(-2));
			_yh = _yh + 50;
			if (_y >= _yh) {
				_y += 1900;
			} else {
				_y += 2000;
			}
		} else if (_y > 3000) {
			_y = 3000;
		}

		return _y;
	},

	number : function(_campo, _precision, _scale, _group, _decSep, _groupSep,
			_autoFormat) {
		var valor = "";
		if (_campo.tagName == "INPUT") {
			valor = new String(_campo.value);
		} else {
			valor = new String(_campo.innerHTML);
		}

		if (valor == "") {
			return;
		}
		var negativo = valor.substr(0, 1) == "-";
		var posDec = valor.lastIndexOf(_decSep);
		if (posDec == -1) {
			posDec = valor.length;
		}
		var decimal = valor.slice(posDec + 1);
		var inteiro = valor.substring(0, posDec);
		decimal = decimal.replace(/\D/g, "");
		inteiro = inteiro.replace(/\D/g, "").replace(/^0+(\d)/g, "$1");

		if (inteiro == "" && decimal == "") {
			retorno = "";
		} else {
			if (_autoFormat) {
				// Retirada esta consistencia para que nao corte o valor
				// informado, deixando para o validator dizer que esta fora do
				// tamanho.
				// if (inteiro.length > (_precision - _scale)) {
				// inteiro = inteiro.slice(-(_precision - _scale));
				// }
				decimal = NlUtil.rpad(decimal, _scale, '0');
				decimal = decimal.substring(0, _scale);
			}
			if (_group) {
				var newInt = new String();
				var _a;
				for (_a = inteiro.length; _a > 3; _a -= 3) {
					newInt = _groupSep + inteiro.substr(_a - 3, 3) + newInt;
				}
				inteiro = inteiro.substring(0, _a) + newInt;
			}
			if (inteiro == "") {
				inteiro = "0";
			}
			retorno = (negativo ? "-" : "") + inteiro;
			if (!_autoFormat) {
				if (decimal.length > 0) {
					retorno += _decSep + decimal;
				}
			} else if (_scale > 0) {
				retorno += _decSep + decimal;
			} else {
				retorno += "";
			}
		}
		if (_campo.tagName == "INPUT") {
			_campo.value = retorno;
		} else {
			_campo.innerHTML = retorno;
		}
	}
}

