// AJAX FUNCTIONS
function getXMLHTTP()
 {
 	try {
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	} catch(ex) {
		//either this is not IE, or it is a version of IE which does not support XMLHTTP
		var notIECompatibleXMLHTTP=true;
	}
	if (notIECompatibleXMLHTTP==true) {
		try {
			request_o = new XMLHttpRequest();
		} catch(ex) {
			//we can't use AJAX because this browser is not compatible.
			request_o = false;
		}
	}

	return request_o;
}

var responseObjectId = null;		// the id of the object to receive the response data
var useCustomFn = false;			// use a custom function to process the results - calling page must have function: processResults(response)
var showLoading = false;			// whether to display the loading div
function sendAjaxRequest(url,objId,loading,customFn) {
		responseObjectId = objId;
		useCustomFn = customFn;
		showLoading = loading;
		
		if (showLoading)
			toggleLayer('loading',true);
			
		http = getXMLHTTP();
		if (http) {
			http.open('get',  url);
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

			/* Define a function to call once a response has been received. This will be our
				handleProductCategories function that we define below. */
			http.onreadystatechange = receiveAjaxResponse;
			/* Send the data. We use something other than null when we are sending using the POST
				method. */
			http.send(null);
		} else {
			alert("xmlHTTP cannot be created");
		}
		
}

function receiveAjaxResponse() {
	if (http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element
			that we can find: innerHTML. */
			//alert(response);
		if (!useCustomFn) {
			document.getElementById(responseObjectId).value = response;
			//alert(response);
		} else {
			processResults(response);		// this function must exist in the calling page
		}
		
		if (showLoading)
			toggleLayer('loading',false);
	}
}

function addOption(selectName,idx,optValue,optText,selected) {
	if (optText) {
		var opt = new Option();
		opt.value = optValue;
		opt.text = optText;
		opt.selected = selected;
		document.getElementById(selectName).options[idx] = opt;
	}
}

function clearOptions(selectName) {
	while (document.getElementById(selectName).options.length > 0)	{
		idx = document.getElementById(selectName).options.length - 1;
	 	document.getElementById(selectName).options[idx] = null;
	}
}
