/*  
*  Standaardscripts voor ministeriÎle websites
*
*  eend ~ maakt internet menselijk
*  ~ Peter-Paul Koch
* 
*  v0.6 - 02/06/2007  */
/*  TEKSTGROOTTE
	Knoppen en functionaliteit worden automatisch gegenereerd in
	het element met id="tekstgrootte" */
var paginaTaal; // welke taal gebruikt de pagina? Gezet in initializeAll
var bloklinks = {
	speciaal: ['extern','anker','download'],
	init: function () {
		var lists = document.getElementsByTagName('li');
		for (var i=0;i<lists.length;i++) {
			if (lists[i].className.indexOf('bloklink') == -1) continue;
			var ul = lists[i].parentNode;
			if (!ul.gedaan) {
				ul.className += ' js-aan';
				ul.gedaan = true;
			}
			var classes = lists[i].className;
			var classesHover = classes + ' over';
			for (var j=0;j<this.speciaal.length;j++) {
				if (classes.indexOf(this.speciaal[j]) != -1) {
					classesHover = classes.replace(this.speciaal[j],this.speciaal[j]+'Over');
					break;
				}
			}
			lists[i].origClass = classes;			
			lists[i].hoverClass = classesHover;
			lists[i].onmouseover = function () {
				this.className = this.hoverClass;
			}
			lists[i].onmouseout = function () {
				this.className = this.origClass;
			}
			lists[i].onclick = function () {
				var href = this.getElementsByTagName('a')[0].href;
				location.href = href;
				return false;
			}
		}
	}
}
/* zoekresultatentabel, exclusief voor minSZW */
var zoekResults = {
	init: function () {
		if (document.getElementById('zoekres')) {
			var deTabel = document.getElementById('zoekres').getElementsByTagName('table')[0];
			if (deTabel) {
				deTabel.id = 'js-aan';
				var x = document.getElementById('zoekres').getElementsByTagName('tr');
				for (var i=0;i<x.length;i++) {
					var y = x[i].getElementsByTagName('td')[3];
					if (!y) continue;
					x[i].link = y.getElementsByTagName('a')[0].href;
					x[i].onmouseover = function () { this.className = 'htr'; }
					x[i].onmouseout = function () { this.className = ''; }
					/* Dit geeft problemen bij rel="popup"
					x[i].onclick = function () {
						location.href = this.link;
					}
					*/
				}
			}
		}
	}
}
/* INITIALISATIE aangeroepen door DOMContentReady */
var inits = [
	'bloklinks',
	'zoekResults'
];
function initializeAll() {
	paginaTaal = document.documentElement.lang || 'nl';
	for (var i=0;i<inits.length;i++) {
		if (window[inits[i]]) {
			window[inits[i]].init(); 
		}
	}
}
/* UTILITIES */
function listify(dataArray) {
	var x = document.createElement('ul');
	for (var i=0;i<dataArray.length;i++) {
		var y = document.createElement('li');
		y.appendChild(dataArray[i]);
		x.appendChild(y);
	}
	return x;
}
function sendRequest(url,callback,postData) {
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
			// alert('HTTP error ' + req.status);
			return;
		}
		callback(req);
	}
	if (req.readyState == 4) return;
	req.send(postData);
}
var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("MSXML2.XMLHTTP.6.0")},
	function () {return new ActiveXObject("MSXML2.XMLHTTP.3.0")},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")},
];
function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try { xmlhttp = XMLHttpFactories[i](); }
		catch (e) { continue; }
		break;
	}
	return xmlhttp;
}
function checkField(formName,fieldName) {
	var postdata = new Object();
	if(document.getElementById(fieldName).type == "checkbox") {
		var postdata = "formname="+encodeURI(formName)+"&fieldname="+encodeURI(fieldName)+"&fieldvalue="+encodeURI(document.getElementById(fieldName).checked);
	}
	else {
		var postdata = "formname="+encodeURI(formName)+"&fieldname="+encodeURI(fieldName)+"&fieldvalue="+encodeURI(document.getElementById(fieldName).value);
	}
	sendRequest("index.cfm?fuseaction=app.validateField", validationResponse, postdata);
}
function validationResponse(req) {
	var hasError = req.responseXML.getElementsByTagName("validationerror")[0].firstChild.nodeValue;
	var fieldName = req.responseXML.getElementsByTagName("fieldname")[0].firstChild.nodeValue;
	var errorMessage = req.responseXML.getElementsByTagName("errormessage")[0].firstChild.nodeValue;
	if(hasError == 'true') {
		document.getElementById("error_"+fieldName).className = "fout";
		document.getElementById("error_"+fieldName).innerHTML = '<img src="img/icon_alert.gif" width="12" height="12" alt="let op!">'+errorMessage;
		document.getElementById("label_"+fieldName).className = "fout";
		if(document.getElementById(fieldName).type != "checkbox") {
			document.getElementById(fieldName).className = "breed fout";
		}
	}
	else {
		document.getElementById("error_"+fieldName).className = "fout hidden";
		document.getElementById("label_"+fieldName).className = "";
		if(document.getElementById(fieldName).type != "checkbox") {
			document.getElementById(fieldName).className = "breed";
		}
	}
}
// push and shift for IE5
function Array_push() {
	var A_p = 0;
	for (A_p = 0; A_p < arguments.length; A_p++) { this[this.length] = arguments[A_p]; }
	return this.length;
}
if (typeof Array.prototype.push == "undefined") { Array.prototype.push = Array_push; }
function Array_shift() {
	var A_s = 0;
	var response = this[0];
	for (A_s = 0; A_s < this.length-1; A_s++) { this[A_s] = this[A_s + 1]; }
	this.length--;
	return response;
}
if (typeof Array.prototype.shift == "undefined") { Array.prototype.shift = Array_shift; }
/* COOKIES */
var Cookies = {
	init: function () {
		var allCookies = document.cookie.split('; ');
		for (var i=0;i<allCookies.length;i++) {
			var cookiePair = allCookies[i].split('=');
			this[cookiePair[0]] = cookiePair[1];
		}
	},
	create: function (name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
		this[name] = value;
	},
	erase: function (name) {
		this.create(name,'',-1);
		this[name] = undefined;
	},
	eraseAll: function () {
		for (var i in this) {
			if (typeof this[i] == 'function') continue;
			this.erase(i);
		}
	}
};
Cookies.init();
/* DOMCONTENTREADY */
function ContentReady() {
	// quit if this function has already been called
	if (arguments.callee.done) { return; }
	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;
	// kill the timer
	if (_timer) { clearInterval(_timer); }
	// do stuff
	initializeAll();
};
/* for Mozilla/Opera9 */
if (document.addEventListener) { document.addEventListener("DOMContentLoaded",ContentReady,false); }
/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			ContentReady(); // call the onload handler
		}
	};
/*@end @*/
/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			ContentReady(); // call the onload handler
		}
	}, 10);
}
/* for other browsers */
window.onload = ContentReady;
