//
// 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);


gQuestionList.ScoreResults(0,"Are you sure you are trying?");
gQuestionList.ScoreResults(1,"You may want to read a little more about Ireland");
gQuestionList.ScoreResults(2,"You may want to read a little more about Ireland");
gQuestionList.ScoreResults(3,"You may want to read a little more about Ireland");
gQuestionList.ScoreResults(4,"You may want to read a little more about Ireland");
gQuestionList.ScoreResults(5,"You may want to read a little more about Ireland");
gQuestionList.ScoreResults(6,"Not bad.");
gQuestionList.ScoreResults(7,"Good job!");
gQuestionList.ScoreResults(8,"Great job!");
gQuestionList.ScoreResults(9,"Excellent job!");
q = gQuestionList.NewQuestion("1. Which day is St. Patrick's Day?");
q.NewAnswer("a) March 17th",true);
q.NewAnswer("b) February 1st",false);
q.NewAnswer("c) December 31st",false);
q = gQuestionList.NewQuestion("2. Who wrote the famous book 'Ulysses'?");
q.NewAnswer("a) Roddy Doyle",false);
q.NewAnswer("b) Frank McCourt",false);
q.NewAnswer("c) James Joyce",true);
q = gQuestionList.NewQuestion("3. What's a shillelagh?");
q.NewAnswer("a) A pot of gold",false);
q.NewAnswer("b) A blackthorn stick",true);
q.NewAnswer("c) A fairy's den",false);
q = gQuestionList.NewQuestion("4. What did Saint Patrick use to explain Christianity to the Irish people?");
q.NewAnswer("a) A harp",false);
q.NewAnswer("b) A shamrock",true);
q.NewAnswer("c) A pint of Guinness",false);
q = gQuestionList.NewQuestion("5. Which of the following legends is associated with Saint Patrick?");
q.NewAnswer("a) He chased the rats away",false);
q.NewAnswer("b) He drove the snakes and serpents out of Ireland",true);
q.NewAnswer("c) He never grew old",false);
q = gQuestionList.NewQuestion("6. Legend has it, that when you kiss the Blarney Stone, you will?");
q.NewAnswer("a) have the 'gift of the gab'",true);
q.NewAnswer("b) marry within a year",false);
q.NewAnswer("c) have bad luck",false);
q = gQuestionList.NewQuestion("7. Which colour is not found on the flag of the Republic of Ireland?");
q.NewAnswer("a) Green",false);
q.NewAnswer("b) Red",true);
q.NewAnswer("c) White",false);
q = gQuestionList.NewQuestion("8. What does Erin go Bragh mean?");
q.NewAnswer("a) Ireland forever",true);
q.NewAnswer("b) Everyone is Irish",false);
q.NewAnswer("c) Kiss me, I'm Irish",false);
q = gQuestionList.NewQuestion("9. Where was Eamonn DeValera born?");
q.NewAnswer("a) Dublin",false);
q.NewAnswer("b) Belfast",false);
q.NewAnswer("c) New York",true);
q = gQuestionList.NewQuestion("10. Which of the following is not an Irish whiskey?");
q.NewAnswer("a) Jameson",false);
q.NewAnswer("b) Midleton",false);
q.NewAnswer("c) Glenfiddich",true);
q = gQuestionList.NewQuestion("11. Who is Ireland's female patron saint?");
q.NewAnswer("a) Saint Catherine",false);
q.NewAnswer("b) Saint Brigid",true);
q.NewAnswer("c) Saint Patrick",false);
q = gQuestionList.NewQuestion("12. An uprising in 1916 is named after which religious festival?");
q.NewAnswer("a) Easter",true);
q.NewAnswer("b) Christmas",false);
q.NewAnswer("c) St. Patrick's Day",false);
q = gQuestionList.NewQuestion("13. Who is the current president of Ireland?");
q.NewAnswer("a) Mary Robinson",false);
q.NewAnswer("b) Mary McAleese",true);
q.NewAnswer("c) Mary Harney",false);
q = gQuestionList.NewQuestion("14. Which Irish musician won an Oscar for the song Falling Slowly?");
q.NewAnswer("a) Paddy Casey",false);
q.NewAnswer("b) Damien Rice",false);
q.NewAnswer("c) Glen Hansard",true);
q = gQuestionList.NewQuestion("15. What is the unit of currency in Ireland?");
q.NewAnswer("a) Punt",false);
q.NewAnswer("b) Euro",true);
q.NewAnswer("c) Pound",false);
q = gQuestionList.NewQuestion("16. When is Bloomsday?");
q.NewAnswer("a) June 16th",true);
q.NewAnswer("b) March 17th",false);
q.NewAnswer("c) May 5th",false);
q = gQuestionList.NewQuestion("17. Which province is Kerry located?");
q.NewAnswer("a) Leinster",false);
q.NewAnswer("b) Munster",true);
q.NewAnswer("c) Ulster",false);
q = gQuestionList.NewQuestion("18. What county is Croagh Patrick located?");
q.NewAnswer("a) Meath",false);
q.NewAnswer("b) Mayo",true);
q.NewAnswer("c) Offaly",false);
q = gQuestionList.NewQuestion("19. What was U2s first album?");
q.NewAnswer("a) How To Dismantle An Atomic Bob",false);
q.NewAnswer("b) Rattle & Hum",false);
q.NewAnswer("c) Boy",true);
q = gQuestionList.NewQuestion("20. Which Irish author said \"It's not that the Irish are cynical. It's rather that they have a wonderful lack of respect for everything and everybody\"?");
q.NewAnswer("a) Oscar Wilde",false);
q.NewAnswer("b) Samuel Beckett",false);
q.NewAnswer("c) Brendan Behan",true);



