/* ---------------------------------------------------------------------------
 *    Make an HTTP GET or POST Request to another URL, 
 *    using URL/GET-style parameters
 */
function HTTPRequest(req_type, url, parameters, callback, asynch) 
{
  var http_request = false;
  var qm = '';

 // http_request = false;
 
  if (asynch == null)
    asynch = true;

  /*  Mozilla, Safari */
  if (window.XMLHttpRequest) 
  { 
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) 
    {
      // set type accordingly to anticipated content type
      //http_request.overrideMimeType('text/xml');
      http_request.overrideMimeType('text/html');
    }
  } 
  /*  Internet Explorer */
  else if (window.ActiveXObject)
  {
    try 
    {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) 
    {
      try 
      {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {}
    }
  }
  if (!http_request) 
  {
    alert('Cannot create XMLHTTP instance');
    return false;
  }
    
  /*  Upon ready-state change, invoke function;
   *  Setup and Issue the HTTP POST request to given url, 
   *  describing content in header, and sending parameters
   */
  if (asynch == true)
  {
    http_request.onreadystatechange = 
      function () 
      {  
        if (http_request.readyState == 4) 
        {
          if (http_request.status == 200) // success!
            callback(http_request.responseText);
          else 
            alert('There was a problem (status ' + 
                   http_request.status + ') with the request.');
        }
      }
  }

  if (req_type == 'POST')
  {
    reqOpen(http_request, req_type, url, callback, asynch);
    http_request.setRequestHeader("Content-type", 
                                  "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    reqSend(http_request, parameters);
  }
  else
  {
    if (url.charAt(url.length - 1) != '?' && parameters.charAt(0) != '?')
      qm = '?';
    reqOpen(http_request, req_type, url + qm + parameters, callback, asynch);
    reqSend(http_request, null);
  }
  /*  Not asynchronous?  Invoke callback function manually  */
  if (asynch == false)
    callback(http_request.responseText);
  return true;
}


/* ---------------------------------------------------------------------------
 *    Send a request with parameters
 */
function reqSend(http_request, parameters)
{
  //try
 // {
 //   if (navigator.appName == 'Netscape')
 //   {
 //     netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
 //     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
 //   }
 // }
 // catch (e) {}
  http_request.send(parameters);
}


/* ---------------------------------------------------------------------------
 *    Wrapper around request opening, to set perms & catch exceptions
 */
function reqOpen(http_request, req_type, full_request, callback, asynch)
{
  try
  {
    /*  Allow Mozilla to perform cross-domain XMLHTTP requests
     *  (as long as about:config has 
     *   signed.applets.codebase_principal_support enabled)
     */
   // try
   // {
      //if (navigator.appName == 'Netscape')
     // {
     //   netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
     //   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
     // }
   // }
   // catch (e) {}
    http_request.open(req_type, full_request, asynch);
  }
  catch (e)
  {
    callback("caught exception: " + e + "<br>");
    return false;
  }
  return true;
}
