function CheckForWinner() {
var box = new Array(10);
for (var i = 1, j=0; i < 10; i++, j++) {
	box[i] = window.opener.document.puzzle[j].value;
}

	if (((box[1] == 1)&&(box[2] == 1)&&(box[3] == 1)) ||
		((box[4] == 1)&&(box[5] == 1)&&(box[6] == 1)) ||
		((box[7] == 1)&&(box[8] == 1)&&(box[9] == 1)) ||
		((box[1] == 1)&&(box[4] == 1)&&(box[7] == 1)) ||
		((box[2] == 1)&&(box[5] == 1)&&(box[8] == 1)) ||
		((box[3] == 1)&&(box[6] == 1)&&(box[9] == 1)) ||
		((box[1] == 1)&&(box[5] == 1)&&(box[9] == 1)) ||
		((box[3] == 1)&&(box[5] == 1)&&(box[7] == 1))) {
			document.write('<BODY BACKGROUND="/Images/StarChild/icons/' +
				'level1_background.gif" BGCOLOR="#000000" ' +
				'TEXT="#FFFFFF" LINK="#66FFFF" VLINK="#33FF33" ALINK="#FF0000">\n');
			document.write('<BR><BR>\n');
			document.write ("<CENTER><FONT SIZE=7>\n");
			document.write ("Congratulations!  You win!</P><P>\n");
			document.write ("</CENTER></FONT>\n");
			window.opener.document.puzzle.gameover.value = 1;
	}
	else if (((box[1] == 2)&&(box[2] == 2)&&(box[3] == 2)) ||
		((box[4] == 2)&&(box[5] == 2)&&(box[6] == 2)) ||
		((box[7] == 2)&&(box[8] == 2)&&(box[9] == 2)) ||
		((box[1] == 2)&&(box[4] == 2)&&(box[7] == 2)) ||
		((box[2] == 2)&&(box[5] == 2)&&(box[8] == 2)) ||
		((box[3] == 2)&&(box[6] == 2)&&(box[9] == 2)) ||
		((box[1] == 2)&&(box[5] == 2)&&(box[9] == 2)) ||
		((box[3] == 2)&&(box[5] == 2)&&(box[7] == 2))) {
			document.write('<BODY BACKGROUND="level1_background.gif" BGCOLOR="#000000" ' +
				'TEXT="#FFFFFF" LINK="#66FFFF" VLINK="#33FF33" ALINK="#FF0000">\n');
			document.write('<BR><BR>\n');
			document.write ("<CENTER><FONT SIZE=7>\n");
			document.write ("The alien wins!</P><P>\n");

			document.write ("</CENTER></FONT>\n");
			window.opener.document.puzzle.gameover.value = 1;
	}
	else if (window.opener.document.puzzle.count.value > 8) {
		document.write('<BODY BACKGROUND="/Images/StarChild/icons/' +
			'level1_background.gif" BGCOLOR="#000000" ' +
			'TEXT="#FFFFFF" LINK="#66FFFF" VLINK="#33FF33" ALINK="#FF0000">\n');
		document.write('<BR><BR>\n');
		document.write ("<CENTER><FONT SIZE=7>\n");
		document.write ("It's a tie!</P><P>\n");
		document.write ("</CENTER></FONT>\n");
		window.opener.document.puzzle.gameover.value = 1;
	}
	else {}
}


function rand(topNumber) {
	var randomNumber;

	randomNumber = Math.round((Math.random())*(topNumber-1)) + 1;
	return randomNumber;
}

function CheckAnswer(CheckForWinner, question, answer) {

	var current = window.opener.document.puzzle.currentbox.value;

	var OImage = new Image();
	OImage.src = "/Images/StarChild/solar_system_level1/o.gif";

	var XImage = new Image();
	XImage.src = "/Images/StarChild/solar_system_level1/x.gif";

	var correct = new Array ("", "PLUTO", "MERCURY", "EARTH", "MARS", "MARS", "VENUS", "SATURN", "URANUS", "JUPITER", "MERCURY", "VENUS", "JUPITER", "MARS", "PLUTO", "VENUS", "MERCURY", "SATURN", "PLUTO", "NEPTUNE AND PLUTO", "JUPITER");

	var alternate = "PLUTO AND NEPTUNE";

	var ANSWER = answer.toUpperCase();
	if (ANSWER == "") {
		ANSWER="BLANK";
	}

	document.write('<BODY BACKGROUND="/Images/StarChild/icons/' +
		'level1_background.gif" BGCOLOR="#000000" ' +
		'TEXT="#FFFFFF" LINK="#66FFFF" VLINK="#33FF33" ALINK="#FF0000">\n');
	document.write('<BR><BR>\n');
	var returnValue = 0;
	var  i = 1;
	while (i <= 20) {
		if (question == i) {
			if ((ANSWER == correct[i]) || ((i == 19) && (ANSWER == alternate))) {
				document.write('<CENTER><FONT SIZE=7>\n');
				document.write('You are correct!</P><P>\n');
				document.write('</CENTER></FONT>\n');
				returnValue =  1;
				if (current == 1) {
					window.opener.document.one.src = XImage.src;
					window.opener.document.puzzle.box1.value = 1;
				}
				else if (current ==  2) {
					window.opener.document.two.src = XImage.src;
					window.opener.document.puzzle.box2.value = 1;
				}
				else if (current ==  3) {
					window.opener.document.three.src = XImage.src;
					window.opener.document.puzzle.box3.value = 1;
				}
				else if (current ==  4) {
					window.opener.document.four.src = XImage.src;
					window.opener.document.puzzle.box4.value = 1;
				}
				else if (current ==  5) {
					window.opener.document.puzzle.box5.value = 1;
					window.opener.document.five.src = XImage.src;
				}
				else if (current ==  6) {
					window.opener.document.puzzle.box6.value = 1;
					window.opener.document.six.src = XImage.src;
				}
				else if (current ==  7) {
					window.opener.document.seven.src = XImage.src;
					window.opener.document.puzzle.box7.value = 1;
				}
				else if (current ==  8) {
					window.opener.document.eight.src = XImage.src;
					window.opener.document.puzzle.box8.value = 1;
				}
				else if (current ==  9) {
					window.opener.document.nine.src = XImage.src;
					window.opener.document.puzzle.box9.value = 1;
				}
				else {document.write('Error changing image!\n');}
				i++;
			}
			else {
				document.write('<CENTER><FONT SIZE=7>\n');
				document.write('The correct answer is ' + correct[i] + '\n');
				document.write('</CENTER></FONT>\n');
				returnValue = 0;
				if (current == 1) {
					window.opener.document.one.src = OImage.src;
					window.opener.document.puzzle.box1.value = 2;
				}
				else if (current ==  2) {
					window.opener.document.two.src = OImage.src;
					window.opener.document.puzzle.box2.value = 2;
				}
				else if (current ==  3) {
					window.opener.document.three.src = OImage.src;
					window.opener.document.puzzle.box3.value = 2;
				}
				else if (current ==  4) {
					window.opener.document.four.src = OImage.src;
					window.opener.document.puzzle.box4.value = 2;
				}
				else if (current ==  5) {
					window.opener.document.five.src = OImage.src;
					window.opener.document.puzzle.box5.value = 2;
				}
				else if (current ==  6) {
					window.opener.document.six.src = OImage.src;
					window.opener.document.puzzle.box6.value = 2;
				}
				else if (current ==  7) {
					window.opener.document.seven.src = OImage.src;
					window.opener.document.puzzle.box7.value = 2;
				}
				else if (current ==  8) {
					window.opener.document.eight.src = OImage.src;
					window.opener.document.puzzle.box8.value = 2;
				}
				else if (current ==  9) {
					window.opener.document.nine.src = OImage.src;
					window.opener.document.puzzle.box9.value = 2;
				}
				else {document.write('Error changing image!\n');}
				i++;
			}
		}
		else { i++; }
	}
	window.opener.document.puzzle.count.value++;
	document.write("<CENTER>")
	document.write("<FORM>");
	document.write("<TABLE BORDER=5 CELLPADDING=10>");
	document.write("<TR><TD ALIGN=\"CENTER\">");
	document.write("<INPUT TYPE=button VALUE=\"Close\" onClick=\"window.close()\"></TD></TR>");
	document.write("</TABLE>");
	document.write("</FORM>");
	document.write("</CENTER>");
	CheckForWinner();
	return returnValue;
}

var current = window.opener.document.puzzle.currentbox.value;

var questions = new Array(21);
for (var i = 9, j = 1; i < 29; i++, j++) {
	questions[j] = window.opener.document.puzzle[i].value;
}

var box = new Array(10);
for (var i = 1, j=0; i < 10; i++, j++) {
	box[i] = window.opener.document.puzzle[j].value;
}

questions[0] = 100;

var question = 0;
while (questions[question] > 0) {
	question = rand(20);
}
window.opener.document.puzzle[question + 8].value = 1;

if (window.opener.document.puzzle.gameover.value != 0) {
	document.write('<CENTER>\n');
	document.write('<BR><BR><BR><BR>\n');
	document.write('<FONT SIZE=7>Sorry, the game is over!</P><P>\n');
	document.write('</FONT></CENTER>\n');
	document.write("<CENTER>")
	document.write("<FORM>");
	document.write("<TABLE BORDER=5 CELLPADDING=10>");
	document.write("<TR><TD ALIGN=\"CENTER\">");
	document.write("<INPUT TYPE=button VALUE=\"Close\" onClick=\"window.close()\"></TD></TR>");
	document.write("</TABLE>");
	document.write("</FORM>");
	document.write("</CENTER>");
}

else if (box[current] != 0) {
	document.write('<CENTER>\n');
	document.write('<BR><BR><BR><BR>\n');
	document.write('<FONT SIZE=7>That box has already been chosen.  Please choose another one.\n');
	document.write('</FONT></CENTER>\n');
	document.write("<CENTER>")
	document.write("<FORM>");
	document.write("<TABLE BORDER=5 CELLPADDING=10>");
	document.write("<TR><TD ALIGN=\"CENTER\">");
	document.write("<INPUT TYPE=button VALUE=\"Close\" onClick=\"window.close()\"></TD></TR>");
	document.write("</TABLE>");
	document.write("</FORM>");
	document.write("</CENTER>");
}

else {
document.write('<H1 ALIGN="CENTER">Question</H1>\n');
document.write('<CENTER>\n');
document.write('<FONT SIZE=5>\n');
document.write('<FORM NAME="response" onSubmit="return false;" >\n');
if (question == 1) {
	document.write('Which planet is sometimes called a double planet?</P><P>\n');
}
else if (question == 2) {
	document.write('Which planet is closest to the Sun?</P><P>\n');
}
else if (question == 3) {
	document.write('Name the third closest planet to the Sun.</P><P>\n');
}
else if (question == 4) {
	document.write('Which planet is called the red planet?</P><P>\n');
}
else if (question == 5) {
	document.write('Evidence of ancient bacteria has recently been ');
	document.write('found in rock samples from which planet?</P><P>\n');
}
else if (question == 6) {
      document.write ("What planet rotates in a direction opposite ");
      document.write ("all the others?<P>\n");
   }
   else if (question == 7) {
      document.write ("Which planet is surrounded by over ");
      document.write ("1000 rings?<P>\n");
   }
   else if (question == 8) {
      document.write ("Which planet rotates on its side?<P>\n");
   }
   else if (question == 9) {
      document.write ("Which planet has the Great Red Spot?<P>\n");
   }
   else if (question == 10) {
      document.write ("Which planet is about the same size as our ");
      document.write ("moon<P>\n");
   }
   else if (question == 11) {
      document.write ("Which planet is called the Earth's twin?<P>\n");
   }
   else if (question == 12) {
      document.write ("Io is a moon of which planet?<P>\n");
   }
   else if (question == 13) {
      document.write ("Which planet has two potato shaped moons?<P>\n");
   }
   else if (question == 14) {
      document.write ("What is the farthest known planet from ");
      document.write ("the sun?<P>\n");
   }
   else if (question == 15) {
      document.write ("What planet is often referred to as the ");
      document.write ("morning and evening star?<P>\n");
   }
   else if (question == 16) {
      document.write ("Which planet travels fastest around the ");
      document.write ("sun?<P>\n");
   }
   else if (question == 17) {
      document.write ("Which planet has at least 18 moons?<P>\n");
   }
   else if (question == 18) {
      document.write ("Which planet has a moon almost as big as ");
      document.write ("itself?<P>\n");
   }
   else if (question == 19) {
      document.write ("Which two planets alternate being the farthest ");
      document.write ("planet from the Sun?  Separate your answers ");
      document.write ("with \"and\".<P>\n");
   }
   else if (question == 20) {
      document.write ("Which planet gives off twice as much heat as ");
      document.write ("it gets from the sun?<P>\n");
   }

else {document.write('ERROR\n');}

document.write('<INPUT  TYPE=text NAME="answer" SIZE=20 MAXLENGTH=20></P><P>\n');
document.write('<TABLE BORDER=5 CELLPADDING=10>');
document.write('<TR><TD ALIGN="CENTER">');
document.write('<INPUT TYPE=submit VALUE="Check me!" NAME="CheckButton" onClick="CheckAnswer(CheckForWinner, question, document.response.answer.value);"></P><P>\n');
document.write('</TD></TR>\n</TABLE>\n');
document.write('</FORM>\n');
document.write('</CENTER>\n');
document.write('</FONT>\n');


}

