//
//	Copyright 2008 Fohn.net
//

var quizText =          'Rabbits are unable to vomit because?\t2\tFalse, rabbits are able to vomit.\tTheir cardiac sphincter is well developed.\tThe amino acids in their stomachs are incapable of gluconeogenesis.\n';
var quizText = quizText+'What name did the Phoenicians give the Iberian Peninsula, which means \'the land of the rabbit\'?\t1\t"i-shephan-im"\t"ant-wi-sumrt"\t"el-lanp-eun-leporida"\n';
var quizText = quizText+'Tularemia was discovered in 1837, Japan, so what was it named after?\t3\tDr. Edward Tulare\tTularengi province, Japan\tTulare County, California\n';
var quizText = quizText+'What time of day are rabbits most active?\t3\tdaytime\tnighttime\tin the mornings and evenings\n';
var quizText = quizText+'Why is it important not to allow a rabbit to twist and kick out with its hind legs, when being held?\t2\tYou could get hit by its legs.\tThe rabbit could injure its back.\tThe rabbit could be injured if you drop it.\n';
var quizText = quizText+'How many children, grandchildren, and great-grandchildren can a single female rabbit produce in one season?\t1\t800\t200\t1,023\n';
var quizText = quizText+'How many eyelids does a rabbit have?\t3\t5\t2\t3\n';
var quizText = quizText+'Which of the following predators do not eat rabbits?\t3\tCoyote\tHawk\tDon\'t be ridiculous, they all do.\n';
var quizText = quizText+'Tonic immobility is observed in other animals besides rabbits. Choose the animal that is not subject to tonic immobility.\t1\tBeavers\tSharks\tLizards\n';
var quizText = quizText+'Rabbits inhabit every continent on the globe except?\t3\tHawaii\tAustralia\tAntarctica';

var theQuiz = new Array;
var theAnswers = new Array;
var theQuestion;
var i, j;

function setupQuiz()
{
	var quizQuestions = quizText.split("\n");
	var	questionPieces;

	for (i = 0; i < quizQuestions.length; i++)
	{
		theQuestion = new Object;
		questionPieces = quizQuestions[i].split("\t");
		for (j = 0; j < questionPieces.length; j++)
		{
			if (j == 0)
			{
				theQuestion['question'] = questionPieces[j];
			}
			else if (j == 1)
			{
				theQuestion['correctAnswer'] = questionPieces[j];
			}
			else if (j == 2)
			{
				theQuestion['answers'] = new Array;
				theQuestion['answers'].push(questionPieces[j]);
			}
			else
			{
				theQuestion['answers'].push(questionPieces[j]);
			}
		}
		theQuiz.push(theQuestion);
	}
}

function displayQuestion(questionNumber)
{
	var quizHtml = "";

	theQuestion = theQuiz[questionNumber-1];
	quizHtml = '<div id="quiz-question">Question #'+questionNumber+': '+theQuestion['question']+'</div><center>';
	for (i = 0; i < theQuestion['answers'].length; i++)
	{
		quizHtml = quizHtml+'<button class="quiz-answer" onclick="selectAnswer('+(i+1)+')">'+theQuestion['answers'][i]+'</button>';
	}
	quizHtml = quizHtml+'</center>';
	document.getElementById('quiz').innerHTML = quizHtml;
}

function restartQuiz()
{
	theAnswers = new Array;
	displayQuestion(1);
}

function gradeQuiz()
{
	var wrongAnswers = new Array;
	var quizHtml;

	for (i = 0; i < theQuiz.length; i++)
	{
		if (theQuiz[i]['correctAnswer'] != theAnswers[i])
		{
			wrongAnswers.push(i);
		}
	}

	var rightAnswers = theQuiz.length - wrongAnswers.length;

	if (rightAnswers == theQuiz.length)
		quizHtml = '<center><b>Great job!</b><br> You answered all '+rightAnswers+' questions correctly!<br><br><br><img src="images/fireworks_big_burst_lw.gif"></center>';
	else
		quizHtml = 'You answered '+rightAnswers+' out of '+theQuiz.length+' correctly.';

	if (wrongAnswers.length > 0)
	{
		quizHtml = quizHtml+'<p>The questions you answered incorrectly are:</p>';
		for (i = 0; i < wrongAnswers.length; i++)
		{
			theQuestion = theQuiz[wrongAnswers[i]];
			quizHtml = quizHtml+'<p><b>Question #'+(wrongAnswers[i] + 1)+': '+theQuestion['question']+'</b><br>Correct Answer: '+theQuestion['answers'][(theQuestion['correctAnswer']*1)-1]+'<br>Your Answer: '+theQuestion['answers'][(theAnswers[wrongAnswers[i]]*1)-1]+'</p>';
		}
		quizHtml = quizHtml+'<p><button class="quiz-answer" onclick="restartQuiz()">Retake the quiz</button></p>';
	}

	document.getElementById('quiz').innerHTML = quizHtml;
}

function selectAnswer(theAnswer)
{
	theAnswers.push(theAnswer);
	if (theAnswers.length == theQuiz.length)
	{
		gradeQuiz();
	}
	else
	{
		displayQuestion(theAnswers.length+1);
	}
}

document.write('<div id="quiz"></div>');
setupQuiz();
displayQuestion(1);
