// It is assumed that AJAX is supported in the web browser being used
var httpRequestSupported = true;

// The indicates to the server that the request is an AJAX one
var ajaxParameter = '&rt=ajax';


// Error message variables
var validationError = false;
var validationErrorMsg = "";

// Navigation history handling
var navDestinationUrl;
var navUrlPath = '/webapp/wcs/stores/servlet/Navigate';
var navParams = [ 'lid', 'pn', 'ps', 'sid', 'sin', 'fid', 'rhi', 'rlo', 'rin',
		'val', 'vid', 'st', 'storeId', 'catalogId', 'listId', 'mfv', 'txt',
		'sfv', 'dfv', 'sfn', 'sinl', 'psl', 'pns', 'aspsp' ];



// /////////////////////////////////////////////////////////
// Returns true if AJAX requests are supported by the
// clients web browser. Else false.
// On a true result, the object returned is an
// Active-X XMLHTTP object.
//
// In Internet Explorer, you create an http object using
// new ActiveXObject("Msxml2.XMLHTTP") or
// new ActiveXObject("Microsoft.XMLHTTP")
// depending on the version of MSXML installed.
//
// In Mozilla and Safari (Gecko engine) you use
// new XMLHttpRequest()
//
// IceBrowser is not supported in this code
//
// @return boolean
// /////////////////////////////////////////////////////////
function isHttpRequestSupported() {

	if (window.XMLHttpRequest) {
		// Test if the Gecko engine is running. Gecko supports AJAX
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('application/json');
		}
	} else if (window.ActiveXObject) {
		// Test if an IE engine is running
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			}
		}
	}

	if (!httpRequest) {
		httpRequestSupported = false;
	}

	return httpRequest;
} // end function

// /////////////////////////////////////////////////////////
// Makes a HTTP request back to the server given a URL.
//
// callbackFunction is a string value containing the name
// of a method to call when the HTTP request is completed.
// The resulting output of the HTTP request is passed in
// as a variable to the callback function. The callback
// function formats the response to be displayed in
// a web browser.
//
// if returnData is specified, the response is retruned
// in xml
//
// @param url
// @param callbackFunction
// @param returnData
// /////////////////////////////////////////////////////////
function makeHttpRequest(url, callbackFunction, callbackParams, returnData) {

	var httpRequest = false;

	// Exit if this function is not supported
	if (!httpRequestSupported) {
		return;
	}

	// check if supported
	httpRequest = isHttpRequestSupported();

	if (!httpRequest) {
		httpRequestSupported = false;
		return false;
	}

	// Map the response to the callback function
	httpRequest.onreadystatechange = function() {
		if (httpRequest.readyState == 4) {
			if (httpRequest.status == 200) {
				if (returnData) {
					eval(callbackFunction + '(httpRequest.responseXML,callbackParams)');
				} else {
					eval(callbackFunction + '(httpRequest.responseText,callbackParams)');
				}
			} else {
				// TODO: Keep this line commented in production. The user will
				// have no idea what this means.
				// alert('AJAX request status: ' + httpRequest.status);
				eval(callbackFunction + '("")');
			}
		}
	}

	httpRequest.open('GET', url, true);
	httpRequest.send(null);
} // end function

///////////////////////////////////////////////////////////
// Makes a synchronous HTTP request to the given a URL.
// Returns the response as text
///////////////////////////////////////////////////////////
function makeSynchronousHttpRequest(url) 
{ 

	var httpRequest = false; 
	
	// Exit if this function is not supported
	if (!httpRequestSupported) {
		return;  
	}
	
	// check if supported
	httpRequest = isHttpRequestSupported();
	
	if (!httpRequest) { 
		httpRequestSupported = false;
		return false; 
	} 
	httpRequest.open('GET', url, false); 
	httpRequest.send(null);
	return httpRequest.responseText;
} // end function

