//////////////////////////////////////
//
// AJAX support
//
function getXMLHttpRequest(){
  if(window.XMLHttpRequest) 
    return new XMLHttpRequest();
  
  if(window.ActiveXObject){
    try{
      return new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        return new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        return null;
      }
    }
  }
}

function isString(it){
    // summary:  Return true if it is a String.
    return (it instanceof String || typeof it == "string");
}

function hitch(thisObject, method) {
    if(isString(method)) {
        var fcn = thisObject[method];
    } else {
        var fcn = method;
    }
    
    return function() {
        return fcn.apply(thisObject, arguments);
    }
}

function AJAXRequest(_url, _handler)
{
  this.url = _url;
  this.handler = _handler;
  this.send = function(){
    this.req = getXMLHttpRequest();
    this.req.open("GET", this.url, /*async*/true);
    this.req.onreadystatechange = hitch(this, "onreadystatechange");
    this.req.send(/*no params*/null);
  }

  this.onreadystatechange = function(){
    if (this.req.readyState != 4) //not complete
      return;
    if (this.req.status != 200){
      alert("There was a problem retrieving the XML data:\n" + this.req.statusText + ":" + this.req.status);
      this.req = null;
      return;
    }
    this.handler(this.req.responseText);
  }
}

