//
//	Copyright 2007 Fohn.net
//

var quizText =          'How long can a snake live with its body cut off?\t2\tSnakes can only live a few minutes without a head.\tAbout an hour after the head has been cut off.\tDeath is instantaneous.\n';
var quizText = quizText+'What can be used to uniquely identify a snake?\t2\tTheir finger prints.\tTheir skin pattern.\tTheir tongue.\n';
var quizText = quizText+'What is an Amelanistic corn snake?\t2\tIt is a corn snake that can not produce red skin pigment.\tIt is a corn snake that can not produce black skin pigment.\tShort sighted snake.\n';
var quizText = quizText+'What is an Anerythristic corn snake?\t1\tIt is a corn snake that can not produce red skin pigment.\tIt is a corn snake that can not produce black skin pigment.\tA snake with diarrhea.\n';
var quizText = quizText+'What do corn snakes eat in the wild?\t2\tElephants.\tSmall lizards, rodents, birds, and eggs.\tTermites, worms, and hedgehogs.\n';
var quizText = quizText+'Do corn snakes have teeth?\t1\tYes\tNo\tI confuse easily.\n';
var quizText = quizText+'Do corn snakes have fangs?\t2\tYes\tNo\tThey do, but they never use them.\n';
var quizText = quizText+'Why are snakes so easily burned by enclosure heaters?\t3\tThey have very small brains.\tTheir nerve conduction speed is too slow.\tThey have a lack of feeling on their scales.\n';
var quizText = quizText+'What are the signs of a dehydrated snake?\t2\tTheir tongue is stuck half way out.\tThe skin around the neck is wrinkled and forms puckers.\tThey turn a darker color.\n';
var quizText = quizText+'How do you know a snake is within 24 hours of shedding?\t1\tWhen the eyes clear the snake is ready to shed.\tWhen its skin becomes moist.\tAfter it has a really big meal.\n';
var quizText = quizText+'When a snake sheds its skin it is important that it comes off in one piece to prevent infections. What should you do to help it come off easily?\t2\tKeep a pair of tweezers on hand.\tSoak the snake in warm water.\tPanic!';

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