//
// JSHttpRequest v1.11. (C) Dmitry Koterov, 2005-01-27.
// http://forum.dklab.ru/users/DmitryKoterov/
//
// Do not remove this comment if you want to use script!
//

var xml_js_method;

function XML_JSHttpRequest()
{
       
        if(window.XMLHttpRequest)
        {
                req=new XMLHttpRequest();

                xml_js_method='xml';

                if (req.setContentType) req.setContentType("text/xml");  // by Gas - fix bug in Opera with POST method
                
                return req;
        }

        if(window.ActiveXObject)
        {
                req = new ActiveXObject("Microsoft.XMLHTTP");

                if(!req) req = new ActiveXObject("Msxml2.XMLHTTP");

                xml_js_method='xml';
                
                if (req.setContentType) req.setContentType("text/xml");  // by Gas

                if(req) return req;
        }

        req=new JSHttpRequest();

        xml_js_method='js';

        return req;
}

function send_data(req,href,content)
{
        // add session name & id to post vars
        if (typeof(_session_name) != 'undefined' && typeof(_session_id) != 'undefined')
            content[_session_name] = _session_id;

        if(xml_js_method=='js')
        {
                req.open('GET', href, true);
                req.send(content);
        } else if(xml_js_method=='xml')
        {
                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('&');     
//                req.open('GET', href+'?'+qs, true);
//                req.send(null);
                req.open('POST', href, true);
//                if (req.setRequestHeader) req.setRequestHeader('content-type', 'application/x-www-form-urlencoded;');                                
                req.send(qs);
        }
}

function parse_data(req)
{
        if(req.responseJS) return req.responseJS;
        if(req.responseText)
        {
                eval(req.responseText);
                return response;
        }
        return false;
}

function get_errors(req)
{
        if(xml_js_method=='js') return req.responseText;
        else if(xml_js_method=='xml')
        {
                eval(req.responseText);
                return responseText;
        }
}

function JSHttpRequest() {}
(function() {
  var count       = 0;
  var pending     = {};
  var cache       = {};
  var spanReuse   = null;

  // Uncomment if you want to switch on <SCRIPT> reusing.
  // But be carefull: seems FireFox does not work with reusing correctly
  // (long delay on fast data reloading via existed <SCRIPT>).
  //spanReuse   = [];

  // Called by server script on data load.
  JSHttpRequest.dataReady = function(id, text, js) {
    var undef;
    var th = pending[id];
    if (th) {
      if (th.caching) cache[th.hash] = [text, js];
      th._dataReady(text, js);
    } else if (typeof(th) != typeof(undef)) {
      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,
    _id: null,

    abort: function() { with (this) {
      readyState = 0;
      onreadystatechange();
      _cleanupScript();
      delete pending[_id];
    }},

    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 = 'jsmode=true&' + 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;
      }
      this._obtainScript(id, href);
      return true;
    },

    getAllResponseHeaders: function() {
      return '';
    },

    getResponseHeader: function(label) {
      return '';
    },

    setRequestHeader: function(label, value) {
    },

    //
    // Internal functions.
    //

    _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();
      _cleanupScript();
    }},

    _obtainScript: function(id, href) { with (document) {
      var span = null;
      if (spanReuse == null || !spanReuse.length) {
        span = body.appendChild(createElement("SPAN"));
        span.style.display = 'none';
        span.innerHTML = 'Text for stupid IE.<script></' + 'script>';
      } else {
        span = spanReuse[spanReuse.length-1];
        spanReuse[spanReuse.length-1] = null;
        spanReuse.length--;
      }
      pending[id] = this;
      setTimeout(function() {
        var s = span.getElementsByTagName("script")[0];
        s.language = "JavaScript";
        if (s.setAttribute) s.setAttribute('src', href); else s.src = href;
      }, 10);
      this._id = id;
      this._span = span;
    }},

    _cleanupScript: function() {
      var span = this._span;
      if (span) {
        this._span = null;
        setTimeout(function() {
          if (spanReuse != null) {
            spanReuse[spanReuse.length] = span;
          } else {
            // without setTimeout - crash in IE 5.0!
            span.parentNode.removeChild(span);
          }
        }, 50);
      }
      //window.status = document.body.childNodes.length + " - " + (spanReuse? spanReuse.length : 'no span reusing')
      return false;
    }
  }
})();
