
function getHTTPObject() {
	/*
	 This gets the object we need to communicate with the host.
	Called from other js functions, not the HTML itself.
	Thanks to Jeremy Keith. 
	*/
  var xhr = false;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        xhr = false;
      }
    }
  }
  return xhr;
}


function grabfile(file) {
	/*	
	Fetch a file from host and id an event handler to deal with the result.
	Typical use is...
	<a href="one.txt" onclick="grabfile(this.href); return false;">One</a>
	...where "one.txt" is the name of the file we want. 
	Note that we display the loading gif at the target spot "content"
	Also change the image source.  
	Watch out! Because the "file" passed here comes from an Href we get a file path name.
	So... href="Services" ...yields... "http://www.someurl.com/services"
	*/
  var request = getHTTPObject();
  if (request) {
    displayLoading(document.getElementById("content"));
    request.onreadystatechange = function() {
      displayResponse(request);
    };
    var filepart = trim(file);
    var filename = filepart + ".txt";
    request.open("GET", filename, true);
    request.send(null);

    filename = filepart + "label.jpg";
    var imagetarget = document.getElementById("pagelabel");
    imagetarget.setAttribute("src", filename);
  }
}


function displayResponse(request) {
	/* 
	Event handler for the file returned from the host.  
	The content of the file will replace the content of an HTML
	tag with the id of "content" as in...
	<p id="content">Replace this content</p> 
	*/
  if (request.readyState == 4) {
    if (request.status == 200 || request.status == 304) {
	var details = document.getElementById("content");
	details.innerHTML = request.responseText;
    }
  }
}


function doLoadFunctions() {
	/*
	Do this when the page loads.
	We want to display the home page text. 
	The name of that file is the content of the "content" division, for example...
	<div class="content" id="content"> HomePage </div>
	"HomePage.txt" is the name of the file we want to grab.  
	*/
  var target = document.getElementById("content");
  var target = target.firstChild;
  grabfile(target.nodeValue);
}


function displayLoading(element) {
	/*
	Display the animated loading gif at the target spot.
	The gif will be overwritten by the results, when (if) they arrive.
	Prior to all that we remove all nodes currently in the target spot.  
	*/
  while (element.hasChildNodes()) {
    element.removeChild(element.lastChild);
  }
  var image = document.createElement("img");
  image.setAttribute("src","loading.gif");
  image.setAttribute("alt","Loading...");
  element.appendChild(image);
}


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}