


// *************************************************************************
// CAVEAT DEVELOPER: THE FOLLOWING IS JAVASCRIPT/ECMASCRIPT CODE, NOT PHP!!!
// *************************************************************************



// ---------------------------------------------------------------------------
// ajaxRead - initiate fetching an AJAX object, which is assumed to be valid
// XML.
// Accepts: file/url, target object, reference to return data handler function
// Returns: none, since the A in AJAX stands for Asynchronous. :-) Return
// data is handled by the function that ajaxRead() registers for this purpose.
// ---------------------------------------------------------------------------
function ajaxRead (file, ajaxTarget, updateHandler) {
  var xmlObj = null;

  // If the target document has an 'ajaxRotator' object, display it:
  if (document.getElementById('ajaxRotator'))
      document.getElementById('ajaxRotator').style.display = 'block';

  // The ubiquitous code branching:
  if (window.XMLHttpRequest) {
    xmlObj = new XMLHttpRequest ();
  } else {
    if (window.ActiveXObject) {
      xmlObj = new ActiveXObject ("Microsoft.XMLHTTP");
    } else {
      alert ('Fatal: Your web browser does not support AJAX features.');
      return;
    }
  }
  // At this point, xmlObj is our object handler for the Ajax object. Now
  // set up the state change event handler that updates the target object:
  xmlObj.onreadystatechange = function () {	// Register return data handler
    if (xmlObj.readyState == 4) {
      window [updateHandler](ajaxTarget, xmlObj.responseXML);
    }
  }
  // Then submit an asynchronous request to fetch the XML data:
  xmlObj.open('GET', file, true);
  xmlObj.send(null);
}


// ---------------------------------------------------------------------------
// Update target object with data using innerHTML. Advantage: fast, simple,
// whole blocks of HTML, markup and all, can be inserted in one go.
// Disadvantage: not standardized, and therefore unpredictable, and
// innerHTML updates the rendered HTML and not the DOM data, meaning
// that updates to input fields in forms, for example, do not reflect
// in the submitted data. Bottom line: use this for simple stuff only.
//
// Accepts: reference to object to update,
//          the data to update the target object with (string)
// Note: the data is supposed to be a string. If it is a (x)HTML snippet,
//       are supposed to be htmlentities-ized (e.g. &lt;123&gt; and not
//       <123>). Having XML or (x)HTML tags here won't work.
// ---------------------------------------------------------------------------
function ajaxUpdateObjInnerHTML (targetObj, xmlDataObj) {

    var data = xmlDataObj.getElementsByTagName('data')[0].firstChild.data;
  document.getElementById(targetObj).innerHTML = data;

  // If the target document has an 'ajaxRotator' object, hide it:
  if (document.getElementById('ajaxRotator'))
      document.getElementById('ajaxRotator').style.display = 'none';
}


// ---------------------------------------------------------------------------
// Update a <select></select> container with <option></option> combo's
// which are returned by ajaxRead(), using the DOM rather than innerHTML().
// The XML data is supposed to be properly formatted.
//
// Accepts: reference to target object to update (assumed to be a <select>),
//          the data to update the target object with.
// ---------------------------------------------------------------------------
function ajaxUpdateSelect (target, xmlDataObj) {

  var targetObj = document.getElementById(target);

  // Clear old target object content:
  while (targetObj.firstChild)
    targetObj.removeChild(targetObj.firstChild);

  var options = xmlDataObj.getElementsByTagName('option');
  for (var i = 0; i < options.length; i++) {
    var opt = document.createElement('option');	// New <option> object
    opt.text = options[i].firstChild.nodeValue;		// Get option text...
    opt.value = options[i].attributes.getNamedItem('value').value; // and value

    if ((options[i].attributes.getNamedItem('selected')) &&
        (options[i].attributes.getNamedItem('selected').value == "selected"))
      opt.selected="true";

    try {
      targetObj.add (opt, null);		// Standards compliant...
    }
    catch (ex) {
      targetObj.add (opt);			// ...and IE only. (Of course.)
    }
  } 
  // If the target document has an 'ajaxRotator' object, hide it:
  if (document.getElementById('ajaxRotator'))
      document.getElementById('ajaxRotator').style.display = 'none';
}


// EOF

