//
// JSHttpRequest v1.2. (C) Dmitry Koterov, 2005-01-27. 
// http://forum.dklab.ru/users/DmitryKoterov/
//
// Do not remove this comment if you want to use script!
// Не удаляйте данный комментарий, если вы хотите использовать скрипт!
//
function JSHttpRequest() {}
(function() {
  var count       = 0;
  var pending     = {};
  var cache       = {};

  // Called by server script on data load.
  JSHttpRequest.dataReady = function(id, text, js) {
    var th = pending[id];
    if (th) {
      if (th.caching) cache[th.hash] = [text, js];
      th._dataReady(text, js);
    } else {
      alert("ScriptLoader: unknown pending id: "+id);
    }
  }
  
  JSHttpRequest.prototype = {
    // Standard properties.
    onreadystatechange: null,
    readyState: 0,
    responseText: null,
    responseXML: null,
    status: 200,
    statusText: "OK",
    // Additional properties.
    responseJS: null, 
    caching: false,
    SID: null,
    // Internals.
    _span: null,
      
    abort: function() { 
      this.readyState = 0;
      var span = this._span;
      if (span) {
        this._span = null;
        setTimeout(function() {
          // without setTimeout - crash in IE 5.0!
          span.parentNode.removeChild(span);
        }, 100);
      }
      return false;
    },
      
    open: function(method, url, asyncFlag, username, password) {
      if ((""+method).toLowerCase() != 'get') {
        alert('Only GET method is supported!');
        return false;
      }
      this.url = url;
      return true;
    },
    
    send: function(content) {
      var id = count++;
      var query = [];
      if (content instanceof Object) {
        for (var k in content) {
          query[query.length] = escape(k) + "=" + escape(content[k]);
        }
      } else {
        query = [content];
      }
      var qs = query.join('&');
      query = id + ':' + this.SID + ':' + qs;
      var href = this.url + (this.url.indexOf('?')>=0? '&' : '?') + query;
      var hash = this.url + '?' + qs;
      this.hash = hash;
      if (this.caching && cache[hash]) {
        var c = cache[hash];
        this._dataReady(c[0], c[1]);
        return false;
      }
      with (document) {
        var span = body.appendChild(createElement("SPAN"));
        span.style.display = 'none';
        span.innerHTML = 'Text for stupid IE.<script></' + 'script>';
        this._span = span;
        pending[id] = this;
        setTimeout(function() {
          var s = span.getElementsByTagName("script")[0];
          s.language = "JavaScript";
          s.src = href;
        }, 10);
      }

      return true;
    },

    getAllResponseHeaders: function() {
      return '';
    },
      
    getResponseHeader: function(label) {
      return '';
    },

    setRequestHeader: function(label, value) {
    },
    
    _dataReady: function(text, js) { with (this) {
      if (text !== null || js !== null) {
        readyState = 4;
        responseText = responseXML = text;
        responseJS = js;
      } else {
        readyState = 0;
        responseText = responseXML = responseJS = null;
      }
      onreadystatechange();
      var span = this._span;
      if (span) {
        this._span = null;
        setTimeout(function() {
          // without setTimeout - chash in IE 5.0!
          span.parentNode.removeChild(span);
        }, 100);
      }
    }}
  }
})();

