/////////////////////////////////////////////////////////////////////////////////
// Simple AJAX API as implemented by Allen Learning Portal
// Copyright (c) Allen Communication Learning Services 2006

/////////////////////////////////////////////////////////////////////////////
//	Create an XMLHttpRequest object.
function CreateXMLHttpRequest()
{
	var		httpRequest = null;
	
	// Check for native XMLHttpRequest object support
	if (typeof(XMLHttpRequest) != 'undefined')
	{ 
		httpRequest = new XMLHttpRequest();
	}
	else if (typeof(ActiveXObject) != 'undefined')
	{
		// Use ActiveX control implementation IE 6.0 and older
		try
		{ 
			httpRequest = new ActiveXObject('Microsoft.XMLHTTP'); 
		}
		catch (e) { } 
	}	
	return httpRequest;
}

/////////////////////////////////////////////////////////////////////////////
// Check to see if the XMLHttpRequest object is supported by the browser.
function IsXMLHttpRequestAvailable()
{
	var		httpRequest = CreateXMLHttpRequest();
	if (httpRequest != null)
		return true;
	return false;
}

/////////////////////////////////////////////////////////////////////////////
//	Use HttpRequest object to get XML from a specific URL.
function ExecuteXMLHttpRequestGET(url, handler, bAsync)
{
	var		httpRequest = CreateXMLHttpRequest();
	if (httpRequest != null)
	{
		// Assign readystate to call handler function and pass XMLHttpRequest as parameter
		if (handler)
			httpRequest.onreadystatechange = function() { handler(httpRequest); };
		httpRequest.open("GET", url, bAsync);
		httpRequest.send(null);

		if (handler == null && bAsync == false)
			return httpRequest.responseText;
		
		return true;
	}
	return false;
}

/////////////////////////////////////////////////////////////////////////////
//	Use HttpRequest object to post content to a specific URL.
function ExecuteXMLHttpRequestPOST(url, data, bAsync)
{
	var		httpRequest = CreateXMLHttpRequest();
	if (httpRequest != null)
	{
		httpRequest.open("POST", url, bAsync);
		httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		httpRequest.send(data);
		
		if (bAsync == false)
			return httpRequest.responseText;
	
		return true;
	}
	return false;
}

