//<![CDATA[
//	Copyright: Well-connected Ltd (http://www.well-connected.net/), 2012. All rights reserved.

var s_target	= "";
var s_url	= "";
var s_query	= "";
var s_formname	= "";

/* ---------------------------------------------------------------------------- */
function ajax_request () {
/* ----------------------------------------------------------------------------

	During an Ajax request, a few properties on the returned object inform of the status of the request.

	"readyState" keeps track of the current stage of the request by returning an integer:
		0: uninitialized
		1: loading
		2: loaded
		3: interactive
		4: complete

	"status" returns the HTTP status code of the request:
		200: OK
		301: Moved permanently
		302: Found
		303: See Other
		403: Forbidden
		404: Not Found

	"statusText" returns a string describing the status of the request.
*/
//	Check for MS IE, whatever version
	var activexmodes = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]	//	ActiveX versions in IE
	if ( window.ActiveXObject ) {
//		Check support for ActiveXObject in IE first, as XMLHttpRequest is broken in IE7
		for ( var i = 0; i < activexmodes.length; i++ ) {
			try { return new ActiveXObject ( activexmodes[i] ) }
			catch ( e ) { x = 0; } // do nothing, i.e. ignore error
		}
		return false;
	}
	else if ( window.XMLHttpRequest ) {
//		M o d e r n  browsers like Firefox, Chrome, Safari, and Opera are so easy!
		return new XMLHttpRequest ();
	}
	else {
		return false;
	}
}

/* ---------------------------------------------------------------------------- */
function ajax_get () {
/* ----------------------------------------------------------------------------

	An Ajax GET request encompasses the following steps (the order is important):

	1	Define an ajax_request.onreadystatechange event handler, which points to a function reference that
		will fire during each stage of the Ajax request. It uses the Ajax object's "readyState" and
		"status" properties to determine when the request is complete, before handling the returned data.

	2	Call ajax_request.open ( "GET", s_url + s_query, true ) with 3 parameters:
		->	The method "GET".
		->	The URL of the request (INCLUDING a URI query string of parameters).
			Use encodeURIComponent() to encode special characters that might occur in parameter values.
		->	true.

	3	Call ajax_request.send ( null ) with null as the single parameter.

	The returned result can be plaintext/html, text/json, or text/xml
	To return the result in XML or JSON, call the method overrideMimeType ().
*/
	if ( get_parameters ( arguments ) ) {
		var a_request = new ajax_request ();
		if ( ajax_request ) {
			a_request.onreadystatechange = function () { statechange_handler ( a_request ); }
			a_request.open ( "GET", s_url + ( s_query > " " ? "?" : "" ) + s_query, true );
			a_request.overrideMimeType ( "text/json" );
			a_request.send ( null );
		}
	}
}

/* ---------------------------------------------------------------------------- */
function ajax_post () {
/* ----------------------------------------------------------------------------

	An Ajax POST request encompasses the following steps (the order is important):

	1	Define an ajax_request.onreadystatechange event handler, which points to a function reference
		that will fire during each stage of the Ajax request. It uses the Ajax object's "readyState"
		and "status" properties to determine when the request is complete, before handling the returned data.

	2	Call ajax_request.open ( "POST", s_url, true ) with 3 parameters:
		->	The method "POST".
		->	The URL of the request (WITHOUT any parameters).
		->	true.

	3	Call ajax_request.setRequestHeader() and set its content type to "application/x-www-form-urlencoded".
		This is needed for any POST request made via Ajax.

	4	Call ajax_request.send ( s_query ) passing a URI query string of parameters that will be sent
		(without the "?" prefix). Use encodeURIComponent() to encode special characters that might occur
		in parameter values.

	The returned result can be plaintext/html, text/json, or text/xml
	To return the result in XML or JSON, call the method overrideMimeType ().
*/
	if ( get_parameters ( arguments ) ) {
		var a_request = new ajax_request ();
		if ( ajax_request ) {
			a_request.onreadystatechange = function () { statechange_handler ( a_request ); }
			a_request.open ( "POST", s_url, true );
			a_request.setRequestHeader ( "Content-type", "application/x-www-form-urlencoded" );
			a_request.overrideMimeType ( "text/json" );
			a_request.send ( s_query );
		}
	}
}

/* ---------------------------------------------------------------------------- */
function get_parameters ( a_args ) {
/* ----------------------------------------------------------------------------

	ajax_get () and ajax_post () must both be called with at least 2 parameters:
	1	The name of the target element that is to receive the result of the Ajax request.
	2	The url of the server script to be called.
	3	For ajax_post (), the name of the form containing input fields to be passed to the server script.
*/
	s_target	= a_args[0];		// 1st parameter: target div
	s_url		= a_args[1];		// 2nd parameter: server url

	if ( a_args.length == 2 )		// ajax_get
		return true;

	else if ( a_args.length > 2 ) {		// ajax_post
		s_formname = a_args[2];		// 3rd parameter: form name

		f = document.forms[s_formname];
		for ( i = 0; i < f.elements.length; i++ )
		{
			fld = f.elements[i];
			s_query += "&" + ( fld.id > " " ? fld.id : fld.name ) + "=" + encodeURIComponent ( fld.value );
		}
		return true;
	}
	else {
		alert ( "System error: Invalid number of parameters for ajax call ()" );
		return false;
	}
}

/* ---------------------------------------------------------------------------- */
function statechange_handler ( a_request ) {
/* ----------------------------------------------------------------------------
	Fires during each stage of an Ajax request, using the Ajax object's "readyState" and "status" properties
	to determine when the request is complete and it is therefore safe to handle any returned data.
*/
	if ( a_request ) {
		if ( a_request.readyState == 4 ) {
			if ( a_request.status == 200 || window.location.href.indexOf ( "http" ) == -1 ) {
//				Populate target element with response
				document.getElementById ( s_target ).innerHTML = a_request.responseText;
			}
			else {
				alert ( "System error: Error in ajax statechange handler\n" +
					"Target: " + s_target + "\n" +
					"URL:    " + s_url + "\n" +
					"Query:  " + s_query + "\n" +
					"Status: " + a_request.status + " " + a_request.statusText + "\n"
				);
			}
		}
	} else {
		alert ( "System error: Error in ajax statechange handler" );
	}
}

//]]>

