		var pages = new Object();
		var activeRequests = new Object();
		var historie = new Array();
		var pageScripts = new Object();
		
		var currentdocid = false;
		var momentgeduld = false;
		//		var w = window.open ();
		var momentgeduldText = "<div id=\"momentgeduldinhoud\">Moment geduld a.u.b.</div>";

        
        
    /**
     * This method checks if it is needed to show the 'naar overzicht' knop.
     * In theory this method can be used for every button when called on it's 
     * onload event. The button is hided when the property verbergOverzichtKnop 
     * of the collectief has been set.
     */
    function verbergOverzichtKnopCheck (button) {
//        alert('IN verbergOverzichtKnopCheck');
        
        if (typeof originalAnswers != "undefined" && typeof button != "undefined") {
            if (typeof originalAnswers["verbergOverzichtKnop"] != "undefined" && originalAnswers["verbergOverzichtKnop"] == "J") {
                var ret = new Object();
                
                ret["buttonOnclick"] = "";
                ret["buttonText"] = "";
                
                button.style.visibility = "hidden";
                
                return ret;
            }
        }
    }
        
        
	/* 	Function: 
			gotoWizardPage
			gotoWizardPage function. 
	*/
   
   //Called from the wizards navigation when we want to change page
	//docid = document id of the page we want to go to
	function gotoWizardPage(docid, fromnext, checkValidation, enabled, serverError){
		//alert("gotoWizardPage");
		if  (typeof customGotoWizardPage !== "undefined") {
			if (customGotoWizardPage(docid, fromnext, checkValidation, enabled) === false)
				return false;
		}
		
		if(typeof enabled !== "undefined" && enabled == "NO"){
			return false;
		}
		
		momentgeduld = true;
		MomentGeduldCheck();
		
		// Extra check to show an wait icon when neccesary.
		volgendeVorigeWachtIcoonCheck(fromnext);
		
		var isValid = true;
		if(checkValidation == true){
			 isValid = checkPageValidation(true);
		}else if (serverError !== true) {
			clearAllErrors();
		}
		if(isValid){
			//If we dont have the page cached...
			if(typeof pages[docid] == "undefined" || typeof pageScripts[docid] !== "undefined"){
				if(typeof activeRequests[docid] == "undefined" || activeRequests[docid] != true){
					activeRequests[docid] = true;
					if(typeof hideuntilbrickid !== "undefined"){
						hideuntilbrickid = rembrickid;
						rembrickid = false;
						hideuntildocid = remdocid;
						remdocid = false;
					}
					var url = "/document.xehtml?DocID="+docid;
					//alert("from:" + currentdocid + "to" + docid);
					var fromdocid = currentdocid;
					currentdocid = docid;
					
					//Create a new httpxmlrequest to teh above url
					var r = http.nonCacheReq (url, true);
					
					//when the request has finished update the page
					
					//uses docid, currentdocid, currentdocid, fromdocid, 
					r.callback = function updatePage(docid) {
							// Temporarily store the doc ID so the request can 
							// be set as processed at the end of the callback. 
							// The variable currentdocid is changed by some 
							// code, so it can't be used.
							var tmpCurrentDocId = currentdocid;
							var nextDocXML = r.responseObject.responsexml.documentElement;
							childNodes = nextDocXML.childNodes;
							var nextDocHtml = "";
							for(i = 0 ; i < childNodes.length ; i++){
								var childNode = childNodes[i];
								if(childNode.nodeType === 3){
									nextDocHtml += childNode.nodeValue;
								}
							}

							try {
								if (pageTracker)
									pageTracker._trackPageview(url);
							} catch(err) {}

							//var root_node = nextDocXML.getElementsByTagName('xmlencodedhtml').item(0);
							//nextDocHtml = root_node.text;
							//alert(nextDocHtml);
							 var regExp = new RegExp ('<scr'+'ipt wiz=true>([^<]+)<\/scr'+'ipt>', 'gim');

							 var match = null;
							 while ((match = regExp.exec (nextDocHtml)) != null) {
								 var errors = eval (match [1]);
							 }
							 if(typeof errors == "undefined" || errors == false){
								 //cache for next time we wanna get this page if it's not a noncachable document
								 if (typeof nonCachableDocs == "undefined" || typeof nonCachableDocs[tmpCurrentDocId] == "undefined" || nonCachableDocs[tmpCurrentDocId] != "true"){
									 pages[tmpCurrentDocId] = nextDocHtml;
								}
							}
							if(typeof errors == "undefined" || errors == false){
								 //change the page

								 changePage(nextDocHtml, fromdocid, fromnext);
								 var regExp = new RegExp ('<scr'+'ipt postwiz=true>([^<]+)<\/scr'+'ipt>', 'gim');
		
								 var match = null;
								 while ((match = regExp.exec (nextDocHtml)) != null) {
									pageScripts[tmpCurrentDocId] = match [1];
									var errors = eval (match [1]);
								 }
							}
							else{
								var newdocid = findFirstPageWithError();
								if(newdocid != false){
									gotoWizardPage(newdocid, false, false, false, true);
										if(typeof highlightClass !== "undefined" && typeof nonHighlightClass !== "undefined"){
											highlightErrors(nonHighlightClass, highlightClass);
										}
								}
							}
							activeRequests[tmpCurrentDocId] = false;
					};
					//do the request
					r.get ();
				}
			}
			//otherwise we have the page cached already
			else{
				
				if(typeof hideuntilbrickid !== "undefined"){
					hideuntilbrickid = rembrickid;
					rembrickid = false;
					hideuntildocid = remdocid;
					remdocid = false;
				}

				var fromdocid = currentdocid;
				//alert("setting currentdocid to "+docid);
				currentdocid = docid;
				
				changePage(pages[docid], fromdocid, fromnext);
				
				if (typeof pageScripts[docid] !== "undefined")
					eval(pageScripts[docid]);
			}
		}
	}	
 
 	
 	/* Function: updatePage
			updatePage function. */
    function updatePage(docid) {
		//get the reply html
		// var res = r.responseObject.responsetext;
		 //cache for next time we wanna get this page
		 	var xmlIO = datamanager.xmlIOs["nl.pagelink.torque.descriptor.CommandResponse"];
			var res = xmlIO.getValue(1,"Response");
		 
		 
         pages[currentdocid] = res;
		 changePage(pages[currentdocid], fromdocid, fromnext);
	}
	
   //Actually change the page to the correct html
  function changePage(htmlpage, fromdocid, fromnext){
	//GET errors html before changing pages
	var errorEl = document.getElementById("wizarderror");
	if(errorEl != null && errorEl != false){
		var orgErrors = errorEl.innerHTML;
	}
	document.body.innerHTML = htmlpage;
	
	// uitbreiding: voer de functies uit die in de array window.execfunctions staat
	if (typeof window.execfunctions == 'object') 
		for (var ii=0;ii< window.execfunctions.length;ii++) 
			if (typeof window[window.execfunctions[ii]]=='function')
				window[window.execfunctions[ii]] ();
		
	// einde uitbreiding: voer de functies uit die in de array window.execfunctions staat

	//generate dynamic html of question bricks
		
	if(typeof orgErrors != "undefined"){
		var errorEl = document.getElementById("wizarderror");
		if(errorEl != null && errorEl != false){
			errorEl.innerHTML = orgErrors;
		}
	}	

	renderQuestionBricks();
	//alert("might change prev button now");
	if(fromdocid !== false){
		if(fromnext == true){
			historie[historie.length] = fromdocid;	
			//window.location.hash = "h" + historie.length;
		}else{
			//We went a page back so the current page can be removed from the historie
			historie.pop();
			if(historie.length > 0){
				//Than set the previous button to the new last page in history
				fromdocid = historie[historie.length - 1];
			}else{
				//No more previous pages known so don't set it
				fromdocid = false;
			}
		}
		if(fromdocid !== false){
			var prevElements = document.getElementsByName("wn_prev");
			//for each next button...
			for(var i = 0; i < prevElements.length; i++){
				var prevElement = prevElements[i];
				//alert("setting prev to " + fromdocid);
				prevElement.setAttribute("gotodocid",fromdocid);
			}
		}
	}	

	var isPageComplete = checkMandatoryQuestionsOnPage();
	
	window.setTimeout(function (){enableNexts(isPageComplete && checkPageValidation(false))}, 10);

	if(typeof highlightClass !== "undefined" && typeof nonHighlightClass !== "undefined"){
		highlightErrors(nonHighlightClass, highlightClass);
	}
	
	document.body.scrollTop = 0;

	if (typeof changePagePostAction == "function") {
		changePagePostAction();
	}
  }
   
   
    function MomentGeduldSetText(text) {
		momentgeduldText = text;
	}
	
	function MomentGeduldDisplay(message) {
		momentgeduld = true;
		var emg = document.getElementById('momentgeduld');
		if (!emg)
			return false;
		
		emg.innerHTML = momentgeduldText;
		var top = MomentGeduldZetHoogte();
		emg.style.top = top;
		emg.style.display = 'block';
	}
	
	function MomentGeduldHide() {
		
		var emg = document.getElementById('momentgeduld');
		if (emg) {
			momentgeduld = false;
			emg.style.display = 'none';
			window.scrollTo(0,0);
		}
	}

	function MomentGeduldCheck() {
		if (typeof momentgeduldDisabled != 'undefined' && momentgeduldDisabled === true) 
			return;

		if (momentgeduld == false)
			MomentGeduldHide();
		else
			MomentGeduldDisplay();
	}

	function MomentGeduldZetHoogte() {
		var maintablehoogte = document.getElementById('divMaintable');
		if (maintablehoogte) {
			maintablehoogte = maintablehoogte.offsetHeight
			var tmptop = maintablehoogte / 2;
			tmptop = Math.round(tmptop) + 80;
			return tmptop;
		} else {
			return 100;
		}
	}
	
	/**
	 * This method displays an wait icon on the volgende and vorige buttons of the wizard. This 
	 * icon is only shown if the option 'useVolgendeVorigeWachtIcoon' is set in the html writer 
	 * of the site and has the value 'true'. The icon is reset when the next or previous page is 
	 * shown, so it isn't neccesary to hide the icon.
	 *
	 * @param	fromnext	Indicates if the volgende or the vorige button was pressed. If the 
	 *						parameter is 'true', then the volgende button was pressed. If the 
	 *						vorige buttons was pressed, then the parameter must be 'false'.
	 *
	 * @see		See Bureau Leemans for an example.
	 */
	function volgendeVorigeWachtIcoonCheck(fromnext) {
	
		if (typeof useVolgendeVorigeWachtIcoon != "undefined" && 
				useVolgendeVorigeWachtIcoon == true) {
			var volgendeVorigeButton = 'wn_next';
			
			if (!fromnext) {
				volgendeVorigeButton = 'wn_prev';
			}
			
			var el = document.getElementById(volgendeVorigeButton);

			if (!el)
				return false;
			
			el.className = 'waitbutton';
			el.innerHTML = '&nbsp;&nbsp;';
		} 
	}

    /*wizard: toon of verberg het veldje waar je een eamiladres kan invullen*/
    function showEmailSurvey(el, nextDocId) {
    	var maildiv = document.getElementById('TMP__1234567890');
    	var actiontype = "premieberekening";
    	if (!maildiv){
    		var  div = nodes.parentNodeBy(el, 'className=wn_navbar');
    		var newDiv = document.createElement('DIV');
    		
    		div.parentNode.appendChild(newDiv);
    		newDiv.id = "TMP__1234567890";
    		newDiv.className = 'emaildiv';
    		newDiv.innerHTML = '<div onclick="hideEmailSurvey(this)">X</div>';
    		newDiv.innerHTML += '<span class=vvkop><strong>Enquete opslaan</strong></span><br>&nbsp;';
    		newDiv.innerHTML += '<p>Als u de enquete op een later tijdstip wilt voortzetten, dan dient u hieronder uw emailadres in te vullen. Er wordt u dan een emailbericht toegezonden met daarin een link naar deze webpagina. Wanneer u op deze link klikt, dan kunt u de enquete voortzetten.</p>';
    		newDiv.innerHTML += '<p><form name="emailsurvey" onsubmit="displayWizardMomentGeduld(\'emailopslaan\');enqueteOpslaan('+nextDocId+',document.forms[\'emailsurvey\'].emailadres.value);return false;"><input name="emailadres" style="width: 200px;"><input type="button" class="wn_otherpage" value="Versturen" onclick="displayWizardMomentGeduld(\'emailopslaan\');enqueteOpslaan('+nextDocId+',document.forms[\'emailsurvey\'].emailadres.value);"></form></p>';
    		
    		document.forms['emailsurvey'].emailadres.value
    		
    		
    	} else {
    		maildiv.style.display='block';
    	}
    }

    function hideEmailSurvey(el) {
    	var maildiv = document.getElementById('TMP__1234567890');
    	if (!maildiv) return false;
    	maildiv.style.display='none';
    }

    /*wizard: toon of verberg het opslaan van de resultaten*/
    function showOpslaanResults(el,responseid,site) {
    	var maildiv = document.getElementById('TMP__1234567890');
    	var actiontype = "premieberekening";
    	if (!maildiv){
    		var  div = nodes.parentNodeBy(el, 'className=wn_navbar');
    		var newDiv = document.createElement('DIV');
    		
    		div.parentNode.appendChild(newDiv);
    		newDiv.id = "TMP__1234567890";
    		newDiv.className = 'emaildiv';
    		newDiv.innerHTML = '<div onclick="hideOpslaanResults(this)">X</div>';
    		newDiv.innerHTML += '<span class=vvkop><strong>Enquete resultaten opslaan</strong></span><br>&nbsp;';
    		newDiv.innerHTML += '<p>Als u de resultaten van de enquete op een later tijdstip wilt raadplegen, dan dient u hieronder uw emailadres in te vullen. Er wordt u dan een emailbericht toegezonden met daarin een link naar deze webpagina. Wanneer u op deze link klikt, dan kunt u de resultaten inzien.</p>';
    		newDiv.innerHTML += '<p><form name="emailsurvey" onsubmit="displayWizardMomentGeduld(\'emailopslaan\');resultatenOpslaan(document.forms[\'emailsurvey\'].emailadres.value,'+responseid+',\''+site+'\');hideOpslaanResults(this);return false;"><input name="emailadres" style="width: 200px;"><input type="button" class="wn_otherpage" value="Versturen" onclick="displayWizardMomentGeduld(\'emailopslaan\');resultatenOpslaan(document.forms[\'emailsurvey\'].emailadres.value,'+responseid+',\''+site+'\');hideOpslaanResults(this);"></form></p>';
    	} else {
    		maildiv.style.display='block';
    	}
    }

    function hideOpslaanResults(el) {
    	var maildiv = document.getElementById('TMP__1234567890');
    	if (!maildiv) return false;
    	maildiv.style.display='none';
    }

    function displayWizardMomentGeduld(soort) {
    	toggleSelects('none');
    	if (soort == 'emailopslaan')
    		document.getElementById('momentgeduldinhoud').innerHTML = '<b>Moment geduld a.u.b.</b><br>De enquete wordt opgeslagen...';
    	else
    		document.getElementById('momentgeduldinhoud').innerHTML = '<b>Moment geduld a.u.b.</b>';
    	var top = ZetMomentHoogte();
    	document.getElementById('momentgeduld').style.top = top;
    	document.getElementById('momentgeduld').style.display = 'block';
    }

    function toggleSelects(actie) {
    	var selects = document.getElementsByTagName('SELECT');

    	if (typeof selects == 'object') {
    		for (var i = 0; i < selects.length; i++)
    			selects[i].style.display = actie;
    	}
    }

    function ZetMomentHoogte() {
    	var maintablehoogte = document.getElementById('mainsite').offsetHeight;
    	var tmptop = maintablehoogte / 2;
    	tmptop = Math.round(tmptop) + 80;
    	return tmptop;
    }
    
    
    function enqueteOpslaan (docId, emailaddress) {
        //alert('IN enqueteOpslaan');
        if(typeof givenAnswers == "undefined"){
			return false;
		}
        
        var url = "/mappings/xml/informationobject/callToServer";

        enqueteOpslaan.docId = docId;
        
		var r = http.nonCacheReq (url, true);	
		var xml = "<module name=\"system::wizard\">";
		xml = xml + "<function name=\"enqueteOpslaan\">";
        xml = xml + "<param name=\"docid\">"+docId+"</param>";
		xml = xml + "<param name=\"email_to\">"+emailaddress+"</param>";
        
        for(var brickid in givenAnswers){
            xml = xml + "<param name=\"bid"+brickid+"\">";
            if(typeof givenAnswers[brickid] == "object"){
                for(var subanswer in givenAnswers[brickid]){
                    if(givenAnswers[brickid][subanswer] == ""){
                        xml = xml + subanswer+ "#####";
                    }
                    else{
                        xml = xml + subanswer+ " -> "+ givenAnswers[brickid][subanswer]+"#####";
                    }
                }
            }	
            else{
                xml = xml + givenAnswers[brickid];
            }
            xml = xml + "</param>";
		}
        
		xml = xml + "</function>";
		xml = xml + "</module>";	

		r.callback = function enqueteOpslaanDone() { gotoWizardPage(enqueteOpslaan.docId, true,false,"YES"); };
		r.post (xml);
    }
    
    
    /**
     * This method is used to send the user an emailmessage with an link. When the user clicks 
     * the link, he/she is send to the resultspage of an enquete. On the moment this method is 
     * only used with Fosag to show issues and verbeterpunten.
     */
    function resultatenOpslaan (emailaddress,responseid,site) {
        //alert('IN enqueteOpslaan');
        if(typeof givenAnswers == "undefined"){
			return false;
		}
        
        var url = "/mappings/xml/informationobject/callToServer";
        
		var r = http.nonCacheReq (url, true);	
		var xml = "<module name=\"system::wizard\">";
		xml = xml + "<function name=\"resultatenOpslaan\">";
		xml = xml + "<param name=\"email_to\">"+emailaddress+"</param>";
		xml = xml + "<param name=\"responseid\">"+responseId+"</param>";
		xml = xml + "<param name=\"site\">"+site+"</param>";
        
        for(var brickid in givenAnswers){
            xml = xml + "<param name=\"bid"+brickid+"\">";
            if(typeof givenAnswers[brickid] == "object"){
                for(var subanswer in givenAnswers[brickid]){
                    if(givenAnswers[brickid][subanswer] == ""){
                        xml = xml + subanswer+ "#####";
                    }
                    else{
                        xml = xml + subanswer+ " -> "+ givenAnswers[brickid][subanswer]+"#####";
                    }
                }
            }	
            else{
                xml = xml + givenAnswers[brickid];
            }
            xml = xml + "</param>";
		}
        
		xml = xml + "</function>";
		xml = xml + "</module>";	

		r.callback = function resultatenOpslaanDone() {};
		r.post (xml);
    }
	

