//Basic Ajax Routine- Author: Dynamic Drive (http://www.dynamicdrive.com)
//Last updated: Jan 15th, 06'

function GetE(elementId){
	return document.getElementById( elementId )  ;
}

function createAjaxObj(){
	var httprequest=false
	if (window.XMLHttpRequest){ // if Mozilla, Safari etc
		httprequest=new XMLHttpRequest()
		if (httprequest.overrideMimeType)
			httprequest.overrideMimeType('text/xml')
	}
	else if (window.ActiveXObject){ // if IE
		try {
			httprequest=new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
		try{
			httprequest=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e){}
		}
	}
	return httprequest
}

var ajaxpack=new Object()
ajaxpack.basedomain="http://"+window.location.hostname
ajaxpack.ajaxobj=createAjaxObj()
ajaxpack.filetype="txt"
ajaxpack.addrandomnumber=0 //Set to 1 or 0. See documentation.

ajaxpack.getAjaxRequest=function(url, parameters, callbackfunc, filetype){
	ajaxpack.ajaxobj=createAjaxObj() //recreate ajax object to defeat cache problem in IE
	if (ajaxpack.addrandomnumber==1) //Further defeat caching problem in IE?
		var parameters=parameters+"&ajaxcachebust="+new Date().getTime()
	if (this.ajaxobj){
		this.filetype=filetype
		this.ajaxobj.onreadystatechange=callbackfunc
		this.ajaxobj.open('GET', url+"?"+parameters, true)
		this.ajaxobj.send(null)
	}
}

function processGet(){
	var myajax=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax.readyState == 4){ //if request of file completed
		if (myajax.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful or running script locally
			var temp_text = ""; // Tijdelijke text variabele (hier staat de string met de content in
			if (myfiletype=="xml") { // het XML object
				xmlItems=myajax.responseXML.getElementsByTagName("item");
				temp_text += '<ul>';
				for (var i=0; i<xmlItems.length; i++){
					temp_text += '<li><a href="'+xmlItems[i].getElementsByTagName('link')[0].firstChild.nodeValue+'" id="link_rss">';
					temp_text += filterDate(xmlItems[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue)+' '+xmlItems[i].getElementsByTagName("title")[0].firstChild.nodeValue;
					temp_text += '</a></li>';
				}
				temp_text += '</ul>';
			} else {
				temp_text = "There are currently no news items";
			}
			GetE('rss_data2').innerHTML = temp_text; // De HTML in de DIV zetten
			showItem(xmlItems[0],'rss_data2');
			showItem(xmlItems[1],'rss_data3');
		}
	}
}

function showItem(xObj,eObj) {
	var outputString = '<div class="news_item">';
	imgNode = xObj.getElementsByTagName("enclosure")[0].firstChild;
	
	outputString += '<div class="news_title"><b><a href="'+xObj.getElementsByTagName('link')[0].firstChild.nodeValue+'" id="link_newstitle">';
	outputString += xObj.getElementsByTagName("title")[0].firstChild.nodeValue;
	outputString += '</b></a></div>'

	GetE(eObj).innerHTML = outputString; // De HTML in de DIV zetten	
}

function filterDate(ufdate) {
	if(ufdate.length == 8) {
		var toFilterDate = new String(ufdate);
		fYear = toFilterDate.substr(0,4);
		fMonth = toFilterDate.substr(4,2);
		fDay = toFilterDate.substr(6,2);
		filteredDate = fDay + '-' + fMonth + '-' + fYear;
		return filteredDate;
	} else {
		var todayDate = new Date();
		filteredDate = todayDate.getDate() + '-' + todayDate.getMonth() + '-' + todayDate.getYear();
		return filteredDate;
	}
	
}
