/*	Simple Javascript RSS Reader Version 1.0
	Copyright (c) 2006 CS Truter
	Written by Christoff Truter
	email: Christoff@cstruter.com - (Please let me know if you intend to use the script) */
	
	
/* Replace all occurances of a string
  (Parameters) totalValue:'complete string' 
		oldValue:'value to be replaced' newValue:'value used for replace' */

function Replace(totalValue,oldValue,newValue)
{
	while(totalValue.indexOf(oldValue) > -1)
		totalValue=totalValue.replace(oldValue,newValue);
			return totalValue;
}

/* Get XML Node
   (Parameters) TagName:'XML Element' node:'Element row number' */

function getNode(xmlDoc, TagName, node)
{
	var currentNode = (node == null) ? xmlDoc.getElementsByTagName(TagName) : 
					items[node].getElementsByTagName(TagName);
	if(currentNode.length > 0)
		return currentNode[0].childNodes[0].nodeValue.replace("&amp;", "&");
}

/* Load XML Object
   (Parameters) rssFeed:'RSS File' Body:'Layer for RSS Body' Title:'Layer for RSS Title' */
   
function getNumItems(rssFeed, xmlDoc)
{
	var xmlhttp = new window.XMLHttpRequest();
	xmlhttp.open("GET",rssFeed,false);
	xmlhttp.send(null);
	
	xmlDoc = xmlhttp.responseXML.documentElement;
	items=xmlDoc.getElementsByTagName('item');
	
	return items.length; 
}

function ReadRSS(xmlDoc, Body, start, finish) 
{
	rssBody = document.getElementById(Body);
	
	SetRSSBody(xmlDoc, rssBody, start, finish);

}

/* Set HTML Template
	Did it this way to make the look and feel of the feed easy customizable, dont like mixing
	layout with code. */

function SetRSSBody(xmlDoc, rssBody, start, finish)
{
		var buffer = "";
		if (finish > items.length)
		{
			finish = items.length;
		}
		for(var i=start; i< finish; i++) 
		{	
			output = Replace(rssBody.innerHTML,"%28::Link::%29",getNode(xmlDoc, 'link',i)) ;
			output = Replace(output,"(::Link::)",getNode(xmlDoc, 'link',i)); 
			output = Replace(output,"(::Title::)",getNode(xmlDoc, 'title',i));
			output = Replace(output,"(::Pubdate::)",getNode(xmlDoc, 'pubDate',i));
			output = Replace(output,"(::Target::)",getNode(xmlDoc, 'target',i));
			output = Replace(output,"(::Description::)",getNode(xmlDoc, 'description',i));
			output = InsertImageIfExists(output, getNode(xmlDoc, 'image',i));
			buffer+= "<tr>" + output + "</tr>";
		}
		rssBody.innerHTML = buffer;
}


function InsertImageIfExists(output, nodeValue)
{
	if (nodeValue != null)
	{
		output = Replace(output,"(::Image::)","<img class=\"FeedImage\" src=\"" + nodeValue + "\" style=\"height:150px\"><br/>");
	}
	else
	{
		output = Replace(output,"(::Image::)","");
	}
	
	return output;
}

