//$Id: httprequest.js,v 1.1 2008/01/16 17:33:04 sdw Exp $

// Class for creating and responding to httprequests
// informed by http://www.sitepoint.com/blogs/2004/05/26/xmlhttprequest-and-javascript-closures/

// Syntax: new HttpRequest().start(url, response_function); 
// The function gets called back with the response text as a parameter


function HttpRequest() {};
new HttpRequest();


HttpRequest.prototype = {
  response_function: null,
  req: null,

  start: function(url, response_function, verb, headers, body){
    if (!verb)
      verb="GET";
    this.response_function=response_function;
    // branch for native XMLHttpRequest object
    var self=this;
    if (window.XMLHttpRequest) {
      this.req = new XMLHttpRequest();
    }else  if (window.ActiveXObject) {
      this.req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (this.req){
      this.req.onreadystatechange = function(){ self.processReqChange(self)};
      this.req.open(verb, url, true);
      if (headers){
        for (var name in headers) 
          this.req.setRequestHeader(name, headers[name]);
      }
      this.req.send(body);
    }
  },
  processReqChange: function(self){
    if (self.req.readyState==4){
      if (self.req.status==200){
        // invoke the response function
        if (self.response_function)
          self.response_function(self.req.responseText);
      }
    }
  }
}

