//
// 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">Thats right!</p>');
			ccount++;
		}
		else
		{
			var correctAnswer = this.qList[lastQuestion].GetCorrectAnswer();
			document.write('<p class="quizRightWrong">That answer was wrong. What are you an idiot?</p>');
			document.write('<p class="quizText">The right answer was: ');
			document.write('' + 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">The results are in. You got ' + ccount + ' out of ' +
			this.qList.length + ' correct.</p>');
		document.write('<p class="quizText">That makes your score ' + 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,"If I still taught at Hartley you'd be on detention for such a shocking score!");
gQuestionList.ScoreResults(1,"Were you paying ANY attention in class? I think some extra lessons are in order - after school!");
gQuestionList.ScoreResults(2,"You've obviously not put in enough effort. Write me a 100 word essay on why I shouldn't put you on detention along with the likes of Rivers and Bordino.");
gQuestionList.ScoreResults(3,"Do you watch the show? You're looking down the barrel of a detention here, son.");
gQuestionList.ScoreResults(4,"Try again - or its forty laps around the footy pitch!");
gQuestionList.ScoreResults(5,"Half marks, you've got potential. Maybe if you spent less time chatting in the Shark Pool and watched the show more you'd do better!");
gQuestionList.ScoreResults(6,"Not bad, though clearly you could do better. Perhaps a trip to Yola would help - I hear she's a bit of an expert when it comes to affairs of the heart. Maybe it's a good job I never became an English teacher with puns like that!");
gQuestionList.ScoreResults(7,"A marked improvement, son. You well might have a future here. If you keep it up, mind. Now get out of here!");
gQuestionList.ScoreResults(8,"Well done, you'll do well in your exams -  providing you don't stuff them up. I'll be keeping my eyes on you.");
gQuestionList.ScoreResults(9,"That's an A! Top marks, you've got a bright future here at Hartley. Ever thought about becoming a prefect in your free time? WHAT?!? You want to play SOCCER instead? Get out of here!!");
q = gQuestionList.NewQuestion("No doubt this was the talk of the playground at the time - but who did I leave Hartley with?");
q.NewAnswer("Christina Milano",false);
q.NewAnswer("Jodie Cooper",false);
q.NewAnswer("Yola Fatoush",true);
q.NewAnswer("Stella Poulos",false);
q = gQuestionList.NewQuestion("In my book it's nothing more than a racket but you students insist it's music. Who never subjected us to their singing?");
q.NewAnswer("Con Bordino",false);
q.NewAnswer("Lee Delaine",false);
q.NewAnswer("Thania Saya",true);
q.NewAnswer("Nikki Ruark",false);
q = gQuestionList.NewQuestion("What was the name of Ronnie's temporary replacement? Hint, he had a daugher called Aurora of all things.");
q.NewAnswer("Albers",false);
q.NewAnswer("Jay",true);
q.NewAnswer("Phil",false);
q.NewAnswer("Les",false);
q = gQuestionList.NewQuestion("Which student wanted to work in the music industry as a sound engineer? Hint: he could do with a haircut!");
q.NewAnswer("Ryan Scheppers",false);
q.NewAnswer("Con Bordino",false);
q.NewAnswer("Lee Delaine",false);
q.NewAnswer("Zac Croft",true);
q = gQuestionList.NewQuestion("Les Bailey tells me that his Hartley High students finally passed the HSC. Who got the highest score of the following:");
q.NewAnswer("Sarah Livingstone",true);
q.NewAnswer("Anita Scheppers",false);
q.NewAnswer("Kurt Petersen",false);
q.NewAnswer("Dennis Klinssman",false);
q = gQuestionList.NewQuestion("Which student had a secret modelling career that was only revealed two years after he left Hartley?");
q.NewAnswer("Ryan Scheppers",false);
q.NewAnswer("Steve Wiley",false);
q.NewAnswer("Nick Poulos",true);
q.NewAnswer("Declan Costello",false);
q = gQuestionList.NewQuestion("Mariel, the spitting image of Thania Saya, offered a free mocca to whom after their recent TV appearance?");
q.NewAnswer("Marco Vialli",false);
q.NewAnswer("Con Bordino",false);
q.NewAnswer("Drazic",true);
q.NewAnswer("Nikki Ruark",false);
q = gQuestionList.NewQuestion("What was the name of the homeless girl who sold Anita art?");
q.NewAnswer("Rose",false);
q.NewAnswer("Jet",false);
q.NewAnswer("Skye",true);
q.NewAnswer("Aimee",false);
q = gQuestionList.NewQuestion("His support of Barnett's anti-violence strategy made an enemy of Albers and he was eventually transferred to another school because of a grudge held by a student. He was...");
q.NewAnswer("Les Bailey",false);
q.NewAnswer("Pat Gregory",true);
q.NewAnswer("Phil North",false);
q.NewAnswer("Rhys Maddox",false);
q = gQuestionList.NewQuestion("Some of the hairstyles are an absolute disgrace thesedays - if I were principal there'd be a strict uniform code! When Dennis was trying to get backstage access to the legendary Tommy Immanuel his hair was...");
q.NewAnswer("Blonde",false);
q.NewAnswer("Blonde in pony tails",false);
q.NewAnswer("Blonde with red and purple patches",true);
q.NewAnswer("Blonde with red tips",false);


// <-- Quiz Source End 

