// Original JavaScript code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.
// dave baldwin modifications to try to get the setstyles and setproperties working correctly

if (document.layers){
      //Netscape 4 specific code
    var  pre = 'document.';
    var  post = '';
    var  stl  = '';
    var  pro  = '';
   }
   if (document.getElementById){
      //Netscape 6 specific code
    var pre = 'document.getElementById("';
    var post = '")';
    var stl = '.style';
    var pro = '';
   }
   if (document.all){
      //IE4+ specific code
     var pre = 'document.all.';
     var post = '';
     var stl = '.style';
     var pro = '';
   }

     

function AjaxRequest()
{
  var req;
  var method = "POST";
  var nocache = false;

  // define public methods

  this.loadXMLDoc = function(url, params)
  {
    if(window.XMLHttpRequest) {
      try {
        req = new XMLHttpRequest();
      } catch(e) {
        req = false;
      }
    } else if(window.ActiveXObject) {
      try {
        req = new ActiveXObject("Msxml2.XMLHTTP");
      } catch(e) {
        try {
          req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {
          req = false;
        }
      }
    }
    if(req) {
      req.onreadystatechange = processReqChange;
      if(nocache) {
        params += (params != '') ? '&' + (new Date()).getTime() : (new Date()).getTime();
      }
      if(method == "POST") {
        req.open("POST", url, true);
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        req.send(params);
      } else {
        req.open(method, url + '?' + params, true);
        req.send(null);
      }
      return true;
    }
    return false;
  }

  this.setMethod = function(newmethod) { method = newmethod.toUpperCase(); }
  this.nocache = function() { nocache = true; }

  // define private methods

  var getNodeValue = function(parent, tagName)
  {
    var node = parent.getElementsByTagName(tagName)[0];
    return (node && node.firstChild) ? node.firstChild.nodeValue : '';
  }

  var processReqChange = function() 
  {
    if(req.readyState == 4 && req.status == 200) {
      if(!req.responseXML) return;
      var response  = req.responseXML.documentElement;
      var commands = response.getElementsByTagName('command');
      for(var i=0; i < commands.length; i++) {
        method = commands[i].getAttribute('method');
        switch(method) {
          case 'alert':
            var message = getNodeValue(commands[i], 'message');
            window.alert(message);
            break;
          case 'setvalue':
            var target = getNodeValue(commands[i], 'target');
            var v = getNodeValue(commands[i], 'val');
            if(target && v != null) {
              
              document.getElementById(target).value = v;
            }
            break;
          case 'setdefault':
            var target = getNodeValue(commands[i], 'target');
            if(target) {
              document.getElementById(target).value = document.getElementById(target).defaultValue;
            }
            break;
          case 'focus':
            var target = getNodeValue(commands[i], 'target');
            if(target) {
              document.getElementById(target).focus();
            }
            break;
          case 'setaction':
            var target = getNodeValue(commands[i], 'target');
            var v = getNodeValue(commands[i], 'val');
            if(target && v!=null) {
              document.getElementById(target).action = v;
            }
            break;
          case 'setcontent':
            var target = getNodeValue(commands[i], 'target');
            var content = getNodeValue(commands[i], 'content');
            if(target && content != null) {
              document.getElementById(target).innerHTML = content;
            }
            break;
          case 'setstyle':
            var target = getNodeValue(commands[i], 'target');
            var p = getNodeValue(commands[i], 'property');
            var v = getNodeValue(commands[i], 'val');
            if(target && p!=null && v!=null) { 
             
              document.getElementById(target).style[p] = v;
           //  eval(pre + target + post + stl + "." + p + "=" + nv);             
            }
            break;
          case 'setproperty':
            var target = getNodeValue(commands[i], 'target');
            var p = getNodeValue(commands[i], 'property');
            var v = getNodeValue(commands[i], 'val');   
            var vn = null;
            var idone = 0;

            if(v == 'true') {
            vn = true;
            idone = 1;
            } 
            if(v == 'false') {
            vn = false;
            idone = 1;
            }

            if(idone == 0) {
            if(v=='') {
            if(target == 'img_first') {
            v = 'images/spacer.gif';
            }
            if(target == 'img_tkey2') {
            v = 'images/spacer.gif';
            }
            }
            vn = "'" + v + "'";
            }

            if(target && vn!=null) {            
               eval(pre + target + post + pro + "." + p + "=" + vn  );
            }
            break;
          default:
            window.console.log("Error: unrecognised method '" + method + "' in processReqChange()");
        }
      }
    }
  }
}



