// VARIABLES DECLARATIONS
//-------------------------------------------------------------------------------

var question = 0;             // int  Index of the country to find.
var game_started = false;     // bool True if the game is in progress.
var num_left = 0;             // int  Num of countries you still have to find.
var num_tot = 0;              // int  Total num of countries you are playing with.
var score = 0;                // int  Player's score.
var right_answers = 0;
var wrong_answers = 0;
var passed_answers = 0;
var process = false;
var countries = 18;
var myTime = "0:0"

var left_array = new Array(countries);  // True if you still have to find the country.
for(var i=0; i<countries; i++) left_array[i]=false;   // Initialized to false.

var passed_array = new Array(countries);  // True if you passed the country.
for(var i=0; i<countries; i++) passed_array[i]=false;   // Initialized to false.

var names = new Array(countries);             // Names of the countries.
for(var i=0; i<countries; i++) names[i]="";   //  Initialized with empty strings.
// ARRAY
  names[0]="Haiti";
  names[1]="Dominican Republic";
  names[2]="Puerto Rico";
  names[3]="Bahamas";
  names[4]="Jamaica";
  names[5]="Cuba";
  names[6]="Cayman Islands";
  names[7]="Panama";
  names[8]="Costa Rica";
  names[9]="Nicaragua";
  names[10]="El Salvador";
  names[11]="Honduras";
  names[12]="Guatemala";
  names[13]="Belize";
  names[14]="Mexico";
  names[15]="Atlantic Ocean";
  names[16]="Pacific Ocean";
  names[17]="Gulf of Mexico";
  names[18]="Caribbean Sea";



//-------------------------------------------------------------------------------
var details = new Array(countries);         // Individual Info pages for IFRAME
  details[0]="iframes/haiti.html"
  details[1]="iframes/dominican.html"
  details[2]="iframes/puertorico.html"
  details[3]="iframes/bahamas.html"
  details[4]="iframes/jamaica.html"
  details[5]="iframes/cuba.html"
  details[6]="iframes/cayman.html"
  details[7]="iframes/panama.html"
  details[8]="iframes/costarica.html"
  details[9]="iframes/nicaragua.html"
  details[10]="iframes/elsalvador.html"
  details[11]="iframes/honduras.html"
  details[12]="iframes/guatemala.html"
  details[13]="iframes/belize.html"
  details[14]="iframes/mexico.html"
  details[15]="iframes/show.html"
  details[16]="iframes/show.html"
  details[17]="iframes/show.html"
  details[18]="iframes/show.html"
//-------------------------------------------------------------------------------
var rand = new RandomNumberGenerator();         // Generates random numbers.
// FUNCTION : PICK COUNTRY
//-------------------------------------------------------------------------------
function pick_country() {
  if (num_left>0)
  {  var pick = Math.ceil( num_left * rand.next() );   // random country.
     for (var i=0; i<countries; i++)
     {   if (left_array[i] == true)
         {   pick--;
             if (pick < 1)
             {   question = i;
                 num_left--;
                 left_array[i] = false;
                 break;
     }   }   }
     update_stats();
     add_to_message ( names[question] );
  }
  else { play_passed();  }
}
//-------------------------------------------------------------------------------
function play_passed() {
  message("");
  for (var i=0; i<countries; i++)
  {   if (true == passed_array[i] )
      {   passed_array[i] = false;  left_array[i] = true;  num_left++;  }
  }
  var st = "Do you want to give another shot\nat the countries you passed ?";
  if ( (num_left > 0) && confirm( st ))
  {   message("Find ");  pick_country();  }
  else
  {   game_over();  }
}
//-------------------------------------------------------------------------------
function game_over() {
  message ( "Game Over.  Click 'New Game' to Start" )
  num_left--;  game_started = false;
  StopTimer();
  update_stats();
  visOff('view1');
  visOff('view2');
  visOn('view3');
}
//-------------------------------------------------------------------------------
// FUNCTION : NEW GAME
//-------------------------------------------------------------------------------
function new_game() {
  if (game_started)                                // Ends the current game.
  {   if (confirm ("Are you sure you want to quit this game ?"))
      {   game_over();  }
  }
  else
  {   game_started = true;
      var process = false;
      score = 0;
      right_answers = 0;
      wrong_answers = 0;
      passed_answers = 0;
      num_tot = 0;
      for(var i=0; i<countries; i++) {  left_array[i] = false;  }
      for(var i=0; i<countries; i++) {  passed_array[i] = false;  }

      enable_cont( 0, 18); num_tot += countries;

      num_left = num_tot;
      if (num_left == 0)
      {   alert("Select at least one continent and click 'New Game'");
          game_started = false;
      }
      else
      {   message( "Find " );
          pick_country();
  }
     }
}

// FUNCTION : CHECK ANSWER
//-------------------------------------------------------------------------------
function checkanswer (answer) {
  if (process == false)
  {
      process = true;
      if (game_started)
      {   if (answer == question)  {  right_answers++;  right_answ();  }
          else  {  wrong_answers++;  wrong_answ( answer );  }
      }
      else
      {   message( "This is " + names[answer] )  }
      process = false;
  }
//   Added line to put appropriate info page in IFRAME
     document.getElementById("v1").src = details[answer];
}
//-------------------------------------------------------------------------------
function right_answ() {
  message( "Right! Now Find " );
  score += 100;  pick_country();
}
//-------------------------------------------------------------------------------
function wrong_answ( answer ) {
  message ( "This is " + names[answer] + ". Find " + names[question] );
  score -= 20;  myTime +=10; update_stats()
}
//-------------------------------------------------------------------------------
// FUNCTION : PASS
//-------------------------------------------------------------------------------
function pass () {
  if (game_started)
  {   if ( confirm("Pass this country?") )
      {   passed_array[question] = true;
          passed_answers++;
          message ( "Find " );
          pick_country();
  }   }
}
// FUNCTION : UPDATE STATISTICS
//-------------------------------------------------------------------------------
function update_stats () {
  document.stats.m_score.value = ("" + score);
  document.stats.m_left.value  = ("" + (num_left+1) + " of " + num_tot);
  document.stats.q1.value = (num_tot);
  document.stats.r1.value = (right_answers);
  document.stats.w1.value = (wrong_answers);
  document.stats.p1.value = (passed_answers);
  document.stats.m1.value = (100*num_tot);
    var p = .1;
    if (right_answers != 0) { p = .01 * Math.round (100 * score / right_answers); }
  else        { p = 0; }
  document.stats.c1.value = (p+"%");
  }
// FUNCTION : ENABLE CONTINENT
//-------------------------------------------------------------------------------
function enable_cont (n1, n2) {
  for (var i = n1; i <= n2; i++)
  {   left_array[i] = true;   }
}
//-------------------------------------------------------------------------------
function format ( string, num ) {
  answer = "" + string;
  for (i = answer.length; i < num; i++ )
  {   answer = "  " + answer;  }
  return( answer );
}
// FUNCTION : RANDOM NUMBER GENERATOR
//-------------------------------------------------------------------------------
function NextRandomNumber()  {
  var hi   = this.seed / this.Q;
  var lo   = this.seed % this.Q;
  var test = this.A * lo - this.R * hi;
  if (test > 0)
    this.seed = test;
  else
    this.seed = test + this.M;
  return (this.seed * this.oneOverM);
}
function RandomNumberGenerator() {
  var d = new Date();
  this.seed = 2345678901 + (d.getSeconds() * 0xFFFFFF) + (d.getMinutes() * 0xFFFF);
  this.A = 48271;
  this.M = 2147483647;
  this.Q = this.M / this.A;
  this.R = this.M % this.A;
  this.oneOverM = 1.0 / this.M;
  this.next = NextRandomNumber;
  return this;
}

function message( string ) {
  document.display.m_message.value = string;
  document.stats.dd.value = string;
}

function add_to_message ( string ) {
  document.display.m_message.value += string;
  document.stats.dd.value += string;
}

