/*************************************
	AJAX Functions

	General AJAX related functionality
*************************************/

// Initialises an AJAX object for use
function ajaxInit()
{
	var xmlHttp;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}




// Submits a form to the specified web page using POST and resets the form. You
// may optionally supply a function pointer as a third parameter to act as a
// callback function for the result of the submission. This function should take
// a single argument, which will be the text returned from the submission page.
function ajaxSubmitPOST ( formId, submitUrl, resultFunction, resultFunctionParam )
{
	// Create content string for POST
	var postString = "";
	var form = document.getElementById(formId);
	for ( i = 0; i < form.elements.length; i++ )
	{
		postString = postString + form.elements[i].name + "=" + encodeURIComponent(form.elements[i].value) + "&";
	}

	// Return if post string is empty
	if ( postString.length == 0 )
		return '(ajaxSubmitPOST) Error: Form ID ' + formId + ' not found.';

	// Else trim trailing &
	else
		postString = postString.substr ( 0, postString.length-1 );



	// Reset the form
	form.reset();




	// Initialise AJAX
	var xmlHttp = ajaxInit();

	// Create update function
	xmlHttp.onreadystatechange=function()
	{
		if( xmlHttp.readyState == 4 )
		{
			// If a callback function was provided then call it
			if ( typeof resultFunction == "function" )
			{
				if ( typeof resultFunctionParam == "undefined" )
				{
					resultFunction ( xmlHttp.responseText );
				}
				else
				{
					resultFunction ( xmlHttp.responseText, resultFunctionParam );
				}
			}
		}
	}

	xmlHttp.open( "POST", submitUrl, true );
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", postString.length);
	xmlHttp.setRequestHeader("Contennection", "close");
	xmlHttp.send( postString );
}




/* Opens the specified URL using AJAX. Useful for processing admin actions which
can be handled by another script without reloading the page. You can optionally
provide a callback funcion which will be called upon the successful opening of
the url, which will recieve the content of the loaded page and, optionally, a
second parameter which you have defined.

Parameters;
	url						Required	Address to open
	resultFunction			Optional	Name of a callback function to call after
											successfully opening the url
	resultFunctionParam		Optional	A value to pass as the second parameter to
											the callback function
*/
function ajaxOpen ( url, resultFunction, resultFunctionParam )
{
	// Initialise AJAX
	var xmlHttp = ajaxInit();

	// Create update function
	xmlHttp.onreadystatechange=function()
	{
		if( xmlHttp.readyState == 4 )
		{
			// If a callback function was provided then call it
			if ( typeof resultFunction == "function" )
			{
				if ( typeof resultFunctionParam == "undefined" )
				{
					resultFunction ( xmlHttp.responseText );
				}
				else
				{
					resultFunction ( xmlHttp.responseText, resultFunctionParam );
				}
			}
		}
	}

	xmlHttp.open( "GET", url, true );
	xmlHttp.send( null );
}




/* Refreshes the inner_html property of the given page element using the content
loaded from the given URL at the specified interval, in seconds. This script does
not support cancelling or modifying the regular updating, so only use when these
actions will not be required.

Parameters;
	url						Required	Address to load content from
	elementId				Required	DOM ID of the element to load content into
	interval				Required	Interval, in seconds, between each update
*/
function ajaxRefreshElement ( url, elementId, interval )
{
	ajaxOpen ( url, ajaxRefreshElementCallback, elementId );
	setTimeout( 'ajaxRefreshElement( "' + url + '", "' + elementId + '", ' + interval + ' )', interval * 1000 );
}

/* Callback for the above function */
function ajaxRefreshElementCallback ( pageContent, elementId )
{
	document.getElementById(elementId).innerHTML = pageContent;
}