var HttpReq = Array(); //The HTTP  object used to send a request to the server
var requestDestination = Array(); //The object where the result of the request will be placed


//-----------------------------------------
// Send an HTTP request to the server
//-----------------------------------------
function getProductList(url, id, requestDestinationId, product_line_id, selected_product_id) {

	url += 'get_product_list.php?product_line_id='+product_line_id;

	//Look for a free handler
	for (i=0; i<HttpReq.length; i++) {
		if (HttpReq[i] == null)	{
			url += '&id='+id+'&selected='+selected_product_id;
			requestDestination[i] = document.getElementById(requestDestinationId);
			sendHttpRequest(url, HttpReq[i]);
			return;
		}
	}

	//There is no free handler, so use a new one
	i = HttpReq.length;
	url += '&id='+id+'&selected='+selected_product_id;
	requestDestination[i] = document.getElementById(requestDestinationId);
	sendHttpRequest(url, i);
	return;
}

//-----------------------------------------
// Send an HTTP request to the server
//-----------------------------------------
function sendHttpRequest(url, index) {
	// Branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        HttpReq[index] = new XMLHttpRequest();
        HttpReq[index].onreadystatechange = getHttpResult;
        HttpReq[index].open("GET", url, true);
        HttpReq[index].send(null);

    // Branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        HttpReq[index] = new ActiveXObject("MSXML2.XMLHTTP");
        if (HttpReq[index]) {
            HttpReq[index].onreadystatechange = getHttpResult;
            HttpReq[index].open("GET", url, true);
            HttpReq[index].send();
        }
    }
}

//-----------------------------------------
// Receive the HTTP resqonse from the server
//-----------------------------------------
function getHttpResult() {
	for (i=0; i<HttpReq.length; i++) {
		if (HttpReq[i].readyState == 4) {		// only if HttpReq shows "complete"
			if (HttpReq[i].status == 200) {		// only if "OK"
				requestDestination[i].innerHTML = HttpReq[i].responseText;
			} else {
				alert("There was a problem retrieving the product list.\n" + HttpReq[i].statusText);
			}
		}
	}
}