// Domain for XmlHTTPRequest.readyState
var XML_READY_STATE_UNINITIALIZED  = 0;
var XML_READY_STATE_LOADING        = 1;
var XML_READY_STATE_LOADED         = 2;
var XML_READY_STATE_INTERACTIVE    = 3;
var XML_READY_STATE_COMPLETED      = 4;

function newXMLHttpRequest() 
// Returns a new XMLHttpRequest object, or false if this browser doesn't support it
// Tries various methods to create the object
{
  	var xmlReq = false;

  	if (window.XMLHttpRequest) 
  	{
 		// Create XMLHttpRequest object in non-Microsoft browsers
    	xmlReq = new XMLHttpRequest();
  	} 
	else if (window.ActiveXObject) 
	{
    	// Create XMLHttpRequest via MS ActiveX
    	try 
		{
      	// Try to create XMLHttpRequest in later versions of Internet Explorer
      	xmlReq = new ActiveXObject("Msxml2.XMLHTTP");
    	} 
		catch (e1) 
		{
      	// Failed to create required ActiveXObject
      	try 
			{
        		// Try version supported by older versions of Internet Explorer
        		xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
      	} 
			catch (e2) 
			{
        		// Unable to create an XMLHttpRequest with ActiveX
      	}
    	}
  	}
  	return xmlReq;
}

function doXmlReq (handlerFn, phpService, formData) 
// Do the given XML request
{
  	// Obtain an XMLHttpRequest instance
  	var xmlReq = newXMLHttpRequest();

  	// Set the handler function to receive callback notifications from the request object
	// when the "readystate" changes
  	var handlerFunction = getReadyStateHandler (xmlReq, handlerFn);
  	xmlReq.onreadystatechange = handlerFunction;
  
  	// Open an HTTP POST connection to the properties.php data server.
  	// N.B. the third parameter specifies request is asynchronous.
	try 
	{
		xmlReq.open ("POST", phpService, true);
	}
	catch (e)
	{
		alert ("Sorry there was a problem communicating with the data server");
	}

  	// Specify that the body of the request contains form data
  	xmlReq.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");

  	// Send form encoded data stating that I want to request info on the specified property.
	formData = formData.replace (/ /g, "%20");
	xmlReq.send (formData);
}

function getReadyStateHandler(xmlReq, responseXmlHandler) 
// Returns a function that waits for the specified XMLHttpRequest to complete, then passes its XML response
// to the given handler function.
// xmlReq - The XMLHttpRequest whose state is changing
// responseXmlHandler - Function to pass the XML response to
{
  	// Return an anonymous function that listens to the XMLHttpRequest instance
  	return function () 
  	{
    	// Check the request's status - goes from 0 (uninitialized) to 4 (complete)
    	if (xmlReq.readyState == 4) 
		{
      	// Check that a successful server response was received
      	if (xmlReq.status == 200) 
			{
        		// Pass the XML payload of the response to the handler function
        		responseXmlHandler (xmlReq.responseXML);
      	} 
			else      		// An HTTP problem has occurred
        		alert("Sorry, a communication error has occurred (" + xmlReq.status + ")");
    	}
  	}
}

function extractTag (tag, xmlStr)
// Extract the contents of the first found given tag from the given xmlString
{
	var pattern = "/<" + tag + ">([\s\S]+)<\/" + tag + ">/";
	var matches = xmlStr.match (pattern);
	if (matches > 0)
		return RegExp.$1;
	else
		return "";
}


