/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn(button, img)
{
  var xmlhttp, bComplete = false;
  var requestTimeout = 12000;
  var displayTimeout = 350;

  var XHI = new XHConnIndicator(button, img);
  
  try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { xmlhttp = false; }}}
  
  this.connect = function(sURL, sMethod, sVars, fnDone, status_id)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();
    
    XHI.buttonoff();

    window.setTimeout(
      function()
      {
        if (!bComplete)
        {
          XHI.imageon();
        }
      }, displayTimeout);

    //sVars = encodeURI(sVars);
    
    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = '';
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      }
      
      xmlhttp.onreadystatechange = function()
      {
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          var networkErr = false;
          try  { var s = xmlhttp.statusText;  }
          catch (e)  {  networkErr = e;  }
          XHI.stop();
          fnDone(xmlhttp, networkErr);
        }
      };
      
      window.setTimeout(
        function()
        {
          if (!bComplete)
          {
            xmlhttp.abort();
            XHI.stop();
            fnDone(xmlhttp, 'The server is taking too long to respond. The request has been cancelled.');
            
          }
        }, requestTimeout);

   
      xmlhttp.send(sVars);
    }

    catch(z) { return false; }
    return true;
  };

  this.create_vars =  function(f, command)
  {
    var encode = (typeof(encodeURIComponent) == 'function') ? encodeURIComponent : escape;
    var result='';
    if (typeof(f)=='undefined') { return result; }
    if (typeof(f.elements)=='undefined') { return result; }
    result = 'cmd=' + command;
    for (k=0; k < f.elements.length; k++)
    {
      var i = f.elements[k];
      if (i.type != 'button')
      {
        if (i.type != 'checkbox' || i.checked) { result += '&' + i.name + '=' + encode(i.value); }
      }
    }
    return result;
  }
}

function XHConnIndicator(button, img)
{

  this.l_button = (typeof(button)=='object') ? button : null;
  this.l_img = (typeof(img)=='object') ? img : null;
  
  this.buttonoff = function()
  {
    if (this.l_button != null) this.l_button.disabled=true;
  }
  
  this.imageon = function()
  {
    if (this.l_img != null) this.l_img.style.visibility = 'visible';
  }

  this.stop = function()
  {
    if (this.l_button != null) this.l_button.disabled=false;
    if (this.l_img != null) this.l_img.style.visibility = 'hidden';
  }
}
