/*
--------------------------------------------------------------------------------
General JavaScript functions and prototypes
--------------------------------------------------------------------------------
*/

String.prototype.ltrim  =function() {return this.replace(/^\s+/,'');}
String.prototype.rtrim  =function() {return this.replace(/\s+$/,'');}
String.prototype.trim   =function() {return this.replace(/^\s+/,'').replace(/\s+$/,'');}
String.prototype.reverse=function() {var s='';var i=this.length;while(i-->0) s+=this.substring(i,i+1);return s;}
String.prototype.toInt  =function() {var a=[];for(var i=0;i<this.length;i++) a[i]=this.charCodeAt(i);return a;}
String.prototype.aryRep =function(a){var t=this;for(var i=0; i<a.length; i++){var n=new RegExp(a[i].r, "gi");t=t.replace(n, a[i].w);}return t;}
//String.prototype.isEmail=function() {return /^[a-zA-Z0-9_\.]\w*@\w*\.\w{2,4}$/gi.test(this)}
String.prototype.isEmail=function() {return /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/gi.test(this)}
Array.prototype.intArrayToString=function(){for(var a='', i=0;i<this.length;i++) try{a+=String.fromCharCode(this[i]);}catch(err){}return a;}
Array.prototype.compareArrays   =function(arr){if (this.length!=arr.length) return false;for(var i=0;i<arr.length;i++){if (this[i].compareArrays){if (!this[i].compareArrays(arr[i])) return false;else continue;}if (this[i]!=arr[i]) return false;}return true;}
Array.prototype.map             =function(fnc){var a=new Array(this.length);for(var i=0;i<this.length;i++) a[i]=fnc(this[i]);return a;}
Array.prototype.foldr           =function(fnc,start){var a=start;for(var i=this.length-1;i>=0;i--) a=fnc(this[i],a);return a;}
Array.prototype.foldl           =function(fnc,start){var a=start;for(var i=0;i<this.length;i++) a=fnc(this[i],a);return a;}
Array.prototype.exists          =function(x){for(var i=0;i<this.length;i++) if (this[i] == x) return true;return false;}
Array.prototype.filter          =function(fnc){var a=[];for(var i=0;i<this.length;i++) if (fnc(this[i])) a.push(this[i]);return a;}
Array.prototype.random          =function(){return this[Math.floor((Math.random()*this.length))];}
Array.prototype.copy            =function(){var n=[];for(var i=0;i<this.length;i++)n[i]=this[i];return n;}
Array.prototype.find            =function(e){for(var i=0;i<this.length;i++)if(this[i]==e)return i;return -1;}
if(Array.prototype.push == null){
	Array.prototype.push = function(){for(var i=0;i<arguments.length;i++){this[this.length]=arguments[i];};return this.length;};
};

// Make FireFox act like IE
if(!document.all){
	document.readyState='complete';
}

function gid(e)            {var ret;try{ret = document.getElementById(e);}catch(err){try{ret = document.all[e];}catch(err2){ret = document.layers[e];}}return ret==null?ret:addPrototypes(ret);}
function getXMLHttpObj()   {var A=null;try{A=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{A=new ActiveXObject('Microsoft.XMLHTTP');}catch(oc){A=null;}}if (!A && typeof XMLHttpRequest!='undefined'){A=new XMLHttpRequest();}return A;}
function mouseCoords(e)    {if(uD(e))e=window.event; if (e.pageX || e.pageY) return {x:e.pageX, y:e.pageY}; return {x:e.clientX+document.body.scrollLeft-document.body.clientLeft, y:e.clientY+document.body.scrollTop-document.body.clientTop};}
function getPos(e)         {var left=0;var top=0; while (e.offsetParent){ left+=e.offsetLeft-e.scrollLeft;top+=e.offsetTop-e.scrollTop;e=e.offsetParent;}return {x:left, y:top};}
function keyCode(e)        {if(e.keyCode) return e.keyCode; return e.which;}
function uD(e)             {return typeof(e)=='undefined';}
function getViewSize()     {if (window.innerHeight!=window.undefined) return {h:window.innerHeight, w:window.innerWidth};if (document.compatMode=='CSS1Compat') return {h:document.documentElement.clientHeight, w:document.documentElement.clientWidth};if (document.body) return {h:document.body.clientHeight, w:document.body.clientWidth};return window.undefined;}
function addEvent(o,e,f,c) {if(c)eventCache.flush(o, e); if(o.attachEvent){o.attachEvent("on"+e,f);} else if(o.addEventListener){o.addEventListener(e, f, true);} else if(typeof o[e] == "function"){var d = o[e];o[e] = function(e){ d(e); f(e); };} else {o[e] = f;};eventCache.add(o, e, f, true);};
function removeEvent(o,e,f){if(o.detachEvent){o.detachEvent("on"+e,f);} else if(o.removeEventListener){o.removeEventListener(e, f, true);} else if(typeof o[e]=="function"){o[e]=null;}}
function sFloat(e,f)       {e[!uD(e.cssFloat)?'cssFloat':'styleFloat']=f;}
function cE(e)             {return addPrototypes(document.createElement(e));}
function addPrototypes(e)  {e.aC=function(e){this.appendChild(e);};return e;}

var eventCache = function(){
	var listEvents = [];

	return {
		listEvents:listEvents,
		add:function(node, sEventName, fHandler, bCapture){listEvents.push(arguments);},
		flush:function(o,e){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(!uD(o)&&!uD(e)&&item[0]!=o||item[1]!=e) continue;

				if(item[0].removeEventListener){item[0].removeEventListener(item[1], item[2], item[3]);};
				if(item[1].substring(0, 2) != "on") item[1] = "on" + item[1];
				if(item[0].detachEvent) item[0].detachEvent(item[1], item[2]);

				item[0][item[1]] = null;
			};
		}
	};
}();

// Event Cleanup - helps to prevents memory leaks
var clearElementProps = ['data', 'onload', 'onmousemove', 'onmouseover', 'onmouseout', 'onmousedown', 'onmouseup', 'ondblclick', 'onclick', 'onselectstart', 'oncontextmenu'];

addEvent(window, "unload", function(){
	var el;
	for(var d = document.all.length;d--;){
		el = document.all[d];
		for(var c = clearElementProps.length;c--;) el[clearElementProps[c]] = null;
	}
});

addEvent(window, "unload", function(){ eventCache.flush(); });