var temp_ajax = null;

function Ajax(response_function, response_function_params) 
{
	this.response_function_params = "";

	if (response_function)
	{
		this.response_function = eval(response_function);

		if (response_function_params)
			this.response_function_params = response_function_params;
	}
	else
		this.response_function = function() { return true; }

	this.request = null;
	this.displayed_error = false;
	this.responseText2 = "";
}

Ajax.prototype.MakeRequest = function(url,params)
{
	var A = null;

	if (typeof(ActiveXObject) == "function")
	{
		try 
		{
			A = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e)
		{
			try 
			{
				A = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (oc) 
			{
				if (typeof(XMLHttpRequest) != "function")
				{
					XMLHttpRequest = function()
					{
						this.i = 0;
						this.url = '';
						this.responseText = '';
						this.iframe = '';
						this.delay = 2;

						this.onreadystatechange = function()
						{
							return false;
						}

						this.open = function(method,url)
						{
							//TODO: POST methods
							//this.i = ++kXHR_instances; // id number of this request
							this.url = url;
							var iframe = document.getElementById('ajax_iframe');
							if (!iframe)
								document.body.appendChild(document.createElement('<iframe id="ajax_iframe" style="display:none" src="/"></iframe>'));
							else /*if (iframe.tagName != "IFRAME")*/
								iframe.outerHTML = '<iframe id="ajax_iframe" style="display:none" src="/"></iframe>'
						}

						this.send = function(postdata)
						{
							//TODO: use the postdata
							document.getElementById('ajax_iframe').src = this.url;
							//kXHR_objs[this.i]=this;
							temp_ajax = this;
							setTimeout(this.checkState, this.delay);
						}

						this.checkState = function()
						{
							var el = document.getElementById('ajax_iframe');

							if (el.readyState == 'complete')
							{
	//							var responseText=document.frames['kXHR_iframe_'+inst].document.body.innerText;
//								var responseText = el.document.body.innerText;
								var responseText = document.frames['ajax_iframe'].document.body.innerHTML;

								temp_ajax.responseText = responseText;
								temp_ajax.readyState = 4;
								temp_ajax.status = 200;
								temp_ajax.onreadystatechange();
								el.parentNode.removeChild(el);
								temp_ajax = null;
							}
							else
							{
								temp_ajax.delay*=1.5;
								setTimeout(temp_ajax.checkState, temp_ajax.delay);
							}
						}
						return true;
					}
				}
			}
		}
	}



	if (!A && typeof XMLHttpRequest != "undefined")
		A = new XMLHttpRequest();

	if (A)
		this.request = A;


	/*if (window.XMLHttpRequest)
		this.request = new XMLHttpRequest();
	else if (window.ActiveXObject)
		this.request = new ActiveXObject("MSXML2.XMLHTTP");*/

	return this.SendRequest(url,params);	
}

Ajax.prototype.SendRequest = function(url,params)
{
	if (this.request)
	{
		this.request.onreadystatechange = this.response_function;//(this.response_function_params);

		if (params) {
		
		this.request.open("POST", url, true);
		this.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.request.setRequestHeader("Content-length", params.length);
		this.request.setRequestHeader("Connection", "close");


		} else {
		
		this.request.open("GET", url, true);
		params = '';

		}

		this.request.send(params);


		return true;
	}
	else if (!this.displayed_error)
	{
		this.displayed_error = true;
		alert("AJAX object could not be created!");
	}

	return false;
}

Ajax.prototype.CheckReadyState = function(obj) 
{
	if (obj.readyState == 0)
		return "Initialising...";	// unitialised
	else if (obj.readyState == 1)
		return "Loading...";
	/*else if (obj.readyState == 2)
		return "Loaded...";			// interactive*/
	else if (obj.readyState == 3)
		return "Waiting...";
	else if (obj.readyState == 4)	// complete
	{
		if (obj.status == 200) 
			return true; // flly loaded
		else
		{
			return true;
			return "There was a problem retrieving the XML!";
		}
	}

	return "Waiting...";
}

Ajax.prototype.FillList = function(list, status)
{

	var return_array = new Array();

	// try and get the SELECT element
		if (list)
		{
			if (typeof(list) == "string")
				list = document.getElementById(list); 
		}

	// try and get the STATUS element, if it's been passed over
		if (status)
		{
			if (typeof(status) == "string")
				status = document.getElementById(status); 
		}

	var is_ready = this.CheckReadyState(this.request);

	if (is_ready == true)
	{
		// Clear List
			while (list.length > 0 && list.options)
				list.options[0] = null;

		if (this.responseText2)
			var responseText = this.responseText2;
		else
			var responseText = this.request.responseText;

		this.responseText2 = "";


		if (responseText == "")
		{
			if (status)
				status.innerHTML = "0 results";
		}
		else
		{
			// remove trailing !AJAXDELIM!
				responseText = responseText.replace(/(.*)!AJAXDELIM!$/,"$1");

			var responseResults = responseText.split("!AJAXDELIM!");

			if (status)
				status.innerHTML = responseResults.length + " result(s)";	

			var line;
			var arr_index;

			for (var i = 0; i < responseResults.length; i++)
			{
				if (responseResults[i])
				{
					line = responseResults[i].split("|");
					arr_index = list.length;

					list.options[arr_index] = new Option(line[1], line[0]);

					// if we have a 3rd parameter
						if (line[2])
						{
							return_array[arr_index] = line[2];
						}
				}
			}
		}
	}
	else if (status)
		status.innerHTML = is_ready;

	return return_array;
}

function createAjax() {

	var ajax = false;

	if (window.XMLHttpRequest)
	{
		ajax = new XMLHttpRequest();
	} else if (window.ActiveXObject)
	{
		ajax = new ActiveXObject("Microsoft.XMLHTTP");
	}
return ajax;
}

function isAjaxReady(ajax) {
if (ajax.readyState == 4) {
return true;
} else {
return false;
}
}
