/**
 * Supports making simultaneous AJAX requests.
 * Example usage...
 *
 *
 *	function makeSomeSpecificRequest(var1, var2) {
 *		var xhri = xhrRequest();
 *		xhr[xhri].open('GET', "urlTo.hit?var1="+var1+"&var2="+var2, true);
 *		xhr[xhri].onreadystatechange = function() {
 *			if (xhr[xhri].readyState == 4 && xhr[xhri].status == 200) {
 *				pageUpdatingFunction(xhr[xhri].responseXML);
 *				xi[xhri] = 1;
 *				xhr[xhri] = null;
 *			}
 *		};
 *		xhr[xhri].send(null);
 *	}
 *	
 *	function pageUpdatingFunction(responseXML) {
 *		var xmldoc = responseXML;
 *		// do stuff to the page here using DOM or whatever you like
 *	}
 *
 * 
 */

var xhr = new Array(); // ARRAY OF XML-HTTP REQUESTS
var xi = new Array(0); // ARRAY OF XML-HTTP REQUEST INDEXES
xi[0] = 1; // FIRST INDEX SET TO 1 MAKING IT AVAILABLE

function xhrRequest() {
	// xhrsend IS THE xi POSITION THAT GETS PASSED BACK
	// INITIALIZED TO THE LENGTH OF THE ARRAY(LAST POSITION + 1)
	// IN CASE A FREE RESOURCE ISN'T FOUND IN THE LOOP
	var xhrsend = xi.length; 
	
	// GO THROUGH AVAILABLE xi VALUES
	for (var i=0; i<xi.length; i++) {

		// IF IT'S 1 (AVAILABLE), ALLOCATE IT FOR USE AND BREAK
		if (xi[i] == 1) {
			xi[i] = 0;
			xhrsend = i;
			break;
		}
	}

	// SET TO 0 SINCE IT'S NOW ALLOCATED FOR USE
	xi[xhrsend] = 0;

	// SET UP THE REQUEST
	if (window.ActiveXObject) {
		try {
			xhr[xhrsend] = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xhr[xhrsend] = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	} else if (window.XMLHttpRequest) {
		xhr[xhrsend] = new XMLHttpRequest();
		if (xhr[xhrsend].overrideMimeType) {
			xhr[xhrsend].overrideMimeType('text/xml');
		}
	}
	return (xhrsend);
}


/**
 * Use this for non simultaneous posting.
 */
function getRequestObject(){
	var http_request = false;
    if (window.XMLHttpRequest) {
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) {
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    return http_request;
}

