
var global_ajax_requests_array = new Array();

function ExamplePost(xml_http_obj, g_current_general_url)
{
	var post_parameter_array = Array();
	post_parameter_array[0] = Array("example_key_1","example_data_1");
	post_parameter_array[1] = Array("example_key_2","example_data_2");
	post_parameter_array[2] = Array("example_key_3","example_data_3");

	AjaxRequest(g_current_general_url+"param=ajax_request&", ExamplePost_Handler, post_parameter_array);
}
function ExamplePost_Handler(xml_http_obj)
{
	if(xml_http_obj.status == 200)
	{
		//document.getElementById('ajax_output_area').innerHTML += xml_http_obj.responseText + "<br />";
		alert("POST result: "+xml_http_obj.responseText);
	}
	else
	{
		alert("There was a problem retrieving the HTTPRequestHandler data:" + xml_http_obj.statusText);
	}
}

function ExampleGet()
{
	AjaxRequest(g_current_general_url+"param=ajax_request&example_key_1=example_data_1&", ExampleGet_Handler);
}
function ExampleGet_Handler(xml_http_obj)
{
	if(xml_http_obj.status == 200)
	{
		//document.getElementById('ajax_output_area').innerHTML += xml_http_obj.responseText + "<br />";
		alert("GET result: "+xml_http_obj.responseText);
	}
	else
	{
		alert("There was a problem retrieving the HTTPRequestHandler data:" + xml_http_obj.statusText);
	}
}




function AjaxRequestTypeClass(freed)
{
	this.freed = freed;
	this.xml_http_obj = false;
	if(window.XMLHttpRequest)
	{
		this.xml_http_obj = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		this.xml_http_obj = new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function AjaxRequest(url, handler_function, post_data_array)
{
	var request_type;
	if(post_data_array == null)
	{
		request_type = "GET";
	}
	else
	{
		request_type = "POST";
	}

	var pos = -1;
	for(var i=0; i<global_ajax_requests_array.length; i++)
	{
		if(global_ajax_requests_array[i].freed == 1)
		{
			pos = i;
			break;
		}
	}
	if(pos == -1)
	{
		pos = global_ajax_requests_array.length;
		global_ajax_requests_array[pos] = new AjaxRequestTypeClass(1);
	}
	if(global_ajax_requests_array[pos].xml_http_obj)
	{

		global_ajax_requests_array[pos].freed = 0;
		global_ajax_requests_array[pos].xml_http_obj.open(request_type,url,true);
		global_ajax_requests_array[pos].xml_http_obj.onreadystatechange = function()
		{
			if(typeof(xmlhttpChange) != 'undefined')
			{
				xmlhttpChange(pos, handler_function);
			}
		}

		if(request_type == "GET")
		{
			if(window.XMLHttpRequest)
			{
				global_ajax_requests_array[pos].xml_http_obj.send(null);
			}
			else if(window.ActiveXObject)
			{
				global_ajax_requests_array[pos].xml_http_obj.send();
			}
		}
		else
		{
			global_ajax_requests_array[pos].xml_http_obj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			global_ajax_requests_array[pos].xml_http_obj.send(ConvertParameterArrayToURLEncodedString(post_data_array));
		}
	}
}

function xmlhttpChange(pos, handler_function)
{
	DisplayReadyState((global_ajax_requests_array[pos].xml_http_obj.readyState==4?null:global_ajax_requests_array[pos].xml_http_obj.readyState));
	if(typeof(global_ajax_requests_array[pos]) != 'undefined' && global_ajax_requests_array[pos].freed == 0 && global_ajax_requests_array[pos].xml_http_obj.readyState == 4)
	{
		handler_function(global_ajax_requests_array[pos].xml_http_obj)
		global_ajax_requests_array[pos].freed = 1;
	}
}

function DefaultHandlerFunction(xml_http_obj)
{
	if(xml_http_obj.status == 200)
	{
		alert(xml_http_obj.responseText);
	}
	else
	{
		alert("There was a problem retrieving the HTTPRequestHandler data:" + xml_http_obj.statusText + "("+xml_http_obj.status+")");
	}
}
