just = {}

just.util = new function() {
	this.getObj = function(tx) {	
		return document.getElementById(tx);
	};
	
	this.getVal = function(tx) {
		return document.getElementById(tx).value;
	};

	// from yahoo ui
	this.setStyle = function(el,property,val) {
		switch(property) {
		case 'opacity' :			
			if (IS_IE && typeof el.style.filter == 'string') { // in case not appended				
				el.style.filter = 'alpha(opacity=' + val * 100 + ')';
                     
				if (!el.currentStyle || !el.currentStyle.hasLayout) {
					el.style.zoom = 1; // when no layout or cant tell
				}
			} else {
				el.style.opacity = val;
				el.style['-moz-opacity'] = val;
				el.style['-khtml-opacity'] = val;
			}			
			break;
		default :
			el.style[property] = val;
		}	
	};

	// from yahoo ui
	this.getStyle = function(el,property) {	
		var value = null;
		var dv = document.defaultView;

		if (property == 'opacity' && el.filters) {// IE opacity
			value = 1;
			try {
				value = el.filters.item('DXImageTransform.Microsoft.Alpha').opacity / 100;
			} catch(e) {
				try {
					value = el.filters.item('alpha').opacity / 100;
				} catch(e) {}
			}			
		} else if (el.style[property]) {
			value = parseInt(el.style[property],10);
		} else if (el.currentStyle && el.currentStyle[property]) {
			value = parseInt(el.currentStyle[property],10);
		} else if ( dv && dv.getComputedStyle ) {  // convert camelCase to hyphen-case
			var converted = '';
			for(var i = 0, len = property.length;i < len; ++i) {
				if (property.charAt(i) == property.charAt(i).toUpperCase()) {
					converted = converted + '-' + property.charAt(i).toLowerCase();
				} else {
					converted = converted + property.charAt(i);
				}
			}
               
			if (dv.getComputedStyle(el, '') && dv.getComputedStyle(el, '').getPropertyValue(converted)) {
				value = dv.getComputedStyle(el, '').getPropertyValue(converted);
			}
		}
		return value;
	};

	
	this.findPosX = function(obj) {
		var curleft = 0;
		if (obj.offsetParent) {
			while (obj.offsetParent) {
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		} else if (obj.x)
			curleft += obj.x;
		return curleft;
	};

	this.findPosY = function(obj) {
		var curtop = 0;
		if (obj.offsetParent) {
			while (obj.offsetParent)	{
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		} else if (obj.y)
			curtop += obj.y;
		return curtop;
	};	
	
	this.addEvent = function(obj, type, func) { 
		if (obj.attachEvent) { 
			obj['_'+type+func] = func; 
			obj[type+func] = function() {obj['_'+type+func](window.event)} 
			obj.attachEvent('on'+type, obj[type+func] ); 
		} else 
    			obj.addEventListener(type, func, false); 
	}


	this.removeEvent = function(obj, type, func) { 
		if (obj.detachEvent) { 
			obj.detachEvent('on'+type, obj[type+func]); 
			obj[type+func] = null; 
		} else 
    		obj.removeEventListener(type, func, false ); 
	} 

	this.removeNL = function(txt) {
		newText = "";			
		for (i=0; i < txt.length; i++) {
			if (txt.charAt(i) != '\n' && txt.charAt(i) != '\r' && txt.charAt(i) != '\t') {
				newText += txt.charAt(i);
			} else {
				newText += " ";
			}
		}
		return newText;			
	}

	this.replaceNL = function(txt) {
		newText = "";			
		for (i=0; i < txt.length; i++) {
			if (txt.charAt(i) != '\n' && txt.charAt(i) != '\r' && txt.charAt(i) != '\t') {
				newText += txt.charAt(i);
			} else {
				if (txt.charAt(i-1) != '\n' && txt.charAt(i-1) != '\r' && txt.charAt(i-1) != '\t') {
					newText += "<br />";
				}
			}
		}
		return newText;			
	}

	this.pickRecentProgID = function(idList){
		var bFound = false;
		for(var i=0; i < idList.length && !bFound; i++){
			try{
				var oDoc = new ActiveXObject(idList[i]);
				o2Store = idList[i];
				bFound = true;		
			} catch (e){}
		}
		if (!bFound) {
			//throw "Could not retreive a valid progID of Class: " + idList[idList.length-1]+". (original exception: "+e+")";
			return null
		}
		idList = null;
		return o2Store;
	}

	this.createXMLhttpRequestObject = function() {
		try {
			if (window.XMLHttpRequest) {
				var req = new XMLHttpRequest();
				//req.overrideMimeType('text/xml');
				// some versions of Moz do not support the readyState property
				// and the onreadystate event so we patch it!
				if (req.readyState == null) {
					req.readyState = 1;
					req.addEventListener("load", function () {
						req.readyState = 4;
						if (typeof req.onreadystatechange == "function")
							req.onreadystatechange();
					}, false);
				}			
				return req;
			}
			if (window.ActiveXObject) {
				return new ActiveXObject(XMLHTTP_PROGID);
			}
		}
		catch (ex) {alert(ex.message);}
		throw new Error("Your browser does not support XmlHttp objects (DSI)");
	}

	this.createXMLTransformObject = function() {
		var xsltProcessor;
		if (IS_IE) xsltProcessor = new ActiveXObject(DOM_PROGID);		
		if (IS_MOZ) xsltProcessor = new XSLTProcessor();
		return xsltProcessor;
	}

	
};

just.easing = new function() {	
	this.easeNone = function(t, b, c, d) {
		return b+c*(t/=d); 
	};
	this.easeIn = function(t, b, c, d) {
   		return b+c*((t/=d)*t*t);
	};
	this.easeOut = function(t, b, c, d) {
   		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(tc + -3*ts + 3*t);
	};
	this.easeBoth = function(t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(-2*tc + 3*ts);
	};
	this.backIn = function(t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(-3.4005*tc*ts + 10.2*ts*ts + -6.2*tc + 0.4*ts);
	};
	this.backOut = function(t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(8.292*tc*ts + -21.88*ts*ts + 22.08*tc + -12.69*ts + 5.1975*t);
	};
	this.backBoth = function(t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(0.402*tc*ts + -2.1525*ts*ts + -3.2*tc + 8*ts + -2.05*t);
	};

};

var IS_MOZ;
if (document.implementation.createDocument && document.implementation.hasFeature) {IS_MOZ = true;} else {IS_MOZ = false;}
var IS_SAFARI = (navigator.userAgent && navigator.vendor && (navigator.userAgent.toLowerCase().indexOf("applewebkit") != -1 || navigator.vendor.indexOf("Apple") != -1));
var IS_IE = document.all && window.ActiveXObject && navigator.userAgent.toLowerCase().indexOf("msie") > -1  && navigator.userAgent.toLowerCase().indexOf("opera") == -1;
var DOM_PROGID = just.util.pickRecentProgID(["Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XMLDOM"]);
//var XMLHTTP_PROGID = just.util.pickRecentProgID(["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]);
var XMLHTTP_PROGID = just.util.pickRecentProgID(["MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]);
var THREADEDDOM_PROGID = just.util.pickRecentProgID(["Msxml2.FreeThreadedDOMDocument.5.0", "MSXML2.FreeThreadedDOMDocument.4.0", "MSXML2.FreeThreadedDOMDocument.3.0"]);
var XSLTEMPLATE_PROGID = just.util.pickRecentProgID(["Msxml2.XSLTemplate.5.0", "Msxml2.XSLTemplate.4.0", "MSXML2.XSLTemplate.3.0"]);

// Create the loadXML method and xml getter for Mozilla - EXECUTE
if (window.DOMParser &&
	window.XMLSerializer &&
	window.Node && Node.prototype && Node.prototype.__defineGetter__) {
		Document.prototype.loadXML = function (s) {
		// parse the string to a new doc	
		var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
		// remove all initial children
		while (this.hasChildNodes())
		this.removeChild(this.lastChild);
		// insert and import nodes
		for (var i = 0; i < doc2.childNodes.length; i++) {
			this.appendChild(this.importNode(doc2.childNodes[i], true));
		}
	};
	Document.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});
}

/* Formata o número com pontos(.) entre as centenas e a vírgula como separador decimal. */
function formatNum(num,cd) {	
	var str = "" + Math.round (num * Math.pow(10,cd));				
	while (str.length <= cd) {
		str = "0" + str;
	}
	var decpoint = str.length - cd;
	var rounded = str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
	return rounded;	
}
