
/*--------------------------------------------------------------------------------
AJAX Queue
----------------------------------------------------------------------------------*/		

var ajaxQueue = new Array;
var ajaxServiceRunning = false;

var httpAjax = false;

/*--------------------------------------------------------------------------------
AjaxService

This is the only interface to the AJAX wrapper. This makes the ajax service 
browser agnostic and provides a single interface for all of the services.

Each service uses a different url and a different parameter layout.
				
----------------------------------------------------------------------------------*/
function AjaxService(servicename, serviceparam) {

	ajaxQueue.push(servicename);
	ajaxQueue.push(serviceparam);

	if(!ajaxServiceRunning) {
		ajaxServiceRunning = true;
		var aParam = ajaxQueue.pop();
		var aService = ajaxQueue.pop();
		ajaxServiceExec(aService, aParam);
	}				
}

/*--------------------------------------------------------------------------------
AjaxService

This is the only interface to the AJAX wrapper. This makes the ajax service 
browser agnostic and provides a single interface for all of the services.

Each service uses a different url and a different parameter layout.
				
----------------------------------------------------------------------------------*/
function ajaxServiceExec(servicename, serviceparam) {

	// Parameters are set, initiate AJAX engine
	if (window.XMLHttpRequest) {
		httpAjax = new XMLHttpRequest();
		if (httpAjax.overrideMimeType) {
			httpAjax.overrideMimeType('text/xml');
		}
	} else if(window.ActiveXObject) {
		try {
			httpAjax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				httpAjax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				alert("Couldnīt build an AJAX instance.");
				return false;
			}
		}
	}

	// Send request to Server and initiate callback.
	try {
		httpAjax.onreadystatechange = getPage;
		httpAjax.open('POST', servicename, true);
		httpAjax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
		httpAjax.send(serviceparam);		
	} catch(e) {
		//alert("onreadystatechange didnīt go well!");
		return false;
	}

	return true;

}

/*--------------------------------------------------------------------------------
getPage

This is the AJAX callback function. It detects the invoking service and handles
the reply from each service accordingly.				
		
----------------------------------------------------------------------------------*/
function getPage() {

	if(httpAjax.readyState == 4) { 

		str = httpAjax.responseText;

		if(ajaxQueue.length>0) {
			var Aparam = ajaxQueue.pop();
			var Aservice = ajaxQueue.pop();
			ajaxServiceExec(Aservice, Aparam);
		} else {
			ajaxServiceRunning=false;				
		}

		return true;				
	} else {
	// If any other ready state than prepared, do nothing!
	}
}

