﻿function SniffBrowser() 
{
   var ua = navigator.userAgent.toLowerCase(); 
   var isOpera       = (ua.indexOf('opera') != -1); 
   var isFireFox	 = (ua.indexOf('firefox') != -1); 
   var isIE          = (ua.indexOf('msie') != -1 && !isOpera && (ua.indexOf('webtv') == -1) );
   var versionMinor = parseFloat(navigator.appVersion); 
   
   if (isIE && (versionMinor >= 4))
   {
      versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
   }   
   var versionMajor = parseInt(versionMinor); 
   var isIE5up = (isIE && versionMajor >= 5);
   
   if (ua.indexOf('msn') > 0)
   {
		return("msn explorer");
   }
   
   if(isIE5up)
   {
	return("IE5up");
   }
   else if (isFireFox)
   {
    return ("FireFox");
   }
   else
   {
	return("other");
   }
}

var gIsFireFox = (SniffBrowser() == "FireFox");
// JScript File

// global ActiveX Control for XML
var xhr;


//OBJECTS

//objects inside the RSS2Item object
function RSS2Enclosure(encElement)
{
	if (encElement == null)
	{
		this.url = null;
		this.length = null;
		this.type = null;
	}
	else
	{
		this.url = encElement.getAttribute("url");
		this.length = encElement.getAttribute("length");
		this.type = encElement.getAttribute("type");
	}
}

function RSS2Guid(guidElement)
{
	if (guidElement == null)
	{
		this.isPermaLink = null;
		this.value = null;
	}
	else
	{
		this.isPermaLink = guidElement.getAttribute("isPermaLink");
		this.value = guidElement.childNodes[0].nodeValue;
	}
}

function RSS2Source(souElement)
{
	if (souElement == null)
	{
		this.url = null;
		this.value = null;
	}
	else
	{
		this.url = souElement.getAttribute("url");
		this.value = souElement.childNodes[0].nodeValue;
	}
}

//object containing the RSS 2.0 item
function RSS2Item(itemxml)
{
	//required
	this.title;
	this.link;
	this.description;

	//optional vars
	this.author;
	this.comments;
	this.pubDate;

	//optional objects
	this.category;
	this.enclosure;
	this.guid;
	this.source;
	
	// gallery custom fields
	this.softDownloadLink;
	this.softRating;
	this.softImage;

	var properties = new Array("title", "link", "description", "author", "comments", "pubDate");
	var tmpElement = null;
	for (var i=0; i<properties.length; i++)
	{
		tmpElement = itemxml.getElementsByTagName(properties[i])[0];
		if (tmpElement != null)
			eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
	}

	this.category = new RSS2Category(itemxml.getElementsByTagName("category")[0]);
	this.enclosure = new RSS2Enclosure(itemxml.getElementsByTagName("enclosure")[0]);
	this.guid = new RSS2Guid(itemxml.getElementsByTagName("guid")[0]);
	this.source = new RSS2Source(itemxml.getElementsByTagName("source")[0]);
	
	// gallery custom feilds	
	this.softDownloadLink = new RSS2Source(itemxml.getElementsByTagName("software:DownloadLink")[0]);
	this.softRating = new RSS2Source(itemxml.getElementsByTagName("software:Rating")[0]);
	this.softImage = new RSS2Source(itemxml.getElementsByTagName("software:image")[0]);
}

//objects inside the RSS2Channel object
function RSS2Category(catElement)
{
	if (catElement == null)
	{
		this.domain = null;
		this.value = null;
	}
	else
	{
		this.domain = catElement.getAttribute("domain");
		this.value = catElement.childNodes[0].nodeValue;
	}
}

//object containing RSS image tag info
function RSS2Image(imgElement)
{
	if (imgElement == null)
	{
	this.url = null;
	this.link = null;
	this.width = null;
	this.height = null;
	this.description = null;
	}
	else
	{
		imgAttribs = new Array("url","title","link","width","height","description");
		for (var i=0; i<imgAttribs.length; i++)
			if (imgElement.getAttribute(imgAttribs[i]) != null)
				eval("this."+imgAttribs[i]+"=imgElement.getAttribute("+imgAttribs[i]+")");
	}
}

//object containing the parsed RSS 2.0 channel
function RSS2Channel(rssxml)
{
	//required
	this.title;
	this.link;
	this.description;

	//array of RSS2Item objects
	this.items = new Array();

	//optional vars
	this.language;
	this.copyright;
	this.managingEditor;
	this.webMaster;
	this.pubDate;
	this.lastBuildDate;
	this.generator;
	this.docs;
	this.ttl;
	this.rating;

	//optional objects
	this.category;
	this.image;

	var chanElement = rssxml.getElementsByTagName("channel")[0];
	var itemElements = rssxml.getElementsByTagName("item");

	for (var i=0; i<itemElements.length; i++)
	{
		Item = new RSS2Item(itemElements[i]);
		this.items.push(Item);
		//chanElement.removeChild(itemElements[i]);
	}

	var properties = new Array("title", "link", "description", "language", "copyright", "managingEditor", "webMaster", "pubDate", "lastBuildDate", "generator", "docs", "ttl", "rating");
	var tmpElement = null;
	for (var i=0; i<properties.length; i++)
	{
		tmpElement = chanElement.getElementsByTagName(properties[i])[0];
		if (tmpElement!= null)
			eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
	}

	this.category = new RSS2Category(chanElement.getElementsByTagName("category")[0]);
	this.image = new RSS2Image(chanElement.getElementsByTagName("image")[0]);
}
function CreateXmlHttp()
{
    var obj = null;
// try recent
    try
    {
        obj =new ActiveXObject("Msxml2.XMLHTTP.4.0");
        return obj;
    }
    catch(ex)
    {
    }
// try older
    try
    {
        obj =new ActiveXObject("MSXML2.XMLHTTP");
        return objp;
    }
    catch(ex)
    {
    }
// try oldest
    try
    {
        obj=new ActiveXObject("Microsoft.XMLHTTP");
        return obj;
    }
    catch(ex)
    {
    }
    return obj;
};


function getRSS(theUrl)
{
    
	xhr = CreateXmlHttp();
	
	xhr.onreadystatechange = function() 
	{
		if (xhr.readyState == 4)
		{
			if (xhr.status == 200)
			{
				if (xhr.responseText != null)
					processRSS(xhr.responseXML);
				else
				{
					//alert("Failed to receive RSS file from the server - file not found.");
					return false;
				}
			}
			else
			{
				//alert("Error code " + xhr.status + " received: " + xhr.statusText);
			    return false;
			}
		}
	}
	xhr.open("GET",theUrl,true);
	xhr.setRequestHeader("Cache-Control", "no-cache");
	xhr.setRequestHeader("Pragma", "no-cache");	
	
	//send the request
	xhr.send(null);
	
	return true;
}

//processes the received rss xml
function processRSS(rssxml)
{
	RSS = new RSS2Channel(rssxml);
	showToolbarRss(RSS);
}

function showToolbarRss(RSS, locationId)
{	
//populate the items
	var oTable = document.getElementById("rssFeedTable");
	var strDownload = document.getElementById("aDwonloadText");
	var strMoreInfo = document.getElementById("aMoreInfoText");
	
	for (var i=0; i<RSS.items.length; i++)
	{	    
		item_html = "<div id="+i+">";
		item_html += (RSS.items[i].title == null) ? "" : "<b>" + RSS.items[i].title + "</b><br>";
		item_html += (RSS.items[i].description == null) ? "" : RSS.items[i].description + "<br>";
		item_html += (RSS.items[i].link == null) ? "" : "<a href=\""+ RSS.items[i].link + "\">" + strMoreInfo.innerHTML + "</a>";
		item_html += (RSS.items[i].softDownloadLink == null) ? "" : " | <a href=\""+ RSS.items[i].softDownloadLink.value + "\">" + strDownload.innerHTML + "</a> <br>";
		item_html += "</div>";
		
		oRow = oTable.insertRow();
	    
	    oTdImg = oRow.insertCell();
		oTdImg.innerHTML="<img alt=\""+ RSS.items[i].title + "\" src=\""+ RSS.items[i].enclosure.url+"\" width=\"40\" height=\"40\">";
		
	    oTdData = oRow.insertCell();
	    oTdData.innerHTML = item_html;
	   
	}
}
function customButton(vtitle, vdescription,vlink, vdownload, vimgurl)
{
    this.title = vtitle;
    this.description = vdescription;
    this.link = vlink;
    this.softDownloadLink = vdownload;
    this.imgurl = vimgurl;
}
function showToolbar(items)
{	
//populate the items
	var oTable = document.getElementById("rssFeedTable");
	var oRow = null;
	var strDownload = document.getElementById("aDwonloadText");
	var strMoreInfo = document.getElementById("aMoreInfoText");
	
	for (var i=0; i<items.length; i++)
	{	    
		item_html = "<div id="+i+">";
		item_html += (items[i].title == null) ? "" : "<b>" + items[i].title + "</b><br>";
		item_html += (items[i].description == null) ? "" : items[i].description + "<br>";
		item_html += (items[i].link == null) ? "" : "<a target=new href=\""+ items[i].link + "\">" + strMoreInfo.innerHTML + "</a>";
		item_html += (items[i].softDownloadLink == null) ? "" : " | <a href=\""+ items[i].softDownloadLink + "\">" + strDownload.innerHTML + "</a> <br>";
		item_html += "</div>";
		
		if (gIsFireFox)
			oRow = document.createElement("tr");
		else
		    oRow = oTable.insertRow();
		    
	    var oTdImg = null;
	    var oTdData = null;
	    if (gIsFireFox)
	    {
		    oTdImg = document.createElement('td');
		    oTdData = document.createElement('td');		
	    }
	    else
	    {
	        oTdImg = oRow.insertCell();
	        oTdData = oRow.insertCell();
	    }
		oTdImg.innerHTML="<img alt=\"\" src=\""+ items[i].imgurl+"\" width=\"40\" height=\"40\">";
		oTdData.innerHTML = item_html;	
		
		if (gIsFireFox)
	    {
		    oRow.appendChild(oTdImg);		
		    oRow.appendChild(oTdData);
	    	oTable.appendChild(oRow);
	     }
	}
	
			
}
