// JavaScript Document

// for any value over set number, gets the position of the last blank going backwards, cuts everything off and adds the ... 

function Truncate(s, n){
    
    if(s.length > n){
        var t =  s.lastIndexOf (" ", n);
        s= s.substring(0,t);
        s += "...";
    }//end if
    
    return s;
}//end Truncate()



/*
* onreadystatechange function
*/

function ReqChange(requestObject, outputTarget) {

	// If data received correctly
	if (requestObject.readyState==4) {
		// if data is valid - ask jason about this part right here.  blog returns some 3318 number when alerting
		//alert(requestObject.responseText.indexOf('invalid'));
		//if (requestObject.responseText.indexOf('invalid') == -1) 
		if (requestObject.responseText.indexOf('invalid') != 0) 
		{ 	
			// Parsing RSS
			var node = requestObject.responseXML.documentElement; 
			
			// Get Channel information
			//var channel = node.getElementsByTagName('channel').item(0);
			var title = "";
			var link = "";
			//var date = "";
			content = '';
					
			// Browse items
			var items = node.getElementsByTagName('item');	
						
//this is where you change out the number of items, the <=0; is what you change
for (var n=0; n <= 0; n++)
			{
				var itemTitle = items[n].getElementsByTagName('title').item(0).firstChild.data;
				var s = items[n].getElementsByTagName('title').item(0).firstChild.data;
				var itemTitle =Truncate(s, 50);
				
			var itemLink = items[n].getElementsByTagName('link').item(0).firstChild.data;
				try 
			{ 
					var itemMoreInfo = items[n].getElementsByTagName('moreInfo').item(0).firstChild.data;
					} 
				catch (e) 
				{ 
						var itemMoreInfo = '';
				}
				
			//content += '<div><a href="'+itemLink+'">'+itemTitle+'</a> </li><li class="rss_date">'+itemMoreInfo+'</div>';
		
			//checks if itemMoreInfo is easy is blank, if so it's the blog and display info differently, otherwise display pr and events
			
			if (itemMoreInfo==''){
			
				var pubDate = items[n].getElementsByTagName('pubDate').item(0).firstChild.data;	
			
				pubDateArray=pubDate.split(" ");
				
				//remove any date of zero with nothing			
				pubDateArrayFormat=pubDateArray[1].replace(01,"1");
				pubDateArrayFormat=pubDateArray[1].replace(02,"2");
				pubDateArrayFormat=pubDateArray[1].replace(03,"3");
				pubDateArrayFormat=pubDateArray[1].replace(04,"4");
				pubDateArrayFormat=pubDateArray[1].replace(05,"5");
				pubDateArrayFormat=pubDateArray[1].replace(06,"6");	
				pubDateArrayFormat=pubDateArray[1].replace(07,"7");
				pubDateArrayFormat=pubDateArray[1].replace(08,"8");
				pubDateArrayFormat=pubDateArray[1].replace(09,"9");		
			   
			   //update variable with array values which were out out of order for the blog
				pubDate=pubDateArray[2] + " " + pubDateArrayFormat + ", " + pubDateArray[3];
				
				content += '<div><a href="'+itemLink+'">'+itemTitle+'</a></div> <div class="rss_date">'+pubDate+'</div>';	
				
				//content += '<div><a href="'+itemLink+'">'+itemTitle+'</a></div> <div class="rss_date">'+pubDate+'</div>';	
			}
		
			else{
				content += '<div><a href="'+itemLink+'">'+itemTitle+'</a></div> <div class="rss_date">'+itemMoreInfo+'</div>';
					}

			}
			
		//	content += 'View More Information...';
			
			// Display the result
			//document.getElementById(outputTarget).innerHTML = content;

		}
		else {
			// Tell the reader that there was error requesting data

			content = '<div>Error requesting data... please check back later.<br><br></div>';
			
			//document.getElementById(outputTarget).innerHTML = "<div class=error>Error requesting data.<div>";
		}
	}
	
}

/*
* Main AJAX RSS reader request
*/
function RSSRequest(outputTarget,loc) {

	var RSSRequestObject = null; // XMLHttpRequest Object
	var Backend = 'feed.php'; // Backend url
	//window.setInterval("update_timer()", 1200000); // update the data every 20 mins

	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(!loc)
        return;
    
	// change the status to requesting data
	document.getElementById(outputTarget).innerHTML = "Loading...";

	//alert(Backend+"?location="+loc);
        
	// Prepare the request
	RSSRequestObject.open("GET", Backend+"?location="+loc , true);
	// Set the onreadystatechange function
	RSSRequestObject.onreadystatechange = function(){
		
		if (RSSRequestObject.readyState < 4)
			return;
		
		ReqChange(RSSRequestObject, outputTarget);
		
						
		if(loc=="http://www.netqos.com/rss/netqos_press_releases.xml"){
		content += '<a href="/newsroom/press_releases/2009/index.html">Read More News</a>';	
		}
		
		else if (loc=="http://www.netqos.com/rss/netqos_events_global.xml"){
		content += '<a href="/newsroom/events.html">View More Events</a>';	
		}
		
		else if (loc=="http://feeds2.feedburner.com/NetworkPerformanceDaily"){
		content += '<a href="http://www.networkperformancedaily.com">Visit the Blog</a>';	
		}
		document.getElementById(outputTarget).innerHTML = content;
	}
	// Send
	RSSRequestObject.send(null); 
}

/*
* Timer
*/
function update_timer() {
	RSSRequest();
}



function GetObject(id){
	var el = document.getElementById(id);
	return(el);
}