// XHR.JS 
// 

function XHR(div, url) {
  this.req = null;
  this.div = (div!=null) ? div : '';
  this.server = (url!=null) ? url : '';
  this.busy = "<br><p align='center'><img src='/HCC/images/rotating_arrow.gif' />&nbsp;Processing...</p><br>";

  this.init = function(url, parameters, target, spin) {
    this.url = (url!=null && url!='') ? url : this.server;
    this.params = (parameters!=null) ? parameters : '';
    this.target = (target!=null && target!='') ? target : this.div;
    this.spin = (spin!=null) ? spin : true;
  }
  this.init(url, '', div, true);

  this.run = function(parameters, target, spin) {
    this.req = null;
    if (this.url=='') {
      // alert('?Server not configured');
      return false;
    }
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
      this.req = new XMLHttpRequest();
      if (this.req.overrideMimeType) this.req.overrideMimeType('text/html');
    }
    else if (window.ActiveXObject) { // IE
      try { this.req = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) {
        try { this.req = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) {}
      }
    }

    if (!this.req) {
      // alert('?Cannot create XMLHTTP instance');
      return false;
    }
    var uri = this.url;
    //alert(uri);
    if (parameters==null) parameters = this.params;
    //alert(parameters);
    var method = (parameters!=null && parameters!='') ? 'POST' : 'GET';
    if (target!=null && target!='') this.target = target;
    //alert(this.target);
    if (spin!=null) this.spin = spin;
    //alert(this.spin);
    var self = this;
    //alert(self);
    this.req.onreadystatechange = function() {
      var result, html='', js='';
      //alert(self.req.readyState);
      if (self.req.readyState!=4) return;
      if (self.req.status==200)
        result = self.req.responseText;
        //alert(result);
      else
        result = '?XHR: ' + self.req.status + ' ' + self.req.statusText;
      while (result.length>0) {
        var i = result.indexOf('<!--code-->');
        //alert(i);
        if (i==-1) {
          html += result;
          //alert(html);
          break;
        }
        html += result.substr(0, i);
        result = result.substr(i+11);
        i = result.indexOf('<!--endcode-->');
        if (i==-1) i = result.length;
        js += result.substr(0, i);
        result = result.substr(i+14);
      }
      if (typeof(self.target)=='function')
        self.target(html, js);
      else {
        document.getElementById(self.target).innerHTML = html;
        try { eval(js); } catch(e) {};
      }
    }

    if (this.spin && this.busy!=null && typeof(this.target)!='function')
      document.getElementById(this.target).innerHTML = this.busy;
    this.req.open(method, uri, true);
    if (method=='POST')
      try {
        this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
      } catch(e) {};
    this.req.send(parameters);
  }

  this.getParams = function(subform) {
    if (this.url=='') {
      // alert('?Server not configured');
      return false;
    }

    if (subform==null || subform=='') subform = this.target;
    //alert(subform)
    obj = document.getElementById(subform)
    var parmstr='';
    tags = obj.getElementsByTagName('*');  
    //alert(tags.length)
    for (i=0; i<tags.length; i++) {
      //alert(parmstr);
      //alert(i + " - " + tags[i].tagName + " - "  + parmstr)    
      switch (tags[i].tagName) {
      case 'INPUT':
        switch (tags[i].type) {
        case 'hidden':
        case 'text':
        case 'password':
          parmstr += "&" + tags[i].name+'='+escape(tags[i].value);
          break;
        case 'checkbox':
          parmstr += "&" + tags[i].name+'=';
          if (tags[i].checked) parmstr += tags[i].value;
          break;
        case 'radio':
          if (tags[i].checked) parmstr += "&" + tags[i].name+'='+tags[i].value;
          break;
        default:
        }
        break;
      case 'SELECT':
        parmstr += "&" + tags[i].name+'='+escape(tags[i].options[tags[i].selectedIndex].value);
        break;
      case 'TEXTAREA':
        parmstr += "&" + tags[i].name+'='+escape(tags[i].value);
        break;
      default:
        continue;
      }
    }
    if (parmstr!='') parmstr = parmstr.substr(1,parmstr.length-1);
    //alert(parmstr);
    return parmstr;
  }
//alert(parmstr);
//alert(subform);
//alert(target);
//alert(spin);
//alert("Made it to this point in the XHR function OK");
  this.submit = function(subform, target, spin) {
    //alert("Made it to this point in the XHR function OK");
    var parmstr = this.getParams(subform);
    this.run(parmstr, target, spin);
  }
}

