

// CONSTANTS AND VARIABLES
var CONST_TAAL_NL = 1;
var CONST_TAAL_EN = 2;
var glb_language = CONST_TAAL_NL; // To be overruled in a specific page

/* 
	This code will always be executed
	(Thanks to http://www.bobbyvandersluis.com/articles/unobtrusiveshowhide.php )
*/
if (document.getElementById && document.getElementsByTagName) {
	// ON LOAD EVENT HANDLERS
	window.onload = attachJSFunctions;
}

/*
  Function required for Sitestat counter
*/
function sitestat(ns_l) {
  ns_l+="&ns__t="+(new Date()).getTime();ns_pixelUrl=ns_l;
  ns_0=document.referrer;
  ns_0=(ns_0.lastIndexOf("/")==ns_0.length-1)?ns_0.substring(ns_0.lastIndexOf("/"),0):ns_0;
  if(ns_0.length>0)ns_l+="&ns_referrer="+escape(ns_0);
  if(document.images){ns_1=new Image();ns_1.src=ns_l;}else
  document.write("<img src="+ns_l+" width=1 height=1>");
}

		// FUNCTIONS
		function attachJSFunctions() {
		if (document.getElementById("homepage")) {
			startNewstimer();
		}
				
		// attachFormHandlers
		if (document.getElementById("bestelformulier")) {
			document.getElementById("bestelformulier").onsubmit = function() { return checkFieldsBestellen(this); }
		}
		if (document.getElementById("formnieuwsbrief")) {
			document.getElementById("formnieuwsbrief").onsubmit = function() { return checkFieldsNieuwsbrief(this); }
		}
		if (document.getElementById("formnieuwsbriefafm")) {
			document.getElementById("formnieuwsbriefafm").onsubmit = function() { return checkFieldsNieuwsbriefAfm(this); }
		}
		if (document.getElementById("formcontact")) {
			document.getElementById("formcontact").onsubmit = function() { return checkFieldsContact(this); }
		}
                                
                                if(document.getElementsByTagName){ 
                                     matchColumns();
                                } 
                                }
		

		
/* Homepage javascript */
var glb_news_timer = 1;
var glb_news_time_secs = 10000; // milli seconds
var glb_user_set_timeout;

/*
	Show requested news item (with ID [nr])
	Hide all others
*/
function showNewsItem(nr) {
	if (document.getElementById) {
		try {
			var countermax = glb_max_newsitems;
			for (var i=1; i < (countermax+1); i++) {
				if (i==nr) {
					document.getElementById("news" + i).className = "uitgelicht";
					document.getElementById("newslink" + i).className = "selected";
				} else {
					document.getElementById("news" + i).className = "hidden";
					document.getElementById("newslink" + i).className = "";
				}
			}
		} catch(er) {}
	}
	resetButtons();
}

/*
	User has selected a certain news item
*/
function showNews(nr) {
	glb_news_timer = nr; // reset glb_news_timer to the ID of requested news item
	startNewstimer();
}

/*
	Start the news timer
	Open new news item every xx seconds
*/
function startNewstimer() {
	clearTimeout(glb_user_set_timeout); // clear the timeout in case the timer still counts (happens after a user click)
	showNewsItem(glb_news_timer);
	if (glb_news_timer >= glb_max_newsitems) { // we've reached the last item, start again from top
		glb_news_timer = 1;
	} else {
		glb_news_timer++;
	}
	glb_user_set_timeout = setTimeout("startNewstimer()", glb_news_time_secs); // call ourselves again after xx seconds
}

/*
	Go to the previous item in the newsticker
*/
function gotoPreviousNewsitem() {
	clearTimeout(glb_user_set_timeout); // clear the timeout in case the timer still counts (happens after a user click)
	var itemno = glb_news_timer-2;
	if (itemno < 1) itemno += glb_max_newsitems;
	showNewsItem(itemno);
	setAllButtonsOff();
	glb_news_timer = glb_news_timer-1;
	if (glb_news_timer < 1) glb_news_timer += glb_max_newsitems;	
	//showNews(itemno);
}

/*
	Go to the next item in the newsticker
*/
function gotoNextNewsitem() {

	clearTimeout(glb_user_set_timeout); // clear the timeout in case the timer still counts (happens after a user click)
	showNewsItem(glb_news_timer);
	setAllButtonsOff();
	if (glb_news_timer >= glb_max_newsitems) { // we've reached the last item, start again from top
		glb_news_timer = 1;
	} else {
		glb_news_timer++;
	}
	//showNews(glb_news_timer);
}

/*
	Stop the timer! User gets crazy...
*/
function stopNewstimer() {
	clearTimeout(glb_user_set_timeout);
}

/*
	Toggle button between start and stop
*/
function toggleNewstimer(bln_stop) {
	if (bln_stop) {
		// Put pausebutton on and play button off
                                if (glb_language == CONST_TAAL_EN) {
		     document.getElementById("toggleStartStop").innerHTML = '<img src="/images/newsticker_play_tcm59-22329.gif" alt="Play" title="Play" onclick="toggleNewstimer(false)"/>';
		      document.getElementById("togglePause").innerHTML = '<img src="/images/newsticker_pause_on_tcm59-22328.gif" alt="Pause" title="Pause" />';
                                } else {
		     document.getElementById("toggleStartStop").innerHTML = '<img src="/images/newsticker_play_tcm59-22329.gif" alt="Speel" title="Speel" onclick="toggleNewstimer(false)"/>';
		      document.getElementById("togglePause").innerHTML = '<img src="/images/newsticker_pause_on_tcm59-22328.gif" alt="Pauze" title="Pauze" />';
                                }
		stopNewstimer();
	} else {
		// Change buttons
                                if (glb_language == CONST_TAAL_EN) {
		    document.getElementById("toggleStartStop").innerHTML = '<img src="/images/play_on_tcm59-22330.gif" alt="Play" title="Play" />';
		    document.getElementById("togglePause").innerHTML = '<img src="/images/newsticker_pause_tcm59-22327.gif" alt="Pause" title="Pause" onclick="toggleNewstimer(true)"/>';	
                                } else {
		    document.getElementById("toggleStartStop").innerHTML = '<img src="/images/play_on_tcm59-22330.gif" alt="Speel" title="Speel" />';
		    document.getElementById("togglePause").innerHTML = '<img src="/images/newsticker_pause_tcm59-22327.gif" alt="Pauze" title="Pauze" onclick="toggleNewstimer(true)"/>';	
                                }
		startNewstimer();
	}
}

/*
	Toggle pause button
*/
function togglePause(bln_pause) {
	if (bln_pause) {
		// Change pause & play button
                                if (glb_language == CONST_TAAL_EN) {
		     document.getElementById("togglePause").innerHTML = '<img src="/images/newsticker_pause_on_tcm59-22328.gif" alt="Pause" title="Pause" />';             
		     document.getElementById("toggleStartStop").innerHTML = '<img src="/images/newsticker_play_tcm59-22329.gif" alt="Play" title="Play" onclick="toggleNewstimer(false)"/>';
                                } else {
                                     document.getElementById("togglePause").innerHTML = '<img src="/images/newsticker_pause_on_tcm59-22328.gif" alt="Pauze" title="Pauze" />';             
		     document.getElementById("toggleStartStop").innerHTML = '<img src="/images/newsticker_play_tcm59-22329.gif" alt="Speel" title="Speel" onclick="toggleNewstimer(false)"/>';

                                }
		stopNewstimer();
	} else {
		startNewstimer();
	}
}

/*
	Show the stop button (it might have been changed to a start button)
*/
function resetButtons() {
	// Playbutton
                if (glb_language == CONST_TAAL_EN) {
	               document.getElementById("toggleStartStop").innerHTML = '<img src="/images/play_on_tcm59-22330.gif" alt="Play" title="Play" />';
                } else {
	               document.getElementById("toggleStartStop").innerHTML = '<img src="/images/play_on_tcm59-22330.gif" alt="Speel" title="Speel" />';
                }
}

function setAllButtonsOff() {
                 if (glb_language == CONST_TAAL_EN) {
                                // Play button
		document.getElementById("toggleStartStop").innerHTML = '<img src="/images/newsticker_play_tcm59-22329.gif" alt="Play" title="Play" onclick="toggleNewstimer(false)"/>';	
                               // Pause button
                               document.getElementById("togglePause").innerHTML = '<img src="/images/newsticker_pause_tcm59-22327.gif" alt="Pause" title="Pause" onclick="toggleNewstimer(true)"/>';

	} else {
                                // Play button
	                document.getElementById("toggleStartStop").innerHTML = '<img src="/images/newsticker_play_tcm59-22329.gif" alt="Speel" title="Speel" onclick="toggleNewstimer(false)"/>';
                                 // Pause button
                                document.getElementById("togglePause").innerHTML = '<img src="/images/newsticker_pause_tcm59-22327.gif" alt="Pauze" title="Pauze" onclick="toggleNewstimer(true)"/>';
	}
}

function checkFieldsBestellen(frm) {
	var bln;
	bln = checkField(frm.publicationTitle, "U heeft geen publicatietitel ingevuld.", "Please fill in a publication title.");
	if (bln) bln = checkField(frm.publicationNumber, "U heeft geen publicatienummer ingevuld.", "Please fill in a publication number.");
	if (bln) bln = checkField(frm.publicationAuthors, "U heeft geen publicatieauteur ingevuld.", "Please fill in a publication author.");
	if (bln) bln = checkField(frm.name, "U heeft uw naam niet ingevuld.", "Please fill in your name.");
	if (bln) bln = checkField(frm.address, "U heeft uw adres niet ingevuld.", "Please fill in your address.");
	if (bln) bln = checkField(frm.zipcode, "U heeft uw postcode niet ingevuld.", "Please fill in your postal code.");
	if (bln) bln = checkField(frm.city, "U heeft uw woonplaats niet ingevuld.", "Please fill in your city.");
	if (bln) bln = checkField(frm.emailAddress, "U heeft uw e-mailadres niet ingevuld.", "Please fill in your e-mail address.");
	if (bln) bln = checkEmail(frm.emailAddress, "Uw e-mailadres is niet geldig.", "Please fill in a valid e-mail address.");
	return bln;
	if (bln) bln = checkField(frm.organization, "U heeft uw organisatie niet ingevuld.", "Please fill in your organization.");
	return bln;
}

/* Validation of empty field */
function checkField(objField,txtErrorNL, txtErrorEN) {
	var waarde = objField.value;
	if (waarde == "") { // Field is empty, show Dutch or English error message
		if (glb_language == CONST_TAAL_EN) {
			alert(txtErrorEN);
		} else {
			alert(txtErrorNL);
		}
		objField.focus();
		return false;
	}
	return true;
}

/* Validation of email address */
function checkEmail(objEmail, txtErrorNL, txtErrorEN) {
	if (objEmail.value.indexOf('@', 0) == -1 || objEmail.value.indexOf('.', 0) == -1 ) {
		if (glb_language == CONST_TAAL_EN) {
			alert(txtErrorEN);
		} else {
			alert(txtErrorNL);
		}
		objEmail.focus();
		return false;
	} else {
		return true;
	}
}		

/* Check fields of form with ID "formnieuwsbrief" */
function checkFieldsNieuwsbrief(frm) {
	var bln;
	bln = checkField(frm.name, "U heeft uw naam niet ingevuld.", "Please fill in your name.");
	if (bln) bln = checkField(frm.emailAddress, "U heeft uw e-mailadres niet ingevuld.", "Please fill in your e-mail address.");
	if (bln) bln = checkEmail(frm.emailAddress, "Uw e-mailadres is niet geldig.", "Please fill in a valid e-mail address.");
	return bln;
}	

/* Check fields of form with ID "formnieuwsbriefafm" */
function checkFieldsNieuwsbriefAfm(frm) {
	var bln;
	if (bln) bln = checkField(frm.emailAddress, "U heeft uw e-mailadres niet ingevuld.", "Please fill in your e-mail address.");
	if (bln) bln = checkEmail(frm.emailAddress, "Uw e-mailadres is niet geldig.", "Please fill in a valid e-mail address.");
	return bln;
}	

/* Check fields of form with ID "formcontact" */
function checkFieldsContact(frm) {
	var bln;
	bln = checkField(frm.name, "U heeft uw naam niet ingevuld.", "Please fill in your name.");
	if (bln) bln = checkField(frm.emailAddress, "U heeft uw e-mailadres niet ingevuld.", "Please fill in your e-mail address.");
	if (bln) bln = checkEmail(frm.emailAddress, "Uw e-mailadres is niet geldig.", "Please fill in a valid e-mail address.");
	return bln;
}

/* functie om 2 div's gelijk te trekken qua hoogte, class="column" toevoegen aan beide div's is genoeg */
matchColumns=function(){ 
     var divs,contDivs,maxHeight,divHeight,d; 
     divs=document.getElementsByTagName('div'); 
     contDivs=[]; 
     maxHeight=0; 
     for(var i=0;i<divs.length;i++){ 
          if(/\bcolumn\b/.test(divs[i].className)){ 
                d=divs[i]; 
                contDivs[contDivs.length]=d; 
                if(d.offsetHeight){divHeight=d.offsetHeight;} 
                else if(d.style.pixelHeight){divHeight=d.style.pixelHeight;} 
                maxHeight=Math.max(maxHeight,divHeight); 
          } 
     } 
     for(var i=0;i<contDivs.length;i++){contDivs[i].style.height=maxHeight + "px";} 
}