// JavaScript Document
//
// Searches for body, extracts and return the content
// New version contributed by users
//
function getBody(content) {
   test = content.toLowerCase();    // to eliminate case sensitivity
   var x = test.indexOf("<body");
   if(x == -1) return "";

   x = test.indexOf(">", x);
   if(x == -1) return "";

   var y = test.lastIndexOf("</body>");
   if(y == -1) y = test.lastIndexOf("</html>");
   if(y == -1) y = content.length;    // If no HTML then just grab everything till end

   return content.slice(x + 1, y);   
} 

//
//	Loads a HTML page
//	Put the content of the body tag into the current page.
//	Arguments:
//		url of the other HTML page to load
//		id of the tag that has to hold the content
//

function loadHTML(url, fun, storage, param) {
  var xhr = createXHR();
  xhr.onreadystatechange=function() { 
    if(xhr.readyState == 4) {
		storage.innerHTML = getBody(xhr.responseText);
    } 
  };
  xhr.open("GET", url , true);
  xhr.send(null); 
} 

//
// Callback
// Assign directly a tag
//
function processHTML(temp, target) {
  target.innerHTML = temp.innerHTML;
}

function loadWholePage(url) {
  var y = document.getElementById("storage");
  var x = document.getElementById("displayed");
  loadHTML(url, processHTML, x, y);
}


//
// Create responseHTML
// for acces by DOM's methods
//
function processByDOM(responseHTML, target) {
  target.innerHTML = "Extracted by id:<br />";

  var message = responseHTML.getElementsByTagName("div").item(1).innerHTML;
  target.innerHTML += message;
  message = responseHTML.getElementsByTagName("form").item(0);
  target.innerHTML += message.dyn.value;
}

function accessByDOM(url) {
  //var responseHTML = document.createElement("body");	// Bad for opera
  var responseHTML = document.getElementById("storage");
  var y = document.getElementById("displayed");
  loadHTML(url, processByDOM, responseHTML, y);
}