// *** PUNY AJAX QUEUE! ***
//
// This bit of code will allow for making multiple AJAX requests in quick succession.

// a class to hold the necessary data to make an AJAX request
function AjaxSpec (url, data, callback) {
    this.url = url;
    this.data = data;
	this.callback = callback;
    this.execute = function() {
		ajaxRequest(this.url, this.data, this.callback);
	};
}


// a new function to prepare multiple asynchronous AJAX requests
var ajaxQueue = [];
function queueAjax(url, data, callback) {
	
	var as = new AjaxSpec(url, data, callback);
	
	if (!waitingForAjax) {
		as.execute();
	}
	else {
		ajaxQueue.push(as);
	}
}


// a traditional, asynchronous AJAX request, now with a flag to indicate waiting status
var waitingForAjax = false;
function ajaxRequest(url, data, callback) {
	
	waitingForAjax = true;
	
	try {
		xhrobj = new XMLHttpRequest();
	} 
	catch (e) {
		try {
			xhrobj=new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			xhrobj=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	xhrobj.open("GET", url);
	xhrobj.setRequestHeader("X-Requested-With", "XMLHttpRequest"); 

	xhrobj.onreadystatechange = function() {
		if (xhrobj.readyState == 4 && xhrobj.status == 200) {
//			alert(url+":     "+xhrobj.responseText);
			callback(xhrobj.responseText);

			waitingForAjax = false;

			if (ajaxQueue.length > 0) {
				var as = ajaxQueue.shift();
				as.execute();
			}
		}
	};
	
	xhrobj.send(data);
}
