
// Define global variables and arrays

var qIndex			= 0;
var checker 		= true;
var keeper 			= new Array();
var correctAnswers 	= 0;
var preventer 		= 1;
var SkillLevel;
var spawn;
var timerID;
var timerRunning	= false;
var timeLimit		= 10;
var xC 				= xC;
var yC 				= 1;

// Initialize all variables and arrays for a replay

function itemReset() 
{
	qIndex 	= 0;
	keeper 			= new Array();
	correctAnswers 	= 0;
	preventer		= 1;
	SkillLevel		= null;
}

// Administer the quiz and record the answers

function startQuiz(openStatus, validTest) 
{
	checker = openStatus;
	if (validTest < qIndex) 
		{ busted(); return; }
	if (checker) 
	{ 
		spawn = open('','quiz','top=0,left=0,width='+wWidth+',height='+wHeight+',scrollbars=yes'); 
		!checker;
	}

	if (qIndex == questions.length) 
		{ gradeTest(); return; }
	
// Separate each array element to form the questions and answer options

	var question 	= questions[qIndex].substring(0, questions[qIndex].indexOf('AAA'));
	var optionA 	= questions[qIndex].substring(questions[qIndex].indexOf(' AAA ') + 5, questions[qIndex].indexOf(' BBB '));
	var optionB 	= questions[qIndex].substring(questions[qIndex].indexOf(' BBB ') + 5, questions[qIndex].indexOf(' CCC '));
	var optionC 	= questions[qIndex].substring(questions[qIndex].indexOf(' CCC ') + 5, questions[qIndex].indexOf(' DDD '));
	if (numOpt == 4) 
	{
		var optionD = questions[qIndex].substring(questions[qIndex].indexOf(' DDD ') + 5, questions[qIndex].length);
	}
	
// Write the questons and answers options to the new window

	spawn.document.clear();
	spawn.document.open();
	spawn.document.writeln('<html><head><title>' + qTitle + '</title>');
	spawn.document.writeln('<style type=\"text/css\">  ');      
	spawn.document.writeln('BODY  {');
	spawn.document.writeln('	font-family : Verdana,Arial,Geneva; ');
	spawn.document.writeln('	background-color : #ffffcc;');
	spawn.document.writeln('	color : #000033;}');
	spawn.document.writeln('DIV.CTime  {');
	spawn.document.writeln('	background-color : #ffffcc;');
	spawn.document.writeln('	text-align : center;');
	spawn.document.writeln('	color : Red;');
	spawn.document.writeln('	position : relative;');
	spawn.document.writeln('	font-size : x-large;');
	spawn.document.writeln('	font-family : Verdana,Arial,Geneva;');
	spawn.document.writeln('	left : 0;');
	spawn.document.writeln('	top : 10;');
	spawn.document.writeln('	width : 20;}');
	spawn.document.writeln('</style>');
	spawn.document.writeln('<SCRIPT LANGUAGE=\"Javascript\"><!--');
	spawn.document.writeln('if (window.focus) {self.focus();}');
	spawn.document.writeln('//--></SCRIPT>');
	spawn.document.writeln('</head><body bgcolor=\"#ffffcc\" onload=\"startClock()\">');
	spawn.document.writeln('<center>');
	spawn.document.writeln('<SCRIPT LANGUAGE=\"Javascript\"><!--');
	spawn.document.writeln('var timeLimit = parent.timeLimit;');
	spawn.document.writeln('var xC = timeLimit;');
	spawn.document.writeln('var yC = 1;');
	spawn.document.writeln('var timerID;');
	spawn.document.writeln('function startClock(){');
	spawn.document.writeln('	if (timerID)');
	spawn.document.writeln('	{');
	spawn.document.writeln('	clearTimeout(timerID);');
	spawn.document.writeln('	}');
	spawn.document.writeln('	if (xC==0)');
	spawn.document.writeln('	{');
	spawn.document.writeln('		parent.keeper[parent.qIndex - 1] = 999;');
	spawn.document.writeln('		parent.startQuiz(false, ' + preventer + ');');
	spawn.document.writeln('	}');
	spawn.document.writeln('	else');
	spawn.document.writeln('	{');
	spawn.document.writeln('		WriteCTime(xC);');
	spawn.document.writeln('        xC = xC-yC;');
	spawn.document.writeln('		timerID = window.setTimeout(\"startClock()\", 1000);');
	spawn.document.writeln('	}');
	spawn.document.writeln('}');
	spawn.document.writeln('function WriteCTime(CTime){');
	spawn.document.writeln('	var Output = CTime;');
	spawn.document.writeln('	if (document.layers){');
	spawn.document.writeln('		var opentags=\'<center><font face=\"Arial,Geneva\" size=\"6\" color=\"red\">\';');
	spawn.document.writeln('		var closetags=\'</font></center>\';');
	spawn.document.writeln('		document.countmain.document.countnssub.document.write(opentags+Output+closetags);');
	spawn.document.writeln('		document.countmain.document.countnssub.document.close();');
	spawn.document.writeln('	}');
	spawn.document.writeln('	else if (document.all||document.getElementById)');
	spawn.document.writeln('	{');
	spawn.document.writeln('		var FDiv = document.getElementById(\'CTimeDiv\');');
	spawn.document.writeln('		FDiv.innerHTML = Output;');
	spawn.document.writeln('	}');
	spawn.document.writeln('}');
	spawn.document.writeln('//--></SCRIPT>');
	spawn.document.writeln('<div id=\"Q\">');
	spawn.document.writeln('<font face=\"arial\"><b>');
	spawn.document.writeln('<h2>Question ' + (qIndex + 1) + '</h2>');
	spawn.document.writeln(question);
	spawn.document.writeln('</b></font><br /><br />');
	spawn.document.writeln('<form>');
	spawn.document.writeln('<table><tr><td>');
	spawn.document.writeln('<font face=\"arial\"><b>');
	spawn.document.writeln('a <input type=radio name=\"answer\" value=\"a\" onclick="parent.keeper[parent.qIndex - 1] = this.value; parent.startQuiz(false, ' + preventer + ')\">&nbsp;&nbsp;' + optionA + '<br />');
	spawn.document.writeln('b <input type=radio name=\"answer\" value=\"b\" onclick="parent.keeper[parent.qIndex - 1] = this.value; parent.startQuiz(false, ' + preventer + ')\">&nbsp;&nbsp;' + optionB + '<br />');
	spawn.document.writeln('c <input type=radio name=\"answer\" value=\"c\" onclick="parent.keeper[parent.qIndex - 1] = this.value; parent.startQuiz(false, ' + preventer + ')\">&nbsp;&nbsp;' + optionC + '<br />');
	if (numOpt == 4) 
	{	
		spawn.document.writeln('d <input type=radio name=\"answer\" value=\"d\" onclick="parent.keeper[parent.qIndex - 1] = this.value; parent.startQuiz(false, ' + preventer + ')\">&nbsp;&nbsp;' + optionD + '<br />');
	}
    spawn.document.writeln('</b></font></td></tr></table>');
	spawn.document.writeln('</form>');
    spawn.document.writeln('</div>');
    spawn.document.writeln('<div class=\"CTime\" id=\"CTimeDiv\"></div>');
    spawn.document.writeln('<table><tr><td>');
    spawn.document.writeln('<ilayer name=\"countmain\" width=\"100\" height=\"50\" visibility=\"show\" bgcolor=\"#ffffcc\" id=\"countmain\">');
    spawn.document.writeln('<layer left=\"0\" top=\"0\" width=\"100\" height=\"50\" id=\"countnssub\">');
    spawn.document.writeln('</layer></ilayer>');
    spawn.document.writeln('</td></tr></table>');
	spawn.document.writeln('</center></body></html>');

	spawn.document.close();
	spawn.focus();
	
// Increment variables for the next question

	qIndex++;
	preventer++;
}

function gradeTest() 
{
// Increment the variables for the last time

	qIndex++;
	preventer++;
	
// Compare submitted answers with correct answers

	for (var i = 0; i < questions.length; i++) 
	{
		if (keeper[i] == answers[i]) 
			{correctAnswers++;}
	}
	
// Determine a ranking according to number of corrrect answers

	if (correctAnswers == 10)
		{SkillLevel = "Perfect! You are a Trivia Guru!"}
	if (correctAnswers < 10)
		{SkillLevel = "Excellent. But you can do even better if you try!"}
	if (correctAnswers < 7)
		{SkillLevel = "Good. But you need to try harder!"}
 	if (correctAnswers < 5)
		{SkillLevel = "You weren't trying, were you?"}
 	if (correctAnswers < 2)
		{SkillLevel = "You were joking, weren't you?"}

// 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><title>Quiz Results</title>');
	spawn.document.writeln('<SCRIPT LANGUAGE=\"Javascript\"><!--');
	spawn.document.writeln('if (window.focus) {self.focus();}');
	spawn.document.writeln('//--></SCRIPT');
	spawn.document.writeln('</head><body bgcolor=\"#ffcc99\">');						
	spawn.document.writeln('<font face=\"Arial\">');
	spawn.document.writeln('<H2>You scored ' + correctAnswers + ' / ' + questions.length + '.</H2>');
	spawn.document.writeln('<b>' + SkillLevel + '</b><br />');
	if (correctAnswers < 10)
		{spawn.document.writeln('<strong>Have another go.</strong><br /><br />');}
	spawn.document.writeln('<font size=\"4\">Here is how you scored: </font><br /><br />');

	for (var i = 0; i < questions.length; i++) 
	{
		spawn.document.writeln('\n\n<b>Question ' + (i + 1) + '</b><br />');
		spawn.document.writeln(questions[i].substring(0, questions[i].indexOf(' AAA ')));
		spawn.document.writeln('<br /><br />\n<font size=\"-1\">a. ' + questions[i].substring((questions[i].indexOf(' AAA ') + 5), questions[i].indexOf(' BBB ')) + '<br />');
		spawn.document.writeln('b. ' + questions[i].substring(questions[i].indexOf(' BBB ') + 5, questions[i].indexOf(' CCC ')) + '<br />');
		spawn.document.writeln('c. ' + questions[i].substring(questions[i].indexOf(' CCC ') + 5, questions[i].indexOf(' DDD ')) + '<br />');
	if (numOpt == 4) 
	{
		spawn.document.writeln('d. ' + questions[i].substring(questions[i].indexOf(' DDD ') + 5, questions[i].length) + '<br />');
	}
		spawn.document.writeln('<br /></font>');

// Determine if the solver answered each question appropriately, and display accordingly

		if (keeper[i] == 999) 
		{
			spawn.document.writeln('<b><i><font color=\"red\">You failed to answer.</font></i></b>\n<br /><br /><br />');
		}	
		else
		{	
			if (keeper[i] == answers[i]) 
			{
				spawn.document.writeln('<b><i><font color=\"green\">You answered ' + keeper[i] + '.<br />This is the correct answer.</font></i></b>\n<br /><br /><br />');
			}		
			else 
			{ 
			spawn.document.writeln('<b><i><font color=\"red\">You answered ' + keeper[i] + '.<br />This is incorrect.</font></i></b>\n<br /><br /><br />'); 
			}
		}
	}

	spawn.document.close();
	spawn.focus();
}

function busted() 
{
	alert('Sorry, you only get one shot at each question.');
	spawn.history.forward(); 
	spawn.focus();
}
