//BlogFunctions.js
//
// This file contains javascript functions for loading and displaying the Blog Feed on an HTML page
// It requires the presence of 5 Div elements to recieve the last 5 blog entries. 
// They must be of the form BlogFeedi Where i is an number 1 to 5.
//
// Date			Who		What
// 30-OCT-2011  DanF		Created
//
// Copyright HealthLink 2011
//

// blogFuncitonsAhah		asynchrious HTML and HTTP function to fetch the Blog data
//
// url		The url to load the feed data from
// target	The Root Div identity for setting the values on the page
// Returns 	Nothing
//
function blogFunctionsAhah(url, target) {
  document.getElementById(target+"0").innerHTML = ' Fetching data...';
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {blogFunctionsAhahDone(target);};
    req.open("GET", url, true);
    req.send("");
  }
}  

// blogFuncitonsAhahDone		Loads the fetched data into the form
//
// target	The Root Div identity for setting the values on the page
//
function blogFunctionsAhahDone(target) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
	  var i=0;
	  //Load all of the data into the first cell
	  document.getElementById(target+i).innerHTML = req.responseText;
	  
	  //Now spread the points out to have there own entries
	  for (i=1; i<3; i++) {
		//alert(document.getElementById('healthLinkBlogEntry'+i).innerHTML);
		document.getElementById(target+i).innerHTML = document.getElementById('healthLinkBlogEntry'+i).innerHTML;
	  }
	  //Now reset the first cell to just a single entry
	  document.getElementById(target+0).innerHTML = document.getElementById('healthLinkBlogEntry'+0).innerHTML;
	  
    } else {
      document.getElementById(target).innerHTML=" AHAH Error:\n"+ req.status + "\n" +req.statusText;
    }
  }
}

// Load
//
// name		The url to load the feed data from
// div		The Root Div identity for setting the values on the page
// Returns 	Nothing
//
function blogFunctionsLoad(name, div) {
	blogFunctionsAhah(name,div);
	return false;
}
