String.prototype.trim = function() {
	if(this.toString().length > 0){
		var str = this.toString();
		str = str.replace(/^\s\s*/, '');
		str = str.replace(/\s\s*$/, '');
		return str;
	}
}
var xmlHttp;
function ajaxFunction() {

	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;
			}
		}
	}
}

function maxSummary(what, maxlength, tname) {
	maxKeys = maxlength;
	keysSoFar = what.value.length;
	if (keysSoFar > maxKeys) {
		what.value = what.value.substring(0, maxKeys); // chop the last typed
		// char
	} else
		document.getElementById(tname).innerHTML = maxKeys - keysSoFar;
}

function wait() {
	document.getElementById("waiting_content").style.display = 'inline';
	document.getElementById("content").style.display = 'none';
}

function setCountry(countryCode) {
	if (countryCode == "US" || countryCode == "CA") {
		document.getElementById("divState").style.display = "";
		//document.getElementById("divLocation").style.display = "none";
		if (countryCode == "US") {
			populate(1);
			document.getElementById("lblState").innerHTML = "State:";
		} else if (countryCode == "CA") {
			populate(2);
			document.getElementById("lblState").innerHTML = "Province:";
		}
	} else {
		document.getElementById("divState").style.display = "none";
		//document.getElementById("divLocation").style.display = "";
		//document.getElementById("location").value = "";
		document.getElementById("lblState").innerHTML = "";
	}
}

function populate(number) {
	var box = document.getElementById("state");
	var list = store[number];
	box.options.length = 0;
	for (i = 0; i < list.length; i++) {
		box.options[i] = new Option(list[i], list[i]);
	}
}

function hide(object) {
	document.getElementById(object).style.display = 'none';
}

function show(object) {
	document.getElementById(object).style.display = 'block';
}

function clear(object) {
	document.getElementById(object).innerHTML = '';
}

function show_pop(id) {
	document.getElementById(id).style.display="block";
	document.getElementById(id).style.zIndex=100;	
}


function ajaxProgress() {
	if (xmlHttp.readyState == 1) {
		document.getElementById('ajaxprogress').style.display = 'inline';
	}
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		document.getElementById('ajaxprogress').style.display = 'none';
	}
}
/**
 * send ajax request to the given url
 * 
 * @param {}
 *            url url to send request
 * @param {}
 *            params array of parameter/values. parameters should be key
 * @param {}
 *            callBack call back function
 */
function ajaxRequest(url, params, callBack) {

	ajaxFunction();
	xmlHttp.onreadystatechange = callBack;

	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}
// ///

// Point Object
// ------------------------------------------------------------------------------------------------

function Point(posX, posY) {
	this.x = posX;
	this.y = posY;
	this.xy = new Array(this.x, this.y);
	this.toString = function() {
		return this.x + " x " + this.y;
	}
}

// Rect Object
// ------------------------------------------------------------------------------------------------

function Rect(posX1, posY1, posX2, posY2) {
	this.left = (posX1 <= posX2) ? posX1 : posX2;
	this.top = (posY1 <= posY2) ? posY1 : posY2;
	this.right = (posX1 <= posX2) ? posX2 : posX1;
	this.bottom = (posY1 <= posY2) ? posY2 : posY1;
	this.width = this.right - this.left;
	this.height = this.bottom - this.top;
	this.xy1 = new Array(this.left, this.top);
	this.xy2 = new Array(this.right, this.bottom);
	this.topLeft = function() {
		return new Point(this.left, this.top);
	}
	this.bottomRight = function() {
		return new Point(this.right, this.bottom);
	}
	this.isEmpty = function() {
		return (this.left == this.right || this.top == this.bottom);
	}
	this.toString = function() {
		return this.topLeft().toString() + " - "
				+ this.bottomRight().toString();
	}
}

// Point and Rect methods
// ------------------------------------------------------------------------------------------------

function pointInRect(p, r) {
	if (p == null || r == null) {
		return false;
	}
	return (p.x >= r.left && p.x <= r.right && p.y >= r.top && p.y <= r.bottom);
}

function rectIntersect(r1, r2) {
	// This function must be revised
	if (r1 == null || r2 == null) {
		return false;
	}
	return (r1.bottom >= r2.top && r1.top < r2.bottom && r1.right >= r2.left && r1.left < r2.right);
}

// Elements position and layout methods
// ------------------------------------------------------------------------------------------------

function findPos(obj) {
	if (obj == null) {
		return new Point(0, 0);
	}
	var objLeft = objTop = 0;
	if (obj.offsetParent) {
		do {
			objLeft += obj.offsetLeft;
			objTop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return new Point(objLeft, objTop);
}

function getObjLayout(obj) {
	if (obj == null) {
		return new Rect(0, 0, 0, 0);
	}
	var objPos = findPos(obj);
	return new Rect(objPos.x, objPos.y, objPos.x + obj.offsetWidth, objPos.y
			+ obj.offsetHeight);
}