/*
 * IMPORTANT - DEPRECATED - use script.js -> asyncGet() instead.
 *
 * A central hub for XMLHttpRequest objects. This hub maintains concurrent asynchronous
 * XMLHttpRequest and HTML element pairs. Responses from the requests are feed directly 
 * to its paired HTML element's innerHTML property. 
 *
 * Cache stores the responses that would otherwise be fed directly to the responseElements.
 * This is currently used by search results to prevent request hits to db per JS renderResults() call.
 * Cache uses responseElements.id as key for uniqueness.
 */

var xmlreqs = new Array();
var responseElements = new Array();
var responseCache = new Array();

function CXMLReq(freed) {
	this.freed = freed;
	this.xmlhttp = false;
	if (window.XMLHttpRequest) { 
		this.xmlhttp = new XMLHttpRequest(); 
	}else if (window.ActiveXObject) {
		this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
}

/* Takes in the request url and the response object to feed innerHTML to */
function xmlreqGET(url, responseElement, responseType) { 
	var pos = -1;
	for (var i=0; i<xmlreqs.length; i++) {
		if (xmlreqs[i].freed == 1) { 
			pos = i; break;
		}
	} 
	if (pos == -1) {
		pos = xmlreqs.length;
		xmlreqs[pos] = new CXMLReq(1);
	}
	if (xmlreqs[pos].xmlhttp) {
		responseElements[pos] = document.getElementById(responseElement);
		xmlreqs[pos].freed = 0;
		xmlreqs[pos].xmlhttp.open("GET",url,true);
		if(responseType == 'fill'){
			xmlreqs[pos].xmlhttp.onreadystatechange = function() {  xmlhttpChangeFill(pos); }
		}
		if(responseType == 'append'){
			xmlreqs[pos].xmlhttp.onreadystatechange = function() {  xmlhttpChangeAppend(pos); }
		}
		if (window.XMLHttpRequest) {
			xmlreqs[pos].xmlhttp.send(null);
		} else if (window.ActiveXObject) {
			xmlreqs[pos].xmlhttp.send();
		}
	}
}

function xmlreqPOST(url,data) {
	var pos = -1;
	for (var i=0; i<xmlreqs.length; i++) {
		if (xmlreqs[i].freed == 1) {
			pos = i;
			break;
		 }
	}
	if (pos == -1) {
		pos = xmlreqs.length;
		xmlreqs[pos] = new CXMLReq(1);
	}
	if (xmlreqs[pos].xmlhttp) {
		xmlreqs[pos].freed = 0;
		xmlreqs[pos].xmlhttp.open("POST",url,true);
		xmlreqs[pos].xmlhttp.onreadystatechange = function() { 
			if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChangeNothing(pos); }
		}
		xmlreqs[pos].xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlreqs[pos].xmlhttp.send(data); 
	}

}

function xmlhttpChangeNothing(pos) {
	if (typeof(xmlreqs[pos]) != 'undefined' && xmlreqs[pos].freed == 0 && xmlreqs[pos].xmlhttp.readyState == 4) { 
		if (xmlreqs[pos].xmlhttp.status == 200 || xmlreqs[pos].xmlhttp.status == 304) { 
			// Do nothing
		} else {
			//handle_error();
		}
		xmlreqs[pos].freed = 1;
	}
}

function xmlhttpChangeFill(pos) {
	if (typeof(xmlreqs[pos]) != 'undefined' && xmlreqs[pos].freed == 0 && xmlreqs[pos].xmlhttp.readyState == 4) { 
		if (xmlreqs[pos].xmlhttp.status == 200 || xmlreqs[pos].xmlhttp.status == 304) { 
			responseElements[pos].innerHTML = xmlreqs[pos].xmlhttp.responseText;
		} else {
			//handle_error();
		}
		xmlreqs[pos].freed = 1;
	}
}

function xmlhttpChangeAppend(pos) {
	if (typeof(xmlreqs[pos]) != 'undefined' && xmlreqs[pos].freed == 0 && xmlreqs[pos].xmlhttp.readyState == 4) { 
		if (xmlreqs[pos].xmlhttp.status == 200 || xmlreqs[pos].xmlhttp.status == 304) { 
			responseElements[pos].innerHTML += xmlreqs[pos].xmlhttp.responseText;
			responseCache[responseElements[pos].id] = xmlreqs[pos].xmlhttp.responseText;
		} else {
			//handle_error();
		}
		xmlreqs[pos].freed = 1;
	}
}