/**
*** class constructor
**/

function xmlreq()
{
  this.req = _xobject();
  this.reqid = 0;
  this.setid = _xsetid;
  this.getid = _xgetid;
  this.array = new Array();
  this.setvar = _xsetvar;
  this.getvar = _xgetvar;
  this.text = _xtext;
  this.onrecv = null;
  this.open = _xopen;
  this.send = _xsend;
  this.abort = _xabort;
}





/**
*** create request object
**/

function _xobject()
{
  if (window.XMLHttpRequest)
    return(new XMLHttpRequest());

  return(new ActiveXObject(
    "Microsoft.XMLHTTP"));
}





/**
*** process request session
**/

function _xsend()
{
  var obj = this;

  this.req.onreadystatechange = function()
  {
    if (obj.req.readyState != 4)
      return;

    var items = new Array();

    if (obj.req.responseXML)
      items = obj.req.responseXML.
        getElementsByTagName('option');

    for (var i = 0; i < items.length; i++)
    {
      var name = items.item(i).
        getElementsByTagName('name').
          item(0).firstChild.nodeValue;

      var value = items.item(i).
        getElementsByTagName('value').
          item(0).firstChild.nodeValue;

      obj.array[name] = value;
    }

    obj.onrecv();
  }

  this.req.send(xarr(this.array));
}





/**
*** extra: prepare request xml
**/

function xarr(a)
{
  var option = String('');

  for (var i in a)
  {
    option += '<option>';
    option += '<name>' + xenc(i) + '</name>';
    option += '<value>' + xenc(a[i]) + '</value>';
    option += '</option>';
  }

  option = '<array>' + option + '</array>';
  option = '<?xml version="1.0"?>' + option;

  return(option);
}





/**
*** extra: escape url string
**/

function xenc(s)
{
  s = String(s);
  s = s.replace(/&/g, '&amp;');
  s = s.replace(/</g, '&lt;');
  s = s.replace(/</g, '&lt;');
  return(s);
}





/**
*** function aliases
**/

function _xsetid(n)
{
  this.reqid = n;
}
function _xgetid()
{
  return(this.reqid);
}
function _xsetvar(n, v)
{
  this.array[n] = v;
}
function _xgetvar(n)
{
  return(this.array[n]);
}
function _xtext()
{
  return(this.req.responseText);
}
function _xopen(u)
{
  this.req.open('POST', u, true);
}
function _xabort()
{
  this.req.abort();
}
