// A cheap custom AJAX implementation by Pap.

// Objective: Creates an XMLHTTPRequest for AJAX.
// Arguments: None.
// Returns:   XMLHTTPRequest object.
function createXMLHttpRequest()
{
	var oXMLHTTPRequest = null;

	var XML_MS_PROGIDS = new Array("Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP");

	if (window.XMLHttpRequest != null)
	{
		// Mozilla, Safari, ...
		oXMLHTTPRequest = new window.XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		var success = false;
		for (var i = 0; i < XML_MS_PROGIDS.length && !success; i++)
		{
			try
			{
				oXMLHTTPRequest = new ActiveXObject(XML_MS_PROGIDS[i]);
				success = true;
			}
			catch (e)
			{}
		}
	}

	if (oXMLHTTPRequest == null) alert("Error in HttpRequest():\n\nCannot create an XMLHttpRequest object.");
	return oXMLHTTPRequest;
}

// Objective: Sends a request to the given URL.
// Arguments: - sURL, string; URL to send request to.
//			  - sMethod, string; optional method for the request, usually GET or POST. Defaults to GET.
//			  - sData, string; optional data to send. Defaults to an empty string.
//			  - myObj, a class; optional, if passed, must be an instance of an object implementing the handleResponse method.
function makeRequest(sURL, sMethod, sData, myObj)
{
	// Request method defaults to GET:
	if (!sMethod) sMethod = "GET"

	var bIsPost = (sMethod == "POST");
	
	var oXMLHTTPRequest = createXMLHttpRequest();

    if (oXMLHTTPRequest)
    {	
		with (oXMLHTTPRequest)
		{
			if (myObj)
			{
				onreadystatechange = function() { myObj.handleResponse(oXMLHTTPRequest, bIsPost); };
			}
			else
			{
				onreadystatechange = function() { handleResponse(oXMLHTTPRequest, bIsPost); };
			}
			open(sMethod, sURL, true);

			if (bIsPost)
			{
				setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				setRequestHeader("Content-length", sData.length);
				setRequestHeader("Connection", "close");
			}

			send(sData);
		}
    }
}