// 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 CheckForErrors()
{
	var InNewNum = Number(TrimString(QuestionFrame.document.cloze.elements[0].value));
	var InNewRem = Number(TrimString(QuestionFrame.document.cloze.elements[1].value));
	var oldNumber = Numbers[Numbers.length-1];
	var correctRemainder = oldNumber % Base
	var correctNumber = (oldNumber - correctRemainder) / Base;


	if ((NaN == InNewNum) || (TrimString(QuestionFrame.document.cloze.elements[0].value).length == 0))
	{
		return (1);
	}
	else if ((NaN == InNewRem) || (TrimString(QuestionFrame.document.cloze.elements[1].value).length == 0))
	{
		return (2);
	}
	else if ((InNewRem >= Base) || (InNewRem < 0))
	{
		return (3);
	}
	else if ((InNewNum*Base + InNewRem) == oldNumber)
	{
		return (4);
	}
	else if (InNewNum == correctNumber)
	{
		return (5);
	}
	else
	{
		return (6);
	}
}


function CheckAllFinalAnswers()
{
	var answer;
	var correctAnswer;
	var allCorrect = true;

	var Feedback = "";

	ansArray = new Array();
	correctAnswers = new Array();

	var i, j;

	correctAnswers[0] = Numbers[Numbers.length-1];
	if (correctAnswers[0] == 10) correctAnswers[0] = "A";
	if (correctAnswers[0] == 11) correctAnswers[0] = "B";
	if (correctAnswers[0] == 12) correctAnswers[0] = "C";
	if (correctAnswers[0] == 13) correctAnswers[0] = "D";
	if (correctAnswers[0] == 14) correctAnswers[0] = "E";
	if (correctAnswers[0] == 15) correctAnswers[0] = "F";
	ansArray[0] = (MaxRemainder>9)?QuestionFrame.document.cloze.elements[0].value:correctAnswers[0];
	if (ansArray[0] == "a") ansArray[0] = "A";
	if (ansArray[0] == "b") ansArray[0] = "B";
	if (ansArray[0] == "c") ansArray[0] = "C";
	if (ansArray[0] == "d") ansArray[0] = "D";
	if (ansArray[0] == "e") ansArray[0] = "E";
	if (ansArray[0] == "f") ansArray[0] = "F";

	for (j=1, i=Remainders.length-1; i>0; i--, j++)
	{
		correctAnswers[j] = Remainders[i];
		if (correctAnswers[j] == 10) correctAnswers[j] = "A";
		if (correctAnswers[j] == 11) correctAnswers[j] = "B";
		if (correctAnswers[j] == 12) correctAnswers[j] = "C";
		if (correctAnswers[j] == 13) correctAnswers[j] = "D";
		if (correctAnswers[j] == 14) correctAnswers[j] = "E";
		if (correctAnswers[j] == 15) correctAnswers[j] = "F";
		ansArray[j] = (MaxRemainder>9)?QuestionFrame.document.cloze.elements[j].value:correctAnswers[i];
		if (ansArray[j] == "a") ansArray[j] = "A";
		if (ansArray[j] == "b") ansArray[j] = "B";
		if (ansArray[j] == "c") ansArray[j] = "C";
		if (ansArray[j] == "d") ansArray[j] = "D";
		if (ansArray[j] == "e") ansArray[j] = "E";
		if (ansArray[j] == "f") ansArray[j] = "F";
	}


	if (MaxRemainder>9)
	{
		for (i=0; i<ansArray.length; i++)
		{
			if (ansArray[i] != correctAnswers[i])
			{
				var tempString = "The value in the " + (i+1);
				if (0==i)
					tempString += "'st";
				else if (1==i)
					tempString += "'nd";
				else if (2==i)
					tempString += "'rd";
				else
					tempString += "'th";

				tempString += " spot is incorrect.<br>"
				Feedback += tempString;
				QuestionFrame.document.cloze.elements[i].value = '';
				allCorrect = false;
			}
		}
	}

	var NumBoxes = (MaxRemainder>9)?1+Remainders.length:1;
	answer = " " + QuestionFrame.document.cloze.elements[NumBoxes-1].value;
	correctAnswer = " ";
	var i;
	for (i=0; i<Remainders.length; i++)
	{
		correctAnswer += correctAnswers[i];
	}

	answer = TrimString(answer)
	answer = answer.toUpperCase();
	correctAnswer = TrimString(correctAnswer);

	correct = (answer == correctAnswer);
	if (correct && allCorrect)
	{
		WriteLastPage(correctAnswer, correctAnswers);
		Feedback = "Correct, " + Numbers[0] + " = " + correctAnswer + "<SUB>" + Base + "</SUB><BR>";
		WriteFeedback(Feedback);
	}
	else if (MaxRemainder>9)
	{
		var tempString = "The value in the " + NumBoxes;
		if (1==NumBoxes)
			tempString += "'st";
		else if (2==NumBoxes)
			tempString += "'nd";
		else if (3==NumBoxes)
			tempString += "'rd";
		else
			tempString += "'th";

		tempString += " spot is incorrect.<br>"
		Feedback += tempString;
		QuestionFrame.document.cloze.elements[NumBoxes-1].value = '';
	}
	else
	{
		Feedback = "Sorry, that value is not correct.<br>";
		QuestionFrame.document.cloze.elements[NumBoxes-1].value = '';
	}

	WriteFeedback(Feedback);
}




function CheckAllAnswers()
{
	var Feedback;
	var InFactor = 0;
	var NextNumber = 0;

	var errorCode = CheckForErrors();

	switch (errorCode)
	{
		case 1:
			QuestionFrame.document.cloze.elements[0].value = '';
			Feedback = "Whoops!  Please enter a number in the first box.<BR>";
		break;
		case 2:
			QuestionFrame.document.cloze.elements[1].value = '';
			Feedback = "Whoops!  Please enter a number in the second box.<BR>";
		break;
		case 3:
			QuestionFrame.document.cloze.elements[1].value = '';
			Feedback = Incorrect + " " + "The remainder must be a number between 0 and " + (Base-1) + ".  Please reenter a value in the second box.<BR>";
		break;
		case 4:
			NextNumber = Number(TrimString(QuestionFrame.document.cloze.elements[0].value));
			if (NextNumber > Base)
			{
				Feedback = Correct + " " + "Now do the next division.<BR>";
			}
			else if (MaxRemainder>9)
			{
				Feedback = Correct + " " + "Now enter character codes for each remainder.<BR>";
			}
			else
			{
				Feedback = Correct + " " + "Now write the base " + Base + " representation of " + Numbers[0] + ".<BR>";
			}
			NextStep();
		break;
		case 5:
			QuestionFrame.document.cloze.elements[1].value = '';
			Feedback = Incorrect + " " + "You have the correct divisor, but an incorrect remainder.  Please reenter the value in the second box.<BR>";
		break;
		case 6:
			QuestionFrame.document.cloze.elements[0].value = '';
			QuestionFrame.document.cloze.elements[1].value = '';
			Feedback = Incorrect + " " + "You have divided incorrectly.  Please reenter values in the two boxes.<BR>";
		break;
	}

	parent.WriteFeedback(Feedback);
}

function NextStep()
{
	var Feedback;
	var errorCode = CheckForErrors();

	if (errorCode != 4) // no error
	{
		Feedback = "Before you can proceed, you need to fill in correct values in the two boxes.<BR><BR>"
		parent.WriteFeedback(Feedback);
		return;
	}

	var InNewNum = Number(TrimString(QuestionFrame.document.cloze.elements[0].value));
	var InNewRem = Number(TrimString(QuestionFrame.document.cloze.elements[1].value));

	Numbers[Numbers.length]       = InNewNum;
	Remainders[Remainders.length] = InNewRem;

	if (InNewRem>MaxRemainder) MaxRemainder = InNewRem;

	if (InNewNum < Base)
	{
		if (InNewNum>MaxRemainder) MaxRemainder = InNewNum;
		WriteLastStep();
	}
	else
	{
		WriteNextStep();
	}

}


function WriteDivisionTable(anotherStep)
{
	var i;
	var numDigits = 2;
	var number = Numbers[Numbers.length-1];
	while (number>=10)
	{
		numDigits++;
		number /= 10;
	}


	QuestionFrame.document.writeln("<CENTER><TABLE cellspacing=0 cellpadding=0>");

	if (anotherStep)
	{
		QuestionFrame.document.writeln("<TR><TD></TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD><FONT SIZE=4>&nbsp;<input TYPE=\"TEXT\" NAME=\"0\" SIZE=\"" + numDigits + "\"></FONT></TD>");
		QuestionFrame.document.writeln("<TD><FONT SIZE=5>&nbsp;R&nbsp;</FONT></TD><TD><FONT SIZE=4><input TYPE=\"TEXT\" NAME=\"1\" SIZE=\"3\"></FONT></TD></TR>");
		QuestionFrame.document.writeln("<TR><TD></TD><TD></TD><TD></TD><TD><HR noshade=\"black\"></TD><TD></TD><TD></TD></TR>");
	}


	i=Numbers.length-1;
	if (anotherStep)
	{
		if (i>0)
		{
			QuestionFrame.document.writeln("<TR><TD><FONT SIZE=5>" + Base + "</FONT></TD><TD></TD><TD></TD><TD><U><FONT SIZE=5>|&nbsp;" + Numbers[i]);
			QuestionFrame.document.writeln("</FONT></U></TD><TD><FONT SIZE=5>&nbsp;R&nbsp;</FONT></TD><TD><FONT SIZE=5>" + Remainders[i] + "</FONT></TD></TR>");
		}
	}
	else
	{
		QuestionFrame.document.writeln("<TR><TD></TD><TD></TD><TD></TD><TD><U><FONT SIZE=5>&nbsp;" + Numbers[i]);
		QuestionFrame.document.writeln("</FONT></U></TD><TD><FONT SIZE=5>&nbsp;R&nbsp;</FONT></TD><TD><FONT SIZE=5>" + Remainders[i] + "</FONT></TD></TR>");
	}

	for (i=Numbers.length-2; i>0; i--)
	{
		QuestionFrame.document.writeln("<TR><TD><FONT SIZE=5>" + Base + "</FONT></TD><TD></TD><TD></TD><TD><U><FONT SIZE=5>|&nbsp;" + Numbers[i]);
		QuestionFrame.document.writeln("</FONT></U></TD><TD><FONT SIZE=5>&nbsp;R&nbsp;</FONT></TD><TD><FONT SIZE=5>" + Remainders[i] + "</FONT></TD></TR>");
	}

	QuestionFrame.document.writeln("<TR><TD><FONT SIZE=5>" + Base + "</FONT></TD><TD></TD><TD></TD><TD><FONT SIZE=5>|&nbsp;" + Numbers[0] + "</FONT></TD><TD></TD><TD></TD></TR>");

	QuestionFrame.document.writeln("</TABLE></CENTER>");
}


function WriteNextStep()
{
	QuestionFrame.document.clear();
	QuestionFrame.document.open();
	QuestionFrame.document.writeln("<html><head><title> Converting from base 10 to base " + Base + " Quizlet </title></head>");
	QuestionFrame.document.writeln("<body BGCOLOR=\"#FFFFFF\"><h2 align=\"center\"><font face=\"Arial\" size=\"5\" color=\"#800080\"><strong> Converting from base 10 to base " + Base + " Quizlet </strong></font></h2>");
	QuestionFrame.document.writeln("<form></form><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 " + Numbers[0] + ".  So far we have:<BR>");

	WriteDivisionTable(true);

	QuestionFrame.document.writeln("<BR>Fill in the two boxes with the result of the division and the remainder and then continue by clicking on a button.<br>");

	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.CheckAllAnswers()\"> </TD>");
	QuestionFrame.document.writeln("<TD><input TYPE=\"button\" VALUE=\"Go to next Step\" onClick=\"parent.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#base10to" + origBase + "'\"> </TD></TR>");

	QuestionFrame.document.writeln("</table></center></div></form></body></html>");
	QuestionFrame.document.close();

	// also clear out the feedback page...
	WriteFeedback("");

}



function WriteLastStep()
{
	QuestionFrame.document.clear();
	QuestionFrame.document.open();
	QuestionFrame.document.writeln("<html><head><title> Converting from base 10 to base " + Base + " 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 10 to base " + Base + " Quizlet </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("We have completed the division process for " + Numbers[0] + " by creating the following table:<BR>");

	WriteDivisionTable(false);

	var NextBoxNum = 0;
	if (MaxRemainder>9)
	{
		QuestionFrame.document.writeln("Before you can write this number in final form, you must convert the greater-than-nine \"digits\" to their corresponding alphabetic digits:");

		QuestionFrame.document.writeln("<CENTER><TABLE>");

		QuestionFrame.document.writeln("<TR><TD><FONT SIZE=5>" + Numbers[Numbers.length-1] + "</FONT></TD><TD><FONT SIZE=4><input TYPE=\"TEXT\" NAME=\"0\" SIZE=\"3\"></FONT></TD></TR>");
		NextBoxNum = 1;
		var i;
		for (i=Remainders.length-1; i>0; i--)
		{
			QuestionFrame.document.writeln("<TR><TD><FONT SIZE=5>" + Remainders[i] + "</FONT></TD><TD><FONT SIZE=4><input TYPE=\"TEXT\" NAME=\"" + NextBoxNum + "\" SIZE=\"3\"></FONT></TD></TR>");
			NextBoxNum++;
		}
		QuestionFrame.document.writeln("</TABLE></CENTER><BR>");
	}

	QuestionFrame.document.writeln("<CENTER><BR>Fill in the following box with the base " + Base + " representation of " + Numbers[0] + " and then continue by clicking on a button.<br>");
	QuestionFrame.document.writeln("<input TYPE=\"TEXT\" NAME=\"" + NextBoxNum + "\" SIZE=\"8\"><SUB>" + Base + "</SUB>");

	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.CheckAllFinalAnswers()\"> </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#base10to" + origBase + "'\"> </TD></TR>");

	QuestionFrame.document.writeln("</table></center></div></form></body></html>");
	QuestionFrame.document.close();

	if (MaxRemainder>9)
	{
		if (Numbers[Numbers.length-1] < 10) 
			QuestionFrame.document.cloze.elements[0].value = Numbers[Numbers.length-1];
		for (i=1; i<Remainders.length; i++)
		{
			if (Remainders[i] < 10) 
				QuestionFrame.document.cloze.elements[Remainders.length-i].value = Remainders[i];
		}
	}

	// also clear out the feedback page...
	WriteFeedback("");

}




function WriteLastPage(correctAnswer, correctAnswers)
{
	var NumBoxes = (MaxRemainder>9)?1+Remainders.length:1;
	var i, j;

	QuestionFrame.document.clear();
	QuestionFrame.document.open();
	QuestionFrame.document.writeln("<html><head><title> Converting from base 10 to base " + Base + " Quizlet </title></head>");

	QuestionFrame.document.writeln("<body BGCOLOR=\"#FFFFFF\"><h2 align=\"center\"><font face=\"Arial\" size=\"5\" color=\"#800080\"><strong> Converting from base 10 to base " + Base + " Quizlet </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("We have completed the division process for " + Numbers[0] + " by creating the following table:<BR>");

	WriteDivisionTable(false);

	var NextBoxNum = 0;
	if (MaxRemainder>9)
	{
		QuestionFrame.document.writeln("<BR>Correct!  After converting the greater-than-nine \"digits\" to their corresponding alphabetic digits ");
		QuestionFrame.document.writeln("the base " + Base + " representation of " + Numbers[0] + " is " + correctAnswer + "<SUB>" + Base + "</SUB>");
	}
	else
	{
		QuestionFrame.document.writeln("<BR>Correct!  The base " + Base + " representation of " + Numbers[0] + " is " + correctAnswer + "<SUB>" + Base + "</SUB>");
	}

	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#base10to" + origBase + "'\"> </TD></TR>");

	QuestionFrame.document.writeln("</table></center></div></form></body></html>");
	QuestionFrame.document.close();

	// also clear out the feedback page...
	var Feedback = "Correct, " + Numbers[0] + " = " + correctAnswer + "<SUB>" + Base + "</SUB><BR>";
	WriteFeedback(Feedback);
}

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();
}


