//$Id: httprequest.js,v 1.1 2009/02/26 16:43:27 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().call(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);
      }
    }
  }
}

function serializeFormElements(elements){
  var serialize="";
  for (var i=0;i<elements.length;i++){
    var element=elements[i];
    if (!element.disabled && element.name){
      var value=null;
      switch(element.type.toLowerCase()){
      case 'checkbox':
      case 'radio':
        if (element.checked)
          value=element.value;
        break;
      case 'select-one':
        if (element.selectedIndex>0)
          value=element.options[element.selectedIndex].value;
        else{
          if (element.options.length>0)
            value=element.options[0].value;
        }
        break;
      case 'select-multiple':
        var values, length = element.length;
        if (length>0){
          for (var j = 0, values = []; j < length; j++) {
            var opt = element.options[j];
            if (opt.selected){
              if (serialize.length>0) serialize+="&";
              serialize+=element.name+"="+encodeURIComponent(opt.value);
            }
          }
        }
        break;
      case 'button':
      case 'submit':
        break;
      default:
        value=element.value;
        break;
      }
      if (value){
        if (serialize.length>0) serialize+="&";
        serialize+=element.name+"="+encodeURIComponent(value);
      }
    }
  }
  return serialize;
}
function submitForm(form_id, response_function){
  form=document.getElementById(form_id);
  url=form.action;
  
  new HttpRequest().start(url, response_function,"POST",{'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, serializeFormElements(form.elements));
}
function submitFormToDiv(form_id, div_id){
  submitForm(form_id,function(responseText){
               document.getElementById(div_id).innerHTML=responseText;
             }
             );
}

