<!-- form logic Gomizu

// Immediate execution
var isNS4 = (document.layers) ? true : false; 
var isIE4 = (document.all && !document.getElementById) ? true : false; 
var isIE5 = (document.all && document.getElementById) ? true : false; 
var isNS6 = (!document.all && document.getElementById) ? true : false;

var analysisShowing = false;
var matrixFirstQuestionNr = 0;
var titleTextAlreadyPrinted = false; 

var nextPage;

// Functions
function numberOfQuestions() {
	return (questions.length <= 0) ? 0 : questions.length-1;
}


function switchTo(question) {
//	alert('switchTo('+question+')');
	analysisShowing = false;
	if ((question > 0) && (!layout['switch']))
		currentQuestion = skipAnyQuestions(question);
	hideSection('top');
	hideSection('bot');
	updateSection('top', getTop(currentQuestion));
	updateSection('bot', getBottom(currentQuestion));
	showSection('top');
	showSection('bot');
}

function switchToAnalysis(question) {
	analysisShowing = true;
	currentQuestion = question;
//	alert('switchToAnalysis('+question+')'); 
	if ((question > 0) && layout['switch'])
		currentQuestion = nextAvailableAnalysis(question);
	hideSection('top');
	hideSection('bot');
	updateSection('top', getTop(question));
	updateSection('bot', getBottom(question));
	showSection('top');
	showSection('bot');
}



function getWindow(section) {
	if (isIE4) {
		return document.all['tab_' + section + '_IE'];
	} else 
	if (isNS4) {
		return document.layers['pos_' + section + '_NN'].document.layers['tab_' + section + '_NN'];
	} else {
		return document.getElementById('tab_' + section + '_IE');
	}	
}


function getElements() {
	var obj;

	if (!isNS4) {
		return (document.forms['questionForm'].elements == null) ? 
			[] : document.forms['questionForm'].elements;
	} else 
	if (isNS4) {
		return (document.layers['pos_top_NN'].document.layers['tab_top_NN'].document.forms['questionForm'].elements == null) ? 
[] : document.layers['pos_top_NN'].document.layers['tab_top_NN'].document.forms['questionForm'].elements;
	} 
}

function getElement(fieldName) {
	var elements = getElements();
	for (var i=0; i<elements.length; i++) {
		if (elements[i].name == fieldName)
			return elements[i];
	}
}


function getBottom(questionFrom) {
	var html = '';
	nextPage = '';
//	alert('getBottom('+questionFrom+')');
	if (questionFrom == 0) {
		html += '<table border="0" cellpadding="5" cellspacing="0" width="600">\n';
	      html += '<tr>\n';
            html += '<td width="100%" align="center">\n';
		if (numberOfQuestions() == 0) {
			html += '<a href="javascript:if (validatePersonalia() && updateSubmitFields()) {checkMail(); document.forms[\'mainForm\'].submit();}">';    	
			html += '<img border="0" src="i_end' + '.gif"  alt="' + 'gegevens opsturen' + '">'; 
			html += '</a>';
			nextPage = 'if (validatePersonalia() && updateSubmitFields()) {checkMail(); document.forms[\'mainForm\'].submit();}';    
		} else {
			if ((questions[0]['text']['text'] != "") && (questions[0]['text']['text'] != "#$#")) {
				// Introductory text displayed 
				html += '<a href="javascript:questions[0][\'text\'][\'text\'] = \'#$#\'; switchTo(0);">';
				html += '<img border="0" src="i_next' + '.gif"  alt="' + 'start met de enquête' + '">'; 
				html += '</a>';
				nextPage = 'questions[0][\'text\'][\'text\'] = "#$#"; switchTo(0);';
			} else {    
				html += '<a href="javascript:if (validatePersonalia()) switchTo(' + 1 + ');">';    	
				html += '<img border="0" src="i_next' + '.gif"  alt="' + 'start met de enquête' + '">'; 
				html += '</a>';
				nextPage = 'if (validatePersonalia()) switchTo(1);';
			}    
		}
		html += '</td>\n';
		html += '</tr>\n'; 
		html += '</table>\n';
	} else
		if (numberOfQuestions() > 0)
			html += getNavigation(questionFrom);
	return html;
}

function getTop(questionFrom) {
	var html = '';
	var htmlBgColor = '';
//	alert('getTop('+questionFrom+')');
	if (questionFrom == 0) {
		if ((questions[0]['text']['text'] != "") && (questions[0]['text']['text'] != "#$#")) {
			html += '<form name="questionForm">\n';
			html += '<table border="0" cellpadding="5" cellspacing="0" width="600"><tr>\n';
    			if (layout['text']['bgColor'] == "")
				htmlBgColor = ''
			else
				htmlBgColor = 'bgColor="' + layout['text']['bgColor'] + '"';  
			html += '<td valign="top" ' + htmlBgColor + ' >' + questions[0]['text']['text'];
			html += '</td></tr></table></form>\n';
		} else {
			html += personalia;
		}
	} else {
		if (numberOfQuestions() == 0) {
			html += '<table border="0" cellpadding="5" cellspacing="0" width="600"><tr>\n';
    			if (layout['text']['bgColor'] == "")
				htmlBgColor = ''
			else
				htmlBgColor = 'bgColor="' + layout['text']['bgColor'] + '"';  
			html += '<td valign="top" ' + htmlBgColor + ' >' + questions[0]['text']['text'];
			html += '</td></tr></table>\n';
		} else {
  			html += getQuestions(questionFrom);
		}
	}
	return html;
}

function getInitTop() {
	var html = '';
      html += '<table width="600"><tr><td height="300">&nbsp;</td></tr></table>';
	return html;
}	


function getInitBottom() {
	var html = '';
      html += '<table width="600"><tr><td height="50">&nbsp;</td></tr></table>';
	return html;
}

function getNavigation(questionFrom) {
	var html = '';
	var questionNr = questionFrom;

	html += '<table border="0" cellpadding="5" cellspacing="0" width="600">\n';
	html += '<tr>\n';
	do {
		questionNr++;
	} while ((questionNr <= numberOfQuestions()) && (!questions[questionNr]['break']));
	  // Stop if next question has a break.

	// Start button (first question)
      html += '<td width="33%" align="left">\n';
	if (questionFrom > 1) {
		if (layout['switch'])
			html += '<a href="javascript:if (confirm(\'Wilt u terug naar het begin van de enquête?\')) switchToAnalysis(\'' + (1) + '\')">'
		else    	
			html += '<a href="javascript:if (confirm(\'Wilt u opnieuw beginnen met de enquête?\')) switchTo(\'' + (1) + '\')">';    	
		html += '<img border="0" src="i_start' + '.gif"  alt="' + 'Wilt u opnieuw beginnen met de enquête?' + '">' 
		html += '</a>';
	} else {
		html += '&nbsp;\n';
	}
      html += '</td>';

	// Refresh button
      html += '<td width="33%" align="center">\n';
	if (analysisShowing)
		html += '<a href="javascript:switchToAnalysis(\'' + (questionFrom) + '\');">';
	else    	
		html += '<a href="javascript:switchTo(\'' + (questionFrom) + '\');">';    	
	html += '<img border="0" src="i_refrsh' + '.gif"  alt="' + 'maak ongedaan en toon opnieuw' + '">' 
	html += '</a>';

	//Analysis/Question button
	if (!analysisShowing && analysisAvailable(questionFrom)) {
		if (layout['switch']) {
			// Response phase has expired. No answers are saved.
			// Switching between question and analysis is possible.
			html += '<a href="javascript:switchToAnalysis(\'' +
 				(questionFrom) + '\');">';    	
			html += '<img border="0" src="i_analys.gif"  alt="' + 'toon analyse' + '">' 
			html += '</a>';
		}
		else {
			html += '<a href="javascript:if (saveQuestions()) switchToAnalysis(\'' +
 				(questionFrom) + '\');">';    	
			html += '<img border="0" src="i_analys.gif"  alt="' + 'bevestig invulling en toon analyse (uw antwoord is hierin nog niet verwerkt!)' + '">' 
			html += '</a>';
		}
	}
	else if (analysisShowing && layout['switch']) {
		html += '<a href="javascript:switchTo(\'' + (questionFrom) + '\');">';    	
		html += '<img border="0" src="i_quest' + '.gif"  alt="' + 'toon vraaggedeelte' + '">' 
		html += '</a>';
	}
      html += '</td>';
      html += '<td width="33%" align="right">\n';
	if ((questionNr <= numberOfQuestions()) && (!layout['switch'] || nextAvailableAnalysis(questionNr)) ) {
		if (layout['switch']) {
			html += '<a href="javascript:switchToAnalysis(\'' +
				(nextAvailableAnalysis(questionNr)) + '\');">';
			nextPage = 'switchToAnalysis(' + (nextAvailableAnalysis(questionNr)) + ');';    
		} else {
	 		if (analysisShowing) { 
				html += '<a href="javascript:switchTo(\'' + (questionNr) + '\');">';
				nextPage = 'switchTo(' + (questionNr) + ');';    
			} else {    	
				html += '<a href="javascript:if (saveQuestions()) switchTo(\'' + (questionNr) + '\');">';
				nextPage = 'if (saveQuestions()) switchTo(' + (questionNr) + ');';    
			}
		}    	
		html += '<img border="0" src="i_next' + '.gif"  alt="' + 'volgende vraag' + '">' 
		html += '</a>';
	} else {
		if (layout['switch']) {
			html += '<a href="javascript:alert(\'Einde van de analyse.\');">';    	
			nextPage = 'alert(\'Einde van de analyse.\');';    
		} else {
			html += '<a href="javascript:if (updateSubmitFields()) {checkMail();document.forms[\'mainForm\'].submit();}">'; 
			nextPage = 'if (updateSubmitFields()) {checkMail();document.forms[\'mainForm\'].submit();}';    
		}   	
		html += '<img border="0" src="i_end' + '.gif"  alt="' + 'verzend de enquête' + '">' 
		html += '</a>';
	}		
	html += '</td>\n';
	html += '</tr>\n'; 
	html += '</table>\n';
	return html;
}


function questionAnalysisAvailable(question) {

  if (question['answer']['type'] == "info")
    return false;

  if (!question['analysis']['publish'])
    return false;

  if ((question['answer']['type'] == "text") ||
      (question['answer']['type'] == "date"))
    return (question['analysis']['textual'] != "");

  return (question['analysis']['frequency']['publish'] ||
	  question['analysis']['graph']['publish'] ||
	  (question['analysis']['medium'] != ""));
}


function analysisAvailable(questionFrom) {
// Return true if analysis data is available for the currently displayed questions.
// So iterate till break or end of survey.
	var available = false;

	do {
		available = questionAnalysisAvailable(questions[questionFrom]);
		questionFrom++;		
	} while (!available && 
			(questionFrom <= numberOfQuestions()) && 
			(!questions[questionFrom]['break']));
		// Stop if question has a break and next question should start on a new tab/page
		// or - of course - when analysis is available.
	return available;
}

function nextAvailableAnalysis(questionFrom) {
// Return the question number of next available analysis.
// If none, result is 0.
	var available = false;
	do {
		available = questionAnalysisAvailable(questions[questionFrom]);
		if (!available)
			questionFrom++;		
	} while (!available && 
			(questionFrom <= numberOfQuestions()));
		// Stop if question has analysis available.
 	if (available) {
	  return questionFrom
	}
	else
  	  return (0);
}

function checkStartMatrix(questionFrom) {

	if ((questionFrom == 1) || (questions[questionFrom]['matrix'] == 'start'))
		 titleTextAlreadyPrinted = false;

	if (questions[questionFrom]['matrix'] != 'none') {
		while ((questionFrom > 1) && (questions[questionFrom]['matrix'] != 'start')) 
			questionFrom--;
		matrixFirstQuestionNr = questionFrom;
	}
}

function getQuestions(questionFrom) {
// Display questions from specified number until and including the question with the 
// break indicator turned on.
// If analysisShowing is true, each question is checked on whether analysis data is available.
	var html = '';
	var htmlBgColor;
        titleTextAlreadyPrinted = false;
        
	html += '	<form name="questionForm">\n';
	html += '<table border="0" cellpadding="5" cellspacing="0" width="600">\n';

	do {
		checkStartMatrix(questionFrom);

		if (!analysisShowing || questionAnalysisAvailable(questions[questionFrom])) {

			html += getQuestion(questionFrom++);

			// Print eventually footnote after matrix.
//			alert('before check: '+ questionFrom + '|'+questions[questionFrom-1]['matrix']+'|'+questions[questionFrom]['break']+'|'+questions[questionFrom]['matrix']);
			if ((questionFrom > 1) && (questions[questionFrom-1]['matrix'] != 'none') &&
				((questionFrom > numberOfQuestions()) ||
					(questions[questionFrom]['break']) ||
					(questions[questionFrom]['matrix'] == 'none') ||
					(questions[questionFrom]['matrix'] == 'start'))) {
				if (!analysisShowing && ((questions[matrixFirstQuestionNr]['footnote']['text'] != "") || !layout['process'])) {   
	   				if (layout['footnote']['bgColor'] == "")
						htmlBgColor = ''
					else
						htmlBgColor = 'bgColor="' + layout['footnote']['bgColor'] + '"';
					html += '<tr><td colspan="2" ' + htmlBgColor + ' >' + questions[matrixFirstQuestionNr]['footnote']['text'];
					if (!layout['process'] && !analysisShowing)
						html+= layout['footnote']['font'] + '<p align="right">Enquête is ten einde. Antwoorden worden niet verwerkt.';
					html += '</td></tr>\n';
				}
			}

			if ((questionFrom <= numberOfQuestions()) &&
				(!questions[questionFrom]['break']) &&
				(questions[questionFrom]['matrix'] != 'next')) {
				html += '<tr><td>&nbsp;</td></tr>\n';

			}
		}
		else
			questionFrom++;
	} while ((questionFrom <= numberOfQuestions()) && (!questions[questionFrom]['break']));
		// Stop if question has a break and next question should start on a new tab/page.

	html += '</table>\n';
	html += '	</form>\n';
	return html;
}

function getQuestion(i) {
	var html = '';
	var question = questions[i];
	var htmlBgColor = '';
	var matrix = (!analysisShowing && (question['matrix'] != 'none'));
	var titlePrinted = false;


      // Matrix
	if ((question['matrix'] == 'start') || ((question['matrix'] == 'next') && (question['break'] || !titleTextAlreadyPrinted))) {
		// Start or continue matrix
		// Display or repeat title + introduction first
	 	// Title of matrix
     		if ((questions[matrixFirstQuestionNr]['title']['text'] != "") || (question['title']['index'] != "")) {
			if (layout['title']['bgColor'] == "")
				htmlBgColor = ''
			else
				htmlBgColor = 'bgColor="' + layout['title']['bgColor'] + '"';  
			if (!analysisShowing) {
				html += '<tr><td width=100% ';
                                if (question['title']['index'] == "")
                                  html += 'colspan="2" ';
                                html += htmlBgColor + ' align="left">' +
					questions[matrixFirstQuestionNr]['title']['text'];
				if (question['title']['index'] != "")
					html += '</td><td width=100 colspan="1" ' + htmlBgColor + ' align="right" valign="top">' +
						question['title']['index'];
			} else {
				html += '<tr><td colspan="2" ' + htmlBgColor + ' align="left" width="100%">' +
					questions[matrixFirstQuestionNr]['title']['text'];
			}
			titleTextAlreadyPrinted = true;
			titlePrinted = true;
			html += '</td></tr>\n';
		}

		if ((question['matrix'] == 'start') || ((question['matrix'] == 'next') && question['break'])) {
	 		// Introduction of matrix
			if ((questions[matrixFirstQuestionNr]['intro']['text'] != "") && !analysisShowing) {
  				if (layout['intro']['bgColor'] == "")
					htmlBgColor = ''
				else
					htmlBgColor = 'bgColor="' + layout['intro']['bgColor'] + '"';  
				html += '<tr><td colspan="2" ' + htmlBgColor + ' align="left" width="100%">' +
					questions[matrixFirstQuestionNr]['intro']['text'];
				html += '</td></tr>\n';
			}

			if (!analysisShowing) {
				// (re)start matrix
			 	if (layout['answer']['bgColor'] == "")
					htmlBgColor = ''
				else
					htmlBgColor = 'bgColor="' + layout['answer']['bgColor'] + '"';  
				html += '<tr><td colspan="2" ' + htmlBgColor + ' align="left" width="100%"><table border="0" cellpadding="5" cellspacing="0" width="100%">\n';
				if (questions[matrixFirstQuestionNr]['answer']['matrix'] != "") {			
					// Display header (options)
					html += '<tr ' + htmlBgColor + ' >' + questions[matrixFirstQuestionNr]['answer']['matrix'];
					html += '</tr>\n';
				}
			}
		}

	}

	if (matrix) {
   		if (layout['answer']['bgColor'] == "")
			htmlBgColor = ''
		else
			htmlBgColor = 'bgColor="' + layout['answer']['bgColor'] + '"';  
		html += '<tr ' + htmlBgColor + '>';
	}
	
	// Title
      if ((!matrix && !titlePrinted) && ((question['title']['text'] != "") || (question['title']['index'] != ""))) {
     		if (layout['title']['bgColor'] == "")
			htmlBgColor = ''
		else
			htmlBgColor = 'bgColor="' + layout['title']['bgColor'] + '"'; 
		html += '<tr>';
		html += '<td width=100% valign="top" ' + htmlBgColor + ' >' + question['title']['text'];
		html += '</td>\n';
		if (!analysisShowing) {
			if (question['title']['index'] != "") {
				html += '<td width=100 ' + htmlBgColor + ' align="right" valign="top">' +
					question['title']['index'] + '</td>\n';
			} else {
				html += '<td width=100 ' + htmlBgColor + '></td>\n';
			}
		}
		html += '</tr>\n';
	}

	// Introduction
	if ((!matrix) && ((question['intro']['text'] != "") && !analysisShowing)) {
   		if (layout['intro']['bgColor'] == "")
			htmlBgColor = ''
		else
			htmlBgColor = 'bgColor="' + layout['intro']['bgColor'] + '"';  
		html += '<tr><td colspan="2" ' + htmlBgColor + ' align="left" width="100%">' +
				question['intro']['text'];
		html += '</td></tr>\n';
	}

	// Text (question phrase)  
   	if (layout['text']['bgColor'] == "")
		htmlBgColor = ''
	else
		htmlBgColor = 'bgColor="' + layout['text']['bgColor'] + '"';  
	if (!matrix)
		html += '<tr><td colspan="2" ' + htmlBgColor + ' >' + question['text']['text'] + '</td></tr>\n';
	else
		html += '<td valign="top" ' + htmlBgColor + ' >' + question['text']['text'] + '</td>\n';

	// Input (answer) or analysis data
	if (question['answer']['type'] != "info") {  
	   	if (layout['answer']['bgColor'] == "")
			htmlBgColor = ''
		else
			htmlBgColor = 'bgColor="' + layout['answer']['bgColor'] + '"';  

		if (!matrix)
			html += '<tr><td colspan="2" ' + htmlBgColor + ' >';

		if (!analysisShowing) 
			html += question['answer']['text']
		else
		if (question['analysis']['textual'] != "")
			html += question['analysis']['textual'] 
		else
			html += showAnalysis(question);
 
		if (!matrix)
			html += '</td></tr>\n';
		else
			html += '</tr>\n';

		if (matrix) {
            	var d = i;
			d++; // Strange construction needed to let explorer function properly (i+1 appends to the string i instead if i++
			if ((numberOfQuestions() == i) || (questions[d]['break']) || (questions[d]['matrix'] != 'next')) {
				html += '</table></td></tr>';
			}
		}
	}
 
	// Footnote
	if (!matrix) {
		if ((question['footnote']['text'] != "") || (!layout['process'])) {   
	   		if (layout['footnote']['bgColor'] == "")
				htmlBgColor = ''
			else
				htmlBgColor = 'bgColor="' + layout['footnote']['bgColor'] + '"';
			html += '<tr><td colspan="2" ' + htmlBgColor + ' >' + question['footnote']['text'];
			if (!layout['process'] && !analysisShowing)
				html+= layout['footnote']['font'] + '<p align="right">Enquête is ten einde. Antwoorden worden niet verwerkt.';
 			html += '</td></tr>\n';
		}
	} 

	// Next question flow
	if (question['footnote']['flow'] && (!analysisShowing)) {
		if (layout['footnote']['bgColor'] == "")
			htmlBgColor = ''
		else
			htmlBgColor = 'bgColor="' + layout['footnote']['bgColor'] + '"';
		var nextFound = false;

		// (1) check jumps in answers 
		if ((question['answer']['type'].indexOf("radio") > -1) || (question['answer']['type'].indexOf("checkbox") > -1)) { 
	      	for (var i=1; i <question['answer']['next'].length; i++) {
				if (question['answer']['next'][i] > 0) {
					if (!nextFound) {
						html += '<tr><td colspan="2" ' + htmlBgColor + '>' + layout['footnote']['font'];
						nextFound = true;
					}
					else
						html += '<br>'; 
					html += question['answer']['category'][i] + ': ga door naar ';
					if (question['answer']['next'][i] <= numberOfQuestions()) 
						html += (questions[question['answer']['next'][i]]['title']['name'] == "") ? 
								'vraag ' + question['answer']['next'][i] :
								questions[question['answer']['next'][i]]['title']['name'].toLowerCase()
					else
						html += 'vraag ' + question['answer']['next'][i];
				}
			}
		}

		// (2) check on conditional jumps
		for (var i = 0; i <question['nextcond'].length; i++) {
			if (!nextFound) {
				html += '<tr><td colspan="2" ' + htmlBgColor + '>' + layout['footnote']['font'];
				nextFound = true;
			}
			else
				html += '<br>';
			html += predToStr(question['nextcond'][i]['pred']) + ' ' + 
  				question['nextcond'][i]['value'] + ': ga door naar '; 
			if (question['nextcond'][i]['next'] <= numberOfQuestions()) 
				html += (questions[question['nextcond'][i]['next']]['title']['name'] == "") ? 
					'vraag ' + question['nextcond'][i]['next'] :
					questions[question['nextcond'][i]['next']]['title']['name'].toLowerCase()
			else
				html += 'vraag ' + question['nextcond'][i]['next'];
		}

		// (3) check default jump
		if (question['next'] > 0) {
			if (!nextFound) {
				html += '<tr><td colspan="2" ' + htmlBgColor + '>' + layout['footnote']['font'];
				nextFound = true;
				html += 'Ga door naar ';
			} else {
				html += '<br>'; 
				html += 'Anders: ga door naar '; 
			}
			if (question['next'] <= numberOfQuestions())
				html += (questions[question['next']]['title']['name'] == "") ? 
					'vraag ' + question['next'] :
					questions[question['next']]['title']['name'].toLowerCase()
			else
				'vraag ' + question['next'];
		}
		
		if (nextFound) 
			html += '</td></tr>';
	}
//	alert('getQuestion('+i+'): '+html);
	return html;
}

function predToStr(predicate) {
	switch (predicate) {
		case 0: return "="; break;
		case 1: return "<"; break;
		case 2: return "<="; break;
		case 3: return ">"; break;
		case 4: return ">="; break;
		case 5: return "<>"; break;
	}
}


function showAnalysis(question) {
	var html = '';
	var htmlBgColor = '';

	// Frequency table
	if (question['analysis']['frequency']['publish']) {
		html += showFrequency(question);
	}

	// Frequency chart
	if (question['analysis']['graph']['publish']) {
		html += showGraph(question);
	}

	// Medium figure
	if (question['analysis']['medium'] != "") {
		html += layout['answer']['font'] + question['analysis']['medium'];
	}
	// Response figures
	if (question['analysis']['response'] != "")
		html += '<br><align="left">' + layout['answer']['font'] + question['analysis']['response'];

	// Conclusion  
	if (question['conclusion']['text'] != "") {
   	  if (layout['conclusion']['bgColor'] == "")
		htmlBgColor = ''
	  else
		htmlBgColor = 'bgColor="' + layout['conclusion']['bgColor'] + '"';  
	  html += '</td></tr><tr><td colspan="2" ' + htmlBgColor + ' >' + question['conclusion']['text'];
	}
	return html;
}


function showFrequency(question) {
	var html = '';
	var htmlBgColor = '';
	var answer;

  	// Frequency table
   	if (layout['answer']['bgColor'] == "")
		htmlBgColor = ''
	else
		htmlBgColor = 'bgColor="' + layout['answer']['bgColor'] + '"';
	htmlFont = layout['answer']['font'];
	html += '<table width="100%" cellspacing="5">\n'  
//	html += '<tr><td colspan="2" ' + htmlBgColor + ' ><table cellspacing="5">\n';
	html += '<tr>\n';
	html += '<td ' + htmlBgColor + ' >' + htmlFont + '<u>#</td>\n';
	html += '<td ' + htmlBgColor + ' >' + htmlFont + '<u>Antwoord</td>\n';
	html += '<td ' + htmlBgColor + ' >' + htmlFont + '<u>Aantal</td>\n';
	html += '<td ' + htmlBgColor + ' >' + htmlFont + '<u>Percentage</td>\n';
	html += '<td ' + htmlBgColor + ' >' + htmlFont + '<u>Valide percentage</td>\n';
	html += '<td ' + htmlBgColor + ' >' + htmlFont + '<u>Cumulatief</td></tr>\n';

	var cummulate = 0;	 
	for (var j=1; j<question['analysis']['frequency']['answer'].length; j++) {
		answer = question['analysis']['frequency']['answer'][j];
		html += '<tr>\n';
	  	html += '<td align="right" ' + htmlBgColor + ' >' + htmlFont + j + '</td>\n';
	  	html += '<td align="left" ' + htmlBgColor + ' >' + 
				htmlFont + answer['category'] + '</td>\n';
	  	html += '<td align="right" ' + htmlBgColor + ' >' + 
				htmlFont + answer['hits'] + '</td>\n';
	  	html += '<td align="right" ' + htmlBgColor + ' >' + 
				htmlFont + decimalAlign(answer['percentage']) + '%</td>\n';
	  	html += '<td align="right" ' + htmlBgColor + ' >' + 
				htmlFont + decimalAlign(answer['valid_percentage']) + '%</td>\n';
	  	html += '<td align="right" ' + htmlBgColor + ' >' + 
				htmlFont + decimalAlign(answer['cummulate']) + '%</td></tr>\n';
	}

	html += '</table>\n';

	return html;
}

function decimalAlign(number) {
	if (number.indexOf(',') == -1)
		return number + "&nbsp;&nbsp;&nbsp;";
	return number;
}


function showGraph(question) {
	var html = '';

  	// Frequency chart
	html += '<img src="' + question['analysis']['graph']['file'] + '" border="0">\n';

	return html;
}


function updateSection(section, html) {
	var elements;
	var win = getWindow(section);		
	if (isIE4) {
		win.innerHTML = html;
	}
	else if (isNS4) {
		win.document.open('text/html');
		win.document.write(html);
		win.document.close();
	} else {
		win.innerHTML = html;
	}
	elements = getElements();
	if (elements.length > 0) {
		elements[0].focus();
		elements[0].select();
	}
}


function hideSection(section) {
	var win = getWindow(section);		
	if (isIE4) {
		win.style.visibility = "hidden";
	} else if (isNS4) {
		win.visibility = "hide";
//	} else {
//		win.style.visibility = "hidden";
	}
}

function showSection(section) {
	var win = getWindow(section);		
	if (isIE4) {
		win.style.visibility = "visible";
	} else if (isNS4) {
		win.visibility = "show";
//	} else {
//		win.style.visibility = "visible";
	}
}


function getSubmitFields() {
	var html = '';
	var question;
	var i;
	var j;

	// Personalia
	for (i=1;i<=personName.length-1;i++) { 
		html += '<input type="hidden" name="' + personName[i] + '" value="' + personValue[i] + '">\n';
	}

	// Questions
	for (i=1;i<=numberOfQuestions();i++) {
		question = questions[i];
		if (question['answer']['type'] != "info") {
			html += '<input type="hidden" name="' + question['answer']['name'] + '">\n';
			if (question['answer']['type'].indexOf("other") > -1) {
				if (question['answer']['type'].indexOf("radio") > -1) {
					html += '<input type="hidden" name="' + question['answer']['name'] + '_F1"> \n';
				} else {
					for (j=1; j<=question['answer']['others']; j++)
						html += '<input type="hidden" name="' + 
							question['answer']['name'] + '_F' + j + '">\n';
		 		}
			}
		}
	}
	return html;
}

function updateSubmitFields() {

//	alert('updateSubmitFields()');
	if (layout['switch']) {
		// Survey is in analysis phase, no response allowed, therefore do not update.
		alert('U heeft alle vragen met analyse bekeken.');
		return false;
	}

	if (numberOfQuestions() > 0)	
		if (!saveQuestions()) {
			return false;
		}

	var i;
	var j;

	// Personalia
	for (i=1;i<=personName.length-1;i++) {
		document.forms['mainForm'].elements[getElementIdx(personName[i])].value = personValue[i];
	}
		
	// Questions
	for (i=1;i<=numberOfQuestions();i++) {
		if (questions[i]['answer']['type'] != "info") {
			document.forms['mainForm'].elements[getElementIdx(questions[i]['answer']['name'])].value
				= questions[i]['answer']['value'];
			if (questions[i]['answer']['type'].indexOf("other") > -1) {
				if (questions[i]['answer']['type'].indexOf("radio") > -1) 
					document.forms['mainForm'].elements[getElementIdx(questions[i]['answer']['name']+'_F1')].value
						= questions[i]['answer']['field'][1]
				else
					for (j=1; j<=questions[i]['answer']['others']; j++) {
						document.forms['mainForm'].elements[getElementIdx(questions[i]['answer']['name']+'_F'+j)].value
							= questions[i]['answer']['field'][j];
				}
			}
		}
	}

      if (testSurvey) {
		alert('Dit was een test, de gegevens worden niet verstuurd.');
		return false;
	}

	if (!layout['process']) {
		alert('De enquête is ten einde. Uw respons wordt niet meer verwerkt.');
		return false;
	}

    	if ((location.search == "") && blockOnNoLink) {
		alert('Dit is een private enquête die alleen kan worden ingevuld door op de code-link in een ontvangen email te klikken. Uw antwoorden zullen niet worden verwerkt!');
		return false;
	}

	return true;	
	
}

function getElementIdx(fieldName) {

	if (isIE4)
		return fieldName;

	var elements = document.forms['mainForm'].elements;
	
	for (var i=0; i<elements.length; i++) {
		if (elements[i].name == fieldName)
			return i;
	}
	alert('error: can not find index of ' + fieldName);
}	

function saveQuestions() {
  	var elements = getElements();
	var attributes;

	// Initialize
	for (var i=0; i<elements.length; i++) {
	  	attributes = getAttr(elements[i].name);
		if (attributes.field == "")
			questions[attributes.number]['answer']['value'] = ""
		else
			questions[attributes.number]['answer']['field'][attributes.field] = ""; 
	}

	// Assign
	for (var i=0; i<elements.length; i++) {
	  	attributes = getAttr(elements[i].name);

//		alert('saveQuestions(): element name = ' + elements[i].name + ', attributes.number = ' + attributes.number);

		if (skipQuestion(attributes.number)) 
			continue;	

		if (!checkAnswer(attributes.number)) {
			return false;
		}

		if (questions[attributes.number]['answer']['type'].indexOf("radio") > -1) {
			// Handle question with radio options as answers
			if (elements[i].checked) {
				// register the selected option
				questions[attributes.number]['answer']['value'] = elements[i].value;
			}
		}

		if (questions[attributes.number]['answer']['type'].indexOf("checkbox") > -1) {
			// Handle question with checkbox options as answers
			if (elements[i].checked) {
				// register the selected option
				questions[attributes.number]['answer']['value'] += elements[i].value + ' ';
			}
		}

		if (questions[attributes.number]['answer']['type'].indexOf("other") > -1) {
			if (attributes.field != "") {
				//Handle alternative option
				questions[attributes.number]['answer']['field'][attributes.field] =
 					elements[i].value;
			}
		}

		if (questions[attributes.number]['answer']['type'].indexOf("text") > -1) { 
			questions[attributes.number]['answer']['value'] = elements[i].value + '\n!END';
		}

		if ( (questions[attributes.number]['answer']['type'].indexOf("number") > -1) || 
		     (questions[attributes.number]['answer']['type'].indexOf("decimal") > -1) ||
		     (questions[attributes.number]['answer']['type'].indexOf("date") > -1) ) {
			questions[attributes.number]['answer']['value'] = elements[i].value.replace(/\./g, '');
			questions[attributes.number]['answer']['value'] = 
  				questions[attributes.number]['answer']['value'].replace(/\,/g, '.');
		}


	}
	return true;	
}	

function questionText(question) {

	if ((question['matrix'] != 'none') || (question['title']['name'] == '')) {
		if (question['text']['pure'].length <40)
			return question['text']['pure'];
		return question['text']['pure'].substr(0, 39) + '...';
	}

	return question['title']['name'];
}
	

function checkAnswer(questionNr) {
	var question = questions[questionNr];
	var type = question['answer']['type'];
	var fieldName = question['answer']['name'];
	var field;
      var number;

	if (type.indexOf("radio") > -1) {
		if (question['required'] && (!radioChecked(question))) {
			alert(questionText(question) + ': selecteer een antwoord.');
			return false;
		}
	}

	if (type.indexOf("checkbox") > -1) {
		if ((question['required'] || anyCheckboxChecked(question)) && (!checkboxChecked(question))) {
			alert(questionText(question) + 
				': selecteer minimaal ' + question['answer']['min'] + 
				((question['answer']['max'] == 0) ? '' : (' en maximaal ' + question['answer']['max'])) + ' ' +
				(((question['answer']['min'] > 1) || (question['answer']['max'] > 1)) ? 'antwoorden' : 'antwoord') + '.');
			return false;
		}
	}

	if ((type.indexOf("number") > -1) || 
	    (type.indexOf("decimal") > -1)) {
		field = getElement(fieldName);
		if (question['required'] && isEmpty(field)) {
			alert(questionText(question) + ': vul een waarde in.');
			field.focus();
			return false;
		}

		if (isEmpty(field))
			// question is not required
			return true;

		if (type.indexOf("number") > -1) {
			if (!checkNumber(field, questionText(question), 
				(question['answer']['min'] != ""), 
				question['answer']['min'], 
				(question['answer']['max'] != ""), 
				question['answer']['max'])) {
				field.focus();
				return false;
			}
		} else {
			if (!checkDecimal(field, questionText(question),
				(question['answer']['min'] != ""), 
				question['answer']['min'], 
				(question['answer']['max'] != ""), 
				question['answer']['max'])) {
				field.focus();
				return false;
			}
		} 

	
	}

	if (type.indexOf("date") > -1) {
		field = getElement(fieldName);
		if (question['required'] && isEmpty(field)) {
			alert(questionText(question) + ': vul een datum in.');
			field.focus();
			return false;
		}

		if (isEmpty(field))
			// question is not required
			return true;

		if (!checkDate(field)) {
			field.focus();
			return false;
		}
	}

	if (type.indexOf("text") > -1) {
		field = getElement(fieldName);
		if ((!question['required'] || (!isEmpty(field))) && !checkText(field, questionText(question), 
			(question['answer']['min'] != ""), 
			question['answer']['min'], 
			(question['answer']['max'] != ""), 
			question['answer']['max'])) {
			field.focus();
			return false;
		}
	}

	return true;
}  



function radioChecked(question) {
	var fieldName = question['answer']['name'];
	var elements = getElements();
	for (var i=0;i<elements.length;i++)
		if ((elements[i].name == fieldName) && elements[i].checked)
			return true;
	return false;
}


function checkboxChecked(question) {
	var nr = 0;
	var fieldName = question['answer']['name'];
	var elements = getElements();
	for (var i=0;i<elements.length;i++)
		if ((elements[i].name == fieldName) && elements[i].checked)
			nr++;
	return ((nr >= question['answer']['min']) && ((question['answer']['max'] == 0) || (nr <= question['answer']['max'])));
	// zero means unlimited
}

function anyCheckboxChecked(question) {
	var nr = 0;
	var fieldName = question['answer']['name'];
	var elements = getElements();
	for (var i=0;i<elements.length;i++)
		if ((elements[i].name == fieldName) && elements[i].checked)
			return true;
	return false;
}


function skipQuestion(questionNr) {
	var question;
	var type;
	var iQuestion = 1;
	var iAnswer;
	var answers = [];

	while (iQuestion <questionNr) {
		
		question = questions[iQuestion];
		type = question['answer']['type'];

		if (question['answer']['value'] == "") {
			// Some question types may be empty, so a jump can be made without filling in the answer.
			// Check this case for text and checkbox, where the minimum attribute is zero.
			if ((type.indexOf("text") > -1) || (type.indexOf("checkbox") > -1) || (type.indexOf("info") > -1)) {
				if (((type.indexOf("info") > -1) || (question['answer']['min'] == 0)) && (question['next'] > 0)) {
					iQuestion = question['next']; // Jump to next question and check (in next loop)
				} else
					iQuestion++;
			} else
				if (!question['required'])
					iQuestion++;
		} else {
			if (type.indexOf("radio") > -1) {
				// Check if the selected answer specifies a jump.
				if (question['answer']['next'][question['answer']['value']] > 0) {
					// Jump to next question and check (in next loop)
					iQuestion = question['answer']['next'][question['answer']['value']]; 
				} else if (question['next'] > 0) {
					// Default jump
					iQuestion = question['next'];
				} else {
					iQuestion++;
				}
			} else if (type.indexOf("checkbox") > -1) {
				// Check if a selected answer specifies a jump.
				answers = question['answer']['value'].split(" ");
				for (j=0; j<answers.length; j++) {
					if (question['answer']['next'][answers[j]] > 0) {
						iQuestion = question['answer']['next'][answers[j]];
						break;
					}
				}
				if (j == answers.length) {
					// No selected answer specifies a jump. Check question default jump.
					if (question['next'] > 0) { 	
						// Default jump
						iQuestion = question['next'];
		 			} else {
						iQuestion++;
					}
				}
			} else if (question['nextcond'].length > 0) {
				// Check if selected answer satisfies a jump condition
				for (j=0; j<question['nextcond'].length; j++) {
					if (checkCondition(question['nextcond'][j], question['answer']['value'])) {
						iQuestion = question['nextcond'][j]['next'];
						break;
					}
				}
				if (j == question['nextcond'].length) {
					// No condition evaluates to a jump. Check question default jump.
					if (question['next'] > 0) { 	
						// Default jump
						iQuestion = question['next'];
		 			} else {
						iQuestion++;
					}
				}
			} else {
				// Type is date, number, decimal or text, check jump on default
				if (question['next'] > 0) {
					// Default jump
					iQuestion = question['next'];
				} else {
					iQuestion++;
				}
			}
		}
	}

	return (iQuestion > questionNr);				
}  


function checkCondition(condition, value) {

	switch (condition['pred']) {
		case 0: return (value = condition['value']);
		case 1: return (value <condition['value']);
		case 2: return (value <= condition['value']);
		case 3: return (value > condition['value']);
		case 4: return (value >= condition['value']);
		case 5: return (value != condition['value']);
	}
	return false;
}

 
function skipAnyQuestions(questionNr) {

	if (!layout['switch']) {
		
		while (skipQuestion(questionNr)) {
			checkStartMatrix(questionNr);
			questions[questionNr]['answer']['value'] = "";
			questionNr++;
		}
	}
	return questionNr;
		
}  



function getAttr (code) {
	// Code has the following (example) format: SRPQQ_RESP_CopyM1_S2_U0_Q2[_F1]
	var attr = [];
	var underscoreNr = 0;
	var buffer = '';
	var charPos = 0;
	for (var i=0; i <code.length; i++) {
		if (code.charAt(i) == "_") {
			underscoreNr++;
			switch (underscoreNr) {
				case 1: attr['appl'] = buffer; break;
				case 2: attr['form'] = buffer; break;
				case 3: attr['master'] = buffer; break;
				case 4: attr['survey'] = parseInt(buffer.substr(1, buffer.length-1)); break;
				case 5: attr['update'] = parseInt(buffer.substr(1, buffer.length-1)); break;
				case 6: attr['question'] = parseInt(buffer.substr(1, buffer.length-1));
					  charPos = i;
					  break;
			}
			buffer = '';
		}
		else
			buffer = buffer + code.charAt(i);
	}
      if (underscoreNr == 5) { 
		attr['question'] = parseInt(buffer.substr(1, buffer.length-1))
		charPos = code.length;
		attr['field'] = "";
	} else
		attr['field'] = parseInt(buffer.substr(1, buffer.length-1));

      for (var j=1; j <= numberOfQuestions(); j++) {
		if (questions[j]['answer']['name'] == code.substr(0, charPos)) {
			attr['number'] = j;
			break;
		}
	}

//	alert(attr.number);
	return attr;
}

function focusCheckedField(option) {
	var field = getElement(option.name + '_' + option.value);
	field.focus();
}	


function checkOptionField(field) {
	var elements = getElements();

	for (var i=0;i<=elements.length;i++) {
		if (((elements[i].type == "radio") || (elements[i].type == "checkbox")) 
			&& ((elements[i].name + '_' + elements[i].value) == field.name)) {
			elements[i].checked = (!isEmpty(field));
			break;
		}
	}
}

function optionFieldOn(field) {
	var elements = getElements();

	for (var i=0;i<=elements.length;i++) {
		if (((elements[i].type == "radio") || (elements[i].type == "checkbox")) 
			&& ((elements[i].name + '_' + elements[i].value) == field.name)) {
			elements[i].checked = true;
			break;
		}
	}
}

function getKeyCode(event) {
	var code;
 
	if (!event) 
		var event = window.event; 
	if (event.keyCode) 
		code = event.keyCode; 
	else 
	if (event.which) 
		code = event.which; 
	
	return code;
}

function keyPressedHandler(event) {
	var keyCode = getKeyCode(event);
	switch(keyCode) {
		case 13: {
			// Enter pressed.
			if (!keyPressHandled) {
				keyPressHandled = true;
				eval(nextPage);
				keyPressHandled = false;
			}
			if (IE) {
				event.returnValue = false;
				event.cancelBubble = true;
			} else {
				if (event.stopPropagation) {
					event.stopPropagation();
					return false;
				}
			}
		}

	}
	return true;
}			
			
// The Central Randomizer 1.3 (C) 1997 by Paul Houle (houle@msc.cornell.edu) 
// See: http://www.msc.cornell.edu/~houle/javascript/randomizer.html 
rnd.today=new Date(); 
rnd.seed=rnd.today.getTime(); 
function rnd() { 
 	rnd.seed = (rnd.seed*9301+49297) % 233280; 
	return rnd.seed/(233280.0); 
}; 

function rand(number) { 
	return Math.ceil(rnd()*number); 
}; 


function randomize() {
// For each question with options, these will be shown in a random order
// if set by user.

	var answer;
	var html;
	var stillToInsert;
	var i;
	var j;
	var k;
	var l;

	for(i=1; i <= numberOfQuestions(); i++) {
		answer = questions[i]['answer'];
		if ((answer['type'].indexOf("radio") > -1) || (answer['type'].indexOf("checkbox") > -1)) {
			if (answer['random']) {
				html = '<table border=0 cellspacing=0 cellpadding=2>'; 
				stillToInsert = new Array(answer['option'].length);
				for(j=1; j <answer['option'].length; j++) 
					stillToInsert[j-1] = answer['option'][j];
				for(j=1; j <answer['option'].length; j++) {
					if (((j - 1) % answer['columns']) == 0) 
            				html += '<tr>';
					k = rand(stillToInsert.length-1);
           //                             alert('j='+j+' k='+k+': '+stillToInsert[k-1]);
					html += stillToInsert[k-1];

					// Delete entry
					for(l=k-1; l<stillToInsert.length; l++)
						stillToInsert[l] = stillToInsert[l+1];
					stillToInsert.length--;
					if ((j % answer['columns']) == 0) 
            				html += '</tr>';
				}
				html += answer['semi'];
				html += '</table>';
				answer['text'] = html;
           //                     alert(html);
			}
		}
	}
}
					
function processEmail() {

	alert(layout['process']);

	return layout['process'];
}					
				
				 


// end form logic -->


