//
// 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,"Were you alive in the 70's?");
gQuestionList.ScoreResults(1,"No one can say you're living in the past!");
gQuestionList.ScoreResults(2,"Dust off those old records and refresh your memory!");
gQuestionList.ScoreResults(3,"You probably remember the music, but not the names.");
gQuestionList.ScoreResults(4,"Hey, you did alright!");
gQuestionList.ScoreResults(5,"Not too shabby!");
gQuestionList.ScoreResults(6,"Very good!");
gQuestionList.ScoreResults(7,"You're still listening to 70's music, aren't you?");
gQuestionList.ScoreResults(8,"Great job! The Bee Gee's would be proud of you!");
gQuestionList.ScoreResults(9,"Excellent job! Now take a break and boogie!");
q = gQuestionList.NewQuestion("What former star of the TV show \"Starsky & Hutch\" had a #1 hit with \"Don't Give Up On Us\"?");
q.NewAnswer("David Banner",false);
q.NewAnswer("David Soul",true);
q.NewAnswer("David Carradine",false);
q.NewAnswer("David Goliath",false);
q = gQuestionList.NewQuestion("Which Gibb brother released the #1 hit \"I Just Want To Be Your Everything\"?");
q.NewAnswer("Maurice Gibb",false);
q.NewAnswer("Robin Gibb",false);
q.NewAnswer("Andy Gibb",true);
q.NewAnswer("Barry Gibb",false);
q = gQuestionList.NewQuestion("What classic did singer/songwriter Van Morrison release in 1977?");
q.NewAnswer("Moondance",true);
q.NewAnswer("You Make Me Feel Like Dancing",false);
q.NewAnswer("Moonlight Romance",false);
q.NewAnswer("Sentimental Lady",false);
q = gQuestionList.NewQuestion("Who accompanied Elton John on the song \"Don't Go Breaking My Heart\"?");
q.NewAnswer("Donna Summer",false);
q.NewAnswer("Samantha Sang",false);
q.NewAnswer("Maureen McGovern",false);
q.NewAnswer("Ki Ki Dee",true);
q = gQuestionList.NewQuestion("How many ways did Paul Simon have to leave his lover?");
q.NewAnswer("20",false);
q.NewAnswer("25",false);
q.NewAnswer("50",true);
q.NewAnswer("75",false);
q = gQuestionList.NewQuestion("In their 1976 #1 hit, what did K.C. and The Sunshine Band ask you to shake?");
q.NewAnswer("Your Love Thang",false);
q.NewAnswer("Your Booty",true);
q.NewAnswer("The House",false);
q.NewAnswer("Your Body",false);
q = gQuestionList.NewQuestion("What greeting did John Sebastian sing in his 1976 #1 hit?");
q.NewAnswer("Welcome Back",true);
q.NewAnswer("Hello, It's Me",false);
q.NewAnswer("Hello",false);
q.NewAnswer("Good Morning",false);
q = gQuestionList.NewQuestion("Who had a #1 hit with \"Rhinestone Cowboy?\"");
q.NewAnswer("Waylon Jennings",false);
q.NewAnswer("Glenn Campbell",true);
q.NewAnswer("Willie Nelson",false);
q.NewAnswer("Conway Twitty",false);
q = gQuestionList.NewQuestion("In 1975 Phil Collins became the lead singer for Genesis. Who did he replace?");
q.NewAnswer("Gabriel Clark",false);
q.NewAnswer("Tommy Shaw",false);
q.NewAnswer("Peter Gabriel",true);
q.NewAnswer("Peter O'Toole",false);
q = gQuestionList.NewQuestion("What do the letters AWB stand for?");
q.NewAnswer("Afghan Wig Boys",false);
q.NewAnswer("Alternative Wide Bodies",false);
q.NewAnswer("Average White Band",true);
q.NewAnswer("Asian Wildlife Barricade",false);
q = gQuestionList.NewQuestion("How many \"Tickets To Paradise\" did Eddy Money have?");
q.NewAnswer("1",false);
q.NewAnswer("2",true);
q.NewAnswer("4",false);
q.NewAnswer("7",false);
q = gQuestionList.NewQuestion("What Montreal born pop/soul singer/songwriter had a charted hit with \"I Just Want To Stop\"?");
q.NewAnswer("Gino Vannelli",true);
q.NewAnswer("Boz Scaggs",false);
q.NewAnswer("Bobby Cauldwell",false);
q.NewAnswer("Bob Welch",false);
q = gQuestionList.NewQuestion("Where did Exile want to kiss you in their 1978 top 10 hit?");
q.NewAnswer("In The Bedroom",false);
q.NewAnswer("On Your Sweet, Sweet Lips",false);
q.NewAnswer("All Over",true);
q.NewAnswer("At The Carwash",false);
q = gQuestionList.NewQuestion("What kind of fever did the Bee Gees suffer from in 1978?");
q.NewAnswer("Disco Fever",false);
q.NewAnswer("Inferno Fever",false);
q.NewAnswer("Dance Fever",false);
q.NewAnswer("Night Fever",true);
q = gQuestionList.NewQuestion("Which singer was \"Bluer Than Blue?\"");
q.NewAnswer("John Paul Young",false);
q.NewAnswer("Michael Johnson",true);
q.NewAnswer("Nick Gilder",false);
q.NewAnswer("Cliff Richard",false);


// <-- Quiz Source End 

