// Encoding: UTF-8
// Latest update: 2008-07-04

var url_root = "http://users.belgacom.net/gaudete/";
var url_start = url_root + "index.html";
var url_feed = url_root + "nieuws.xml";



// keep page out of frames
if (window.top.location != window.self.location){
	window.top.location.href = window.self.location;
}


/**
 * Checks if an element has a given class
 * @param {Object} e			Element to check.
 * @param {String} theClass		Classname to check for.
 */
function hasClass(e, theClass){
	if(e.className) {
		return(e.className.match( new RegExp('\\b'+theClass+'\\b','g')));
	}
	return false;
}
	


function twodigits(inValue)
{
	if (inValue > 9) {
		return inValue;
	}
	else {
		return "0" + inValue;
	}
}

function DagNaam(d) // vertaal dagnummer 0..6 naar dagnaam zondag..zaterdag
{
 dn = ["zondag","maandag","dinsdag","woensdag","donderdag","vrijdag","zaterdag"];
 return ((d > 6) ? "?" : dn[d]); 
}

function MaandNaam(m) // vertaal maandnummer 0..11 naar maandnaam januari..december
{
 mn = ["januari","februari","maart","april","mei","juni","juli","augustus","september","oktober","november","december"];
 return ((m > 11) ? "?" : mn[m]); 
}

function dagDDmaandJJJJ(date) // formatteer een Date object als "dag DD maand JJJJ"
{
 return (DagNaam(date.getDay()) + " " + date.getDate() + " " + MaandNaam(date.getMonth())+ " " + date.getFullYear());
}

function DDmaandJJJJ(date) // formatteer een Date object als "DD maand JJJJ"
{
 return (date.getDate() + " " + MaandNaam(date.getMonth())+ " " + date.getFullYear());
}

function DDmaand(date) // formatteer een Date object als "DD maand" (bijv. "1 april")
{
 return (date.getDate() + " " + MaandNaam(date.getMonth()));
}

function UUMMSS(date)	// formatteer een Date object als "UU:MM:SS"
{
 return (twodigits(date.getHours()) + ':' + twodigits(date.getMinutes()) + ':' + twodigits(date.getSeconds()));
}

function lastMod()
{
	// (functie ontleend aan www.quirksmode.org en licht aangepast)

	if  (document.lastModified === ""){return "";}
	
	var x = new Date (document.lastModified);
	Modif = new Date(x.toGMTString());
	Year = takeYear(Modif);
	Month = Modif.getMonth();
	Day = Modif.getDate();
	Mod = (Date.UTC(Year,Month,Day,0,0,0))/86400000;
	x = new Date();
	today = new Date(x.toGMTString());
	Year2 = takeYear(today);
	Month2 = today.getMonth();
	Day2 = today.getDate();
	now = (Date.UTC(Year2,Month2,Day2,0,0,0))/86400000;
	daysago = now - Mod;
	if (daysago < 0) {
		return '';
	}
	unit = 'dagen';
	if (daysago > 730)
	{
		daysago = Math.floor(daysago/365);
		unit = 'jaar';
	}
	else if (daysago > 60)
	{
		daysago = Math.floor(daysago/30);
		unit = 'maanden';
	}
	else if (daysago > 14)
	{
		daysago = Math.floor(daysago/7);
		unit = 'weken';
	}
	var towrite = 'Pagina laatst gewijzigd: ';
	if ((daysago === 0) && (unit == 'dagen')) {
		towrite += 'vandaag';
	}
	else if ((daysago == 1) && (unit == 'dagen')) {
		towrite += 'gisteren';
	}
	else if ((daysago == 2) && (unit == 'dagen')) {
		towrite += 'eergisteren';
	}
	else {
		towrite += daysago + ' ' + unit + ' geleden';
	}
	return towrite;
}

function takeYear(theDate)
{
	var x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}

function DocTimeStamp()
{
/* var lm = new Date(document.lastModified);
 return("Pagina laatst gewijzigd op " + DDmaandJJJJ(lm));*/
 return(lastMod());
}

function DaysToGo(YYYY,MM,DD)
{
	Future = (Date.UTC(YYYY,MM-1,DD,0,0,0))/86400000;
//	document.write("Future = " + Future);
	x = new Date();
	today = new Date(x.toGMTString());
	Year2 = takeYear(today);
	Month2 = today.getMonth();
	Day2 = today.getDate();
	now = (Date.UTC(Year2,Month2,Day2,0,0,0))/86400000;
	daystogo = Future - now;
	return daystogo;
}



function Today(){		/* geef vandaag weer als YYYYMMDD */
	var today,d = new Date();
	today = d.getFullYear();
	var mm = d.getMonth()+1;
	today+= ((mm<10)?"0":"") + mm;
	var dd = d.getDate();
	today+= ((dd<10)?"0":"") + dd;
	return today;
}


// add a paragraph with the page's last modification date
function PageTimestamp( id ){
	var objPara = document.createElement("p");
	if (lastMod) {
		objPara.appendChild(document.createTextNode(lastMod()));
	}
	document.getElementById( id ).appendChild(objPara);
}

// add a paragraph with the page URL
function PageUrl( id ){
	var objPara = document.createElement("p");
	objPara.appendChild(document.createTextNode("URL: "));
	objPara.appendChild(document.createTextNode(document.URL));
	document.getElementById( id ).appendChild(objPara);
}


/**
 * Adds a paragraph with the page validation links, selectable by a bitfield.
 * @param {String} id
 * @param {Number} flags
 */ 
function PageValidate( id , flags ) {
	var checks = [
		{bit: 0x01,	href: "http://validator.w3.org/check/referer", 										add: "", 			title: "test de HTML code", 	test: "HTML"},
		{bit: 0x02, href: "http://jigsaw.w3.org/css-validator/check/referer",							add: "", 			title: "test de CSS code",		test: "CSS"},
		{bit: 0x04, href: "http://www.icra.org/sitelabel/",												add: "", 			title: "test het ICRA label",	test: "ICRA"},
		{bit: 0x08, href: "http://feedvalidator.org/check.cgi?url=",									add: url_feed, 		title: "test de RSS feed",		test: "RSS"},
		{bit: 0x10, href: "http://microformatique.com/optimus/?format=validate&uri=",					add: document.URL, 	title: "test de microformats",	test: "uF"},		
		{bit: 0x20, href: "http://validator.w3.org/checklink?check=Check&hide_type=all&summary=on&uri=",add: document.URL, 	title: "test de links",			test: "links"}
		];
	var empty = true ;
	var p = document.createElement("p");

	for (var i = 0; i < checks.length; i++) {
		if ((flags & checks[i].bit) == checks[i].bit) {
			if (!empty) {
				p.appendChild(document.createTextNode(" - "));
			}
			
			var a = document.createElement("a");
			a.setAttribute("href", checks[i].href + checks[i].add);
			a.setAttribute("title", checks[i].title);
			a.className = "noredirect";		// don't redirect through go.html (these tests work on the original page URL)
			a.appendChild(document.createTextNode(checks[i].test));
			p.appendChild(a);
			empty = false;
		}
	}
	document.getElementById( id ).appendChild(p);
}




/**
 * Use of the query string in URL's
 */
var query = {
	/**
	 * Returns the actual query data as an object.
	 * @return {Object}
	 */
	toObject : function(){
		var name, t;
		var q = {};
		
		// get the query, without the leading '?'
		var x = window.location.search.substring(1);
		// replace plusses with blanks
		x = x.replace(/\+/g, ' ');
		// semicolons as separators are nonstandard, but we accept them (not required if query was created by ourselves)
		x = x.replace(/;/g, '&');
		// split at every '&'
		x = x.split('&');
		
		for (var i = 0; i < x.length; i++){
			t = x[i].split('=', 2);
			name = decodeURIComponent(t[0]);
			if (!q[name]) {
				q[name] = [];
			}
			if (t.length > 1) {
				q[name][q[name].length] = decodeURIComponent(t[1]);
			}
			// next two lines are nonstandard
			else {
				q[name][q[name].length] = true;
				// if not a 'name=value' pair given, but only a name, then this is assumed to be a boolean which received a value of true
			}
		}
		return q;
	},
	/**
	 * Returns a query string from a two-dimensional array of name/value pairs.
	 * @param {Array} aa		Two-dimensional array; list of name/value pairs.
	 * @return {String}
	 */
	create : function( aa ){
		var result = "";
		for( var p = 0; p < aa.length; p++ ){
			// add an ampersand & (except the first time)
			if(result !== ""){ result += "&"; }
			// add the 'name' part (properly encoded)
			result += encodeURIComponent(aa[p][0]);
			if (aa[p].length > 1) {
				// add an equal sign =
				result += "=";
				// add the 'value' part (properly encoded)
//				result += encodeURIComponent(aa[p][1]);
				result += encodeURIComponent(aa[p][1]).replace(/\%20/g ,"+");	// replace %20 (space) with a '+'
			}
		}
		// prepend a query sign ?
		if(result !== ""){ result = "?" + result; }
		
		return result;
	}
};



/**
 * Modifies all external links' href to go through the 'go.html' page.
 * 
 * Several methods are possible, selected with 0/1 in the inner 'if else else' test.
 * Attention: Some browsers (e.g. Opera 9 and Safari 3) still have the redirected url in the href
 * when returning to the page with the 'Back' button. We must take care to not redirect twice!
 * 
 * encodeURIComponent() is required for those URL's containing a ? = or &
 */
function externalLinksGo(){
	var encode = function(uri){
		return encodeURIComponent(uri);
	};
	var sToAdd = "go.html?url=";
	var a = document.getElementsByTagName("a");
	for(var i = 0; i < a.length; i++){
		// external links with class "noredirect" must not be changed (e.g. page checks HTML, CSS etc...)
		if(hasClass(a[i], "noredirect")){
			continue;
		}
		if( a[i].href.substring(0,7) == "http://"){
			if (a[i].href.substring(0, url_root.length) !== url_root) {
				if (0) {// method 1: directly modify the href (debug mode)
					//  Take care to not redirect twice!
					var j = a[i].href.indexOf(sToAdd);
					if (j == -1) {
						a[i].href = sToAdd + encode(a[i].href);
					}
//					else{
//						d_alert(sToAdd + " found at index " + j + " onload()");
//					}
				}
				else { // method 3: make the jump at onclick() [dont't change the href; no problems with the Back key]
					a[i].onclick = function(){
												window.location.href = sToAdd + encode(this.href);
												return false;
					};					
				}
			}
		}
	}
}


/**
 * Loads an XML file 
 * and either processes it with the given function (asynchronous mode; preferred method) 
 * or returns it as a DOM object (synchronous mode).
 * @param {String} filename			the XML file to load
 * @param {Object} [processXML]		function to call when the XML is ready (asynchronous!); if absent, works synchronously
 * @return {Object}					in async mode: success flag; in sync mode: the XML object
 */
function loadXML( filename, processXML ){
	/* Supported browsers		used method
	 * --------------------		--------------------------------------------
	 * - Internet Explorer		ActiveX		(Note: an IE-only method without ActiveX is described at http://dean.edwards.name/weblog/2006/04/easy-xml/)
	 * - Firefox				XMLHttpRequest
	 * - Opera					XMLHttpRequest
	 * - Safari					XMLHttpRequest
	 * - Chrome					XMLHttpRequest
	 */
	var async = ( typeof(processXML) == 'function' ) ;
	//alert("async = " + async ) ;

	var xml = null, req = false;
	
	try {
		// method for Internet Explorer (ActiveX)
		xml = new ActiveXObject("Microsoft.XMLDOM");
		xml.async = async;
		if( async ){
			xml.onreadystatechange = function(){
				if (xml.readyState == 4) {
					processXML(xml);
				}
			};
		}
		//alert("using method ActiveX");
		xml.load(filename);
		return (async ? true : xml );	// success
	}
	catch(e) {
		//alert("ActiveX failed");
		xml = null;
	}

	try {
		// this method works in FF, Opera, Safari and Chrome (info at http://ajaxpatterns.org/XMLHttpRequest_Call)
		req = new XMLHttpRequest();
		if( async ) {
			req.onreadystatechange = function(){
				if ( (req.readyState == 4) ) {
					processXML(req.responseXML);
				}
			};
		}
		req.open("GET", filename, async);
		req.send("");
		if( ! async ){
			xml = req.responseXML;
		}
		return (async ? true : xml );	// success
	} 
	catch (e) {
		//alert("XMLHttpRequest() failed");
		xml = null;
		req = false;
	}
	
	try {
		// this method works in FF, Opera (not Safari, not Chrome) (from the original from http://www.puuba.com)
		if(document.implementation && document.implementation.createDocument){
			xml = document.implementation.createDocument("", "", null);
			if (async) {
				xml.onload = function(){
					processXML(xml);
				};
			}
			xml.load(filename);
			return (async ? true : xml );	// success
		}
	}
	catch (e) {
		//alert("document.implementation failed");
		xml = null;
		req = false;
	}

	return (async ? false : null );	// failure
}


function addSiteheaderNavigationItem(text, href, title)
{
	var siteheader = document.getElementById("siteheader");
	var siteheaderdivs = siteheader.getElementsByTagName("div");
	
	for (var i = 0; i < siteheaderdivs.length; i++){
		if(hasClass(siteheaderdivs[i], "navigation")){
			var navigation = siteheaderdivs[i];
			var a;
			
			if (title && (title == text)) {
				a = document.createElement("span");
				a.innerHTML = text;
				navigation.appendChild(a);
			}
			else {
				a = document.createElement("a");
				a.href = href;
				a.innerHTML = text;
				navigation.appendChild(a);
			}
			
			// truuk om het 'redraw' probleem van IE6 op te lossen
			siteheader.style.display = "none";
			siteheader.style.display = "block";
			
			return true;
		}
	}
	return false;
}


function globalAtEndOfPage(title){
	// voeg de tab "Nieuws" toe op het hoofdmenu
	addSiteheaderNavigationItem("Nieuws", "nieuws.html", title);

	// set external links
	externalLinksGo();
}


