String.prototype.trim = function() {
	return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};

/* handle more than one request */
var arrayXMLHttpRequests = [];

/* create a object that will contain all the associated data for each AJAX call */
function QueuedRequest(theRequest, theUrl, theParams) {
	this.xmlHttp = theRequest;
	this.url = theUrl;
	this.param = theParams;
}

/* create a new XMLHTTPRequest */
function ajax_AssignObject() {
	var xmlHttp = false;

	try {
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			xmlHttp = false;
		}
	}

	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
		xmlHttp = new XMLHttpRequest();
	}
	return xmlHttp;
}

function AJAX_vServerResponse() {
	var objRequest = false;

	// * the first item will be the one whose response we will receive
	objRequest = arrayXMLHttpRequests[0];

	if (objRequest.xmlHttp.readyState == 4) {
		var response = objRequest.xmlHttp.responseText.split('|||');
		var result = new String(response[0]);
		result = result.trim();
		// alert(objRequest.xmlHttp.responseText);
		if (result == 'true') {
			window.location.reload();
		} else if (result == 'redirect') {
			window.location = response[1];
		} else if (result == 'java') {
			var script = new String(response[1]);
			eval(response[1]);
		} else if (result == 'alert') {
			var message = new String(response[1]);
			message = message.trim();
			alert(message);
		} else {
			for ( var i = 1; i < response.length; i += 2) {
				var element = new String(response[i]);
				var value = new String(response[i + 1]);
				element = element.trim();
				value = value.trim();
				try {
					document.getElementById(element).innerHTML = value;
				} catch (e) {
					document.getElementById(element).value = value;
				}
			}
		}
		arrayXMLHttpRequests.shift();
	}
}

function AJAX_vExecute(thisaction) {
	var xmlHttp = false;
	var url = "/ajaxactions.php";
	var params = "ms=" + new Date().getTime() + "&action=" + escape(thisaction)
			+ "&arguments=" + escape(AJAX_vExecute.arguments.length - 1);
	var thisargument;
	var thisvalue;
	// iterate through arguments
	for ( var i = 1; i < AJAX_vExecute.arguments.length; i++) {
		thisargument = arguments[i];
		params = params + "&argument" + i + "=" + escape(thisargument);
	}
	// * add object to the array
	// alert(url);
	// * add object to the array
	xmlHttp = ajax_AssignObject();
	if (typeof (xmlHttp) != 'undefined') {
		objRequest = new QueuedRequest(xmlHttp, url,params);
		arrayXMLHttpRequests.push(objRequest);
		// * only send the request if this is the only item in the array
		if (arrayXMLHttpRequests.length == 1) {
			// * open connection
			xmlHttp.open("POST", url, true);
			// * Send the proper header information along with the request
			xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", params.length);
			xmlHttp.setRequestHeader("Connection", "close");
			// * setup the callback function
			xmlHttp.onreadystatechange = AJAX_vServerResponse;
			// * send data
			xmlHttp.send(params);
		} else {
			arrayXMLHttpRequests.pop();
			//alert('Please wait for previous request to complete.');
		}
	}
}

function AJAX_vRequest(thisaction) {
	var xmlHttp = false;
    var url = "/ajaxactions.php";
	var params = "ms=" + new Date().getTime() + "&action="
			+ escape(thisaction) + "&arguments="
			+ escape(2 * (AJAX_vRequest.arguments.length - 1));
	var thisargument;
	var thisvalue;
	// iterate through arguments
	for ( var i = 1; i < AJAX_vRequest.arguments.length; i++) {
		thisargument = arguments[i];
		thisvalue = document.getElementById(thisargument).value;
		if (thisvalue == undefined) {
			thisvalue = document.getElementById(thisargument).innerHTML;
		}
		params = params + "&argument" + (i * 2 - 1) + "=" + escape(thisargument);
		params = params + "&argument" + (i * 2) + "=" + escape(thisvalue);
	}
	// alert(url);
	// * add object to the array
	xmlHttp = ajax_AssignObject();
	if (typeof (xmlHttp) != 'undefined') {
		objRequest = new QueuedRequest(xmlHttp, url,params);
		arrayXMLHttpRequests.push(objRequest);
		// * only send the request if this is the only item in the array
		if (arrayXMLHttpRequests.length == 1) {
			// * open connection
			xmlHttp.open("POST", url, true);
			// * Send the proper header information along with the request
			xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", params.length);
			xmlHttp.setRequestHeader("Connection", "close");
			// * setup the callback function
			xmlHttp.onreadystatechange = AJAX_vServerResponse;
			// * send data
			xmlHttp.send(params);
		} else {
			arrayXMLHttpRequests.pop();
			//alert('Please wait for previous request to complete.');
		}
	}
}