var needChainedAjaxQuery = false;
var net=new Object();

net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1 ;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;
// 2 Конструктор
// --- 2007/09/26 ---
//net.ContentLoader=function(url,onload,onerror)
// --- 2007/09/26 ---
// +++ 2007/09/26 +++
net.ContentLoader=function(url,onload,onerror,method,postData)
// +++ 2007/09/26 +++
{
  this.url=(url) ? url : null;
  this.req=null;
  // +++ 2007/09/26 +++
  this.method=(method) ? method : 'GET';
  this.postData=(postData) ? postData : null;
  // +++ 2007/09/26 +++
  this.onload=(onload) ? onload : null;
  this.onerror=(onerror) ? onerror : this.defaultError;
  if ((this.url != null) && (this.onload != null)) {this.loadXMLDoc();}
}
  
net.ContentLoader.prototype=
{
  loadXMLDoc:function()
  {
    needChainedAjaxQuery = true;
    if (window.XMLHttpRequest) 
    {
      try { this.req = new XMLHttpRequest(); } catch(err) { this.req = false; }
    }
    else if (window.ActiveXObject)
    {
      try { this.req = new ActiveXObject("Msxml2.XMLHTTP"); } catch(err) {
      try { this.req = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { this.req = false; } }
    }
    if (this.req)
    {
      try 
      {
        // +++ 2007/09/27 +++
        if (this.onload)
        {
        // +++ 2007/09/27 +++
          var loader=this;
          this.req.onreadystatechange=function()
          {
            loader.onReadyState.call(loader);
          }
        // +++ 2007/09/27 +++
        }
        // +++ 2007/09/27 +++
        // --- 2007/09/26 --- 
        //this.req.open('GET', this.url, true);
        //this.req.send(null);
        // --- 2007/09/26 --- 
        // +++ 2007/09/26 +++
        this.req.open(this.method, this.url, true);
        if (this.method == 'POST')
        {
          this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
          this.req.send(this.postData);
        }
        else
        {
          this.req.send(null);
        }
        // +++ 2007/09/26 +++
      }
      catch (err)
      {
        this.onerror.call(this);
      }
    }
  },
  
  onReadyState:function()
  {
    if (this.req.readyState==net.READY_STATE_COMPLETE)
    {
      needChainedAjaxQuery = false;
      if ((this.req.status==200) && (this.req.responseXML || this.req.responseText))
      //if ((this.req.status==200) && (this.req.responseXML))
      {
        //this.onload(this.req.responseXML);
        try { this.onload.call(this); } catch(err) { this.onerror.call(this); }
      }
      else
      {
        this.onerror.call(this);
      }
    }
  },
    
  defaultError:function()
  {
    return false;
  },
  
  defaultErrorOld:function()
  {
    /*
    alert("error fetching data!"
    +"\n\nurl: "+this.url
    +"\ncallback: "+this.onload
    +"\nreadyState: "+this.req.readyState
    +"\nstatus: "+this.req.status
    +"\nheaders: "+this.req.getAllResponseHeaders ()) ;
    */
  }
}

// --- create Ajax Object ---
var ld = new net.ContentLoader();

function ajaxChainLoaderGet(url,f_callback)
{
  if (needChainedAjaxQuery)
  {
    setTimeout("ajaxChainLoaderGet('"+url+"',"+f_callback+")",1000);
  }
  else
  {
    ld.url = url;
    ld.method = 'GET';
    ld.onload = f_callback;
    ld.loadXMLDoc();
  }
}

function ajaxChainLoaderPost(url,postData,f_callback)
{
  if (needChainedAjaxQuery)
  {
    setTimeout("ajaxChainLoaderPost('"+url+"','"+postData+"',"+f_callback+")",1000);
  }
  else
  {
    ld.url = url;
    ld.method = 'POST';
    ld.postData = postData;
    ld.onload = f_callback;
    ld.loadXMLDoc();
  }
}
