var rpcDebug = true;

function jsRPC(href)
{
  this.href = href;
  this.execute = fc;
  this.sendForm = jsRPC_sendForm;
  this.first = 1;
  this.last = '';
  this.ignoreOutOfSequence = false;
}

// Provide the XMLHttpRequest class for IE 5.x-6.x:
if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {
  try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
  try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
  try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
  throw new Error( "This browser does not support XMLHttpRequest." )
};


var k = new Object();
var kh = new Object();
var ki = 0;
function fc(Rb)
{
  if (!Rb) Rb = this.href;
  var i = ki++;
  k[i] = new XMLHttpRequest();
  if(k[i])
  {
    var d = new Date();
    if (Rb.indexOf('?') == -1)
      Rb += '?'; else Rb += '&';
    Rb += 'tz=' + escape(d.getTimezoneOffset()) + '&rnd=' + Math.random();
    kh[i] = Rb;
    k[i].open("GET", Rb, true);
    k[i].onreadystatechange = fch(i, this.ignoreOutOfSequence);
    k[i].send(null)
  }
}

function showError(message, line, href, text)
{
  var div = document.getElementById('rpc-error-div');
  if (!div)
  {
    div = document.createElement('div');
    div.id = 'rpc-error-div';
    div.style.position = 'absolute';
    div.style.bottom = '0';
    div.style.left = '0';
    div.style.width = '100%';
    div.style.height = '200px';
    div.style.overflowY = 'scroll';
    div.style.zIndex = '10000';
    div.style.color = '#FFFF00';
    div.style.backgroundColor = '#0000CC';
    document.getElementsByTagName('body')[0].appendChild(div);
  }
  else
    div.style.display = 'block';
  div.innerHTML = '<button onclick="document.getElementById(\'rpc-error-div\').style.display = \'none\'; return false;" style="float: right;">Close Error Window</button>Error: ' + message + ' (line: ' + line + ')<br />Href: ' + href + '<br /><br />' + text;
  
}

function fch(i, ignoreOutOfSequence) {

  return (
    function handler()
    {
      if (ignoreOutOfSequence)
        if (i + 1 != ki) return false;

      if(k[i].readyState == 4 && k[i].responseText)
      {
        try
        {
          eval(k[i].responseText);
        }
        catch(e)
        {
          if (rpcDebug)
          {
            showError(e.message, e.lineNumber, kh[i], k[i].responseText);
          }
        }
        k[i] = null;
      }
    }
  );
}

function jsRPC_sendForm(form)
{
  // collect all the values
  var inputs = new Object();
  
  for (var i = 0; i < form.elements.length; i++)
  {
    // determine which type of field it is then add it to the array
    el = form.elements[i];
    if ((el.type == 'textarea') || (el.type == 'text') || (el.type == 'password') || (el.type == 'hidden'))
    {
      if (typeof(inputs[el.name]) == 'undefined')
        inputs[el.name] = new Array();
      inputs[el.name][inputs[el.name].length] = el.value;
    }
    
    if ((el.type == 'radio') || (el.type == 'checkbox'))
    {
      if (el.checked)
      {
        if (typeof(inputs[el.name]) == 'undefined')
          inputs[el.name] = new Array();
        inputs[el.name][inputs[el.name].length] = el.value;
      }    
    }

    if (el.options && (el.type == 'select-one'))
    {
      if (typeof(inputs[el.name]) == 'undefined')
        inputs[el.name] = new Array();
      inputs[el.name][inputs[el.name].length] = el.options[el.options.selectedIndex].value;
    }
    
    if (el.options && (el.type == 'select-multiple'))    
    {
      if (typeof(inputs[el.name]) == 'undefined')
        inputs[el.name] = new Array();
      for (var j = 0; j < el.options.length; j++)
        if (el.options[j].selected)
          inputs[el.name][inputs[el.name].length] = el.value;
    }
  }
  
  // create a query string
  qs_text = '';
    
  for (i in inputs)
  {
    for (j = 0; j < inputs[i].length; j++)
    qs_text += escape(i) + '=' + escape(inputs[i].join(',')) + '&';
  }
  
  // post the data
  var href = this.href + '?' + qs_text;
  fc(href);
}
