/*
Funções Utilizadas para Formatação de Input
*/

	//=============================================================
	//
	// Generic Functions
	//
	//=============================================================
	
	/*
	***********************************************************
	* Função utilizada para procura de objetos na página      *
	* @params:												  *
	*	n = ID do objeto a ser localizado					  *
	*	d = quadro do objeto a ser localizado				  *
	***********************************************************
	*/
	function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
	}

	//=============================================================
	//
	// Input Format Functions
	//
	//=============================================================

	/*
	**************************************
	* Event Listener Functions v1.4      *
	**************************************
	*/
	addEvent = function(o, e, f, s){
		var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
		r[r.length] = [f, s || o], o[e] = function(e){
			try{
				(e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
				e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
				e.target || (e.target = e.srcElement || null);
				e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
			}catch(f){}
			for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
			return e = null, !!d;
		}
	};

	removeEvent = function(o, e, f, s){
		for(var i = (e = o["_on" + e] || []).length; i;)
			if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
				return delete e[i];
		return false;
	};
	
	/*
	***********************************************************
	* Função utilizada para máscara de dinheiro para Inputs,  *
	* ignora botão enter para submit						  *
	* @params:												  *
	*	o   = ID do objeto a ser mascarado					  *
	*	n   = número de casas decimais 						  *
	*	len = tamanho do campo 								  *
	*	dig = string representando o separador de milhar  	  *
	*	dec = string representando o separador decimal 		  *
	*														  *
	* - REQUER: Event Listener Functions v1.4				  *
	*											Thiago Pitta  *
	***********************************************************
	*/
	function maskFormatCurrency(o, n, len, dig, dec){
		o.c = !isNaN(n) ? Math.abs(n) : 2;
		o.dec = dec || ",", o.dig = dig || ".";
		addEvent(o, "keypress", function(e){
			if ((this.value.length<=len) && (e.keyCode!=13))
			{
				if(e.key > 47 && e.key < 58){
					var o, s = ((o = this).value.replace(/^0+/g, "") + String.fromCharCode(e.key)).replace(/\D/g, ""), l, n;
					(l = s.length) <= (n = o.c) && (s = new Array(n - l + 2).join("0") + s);
					for(var i = (l = (s = s.split("")).length) - n; (i -= 3) > 0; s[i - 1] += o.dig);
					n && n < l && (s[l - ++n] += o.dec);
					o.value = s.join("");
				}
				e.key > 30 && e.preventDefault();
			}
			else	e.keyCode = 0;
		});
	}
	
	/****************************************
	* Formatar valores monetários
	*****************************************/
	fmtMoney = function(n, c, d, t){ //v1.0
		var m = (c = Math.abs(c) + 1 ? c : 2, d = d || ",", t = t || ".",
			/(\d+)(?:(\.\d+)|)/.exec(n + "")), x = m[1].length > 3 ? m[1].length % 3 : 0;
		return (x ? m[1].substr(0, x) + t : "") + m[1].substr(x).replace(/(\d{3})(?=\d)/g,
			"$1" + t) + (c ? d + (+m[2] || 0).toFixed(c).substr(2) : "");
	};
	
	/****************************************
	* Transforma valor monetário para formato
	* padrão javascript
	*****************************************/
	moneyToJSFormat = function(value){
		ReturnValue = value.replace('.','');
		while(ReturnValue.indexOf('.')>0)
		{
			ReturnValue = ReturnValue.replace('.','');
		}
		ReturnValue = ReturnValue.replace(',','.');
		return ReturnValue;
	};	
