// Define global variables and arrays
var questionIndex	= 0;
var checker 		= true;
var keeper 		= new Array();
var performance 	= new Array('No offense, but you are really weak. More effort needed.','Few have done worse.','You are \"meteorology\"-impaired.','You need some real work.','You\'re better than some, but not too many.','You\'re better than some, but not many.','You\'ve done a good job!.','Very Good!','You are an exhalted number guru.');
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=640,height=350,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 answrs options to the new window
	spawn.document.clear();
	spawn.document.open();
	spawn.document.writeln('<HTML><HEAD><TITLE>How much do you know about numbers?</TITLE></HEAD>');
	spawn.document.writeln('<BODY BGCOLOR=#ffffee>');
	spawn.document.writeln('<FONT FACE=\"Arial\">');
	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></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>Results</TITLE>');
	spawn.document.writeln('</HEAD><BODY BGCOLOR=#ffffee>');						
	spawn.document.writeln('<FONT FACE=\"Arial\">');
	spawn.document.writeln('<H2>You scored ' + correctAnswers + '/' + killers.length + ' correctly.</H2>');
	spawn.document.writeln('<B>Ranking: ' + jsAbility + '</B><BR>');
	spawn.document.writeln('<BR><BR><FONT 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 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\">You answered this correctly.</FONT></I></B>\n<BR><BR><BR>');
				}		
			else { spawn.document.writeln('<B><I><FONT COLOR=\"RED\">The correct answer is: ' + answers[i] + '\n<BR><BR><BR></FONT></I></B>'); }
		}

	// Add extra info about the ranking, questions, and application
	spawn.document.writeln('\n\n<BIG>Your Ranking</BIG><BR><BR>If you came out on top, congratulations! You have a good grasp of numbers and their representation. If not, don\'t worry. Try harder next time! ');
	spawn.document.writeln('You can always <A HREF="javascript: opener.itemReset(); opener.startQuiz(false);" onMouseOver="window.status=\'\'; return true"><B>retake</B></A> it. ');

	spawn.document.writeln('\n\n<BR><BR><BR><BIG>Questions About Those Questions?</BIG><BR><BR>Some of those were pretty rough, huh? Some even downright unfair? That\'s open for debate, but one thing is ');
	spawn.document.writeln('certain: Each answer is documented somewhere in your book or addressed in the lessons. If you have a comment or question, do send me an e-mail or discuss with me in person.');
	
	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();
	}
