function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function show_popup(url,width,height) {
	theWidth = width;
	theHeight = height;
	theTop = (screen.height-theHeight*1.5)/2;
	theLeft = (screen.width-theWidth)/2;
	winopts = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,copyhistory=0,width="+theWidth+",height="+theHeight+",top="+theTop+",left="+theLeft;
	smallwindow=window.open(url,"Detail",winopts);
	smallwindow.focus();
}

function menu() {
	//if (window.attachEvent) { 
		// IE does not do :hover on <li> ==> add a class onmouseover
		sfHover("tabNavigation");		
	//} 
}

function sfHover(id) {
	var BUA = navigator.userAgent;
	var BIE = BUA.indexOf("MSIE");
	var BIsIE = BIE>=0;

	var ul = document.getElementById(id);
	if(ul) {
		var lis = ul.getElementsByTagName('li');
		for(i=0;i<lis.length;i++) {
			if(lis[i].parentNode == ul) {
				// enkel die LIs die subitems hebben
				lis[i].onmouseover=function() {this.className+=" sfhover";}
				lis[i].onmouseout=function() {this.className=this.className.replace(new RegExp(" sfhover\\b"), "");}
							
				//all is normal till here. If we're using IE then this tells any first level item to have a width of 1px
				//lis[i].style.width = '1px';
			}
		}
	}
}

function search() {
	var searchlnk = document.getElementById("start");
	if(searchlnk) {
		searchlnk.onclick = function() {
			document.searchform.submit();
			return false;
		}
	}
}

function validateEmail(email) {
	
	if (email != null && email != "") {
		a = email.lastIndexOf("@");
		b = email.lastIndexOf(".");
		c = email.indexOf(":");
		d = email.indexOf("/");
		e = email.substring(0,a);
		f = e.indexOf("@");
		g = email.substring(a+1,email.length);
		h = g.indexOf("[");
		i = g.indexOf("]");
		j = g.indexOf("<");
		k = g.indexOf(">");
		l = email.substring(a+1,b);
		m = email.substring(b+1,email.length);
		n = email.substring(0,a);
		p = email.indexOf("'");
		q = email.indexOf("\"");
		o = 0;
		if (a > b) {o++};
		if (c != -1) {o++};
		if (d != -1) {o++};
		if (f != -1) {o++};
		if (h != -1) {o++};
		if (i != -1) {o++};
		if (j != -1) {o++};
		if (k != -1) {o++};
		if (l.length < 2) {o++};
		if (m.length < 2) {o++};
		if (n.length < 1) {o++};
		if (p != -1) {o++};
		if (q != -1) {o++};
		if (o != 0) {
			return false;
		}
	}
	return true;
}


function validateContact(theForm) {
	var error = 0;
	var msg = "";
	
	if(theForm.naam.value.length==0) {
		error++;
		msg += "\n- naam";
	}
	if(theForm.voornaam.value.length==0) {
		error++;
		msg += "\n- voornaam";
	}
	if(theForm.contact_email.value.length==0 || !validateEmail(theForm.contact_email.value)) {
		error++;
		msg += "\n- email";
	}
	if(error) {
		alert("Volgende velden zijn verplicht in te vullen:" + msg);
		return false;
	}
	theForm.x.value = "abc123";
	return true;
}



function validateTellafriend(theForm) {
	var error = 0;
	var msg = "";
	
	if(theForm.to.value.length==0 || theForm.to.value == "naam ontvanger") {
		error++;
		msg += "\n- naam ontvanger";
	}
	if(theForm.from.value.length==0 || theForm.from.value == "naam zender") {
		error++;
		msg += "\n- naam zender";
	}
	if(theForm.email.value.length==0 || !validateEmail(theForm.email.value)) {
		error++;
		msg += "\n- email ontvanger";
	}
	if(theForm.emailfrom.value.length==0 || !validateEmail(theForm.emailfrom.value)) {
		error++;
		msg += "\n- email zender";
	}
	if(error) {
		alert("Volgende velden zijn verplicht in te vullen:" + msg);
		return false;
	}
	
	return true;
}

function forms() {
	var nieuwsbriefform = document.form_nieuwsbrief;
	if (nieuwsbriefform) {
		nieuwsbriefform.onsubmit = function() {
			if(this.email.value != "" && validateEmail(this.email.value)) {
				document.getElementById("").className
			}
			alert("Gelieve een geldig emailadres in te geven.");
			return false;
		}
	}
	var newsletter_form = document.newsletterform;
	if (newsletter_form) {
		newsletter_form.onsubmit = function() {
			if(this.email.value != "" && validateEmail(this.email.value)) {
				return true;
			}
			alert("Gelieve een geldig emailadres in te geven.");
			return false;
		}
	}
	var tellafriend_form = document.tellafriendformulier;
	if (tellafriend_form) {
		tellafriend_form.onsubmit = function() {
			return validateTellafriend(this);
		}
	}
	var contact_form = document.contactformulier;
	if (contact_form) {
		contact_form.onsubmit = function() {
			return validateContact(this);
		}
	}
}

addLoadEvent(forms);
addLoadEvent(search);
addLoadEvent(menu);