
// some globals
// number of questions in database
var phq = 599;
// total number of questions to be answered
var total = 7;
// used to pick particular sets of questions
var base = 0;
var range = 600;
// application timer
var appTimeout;
// answer entered from question
var choice = "";
// correct answer from answer page
var correct = "";
// current question
var current = 0;
// starting number of lives
var lives = 3;
// starting score
var score = 0;
// amount of time (ms) question displayed for
var qTime = 60000;
// second timer to display
var secTimer = qTime;
// amount of time (ms) answer displayed for
var aTime = 2000;
// amount of time (ms) highlight displayed for
var hTime = 2000;
// amount of time (ms) delay between elements popping up displayed for
var dTime = 2000;

// question and answers
var qText = "";
var aAText = "";
var aBText = "";
var aCText = "";
var aDText = "";
var aImage = "";

// variables to say if displaying choice / answer for NN JASS
var displayingChoice = false;
var displayingAnswer = false;
// nn mac v timing dependent
var buggy = false;
var imageNo;
// stop people from entering an answer more than once - make UI dead once clicked
var uiDead = false;
// for those who don't want sound
var playingSound = false;
// question numbers in test, unique from resource
var questionNumbers;

// set up question nos
function setupQ()
{
	questionNumbers = new Array(total);
	for ( i = 0 ; i < total ; i++)
	{
		var num = Math.round(base + (Math.random() * range));
		if (num < 10) num = "0" + num;
		if (num < 100) num = "0" + num;
		questionNumbers[i] = num;
		for ( j = 0 ; j < i ; j++)
		{
			while (questionNumbers[i] == questionNumbers[j])
			{
				num = Math.round(base + (Math.random() * range));
				if (num < 10) num = "0" + num;
				if (num < 100) num = "0" + num;
				questionNumbers[i] = num;
				j = 0;
			}
		}
	}
}

function qLoaded()
{
// function called when question loaded into frame
	qText = top.frames["qa"].iqText;

	aAText = top.frames["qa"].iaAText;
	aBText = top.frames["qa"].iaBText;
	aCText = top.frames["qa"].iaCText;
	aDText = top.frames["qa"].iaDText;
	aImage = top.frames["qa"].aImage;

	top.frames["template"].location.replace("../template.htm");
	playSound('timer.au', 'loop');

	appTimeout = setInterval("countDown()", qTime/30);
	imageNo = 29;
}

function countDown()
{
	if (!tLoaded) return;
	if (document.all)
	{
		top.frames["template"].document.images[top.imageNo].src = "images/black.gif";
		secTimer -= qTime/30;
		imageNo--;
		if (top.imageNo == 4)
		{
		playSound('alarm.au', 'loop');
			for (j = 0 ; j < 5 ; j++ )
			{
				top.frames["template"].document.images[j].src = "images/red.gif";
				top.frames["template"].focus();
			}
		}
			
		if (imageNo <= 0) imageNo = 0;

		if (secTimer <= 0) 
		{
			clearInterval(top.appTimeout);
			secTimer = qTime;
			checkA("Timeout");
		}
	}
}

function checkA(inChar)
{
	// function called when answer clicked (or keyboard hit)
	// displays highlight then loads answer frame
	switch (inChar)
	{
		case 'A':
		case 'B':
		case 'C':
		case 'D':
			if (!uiDead)
			{
				secTimer = qTime;
				clearInterval(appTimeout);
				uiDead = true;
				playSound('select.au', 'play');
				playSound('timer.au', 'stop');
				playSound('alarm.au', 'stop');
				if (document.all)
				{
				// MSIE 4.0 code
					choice = inChar;
					top.frames["template"].document.all["c" + inChar].style.color = "#ff6600";
					top.frames["template"].document.all["td" + inChar].style.borderStyle = "solid";
					top.frames["template"].document.all["td" + inChar].style.borderColor = "#ff6600";
					top.frames["template"].document.all["td" + inChar].style.borderWidth = "2";
				}
				top.frames["qa"].location.replace("mcqs/" + top.questionNumbers[top.current]   + "a.htm");	
			}
		break;
		case 'Timeout':
				top.frames["template"].focus();
				uiDead = true;
				playSound('timeout.au', 'play');
				playSound('timer.au', 'stop');
				playSound('alarm.au', 'stop');
				if (document.all)
				{
				// MSIE 4.0 code
					top.frames["qa"].location.replace("mcqs/" + top.questionNumbers[top.current]   + "a.htm");	
				}
		break;
		default:
				playSound('wrongkey.au', 'play');
	}
			
}


// called when answer page loaded
function aLoaded()
{
	top.appTimeout = setTimeout("aAfterTimeout()", top.hTime);
}

function aAfterTimeout()
{
// function called when answer frame loaded, highlights correct answer
	if (document.all)
	{
	// MSIE 4.0 code
		correct = top.frames["qa"].correct;
		top.frames["template"].document.all["c" + correct].style.color = "#00ccff";	
		top.frames["template"].document.all["td" + correct].style.borderLeft = "solid";	
		top.frames["template"].document.all["td" + correct].style.borderTop = "solid";	
		top.frames["template"].document.all["td" + correct].style.borderBottom = "solid";	
		top.frames["template"].document.all["td" + correct].style.borderRight = "none";	
		top.frames["template"].document.all["td" + correct].style.borderColor = "#0099ff";	
		top.frames["template"].document.all["td" + correct].style.borderWidth = "2";

		top.frames["template"].document.all["a" + correct].style.color = "#ff6600";	
		top.frames["template"].document.all["a" + correct + "Text"].style.color = "#00ccff";	
		top.frames["template"].document.all["a" + correct].style.borderRight = "solid";	
		top.frames["template"].document.all["a" + correct].style.borderTop = "solid";	
		top.frames["template"].document.all["a" + correct].style.borderBottom = "solid";	
		top.frames["template"].document.all["a" + correct].style.borderColor = "#0099ff";	
		top.frames["template"].document.all["a" + correct].style.borderWidth = "2";
		if (correct == choice)
		{
			playSound('right.au', 'play');
			score++;
			top.frames["template"].document.all["score"].innerHTML = score;	
			top.frames["template"].document.all["score"].style.color = "#00ccff";	
		}
		else
		{
			playSound('wrong.au', 'play');
			lives--;
			top.frames["template"].document.all["lives"].innerHTML = lives;	
			top.frames["template"].document.all["lives"].style.color = "#00ccff";	
		}
	}

	if ((lives == 0) || (current == total - 1))
	{
		appTimeout = setTimeout("endQ()", aTime);
	}
	else
	{
		appTimeout = setTimeout("nextQ()", aTime);
	}
}

function nextQ()
{
	clearTimeout(appTimeout);
	correct = "";
	displayingAnswer = false;
	choice = "";
	displayingChoice = false;
	uiDead = false;
	current++;
	top.frames["qa"].location.replace("mcqs/" + top.questionNumbers[top.current]   + ".htm");
}

function endQ()
{
	clearTimeout(appTimeout);
	top.frames["template"].location.replace("end.htm");	
	top.frames["qa"].location.replace("black.htm");	
}

function playSound(inSound, inAction)
{
	if (playingSound)
	{
		if (document.all)
		{
		// MSIE 4.0 code
			top.frames["sndlib"].document.sounder.soundAction(inSound, inAction);
		}
	}
}



// Next lot of functions are for front page

function getState()
{
	top.frames["template"].document.forms["setup"].elements["sound"].checked = top.playingSound;	
	top.frames["template"].document.forms["setup"].elements["question"].selectedIndex = top.total - 1;	
}

function setQ()
{
	top.total = parseInt(top.frames["template"].document.forms["setup"].elements["question"].options[top.frames["template"].document.forms["setup"].elements["question"].selectedIndex].text);;
}

function setSound()
{
	top.playingSound = top.frames["template"].document.forms["setup"].elements["sound"].checked;
	if (top.playingSound)
	{
		top.frames["sndlib"].location.replace("sndlib.htm");
	}
	else
	{
		top.frames["sndlib"].location.replace("black.htm");
	}
}

function hiLight(inArea)
{
	document.images["str"].src = "images/" + inArea + "Str.gif";
}

function select(inArea, inBase, inRange)
{
	document.images["str"].src = "images/" + inArea + "Str.gif";
	document.images["selector"].src = "images/" + inArea + "S.jpg";
	setQ();
	setSound();
	top.base = inBase;
	top.range = inRange;
	top.uiDead = false;
	top.current = 0;
	top.score = 0;
	top.lives = 3;
	top.setupQ();
	appTimeout = setTimeout("realStart()", 2000);
}

function realStart()
{
	top.frames["qa"].location.replace("mcqs/" + top.questionNumbers[0]   + ".htm");
}

function unLight()
{
	document.images["str"].src = "images/SciStr.gif";
}

function genericImage(inImageName, inRelSrcURL, inStatusBar, inImportance)
{
	if (document.images)
	{
		document.images[inImageName].src = inRelSrcURL;
		window.status = inStatusBar;
	}
}

