/*
   XmlHttpClient is wrapper for browser independant XMLHttpRequest.

   Note: do not call private functions.

   Usage:
   1. Create global object from XmlHttpClient
   2. Call executeGet with url and callback methode
   3. Handle calls to callback methode, use global xmlHttpClient object to check status
      and get the output string.

*/

function XmlHttpClient()
{

   var xmlHttpRequest;
   var usingInternetExplorer;
   
   this.hasXmlHttpSupport = function()
   {
      return (this.xmlHttpRequest != null);
   }

   this.isInitialized = function()
   {
      if (this.xmlHttpRequest == null)
      {
         return false;
      }

      return (this.xmlHttpRequest.readyState != 0);
   }

   this.isLoading = function()
   {
      if (this.xmlHttpRequest == null)
      {
         return false;
      }

      return (this.xmlHttpRequest.readyState == 1);
   }

   this.isLoaded = function()
   {
      if (this.xmlHttpRequest == null)
      {
         return false;
      }

      return (this.xmlHttpRequest.readyState == 2);
   }

   this.isInteractive = function()
   {
      if (this.xmlHttpRequest == null)
      {
         return false;
      }

      return (this.xmlHttpRequest.readyState == 3);
   }

   this.isComplete = function()
   {
      if (this.xmlHttpRequest == null)
      {
         return false;
      }

      return (this.xmlHttpRequest.readyState == 4);
   }

   this.executeGet = function(strUrl, readyStateCallback)
   {
      //Is httpRequest initialized and in use?
      //Then abort and cleanup old object.
      if (this.isInitialized())
      {
         this.xmlHttpRequest.abort();
         this.xmlHttpRequest = null;
      }

      //Get a new request
      this.private_initializeXmlHTTP();

      //Return if no support for XMLHTTP
      if (!this.hasXmlHttpSupport())
      {
         return;
      }

      //Params: methode, url,[ asynchronious, username, password ]
      this.xmlHttpRequest.open("GET", strUrl, true);

      //create code inside a function for xmlHttpRequest object to callback as
      //soon as the ready state changes. This is only called if XmlHttpRequest
      //finishes
      this.xmlHttpRequest.onreadystatechange = readyStateCallback;

      //Send request.
      this.xmlHttpRequest.send(null);
   }


   this.getXmlResponse = function()
   {
      if (this.usingInternetExplorer)
      {
         var xmlDocument = new ActiveXObject("Microsoft.XMLDOM");
         xmlDocument.async = false;
         xmlDocument.loadXML(this.private_getRawResponseString());
         return xmlDocument;
      }
      else
      {
         return this.xmlHttpRequest.responseXML;
      }
   }

   this.private_getRawResponseString = function()
   {
      if (this.isInitialized())
      {
         return this.xmlHttpRequest.responseText;
      }
      else
      {
         return null;
      }
   }


   this.private_initializeXmlHTTP = function()
   {
      this.xmlHttpRequest = null;

      try
      {
         //Some IE versions use Msxml2.XMLHTTP
         this.xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP")
      }
      catch(e)
      {
         try
         {
            //Some IE versions use Microsoft.XMLHTTP
            this.xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch(oc)
         {
            //No IE XMLHTTP object found.
            this.xmlHttpRequest = null;
         }
      }

      //If no IE XMLHTTP object found, try to find a Mozilla/safari version
      if (!this.xmlHttpRequest && (typeof XMLHttpRequest != "undefined"))
      {
         //Mozilla Safari
         this.usingInternetExplorer = false;
         this.xmlHttpRequest = new XMLHttpRequest();
      }
      else
      {
         this.usingInternetExplorer = true;
      }
   }



   this.private_initializeXmlHTTP();
}
