function CheckForWinner() {
  var box = new Array(10);
  for (var i = 1, j=0; i < 10; i++, j++) {
    box[i] = 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))) {
    alert ("\n\n_______________________________     \n\n" + 
	   "    Congratulations!  You win!\n\n" +
	   "_______________________________     \n\n");
    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))) {
    alert ("\n\n_______________________________     \n\n" + 
	   "         The alien wins!\n\n" +
	   "_______________________________     \n\n");
    document.puzzle.gameover.value = 1;
  }
  else if (document.puzzle.count.value > 8) {
    alert ("\n\n_______________________________     \n\n" + 
	   "           It's a tie!\n\n" +
	   "_______________________________     \n\n");
    document.puzzle.gameover.value = 1;
  }
  else {}
}


function CheckAnswer(question, answer) {

  var current = document.puzzle.currentbox.value;

  var OImage = new Image();
  OImage.src = "images/o.gif";

  var XImage = new Image();
  XImage.src = "images/x.gif";

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

  var alternate = "URANUS AND NEPTUNE";

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

  var returnValue = 0;
  var  i = 1;
  while (i <= 24) {
    if (question == i) {
      if ((ANSWER == correct[i]) || ((i == 15) && (ANSWER == alternate))) {
	alert("\n\n_______________________________     \n\n" + 
	      "       You are correct!\n\n" +
	      "_______________________________     \n\n");
	returnValue =  1;
	if (current == 1) {
	  document.one.src = XImage.src;
	  document.puzzle.box1.value = 1;
	}
	else if (current ==  2) {
	  document.two.src = XImage.src;
	  document.puzzle.box2.value = 1;
	}
	else if (current ==  3) {
	  document.three.src = XImage.src;
	  document.puzzle.box3.value = 1;
	}
	else if (current ==  4) {
	  document.four.src = XImage.src;
	  document.puzzle.box4.value = 1;
	}
	else if (current ==  5) {
	 document.puzzle.box5.value = 1;
	  document.five.src = XImage.src;
	}
	else if (current ==  6) {
	  document.puzzle.box6.value = 1;
	  document.six.src = XImage.src;
	}
	else if (current ==  7) {
	  document.seven.src = XImage.src;
	  document.puzzle.box7.value = 1;
	}
	else if (current ==  8) {
	  document.eight.src = XImage.src;
	  document.puzzle.box8.value = 1;
	}
	else if (current ==  9) {
	  document.nine.src = XImage.src;
	  document.puzzle.box9.value = 1;
	}
	else {alert("\n\n_______________________________     \n\n" + 
		    "       Error changing image!\n\n" +
		    "_______________________________     \n\n");}
	i++;
      }
      else {
	alert("\n\n_______________________________     \n\n" + 
	      "The correct answer is " + correct[i] + "\n\n" +
	      "_______________________________     \n\n");
	returnValue = 0;
	if (current == 1) {
	  document.one.src = OImage.src;
	  document.puzzle.box1.value = 2;
	}
	else if (current ==  2) {
	  document.two.src = OImage.src;
	  document.puzzle.box2.value = 2;
	}
	else if (current ==  3) {
	  document.three.src = OImage.src;
	  document.puzzle.box3.value = 2;
	}
	else if (current ==  4) {
	  document.four.src = OImage.src;
	  document.puzzle.box4.value = 2;
	}
	else if (current ==  5) {
	  document.five.src = OImage.src;
	  document.puzzle.box5.value = 2;
	}
	else if (current ==  6) {
	  document.six.src = OImage.src;
	  document.puzzle.box6.value = 2;
	}
	else if (current ==  7) {
	  document.seven.src = OImage.src;
	  document.puzzle.box7.value = 2;
	}
	else if (current ==  8) {
	  document.eight.src = OImage.src;
	  document.puzzle.box8.value = 2;
	}
	else if (current ==  9) {
	  document.nine.src = OImage.src;
	  document.puzzle.box9.value = 2;
	}
	else {alert("\n\n_______________________________     \n\n" + 
		    "       Error changing image!\n\n" +
		    "_______________________________     \n\n");}
	i++;
      }
    }
    else { i++; }
  }
  document.puzzle.count.value++;
  CheckForWinner();
  //  refresh();
  return returnValue;
}

function rand(topNumber) {
  var randomNumber;

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

function GetQuestion() {

  var current = document.puzzle.currentbox.value;
  //	alert ("current: " + current);

  var questions = new Array(25);
  for (var i = 9, j = 1; i < 33; i++, j++) {
    questions[j] = document.puzzle[i].value;
    //	  alert ("questions[" + j + "]: " + questions[j] + "\n" +
    //		 "document.puzzle[" + i + "].value: " + document.puzzle[i].value + "\n");
  }

  var box = new Array(10);
  for (var i = 1, j=0; i < 10; i++, j++) {
    box[i] = document.puzzle[j].value;
    //	  alert ("box[" + i + "]: " + box[i] + "\n" +
    //		 "document.puzzle[" + j + "].value: " + document.puzzle[j].value + "\n");
  }

  questions[0] = 100;

  var question = 0;
  var answer = "";

  while (questions[question] > 0) {
    question = rand(24);
  }

  document.puzzle[question + 8].value = 1;

  if (document.puzzle.gameover.value != 0) {
    alert("\n\n_______________________________     \n\n" + 
	  "     Sorry, the game is over!\n\n" +
	  "_______________________________     \n\n");
  }

  else if (box[current] != 0) {
    alert("\n\n____________________________________     \n\n" + 
	  "   That box has already been chosen.\n" +
	  "   Please choose another one.\n\n" +
	  "____________________________________     \n\n");
    //	  alert("box[" + current + "]: " + box[current] + "\n");
  }

  else {
    if (question == 1) {
      answer = prompt("Which planet is closest to the Sun?","");
    }
    else if (question == 2) {
      answer = prompt("Name the third closest planet to the Sun.","");
    }
    else if (question == 3) {
      answer = prompt("Which planet is called the red planet?","");
    }
    else if (question == 4) {
      answer = prompt("Man-made rovers discovered that liquid water had once been " +
		      "plentiful on the surface of which planet?","");
    }
    else if (question == 5) {
      answer = prompt("Which planet has the hottest surface temperature?","");
    }
    else if (question == 6) {
      answer = prompt("Which planet is surrounded by over " +
		      "1000 rings?","");
    }
    else if (question == 7) {
      answer = prompt("Which planet appears to rotates on its side?","");
    }
    else if (question == 8) {
      answer = prompt("Which planet has the Great Red Spot?","");
    }
    else if (question == 9) {
      answer = prompt("Which planet is about the same size as our " +
		      "Moon","");
    }
    else if (question == 10) {
      answer = prompt("Which planet is called the Earth\'s twin?","");
    }
    else if (question == 11) {
      answer = prompt("Io is a moon of which planet?","");
    }
    else if (question == 12) {
      answer = prompt("Which planet has two potato shaped moons?","");
    }
    else if (question == 13) {
      answer = prompt("What planet is often referred to as the " +
		      "morning and evening star?","");
    }
    else if (question == 14) {
      answer = prompt("Which planet travels fastest around the " +
		      "sun?","");
    }
    else if (question == 15) {
      answer = prompt("Which two gas giants look blue in color?  " +
		      "Separate your answers with \"and\".","");
    }
    else if (question == 16) {
      answer = prompt("Which planet is the farthest " +
		      "planet from the Sun?","");
    }
    else if (question == 17) {
      answer = prompt("Which planet gives off twice as much heat as " +
		      "it gets from the sun?","");
    }
   else if (question == 18) {
      answer = prompt ("On which planet have we measured the fastest  " +
		       "\(2000 km/hr\) winds?","");
   }
   else if (question == 19) {
      answer = prompt ("On which planet's surface does liquid water " +
		       "exist today?","");
   }
   else if (question == 20) {
      answer = prompt ("Which planet has a moon called Titan?","");
   }
   else if (question == 21) {
      answer = prompt ("Which planet do you live on?","");
   }
   else if (question == 22) {
      answer = prompt ("Which planet is the second largest in our  " +
		       "solar system?","");
   }
   else if (question == 23) {
      answer = prompt ("Which planet is the smallest of the gas  " +
		       "giant planets?","");
   }
   else if (question == 24) {
      answer = prompt ("Which planet has 11 known rings?","");
   }

    else {
      alert("Error");
    }
    CheckAnswer(question,answer);
  }
}

function reset() {
  location = "planet_tac_toe.html";
}

