/*==============================================================
	Copyright 2006
	Dynamic-Tools.net
	Do not remove this header.

Script:			Dynamic-Tools AJAX Toolkit

Description:	This script simplifies sending AJAX requests and
				handling the responses to those requests.  The 
				requests are threadsafe so you can send off as
				many at a time as you like.

Author:			Peter Wilkinson

==============================================================*/

// send a threadsafe XMLHTTPRequest
function ajaxSend(method, url, parameters, responseHandler, asynchronous)
{
	if (typeof method != 'string')
	{
		alert("Invalid request method.");
		return false;
	}
	method = method.toUpperCase();
	if (method != 'POST' && method != 'GET')
	{
		alert("Invalid request method.");
		return false;
	}

	if (typeof parameters != 'string') parameters = '';

	if (typeof asynchronous == 'undefined') asynchronous = true;

	// build the request
	var ajaxRequest = null;
	if (window.XMLHttpRequest) ajaxRequest = new XMLHttpRequest();
	else if (window.ActiveXObject) ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");

	// bind the callback.
	function ajaxBindCallback()
	{
		if (ajaxRequest.readyState == 4)
		{
			if (ajaxRequest.status == 200 || ajaxRequest.status == 0) // is either valid XML or no
			{
				if (responseHandler) responseHandler(ajaxRequest);
			}
			else
			{
				if (ajaxRequest.status == 404)
				{
					alert("The requested service could not be found.");
				}
				else
				{
					alert("There was a problem retrieving the xml data:\n" + ajaxRequest.status + ":\t" + ajaxRequest.statusText + "\n" + ajaxRequest.responseText);
				}
			}
		}
	}		

	// send the request
	if (ajaxRequest)
	{
		if (asynchronous) ajaxRequest.onreadystatechange = ajaxBindCallback;
		if (method == "GET")
		{
			url = url + '?' + parameters
			parameters = null;
		}
		ajaxRequest.open(method, url, asynchronous);
		if (method == 'POST')
		{
			ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		}
		ajaxRequest.send(parameters);

		if (!asynchronous) ajaxBindCallback();
	}
	else alert("Unable to build XMLHttpRequest");
}

function getXMLEntries(xmlBranch, tagName)
{
	var tagList = xmlBranch.getElementsByTagName(tagName);
	var results = [];
	var length = tagList.length;
	for (var i=0; i < length; i++)
	{
		results.push(getXMLData(tagList[i]));
	}
	return results;
}

function getXMLData(xmlNode)
{
  return xmlNode.firstChild ? xmlNode.firstChild.data : '';
}