// - you have your HTML call load
// - load calls loadSomething to set up asynchronous request
// - loadSomething calls doneLoading to handle response when it is done

// arguments are strings
// called from load so it can set up asynchronous loading
function loadSomething( uriString, targetId ) {
  document.getElementById( targetId ).innerHTML = "  Fetching data...";
  if ( window.XMLHttpRequest ) {  // not IE
    currentRequest = new XMLHttpRequest();
  }
  else if ( window.ActiveXObject ) {  // IE only
    currentRequest = new ActiveXObject( "Microsoft.XMLHTTP" );
  }
  if ( currentRequest != undefined ) {   // else unable to get a request object
    currentRequest.onreadystatechange = function() { doneLoading( uriString, targetId ); };
    currentRequest.open( "GET", uriString, true );  // true means non-blocking/asynchronous I/O
    currentRequest.send( "" );
  }
  else {
    document.getElementById( targetId ).innerHTML = " Error obtaining request object...\n";    
  }
}

// arguments are strings
// called from loadSomething when load process is completed 
function doneLoading( uriString, targetId ) {
  if ( currentRequest.readyState == 4 ) { // if currentRequest state is "loaded"
    if ( currentRequest.status == 200 ) {  // if status code is "OK"
      document.getElementById( targetId ).innerHTML = currentRequest.responseText;
    }
    else {
      document.getElementById(targetId ).innerHTML = " Error loading:\n" + 
      currentRequest.status + "\n" + currentRequest.statusText;
    }
  }
}

// arguments are strings
// main entry point called from HTML (say, onclick)
// to load something into a generic element
function load( targetString, targetElement ) {
	loadSomething( targetString, targetElement );
	return false;
}
