//
// 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 id="' + this.id + '" type="radio" value="' + this.id + '" name="answers"> ');
	document.write('<span><label for="' + this.id + '">' + this.text + '</label></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('<H3>Q: ' + this.text + '</H3>');
	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)"><p>');
		
	
	if (lastQuestion!=null)
	{
		lastQuestion = parseInt(lastQuestion);
		index = 1 + lastQuestion;
		var answerID = parseInt(QueryString("answers"));
		
		if (this.qList[lastQuestion].answerList.aList[answerID].correct)
		{
			document.write('<H5><font color="red">Correct!</font></h5><p></p>');
			ccount++;
		}
		else
		{
			var correctAnswer = this.qList[lastQuestion].GetCorrectAnswer();
			document.write('<H5><font color="red">That answer was not correct.</font></h5>');
			document.write('<p>The correct answer ');
			document.write('to:  <b>' + this.qList[lastQuestion].text + '</b>');
			document.write('<b><font color="red">  ' + correctAnswer + '</font></b></p><p></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('<h3><b>Congratulations!</b>  You answered ' + ccount + ' items out of ' +
			this.qList.length + ' correctly.</h3>');
		document.write('<h4>Your score is <b>' + score + '%.</b> ' + scoreResults + '</h4>');
		document.write('<p></p>');
		document.write('<img border="0" src="images/skwormdone.gif" alt="SK Worms Says You are Done!" align="right" hspace="6" vspace="3" width="159" height="202">');
		document.write('<h3>Learn More About Conservation!</h3><ul><li><a href="earth_team.html">Volunteer for the Earth Team</a>.</li>');
		document.write('<li><a href="internships.html">Become an Intern</a>.</li><li><a href="jobs.html">Employment Opportunities</a>.</li>');
		document.write('<li><a href="http://www.nrcs.usda.gov/feature/education/squirm/skworm.html">Let S.K. Worm teach you more</a>.</li></ul>');
	}
	
	document.write('<input type="hidden" name="ccount" value="' + ccount + '">')
	document.write('</p></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(20);
	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,"You may want to practice more.");
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,"You may want to practice more.");
gQuestionList.ScoreResults(7,"You may want to practice more.");
gQuestionList.ScoreResults(8,"You may want to practice more.");
gQuestionList.ScoreResults(9,"You may want to practice more.");
gQuestionList.ScoreResults(10,"Not bad.");
gQuestionList.ScoreResults(11,"Good job!");
gQuestionList.ScoreResults(12,"Good job!");
gQuestionList.ScoreResults(13,"Great job!");
gQuestionList.ScoreResults(14,"Great job!");
gQuestionList.ScoreResults(15,"Excellent job!");
gQuestionList.ScoreResults(16,"Excellent job!");
gQuestionList.ScoreResults(17,"Excellent job!");
gQuestionList.ScoreResults(18,"Excellent job!");
gQuestionList.ScoreResults(19,"Excellent job!");

q = gQuestionList.NewQuestion("Soils come in a rainbow of colors.");
q.NewAnswer("True, they can come in black, red, yellow, white, brown, and gray.",true);
q.NewAnswer("False, soil is only brown and black and that's it.",false);

q = gQuestionList.NewQuestion("How much body fat do earth worms have?");
q.NewAnswer("1%.",true);
q.NewAnswer("10%, they're lean!",false);
q.NewAnswer("20%.",false);
q.NewAnswer("About 90%; that's why they're 'plump'!",false);

q = gQuestionList.NewQuestion("The Fox River in Wisconsin actually flows north instead of south, like most other rivers in the U.S.");
q.NewAnswer("True.",true);
q.NewAnswer("False.",false);

q = gQuestionList.NewQuestion("How many counties in the United States have a soil survey?");
q.NewAnswer("100,000",false);
q.NewAnswer("3,000",true);
q.NewAnswer("1,000",false);
q.NewAnswer("Just Dane county, Wisconsin",false);

q = gQuestionList.NewQuestion("Planets like Mars and Venus have soil.");
q.NewAnswer("True, all planets in our solar system have soil.",false);
q.NewAnswer("False, just Earth has soil.",true);

q = gQuestionList.NewQuestion("The perfect soil for most plants and soil organisms is:");
q.NewAnswer("Cement.",false);
q.NewAnswer("Sand.",false);
q.NewAnswer("Silt.",false);
q.NewAnswer("Clay.",false);
q.NewAnswer("Loam.",true);

q = gQuestionList.NewQuestion("One cup of soil can hold how many organisms?");
q.NewAnswer("A couple of really big bugs.",false);
q.NewAnswer("Around 100.",false);
q.NewAnswer("60,000.",false);
q.NewAnswer("6 Billion.",true);


q = gQuestionList.NewQuestion("How long does it take to make an inch of topsoil?");
q.NewAnswer("One day.",false);
q.NewAnswer("Around 12 - 18 months.",false);
q.NewAnswer("A lifetime.",false);
q.NewAnswer("200 to 1,000 years.",true);

q = gQuestionList.NewQuestion("How many gallons of water does it take to produce 1 pound of plant material?");
q.NewAnswer("1 gallon.",false);
q.NewAnswer("10 gallons.",false);
q.NewAnswer("About 25 - 120 gallons.",true);
q.NewAnswer("About 200 gallons.",false);

q = gQuestionList.NewQuestion("The bacteria in an acre of soil (the size of a football field) can weigh as much as:");
q.NewAnswer("A dog.",false);
q.NewAnswer("A couple of people.",false);
q.NewAnswer("A couple of cows.",true);
q.NewAnswer("A tank.",false);

q = gQuestionList.NewQuestion("An acre of corn can give off 4,000 gallons of water per day in evaporation.");
q.NewAnswer("True.",true);
q.NewAnswer("False.",false);

q = gQuestionList.NewQuestion("How fast does water travel up a tree?");
q.NewAnswer("As fast as an airplane.",false);
q.NewAnswer("1 foot per hour.",false);
q.NewAnswer("50 feet per hour.",false);
q.NewAnswer("150 feet per hour.",true);

q = gQuestionList.NewQuestion("Five tons of topsoil spread over an acre is as thick as:");
q.NewAnswer("A hair.",false);
q.NewAnswer("5 mm.",true);
q.NewAnswer("2 inches.",false);
q.NewAnswer("A hand.",false);

q = gQuestionList.NewQuestion("How many flowers must a bee tap to make a pound of honey?");
q.NewAnswer("2 Million.",true);
q.NewAnswer("200,000.",false);
q.NewAnswer("200.",false);
q.NewAnswer("1 big daisey.",false);

q = gQuestionList.NewQuestion("One farmer produces enough food for how many people?");
q.NewAnswer("3.",false);
q.NewAnswer("90.",false);
q.NewAnswer("130.",true);
q.NewAnswer("13 Million.",false);

q = gQuestionList.NewQuestion("Why does a farmer plant cover crops?");
q.NewAnswer("To provide pretty blooms.",false);
q.NewAnswer("To cover ugly land.",false);
q.NewAnswer("To prevent the soil from washing away.",true);
q.NewAnswer("To justify buying more farm equipment.",false);

q = gQuestionList.NewQuestion("Trees create how many of their living parts from scratch each year?");
q.NewAnswer("Just the leaves.",false);
q.NewAnswer("The bark and leaves.",false);
q.NewAnswer("50% of the tree.",false);
q.NewAnswer("99% of the tree.",true);

q = gQuestionList.NewQuestion("How much of Wisconsin's prime farmland is developed every year?");
q.NewAnswer("The size of Marathon county, Wisconsin.",false);
q.NewAnswer("The size of Green Bay, Wisconsin.",false);
q.NewAnswer("About 38 square miles.",true);
q.NewAnswer("About 12.5 square miles.",false);


q = gQuestionList.NewQuestion("How much of America's soil is still at risk of erosion?");
q.NewAnswer("Erosion?  What's erosion?",false);
q.NewAnswer("35%",true);
q.NewAnswer("All of it.",false);
q.NewAnswer("None of it, everything OK.",false);

// <-- Quiz Source End 
