//
//	Copyright 2007 Fohn.net
//

var quizText =          'The camel has played such an important role in Arab culture there are how many words for it in the Arab language?\t4\t95\t200\t3\t160\n';
var quizText = quizText+'You can easily identify a dromedary camel if you remember that their single hump forms the letter "D" on its side. Can you remember what the two hump camel is called?\t3\tRamonian\tCollian\tBactrian\n';
var quizText = quizText+'The camel\'s eye is protected from sand by two rows of extra long eyelashes, one on the upper eyelid, and one on the lower eyelid. In addition to this, each eye also has a very thin third eyelid that moves in which direction?\t1\tSide to side\tUp and down\tDiagonally\n';
var quizText = quizText+'Although camels will normally select the freshest vegetation available, when food is scarce they will eat anything-- salty plants, dried plants, bones, fish, meat, leather, and even on occasion their owner\'s tent. What is the scientific term for an animal that will eat anything?\t2\tHungry\tOmnivore\tEatae-anitae\n';
var quizText = quizText+'In the wintertime, camels can gather enough moisture from the plants they eat to go as much as 50 days without water. In the summertime, how many days can a camel go without water?\t1\t5 days\t10 days\t15 days\n';
var quizText = quizText+'Camels are capable of losing safely how much of their body\'s weight in water?\t3\t10%\t25%\t30%\n';
var quizText = quizText+'The term hydrophilicity, which describes the hemoglobins in a camel\'s red blood cells, means what?\t2\tRepels water\tAttracts water\tWill drink anything\n';
var quizText = quizText+'Camels are capable of drinking how many gallons / liters of water in 10 minutes?\t2\t15 gallons / 65.78 L\t30 gallons / 113.56 L\t50 gallons / 190 L\n';
var quizText = quizText+'A camel keeps as cool as it can by resting when the weather is extremely hot.  It will lay down in a shady place, if it can find one. What will a camel do if it cannot find a shady place to rest on a hot day?\t1\tFace the sun to minimize how much of its body is exposed to the rays.\tStick its head in the sand.\tDig a hole.\n';
var quizText = quizText+'Although chicken is the most widely consumed domestic meat in Saudi Arabia today, camel meat could be the meat of the future, especially in health-conscious Western countries. Why is this?\t2\tBecause camels being stupid are easily caught.\tBecause camel meat has no cholesterol and hardly any fat.\tBecause Australia has so many, they don\'t know what else to do with them.';

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