﻿var ajaxActionXMLHttpObject;
var ajaxActionSuccessFunction	= null;
var ajaxActionFailFunction		= null;
var AJAX_ACTION_ERROR_MESSAGE	= "There was a problem with this action, if this problem persists (even after a page refresh and cache clean out) then please contact support";


// Send a http request for an action request
function performAjaxAction(ajaxActionType, ajaxActionData, thisAjaxActionSuccessFunction, thisAjaxActionFailFunction) {
	
	// Build the URI to perform the action
	var requestUri = "/common/ajaxFeeds/ajaxActionEngine.asp?actionType=" + ajaxActionType + "&actionData=" + escape(ajaxActionData);
	
	// Register the success and fail functions
	ajaxActionSuccessFunction = thisAjaxActionSuccessFunction;
	ajaxActionFailFunction = thisAjaxActionFailFunction;
	
	// Send the request
	ajaxActionXMLHttpObject = createXMLHttpRequestObject();
	ajaxActionXMLHttpObject.onreadystatechange = handlePerformAjaxActionStateChange;
	ajaxActionXMLHttpObject.open("GET",requestUri , true);
	ajaxActionXMLHttpObject.send(null);
}


// Handle the response from an ajax action call
function handlePerformAjaxActionStateChange() {

	if(ajaxActionXMLHttpObject.readyState == 4) {
		if (ajaxActionXMLHttpObject.status == 200) {
			
			// Load the response XML and populate the specified object with the data
			var ajaxActionResponseXML = loadResponseXML(ajaxActionXMLHttpObject.responseText);
			var ajaxActionResult = ajaxActionResponseXML.documentElement.getAttribute("status");
			
			// Call the appropriate function based on the result
			if (ajaxActionResult == "SUCCESS") {
				ajaxActionSuccessFunction(ajaxActionResponseXML);
			} else {
				ajaxActionFailFunction(ajaxActionResponseXML);
			}
		}
	}
}