var ajax_queue = new Array();
var ajax_current_record = new Array();
var ajax_queue_state = 0; //keeps a lock
var ajax_http; //http obj
document.messageHeaders = new Array(); //storage


function ajax_addToQueue(url,callback){
	tmpVar = new Array(url,callback);
	if (log){log("adding to queue.. " + tmpVar)};
	ajax_queue.push(tmpVar);
}

function ajax_pollQueue(){
	//poll the queue for items
	setTimeout("ajax_pollQueue();",500);
	
	if (ajax_queue_state == 0){ //ready
		
		//remove an item
		if (ajax_queue.length > 0){
			ajax_queue_state = 1;
			ajax_current_record = ajax_queue[ajax_queue.length-1];
			ajax_queue.pop();
			
			ajax_http = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();    ; //xmlhttprequest obj			
			if (log){log("calling url " + ajax_current_record[0])};
			ajax_http.onreadystatechange = ajax_callbackFromQueue;
		    ajax_http.open("GET", ajax_current_record[0], true);
		    ajax_http.send(true);
		    
		    if (log){log("call made, awaiting callback")};
		}else{
			if (log){log("no items in the poll queue")};
		}
	}else{
		if (log){log('Poll Queue says something else is already processing')};
	}
}

function ajax_callbackFromQueue(){
	//check and call
	if (typeof(ajax_http) == 'object'){
		if (log){log("got callback from queue, readystate = " + ajax_http.readyState)};
		if (ajax_http.readyState == 4){
			ajax_queue_state = 0;
			ajax_response = ajax_http.responseText;
			eval("" + ajax_current_record[1]);
		}
	}
}

setTimeout("ajax_pollQueue();",500);
setTimeout("ajax_callbackFromQueue()",1000);
