function getPageCoords() {
	var element = arguments[0];
	element = (typeof element=="object")? element :document.getElementById(element);
	var coords = {x: 0, y: 0};
	while (element) {
		coords.x += element.offsetLeft;
		coords.y += element.offsetTop;
		element = element.offsetParent;
	}
	return coords;
}

function urlEncode(str){
	str = escape(str);
	str = str.replace(/\+/g, "%2b");
	str = str.replace(/ /g, "+");
	return str;
}

function urlDecode(str){
	str = str.replace(/\+/g, " ");
	str = str.replace(/\%2[B,b]/g, "+");
	str = unescape(str);
	return str;
}

var _pw_l,_pw_t,_pw_z;
function popWin(url,n,w,h,o) {
	if (w>screen.availWidth-12) w=screen.availWidth-12;
	if (h>screen.availHeight-48) h=screen.availHeight-48;
	_pw_l=(screen.availWidth-w-12)/2;
	_pw_t=(screen.availHeight-h-48)/2;
	_pw_z=window.open(url,n,'width='+w+',height='+h+',left='+_pw_l+',top='+_pw_t+','+o);
}

//MP based functions
function toggleLayer() {
	var obj = document.getElementById(arguments[0]);
	if (obj.style.display == "block") {
		obj.style.display = "none"
	} 
	else {obj.style.display = "block"}
}

function genRandNum(range,start){
	if (!start) start = 0;
	num = Math.floor(Math.random()*range) + start;
	return num;
}

function getRandArrVal(mArr){
	return(mArr[genRandNum(mArr.length)]);
}

// Added by Sam
//Returns a reference to an object by id
function getObject(id) {
	var obj = document.getElementById(id);
	return obj;
}

//Returns an Outer HTML of an object (works in Mozilla browsers)

function getOuterHTML(obj) {
	var out = "";
	if(!document.all) {	//Netscape fix for outerHTML (has none);
		var tmpTag = document.createElement("temp");
		tmpTag.appendChild(obj);
		out = tmpTag.innerHTML;
	} else {
		out = obj.outerHTML;
	}
	return out;	
}

String.prototype.trim = function() {
     return this.replace(/^\s+|\s+$/gm,'');
}

String.prototype.clean = function() {
     return this.replace(/\s/gm,' ');
}

String.prototype.capitalize = function() {
     var tStr = this.toLowerCase();
	 return tStr.charAt(0).toUpperCase() + tStr.substring(1);
}
//Date functions
//Increments Year by Value (can be negative)
Date.prototype.incrementYear = function(val) {
		var nDate = new Date((this.getYear()+val), this.getMonth(), this.getDate());
		return nDate;	
}
//Increments Month by Value
Date.prototype.incrementMonth = function(val) {
	var nDate = new Date(this.getYear(), (this.getMonth()+val), this.getDate());
	return nDate;	
}
//Increments Date by Value
Date.prototype.incrementDate = function(val) {
	var nDate = new Date(this.getYear(), this.getMonth(), (this.getDate() + val));
	return nDate;	
}
//Parses Date in a short format (mm/dd/yyyy);
Date.prototype.parseShortDate = function(strDate) {
	var dp = strDate.split("/");
	if(dp.length > 0) {
		return new Date(dp[2],(parseInt(dp[0])-1),parseInt(dp[1]));
	} else {
		return null;
	}
}
//Converts Date to a short format string (mm/dd/yyyy);
Date.prototype.toShortDate = function() {
	var mm = ((this.getMonth()+1) < 10)?"0"+(this.getMonth()+1):(this.getMonth()+1);
	var dd = (this.getDate() < 10)?"0"+this.getDate():this.getDate();
	var yy = this.getFullYear();
	return mm + "/" + dd + "/" + yy;
}

//Parses Date in a long format (YYYYMMDDhh:mm:ss);
Date.prototype.parseLongDate = function(strDate) {
	var y = strDate.substring(0,4);
	var m = strDate.substring(4,6);
	var d = strDate.substring(6,8);
	if(y && m && d) {
		return new Date(y,(parseInt(m)-1),parseInt(d));
	} else {
		return null;
	}
}

//Array/Collection functions
//Inserts a unique value into an array
Array.prototype.insert = function(val) {
	if(!this.contains(val)) {
		this.push(val);
		return this[this.length-1];
	}
	return false;	
}


//Hashtable
function Hashtable() {
	this.holder = new Array();	
	this.contains;
	this.add;
	this.remove;
	this.getKeys;
	this.getValues;
	this.get;
	this.length = 0;
	
	this.hash = function(n, v) {
		this.name = n;
		this.value = v;
	}
}
//Contains check
Hashtable.prototype.contains = function(n) {
	var tHash;
	var hLen = this.holder.length - 1;
	if(hLen >= 0) {
		do {
			tHash = this.holder[hLen];
			if(tHash.name == n) return true;
		} while(hLen--);
	}
	return false;
}
//IndexOf function
Hashtable.prototype.indexOf = function(n) {
	var tHash;
	var hLen = this.holder.length - 1;
	if(hLen >= 0) {
		do {
			tHash = this.holder[hLen];
			if(tHash.name == n) return hLen;
		} while(hLen--);
	}
	return -1;
}
//Add function
Hashtable.prototype.add = function(n, v) {
	if(!this.contains(n)) {
		this.length++;
		return this.holder.push(new this.hash(n, v));
	}
	return this.holder[this.indexOf(n)];
}
//Remove function
Hashtable.prototype.remove = function(n) {
	var idxOf = this.indexOf(n);
	if(idxOf != -1) {
		this.length--;
		return this.holder.splice(idxOf,1)[0];
	}
	return null;
}
//Get function
Hashtable.prototype.get = function(n) {
	var idxOf = this.indexOf(n);
	if(idxOf != -1) {
		return this.holder[idxOf].value;
	}
	return null;
}
//Returns Keys collection
Hashtable.prototype.getKeys = function() {
	var keys = new Array();
	var hLen = this.holder.length - 1;
	if(hLen >= 0) {
		do {
			tHash = this.holder[hLen];
			keys.push(tHash.name);
		} while(hLen--);		
	}
	return keys;
}
//Returns Values collection
Hashtable.prototype.getValues = function() {
	var values = new Array();
	var hLen = this.holder.length - 1;
	if(hLen >= 0) {
		do {
			tHash = this.holder[hLen];
			values.push(tHash.value);
		} while(hLen--);		
	}
	return values;
}
