var RSSRequestObject = false; // XMLHttpRequest Object

var Local = '/rss/local.rss'; // Local News
var National = '/rss/national.rss'; // National News
var World = '/rss/world.rss'; // World News
var Sports = '/rss/sports.rss'; // Sports News
var mlb = '/rss/mlb.rss'; // MLB Scores
var nfl = '/rss/nfl.rss'; // NFL Scores
var nba = '/rss/nba.rss'; // NBA Scores
var nhl = '/rss/nhl.rss'; // NHL Scores
var ncaafb = '/rss/ncaafb.rss'; // NCAA FB Scores


if (window.XMLHttpRequest) // try to create XMLHttpRequest
	RSSRequestObject = new XMLHttpRequest();

if (window.ActiveXObject)	// if ActiveXObject use the Microsoft.XMLHTTP
	RSSRequestObject = new ActiveXObject("Microsoft.XMLHTTP");

if (window.XMLHttpRequest) // try to create XMLHttpRequest
	RSSRequestObject2 = new XMLHttpRequest();

if (window.ActiveXObject)	// if ActiveXObject use the Microsoft.XMLHTTP
	RSSRequestObject2 = new ActiveXObject("Microsoft.XMLHTTP");

if (window.XMLHttpRequest) // try to create XMLHttpRequest
	RSSRequestObject3 = new XMLHttpRequest();

if (window.ActiveXObject)	// if ActiveXObject use the Microsoft.XMLHTTP
	RSSRequestObject3 = new ActiveXObject("Microsoft.XMLHTTP");

/*
* onreadystatechange function
*/
function ReqChange() {

	// If data received correctly
	if (RSSRequestObject.readyState==4) {
	
		// if data is valid
		if (RSSRequestObject.responseText.indexOf('invalid') == -1) 
		{ 	
			// Parsing RSS
			var node = RSSRequestObject.responseXML.documentElement; 
			
               var channel = node.getElementsByTagName('channel').item(0);  
			
			content = '<ul class=\"newsul\">';
		
			// Browse items
			var items = channel.getElementsByTagName('item');
			for (var n=0; n < 7; n++)
			{
				var itemTitle = items[n].getElementsByTagName('title').item(0).firstChild.data;
				var itemLink = items[n].getElementsByTagName('link').item(0).firstChild.data;
					
				content += '<li class=\"newsli\"><a class=\"newsa\" href="'+itemLink+'">'+itemTitle+'</a></li>';
			}
			
			
			content += '</ul>';
			// Display the result
			document.getElementById("ajaxnews").innerHTML = content;

			// Tell the reader the everything is done
			document.getElementById("statusnews").innerHTML = "Done.";
			
		}
		else {
			// Tell the reader that there was error requesting data
			document.getElementById("statusnews").innerHTML = "<div class=error>Error requesting data.<div>";
		}
	}
	
}

function ReqChangeSports() {

	// If data received correctly
	if (RSSRequestObject2.readyState==4) {
	
		// if data is valid
		if (RSSRequestObject2.responseText.indexOf('invalid') == -1) 
		{ 	
			// Parsing RSS
			var node = RSSRequestObject2.responseXML.documentElement; 
			
               var channel = node.getElementsByTagName('channel').item(0);  
			
			content = '<ul class=\"sportsul\">';
		
			// Browse items
			var items = channel.getElementsByTagName('item');
			for (var n=0; n < 7; n++)
			{
				var itemTitle = items[n].getElementsByTagName('title').item(0).firstChild.data;
				var itemLink = items[n].getElementsByTagName('link').item(0).firstChild.data;
					
				content += '<li class=\"sportsli\"><a class=\"sportsa\" href="'+itemLink+'">'+itemTitle+'</a></li>';
			}
			
			
			content += '</ul>';
			// Display the result
			document.getElementById("ajaxsports").innerHTML = content;

			// Tell the reader the everything is done
			document.getElementById("statussports").innerHTML = "Done.";
			
		}
		else {
			// Tell the reader that there was error requesting data
			document.getElementById("statussports").innerHTML = "<div class=error>Error requesting data.<div>";
		}
	}
	
}

function ReqChangeScores() {

	// If data received correctly
	if (RSSRequestObject3.readyState==4) {
	
		// if data is valid
		if (RSSRequestObject3.responseText.indexOf('invalid') == -1) 
		{ 	
			// Parsing RSS
			var node = RSSRequestObject3.responseXML.documentElement; 
			
               var channel = node.getElementsByTagName('channel').item(0);  
               
               var prevDate = "";
	
		     content = "<div style=\"width: 350px; height: 150px; margin: 5px auto 5px auto; overflow: auto; \">";
		     
			// Browse items
			var items = channel.getElementsByTagName('item');
			for (n=0;n<items.length;n++)
			{
			     /* var pubDate = items[n].getElementsByTagName('pubDate').item(0).firstChild.data;
			     var newDate = pubDate.split(" ");
			     
			     var currDate = newDate[2] + " " + newDate[1];
			     
			     if(currDate != prevDate)
			     {
                         content += "<p>" + currDate + "</p>";
                    }
                    prevDate = currDate;
                    */
			     
				var score = items[n].getElementsByTagName('description').item(0).firstChild.data;
				score = score.replace("<h2>Major League Baseball</h2>", "");
				score = score.replace("<h2>NFL</h2>", "");
				score = score.replace("<p>", "");
				score = score.replace("</p>", "");
				var scoreSplit = score.split("<br>");
				
				n++;
				
				if(n<items.length)
				{
				 var score2 = items[n].getElementsByTagName('description').item(0).firstChild.data;
				 score2 = score2.replace("<h2>Major League Baseball</h2>", "");
				 score2 = score2.replace("<h2>NFL</h2>", "");
				 score2 = score2.replace("<p>", "");
				 score2 = score2.replace("</p>", "");
				 var score2Split = score2.split("<br>");
				} else {
				 var blank = "<br>";
				 var score2Split = blank.split("<br>");
				}
				
			     
			     content += "<table width=\"320\" border=\"1\"><tr><td style=\"text-align: right;\" width=\"160\">"+scoreSplit[0]+"</td><td style=\"text-align: right;\">"+score2Split[0]+"</td></tr><tr><td style=\"text-align: right;\">"+scoreSplit[1]+"</td><td style=\"text-align: right;\">"+score2Split[1]+"</td></tr></table><br />";
			}
			
			content += "</div>";
			
			// Display the result
			document.getElementById("ajaxsports").innerHTML = content;

			// Tell the reader the everything is done
			document.getElementById("statussports").innerHTML = "Done.";
			
		}
		else {
			// Tell the reader that there was error requesting data
			document.getElementById("statussports").innerHTML = "<div class=error>Error requesting data.<div>";
		}
	}
	
}


/*
* Main AJAX RSS reader request
*/
function RSSRequest(whichOne) {

	// change the status to requesting data
	document.getElementById("statusnews").innerHTML = "Requesting data ...";
	
	if (whichOne == 'Local')
	{
	// Prepare the request
	RSSRequestObject.open("GET", Local , true);
	// Set the onreadystatechange function
	RSSRequestObject.onreadystatechange = ReqChange;
	// Send
	RSSRequestObject.send(null); 
	}
	else if (whichOne == 'National')
	{
     // Prepare the request
	RSSRequestObject.open("GET", National , true);
	// Set the onreadystatechange function
	RSSRequestObject.onreadystatechange = ReqChange;
	// Send
	RSSRequestObject.send(null); 
     }
     else if (whichOne == 'World')
     {
     // Prepare the request
	RSSRequestObject.open("GET", World , true);
	// Set the onreadystatechange function
	RSSRequestObject.onreadystatechange = ReqChange;
	// Send
	RSSRequestObject.send(null); 
     }
     else if (whichOne == 'mlb')
     {
     // Prepare the request
	RSSRequestObject3.open("GET", mlb , true);
	// Set the onreadystatechange function
	RSSRequestObject3.onreadystatechange = ReqChangeScores;
	// Send
	RSSRequestObject3.send(null); 
     }
     else if (whichOne == 'nfl')
     {
     // Prepare the request
	RSSRequestObject3.open("GET", nfl , true);
	// Set the onreadystatechange function
	RSSRequestObject3.onreadystatechange = ReqChangeScores;
	// Send
	RSSRequestObject3.send(null); 
     }
     else if (whichOne == 'nba')
     {
     // Prepare the request
	RSSRequestObject3.open("GET", nba , true);
	// Set the onreadystatechange function
	RSSRequestObject3.onreadystatechange = ReqChangeScores;
	// Send
	RSSRequestObject3.send(null); 
     }
     else if (whichOne == 'nhl')
     {
     // Prepare the request
	RSSRequestObject3.open("GET", nhl , true);
	// Set the onreadystatechange function
	RSSRequestObject3.onreadystatechange = ReqChangeScores;
	// Send
	RSSRequestObject3.send(null); 
     }
     else if (whichOne == 'ncaafb')
     {
     // Prepare the request
	RSSRequestObject3.open("GET", ncaafb , true);
	// Set the onreadystatechange function
	RSSRequestObject3.onreadystatechange = ReqChangeScores;
	// Send
	RSSRequestObject3.send(null); 
     }
     else
     {
     RSSRequestObject2.open("GET", Sports , true);
     // Set the onreadystatechange function
	RSSRequestObject2.onreadystatechange = ReqChangeSports;
	// Send
	RSSRequestObject2.send(null); 
     }
	
}

function GetObject(id){
	var el = document.getElementById(id);
	return(el);
}

function setActiveTab(tabID, wrongTab1, wrongTab2) { 

var currTabElem = document.getElementById(tabID); 
currTabElem.setAttribute("class", "current");
currTabElem.setAttribute("className", "current");

var TabElem2 = document.getElementById(wrongTab1);
TabElem2.removeAttribute("class");
TabElem2.removeAttribute("className");

var TabElem3 = document.getElementById(wrongTab2);
TabElem3.removeAttribute("class");
TabElem3.removeAttribute("className");


}

function setActiveFourTab(tabID, wrongTab1, wrongTab2, wrongTab3) { 

var currTabElem = document.getElementById(tabID);
currTabElem.setAttribute("class", "current");
currTabElem.setAttribute("className", "current");

var TabElem2 = document.getElementById(wrongTab1);
TabElem2.removeAttribute("class");
TabElem2.removeAttribute("className");

var TabElem3 = document.getElementById(wrongTab2);
TabElem3.removeAttribute("class");
TabElem3.removeAttribute("className");

var TabElem4 = document.getElementById(wrongTab3);
TabElem4.removeAttribute("class");
TabElem4.removeAttribute("className");

} 

