//Array(XMLHttpRequest, in-use)
var __ajaxRequestPool = new Array();

function showPoolStatus(){
	var msg = "POOL STATUS.\n";
	for(var i=0; i<__ajaxRequestPool.length; i++) {
		msg += "<li>" + i + ": In Use : " + __ajaxRequestPool[i][0] + " : " + __ajaxRequestPool[i][1] +"</li>";
	}
	return msg;
}

//showPoolStatus();

AjaxRequest.prototype.poolIndex = -1;

AjaxRequest.prototype.setRequestURL = function($in) {this.requestURL = $in;}
AjaxRequest.prototype.getRequestURL = function() {return this.requestURL;}

AjaxRequest.prototype.setRequestType = function($in) {this.requestType = ($in == null || ($in.toUpperCase() != 'POST' && $in.toUpperCase() != 'GET')) ? "GET" : $in;	}
AjaxRequest.prototype.getRequestType = function() {return this.requestType;}

AjaxRequest.prototype.setRequestParameter = function($name, $value) {this.requestParameters.push(new Array($name, $value));}
AjaxRequest.prototype.getRequestParameterList = function() {
	var retVal = "";
	for (var i=0; i<this.requestParameters.length; i++) {
		var nvArr = this.requestParameters[i];
		retVal += (i>0) ? "&" : "";
		retVal += nvArr[0] + "=" + nvArr[1];
	}
	return retVal;
}

AjaxRequest.prototype.setPreventCaching = function($in) {this.preventCaching = $in;}
AjaxRequest.prototype.getPreventCaching = function() {return this.preventCaching;}

AjaxRequest.prototype.setCallbackFunction = function($in) {this.callback = $in;}
AjaxRequest.prototype.getCallbackFunction = function() {return this.callback;}

AjaxRequest.prototype.getResponse = function(){return this.ajro.getResponse();}

AjaxRequest.prototype.GetXmlHTTPObject = function() { 
	var obFound = false;
	var retVal = -1;
	for (var i=0; i<__ajaxRequestPool.length; i++) {
		if (__ajaxRequestPool[i][1] == false) {
			__ajaxRequestPool[i][1] = true;
			retVal = i;
			obFound = true;
			break;
		}
	}

	if (obFound == false) {
			var ob = this.InitialiseXmlHTTPObject();
			retVal = __ajaxRequestPool.length;
			__ajaxRequestPool.push(new Array(ob, true));
	}

	return retVal;
}

AjaxRequest.prototype.InitialiseXmlHTTPObject = function() { 
	var ob = null;
	if (window.XMLHttpRequest) {
		try {
			ob = new XMLHttpRequest();
		} catch(e) {
			ob = false;
		}
	} else if (window.ActiveXObject ) {
		try {
			ob = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				ob = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				ob = false;
			}
		}
	}
	return ob;
}

AjaxRequest.prototype.execute = function() {
	this.poolIndex = this.GetXmlHTTPObject();
	if (this.poolIndex > -1) {
		if (this.getPreventCaching()) {
			this.setRequestParameter("curTime", new Date().getTime());
		}
		this.ajro = new AjaxRequestObject(this.poolIndex, this.getRequestURL(), this.getRequestType(), this.getRequestParameterList());		
		this.waitForResponse();
	} else {
		__ajaxRequestPool[this.poolIndex][0].abort();
		alert("The XMLHttpRequest Object is not supported");
	}		
}
	
AjaxRequest.prototype.waitForResponse = function(){
	if (this.getResponse() != null) {
		if (this.getCallbackFunction() != null && this.getCallbackFunction() != '') {
			eval(this.getCallbackFunction());
		} 
		this.finalise();
	} else {
		var obj = this;
		window.setTimeout(function () { obj.waitForResponse(); }, 100);
	}
}	

AjaxRequest.prototype.finalise = function(){
	if (this.ajro != null) {
		this.ajro.finalise();
		this.ajro = null 
	}
	this.preventCaching = true;
	this.requestURL = null;
	this.callback = null;
	this.requestType = null;
	this.requestParameters = new Array();	
	this.requestType = "GET";
	__ajaxRequestPool[this.poolIndex][1] = false;
}

function AjaxRequest(){
	this.ajro = null;
	this.preventCaching = true;
	this.requestURL = null;
	this.callback = null;
	this.requestType = null;
	this.requestParameters = new Array();	
	this.requestType = "GET";
}



AjaxRequestObject.prototype.setResponse = function($in){this.ajResponse = $in;}  
AjaxRequestObject.prototype.getResponse = function(){return this.ajResponse;} 

function AjaxRequestObject($poolIndex, $req, $type, $params) {
	this.__successfulResponse = false;
		
	this.ajResponse = null;
	this.tp = $type;
	this.pm = $params;
	
	
	this.makeRequest = function($req) {
		
		var obj = this;
		if (this.tp.toLowerCase() == "get") {
			__ajaxRequestPool[$poolIndex][0].open("GET", $req + "?" + this.pm, true); 
			__ajaxRequestPool[$poolIndex][0].onreadystatechange = function(){ obj.stateChanged(obj); }
			__ajaxRequestPool[$poolIndex][0].send("");
		} else {
			__ajaxRequestPool[$poolIndex][0].open('POST', $req, true);
			__ajaxRequestPool[$poolIndex][0].onreadystatechange = function(){ obj.stateChanged(obj); }
			__ajaxRequestPool[$poolIndex][0].setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
			__ajaxRequestPool[$poolIndex][0].setRequestHeader("Content-length", this.pm.length);
			__ajaxRequestPool[$poolIndex][0].setRequestHeader("Connection", "close");
			__ajaxRequestPool[$poolIndex][0].send(this.pm);
		} 
		this.wait();
	} 
	
	this.stateChanged = function($obj) {
		if (__ajaxRequestPool[$poolIndex][0].readyState == 4) { 
			if (__ajaxRequestPool[$poolIndex][0].status == 200 || __ajaxRequestPool[$poolIndex][0].status == 0) {
				$obj.__successfulResponse = true;
				$obj.setResponse(__ajaxRequestPool[$poolIndex][0].responseText);
			} else {
				alert("There was an issue retrieving the data:\nReason: " + __ajaxRequestPool[$poolIndex][0].statusText);
			}
		}     
	}
	
	this.wait = function() {
		if (this.__successfulResponse == false) {
			var obj = this;
			setTimeout(function(){obj.wait();}, 250);
		}	
	}

	this.finalise = function(){
		this.ajResponse = null;
		this.tp = null;
		this.pm = null;
		this.__ajOb = null;
	}
	
	this.makeRequest($req);
}


