function Game(players,possibleAnswers,strictPossibility){
   this.players = players;
   this.possibleAnswers = possibleAnswers;
   this.strictPossibility = strictPossibility; 
   this.playerList = new Array();
   this.makePlayerList = makePlayerList;
   this.startGame = startGame;
   this.resetGame = resetGame;
   this.gameRound = 1;
   this.answer;
   this.showScores = showScores;
   this.addRound = addRound;
   this.displayAnswers = displayAnswers;
   this.showGameRound=showGameRound;
 }
 
    function displayAnswers(){
    output = '';

    for(var x=0;x<this.possibleAnswers;x++){
	   nextNum = x+1;
	   output+='<label for="answer'+nextNum+'">Answer '+nextNum+'<input type="radio" name="myAnswer" value="'+nextNum+'" id="answer'+nextNum+'" class="radioAnswers"/></label>';
       if(x+1 == (this.possibleAnswers / 2)){
	    output += '<br/>';
	   }
	
	}

	
    document.getElementById("answerList").innerHTML = output;

   }
 
 function addRound(){
   this.gameRound++;
 
 }
 
 function makePlayerList(){
   expectedPercentage = Math.round(((1/this.possibleAnswers) * 100));
   output = '<table border="1" class="resultTable"><caption>Game Answer Results</caption><tr><th>Player Name</th><th>Hits</th><th>Misses</th><th>Hit Percent</th><th>Last Answer</th></tr><tr><td colspan="3" style="text-align:left">Game</td><td id="hitPercent" class="lastAnswer expected">'+expectedPercentage+'%</td><td id="lastAnswer" class="lastAnswer"></td></tr>';
   for(var x=0;x<this.players;x++){
    theName = 'Player '+(x+1);
    this.playerList[x] = new Player(theName,0,0,0);
	output += this.playerList[x].displayScore();
   }
	output += player.displayScore();
	output += '</table>';
	document.getElementById("resultTable").innerHTML = output;
 }
 
 function showScores(){
  
  currentAnswer =  getRadioValue(document.getElementById("answerForm").myAnswer);



if(currentAnswer > 0 || (this.possibleAnswers==1)){
   player.myGuess((currentAnswer*1));
   for(var x=0;x<this.players;x++){
     this.playerList[x].myGuess(null);
     this.playerList[x].fillMyScoreboard();
   }
   player.fillMyScoreboard();
//   if (this.gameRound > 1){
//	  str ='document.getElementById("answer'+this.answer+'").parentNode.style.bgColor="white";';
//      eval(str);
//	}
   this.addRound();
   this.answer = answer.value;
//	str ='document.getElementById("answer'+this.answer+'").parentNode.style.borderColor="yellow";';
//    eval(str);
  
  this.displayAnswers();
  
  document.getElementById("lastAnswer").innerHTML = '<b>'+answer.value+'</b>';
  this.showGameRound();
answer.randomAnswer();

 } else {
    if (this.gameRound > 1){
    alert('First choose an answer.');}
  }
 }
 function showGameRound(){
    document.getElementById("gameRound").innerHTML = this.gameRound; 
 }
 function startGame(){
 
  document.getElementById("answerForm").style.display = 'block';
  document.getElementById("gameForm").style.display = 'none';
  
  //gameForm = document.getElementById("gameForm");
  this.players = gameForm.totalPlayers.value;
  this.possibleAnswers = gameForm.possibleAnswers.value;
  this.strictPossibility = gameForm.strictPossibility.value;
  player.pName = gameForm.playerName.value;
  this.makePlayerList();
  answer.possible = this.possibleAnswers;
  answer.randomAnswer();
  this.displayAnswers();

 } 
 
 function resetGame(){
  this.players = '';
  this.gameRound = 1;
  this.possibleAnswers = 0;
  this.strictPossibility = true;
  this.showGameRound();
  player.total = 0;
  player.hits = 0;
  player.misses = 0;
  player.percentage = 0;
  document.getElementById("answerForm").style.display = 'none';
  document.getElementById("gameForm").style.display = 'block';
  this.makePlayerList();
 } 
 
 
 function Player(pName,total,hits,misses){
    this.pName = pName;
    this.total=total;
	this.hits=hits;
	this.misses=misses;
	this.addHit=addHit;
	this.currentGuess;
	this.myGuess = myGuess;
	this.displayName = displayName;
	this.displayScore = displayScore;
	this.fillMyScoreboard = fillMyScoreboard;
	this.percentage = 0;
 }
 
 function myGuess(value){
    if(value*1<1){
      value = getRandomValues(game.possibleAnswers);
    }
   this.currentGuess = value;
   if(value  == answer.value){
     this.addHit(1);
   } else {this.addHit(-1);}
   return value;
 }
 
 function displayName(tag){
  output = '<'+tag+'>'+this.pName+'</'+tag+'>';
  return output;
 }
 
 function displayScore(){
  output = '<tr>'+this.displayName('td')+'<td class="hit" id="hit'+this.pName+'">0</td><td class="miss" id="miss'+this.pName+'">0</td><td id="percent'+this.pName+'"></td><td id="guess'+this.pName+'">-</td></tr>';
  return output;
 }
 function fillMyScoreboard(){
   var str = 'document.getElementById("hit'+this.pName+'").innerHTML = '+this.hits+';';

   str += 'document.getElementById("miss'+this.pName+'").innerHTML = '+this.misses+';';

   str += 'document.getElementById("percent'+this.pName+'").innerHTML = '+this.percentage+'+"%";';

   str += 'document.getElementById("guess'+this.pName+'").innerHTML = '+this.currentGuess+';';

 eval(str);
 }
 
 function addHit(hitTheMark){
  hitTheMark = hitTheMark * 1;
  if(hitTheMark != 0){
    if(hitTheMark > 0){
	  this.hits = this.hits + 1;
	} else {
	  this.misses = this.misses + 1;
	}
	this.total = this.total + 1;

	this.percentage = (this.hits/this.total)*100;
    this.percentage = Math.round(this.percentage);
  }
 }
 
 function getRandomValues(limit){
  limit = limit *1;
  return Math.floor((Math.random()*limit+1));
 }
 
 
   function Answer(possible,value){
     this.possible = possible;
	 this.value = value;
	 this.randomAnswer = randomAnswer;
     this.randomAnswer();
   }
   
   function randomAnswer(){
   	 if(this.possible==null){
	    this.possible = getRandomValues(getRandomValues(5));
	 }
	 
	 this.value = getRandomValues(this.possible);
	 
   }


function getRadioValue(radioObject){
 var value=null;
 for (var i=0; i<radioObject.length; i++){
  if (radioObject[i].checked){
   value = radioObject[i].value;
   break;
  }
 }
return value;
}
   
 //	<select id="strictPossibility" name="strictPossibility">
//	 <option value="true">True</option>
//	 <option value="false">False</option>
//	</select>

 var game = new Game(null,null,null);
 var player = new Player('Sylvia Brown',0,0,0);
 var answer = new Answer();
 