/********************************************************************************************
 * subject     :  h.com implementation of a httprequest object (using AJAX)                 *
 *                (c) 2006 by h.com networkers                                              *
 * version     :  1.0                                                                       *
 * cvs         :  $Id: htmlrequest.js,v 1.1 2009-12-16 13:14:08 karlb Exp $
 * description :  implements an http-request object, encapsulate the AJAX XML-Object        *
 *                provided by Firefox and Internet-Explorer.                                *
 *                required a XMLHttpRequest offered by the browser                          *
 ********************************************************************************************/

// constants
REQUEST_READY_STATE_NOT_INIT = 0;
REQUEST_READY_STATE_LOAD = 1;
REQUEST_READY_STATE_LOADED = 2;
REQUEST_READY_STATE_INTERACT = 3;
REQUEST_READY_STATE_COMPLETE = 4;

function ClientHttpRequest() {
  if (window.ClientHttpRequestList == null) window.ClientHttpRequestList = new Array();
  this.index = window.ClientHttpRequestList.length;
  window.ClientHttpRequestList[this.index] = this;

  this.file_mode = window.location.protocol.toUpperCase() == "FILE:";

  this.http_request = null;
  this.browser = null;

  this.Asynchron = true;

  this.Status = 200;
  this.StatusText = "";
  this.responseText = null;
  this.responseXML = null;

  this.getXmlHttpRequest = function () {
    var Result = null;
    if (window.XMLHttpRequest) { // Mozilla, Safari, IE7 ...
      this.browser = "ns";
      Result = new window.XMLHttpRequest();
      if (Result.overrideMimeType) Result.overrideMimeType("text/xml");
    } else if (window.ActiveXObject) { // IE
      this.browser = "ie";
      try {
        Result = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
        try {
          Result = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
          Result = null;
        }
      }
    } else {
      this.browser = null;
    }
    if(Result)
      eval("Result.onreadystatechange = function() { ClientHttpRequestList[" +  this.index + "]._request_state_changed(); }");

    return Result;
  }

  this._request_state_changed = function () {
    if(this.onStateChanged) this.onStateChanged(this.http_request.readyState, this);
    if(this.http_request.readyState == 4) this._request_ready();
  }

  this._request_ready = function () {
    this.Status = this.http_request.status;
    if(this.Status == 0) this.Status = 200; // Workaround f³r file://
    this.StatusText = this.http_request.statusText;
    if(!this.StatusText && (this.Status == 200)) this.StatusText = "OK";

    this.responseText = this.http_request.responseText;
    this.responseXML = this.http_request.responseXML;
    if((this.browser == "ie") && this.file_mode && this.responseXML && (this.responseXML.childNodes.length == 0)) {
      var xmldoc = null;
      try {
        xmldoc = new ActiveXObject("Msxml2.DOMDocument");
      } catch (e) {
        try {
          xmldoc = new ActiveXObject("Microsoft.XMLDOM");
        } catch (e) {
          xmldoc = null;
        }
      }
      if(xmldoc != null) {
        xmldoc.loadXML(this.responseText);
        this.responseXML = xmldoc;
      }
    }

    if(this.onReady) this.onReady(this);
    if (this.Status == 200) {
      if(this.onLoaded) this.onLoaded(this);
    } else {
      if(this.onError) this.onError(this);
    }
    this.http_request.onreadystatechange = function () {};
    this.http_request = null;
    this.responseText = null;
    this.responseXML = null;
    window.ClientHttpRequestList[this.index] = null;
  }

  this.Get = function (url, data) {
    window.ClientHttpRequestList[this.index] = this;
    if(this.http_request == null) this.http_request = this.getXmlHttpRequest();
    this.http_request.open("GET", url, this.Asynchron);
    this.http_request.send(data);
    return true;
  }

  this.onStateChanged = function (readyState, Sender) { }
  this.onReady = function (Sender) { }
  this.onLoaded = function (Sender) { }
  this.onError = function (Sender) { }

  this.http_request = this.getXmlHttpRequest();
  this.Supported = this.http_request ? true : false;
}

if(window.ClientHttpRequestOldUnload == null) {
  window.ClientHttpRequestOldUnload = window.onunload;
  window.onunload = function () {
    var old = window.ClientHttpRequestOldUnload;
    window.ClientHttpRequestList = null;
    window.ClientHttpRequestOldUnload = null;
    if(old) old();
  }
}

