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);