var FeedName = 'feed.xml';
var FeedRealDir = '/dualgadgets/jsrss/';
var FeedAbsDir = '/var/www/data/virtual/ces-cz.eu/htdocs/dualgadgets/jsrss/';

function processFeedRequest(httpFeedRequest)
{
	if (httpFeedRequest.readyState == 4)
	{
		if(( httpFeedRequest.status >= 200 && httpFeedRequest.status < 300 ) || httpFeedRequest.status == 304)
		{
			//alert(httpFeedRequest.responseText);
		}
		else
		{
			var errorHappendHere = "Not found downloading core";
		}
	}
}

function FeedDownload(uriSource, uriTarget)
{
	var httpFeedRequest;
	
	if(typeof window.ActiveXObject != 'undefined')
	{
		httpFeedRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		httpFeedRequest = new XMLHttpRequest();
	}
	
	httpFeedRequest.open("GET", FeedRealDir+"rss.php?uriSource="+uriSource+"&uriTarget="+uriTarget, true);
	httpFeedRequest.onreadystatechange = function () { processFeedRequest(httpFeedRequest) };
	httpFeedRequest.send(null);
}

function Replace(totalValue,oldValue,newValue)
{
	while(totalValue.indexOf(oldValue) > -1)
		totalValue=totalValue.replace(oldValue,newValue);
			return totalValue;
}

function getNode(TagName, node)
{
	var currentNode = (node == null) ? xmlDoc.getElementsByTagName(TagName) : 
					items[node].getElementsByTagName(TagName);
	if(currentNode.length > 0)
		return currentNode[0].firstChild.nodeValue;
}

function ReadRSS(rssFeedSource, Body, Title, Limit)
{
	rssTitle = document.getElementById(Title);	
	rssBody = document.getElementById(Body);

	try
	{
		if (document.all)
		{
			var errorHappendHere = "Check Browser and security settings";
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		}
		else
		{
			var errorHappendHere = "Apparently one cant read remote xml via firefox, please copy the file to your server";
			xmlDoc = document.implementation.createDocument("","",null);
		}
		
		xmlDoc.async=false;
		xmlDoc.load(FeedRealDir+FeedName);
	
		items=xmlDoc.getElementsByTagName('item');
		SetRSSTemplates(Limit);
		FeedDownload(rssFeedSource, FeedAbsDir+FeedName);
	}
	
	catch(e)
	{
		rssTitle.innerHTML = 'Error occured';
		rssBody.innerHTML = 'Thrown Error:'+e.message+"<br/>Note: "+errorHappendHere;
	}
}

function SetRSSTemplates(Limit)
{
	if (rssBody)
	{
		if(Limit == 0)
		{
			Limit = items.length;
		}
		
		var buffer = "";
		for(var i=0; i< Limit; i++)
		{
			var output = (document.all) ? Replace(rssBody.innerHTML,"(::Link::)",getNode('link',i)) 
									   : Replace(rssBody.innerHTML,"%28::Link::%29",getNode('link',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;
	}
}
