// JavaScript Document

//determine the root window object
_rootWindow = window;
while (_rootWindow.opener != null) _rootWindow = _rootWindow.opener;

function setupAJAXRequest() { //setup an AJAX request and return the xmlHTTP object
	var xmlHttp;
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	
	return xmlHttp;
}

function getParameterFromURL(key) {
	var url = window.location.toString();
	var queryString;
	var sets;
	var set;
	var i;
	var retVal = false;
	
	//if a second argument, a URL, has been passed in then use it as the URL
	if (arguments.length > 1) url = arguments[1];
	
	if (url.split("?").length > 1) {
		queryString = url.split("?")[1];
		sets = queryString.split("&");
		for (i = 0; i < sets.length; i++) {
			set = sets[i].split("=");
			if (set[0] == key) {
				if (set.length > 1) {
					retVal = set[1];
				} else {
					retVal = true;
				}
				i = sets.length;
			}
		}
	}
	
	return retVal;
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
		if (oldonload) {
			oldonload();
		}
			func();
		}
	}
}

function addResizeEvent(func) {
	var oldonresize = window.onresize;
	if (typeof window.onresize != 'function') {
		window.onresize = func;
	} else {
		window.onresize = function() {
		if (oldonresize) {
			oldonresize();
		}
			func();
		}
	}
}

function addScrollEvent(func) {
	var oldonscroll = window.onscroll;
	if (typeof window.onscroll != 'function') {
		window.onscroll = func;
	} else {
		window.onscroll = function() {
		if (oldonscroll) {
			oldonscroll();
		}
			func();
		}
	}
}

//////////////////////////////////
//		STRING PROTOTYPE		//
//////////////////////////////////
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

String.prototype.toHex = function() {
	var hexString = "";
	var hexChar;
	for (i = 0; i < this.length; i++) {
		hexChar = this.charCodeAt(i).toString(16);
		if (hexChar.length == 1) hexChar = "0" + hexChar;
		hexString += hexChar;
	}
	return hexString;	
}

String.prototype.hexToString = function() {
	var string = "";
	var hexCode;
	var value;
	var i;
	
	for (i = 0; i < this.length - 1; i += 2) {
		hexCode = this.substr(i, 1) + this.substr(i + 1, 1);
		value = parseInt(hexCode, 16);
		string += String.fromCharCode(value);
	}
	return string;	
}

String.prototype.addslashes = function() {
    return (this + '').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");
}

String.prototype.stripslashes = function() {
    return (this + '').replace(/\0/g, '0').replace(/\\([\\'"])/g, '$1');
}




//////////////////////////////
//		ARRAY PROTOTYPE		//
//////////////////////////////
Array.prototype.inArray = function(value) {
	var i;
	var found = false;
	for (i = 0; i < this.length; i++) {
		if (this[i] == value) {
			found = true;
			i = this.length;
		}
	}
	return found;
}
Array.prototype.remove = function(value) {
	var i;
	var index = -1;
	
	for (i = 0; i < this.length; i++) {
		if (this[i] != value) {
			index = i;
			i = this.length;
		}
	}
	
	if (index >= 0) this.splice(index, 1);
}

//////////////////////////////////
//		DOM NODE PROTOTYPE		//
//////////////////////////////////
/*Node.prototype.swapNode = function(node) {
	var nextSibling = this.nextSibling;
	var parentNode = this.parentNode;
	node.parentNode.replaceChild(this, node);
	parentNode.insertBefore(node, nextSibling);
}*/