//
//	Copyright 2007 Fohn.net
//

var quizText =          'Which of the following is true:\t2\tAll skunks have stripes.\tSpotted Skunks don\'t actually have spots.\tAll skunks are black and white.\n';
var quizText = quizText+'The English word Skunk is derived from the Algonquian Indian name "seganku," which meant:\t1\t"One who squrits"\t"Bad smell"\t"Black and white cat"\n';
var quizText = quizText+'Skunks are members of which family?\t3\tMustelidae\tProcyonidae\tMephitidae\tAiluridae\n';
var quizText = quizText+'When confronted with a threat, which of the following would a skunk NOT do?\t3\tA handstand\tRun away\tStick there head in the sand\tPlay dead\n';
var quizText = quizText+'The best way to remove skunk musk is to rinse the sprayed item in tomato juice.\t2\tTrue\tFalse\n';
var quizText = quizText+'Which animal is the skunk\'s most deadly enemy?\t3\tPerigean Falcon\tBobcat\tGreat Horned Owl\tBarn Owl\n';
var quizText = quizText+'On average, a skunk can have enough musk for how many sprays?\t4\tTwo sprays\tThree sprays\tFour sprays\tFive sprays\tSix sprays\n';
var quizText = quizText+'Which animal is not a member of the <i>Mephitidae</i> family.\t2\tHooded Skunk\tZorilla\tStink Bandger\n';
var quizText = quizText+'What is the primary smell producing chemical in skunk musk?\t3\ttrans-p-menthan-8-thiol-3-one\ttrans-2-enoyl-coenzyme\ttrans-2-butene-1-thiol\n';
var quizText = quizText+'You can determine the gender of a skunk by its stripe pattern.\t2\tTrue\tFalse';

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);
