/* DO NOT EDIT. THIS IS A CACHE FILE AND WILL GET OVERWRITTEN RANDOMLY.
INSTEAD EDIT THE TEMPLATE FROM WITHIN THE ADMIN ZONE, OR BY MANUALLY EDITING A TEMPLATES_CUSTOM OVERRIDE. */

var AJAX_REQUESTS=[];
var AJAX_METHODS=[];
var AJAX_TIMEOUTS=[];
var AJAX_RESULT;

/* Calls up a URL to check something, giving any 'feedback' as an error (or if just 'false' then returning false with no message) */
function do_ajax_field_test(url)
{
	if (!ajax_supported()) return true;

	if (typeof window.keep_stub!='undefined') url=url+keep_stub();
	var xmlhttp=load_XML_doc(url);
	if ((xmlhttp.responseText!='') && (xmlhttp.responseText!=''))
	{
		if (xmlhttp.responseText!='false')
		{
			if (xmlhttp.responseText.length>1000)
			{
				var error_window=window.open();
				if (error_window)
				{
					error_window.document.write(xmlhttp.responseText);
					error_window.document.close();
				}
			} else
			{
				window.alert(xmlhttp.responseText);
			}
		}
		return false;
	}
	return true;
}

function ajax_supported()
{
	// Intentionally not a single line, to help validator
	if ((window.XMLHttpRequest) || (window.ActiveXObject)) return true;
	return false;
}

function load_XML_doc(url,method,post) // Note: 'post' is not an array, it's a string (a=b)
{
	var synchronous=!method;

	if ((url.indexOf('://')==-1) && (url.substr(0,1)=='/'))
	{
		url=window.location.protocol+'//'+window.location.host+url;
	}

	var index=AJAX_REQUESTS.length;
	AJAX_METHODS[index]=method;
	if (typeof window.XMLHttpRequest!='undefined')
	{
		// Branch for none-IE
		AJAX_REQUESTS[index]=new XMLHttpRequest();
		if (!synchronous) AJAX_REQUESTS[index].onreadystatechange=process_request_changes;
		if (post)
		{
			AJAX_REQUESTS[index].open('POST',url,!synchronous);
			AJAX_REQUESTS[index].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			AJAX_REQUESTS[index].send(post);
		} else
		{
			AJAX_REQUESTS[index].open("GET",url,!synchronous);
			AJAX_REQUESTS[index].send(null);
		}
	}
	else if (typeof window.ActiveXObject!='undefined')
	{
		// Branch for IE
		AJAX_REQUESTS[index]=new ActiveXObject("Microsoft.XMLHTTP");
		if (AJAX_REQUESTS[index])
		{
			if (!synchronous) AJAX_REQUESTS[index].onreadystatechange=process_request_changes;
			if (post)
			{
				AJAX_REQUESTS[index].open('POST',url,!synchronous);
				AJAX_REQUESTS[index].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				AJAX_REQUESTS[index].send(post);
			} else
			{
				AJAX_REQUESTS[index].open("GET",url,!synchronous);
				AJAX_REQUESTS[index].send();
			}
		}
	}

	if ((!window.AJAX_REQUESTS) || (typeof window.AJAX_REQUESTS=="undefined")) return null; // Probably the page is in process of being navigated away so window object is gone
	var result=AJAX_REQUESTS[index];
	if (synchronous)
	{
		AJAX_REQUESTS[index]=null;
	}
	return result;
}

function process_request_changes()
{
	if (!AJAX_REQUESTS) return; // Probably the page is in process of being navigated away so window object is gone

	// If any AJAX_REQUESTS are 'complete'
	var i,ajax_request;
	for (i=0;i<AJAX_REQUESTS.length;i++)
	{
		ajax_request=AJAX_REQUESTS[i];
		if ((ajax_request!=null) && (ajax_request.readyState) && (ajax_request.readyState==4))
		{
			AJAX_REQUESTS[i]=null;

			// If status is 'OK'
			if ((ajax_request.status) && (ajax_request.status==200))
			{
				//Process the result
				var xml=handle_errors_in_result(ajax_request);
				if (xml)
				{
					xml.validateOnParse=false;
					var response=xml.documentElement;
					if (!response) response=xml;
					process_request_change(response,i);
				}
			}
			else
			{
				try
				{
					window.alert("There was a problem retrieving the XML data:\n"+ajax_request.status+": "+ajax_request.statusText+".");
				}
				catch (e)
				{
					//window.alert("There was a problem retrieving the XML data:");		This is probably clicking back
				}
			}
		}
	}
}

function handle_errors_in_result(request)
{
	if ((request.responseXML==null) || (request.responseXML.childNodes.length==0))
	{
		// Try and parse again. Firefox can be weird.
		var xml;
		if (typeof DOMParser!="undefined") xml=(new DOMParser()).parseFromString(request.responseText,"application/xml");
		else {
			var ieDOM=["MSXML2.DOMDocument","MSXML.DOMDocument","Microsoft.XMLDOM"];
			for (var i=0;i<ieDOM.length && !xml;i++) {
				try { xml=new ActiveXObject(ieDOM[i]);xml.loadXML(request.responseText); }
				catch(e) {}
			}
		}
		if (xml) return xml;

		if ((request.responseText) && (request.responseText!='')/* && (!request.synchronous)*/)
		{
			if (typeof console.debug!='undefined') console.debug(request);

			var error_window=window.open();
			if (error_window)
			{
				error_window.document.write(request.responseText);
				error_window.document.close();
			}
		}
		return false;
	}
	return request.responseXML;
}

function process_request_change(response,i)
{
	if (!response) return; // Needed for Opera
	if (!AJAX_REQUESTS) return; // Probably the page is in process of being navigated away so window object is gone

	if (response.getElementsByTagName("message")[0])
	{
		//Either an error or a message was returned. :(
		var message=response.getElementsByTagName("message")[0].firstChild.data;

		if (response.getElementsByTagName("error")[0])
		{
			//It's an error :|
			window.alert("An error ("+response.getElementsByTagName("error")[0].firstChild.data+") message was returned by the server: "+message);
			return;
		}

		window.alert("An informational message was returned by the server: "+message);
		return;
	}

	AJAX_RESULT=response.getElementsByTagName("result")[0];

	if ((response.getElementsByTagName("method")[0]) || (AJAX_METHODS[i]))
	{
		var method=(response.getElementsByTagName("method")[0])?eval('return '+response.getElementsByTagName("method")[0].firstChild.data):AJAX_METHODS[i];

		if (typeof method.response!='undefined') method.response(response);
		else method(response);

	}// else window.alert("Method required: as it is non-blocking");
}

function create_xml_doc()
{
	var xml_doc;

	if (typeof window.ActiveXObject!='undefined')
	{
		xml_doc=new ActiveXObject("Microsoft.XMLDOM");
	}
	else if (document.implementation && document.implementation.createDocument)
	{
	  xml_doc=document.implementation.createDocument("","",null);
	}
	return xml_doc;
}

function merge_text_nodes(childNodes)
{
	var i,text='';
	for (i=0;i<childNodes.length;i++)
	{
		if (childNodes[i].nodeName=='#text')
		{
			text+=childNodes[i].data;
		}
	}
	return text;
}


