function Ajax()
{
  this.async          = true;
  this.handleResponse = null;
  this.method         = "GET";
  this.mimeType       = null;
  this.postData       = null;
  this.readyState     = null;
  this.request        = null;
  this.responseFormat = "text" // "text", "xml",  or "object" are the available options
  this.responseXML    = null;
  this.responseText   = null;
  this.status         = null;
  this.statusText     = "";
  this.url            = null;
  
  this.init = function()
  {
    var i = 0;
    var requestTry = [
        function() { return new XMLHttpRequest(); }, //  Try to create an opbject for Firefox, Safari, IE7, etc.
        function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
        function() { return new ActiveXObject("Microsoft.XMLHTTP"); } ]; // Try creating an object for earlier versionsof IE
        
    while(!this.request && (i < requestTry.length))
    {
      try
      {
        this.request = requestTry[i++]();
      }
      catch(e) {}
    }
    return true;
  }; // End init() method

  this.doGet = function(url, dataHandler, format)
  {
    this.url            = url;
    this.handResp       = dataHandler;
    this.responseFormat = format || "text";
    this.doRequest();
  };
  
  this.doPost = function(url, postData, dataHandler, format)
  {
    this.url            = url;
    this.postData       = postData;
    this.handleResponse = dataHandler;
    this.responseFormat = format || "text";
    this.method         = "POST";
    this.doRequest();
  };
  
  this.doRequest = function()
  {
    if(!this.init())
    {
      alert("Could not create the XMLHttpRequest object.");
      return;
    }
    
    this.request.open(this.method, this.url, this.async);
    
    if(this.method == "POST")
    {
      this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    }
      
    if(this.mimeType)
    {
      try
      {
        request.overrideMimeType(this.mimeType);
      }
      catch(e)
      {
        //  Could not override mime type
      }
    }
    
    var self = this; // Fixes the loss of scope in the inner function
    this.request.onreadystatechange = function()
    {
      var response = null;
      if(self.request.readyState == 4)
      {
        switch(self.responseFormat)
        {
          case "text":
            response = self.request.responseText;
            break;
            
          case "xml":
            response = self.request.responseXML;
            break;
            
          case "object":
            response = request;
            break;
        }
        if(self.request.status >= 200 && self.request.status <= 299)
          self.handleResponse(response);
        else
          self.handleErr(response);
        // handle the response here
      }
    };
    this.request.send(this.postData);
  }; // End doReq() method
  
  this.handleErr = function()
  {
    var errorWin;
    try
    {
      errorWin= window.open("", "errorWin");
      errorWin.document.body.innerHTML = this.responseText;
    }
    catch(e)
    {
      alert("An error occured, but the error message cannot be displayed. "
          + "This is probably because of your browser blocking pop-ups.\n"
          + "Please allow pop-ups from this website if you want to see the full error message.\n"
          + "\n"
          + "Status Code: " + this.request.status + "\n"
          + "Status Description: " + this.request.statusText);
    }
  };
  
  this.handlerBoth = function(funcRef)
  {
    this.HandleResp = funcRef;
    this.HandleErr  = funcRef;
  };
  
  this.abort = function()
  {
    if(this.request)
    {
      this.request.onreadystatechange= function() {};
      this.rew.abort();
      this.request = null;
    }
  };
      
  this.setMimeType = function(mimeType)
  {
    this.mimeType = mimeType;
  };
}

/*
---------------------------------------------------------------------------

 *   This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2, or (at your option)
 *      any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program (see the file COPYING); if not, write to the
 *      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *      Boston, MA  02111-1307, USA

This code is copyright 2003 by Matthew Eernisse (mde@state26.com)

Additional bugfixes by Mark Pruett (mark.pruett@comcast.net)

---------------------------------------------------------------------------
*/

// The var docForm should be a reference to a <form>
//   docForm = document.getElementById(form id)
//
function formData2QueryString(docForm) {

  var strSubmitContent = "";
  var formElem;
  var strLastElemName = "";

  for (i = 0; i < docForm.elements.length; i++) {

    formElem = docForm.elements[i];
    switch (formElem.type) {
      // Text fields, hidden form elements
      case "text":
      case "hidden":
      case "password":
      case "textarea":
      case "select-one":
        strSubmitContent += formElem.name + "=" + escape(formElem.value) + "&"
        break;

      // Radio buttons
      case "radio":
        if (formElem.checked) {
          strSubmitContent += formElem.name + "=" + escape(formElem.value) + "&"
        }
        break;

      // Checkboxes
      case "checkbox":
        if (formElem.checked) {
          // Continuing multiple, same-name checkboxes
          if (formElem.name == strLastElemName) {
            // Strip of end ampersand if there is one
            if (strSubmitContent.lastIndexOf("&") == strSubmitContent.length-1) {
              strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
            }
            // Append value as comma-delimited string
            strSubmitContent += "," + escape(formElem.value);
          }
          else {
            strSubmitContent += formElem.name + "=" + escape(formElem.value);
          }
          strSubmitContent += "&";
          strLastElemName = formElem.name;
        }
        break;
    }
  }

  // Remove trailing separator
  strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
  return strSubmitContent;
}

