// COPYRIGHT: This page, all contained graphics, and all javascript code was created by John and Elizabeth Stapel at stapel@purplemath.com.  We retain full ownership rights to this page, including the text, graphics, scripts, images, buttons, animations, and phrasing.  You are allowed to link to whichever part of this site, including this quiz, that you like, as long as the ownership of this site is still clearly John and Elizabeth Stapel's.  You may not download this site for presentation as your own material, nor may you mirror this site in any manner.


function CheckAnswers(currentStep)
{
	var IsWrong = CheckForErrors(currentStep);

	if ((!IsWrong) && (currentStep < 7)) NextStep(currentStep);

	return IsWrong;
}

function CheckForErrors(currentStep)
{
	var answerArray = new Array();
	var thisError = true;
	var Error = false;

	switch (currentStep)
	{
		case 1:
			answerArray = CharDigits;
		break;
		case 2:
			answerArray = Digits;
		break;
		case 3:
			answerArray = Orders;
			// check to see if the student started counting from 1 instead of 0
			var i;
			thisError = true;
			for (i=0; (i<(QuestionFrame.document.cloze.elements.length) && thisError); i++)
			{
				InString = TrimString(QuestionFrame.document.cloze.elements[i].value);
				InString = InString.toUpperCase();
				if (InString != (answerArray[QuestionFrame.document.cloze.elements.length-1-i]+1)) thisError = false;
			}
			if (thisError)
			{
				Error = true;
				Feedback = "Whoops!  You need to start counting from 0, not 1.";
			}

			// check to see if the student started counting from 0, but reversed the order of the digits
			thisError = true;
			for (i=0; (i<(QuestionFrame.document.cloze.elements.length) && thisError); i++)
			{
				InString = TrimString(QuestionFrame.document.cloze.elements[i].value);
				InString = InString.toUpperCase();
				if (InString != i) thisError = false;
			}
			if (thisError)
			{
				Error = true;
				Feedback = "Whoops!  You started counting from 0, but you need to have the 0 in the last box.";
			}

			// check to see if the student started counting from 1, reversing the order of the digits
			thisError = true;
			for (i=0; (i<(QuestionFrame.document.cloze.elements.length) && thisError); i++)
			{
				InString = TrimString(QuestionFrame.document.cloze.elements[i].value);
				InString = InString.toUpperCase();
				if (InString != (i+1)) thisError = false;
			}
			if (thisError)
			{
				Error = true;
				Feedback = "Whoops!  You need to start counting from 0, with the 0 in the last box.";
			}

		break;
		case 4:
			answerArray = Powers;
		break;
		case 5:
		{
			answerArray = Products;
			Error = false;

			for (i=0; (i<(QuestionFrame.document.cloze.elements.length) && !Error); i++)
			{
				InString = TrimString(QuestionFrame.document.cloze.elements[i].value);
				InString = InString.toUpperCase();

				if ((InString.length==0) || (InString != answerArray[QuestionFrame.document.cloze.elements.length-1-i]))
				{
					QuestionFrame.document.cloze.elements[i].value = "";
					Error = true;
				}
			}
			if (Error)
			{
				Feedback = "Whoops!  Make sure that you are multiplying the values in first and the third rows.";
			}
		}
		break;
		case 6:
		{
			answerArray[0] = Sum;
			Error = false;

			InString = TrimString(QuestionFrame.document.cloze.elements[0].value);
			InString = InString.toUpperCase();

			if ((InString.length==0) || (InString != answerArray[0]))
			{
				QuestionFrame.document.cloze.elements[0].value = "";
				Error = true;
			}
			if (Error)
			{
				Feedback = "Whoops!  Make sure that you are adding the values in the last row.";
			}
		}
		break;
	}

	var IsWrong = false;
	if (Error)
	{
		if (currentStep == 3)
		{
			for (i=0; i<(QuestionFrame.document.cloze.elements.length); i++)
				QuestionFrame.document.cloze.elements[i].value = '';
		}
		IsWrong = true;
	}
	else
	{

		var x;
		var InString = '';
		var tempString = "";
		var Feedback = '';

		IsWrong = false;

		for (x=0; x<(QuestionFrame.document.cloze.elements.length); x++)
		{
			InString = TrimString(QuestionFrame.document.cloze.elements[x].value);
			InString = InString.toUpperCase();

			if ((InString.length==0) || (InString != answerArray[QuestionFrame.document.cloze.elements.length-1-x]))
			{
				IsWrong = true;
				y = x+1;
				QuestionFrame.document.cloze.elements[x].value = '';
				tempString += "The value in the " + (x+1);
				if (0==x)
					tempString += "'st";
				else if (1==x)
					tempString += "'nd";
				else if (2==x)
					tempString += "'rd";
				else
					tempString += "'th";
				if (currentStep == 4)
				{
					tempString += " spot should be the value of " + Base + "<SUP>" + Orders[Orders.length-1-x] + "</SUP>.<br>"
				}
				else
				{
					tempString += " spot is incorrect.<br>"
				}
			}
		}

		tempString += "<BR>";

		if (IsWrong) 
		{
			Feedback = tempString + Incorrect;
		}
		else 
		{
			Feedback = Correct;
		}
	}

	parent.WriteFeedback(Feedback);
	return IsWrong;
}


function NextStep(currentStep)
{
	var Feedback;
	var error = CheckForErrors(currentStep);

	if (error)
	{
		Feedback = "Before you can proceed, you need to fill in correct values in the boxes.<BR>"
		parent.WriteFeedback(Feedback);
		return;
	}

	if (currentStep == 6)
	{
		WriteLastPage();
	}
	else if (currentStep > 4)
	{
		WriteLastStep();
	}
	else
	{
		WriteNextStep(currentStep);
	}

}


function WriteSetOfBoxes()
{
	var i;
	QuestionFrame.document.write("<TR>");
	for (i=0; i<numDigits; i++)
	{
		QuestionFrame.document.write("<TD WIDTH=50 ALIGN=\"RIGHT\"><input TYPE=\"TEXT\" NAME=\"" + i + "\" SIZE=\"4\"></TD>");
	}
	QuestionFrame.document.writeln("</TR>");
}
function WriteCharDigits(bgColor)
{
	var i;
	if (arguments.length == 0)
	{
		QuestionFrame.document.write("<TR>");
	}
	else
	{
		QuestionFrame.document.write("<TR BGCOLOR=\"" + bgColor + "\">");
	}

	for (i=numDigits-1; i>=0; i--)
	{
		QuestionFrame.document.write("<TD WIDTH=50 ALIGN=\"RIGHT\">" + CharDigits[i] + "</TD>");
	}
	QuestionFrame.document.writeln("</TR>");
}
function WriteDigits(bgColor)
{
	var i;
	if (arguments.length == 0)
	{
		QuestionFrame.document.write("<TR>");
	}
	else
	{
		QuestionFrame.document.write("<TR BGCOLOR=\"" + bgColor + "\">");
	}

	for (i=numDigits-1; i>=0; i--)
	{
		QuestionFrame.document.write("<TD ALIGN=\"RIGHT\">" + Digits[i] + "</TD>");
	}
	QuestionFrame.document.writeln("</TR>");
}
function WriteOrders(bgColor)
{
	var i;
	if (arguments.length == 0)
	{
		QuestionFrame.document.write("<TR>");
	}
	else
	{
		QuestionFrame.document.write("<TR BGCOLOR=\"" + bgColor + "\">");
	}

	for (i=numDigits-1; i>=0; i--)
	{
		QuestionFrame.document.write("<TD ALIGN=\"RIGHT\">" + Orders[i] + "</TD>");
	}
	QuestionFrame.document.writeln("</TR>");
}
function WritePowers(bgColor)
{
	var i;
	if (arguments.length == 0)
	{
		QuestionFrame.document.write("<TR>");
	}
	else
	{
		QuestionFrame.document.write("<TR BGCOLOR=\"" + bgColor + "\">");
	}

	for (i=numDigits-1; i>=0; i--)
	{
		QuestionFrame.document.write("<TD ALIGN=\"RIGHT\">" + Powers[i] + "</TD>");
	}
	QuestionFrame.document.writeln("</TR>");
}
function WriteProducts(bgColor)
{
	var i;
	if (arguments.length == 0)
	{
		QuestionFrame.document.write("<TR>");
	}
	else
	{
		QuestionFrame.document.write("<TR BGCOLOR=\"" + bgColor + "\">");
	}

	for (i=numDigits-1; i>=0; i--)
	{
		QuestionFrame.document.write("<TD ALIGN=\"RIGHT\">" + Products[i] + "</TD>");
	}
	QuestionFrame.document.writeln("</TR>");
}




function WriteNextStep(currentStep)
{
	var nextStep = currentStep+1;
	if ((maxDigit<10) && (nextStep == 2)) nextStep++;

	QuestionFrame.document.clear();
	QuestionFrame.document.open();
	QuestionFrame.document.writeln("<html><head><title> Converting from base " + Base + " to base 10 Quizlet </title></head>");
	QuestionFrame.document.writeln("<body BGCOLOR=\"#FFFFFF\"><h2 align=\"center\"><font face=\"Arial\" size=\"5\" color=\"#800080\"><strong> Converting from base " + Base + " to base 10 </strong></font></h2>");
	QuestionFrame.document.writeln("<form NAME=\"cloze\"><blockquote>");

	QuestionFrame.document.writeln("<p><font face=\"Times New Roman\" color=\"#000000\"><strong>");
	QuestionFrame.document.writeln("We are continuing to convert the number " + numString + ".  So far we have:<BR>");

	QuestionFrame.document.writeln("<CENTER><TABLE BORDER=2>");
	if (currentStep > 0)
	{
		if ((currentStep==4) && (maxDigit<10))
			WriteCharDigits("#99FFFF");
		else
			WriteCharDigits();
	}
	if ((currentStep > 1) && (maxDigit>9))
	{
		if (currentStep==4)
			WriteDigits("#99FFFF");
		else
			WriteDigits();
	}
	if (currentStep > 2)
	{
		if (currentStep==3)
			WriteOrders("#99FFFF");
		else
			WriteOrders();
	}
	if (currentStep > 3)
	{
		if (currentStep==4)
			WritePowers("#99FFFF");
		else
			WritePowers();
	}
	if (currentStep > 4)
	{
		if (currentStep==5)
			WriteProducts("#99FFFF");
		else
			WriteProducts();
	}
	WriteSetOfBoxes();
	QuestionFrame.document.writeln("</TABLE></CENTER>");

	switch (nextStep)
	{
		case 2:
			QuestionFrame.document.writeln("<BR>Fill in the boxes with the numeric value of the character digits and then continue by clicking on a button.<br>");
			break;
		case 3:
			QuestionFrame.document.writeln("<BR>Fill in the boxes by counting down so that 0 ends up in the last box.<BR>");
			QuestionFrame.document.writeln("Hint:It is easier to start by putting a 0 in the rightmost box and then count up as you go to the left.<br>");
			break;
		case 4:
			QuestionFrame.document.writeln("<BR>Fill in the boxes with the decimal number equal to the base raised to the exponent in the previous row and then continue by clicking on a button.<br>");
			QuestionFrame.document.writeln("<BR>For example, " + Base + "<SUP>" + Orders[Orders.length-1] + "</SUP> = " + Powers[Powers.length-1] + ", so " + Powers[Powers.length-1] + " would go in the first box.<br>");
			break;
		case 5:
			if (maxDigit>9)
				QuestionFrame.document.writeln("<BR>Fill in the boxes with the product of the numbers in the second and fourth rows and then continue by clicking on a button.<br>");
			else
				QuestionFrame.document.writeln("<BR>Fill in the boxes with the product of the numbers in the first and third rows and then continue by clicking on a button.<br>");
			break;
	}

	QuestionFrame.document.writeln("</strong></font></p></blockquote></form>");

	QuestionFrame.document.writeln("<form NAME=\"NavigationButtons\"><div align=\"center\"><center><table>");
	QuestionFrame.document.writeln("<tr><td colspan=\"4\" width=\"435\" height=\"21\" bgcolor=\"#FFFFFF\"><hr width=\"80%\" align=\"center\"></td></tr>");
	QuestionFrame.document.writeln("<tr><td>&nbsp;</td></tr>");

	QuestionFrame.document.writeln("<TR><TD><input TYPE=\"button\" VALUE=\"Check Answers\" onClick=\"parent.CheckAnswers(" + nextStep + ")\"> </TD>");
	QuestionFrame.document.writeln("<TD><input TYPE=\"button\" VALUE=\"Go to next Step\" onClick=\"parent.NextStep(" + (nextStep) + ")\"> </TD>");
	QuestionFrame.document.writeln("<TD><input TYPE=\"button\" VALUE=\"Another Question\" onClick=\"parent.checkRequired()\"> </TD>");
	QuestionFrame.document.writeln("<TD><input TYPE=\"button\" VALUE=\"Go back to the lesson\"  onClick=\"parent.location.href='numbbase.htm#base" + origBase + "to10'\"> </TD></TR>");

	QuestionFrame.document.writeln("</table></center></div></form></body></html>");
	QuestionFrame.document.close();

	if (nextStep == 2)
	{
		for (i=0; i<numDigits; i++)
			if (Digits[i]<10)
				QuestionFrame.document.cloze.elements[numDigits-1-i].value = Digits[i];
	}

}

function WriteLastStep()
{
	QuestionFrame.document.clear();
	QuestionFrame.document.open();
	QuestionFrame.document.writeln("<html><head><title> Converting from base " + Base + " to base 10 Quizlet </title></head>");

	QuestionFrame.document.writeln("<script LANGUAGE=\"JavaScript\"> var clickedButton = false;");
	QuestionFrame.document.writeln("function check(){if(clickedButton){clickedButton = false;return true;}else return false;}");
	QuestionFrame.document.writeln("</script>");

	QuestionFrame.document.writeln("<body BGCOLOR=\"#FFFFFF\"><h2 align=\"center\"><font face=\"Arial\" size=\"5\" color=\"#800080\"><strong> Converting from base " + Base + " to base 10 </strong></font></h2>");
	QuestionFrame.document.writeln("<form NAME=\"cloze\" onSubmit=\"return check()\"><blockquote>");

	QuestionFrame.document.writeln("<p><font face=\"Times New Roman\" color=\"#000000\"><strong>");
	QuestionFrame.document.writeln("<CENTER>We have created the following table:<BR>");

	QuestionFrame.document.writeln("<CENTER><TABLE BORDER=2>");
	WriteCharDigits();
	if (maxDigit>9)
		WriteDigits();
	WriteOrders();
	WritePowers();
	WriteProducts("#99FFFF");
	QuestionFrame.document.writeln("</TABLE>");

	QuestionFrame.document.writeln("<p>&nbsp;</p>Now add up the numbers in the last row to get the decimal representation of " + numString + "<BR>");

	QuestionFrame.document.writeln("<BR>Fill in the following box and then continue by clicking on a button.<br>");
	QuestionFrame.document.writeln("<TABLE><TR><TD>" + numString + " = </TD><TD><input TYPE=\"TEXT\" NAME=\"0\" SIZE=\"8\"></TD></TR></TABLE></CENTER>");

	QuestionFrame.document.writeln("<BR></FONT></strong></font></p></blockquote></form>");

	QuestionFrame.document.writeln("<form NAME=\"NavigationButtons\"><div align=\"center\"><center><table>");
	QuestionFrame.document.writeln("<tr><td colspan=\"4\" width=\"435\" height=\"21\" bgcolor=\"#FFFFFF\"><hr width=\"80%\" align=\"center\"></td></tr>");
	QuestionFrame.document.writeln("<tr><td>&nbsp;</td></tr>");

	QuestionFrame.document.writeln("<TR><TD><input TYPE=\"button\" VALUE=\"Check Answers\" onClick=\"parent.CheckAnswers(6)\"> </TD>");
	QuestionFrame.document.writeln("<TD><input TYPE=\"button\" VALUE=\"Another Question\" onClick=\"parent.checkRequired()\"> </TD>");
	QuestionFrame.document.writeln("<TD><input TYPE=\"button\" VALUE=\"Go back to the lesson\"  onClick=\"parent.location.href='numbbase.htm#base" + origBase + "to10'\"> </TD></TR>");

	QuestionFrame.document.writeln("</table></center></div></form></body></html>");
	QuestionFrame.document.close();

}

function WriteLastPage()
{
	QuestionFrame.document.clear();
	QuestionFrame.document.open();
	QuestionFrame.document.writeln("<html><head><title> Converting from base " + Base + " to base 10 Quizlet </title></head>");

	QuestionFrame.document.writeln("<body BGCOLOR=\"#FFFFFF\"><h2 align=\"center\"><font face=\"Arial\" size=\"5\" color=\"#800080\"><strong> Converting from base " + Base + " to base 10 </strong></font></h2>");
	QuestionFrame.document.writeln("<form NAME=\"cloze\" onSubmit=\"return check()\"><blockquote>");

	QuestionFrame.document.writeln("<p><font face=\"Times New Roman\" color=\"#000000\"><strong>");
	QuestionFrame.document.writeln("<CENTER>We have created the following table:<BR>");

	QuestionFrame.document.writeln("<CENTER><TABLE BORDER=2>");
	WriteCharDigits();
	if (maxDigit>9)
		WriteDigits();
	WriteOrders();
	WritePowers();
	WriteProducts("#99FFFF");
	QuestionFrame.document.writeln("</TABLE>");

	QuestionFrame.document.writeln("<p>&nbsp;</p>Having added up the numbers in the last row, we now have that " + numString + " = " + Sum + ".<BR>");

	QuestionFrame.document.writeln("<BR></FONT></strong></font></p></blockquote></form>");

	QuestionFrame.document.writeln("<form NAME=\"NavigationButtons\"><div align=\"center\"><center><table>");
	QuestionFrame.document.writeln("<tr><td colspan=\"4\" width=\"435\" height=\"21\" bgcolor=\"#FFFFFF\"><hr width=\"80%\" align=\"center\"></td></tr>");
	QuestionFrame.document.writeln("<tr><td>&nbsp;</td></tr>");

	QuestionFrame.document.writeln("<TD><input TYPE=\"button\" VALUE=\"Another Question\" onClick=\"parent.checkRequired()\"> </TD>");
	QuestionFrame.document.writeln("<TD><input TYPE=\"button\" VALUE=\"Go back to the lesson\"  onClick=\"parent.location.href='numbbase.htm#base" + origBase + "to10'\"> </TD></TR>");

	QuestionFrame.document.writeln("</table></center></div></form></body></html>");
	QuestionFrame.document.close();

}

function WriteFeedback(Feedback) 
{
	FeedbackFrame.document.clear();
	FeedbackFrame.document.open();
	FeedbackFrame.document.writeln ("<html>");
	FeedbackFrame.document.writeln ("<body bgcolor=#FFFFFF text=#BA050A>");
	FeedbackFrame.document.writeln ("<font size = +1><center><b>");
	FeedbackFrame.document.writeln (Feedback);
	FeedbackFrame.document.writeln ("</b></center></font></body></html>");
	FeedbackFrame.document.close();
}


