/*	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;
}
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
		// will store the reference to the XMLHttpRequest object
		var xmlHttp;
		
		// if running Internet Explorer
		if(window.ActiveXObject)
		{
			try
			{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			
			}
			catch (e)
			{
			xmlHttp = false;
			}
		}
		// if running Mozilla or other browsers
		else
		{
			try
			{
			xmlHttp = new XMLHttpRequest();
			}
			catch (e)
			{
			xmlHttp = false;
			}
		}
			// return the created object or display an error message
			if (!xmlHttp)
			alert("Error creating the XMLHttpRequest object.");
			else
			return xmlHttp;
}
/* Get XML Node
   (Parameters) TagName:'XML Element' node:'Element row number' */

function getNode(TagName, node)
{
	var currentNode = (node == null) ? xmlDoc.getElementsByTagName(TagName) : 
					items[node].getElementsByTagName(TagName);
	if(currentNode.length > 0)
		return currentNode[0].firstChild.nodeValue;
}

/* Load XML Object
   (Parameters) rssFeed:'RSS File' Body:'Layer for RSS Body' Title:'Layer for RSS Title' */

function ReadRSS(rssFeed, Body, Title) 
{
	
	
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
	

	rssTitle = document.getElementById(Title);	
	rssBody = document.getElementById(Body);
	
	var method="GET";
	var  bool ="true";
	
	xmlHttp.open(method,rssFeed ,bool );

	xmlHttp.onreadystatechange = handleServerResponse;
	
	xmlHttp.send(null);
	
	
	
	}
	else
	// if the connection is busy, try again after one second
	{
		//alert("Problem retrieving XML data");
	}
	
	
	


	
}
function handleServerResponse()
{	


	if (xmlHttp.readyState == 4)
	{
	

	if (xmlHttp.status == 200)
	{
	//responseText=xmlHttp.responseText;
	//alert("responseText/"+responseText)
	xmlResponse = xmlHttp.responseXML;
	
	xmlDocumentElement = xmlResponse.documentElement;
	//alert("xmlDocumentElement"+xmlDocumentElement)
	
	items = xmlDocumentElement.getElementsByTagName('item');
	
	//alert("items..."+items)
	if (items) {
		
		//alert("response 1")
		
	}
	else 
	{
		
		//alert("response 0")
		
	}
	
	SetRSSTemplates();
	}
	
	}
}
/* 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 SetRSSTemplates()
{
	if (rssBody)
	{
		var buffer = "";
		for(var i=0; i< items.length; i++) 
		{
			var output = (document.all) ? Replace(rssBody.innerHTML,"(::Link::)",getNode('link',i)) 
									   : Replace(rssBody.innerHTML,"%28::Link::%29",getNode('link',i));
			output = Replace(output,"(::Picture::)",getNode('Picture',i));
			output = Replace(output,"(::Title::)",getNode('title',i));
			output = Replace(output,"(::Pubdate::)",getNode('pubDate',i));
			output = Replace(output,"(::Description::)",getNode('description',i));
			buffer+=output;
		}
		rssBody.innerHTML = buffer;
	}

	if (rssTitle)
	{
		var output = Replace(rssTitle.innerHTML,"(::Title::)",getNode('title'));
		output = (document.all) ? Replace(output,"(::Link::)",getNode('link'))
							   : Replace(output,"%28::Link::%29",getNode('link'));		
		output = Replace(output,"(::Description::)",getNode('description'));
		rssTitle.innerHTML = output;
	}
}
