var Utils = new Object();

Utils.includeJS = function(filepaths)
{
	for(var i=0; i<filepaths.length; i++)
	{
		document.write('<script type="text/javascript" src="'+filepaths[i]+'"></script>');
	}
}

Utils.includeCSS = function(filepaths)
{
	for(var i=0; i<filepaths.length; i++)
	{
		document.write('<link href="'+filepaths[i]+'" rel="stylesheet" type="text/css" />');
	}
}

Utils.getElement = function(i) { return document.getElementById(i); }
function $gE(i){ return Utils.getElement(i); }

Utils.getElementsByTagName = function(tagName,container){
	return (container&&container.getElementsByTagName)?container.getElementsByTagName(tagName):document.getElementsByTagName(tagName);
}
function $T(t,c){ return (c)?Utils.getElementsByTagName(t,c):Utils.getElementsByTagName(t); }

Utils.debug = function(val)
{
	this.getElement('debug').innerHTML += val +"<br/>";
}

Utils.toggle = function(id)
{
	this.getElement(id).style.display = (this.getElement(id).style.display == '') ? 'none' : '';
}

Utils.createElement = function(e, obj)
{
	var a = document.createElement(e);
	this.setAttributes(a,obj);
	/*
	for(prop in obj)
	{
		a[prop] = obj[prop];
	}
	*/
	return a;
}
function Ł(e,o,s){
	var elm=Utils.createElement(e,o);
	if(s) Utils.setStyle(elm,s);
	return elm;
}

Utils.appendChild = function()
{
	if(this.appendChild.arguments.length > 1)
	{
		var a = this.appendChild.arguments[0];
		for(i=1; i<this.appendChild.arguments.length; i++){
			if(arguments[i]){
				a.appendChild(this.appendChild.arguments[i]);
			}
		}
		return a;
	}
	else
	{
		return null;
	}
}

Utils.removeChildren = function(node)
{
	if(node == null)
	{
		return;
	}

	while(node.hasChildNodes())
	{
		node.removeChild(node.firstChild);
	}
}

Utils.addListener = function(obj, eventName, listener)
{
	if (obj.attachEvent)
	{
		obj.attachEvent("on"+eventName, listener);
	}
	else if(obj.addEventListener)
	{
		obj.addEventListener(eventName, listener, false);
	}
	else
	{
		return false;
	}
	return true;
}

Utils.removeListener = function(obj, eventName, listener)
{
	if(obj.detachEvent)
	{
		obj.detachEvent("on"+eventName, listener);
	}
	else if(obj.removeEventListener)
	{
		obj.removeEventListener(eventName, listener, false);
	}
	else
	{
		return false;
	}

	return true;
}

Utils.changeOpac = function(e,opacity)
{
	var object = (typeof(e)=='string')?Utils.getElement(id).style:e.style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

Utils.setStyle = function(e,styleDef){
	if(typeof(e)=='string') e=$gE(e);
	for(var s in styleDef){
		if(s=='float'){
			if(Utils.isIE) e.style.styleFloat=styleDef[s];
			else e.style.cssFloat=styleDef[s];
		} else e.style[s]=styleDef[s];
	}
}

Utils.position = function(e,width,height,left,top){
	if(typeof(e)=='string') e=$gE(e);
	Utils.setStyle(e,
		{
			'width'  : (typeof(width)=='string')?width:width+'px',
			'height' : (typeof(height)=='string')?height:height+'px',
			'left'   : (typeof(left)=='string')?left:left+'px',
			'top'    : (typeof(top)=='string')?top:top+'px'
		}
	);
}

Utils.getPosition = function(obj){
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else {
		if (obj.x) curleft += obj.x;
		if (obj.y) curtop += obj.y;
	}
	return {'left':curleft,'top':curtop};
}

Utils.setAttributes=function(e,attributeDef){
	for(var a in attributeDef) e.setAttribute(a,attributeDef[a]);
	return e;
}

Utils.getEventDetails = function(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ&&(targ.nodeType == 3)) targ = targ.parentNode;

	var code;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var character = String.fromCharCode(code);

	var button, rightclick;
	if (e.which) rightclick = (e.which == 3);
	else if (e.button) rightclick = (e.button == 2);
	button = (rightclick) ? 'right' : 'left';

	var posx = 0;
	var posy = 0;
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}

	return {
		'target': targ,
		'keyCode': code,
		'button': button,
		'mouseX': posx,
		'mouseY': posy
	}
}

Utils.strpos=function( haystack, needle, offset){
    var i = haystack.indexOf( needle, offset ); // returns -1
    return i >= 0 ? i : false;
}

Utils.addInit=function(func) {
    var oldQueue = window.onload? window.onload: function() {};
    window.onload = function() {
        oldQueue();
        eval(func+'()');
    }
}

Utils.isIE = (navigator.userAgent.toLowerCase().indexOf( 'msie', 0 )>0);

Utils.switchDisplay=function(e){
	if(typeof(e)=='string') e=$gE(e);
	this.setStyle(e,{'display':(e.style.display=='none')?'block':'none'});
}

Utils.showHideLayers=function(){
	var i,p,v,obj,args=Utils.showHideLayers.arguments;
	for (i=0; i<(args.length-2); i+=3)
	with (document)
		if (getElementById && ((obj=getElementById(args[i]))!=null)) {
			v=args[i+2];
			if (obj.style) {
				obj=obj.style;
				v=(v=='show')?'visible':(v=='hide')?'hidden':v;
			}
		obj.visibility=v;
	}
}

Utils.RGBtoHex = function(R, G, B) {
	if ((R == null) && (G == null) && (B == null)) return false;
	else if (R.indexOf('#') == 0) return R;
	else if (R.indexOf('rgb') == 0) {
		var components = R.substring(0, R.lastIndexOf(')')).substring(4).split(',');
		if (components.length < 3) return false;
		else return Utils.toHex(components[0]) + Utils.toHex(components[1]) + Utils.toHex(components[2]);
	} else return Utils.toHex(R) + Utils.toHex(G) + Utils.toHex(B);
}
Utils.toHex = function(N) {
	if (N == null) return "00";
	N = parseInt(N); if (N == 0 || isNaN(N)) return "00";
	N = Math.max(0, N); N = Math.min(N, 255); N = Math.round(N);
	return "0123456789abcdef".charAt((N - N % 16) / 16)
      + "0123456789abcdef".charAt(N % 16);
}



// If Push and pop is not implemented by the browser
if (!Array.prototype.push) {
	Array.prototype.push = function array_push() {
		for(var i=0;i<arguments.length;i++)
			this[this.length]=arguments[i];
		return this.length;
	}
};
if (!Array.prototype.pop) {
	Array.prototype.pop = function array_pop() {
		lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
		return lastElement;
	}
};