var ie_lt7 = false, maakRijkeTabel = false, rondehoeken = false;
window.onload = function() {
	if (document.getElementById("topmenu") && (ie_lt7 || window.opera)) {
		// zorgt ervoor dat menu uitklapt in ie<7 en via toetsenbord in opera
		var lis = document.getElementById("topmenu").getElementsByTagName("li");
		for (var i = 0; i in lis; i++) {
			if (lis[i].className.indexOf('submenucontainer') > -1) {
				lis[i].onmouseover = function () {this.className += ' erover';};
				var w = lis[i].offsetWidth;
				var ul = lis[i].getElementsByTagName("ul")[0];
				ul.style.width = w*1 -2 + 'px';
				var sublis = ul.getElementsByTagName("li");
				for (var j = 0; j in sublis; j++) {
					sublis[j].style.width = w*1 -1 + 'px';
					sublis[j].getElementsByTagName('a')[0].style.width = w*1 - 20 + 'px'; // padding
					sublis[j].onmouseover = function () {this.className = 'subli_hover'};
					sublis[j].onmouseout = function () {this.className = ''};
				}
				lis[i].onmouseout = function () {this.className = this.className.replace(' erover', '');};
			}
		}
	}
	if (maakRijkeTabel) {
		initRijkeTabel();
	}
	if (rondehoeken && typeof Nifty == 'function') {
		Nifty("div.zijkantblokkop", "top");
		Nifty("div.zijkantblokbinnenvak", "bottom");
	}
}

Array.prototype.inArray = function (s) {
	// aanroep: array.inArray(string)
	for (var i = 0, tL = this.length; i < tL; i++)
		if (this[i] == s) return true; 
	return false;
};
String.prototype.nl2br = function () {
	// aanroep: string.nl2br()
	return this.replace(/\r\n|\r|\n/g, '<br>');
};

function createObjectCallback(obj, fn) {
	// bron: http://www.outofhanwell.com/blog/index.php?title=object_oriented_callbacks&more=1&c=1&tb=1&pb=1
	return function() { fn.apply(obj, arguments); };
}

var HttpVerzoek = function () {
	if (typeof this.req != 'object') {
		if (window.XMLHttpRequest) {
			try {this.req = new XMLHttpRequest();} catch(e) {this.req = false;}
		} else if (window.ActiveXObject) {
			try {this.req = new ActiveXObject("Msxml2.XMLHTTP");} catch(e) {
				try {this.req = new ActiveXObject("Microsoft.XMLHTTP");} catch(e) {this.req = false;}
			}
		}
		if (!this.req) {alert("unsupported browser"); return false;}
	}
	this.sendstring = '';
	this.methode = 'POST';
	this.verzoekscript = 'http://' + location.hostname + location.pathname;
	this.async = true;
	this.verwerker = this.alertResponseText;
};
HttpVerzoek.prototype.doeverzoek = function() {
	if (this.req) {
		this.req.open(this.methode, this.verzoekscript, this.async);
		this.req.onreadystatechange = createObjectCallback(this.req, this.verwerker);
		this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		this.req.send(this.sendstring);
	}
};
HttpVerzoek.prototype.alertResponseText = function() {
    if (this.readyState == 4) {
		alert(this.responseText);
	}
};
var berichtbox;
var berichtbox_to; // timeout-ding
var berichtbox_to_duur = 2; // seconden
function toonbericht() {
	if (this.readyState == 4) {
		berichtbox = document.getElementById('berichtbox');
		if (typeof berichtbox_to != 'undefined')
			clearTimeout(berichtbox_to);
		berichtbox.innerHTML = this.responseText.nl2br();
		var y = document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop;
		berichtbox.style.top = y + 'px';
		berichtbox.style.display = 'block';
		if (berichtbox_to_duur > 0) {
			berichtbox_to = setTimeout('berichtbox.style.display="none"', berichtbox_to_duur*1000);
		}
	}
}

/*
validatie-objectje. nog niet overal geimplementeerd...
*/
function MyValid() {
	this.errMsg = [];
}
MyValid.prototype.voegToe = function(tekst) {
	this.errMsg.push(tekst);
	if (arguments.length > 1) {
		arguments[1].className += ' nietgoed';
	}
};
MyValid.prototype.rondAf = function() {
	if (this.errMsg.length > 0) {
		alert(this.errMsg.join('\n'));
		this.errMsg = new Array();
		return false;
	} else {
		return true;
	}
};


var errMsg = new Array;
function valideerDeelnemer(f) {
	// return true;
	if (f.achternaam.value.length < 2) {
		errMsg.push('Vul s.v.p. uw achternaam in.');
	}
	if (f.voorletters.value.length < 1) {
		errMsg.push('Vul s.v.p. uw voorletters in.');
	}
	if (!haalGekozenRadioWaarde(f.titulatuur)) {
		errMsg.push('Kies s.v.p. tussen \'dhr.\' en \'mw.\'');
	}
	if (f.email.value.length < 2) {
		errMsg.push('Vul s.v.p. uw e-mailadres in.');
	} 
	if (f.email.value.length > 1 && !isGeldigEmailadres(f.email.value)) {
		errMsg.push('Controleer s.v.p. uw e-mailadres; het lijkt niet valide.');
	}
	if (typeof f.ismailinglijst == 'undefined') {
		if (f.adres1.value.length < 2) {
			errMsg.push('Vul s.v.p. uw adres in.');
		}
		/*
		if (f.postcode.value.length < 2) {
			errMsg.push('Vul s.v.p. uw postcode in.');
		}
		if (f.plaats.value.length < 2) {
			errMsg.push('Vul s.v.p. de plaats in.');
		}
		*/
		if (f.landID.value == 0) {
			errMsg.push('Vul s.v.p. uw land in.');
		}
		if (f.functie.value.length < 1) {
			errMsg.push('Vul s.v.p. uw functie of beroep in.');
		}
		if (f.taalvan_1.value == 0 || f.taalnaar_1.value == 0) {
			errMsg.push('Geef s.v.p. een talencombinatie aan.');
		}
		if (!haalGekozenRadioWaarde(f.bezwaar)) {
			errMsg.push('Maak s.v.p. een keuze bij \'gegevens op deelnemerslijst\'.');
		}
		if (!haalGekozenRadioWaarde(f.wilemail)) {
			errMsg.push('Maak s.v.p. een keuze bij \'mailingen\'.');
		}
	}
	
	return rondValidatieAf();
}

function rondValidatieAf() {
	if (errMsg.length > 0) {
		alert(errMsg.join('\n'));
		errMsg = new Array();
		return false;
	} else {
		return true;
	}
}

function haalGekozenRadioWaarde(veld) {
	var waarde = false;
	for (var i = 0; i < veld.length; i++) {
		if (veld[i].checked)
			waarde = veld[i].value;
	}
	return waarde;
}


function isGeldigEmailadres(email) {
	return email.match(/^[a-z0-9]([a-z0-9.]|([\w\-]+[a-z0-9]))*(\.([a-z0-9]|[a-z0-9][\w\-]+[a-z0-9]))*@[a-z0-9][\w\-]*[a-z0-9]\.([a-z0-9][\w\-]*[a-z0-9]\.)*[a-z]{2,6}$/i);
}


var wachter=1000, menuYpos=0, schermhoogte, drijfDiv;
function loper() { 
	this.currentY = parseInt(drijfDiv.style.top); 
	this.scrollTop = document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop;
	mainTrigger(); 
}
function mainTrigger() { 
	var newTargetY = Math.max(this.scrollTop, menuYpos); 
	if ( this.currentY != newTargetY ) { 
		if ( newTargetY != this.targetY ) { 
			this.targetY = newTargetY;
			floatStart(); 
		}
		animator(); 
	}
}
function floatStart() {
	var now = new Date() ; 
	this.A = this.targetY - this.currentY ; 
	this.B = Math.PI / ( 2 * this.wachter ); 
	this.C = now.getTime(); 
	if (Math.abs(this.A) > this.schermhoogte) { 
		this.D = this.A > 0 ? this.targetY - this.schermhoogte : this.targetY + this.schermhoogte;
		this.A = this.A > 0 ? this.schermhoogte : -this.schermhoogte;
	} else {  
		this.D = this.currentY; 
	}
}
function animator() {
	var now = new Date(); 
	var newY = this.A * Math.sin( this.B * ( now.getTime() - this.C ) ) + this.D;
	newY = Math.round(newY);
	if (( this.A > 0 && newY > this.currentY ) || ( this.A < 0 && newY < this.currentY )) { 
		drijfDiv.style.top = newY + "px";
	}
}
function initDrijfmenu(divID) {
	drijfDiv = document.getElementById(divID);
	schermhoogte = window.innerHeight ? 
		window.innerHeight : 
		document.documentElement ? 
			document.documentElement.clientHeight : 
			document.body.clientHeight;
	drijfDiv.style.position = 'relative';
	drijfDiv.style.top = menuYpos + 'px';
	window.setInterval("loper()", 20);
}

var objDisplayStates = new Object();
function toggleDisplay(elID, initState) {
	if (typeof objDisplayStates[elID] == 'undefined') {
		objDisplayStates[elID] = initState == 1 ? 0 : 1;
	} else {
		objDisplayStates[elID] ^= 1;
	}
	document.getElementById(elID).style.display = objDisplayStates[elID] == 1 ? 'block' : 'none';
}
 
/**
 * wrapper om display van twee of meer boxen aan of uit te zetten
 * aanroep: toggleDisplayMultibox(elmID, initstate[, elmID, initstate[..]])
 *
 */
function toggleDisplayMultibox() {
	var nboxen = arguments.length;
	for (var i = 0; i < nboxen; i = i + 2) {
		toggleDisplay(arguments[i], arguments[i+1]);
	}
}
