// Define global variables and arrays
var questionIndex	= 0;
var checker 		= true;
var keeper 		= new Array();
var performance 	= new Array('No offense, but it could be better.','You need some real work.','Please do more exercises.','You need more practise.','You are getting better!','Your performance is adequate.','Your work is paying off now.','You are getting better and better.','You are excellent. Well done!');
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=600,height=400,scrollbars=yes'); 
		!checker;
		}

	if (questionIndex == killers.length) { gradeTest(); return; }
	
	// Separate each array element to form the questions and answer 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>JavaScript Mathematic Test</title></head>');
	spawn.document.writeln('<body background=background.gif>');
	spawn.document.writeln('<font face=arial>');
	spawn.document.writeln("<p><center><input type=button value='Print this page' name=button3 a href=# onClick=window.print()></a></center></p>");
	spawn.document.writeln("<img src=images/train.gif width=100% height=25 border=0 align=center alt=divider><br />");
	spawn.document.writeln('<h2>Question ' + (questionIndex + 1) + '</h2>');
	spawn.document.writeln('<form>');
	spawn.document.writeln('<b>' + question + '</b><br /><br />');
	spawn.document.writeln('<input type=radio name=\"answer\" value=\"a\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionA + '<br />');
	spawn.document.writeln('<input type=radio name=\"answer\" value=\"b\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionB + '<br />');
	spawn.document.writeln('<input type=radio name=\"answer\" value=\"c\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionC + '<br />');
	spawn.document.writeln('<input type=radio name=\"answer\" value=\"d\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionD + '<br />');
	spawn.document.writeln('</form>');
	spawn.document.writeln('</font>');
	spawn.document.writeln('</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>JavaScript Mathematic Test Results</title>');
	spawn.document.writeln('</head><body background=background.gif>');	
	spawn.document.writeln('<table><tr><td valign=top align=center>');						
	spawn.document.writeln('<font face=arial>');
	spawn.document.writeln("<p><center><input type=button value='Print this page' name=button3 a href=# onClick=window.print()></a></center></p>");  	
	spawn.document.writeln("<img src=images/train.gif width=100% height=25 border=0 align=center alt=divider><br />");
	spawn.document.writeln('<h2>You scored ' + correctAnswers + '/' + killers.length + ' correctly.</h2>');
	spawn.document.writeln('<b>Ranking: ' + jsAbility + '</b><br />');
	spawn.document.writeln('</font>');
	spawn.document.writeln('<br /><br /><font face=arial size=\"4\">Here is how you scored: </font><br /><br />');

	for (var i = 0; i < killers.length; i++) {
		spawn.document.writeln('\n\n<b>Question ' + (i + 1) + '</b><br />');
		spawn.document.writeln(killers[i].substring(0, killers[i].indexOf('$$')));
		spawn.document.writeln('<br /><br />\n<font face=arial>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) + '</font><br />');

			// Determine if the student answered each question appropriately, and display accordingly
			if (keeper[i] == answers[i]) {
			spawn.document.writeln('<b><font face=arial color=\"green\">You answered this correctly.</font></b>\n<br /><br /><br />');
				}		
			else { spawn.document.writeln('<b><font face=arial color=\"red\">The correct answer is: ' + answers[i] + '\n</font></b><br /><br /><br />'); }
		}

	// Add extra info about the ranking, questions, and application
	spawn.document.writeln('<font face=arial>');
	spawn.document.writeln('\n\n<big>Your Ranking</big><br /><br />If you came out on top, congratulations! You have a good grasp of Mathematics. If not, don\'t worry. ');
	spawn.document.writeln('This test is a bit difficult. If it was not hard, doing well would not mean that much. You can always <a href="javascript: opener.itemReset(); opener.startQuiz(false);" onMouseOver="window.status=\'\'; return true"><b>retake</b></a> it. ');
	spawn.document.writeln('</font>');
	spawn.document.writeln('</td></tr></table>');
	spawn.document.writeln('\n\n</body></html>');
	spawn.document.close();
	}

function busted() {
	alert('Sorry, you only get one shot at each question.');
	spawn.history.forward(); 
	spawn.focus();
	}