//
//	Copyright 2007 Fohn.net
//

var quizText =          'Beaver is the common name for three species of rodent in two different taxonomical families. They are?\t2\tMuridae and Aplodontidae\tAplodontidae and Castoridae\tCastoridae and Cervidae\n';
var quizText = quizText+'The beavers of the family Castoridae are the largest living rodent in the world, except for what South American rodent?\t3\tguinea pig\tNew World porcupines\tcapybara\n';
var quizText = quizText+'Name three benefits of the beaver\'s dam building activities?\t1\tPurifies the water, economically maintains wetlands, soaks up floodwaters.\tCuts down small trees, purifies the water, slows the flow of river water.\tAnnoys farmers, attracts tourist dollars, encourages understanding of the animal kingdom.\n';
var quizText = quizText+'The longest beaver dam on record (which is found in Three Forks, Montana) is how long?\t2\t1,270 ft. (387 m)\t2,140 ft. (652. 2 m)\t3,120 ft. (950.9 m)\n';
var quizText = quizText+'What is the minimum water level required to keep the lodge entrance free from the ice during the winter?\t2\t3 to 4 ft. (.9 to 1.2 m)\t2 to 3 ft. (.6 to .9 m)\t6 to 7 ft. (1.8 to 2.1 m)\n';
var quizText = quizText+'Beavers will eat bark from hardwood trees such as...?\t1\tadler\teastern redcedar\tNorway spruce\n';
var quizText = quizText+'What factors determine the size of each litter?\t3\tThe amount of rainfall.\tThe number of predators in the area.\tThe amount of food and general health of the female.\n';
var quizText = quizText+'Which predators do beavers sometimes face?\t3\tcoyotes\talligators\tboth\n';
var quizText = quizText+'Which species of beaver has been the official emblem of Canada since 1975?\t2\tCastor fiber\tCastor canadensis\tCanadians would never have a beaver as a national symbol.\n';
var quizText = quizText+'How long can a beaver spend underwater?\t3\t6 to 7 minutes\t15 to 20 minutes\t10 to 15 minutes\n';
var quizText = quizText+'What does "plantigrade" mean?\t3\tTo not move very fast.\tTo like plants.\tTo walk with the entire sole of the foot on the ground.';

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