// ---------------------------------------------------------------------------
// (c) 2005 KystAtlas, Hans Martin Mohn
// ---------------------------------------------------------------------------
//
var PROCESSING_INSTRUCTION_NODE = 7;

// Perform an asynchronous web request with the given string sUrl.
// When a successful response is available, the callback is called.
//
// Usage pattern:
//
//    obj.cbAjaxComplete = function cbAjaxComplete(ajax, xml_dom, text)
//    {
//      alert(this.m_name);       // Access members of "obj"
//      alert(ajax.m_user_data);  // Access userData
//    }; // end cbAjaxComplete()
//
//    var ajax = new gtAjax(url, cbAjaxComplete, obj, userData, postData)
//
// NOTE: For static (global) use, all references to "obj" above may be omitted

function gtAjax(sUrl, callback, me, user_data, post_data, content_type)
{
  this.m_url        = sUrl;
  this.m_callback   = callback;
  this.m_me         = me;
  this.m_user_data  = user_data;
  
  if (window.XMLHttpRequest)
  {
    // branch for native XMLHttpRequest object
    this.m_req = new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {
    // branch for IE/Windows ActiveX version
    this.m_req = new ActiveXObject("Microsoft.XMLHTTP");
  }

  this.m_req.onreadystatechange = this.createCallback();
  if (post_data)
  {
    this.m_req.open("POST", sUrl, true);
    this.m_req.setRequestHeader("Content-Type", content_type ? content_type : "application/x-www-form-urlencoded");
    this.m_req.send(post_data);
  }
  else
  {
    this.m_req.open("GET", sUrl, true);
    this.m_req.send(null);
  }
} // end gtAjax Constructor



// Use the Javascript closeure-mechanism to bundle a function and
// an object reference as a new function!
gtAjax.prototype.createCallback = function createCallback()
{
  var me = this;
  return function()
  {
    if (me.m_req.readyState == 4)
    {
      if (me.m_req.status == 200)
        me.m_callback.call(me.m_me, me, me.m_req.responseXML, me.m_req.responseText);
      else
        alert("gtAjax: Error loading URL " + me.m_url + "\n response: " + me.m_req.responseText);
    }
  } // end handleReadyStateChange()
}; // end createCallback()
