var xmlhttp=null;
var loadQueue = new Array();
var firstRun = 'true';

function loadXMLDoc(url, divID, functionList)
{
	if((url == 'subcategory_list.inc.php' || url == 'subcategory_form.inc.php') && functionList != 'reset' && window.localStorage[url].length == '1684979') {
		document.getElementById('debug').innerHTML = 	document.getElementById('debug').innerHTML + '<BR>Using Local Storage (' + window.localStorage[url].length + ')';
		localURL = window.localStorage[url];
		if(localURL != undefined) {
			document.getElementById(divID).innerHTML=localURL;
			
			if(loadQueue.length > 1) {
				nextLoad();
				//alert('next');
			} else {
				//document.getElementById('ajax_status').style.display = 'none';
				$('#ajax_status').fadeOut('medium');
				firstRun = 'true';
				//alert('complete');
			}	
			return;
		} else {
			functionList = url;
		}
	} else {
	if(functionList == 'reset') {
		functionList = undefined;
	}
	pausecomp(500);
	xmlhttp=null;
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
		{
		xmlhttp=new XMLHttpRequest()
		}
	// code for IE
	else if (window.ActiveXObject)
		{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		}
	if (xmlhttp!=null)
		{
		xmlhttp.onreadystatechange=function() {state_Change(divID, functionList)}
		xmlhttp.open("GET",url,true)
		xmlhttp.send(null)
		}
	else
		{
		alert("Your browser does not support XMLHTTP.");
		document.getElementById('ajax_status').style.display = 'none';
		}
	}
}

function state_Change(divID, functionList)
{	
	
	if(document.getElementById('debug')) {
		document.getElementById('debug').innerHTML = 	document.getElementById('debug').innerHTML + '<BR>' + divID + '(' + functionLst + '): ' +  xmlhttp.readyState + '-' + xmlhttp.status + '(' + loadQueue.length + ')<BR>' + loadQueue + '<BR>';
	}
	//alert(xmlhttp.readyState);
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4)
		{
		// if "OK"
		if (xmlhttp.status==200)
		{
			document.getElementById(divID).innerHTML=xmlhttp.responseText;
			if(functionList == 'subcategory_list.inc.php' || functionList == 'subcategory_form.inc.php') {
				document.getElementById('debug').innerHTML = 	document.getElementById('debug').innerHTML + '<BR>Saving to Local Storage';
				window.localStorage[functionList] =document.getElementById(divID).innerHTML;
				functionList = undefined;
			}
			if (functionList) {
				var x;
				var function_array = functionList.split(',');
				for (x in function_array) {
					eval(function_array[x]);
				}
			}
		}
		else
		{
			alert("Problem retrieving data: "  + xmlhttp.statusText + " (" + divID + ")");
			document.getElementById('ajax_status').style.display = 'none';
		}
		if(loadQueue.length > 1) {
			nextLoad();
			//alert('next');
		} else {
			//document.getElementById('ajax_status').style.display = 'none';
			$('#ajax_status').fadeOut('medium');
			firstRun = 'true';
			//alert('complete');
		}
	}
}

// www.sean.co.uk

function pausecomp(millis)
{
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); }
	while(curDate-date < millis);
}


function nextLoad() {
	if (xmlhttp == null) {
		loadXMLDoc(loadQueue.shift(),loadQueue.shift(),loadQueue.shift());
	} else {
	if (xmlhttp.readyState==0 || xmlhttp.readyState==4) {
		loadXMLDoc(loadQueue.shift(),loadQueue.shift(),loadQueue.shift());
	};}
}

function insertQueue(url,divID, functionList) {
	document.getElementById(divID).innerHTML= 'Loading...';
	if (firstRun == 'true') {
		document.getElementById('ajax_status').innerHTML = 'Loading... Please wait';
		//document.getElementById('ajax_status').style.display = 'block';
		$('#ajax_status').fadeIn('fast');
	}
	firstRun = 'false';
	loadQueue.push(url, divID, functionList);
}
