//	-------------------------------------
//	Global Vars
//	-------------------------------------

var timeout	= 5000;	// Use this to set the timeout interval on AJAX requests.

//	-------------------------------------
//	Submit Form
//	-------------------------------------

function submit_form( to, from, loading )
{
	var target		= $(to);
	var spinner		= $(loading);
	var theform		= $(from);
	var act			= theform.elements['ACT'].value;
	var url			= theform.action + '?ACT=' + act;
	var pars		= Form.serialize(theform);
	
	if ( ! theform.elements['template'] )
	{
		target.innerHTML	= 'Unable to find a template to use for output formatting.';
		return false;
	}
	else
	{
		url	= theform.elements['template'].value;
	}
	
	//	-------------------------------------
	//	Prepare timeout handler
	//	-------------------------------------
	
	function callInProgress (xmlhttp)
	{
		switch (xmlhttp.readyState)
		{	
			case 1: case 2: case 3:	
				return true;	
			break;
	
			// Case 4 and 0	
			default:	
				return false;	
			break;	
		}	
	}
	
	//	-------------------------------------
	//	Register global responders that will
	//	occur on all AJAX requests.
	//	-------------------------------------

	Ajax.Responders.register({
		onCreate: function(request)
		{
			request['timeoutId'] = window.setTimeout(
	
				function()
				{
					//	-------------------------------------
					//	If we have hit the timeout and the
					//	AJAX request is active, abort it
					//	and let the user know.
					//	-------------------------------------
	
					if (callInProgress(request.transport))
					{	
						request.transport.abort();
						
						//	-------------------------------------
						//	Run the onFailure method if we set
						//	one up when creating the AJAX object.
						//	-------------------------------------	
	
						if (request.options['onFailure'])
						{	
							request.options['onFailure'](request.transport, request.json);	
						}	
					}	
				},	
				timeout
			);	
		},
	
		onComplete: function(request)
		{
			//	-------------------------------------
			//	Clear the timeout, the request
			//	completed ok.
			//	-------------------------------------
			
			window.clearTimeout(request['timeoutId']);
		}	
	});
	
	//	-------------------------------------
	//	Show loading.
	//	-------------------------------------
	
	Element.hide( spinner );
	Element.toggle( spinner );
	
	//	-------------------------------------
	//	Remove contents from target.
	//	-------------------------------------
	
	target.innerHTML	= '';
	Element.hide( target );
	
	//	-------------------------------------
	//	Execute AJAX
	//	-------------------------------------
	
	var	aj			= new Ajax.Request( url, { method: 'post', postBody: pars, onSuccess: put, onFailure: fail, onException: exception } );

	function put(t)
	{
		//	Hide loader
		Element.hide(spinner);
		
		//	Place list
		var output				= t.responseText;
		Element.show( target );
		target.innerHTML		= output;
		
		Element.hide( theform );
	}
	
	function fail()
	{
		//	Show slow server message
		Element.hide(spinner);
		Element.show( target );
		target.innerHTML	= 'The server is currently under heavy load. Please try later.';
	}
	
	function exception(t)
	{
		//	Show exception message
		Element.hide( spinner );
		Element.show( target );
		target.innerHTML	= 'A server exception occurred. Please try later.';
	}
}

//	End submit_form


//	-------------------------------------
//	Print_r utility function for testing
//	-------------------------------------

function print_r(theObj)
{
	var out	= '';
	
	if (theObj.constructor == Array || theObj.constructor == Object)
	{		
		for (var p in theObj)
		{
			if (theObj[p].constructor == Array || theObj[p].constructor == Object)
			{
				out	= out + "- ["+p+"] => "+typeof(theObj) + "\n";
				out	= out + "\n";
				print_r(theObj[p]);
				out	= out + "\n";
			}
			else
			{
				out	= out + "- ["+p+"] => "+theObj[p] + "\n";
			}
		}
	}
	
	return out;
}

//	End print_r