//------------------------------------------
// mrcHttpDebug -- if true, we are debugging
//------------------------------------------

var mrcHttpDebug = 0;


//--------------------------------------------------
// mrcGetHttpObject -- gets an XMLHttpRequest object
// which we can use to contact a server by HTTP
//--------------------------------------------------

if (typeof(mrcGetHttpObject) == 'undefined')
{
  mrcGetHttpObject = function()
  {
    var httpRequest;

    //----------------------------------
    // try to get an HTTP Request object
    //----------------------------------

    if (window.XMLHttpRequest)
    {
      //---------------------
      // Mozilla, Safari, etc
      //---------------------

      try
      {
        httpRequest = new XMLHttpRequest();
      }
      catch(e)
      {
        httpRequest = false;
      }
    }
    else
    if (window.ActiveXObject)
    {
      //---------------------
      // MS Internet Explorer
      //---------------------

      try
      {
        httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch(e)
      {
        try
        {
          httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
          httpRequest = false;
        }
      }
    }
    else
    {
      //-----------------------
      // no requester available
      //-----------------------

      httpRequest = false;
    }

    return httpRequest;
  }
}


//----------------------------------------------------
// mrcXmlHttpConnector -- a control class that can be
// used to fetch data from a server (thru a URL) and
// invoke a function defined within the calling object
// to make use of that data in some way
//----------------------------------------------------

if (typeof(mrcXmlHttpConnector) == 'undefined')
{
  mrcXmlHttpConnector = function()
  {
    var bComplete = false;
    var bSuccess = false;
    var XmlHttp;

    //------------------------------
    // try to get a requester object
    //------------------------------

    XmlHttp = mrcGetHttpObject();

    //------------------------------
    // define the connect() function
    //------------------------------

    this.connect = function(HttpMethod, UrlString, ArgString,
                            SuccessCallBackFunc, FailureCallBackFunc)
    {
      var CombinedString;

      //----------------------------------------
      // no requester object => we can't do much
      //----------------------------------------

      if (!XmlHttp)
      {
        return false;
      }

      //----------------------
      // task not complete yet
      //----------------------

      bComplete = false;
      bSuccess = false;

      //-------------------------------------
      // ensure the HTTP method is upper case
      //-------------------------------------

      HttpMethod = HttpMethod.toUpperCase();

      //------------------------
      // try to send the request
      //------------------------

      try
      {
        //------------------------------------
        // GET or POST => use the right method
        //------------------------------------

        if (HttpMethod == "GET")
        {
          if (ArgString.length > 0)
          {
            CombinedString = UrlString + "?" + ArgString;
          }
          else
          {
            CombinedString = UrlString;
          }

          XmlHttp.open(HttpMethod, CombinedString, true);
          ArgString = "";
        }
        else
        {
          CombinedString = "POST " + UrlString + " HTTP/1.1";

          XmlHttp.open(HttpMethod, UrlString, true);
          XmlHttp.setRequestHeader("Method", CombinedString);
          XmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        }

        //---------------------------------------------
        // define the job to do when the answer arrives
        //---------------------------------------------

        XmlHttp.onreadystatechange = function()
        {
          if ((XmlHttp.readyState == 4) && (bComplete == false))
          {
            bComplete = true;

            if (XmlHttp.status == 200)    // transaction succeeded
            {
              bSuccess = true;
              SuccessCallBackFunc(true, XmlHttp);
            }
            else    // transaction failed
            {
              bSuccess = false;

              if (FailureCallBackFunc)
              {
                FailureCallBackFunc(XmlHttp);
              }
              else
              {
                SuccessCallBackFunc(false, XmlHttp);
              }
            }
          }
        }

        //--------------------------------------
        // if POST, send arguments at this point
        //--------------------------------------

        XmlHttp.send(ArgString);
      }
      catch(z)
      {
        //---------------------------------
        // ooh, a fault => that failed then
        //---------------------------------

        return false;
      }

      //------------------------------------------
      // if we get here, the request went out okay
      //------------------------------------------

      return true;
    }

    //-----------------------------------------------
    // on creation of this object, return its address
    //-----------------------------------------------

    return this;
  }
}


//----------------------------------------------------
// mrcContentFetcher -- a function that can be used to
// fetch data from a server (thru a URL) and then fill
// a document element with the resulting text/html/xml
//----------------------------------------------------

if (typeof(mrcContentFetcher) == 'undefined')
{
  mrcContentFetcher = function()
  {
    var bComplete = false;
    var bSuccess = false;
    var XmlHttp;

    //------------------------------
    // try to get a requester object
    //------------------------------

    XmlHttp = mrcGetHttpObject();

    //----------------------------
    // define the fetch() function
    //----------------------------

    this.fetch = function(HttpMethod, UrlString, ArgString, DocElemName)
    {
      var CombinedString;
      var DocElement;

      //----------------------------------------
      // no requester object => we can't do much
      //----------------------------------------

      if (!XmlHttp)
      {
        return false;
      }

      //---------------------------------------------
      // invalid document element => we can't do much
      //---------------------------------------------

      DocElement = document.getElementById(DocElemName);

      if (!DocElement)
      {
        return false;
      }

      //----------------------
      // task not complete yet
      //----------------------

      bComplete = false;
      bSuccess = false;

      //-------------------------------------
      // ensure the HTTP method is upper case
      //-------------------------------------

      HttpMethod = HttpMethod.toUpperCase();

      //------------------------
      // try to send the request
      //------------------------

      try
      {
        //------------------------------------
        // GET or POST => use the right method
        //------------------------------------

        if (HttpMethod == "GET")
        {
          if (ArgString.length > 0)
          {
            CombinedString = UrlString + "?" + ArgString;
          }
          else
          {
            CombinedString = UrlString;
          }

          XmlHttp.open(HttpMethod, CombinedString, true);
          ArgString = "";
        }
        else
        {
          CombinedString = "POST " + UrlString + " HTTP/1.1";

          XmlHttp.open(HttpMethod, UrlString, true);
          XmlHttp.setRequestHeader("Method", CombinedString);
          XmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        }

        //---------------------------------------------
        // define the job to do when the answer arrives
        //---------------------------------------------

        XmlHttp.onreadystatechange = function()
        {
          if ((XmlHttp.readyState == 4) && (bComplete == false))
          {
            bComplete = true;

            if (XmlHttp.status == 200)    // transaction succeeded
            {
              bSuccess = true;
              DocElement.innerHTML = XmlHttp.responseText;
            }
            else    // transaction failed
            {
              bSuccess = false;
              DocElement.innerHTML = "Failed to fetch content";
            }
          }
        }

        //--------------------------------------
        // if POST, send arguments at this point
        //--------------------------------------

        XmlHttp.send(ArgString);
      }
      catch(z)
      {
        //---------------------------------
        // ooh, a fault => that failed then
        //---------------------------------

        return false;
      }

      //------------------------------------------
      // if we get here, the request went out okay
      //------------------------------------------

      return true;
    }

    //-----------------------------------------------
    // on creation of this object, return its address
    //-----------------------------------------------

    return this;
  }
}


//--------------------------------------------
// mrcStaticContent -- a function that fetches
// static content from the server and puts the
// text response into a named document element
//--------------------------------------------

if (typeof(mrcStaticContent) == 'undefined')
{
  mrcStaticContent = function(DocElementName, UrlString, ArgString)
  {
    var contentFetcher = new mrcContentFetcher();
    contentFetcher.fetch("GET", UrlString, ArgString, DocElementName);
  }
}


//-----------------------------------------------------
// mrcGetFormValue -- retrieves the value of a named
// form within a given parent form - in XML
//-----------------------------------------------------

if (typeof(mrcGetFormValue) == 'undefined')
{
  mrcGetFormValue = function(parentForm, subFormName)
  {
    var subElements = parentForm.getElementsByTagName(subFormName);
    var result = "";

    if (subElements.length > 0)
    {
      result = subElements[0].firstChild.data;
      result = unescape(result);
    }

    return result;
  }
}

