// Javascript tool written to pull recent news articles from XML file
// Purpose: to integrate i-Say Wordpress blog with IPS


var xmlFileName = "/recentposts.xml";  // source file
var numPosts = "1";  // Maximum number of posts to display
var language = "EN-US";  // Default language
var newsfeed;

function recentposts(numPostsParam, languageParam) {

	var txt;
	var output="";
	var articlelanguage;
	var displayed = 0;
	
	// Handle variables passed through
	if (languageParam != undefined) {
		language = languageParam;
	}
	
	if (numPostsParam != undefined) {
		numPosts = numPostsParam;	
	}
	
	if (window.XMLHttpRequest)
  	{
  		xhttp=new XMLHttpRequest();
  	}
	else // Internet Explorer 5/6
  	{
  		xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	
	try {
		xhttp.open("GET",xmlFileName,false);
		xhttp.send("");
		xmlDoc=xhttp.responseXML; 
	}
	
	catch(err){
		document.write("Error loading XML Data File.");
	}
	
	newsfeed=xmlDoc.getElementsByTagName("article");
	
	try {
		for (i=0;i<=newsfeed.length;i++) {
			
			// Read data
			url=(newsfeed[i].getElementsByTagName("link")[0].childNodes[0].nodeValue);
			title=(newsfeed[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
			articlelanguage=(newsfeed[i].getElementsByTagName("language")[0].childNodes[0].nodeValue);
			txt = "<li><a href=\"" + url + "\">" + title + "</a></li>"; 
	
			// If article is in the correct language, display it
			if (articlelanguage == language)
			{
				output = output + txt;
				displayed++;  // Increment displayed counter
			} 
			// If you hit the amount of posts requested, override loop to terminate
			if (displayed == numPosts) i=newsfeed.length;
		}
	}
	catch(err){
		document.write("Error processing XML Data.");
	}
		
	document.write("<ul>" + output + "</ul>");
}

