//
// 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,"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.");
gQuestionList.ScoreResults(7,"Good job!");
gQuestionList.ScoreResults(8,"Great job!");
gQuestionList.ScoreResults(9,"Excellent job!");
q = gQuestionList.NewQuestion("Traditional overland transport by horse- or ox-drawn cart was both costly and time consuming.  It worked against developing products for the marketplace.  What new means of transportation gave the town of Worcester its start in becoming an important industrial city in the early nineteenth century (early 1800s)?");
q.NewAnswer("Providence & Worcester Railroad",false);
q.NewAnswer("The wheel",false);
q.NewAnswer("The Blackstone Canal",true);
q.NewAnswer("Interstate 290",false);
q = gQuestionList.NewQuestion("In the mid-nineteenth century (mid 1800s), a local industrialist named Ichabod Washburn led the movement to erect a building that helped a group of like minded men form an identity as a social class of their own. What building is this?");
q.NewAnswer("Horticultural Hall",false);
q.NewAnswer("The Centrum Centre",false);
q.NewAnswer("Mechanics Hall",true);
q.NewAnswer("City Hall",false);
q = gQuestionList.NewQuestion("How was Worcester different in its industrial development from most other New England industrial cities of the early nineteenth-century?");
q.NewAnswer("Worcester is in Massachusetts.",false);
q.NewAnswer("Unions were more powerful in Worcester.",false);
q.NewAnswer("Worcester is located on a major river.",false);
q.NewAnswer("Many industries made many different products.",true);
q = gQuestionList.NewQuestion("How did local investors in the early 1800s, like Stephen Salisbury II and William T. Merrifield, help make Worcester a city of many industries?");
q.NewAnswer("They built water- or steam-powered factories and rented space in them.",true);
q.NewAnswer("They founded companies that grew huge.",false);
q.NewAnswer("They founded a baseball team that made Worcester famous.",false);
q.NewAnswer("They opened Worcester’s first bank.",false);
q = gQuestionList.NewQuestion("By the late nineteenth century thousands of men and women worked in large factories in Worcester.  What kind of laborers made up the majority of this industrial workforce?");
q.NewAnswer("Hippies",false);
q.NewAnswer("Debtors",false);
q.NewAnswer("Skilled tradespeople",false);
q.NewAnswer("Unskilled employees",true);
q = gQuestionList.NewQuestion("People who came to work in Worcester industries needed places to live.  What new type of housing, which still exists in Worcester, was designed in the late-nineteenth century to house the city's rapidly growing working class?");
q.NewAnswer("Log cabins",false);
q.NewAnswer("Condominiums",false);
q.NewAnswer("Tenement buildings",false);
q.NewAnswer("Three-decker",true);
q = gQuestionList.NewQuestion("As Americans moved westward, their need for certain things influenced local industrial development. Can you name the Worcester-made product that helped farmers and ranchers control the vast, open landscape of the west in the late nineteenth century?");
q.NewAnswer("Smiley Face",false);
q.NewAnswer("Steam engine locomotives",false);
q.NewAnswer("The telegraph",false);
q.NewAnswer("Barbed wire",true);
q = gQuestionList.NewQuestion("Some industrial cities are closely associated with a single well-known product.  For example, in Massachusetts, Lowell and Lawrence were once renowned for textiles and Lynn was known for footwear.  In today's world, Detroit, Michigan, is known for cars, and California's Silicon Valley is famous for computer products.  Why has Worcester remained less well known for a particular product?");
q.NewAnswer("Worcester did not make anything important.",false);
q.NewAnswer("No one uses Worcester products any more.",false);
q.NewAnswer("Worcester products were never used outside of Worcester.",false);
q.NewAnswer("Worcester's biggest strength in industry always has been in the manufacture of machines and products used to make or assemble other products.",true);
q = gQuestionList.NewQuestion("Until 1828, Worcester was a rural inland town, and almost all of its inhabitants were of  English descent.  Over the course of the nineteenth century, many immigrant groups came to Worcester. They formed distinctive ethnic communities in the city.  What ethnic group settled along Water Street in the late 1800s?");
q.NewAnswer("The Vietnamese",false);
q.NewAnswer("The Irish",false);
q.NewAnswer("The Swedes",false);
q.NewAnswer("The Jews",true);
q = gQuestionList.NewQuestion("Many people who have studied diners believe they evolved from the night night lunch carts that were manufactured in Worcester between 1890 and 1908 by T. H. Buckley.  In fact, Buckley's Worcester Lunch Car Company was one of a number of diner manufacturers in America, and it was not the first.  Diners were invented in the context of industrialization.  What makes a diner different from any other kind of restaurant?");
q.NewAnswer("The food",false);
q.NewAnswer("The late hours open",false);
q.NewAnswer("The customers",false);
q.NewAnswer("The mobile building",true);
q = gQuestionList.NewQuestion("In most of the region's manufacturing centers, large numbers of workers were unionized by late  the 1800s.  In Worcester, however, unions did not become popular until the late 1930s.  Why?");
q.NewAnswer("Worcester workers were paid better than workers in other cities.",false);
q.NewAnswer("Worcester had only one ethnic group.",false);
q.NewAnswer("Worcester had only one industry.",false);
q.NewAnswer("Worcester employers took care of their employees.",true);
q = gQuestionList.NewQuestion("What was the first industry in Worcester to employ women and girls in large numbers?");
q.NewAnswer("Plastics",false);
q.NewAnswer("Steel & Wire",false);
q.NewAnswer("Valentines",false);
q.NewAnswer("Clothing",true);
q = gQuestionList.NewQuestion("With the outbreak of the World War II, American industries began to make wartime products to sell to European allies.  When the United States joined the war effort in 1941, Worcester industries began supplying our own armed forces.  What item was invented in Worcester that saved thousands of lives and helped win the war?");
q.NewAnswer("The Space Suit",false);
q.NewAnswer("The Monkey Wrench",false);
q.NewAnswer("The Woven Cartridge Belt",false);
q.NewAnswer("The WaterFog Nozzle ",true);
q = gQuestionList.NewQuestion("Between the 1960s and 1980s, the region's industry experienced tremendous change.  By 1990, most of Worcester's industrial giants had dramatically restructured or disappeared, and many people lost their jobs or switched careers.  What caused these major changes?");
q.NewAnswer("Union Station closed",false);
q.NewAnswer("The Information Age",false);
q.NewAnswer("AIDS",false);
q.NewAnswer("De-industrialization",true);
q = gQuestionList.NewQuestion("Which piece of the equipment that Neil Armstrong wore when he set foot on the moon was made in Worcester?");
q.NewAnswer("His earrings",false);
q.NewAnswer("His space suit",false);
q.NewAnswer("His wristwatch",false);
q.NewAnswer("His headset",true);
q = gQuestionList.NewQuestion("Is it true that there is no manufacturing in Worcester today?");
q.NewAnswer("Yes, hospitals and insurance companies are Worcester's main employers today.",false);
q.NewAnswer("Yes, heavy industry no longer exists in Worcester.",false);
q.NewAnswer("Yes, manufacturing moved out of the city long ago.",false);
q.NewAnswer("No, Worcester's tradition of innovation in manufacturing continues.",true);


// <-- Quiz Source End 
