var objAjax = {
	init: function() {},
	
	createXmlHttpRequestObject: function() {
		var xhr;
		// this should work for all browsers except IE6 and older
		try {
			// try to create a new instance of XMLHttpRequest object
			xhr = new XMLHttpRequest();
		} catch(e) {
			// assume IE6 or older
			var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
											'MSXML2.XMLHTTP.5.0',
											'MSXML2.XMLHTTP.4.0',
											'MSXML2.XMLHTTP.3.0',
											'MSXML2.XMLHTTP',
											'Microsoft.XMLHTTP');
			// try every prog id until one works
			for (var i=0; i<XmlHttpVersions.length && !xhr; i++) {
				try { 
					// try to create a new instance of  XMLHttpRequest object
					xhr = new ActiveXObject(XmlHttpVersions[i]);
				} catch (e) {
					//cannot instantiate the object
					xhr = false;
				}
			}
		}
		// return the created object or display an error message
		if (!xhr) {
			alert("Error creating the XMLHttpRequest object.");
		} else { 
			return xhr;
		}
	},	
	
	getQueryString: function(formId){
		var sQueryString = "";
		//get the values entered into the search form fields
		for (var i=0;i<document.forms[formId].length;i++)
		{
			oInput = document.forms[formId].elements[i];
			//if the form fields are not the search and reset buttons
			if ((oInput.id != 'filter-search') && (oInput.id != 'filter-reset') && (oInput.id != 'submit'))
			{
				sQueryString += '&'+oInput.name+'='+escape(oInput.value);
			}
		}
		return sQueryString;
		},
		
	filterDataSet: function(formId, methodpath, contentDivID){
		
		
		if (contentDivID)
		{
			var contentWrapper = contentDivID;
		}
		else
		{
			var contentWrapper = "content-list-wrap";
		}
		
		//alert(contentWrapper);
		
		var xhr = objAjax.createXmlHttpRequestObject();
		
		if (xhr != null && xhr)
		{
			// url where process will be actioned
			xhr.open("GET",methodpath + objAjax.getQueryString(formId), true);
			//reference to the event
			xhr.onreadystatechange = function()
			{
				objAjax.preLoading();
				//if the XMLHttp ready state is 'complete'
				if (xhr.readyState == 4)
				{
					//if the XMLHttp status is 'OK' or 'Not Modified'
					if (xhr.status == 200 || xhr.status == 304)
					{
						//return XMLHttpRequest response as Text
						if (xhr.responseText)
						{
							objAjax.reloadDataSet(xhr.responseText,contentWrapper);
						}
					} 
					else
					{
						objAjax.errorLoading(xhr);
					}
					objAjax.postLoading();
				}
			};
			xhr.send(null);
			return false;
		}
		return true;
				
	},
	
	reloadDataSet: function(response,contentWrapper){
		document.getElementById(contentWrapper).innerHTML = response;
	},
	

	preLoading: function()
	{
		//document.getElementById("disabledZone").style.visibility = 'visible';
		document.getElementById("ajaxLoading").style.visibility = 'visible';
		document.getElementById("ajaxError").style.display = 'none';
	},
	postLoading: function()
	{
		
		document.getElementById("ajaxLoading").style.visibility = "hidden";
		//alert(document.getElementById("ajaxLoading").style.visibility );
	},
	errorLoading: function(xhr)
	{
		document.getElementById("ajaxError").style.display = 'block';
		document.getElementById("ajaxError").innerHTML = "There has been an error. Status Code: " + xhr.status + " Status Text: " + xhr.statusText + ". Please refresh page.";
	}
}

addLoadEvent(objAjax.init);


// threadsafe asynchronous XMLHTTPRequest code
function ajaxSend(url, divid){

	// we use a javascript feature here called "inner functions"
	// using these means the local variables retain their values after the outer function
	// has returned. this is useful for thread safety, so
	// reassigning the onreadystatechange function doesn't stomp over earlier requests.
		
	function ajaxBindCallback(){
		if (ajaxRequest.readyState == 4) {
			if (ajaxRequest.status == 200) {
				document.getElementById(divid).innerHTML = ajaxRequest.responseText;
			}
		}
	}

	// use a local variable to hold our request and callback until the inner function is called...
	var ajaxRequest = null;
	
	// bind our callback then hit the server...
	if (window.XMLHttpRequest) {
		// moz et al
		ajaxRequest = new XMLHttpRequest();
		ajaxRequest.onreadystatechange = ajaxBindCallback;
		ajaxRequest.open("GET", url, true);
		ajaxRequest.send(null);
	} else if (window.ActiveXObject) {
		// ie
		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		if (ajaxRequest) {
			ajaxRequest.onreadystatechange = ajaxBindCallback;
			ajaxRequest.open("GET", url, true);
			ajaxRequest.send();
		}
	}
}

function saveOrderNotes(divid, notes, ordid, url){

	var ajaxurl = url + '&notes=' + escape(notes) + '&orderid=' + ordid;
	ajaxSend(ajaxurl, divid);
}