function sbm_createRequestObject() {
  var request;

  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = false;
      }
    }
  }

  if (!request)
    alert("Error initializing XMLHttpRequest!");

  return request;
}

var _sbm_http = Array();
var _sbmctr = 1;

function sbm_sndReq(scriptname,action) {
  // We need a fudge to prevent IE cacheing the result
  var now = new Date();

  _sbmctr++;  
  var fixedctr = _sbmctr;

  _sbm_http[_sbmctr] = sbm_createRequestObject();
  _sbm_http[_sbmctr].open('get', scriptname+'?now='+Date.UTC(now.getYear(),now.getMonth(),now.getDate(),now.getHours(),now.getMinutes(),now.getSeconds())+'&action='+action, true);
  // We build the handle funtion here to cope with the separated request objects
  _sbm_http[_sbmctr].onreadystatechange = function() {
    if (_sbm_http[fixedctr].readyState == 4) {
      if (_sbm_http[fixedctr].status == 200 || _sbm_http[fixedctr].status == 304) {
        var response = _sbm_http[fixedctr].responseText;
        var update;
        if (response.length > 0) {
          // If this looks like a php error we alert it instead of trying to process
          if (response.indexOf('T_VARIABLE') != -1) {
            alert(response);
          } else 
          if(response.indexOf('|' != -1)) {
            // Each three elements are element id|innerHTML|javascript to execute
            update = response.split('|');
            // We need to traverse the whole array
            for (var i = 0; i < update.length; i+=3) {
              var update_cell = document.getElementById(update[i]);
              // Change any html we were asked to
              if (update_cell) {
                update_cell.innerHTML = update[i+1];
              }
              // If we were asked to call a function too, do it now.
              if (update[i+2] &&
                  update[i+2].length > 0) {
                eval(update[i+2]);
              }
            }
          }
        }
      }
    }
  }
  _sbm_http[_sbmctr].send(null);
}


