//
// 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
//  Edit comments here -->
gQuestionList.ScoreResults(0,"Are you sure you are really trying?");
gQuestionList.ScoreResults(1,"You may want to practice more.");
gQuestionList.ScoreResults(2,"You may want to practice more.");
gQuestionList.ScoreResults(3,"You may want to practice more.");
gQuestionList.ScoreResults(4,"You may want to practice more.");
gQuestionList.ScoreResults(5,"You may want to practice more.");
gQuestionList.ScoreResults(6,"Not bad - could improve however!");
gQuestionList.ScoreResults(7,"Good job - getting there!");
gQuestionList.ScoreResults(8,"Great job - almost 100%!");
gQuestionList.ScoreResults(9,"Excellent - you have been reading the Laws of the Game!");

<!-- begin questions here -->
q = gQuestionList.NewQuestion("What happens if a bowl is delivered over the sideline of the bowling area?");
q.NewAnswer("Nothing - this is permitted to get extra green.",false);
q.NewAnswer("The Umpire shall stop the bowl and declare it dead.",true);
q.NewAnswer("The Umpire shall warn the player.",false);

q = gQuestionList.NewQuestion("What happens if a substitute is needed in a game of triples?");
q.NewAnswer("The player may play lead only.",true);
q.NewAnswer("The player may play anywhere in the team.",false);
q.NewAnswer("The player may play lead or two.",false);

q = gQuestionList.NewQuestion("Which of these statements correctly describes legal footware?");
q.NewAnswer("Be comfortable and wear Jandals or bare feet.",false);
q.NewAnswer("Wear sports shoes with treaded soles so you don't slip over.",false);
q.NewAnswer("Wear footwear that is flat, smooth or fine treaded.",true);

q = gQuestionList.NewQuestion("Where should the bowls be before the jack is bowled?");
q.NewAnswer("The player who is about to bowl may have their bowl in the bowling area but all the rest clear of the bowling area",false);
q.NewAnswer("The bowls should be behind the front line and clear of the bowling area",true);
q.NewAnswer("Don't waste time clearing the bowls just put the jack up quickly",false);

q = gQuestionList.NewQuestion("What should happen if the time signal sounds in a nine end game and only eight ends have been played and the end is killed");
q.NewAnswer("The eighth end becomes the last end, the appropriate penalty is scored and the final end may be played if requested by either skip or player.",true);
q.NewAnswer("The non-offending player shall score the appropriate penalty and the game is ended.",false);
q.NewAnswer("The eighth end becomes the last end, the appropriate penalty is scored and a final end is played if requested by the non-offending skip or player.",false);

q = gQuestionList.NewQuestion("What would happen if a dead bowl is left on the mat and another comes in contact with it?");
q.NewAnswer("The dead bowl and any bowl coming into contact with it shall be removed from the mat.",false);
q.NewAnswer("The bowl coming into contact with the dead bowl shall remain where it comes to rest and the dead bowl removed from the mat.",true);
q.NewAnswer("The end shall be declared dead.",false);

q = gQuestionList.NewQuestion("When can a skip in a game of fours lodge an appeal with the controlling authority?");
q.NewAnswer("If they disagree with an Umpires measure.",false);
q.NewAnswer("They sign the card but then after discussion they decide to lodge an appeal about something they disagree with but half an hour after the game is over.",false);
q.NewAnswer("After a dispute has been referred to an Umpire and they disagree with a ruling on a point of Law and they lodge the appeal withing 5 minutes of the completion of the game.",true);

q = gQuestionList.NewQuestion("When does a player have control of the playing area?");
q.NewAnswer("When it is the players turn to bowl and the last bowl has come to rest.",true);
q.NewAnswer("When the last bowl has come to rest.",false);
q.NewAnswer("As soon as the opponent delivers their bowl.",false);

q = gQuestionList.NewQuestion("When can the bowls or jack be changed during the course of the game?");
q.NewAnswer("The bowls do not match so the players swap them with some that do.",false);
q.NewAnswer("With the consent of the opposing player or skip and the Controlling Authority unless a jack or bowl is broken.",true);
q.NewAnswer("A jack or bowl seems light and so another is taken from another mat.",false);

q = gQuestionList.NewQuestion("Is a duty Measurer's decision final?");
q.NewAnswer("No - you can call an umpire if you are not satisfied.",false);
q.NewAnswer("Yes.",true);

q = gQuestionList.NewQuestion("What would happen if an end is declared dead?");
q.NewAnswer("Replay the end from the same bowling area and the order of play shall be the same as for the end delared dead.",true);
q.NewAnswer("Replay the end from the end where the most bowls are to save time and the order of play shall be the same as for the end declard dead.",false);

q = gQuestionList.NewQuestion("Who measures in a game of triples");
q.NewAnswer("The lead or two of the team who played the last bowl.",false);
q.NewAnswer("The two of the team who played the last bowl.",true);
q.NewAnswer("The lead or the two who played the first bowl",false);

// <-- Quiz Source End 
