/** crossbrowser xmlhttp wrapper
 *
 *  xmlhttp: constructor for xmlhttp object
 *    async: boolean, true if request should be asynchronous
 *            (requires callback if true)
 *    callback(xmlhttprequest obj): function to call if async call completed
 *
 *  xmlhttp.req(method, url, data)
 *    executes xmlhttp request.
 *      method: string ("POST" or "GET")
 *      url: url
 *      data: post data (if any)
 *
 *    returns xmlhttp object, if async was false.
 *
 *  example:
 *
 *  function z(x) {alert (x.responseText)};
 *  r = new xmlhttp(1, z);
 *  r.req("GET", "/xmlhttphandler.php", "");
 *
 **/


function xmlhttp(async, callback)
{
  if (async == undefined)
    async = 0;
  if (callback == undefined)
    callback = null;
  this.async = async;
  this.callback = callback;
}

xmlhttp.prototype.req = function(method, url, data)
{
  if (window.XMLHttpRequest)
    this.x = new XMLHttpRequest()
  else if (window.ActiveXObject)
    this.x = new ActiveXObject("Microsoft.XMLHTTP")
  else
    return false;

  if (this.async)
    this.x.onreadystatechange = function(q, w) {return function(){q.getresponse(w)} } (this, this.callback);

  this.x.open(method, url, this.async);
  this.x.send(data);
  if (!this.async)
    return this.x;
}

xmlhttp.prototype.getresponse = function(callback)
{
  if (this.x.readyState == 4 && this.x.status == 200)
    callback(this.x);
}
