/*
 * Returns a new XMLHttpRequest object, or false if this browser
 * doesn't support it
 */
function newXMLHttpRequest()
{

  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;
}



/*
 * Returns a function that waits for the specified XMLHttpRequest
 * to complete, then passes its XML response
 * to the given handler function.
 * req - The XMLHttpRequest whose state is changing
 * responseXmlHandler - Function to pass the XML response to
 */
function getReadyStateHandlerXML(req, responseXmlHandler)
{

  // Return an anonymous function that listens to the 
  // XMLHttpRequest instance
  return function ()
  {

    // If the request's status is "complete"
    if (req.readyState == 4)
    {
      
      // Check that a successful server response was received
      if (req.status == 200)
      {

        // Pass the XML payload of the response to the 
        // handler function
        responseXmlHandler(req.responseXML);

      }
      else
      {

        // An HTTP problem has occurred
        alert("HTTP error: "+req.status+"\n'"+req.statusText+"'");
      }
    }
  }
}

  
  
/*
 * Returns a function that waits for the specified XMLHttpRequest
 * to complete, then passes its response
 * to the given handler function.
 * req - The XMLHttpRequest whose state is changing
 * responseXmlHandler - Function to pass the XML response to
 */
function getReadyStateHandlerResponse(req, responseXmlHandler)
{

  // Return an anonymous function that listens to the 
  // XMLHttpRequest instance
  return function ()
  {

    // If the request's status is "complete"
    if (req.readyState == 4)
    {
      
      // Check that a successful server response was received
      if (req.status == 200)
      {

        // Pass the XML payload of the response to the 
        // handler function
        responseXmlHandler(req);

      }
      else
      {

        // An HTTP problem has occurred
        alert("HTTP error: "+req.status+"\n'"+req.statusText+"'");
      }
    }
  }
}



/*
 * Exactly the same as the default readyStateHandler above but with
   an additional param accepted to pass through to the responseXmlHandle function
 * req - The XMLHttpRequest whose state is changing
 * param - additional paramater passed through to the repsonseXmlHandler
 * responseXmlHandler - Function to pass the XML response to
 */
function getSportplanReadyStateHandlerResponse (req, param, responseXmlHandler)
{

  // Return an anonymous function that listens to the 
  // XMLHttpRequest instance
  return function ()
  {

    // If the request's status is "complete"
    if (req.readyState == 4)
    {
      
      // Check that a successful server response was received
      if (req.status == 200)
      {

        // Pass the XML payload of the response to the 
        // handler function
        responseXmlHandler(req, param);

      }
      else
      {

        // An HTTP problem has occurred
        alert("HTTP error: "+req.status+"\n'"+req.statusText+"'");
      }
    }
  }
}


/*
 * Stop event calls of higher level elements
 * being processed from this event call
 * event - the Event object
 */
function cancelEventPropagation (event)
{
  if (event.stopPropagation)
  {
    event.stopPropagation();
  }
  else
  {
    event.cancelBubble = true;
  }
}
