// 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 = 41;
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]="Talinn";
    names[1]="Riga";
    names[2]="Vilnius";
    names[3]="Minsk";
    names[4]="Kiev";
    names[5]="Chisnau";
    names[6]="Bucharest";
    names[7]="Belgrade";
    names[8]="Sofia";
    names[9]="Skopje";
    names[10]="Athens";
    names[11]="Tirana";
    names[12]="Podgorica";
    names[13]="Sarajevo";
    names[14]="Zagreb";
    names[15]="Ljubljana";
    names[16]="Budapest";
    names[17]="Bratislava";
    names[18]="Vienna";
    names[19]="Prague";
    names[20]="Paris";
    names[21]="Warsaw";
    names[22]="Berlin";
    names[23]="Valletta";
    names[24]="Rome";
    names[25]="Lisbon";
    names[26]="Madrid";
    names[27]="Andorra";
    names[28]="Monaco";
    names[29]="Bern";
    names[30]="Vaduz";
    names[31]="Luxembourg";
    names[32]="Brussels";
    names[33]="Amsterdam";
    names[34]="London";
    names[35]="Dublin";
    names[36]="Moscow";
    names[37]="Helsinki";
    names[38]="Stockholm";
    names[39]="Oslo";
    names[40]="Reykjavik";
    names[41]="Copenhagen";

//-------------------------------------------------------------------------------
var details = new Array(countries);         // Individual Info pages for IFRAME
    details[0]="iframes/estonia.html"
    details[1]="iframes/latvia.html"
    details[2]="iframes/lithuania.html"
    details[3]="iframes/belarus.html"
    details[4]="iframes/ukraine.html"
    details[5]="iframes/moldova.html"
    details[6]="iframes/romania.html"
    details[7]="iframes/serbia.html"
    details[8]="iframes/bulgaria.html"
    details[9]="iframes/macedonia.html"
    details[10]="iframes/greece.html"
    details[11]="iframes/albania.html"
    details[12]="iframes/montenegro.html"
    details[13]="iframes/bosna.html"
    details[14]="iframes/croatia.html"
    details[15]="iframes/slovenia.html"
    details[16]="iframes/hungary.html"
    details[17]="iframes/slovakia.html"
    details[18]="iframes/austria.html"
    details[19]="iframes/hungary.html"
    details[20]="iframes/france.html"
    details[21]="iframes/poland.html"
    details[22]="iframes/germany.html"
    details[23]="iframes/malta.html"
    details[24]="iframes/italy.html"
    details[25]="iframes/portugal.html"
    details[26]="iframes/spain.html"
    details[27]="iframes/andorra.html"
    details[28]="iframes/monaco.html"
    details[29]="iframes/switzerland.html"
    details[30]="iframes/liechtenstein.html"
    details[31]="iframes/luxembourg.html"
    details[32]="iframes/belgium.html"
    details[33]="iframes/netherlands.html"
    details[34]="iframes/uk.html"
    details[35]="iframes/ireland.html"
    details[36]="iframes/russia.html"
    details[37]="iframes/finland.html"
    details[38]="iframes/sweden.html"
    details[39]="iframes/norway.html"
    details[40]="iframes/iceland.html"
    details[41]="iframes/denmark.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, 41); 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;
}

