// Define global variables and arrays
var questionIndex	= 0;
var checker 		= true;
var keeper 		= new Array();
var performance 	= new Array('geen beste uitslag. Dat kun je vast veel beter.','onvoldoende.', 'een mager vijfje', 'voldoende.','goed','perfect! Jij bent een echte ijsbeerkenner.');
var correctAnswers 	= 0;
var jsAbility;
var preventer = 1;
var spawn;

// For resetting all variables and arrays for a retake
function itemReset() {
	questionIndex 	= 0;
	keeper 		= new Array();
	correctAnswers 	= 0;
	preventer	= 1;
	jsAbility	= null;
	}

// Administer the test and record the answers
function startQuiz(openStatus, whetherOrNot) {
	checker = openStatus;
	if (whetherOrNot < questionIndex) { busted(); return; }
	if (checker) { 
		spawn = open('','','width=350,height=300,left=280,top=240,scrollbars=yes'); 
		!checker;
		}

	if (questionIndex == killers.length) { gradeTest(); return; }
	
	// Separate each array element to form the questions and answr options
	var question 	= killers[questionIndex].substring(0, killers[questionIndex].indexOf('$$'));
	var optionA 	= killers[questionIndex].substring(killers[questionIndex].indexOf('$$') + 2, killers[questionIndex].indexOf('%%'));
	var optionB 	= killers[questionIndex].substring(killers[questionIndex].indexOf('%%') + 2, killers[questionIndex].indexOf('^^'));
	var optionC 	= killers[questionIndex].substring(killers[questionIndex].indexOf('^^') + 2, killers[questionIndex].indexOf('&&'));
	var optionD 	= killers[questionIndex].substring(killers[questionIndex].indexOf('&&') + 2, killers[questionIndex].length);

	// Write the questons and answers options to the new window
	spawn.document.clear();
	spawn.document.open();
	spawn.document.writeln('<HTML><HEAD><TITLE>De quiz</TITLE></HEAD>');
	spawn.document.writeln('<LINK rel=\"stylesheet\" href=\"\/ijs_info/\info_css\" type=\"text/css\">');
	spawn.document.writeln('<BODY bgcolor=\"#FFFFFF\" margin=\"30\">');
	spawn.document.writeln('<H1>Vraag ' + (questionIndex + 1) + '</H1>');
	spawn.document.writeln('<FORM>');
	spawn.document.writeln('<p><B>' + question + '</B><p>');
	spawn.document.writeln('<INPUT TYPE=CHECKBOX  NAME=\"answer\" VALUE=\"a\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionA + '<BR>');
	spawn.document.writeln('<INPUT TYPE=CHECKBOX NAME=\"answer\" VALUE=\"b\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionB + '<BR>');
	spawn.document.writeln('<INPUT TYPE=CHECKBOX NAME=\"answer\" VALUE=\"c\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionC + '<BR>');
	spawn.document.writeln('<INPUT TYPE=CHECKBOX NAME=\"answer\" VALUE=\"d\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionD + '<BR>');
	spawn.document.writeln('</FORM><img src=\"http://www.ijsbeer.info/gfx/overige/footer200.gif\" width=\"200\" height=\"36\"  border=\"0\"></BODY></HTML>');
	spawn.document.close();
	
	// Increment variables for the next question
	questionIndex++;
	preventer++;
	}

function gradeTest() {
	// Increment the variables for the last time
	questionIndex++;
	preventer++;
	
	// Compare student answers with correct answers
	for (var i = 0; i < killers.length; i++) {
		if (keeper[i] == answers[i]) {
			correctAnswers++;
			}
		}
	
	// Determine a ranking according to number of corrrect answers
	jsAbility = performance[Math.ceil((correctAnswers/killers.length) * performance.length - 1)];

	// Print the test results
	printResults();
	}

// Print the questions, answer options, and other info
function printResults() {
	spawn.document.clear();
	spawn.document.open();
	spawn.document.writeln('<HTML><HEAD>');
	spawn.document.writeln('<TITLE>IJsbeer-quiz uitslag</TITLE>');
	spawn.document.writeln('<LINK rel=\"stylesheet\" href=\"..\/..\/admin\/css\/info.css\" type=\"text/css\">');
	spawn.document.writeln('</HEAD><BODY BGCOLOR=\"WHITE\">');						
	spawn.document.writeln('<H1>Je had ' + correctAnswers + ' van de ' + killers.length + ' antwoorden goed.</H1>');
	spawn.document.writeln('<p><B>De IJsbeer vindt dat ' + jsAbility + '</B><BR>');
	spawn.document.writeln('<BR><BR><FONT SIZE=\"2\">Dit is de uitslag met de juiste antwoorden: </FONT><p>');

	for (var i = 0; i < killers.length; i++) {
		spawn.document.writeln('\n\n<B>Vraag ' + (i + 1) + '</B><BR>');
		spawn.document.writeln(killers[i].substring(0, killers[i].indexOf('$$')));
		spawn.document.writeln('<BR><BR>\n<FONT SIZE=\"-1\">a. ' + killers[i].substring((killers[i].indexOf('$$') + 2), killers[i].indexOf('%%')) + '<BR>');
		spawn.document.writeln('b. ' + killers[i].substring(killers[i].indexOf('%%') + 2, killers[i].indexOf('^^')) + '<BR>');
		spawn.document.writeln('c. ' + killers[i].substring(killers[i].indexOf('^^') + 2, killers[i].indexOf('&&')) + '<BR>');
		spawn.document.writeln('d. ' + killers[i].substring(killers[i].indexOf('&&') + 2, killers[i].length) + '<BR></FONT>');

			// Determine if the student answered each question appropriately, and display accordingly
			if (keeper[i] == answers[i]) {
			spawn.document.writeln('<B><I><FONT COLOR=\"GREEN\">Deze vraag had je goed. Het juiste antwoord is inderdaad  ' + answers[i] + '.\n</FONT></I></B>\n<BR><BR><BR>');
				}		
			else { spawn.document.writeln('<B><I><FONT COLOR=\"RED\">Het juiste antwoord is: ' + answers[i] + '\n<BR><BR><BR></FONT></I></B>'); }
		}

	// Add extra info about the ranking, questions, and application
	spawn.document.writeln('\n\n<H2>Je score</H2><p>Als je meer dan 9 vragen goed had, gefeliciteerd! Jij weet heel veel van ijsberen. Had je veel fouten, niet sippen. Deze quiz was niet zo makkelijk. ');
	spawn.document.writeln('Als je wilt, kun je de test nog <A HREF="javascript: opener.itemReset(); opener.startQuiz(false);" onMouseOver="window.status=\'\'; return true"><B>een keer doen</B></A>. ');

	spawn.document.writeln('\n\n<p><H2>Vragen?</H2><p>Enkele vragen waren best moeilijk, toch? Misschien ben je het met sommige antwoorden niet eens. Maar ja, een ding is zeker: ');
	spawn.document.writeln('het antwoord op elke vraag vind je terug op ijsbeer.info. <p>Als je opmerkingen, complimenten of (nieuwe) vragen hebt, laat het me dan weten. Wellicht zie je jouw vraag spoedig hier terug. ');

	//spawn.document.writeln('\n\n<BR><BR><BR>\n\n<BIG>A Word About The Application</BIG><BR><BR>This test is pure unadulterated JavaScript. From the administration to the grading to the results display, everything happens through JavaScript. For those of you who want to ');
	//spawn.document.writeln('<A HREF=\"download/masher.zip\" onMouseOver=\"window.status=\'\'; return true"><B>download</B></A> this application and modify it for a test of your own, you\'ll probably want a different method for grading. Although somewhat of an annoyance, it wouldn\'t be too hard to get the ');
	//spawn.document.writeln('answers to this test. You might want to send the results to a server for grading.\n<BR><BR><BR>');
	spawn.document.writeln('\n\n</BODY></HTML>');
	spawn.document.close();
	}

function busted() {
	alert('Sorry, je kunt per vraag maar één antwoord geven.');
	spawn.history.forward(); 
	spawn.focus();
	}