/****************************************************************
* Generic AJAX functions for reading an XML file and returning
* a result. The XML must be formatted as follows:
* 
* <?xml version='1.0' encoding='ISO-8859-1' standalone='yes' ?>
* <response>
*    <method>getItems</method>
*    <result>...XML Result Content...</result>
* </response>
*
* Note: Replace 'getItems' with the name of your javascript method
* 
*****************************************************************/

var req; // global request object

///////////////////////////////////////////////////////
// Retrieves XML document
// Parameters:
//    url : URL string (relative or complete) to an 
//          .xml file whose Content-Type is a valid XML
//          type, such as text/xml; XML source must be 
//          from same domain 
///////////////////////////////////////////////////////
function loadXMLDoc( url ) 
{	
   // branch for native XMLHttpRequest object
   if (window.XMLHttpRequest) 
   {
      req = new XMLHttpRequest();
      req.onreadystatechange = processReqChange;
      req.open( "GET", url, true );
      req.send( null );
   } 
   // branch for IE/Windows ActiveX version
   else if (window.ActiveXObject) 
   {
      req = new ActiveXObject("Microsoft.XMLHTTP");
      if (req) 
      {
         req.onreadystatechange = processReqChange;
         req.open("GET", url, true);
         req.send();
      }
   }
}

///////////////////////////////////////////////////////
// Event handler for onreadystatechange event. Parses
// XML from the server and passes the result to the 
// 'method' function for output.
///////////////////////////////////////////////////////
function processReqChange()
{
	if (req.readyState == 4) // only if req shows "complete"
	{
		if (req.status == 200) // only if "OK"
		{		
			var response  = req.responseXML.documentElement;
         if ( response )
         {
			   method = response.getElementsByTagName('method')[0].firstChild.data;
			   result = response.getElementsByTagName('result')[0].firstChild.data;
			   var start = req.responseText.indexOf('<result>') + 8
			   var stop = req.responseText.indexOf('</result>')
			   result = req.responseText.substring(start,stop);
			   eval( method + '(\'\',\'\',\'\',\'\', result)' );
			}
		} 
		else 
		{
			alert( "There was a problem retrieving the XML data:\n" + req.statusText );
		}
	}
}
