//
// QueryString
//

function QueryString(key)
{
	var value = null;
	for (var i=0;i<QueryString.keys.length;i++)
	{
		if (QueryString.keys[i]==key)
		{
			value = QueryString.values[i];
			break;
		}
	}
	return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse()
{
	var query = window.location.search.substring(1);
	var pairs = query.split("&");
	
	for (var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos >= 0)
		{
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			QueryString.keys[QueryString.keys.length] = argname;
			QueryString.values[QueryString.values.length] = value;		
		}
	}

}

QueryString_Parse();


//
// Answer
//
function Answer_WriteHTML()
{
	document.write('<INPUT type="radio" value="' + this.id + '" name="answers"> ');
	document.write('<span  class="quizText">' + this.text + '</span><br>');
}

function Answer(aID)
{
	this.text = "New Answer";
	this.id = aID;
	this.correct = false;
	
	this.WriteHTML = Answer_WriteHTML;
}

//
// AnswerList
//

function AnswerList_NewAnswer()
{
	var a = new Answer(this.sequenceID);
	this.sequenceID++;
	this.aList[this.aList.length] = a;

	// Optional Args: text, correct
	if (arguments.length > 0)
		a.text = arguments[0];

	if (arguments.length > 1)
		a.correct = arguments[1];

	if (this.editor)
		this.editor.AnswerSectionUpdate();
		
	return a;
}

function AnswerList_Remove(id)
{
	for (var i=0;i<this.aList.length;i++)
	{
		if (this.aList[i] && this.aList[i].id == id)
		{
			this.aList[i] = null;
			break;
		}
	}
}

function AnswerList_Find(id)
{
	var result = null;
	for (var i=0;i<this.aList.length;i++)
	{
		if (this.aList[i] && this.aList[i].id == id)
		{
			result = this.aList[i];
			break;
		}
	}
	return result;
}

function AnswerList_WriteHTML()
{
	for (var i=0;i<this.aList.length;i++)
		this.aList[i].WriteHTML();
}

function AnswerList(editor)
{
	this.editor = editor;
	this.sequenceID = 0;
	this.aList = new Array();
	
	this.NewAnswer = AnswerList_NewAnswer;
	this.Remove = AnswerList_Remove;
	this.Find = AnswerList_Find;
	this.WriteHTML = AnswerList_WriteHTML;
}

//
// Question
//

function Question_NewAnswer(text,correct)
{
	this.answerList.NewAnswer(text,correct);
}

function Question_WriteHTML()
{
	document.write('<p class="quizQuestion">Q: ' + this.text + '</p>');
	this.answerList.WriteHTML();
}

function Question_GetCorrectAnswer(text,correct)
{
	var result = "";
	for (var i=0;i<this.answerList.aList.length;i++)
	{
		if (this.answerList.aList[i] && this.answerList.aList[i].correct)
		{
			result = this.answerList.aList[i].text;
			break;
		}
	}
	return result;
}

function Question(qID,editor)
{
	this.text = "New Question";
	this.id = qID;
	this.editor = editor;
		
	this.answerList = new AnswerList(editor);
	
	this.NewAnswer = Question_NewAnswer;
	this.WriteHTML = Question_WriteHTML;
	this.GetCorrectAnswer = Question_GetCorrectAnswer;
}

//
// QuestionList
//

function QuestionList_NewQuestion()
{
	var q = new Question(this.sequenceID,this.editor);
	this.sequenceID++;
	this.qList[this.qList.length] = q;
	
	// Optional Args: text
	if (arguments.length > 0)
		q.text = arguments[0];
	
	if (this.editor)
		this.editor.QuestionItemsAdd(q);
		
	return q;
}

function QuestionList_Remove(id)
{
	for (var i=0;i<this.qList.length;i++)
	{
		if (this.qList[i] && this.qList[i].id == id)
		{
			this.qList[i] = null;
			break;
		}
	}
}

function QuestionList_Find(id)
{
	var result = null;
	for (var i=0;i<this.qList.length;i++)
	{
		if (this.qList[i] && (this.qList[i].id == id))
		{
			result = this.qList[i];
			break;
		}
	}
	return result;
}

function QuestionList_WriteHTML()
{
	var index = 0;
	
	var lastQuestion = QueryString("lastQuestion");
	var ccount = QueryString("ccount");

	if (ccount == null)
		ccount = 0;
	else
		ccount = parseInt(ccount);

	document.write('<form name="quiz" method="GET" onsubmit="return QuestionListValidate(this)">');
		
	
	if (lastQuestion!=null)
	{
		lastQuestion = parseInt(lastQuestion);
		index = 1 + lastQuestion;
		var answerID = parseInt(QueryString("answers"));
		
		if (this.qList[lastQuestion].answerList.aList[answerID].correct)
		{
			document.write('<p class="quizRightWrong">Correct!</p>');
			ccount++;
		}
		else
		{
			var correctAnswer = this.qList[lastQuestion].GetCorrectAnswer();
			document.write('<p class="quizRightWrong">That answer was not correct.</p>');
			document.write('<p class="quizText">The correct answer to:</p>');
			document.write('<p class="quizIndent">' + this.qList[lastQuestion].text + '</p>');
			document.write('<p class="quizText">is:</p>');
			document.write('<p class="quizIndent">' + correctAnswer + '</p>');
		}
		
		
	}
	
	if (index < this.qList.length)
	{
		document.write('<input type="hidden" name="lastQuestion" value="' + index + '">')
		
		this.qList[index].WriteHTML();
		document.write('<p><input type="submit" name="submit" value="Next Question >>"></p>')
	}
	else
	{
		var score = Math.round((ccount*100)/this.qList.length);
		var scoreResults = this.ScoreResults(Math.min(Math.floor(score/10),9));
		document.write('<p class="quizText">You answered ' + ccount + ' items out of ' +
			this.qList.length + ' correctly.</p>');
		document.write('<p class="quizText">Your score is ' + score + '%. ' + scoreResults + '</p>');
	}
	
	document.write('<input type="hidden" name="ccount" value="' + ccount + '">')
	document.write('</form>');
	
}

function QuestionList_ScoreResults(index,text)
{
	// Optional Args: text
	if (arguments.length > 1)
	{
		this.scoreResults[index] = text;
	}
		
	return this.scoreResults[index];
}

function QuestionList(editor)
{
	this.sequenceID = 0;
	this.qList = new Array();
	this.scoreResults = new Array(10);
	this.editor = editor;
	
	this.NewQuestion = QuestionList_NewQuestion;
	this.Remove = QuestionList_Remove;
	this.Find = QuestionList_Find;
	this.WriteHTML = QuestionList_WriteHTML;
	this.ScoreResults = QuestionList_ScoreResults;
}

function QuestionListValidate(theForm)
{
	var validated = false;
	
	for (var i=0;i<theForm.answers.length;i++)
	{
		if (theForm.answers[i].checked == true)
		{
			validated = true;
			break;
		}
	}
	
	if (!validated)
		alert("Please select an answer before continuing.");
	
	return validated;
}

var gQuestionList = new QuestionList(null);

// Quiz Source Start (to edit with QuizEditor copy/paste between here and end
// -->
gQuestionList.ScoreResults(0,"Are you sure you are trying?");
gQuestionList.ScoreResults(1,"Are you sure you are trying?");
gQuestionList.ScoreResults(2,"You may want to pay attention a little more.");
gQuestionList.ScoreResults(3,"You may want to pay attention a little more.");
gQuestionList.ScoreResults(4,"You may want to pay attention a little more.");
gQuestionList.ScoreResults(5,"You may want to pay attention a little more.");
gQuestionList.ScoreResults(6,"Not bad.");
gQuestionList.ScoreResults(7,"Good job!");
gQuestionList.ScoreResults(8,"Great job!");
gQuestionList.ScoreResults(9,"Excellent job! ");
q = gQuestionList.NewQuestion("In the poem \"The Night Before Christmas\" by Clement C. Moore what creature is mentioned?");
q.NewAnswer("Fly",false);
q.NewAnswer("Mouse",true);
q.NewAnswer("Cat",false);
q.NewAnswer("Dog",false);
q = gQuestionList.NewQuestion("In the movie The Christmas Story, what did Ralphie want for Christmas?");
q.NewAnswer("Go Cart",false);
q.NewAnswer("Bike",false);
q.NewAnswer("BB Gun",true);
q.NewAnswer("Lamp",false);
q = gQuestionList.NewQuestion("Other names for Santa Claus do not include:");
q.NewAnswer("St. Nick",false);
q.NewAnswer("Sint Klaas",false);
q.NewAnswer("Father Christmas ",false);
q.NewAnswer("Feliz Navidad",true);
q = gQuestionList.NewQuestion("The name of the girl that asked if there is a Santa in the famous editorial that answered that question.");
q.NewAnswer("Sally",false);
q.NewAnswer("Samantha",false);
q.NewAnswer("Betty",false);
q.NewAnswer("Virginia",true);
q = gQuestionList.NewQuestion("In the movie \"It's a Wonderful Life\" what is the angel's first name?");
q.NewAnswer("Sidney",false);
q.NewAnswer("Oliver",false);
q.NewAnswer("Clarence",true);
q.NewAnswer("George",false);
q = gQuestionList.NewQuestion("In \"Rudolph the Red Nosed Reindeer\" the elf named Hermey wanted to be a ");
q.NewAnswer("Dentist",true);
q.NewAnswer("Toy Painter",false);
q.NewAnswer("Doctor",false);
q.NewAnswer("Prospector",false);
q = gQuestionList.NewQuestion("In the Christmas classic, \"Home Alone\" where did the family go?");
q.NewAnswer("England",false);
q.NewAnswer("Scotland",false);
q.NewAnswer("France",true);
q.NewAnswer("Germany",false);
q = gQuestionList.NewQuestion("The biggest selling Christmas song of all time is: ");
q.NewAnswer("Bing Crosby's White Christmas",true);
q.NewAnswer("Brenda Lee's Rockin' Around the Christmas Tree",false);
q.NewAnswer("Connie Francis's Winter Wonderland",false);
q.NewAnswer("Pat Boone's I'll Be Home For Christmas",false);
q = gQuestionList.NewQuestion("In the 12 Days of Christmas, how many Lords a-leaping are there?");
q.NewAnswer("8",false);
q.NewAnswer("9",false);
q.NewAnswer("10",true);
q.NewAnswer("11",false);
q = gQuestionList.NewQuestion("In what city did Miracle on 34th Street take place?");
q.NewAnswer("New York",true);
q.NewAnswer("Chicago",false);
q.NewAnswer("Cleveland",false);
q.NewAnswer("Buffalo",false);
q = gQuestionList.NewQuestion("What is dressed in \"holiday style\" in Silver Bells?");
q.NewAnswer("Streets",false);
q.NewAnswer("Store Windows",false);
q.NewAnswer("Sidewalks",true);
q.NewAnswer("Train Station",false);
q = gQuestionList.NewQuestion("What is the name of the head elf in \"The Santa Clause\"?");
q.NewAnswer("Scott",false);
q.NewAnswer("Judy",false);
q.NewAnswer("Bernard",true);
q.NewAnswer("Larry",false);
q = gQuestionList.NewQuestion("Every year most homes in the United States bring something from the outdoors into their homes and decorate it.  This object is:");
q.NewAnswer("A Fir or Evergreen Tree",true);
q.NewAnswer("A Palm Tree",false);
q.NewAnswer("A log",false);
q.NewAnswer("A bush",false);
q = gQuestionList.NewQuestion("OK that was an easy one!  What country did the tradition of having Christmas tree come from?");
q.NewAnswer("England",false);
q.NewAnswer("Denmark",false);
q.NewAnswer("Germany",true);
q.NewAnswer("France",false);


// <-- Quiz Source End 
