// JavaScript Document

var RSSRequestObject = false; // 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");


// 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() {

	// 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; 
			
			
			// Get Channel information
			//var channel = node.getElementsByTagName('channel').item(0);
			var title = "";
			var link = "";
			
			content = '<ul>';
		
			// Browse items
			var items = node.getElementsByTagName('item');
			for (var n=0; n < 3; n++)
			{
				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 itemPubDate = '<font color=gray>['+items[n].getElementsByTagName('pubDate').item(0).firstChild.data+'] ';
				} 
				catch (e) 
				{ 
					var itemPubDate = '';
				}
				
			
				content += '<li><a href="'+itemLink+'">'+itemTitle+'</a></li>';
			}

			content += '</ul>';
			
			
			// Display the result
			document.getElementById("ajaxreader").innerHTML = content;

			
			
		}
		else {
			// Tell the reader that there was error requesting data
			document.getElementById("ajaxreader").innerHTML = "<div class=error>Error requesting data.<div>";
		}
	}
	
}

/*
* Main AJAX RSS reader request
*/
function RSSRequest(loc) {

    if(!loc)
        return;
    
	// change the status to requesting data
	document.getElementById("ajaxreader").innerHTML = "Requesting data ...";
	//alert(Backend+"?location="+loc);
        
	// Prepare the request
	RSSRequestObject.open("GET", Backend+"?location="+loc , true);
	// Set the onreadystatechange function
	RSSRequestObject.onreadystatechange = ReqChange;
	// Send
	RSSRequestObject.send(null); 
}

/*
* Timer
*/
function update_timer() {
	RSSRequest();
}


function GetObject(id){
	var el = document.getElementById(id);
	return(el);
}
