/*
	Pearson-nl.com Functies javascript
	Auteur: Caspar Dunant
	Auteur 2: Paul van Roosendaal
	Versie: 1.0
*/
function checkForm(form) {
	var ppoDiv = document.getElementById('PPO');
	var akkoordPPO = document.getElementById('voorwaardenAkkoordPPO');
	if(ppoDiv && ppoDiv.style.display == 'block' && !akkoordPPO.checked) {
		alert('U moet akkoord gaan met de licentievoorwaarden P2O.');
		return false;
	}
	
	var akkoord = document.getElementById('voorwaardenAkkoord');
	if(!akkoord.checked) {
		alert('U moet akkoord gaan met de licentievoorwaarden.');
		return false;
	}
	
	return true;
}


function showPPO(){ 
	var ppoCheckbox = document.getElementById('aanmeldenPPO');
	var ppoDiv = document.getElementById('PPO');
	if(ppoCheckbox.checked) {
		ppoDiv.style.display = 'block';
	} else {
		ppoDiv.style.display = 'none';			
	}
}

function showDiv(divID){ 
	var div = document.getElementById(divID);
	if(div.style.display == '' || div.style.display == 'none') {
		div.style.display = 'block';
	} else {
		div.style.display = 'none';			
	}
}

function checkSollicitatieForm() {
	if (document.getElementById('naam').value == '') {
		document.getElementById('errorDiv').innerHTML = 'U dient uw naam in te voeren';
		document.getElementById('errorDiv').style.display = 'block';
		return false;
	} else if (document.getElementById('voorletters').value == '') {
		document.getElementById('errorDiv').innerHTML = 'U dient uw voorletters in te voeren';
		document.getElementById('errorDiv').style.display = 'block';
		return false;
	} else if (document.getElementById('voornaam').value == '') {
		document.getElementById('errorDiv').innerHTML = 'U dient uw voornaam in te voeren';
		document.getElementById('errorDiv').style.display = 'block';
		return false;
	} else if (document.getElementById('adres').value == '') {
		document.getElementById('errorDiv').innerHTML = 'U dient uw adres in te voeren';
		document.getElementById('errorDiv').style.display = 'block';
		return false;
	} else if (document.getElementById('postcode').value == '') {
		document.getElementById('errorDiv').innerHTML = 'U dient uw postcode in te voeren';
		document.getElementById('errorDiv').style.display = 'block';
		return false;
	} else if (document.getElementById('woonplaats').value == '') {
		document.getElementById('errorDiv').innerHTML = 'U dient uw woonplaats in te voeren';
		document.getElementById('errorDiv').style.display = 'block';
		return false;
	} else if (document.getElementById('telefoon').value == '') {
		document.getElementById('errorDiv').innerHTML = 'U dient uw telefoon in te voeren';
		document.getElementById('errorDiv').style.display = 'block';
		return false;
	} else if (document.getElementById('email').value == '' || !isValidEmail(document.getElementById('email').value)) {
		document.getElementById('errorDiv').innerHTML = 'U dient een geldig emailadres in te voeren';
		document.getElementById('errorDiv').style.display = 'block';
		return false;
	} else if (document.getElementById('geboortedatum').value == '' || !isValidDatum(document.getElementById('geboortedatum').value)) {
		document.getElementById('errorDiv').innerHTML = 'U dient een geldige geboortedatum in te voeren';
		document.getElementById('errorDiv').style.display = 'block';
		return false;
	} else if (document.getElementById('cv').value == '') {
		document.getElementById('errorDiv').innerHTML = 'U dient uw cv te selecteren';
		document.getElementById('errorDiv').style.display = 'block';
		return false;
	}
	return true;
}

function isValidDatum(data) {
	validRegExp = /^[0-9]{2}\-[0-9]{2}\-[0-9]{4}$/i;
	strEmail = data;

	// search email text for regular exp matches
	if (strEmail.search(validRegExp) == -1) {
		return false;
	}
	return true;
}
function isValidEmail(data){
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	strEmail = data;

	// search email text for regular exp matches
	if (strEmail.search(validRegExp) == -1) {
		return false;
	}
	return true;
}

getElementsByClassName = function (className, incAllClasses, tag, elm) {
	incAllClasses = incAllClasses || false;
	tag = tag || "*";
	elm = elm || document;
	var classes = className.split(" "),
		classesToCheck = [],
		elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
		current,
		returnElements = [],
		match;
	for(var k=0, kl=classes.length; k<kl; k+=1){
		classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
	}
	for(var l=0, ll=elements.length; l<ll; l+=1){
		current = elements[l];
		match = false;
		for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
			match = classesToCheck[m].test(current.className);
			if (match != incAllClasses) {
				break;
			}
		}
		if (match != incAllClasses) {
			returnElements.push(current);
		}
	}
	return returnElements;
}

var isGecko = (navigator.userAgent.search(/gecko/i) != -1);
var isIE6   = (navigator.userAgent.search(/MSIE 6/i) != -1);

function setHeight(oDiv, sHeight)
{
	if (isIE6)
	{
		oDiv.style.height = sHeight;
	}
	else
	{
		oDiv.style.minHeight = sHeight;
	}
}
function resizeDivs()
{
	var iDivOffsetTop = 12;
	var iMainNavOffset = 0;
	var contentSectionClasses = 'contentSection contentSectionVacatures contentSectionDownload contentSectionFaq contentSectionSitemap contentSectionSpecs contentSectionItems';
	var oLastTest = new RegExp("(^|\\s)lastSection(\\s|$)");

	var oMainNav = document.getElementById('mainNav');
	var iMainNav = oMainNav.offsetHeight;
	var oMainContent = document.getElementById('mainContent');
	var iMainContent = oMainContent.offsetHeight;

	var oMainContentFull = document.getElementById('mainContentFull');

	var iMainContentOffset = 0;
	if (!oMainContentFull)
	{
		var oMainContentLeft = document.getElementById('mainContentLeft');
		var oMainContentRight = document.getElementById('mainContentRight');

		if (oMainContentLeft && oMainContentRight)
		{
			var oSHLeft = getElementsByClassName('sectionHeader', false, 'div', oMainContentLeft);
			var iSHLeft = oSHLeft.length;
			var oSHRight = getElementsByClassName('sectionHeader', false, 'div', oMainContentRight);
			var iSHRight = oSHRight.length;
			if (oMainContentLeft.offsetHeight < oMainContentRight.offsetHeight)
			{
				iMainContentOffset = 16;
				if (iSHRight > 0)
				{
					iMainNavOffset = 8;
				}
			}
			else
			{
				//if ((iSHLeft > 0) || (iSHRight > 0))
				if (iSHLeft > 0)
				{
					if (iSHRight > 0)
						iMainNavOffset = -20;
					else
						iMainNavOffset = -8;
				}
			}
			if (iSHLeft >= 1)
			{
				iMainNavOffset += 20;
				iMainContentOffset += 20;
			}
		}
	}
	else
	{
		var oProductDivs = getElementsByClassName('contentSectionProductSearch', false, 'div', oMainContentFull);
		if (oProductDivs.length > 0)
		{
			iMainNavOffset += 12;
		}
		else
		{
			iMainNavOffset = 12;
		}
	}

	if (iMainNav < iMainContent)
	{
		var oHomeNews = document.getElementById('homeNews');
		//setHeight(oMainNav, (iMainContent - (oHomeNews ? 8 : (oMainContentFull ? iDivOffsetTop : -4)) + iMainNavOffset) +'px');
		iMainNav = iMainContent - (oHomeNews ? 8 : (oMainContentFull ? -4 : -4 + iMainNavOffset));
		setHeight(oMainNav, (iMainNav + iMainNavOffset) +'px');
		if (!isIE6 && oMainContentFull)
		{
			iDivOffsetTop = 0;
		}
	}
	else
	{
		if (oMainContentLeft && oMainContentRight)
		{
			if (iSHLeft >= 1)
			{
				setHeight(oMainNav, (iMainNav + 8) +'px');
				//iDivOffsetTop -= 8;
			}
		}
	}
	var iOffset = 0;
	var oMainIntAreas = document.getElementById('mainIntAreas');
	if (oMainIntAreas)
		iOffset += oMainIntAreas.offsetHeight;
	var oMainPageMenu = document.getElementById('mainPageMenu');
	if (oMainPageMenu)
		iOffset += oMainPageMenu.offsetHeight;
	var oCrumblePath  = document.getElementById('crumblePath');
	if (oCrumblePath)
		iOffset += oCrumblePath.offsetHeight;

	setHeight(oMainContent, (iMainNav + iMainContentOffset) +'px');
	//oMainContent.style.height = (iMainNav) +'px';
	if (oMainContentFull)
	{
		var oSH = getElementsByClassName('sectionHeader', false, 'div', oMainContentFull);
		var iSH = oSH.length;
		var oDivs = getElementsByClassName('contentSection contentSectionFaq contentSectionItems contentSectionSitemap', false, 'div', oMainContentFull);
		var oFaqDivs = getElementsByClassName('contentFaq', false, 'table', oMainContentFull);
		if (oDivs && (oDivs.length > 0))
		{
			if (oProductDivs.length > 1)
				iDivOffsetTop = 8;
			oDiv = oDivs.pop();
			setHeight(oDiv, (iMainNav - oDiv.offsetTop - iOffset - iDivOffsetTop) +'px');
			//oDiv.style.height = (iMainNav - oDiv.offsetTop - iOffset - iDivOffsetTop) +'px';
		}
	}
	else
	{
		var oTODivs = getElementsByClassName('contentSectionTOItem', false, 'div', oMainContentLeft);
		var iOffsetBanner = 0;
		var oBottomBanner = document.getElementById('bottomBanner');
		if (oBottomBanner)
			iOffsetBanner = oBottomBanner.offsetHeight + 8;
		var oTopBanner = document.getElementById('topBanner');

		var oMainContentRight = document.getElementById('mainContentRight');
		if (oMainContentRight)
		{
			var oDiv = getElementsByClassName('lastSection', false, 'div', oMainContentRight);
			var oSHRight = getElementsByClassName('sectionHeader', false, 'div', oMainContentRight);
			var iSHRight = oSHRight.length;
			if (oDiv && (oDiv.length > 0))
			{
				oDiv = oDiv.pop();

				if (oDiv.offsetTop > 0)
				{
					//if (oBottomBanner)
					//	iSHRight++;
					if (!oBottomBanner && !oTopBanner && (iSHRight < 1)) // != 1
						iSHRight++;
				}
				var iNewHeight = (iMainNav - oDiv.offsetTop - iOffset - iOffsetBanner - (iSHRight * 20) - iDivOffsetTop) + (isGecko ? 0 : 8);
				if (iSHLeft > 0)
					iNewHeight += 8;
				setHeight(oDiv, iNewHeight +'px');
				//oDiv.style.height = iNewHeight +'px';

				if (oBottomBanner)
				{
					if (oDiv.offsetTop >= 0)
					{
						if (iSHRight == 1)
							iSHRight++;
						if (iSHRight == 0)
							iSHRight += 2;
					}

					if (oTODivs.length > 0)
					{
						iDivOffsetTop -= 8;
					}
					var iBannerOffset = (oBottomBanner.offsetHeight + oBottomBanner.offsetTop) + (iSHRight * 20);
					iNewHeight += (iMainNav - iBannerOffset) - iDivOffsetTop;
					setHeight(oDiv, iNewHeight +'px');
					//oDiv.style.height = iNewHeight +'px';
				}
			}
		}

		var oMainContentLeft = document.getElementById('mainContentLeft');
		if (oMainContentLeft)
		{
			var oSHLeft = getElementsByClassName('sectionHeader', false, 'div', oMainContentLeft);
			var iSHLeft = oSHLeft.length;

			if ((oMainContentLeft.offsetHeight < oMainContentRight.offsetHeight))
			{
				if (iSHRight > 1)
					iOffset -= 8;
				if (iSHLeft > 1)
					iOffset += 8;

				var oVacatureDivs = getElementsByClassName('contentSectionVacatures', false, 'div', oMainContentLeft);
				if (oVacatureDivs.length > 0)
				{
					iOffset -= 16;
				}
			}
			else
			{
				if (oTODivs.length > 0)
				{
					iOffset -= 8;
				}
			}

			var oDiv = getElementsByClassName('lastSection', false, 'div', oMainContentLeft);
			oDiv = oDiv.pop();
			if (oDiv)
			{
				if (iSHLeft > 0)
					iDivOffsetTop += 0;
				setHeight(oDiv, (iMainNav - oDiv.offsetTop - iOffset - iDivOffsetTop) +'px');
				//oDiv.style.height = (iMainNav - oDiv.offsetTop - iOffset - iDivOffsetTop) +'px';
			}
		}
	}
}

function objItemInt_Click(e)
{
	var objItemIntSub = document.getElementById('itemIntSub');
	if (!objItemIntSub)
		return ;

	if (this.className == '')
	{
		this.className = 'itemIntActive';
		objItemIntSub.style.display = 'block';
	}
	else
	{
		this.className = '';
		objItemIntSub.style.display = 'none';
	}
}

var prev_onload = (window.onload) ? window.onload : function () {};
window.onload = function() {
	var objItemInt = document.getElementById('itemInt');
	if (objItemInt)
	{
		objItemInt.onclick = objItemInt_Click;
	}
	resizeDivs();
	prev_onload();
}

function toggleHint()
{
	var objHint = document.getElementById('searchFaqHint');

	if (objHint)
	{
		if (objHint.style.display == 'none')
			objHint.style.display = 'block';
		else
			objHint.style.display = 'none';
	}
}

var intPrevFaqId = -1;
function toggleFaqQuestion(intFaqId)
{
	if (intPrevFaqId != -1)
	{
		var objPrevFaqQuestion = document.getElementById('faq_q_'+ intPrevFaqId);
		var objPrevFaqAnswer = document.getElementById('faq_a_'+ intPrevFaqId);
		if (objPrevFaqQuestion && objPrevFaqAnswer)
		{
			objPrevFaqQuestion.style.backgroundColor = '';
			objPrevFaqAnswer.style.display = 'none';
		}
	}
	if (intPrevFaqId != intFaqId)
	{
		intPrevFaqId = intFaqId;
		var objFaqQuestion = document.getElementById('faq_q_'+ intFaqId);
		var objFaqAnswer = document.getElementById('faq_a_'+ intFaqId);
		if (objFaqQuestion && objFaqAnswer)
		{
			objFaqQuestion.style.backgroundColor = '#fff';
			objFaqAnswer.style.display = '';
		}
	}
	else
	{
		intPrevFaqId = -1;
	}
	resizeDivs();
}

var intPrevTO = -1;
function toggleTO(iTO)
{
	if (intPrevTO != -1)
	{
		$('#totaaloplossing'+ intPrevTO).hide();
	}
	if (intPrevTO != iTO)
	{
		intPrevTO = iTO;
		$('#totaaloplossing'+ iTO).show();
		resizeDivs();
	}
	else
	{
		intPrevTO = -1;
	}
}

function fncChangeToepassing()
{
	oVraagstuk.options.length = 0;
	oVraagstuk.options[0] = new Option('Maak een keuze','');
	if (aVraagstuk[this.value] && (aVraagstuk[this.value].length > 0))
	{
		var xl = aVraagstuk[this.value].length;
		for (siId in aVraagstuk[this.value])
		{
			oVraagstuk.options[oVraagstuk.options.length] = new Option(aVraagstuk[this.value][siId],siId);
		}
	}
	if (this.value == iToepassing)
	{
		oVraagstuk.value = iVraagstuk;
	}

	if (aDoelgroepQ[this.value])
	{
		oDoelgroepLbl.innerHTML = aDoelgroepQ[this.value];
	}
	else
	{
		oDoelgroepLbl.innerHTML = aDoelgroepQ[0];
	}
	oDoelgroepLbl.innerHTML = '3. '+ oDoelgroepLbl.innerHTML +' *';

	oDoelgroep.options.length = 0;
	oDoelgroep.options[0] = new Option('Maak een keuze','');
	if (aDoelgroep[this.value] && (aDoelgroep[this.value].length > 0))
	{
		var xl = aDoelgroep[this.value].length;
		for (siId in aDoelgroep[this.value])
		{
			oDoelgroep.options[oDoelgroep.options.length] = new Option(aDoelgroep[this.value][siId],siId);
		}
	}
	if (this.value == iToepassing)
	{
		oDoelgroep.value = iDoelgroep;
	}
}

function handleFavLinkClick()
{
	var sLink = $(this).attr('href');
	sItem = 'favID';
	if (sLink.indexOf(sItem +'=') == -1)
	{
		sItem = 'delFavID';
	}

	var iId = parseInt(sLink.substr(sLink.indexOf(sItem +'=') + sItem.length + 1));
	if (iId > 0)
	{
		params = {};
		params[sItem] = iId;
		$.get('/producten/producten_fav_ajax.html', params,
			function (sData)
			{
				$('#favorietenLijst').html('Favorietenlijst'+ sData);
				resizeDivs();
				$('a.addFavLink,a.delFavLink').click(handleFavLinkClick);
			});
		return false;
	}
	return false;
}

$(document).ready(function()
{
	$('.toRadio').click(function ()
   {
		if (intPrevTO != -1)
		{
			$('#totaaloplossing'+ intPrevTO).hide();
		}
		intPrevTO = $(this).val();
		$('#totaaloplossing'+ intPrevTO).show();
		$('#totaalOplossingBestelKnop').show();
		resizeDivs();
   });

	$('.totaalpakketOpties').change(function ()
	{
		var val = $(this).val() + '';
		if (val.substr(0, 1) == ',')
			val = val.substr(1);

		var iTO = $(this).attr('id').substr(6); // opties..

		$('#div_'+ $(this).attr('id')).html('<em>Producten worden opgezocht...</em>')
			.load('/producten/totaaloplossing_wizard_ajax.html', { a: 'pakketoptie', iTO: iTO, opties: "'"+ val +"'" }, resizeDivs);
	});

	$('a.addFavLink,a.delFavLink').click(handleFavLinkClick);

	$('#toepassing').change(fncChangeToepassing).trigger('change');

	$('#contentMAFForm').submit(function ()
	{
		if (checkMAF())
		{
			var params = {};
			$(this).find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea")
				.filter(":enabled")
				.each(function()
				{
					// multiple values ?
					if (this.name && (this.name.indexOf('[]') != -1))
					{
						if (!params[this.name])
							params[this.name] = [];
						params[this.name][params[this.name].length] = this.value;
					}
					else
					{
						params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
					}
				});
			$.post($(this).attr("action"), params, function (e)
			{
				if (e != '')
					alert(e);
				else
					hideMAF();
			});
		}
		return false;
	});
});

function input_initSelect(str_id, fnc_changed, bln_callonchange)
{
	if (typeof bln_callonchange == 'undefined')
	{
		bln_callonchange = false;
	}
	var obj_input = document.getElementById(str_id);
	obj_input.onfocus   = input_selectFocussed;
	obj_input.onchange  = input_selectChanged;
	obj_input.onkeydown = input_selectKeyed;
	obj_input.onclick   = input_selectClicked;

	obj_input.fnc_changed = fnc_changed;

	if (bln_callonchange)
	{
		obj_input.changed = true;
		obj_input.onchange();
	}
}

function input_selectChanged(obj_e)
{
	var obj_input;
	if (obj_e && obj_e.value)
	{
		obj_input = obj_e;
	}
	else
	{
		obj_input = this;
	}
	if (!obj_input.changed)
	{
		return ;
	}

	if (obj_input.fnc_changed)
		obj_input.fnc_changed();

	obj_input.changed = false;
}

function input_selectClicked()
{
	this.changed = true;
}

function input_selectFocussed()
{
	this.initValue = this.value;
}

function input_selectKeyed(e)
{
	var obj_e;
	var keyCodeTab   = "9";
	var keyCodeEnter = "13";
	var keyCodeEsc   = "27";

	if (e)
	{
		obj_e = e;
	}
	else
	{
		obj_e = event;
	}

	if ((obj_e.keyCode == keyCodeEnter || obj_e.keyCode == keyCodeTab) && this.value != this.initValue)
	{
		this.changed = true;
		this.onchange(this);
	}
	else if (obj_e.keyCode == keyCodeEsc)
	{
		this.value = this.initValue;
	}
	else
	{
		this.changed = false;
	}
}

var toggleSelectTo = false;
function toggleSelect() {
	if (toggleSelectTo) {
		var selectboxen = document.getElementsByTagName("select");
		for(i = 0; i < selectboxen.length; i++) {
			selectboxen[i].style.visibility = toggleSelectTo;
		}
	}
}

function hideSelect() {
	toggleSelectTo = "hidden";
	toggleSelect();
}

function unhideSelect() {
	toggleSelectTo = "visible";
	toggleSelect();
}

function showMAF()
{
	hideSelect();
	changeOpac(40, 'mafBlend');
	document.getElementById('mafBlend').style.display = 'block';
	document.getElementById('mafContainer').style.display = 'block';
	document.getElementById('contentMAFForm').reset();

	//return false;
}

function hideMAF()
{
	unhideSelect();
	document.getElementById('mafBlend').style.display = 'none';
	document.getElementById('mafContainer').style.display = 'none';
	return false;
}

function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

function isValidEmail(strEmail){
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	// search email text for regular exp matches
	return (strEmail.search(validRegExp) != -1);
}

function checkMAF()
{
	if (document.getElementById('mafName').value == '') {
		alert('Je moet je naam invullen');
		return false;
	} else if (document.getElementById('mafEmail').value == '' || !isValidEmail(document.getElementById('mafEmail').value)) {
		alert('Je moet een geldig e-mailadres invullen');
		return false;
	} else if (document.getElementById('mafFriend').value == '' || !isValidEmail(document.getElementById('mafFriend').value)) {
		alert('Je moet een geldig vriend e-mailadres invullen');
		return false;
	} else if (document.getElementById('mafFriend1').value != '' && !isValidEmail(document.getElementById('mafFriend1').value)) {
		alert('Vriend 1 e-mailadres is ongeldig');
		return false;
	} else if (document.getElementById('mafFriend2').value != '' && !isValidEmail(document.getElementById('mafFriend2').value)) {
		alert('Vriend 2 e-mailadres is ongeldig');
		return false;
	} else if (document.getElementById('mafFriend3').value != '' && !isValidEmail(document.getElementById('mafFriend3').value)) {
		alert('Vriend 3 e-mailadres is ongeldig');
		return false;
	} else if (document.getElementById('mafFriend4').value != '' && !isValidEmail(document.getElementById('mafFriend4').value)) {
		alert('Vriend 4 e-mailadres is ongeldig');
		return false;
	}

	document.getElementById('mafUrl').value = window.location.href;
	return true;
}