/*
 * These functions retrieve XML data asynchronously from somewhere on
 * the web. This 'should' (but need not necessarily) be from the same
 * site the main web page came from.  In this version, only one fetch
 * can be active at a time.
 */

/*
Sample elements from the XML response:
  xmlDoc=xmlhttp.responseXML;
  x = xmlDoc.getElementsByTagName("firstname")[0].childNodes[0].nodeValue;
  x = xmlDoc.getElementsByTagName("lastname")[0].childNodes[0].nodeValue;
  x = xmlDoc.getElementsByTagName("job")[0].childNodes[0].nodeValue;
  x = xmlDoc.getElementsByTagName("age")[0].childNodes[0].nodeValue;
  x = xmlDoc.getElementsByTagName("hometown")[0].childNodes[0].nodeValue;
 */


var xmlobj;
var what_element = false; // fetches initiate only when this = false.
var fetch_this;

/*
 * Main function to load XML data.  Receive:
 *  - URL of document to fetch
 *  - datum we are to fetch from the returned result
 *  - the document element that should receive the result
 */
function loadXMLDoc(url, we_want, element_id)
{
  if (what_element)
  {
    // Something else is already fetching. Wait half a second, try again.
    var r = 'loadXMLDoc("' + url + '","' + element_id + '","' + we_want + '")';
    setTimeout(r, 500);
    return;
  }
  xmlobj = GetXmlHttpObject();
  if (xmlobj == null)
  {
    alert("Your browser does not support XMLHTTP.");
    return;
  }
  what_element = element_id;
  fetch_this = we_want;
  xmlobj.onreadystatechange = stateChanged;
  xmlobj.open("GET",url,true);
  xmlobj.send(null);
}

/*
 * Browser-specific function to return a request object.
 */
function GetXmlHttpObject()
{
  if (window.XMLHttpRequest)
  {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
  }
  if (window.ActiveXObject)
  {
    // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
  return null;
}

/*
 * Fetch and return the tag named 'tagName' from its XML parent, 'parent'.
 */
function getNodeValue(parent, tagName)
{
  var node = parent.getElementsByTagName(tagName)[0];
  if (node) {
    if (node.firstChild)
      return node.firstChild.nodeValue;
  }
  return false;
}

/*
 * Fetch element 'nm' from global XML object 'xmlobj'.
 */
function fetchElement(nm)
{
  var ret = '';
  var r = xmlobj.responseXML.documentElement;
  var elem = r.getElementsByTagName('data');
  for(var i = 0; i < elem.length; i++)
  {
    if (i > 0) ret = ret + ' ';
    var r = getNodeValue(elem[i], nm);
    if (r != false) {
      ret = ret + r;
    }
  }
  return ret;
}

/*
 * Async function to handle document state changes.  When state
 * becomes 4, the document is fully loaded.  This function uses
 * global variables 'what_element' and 'fetch_this', clearing
 * 'what_element' when the document is fully loaded.
 */
function stateChanged()
{
  if (xmlobj.readyState == 4)
  {
    // We are done fetching. Let others run.
    var w = what_element;
    var e = fetch_this;
    what_element = false;

    if (xmlobj.status == 200)
    {
	  var o = document.getElementById(w);
	  if (o) {
        var a = fetchElement(e);
        if (a) o.innerHTML = a;
      }
      else {
        alert("Document element '" + w + "' does not exist.");
      }
    }
    else
    {
      alert('Error ' + xmlobj.status + ' retrieving XML data: '
            + xmlobj.statusText);
    }
  }
}

