//====================================================================================
//= Project:       Zoo Numbat game
//= Module:        numbat.js
//= Author:        Dave Catley 
//= Date Created:  2/12/2003
//=
//= Description:  Supporting functions for numbat game application
//=
//= Version:
//= Methods:
//=    ====================================================================================
//=    ====================================================================================
//=
//=                                 © Madcat 2003
//====================================================================================

//====================================================================================
//= Global Constants
//====================================================================================
PNL_LOADING = "Loading";
PNL_GAME = "Game";
PNL_PLAYER1 = "Player1";
PNL_PLAYER2 = "Player2";
PNL_PLAYER3 = "Player3";
PNL_PLAYER4 = "Player4";
PNL_PLAYERPANEL1 = "Player1Panel";
PNL_PLAYERPANEL2 = "Player2Panel";
PNL_PLAYERPANEL3 = "Player3Panel";
PNL_PLAYERPANEL4 = "Player4Panel";
PNL_PLAYERDICE1 = "Player1Dice";
PNL_PLAYERDICE2 = "Player2Dice";
PNL_PLAYERDICE3 = "Player3Dice";
PNL_PLAYERDICE4 = "Player4Dice";

MAX_MOVES = 49;

MAX_PLAYERS = 4;


CURR_NO_VALUE = -1;

//====================================================================================
//= Global Variables
//====================================================================================

//Game square Item array, contains the x and y coordinate of each square of the grid plus number of 
//squares to move if a special square and the text to display
var GridSquare = new Array(
new Array("13","425","0","","",1),
new Array("77","425","0","","",2),
new Array("139","425","0","","",3),
new Array("202","425","6","0","",4),
new Array("264","425","0","","",5),
new Array("326","425","0","","",6),
new Array("388","425","0","","",7),

new Array("388","363","-2","1","",8),
new Array("326","363","0","","",9),
new Array("264","363","0","","",10),
new Array("202","363","0","","0",11),
new Array("139","363","0","","",12),
new Array("77","363","0","","",13),
new Array("13","363","14","2","",14),

new Array("13","301","0","","",15),
new Array("77","301","-15","3","",16),
new Array("139","301","0","","",17),
new Array("202","301","-9","4","",18),
new Array("264","301","0","","",19),
new Array("326","301","0","","1",20),
new Array("388","301","4","5","",21),

new Array("388","239","0","","",22),
new Array("326","239","11","6","",23),
new Array("264","239","0","","2",24),
new Array("202","239","0","","",25),
new Array("139","239","-23","7","",26),
new Array("77","239","0","","",27),
new Array("13","239","0","","",28),

new Array("13","176","-2","8","",29),
new Array("77","176","12","9","",30),
new Array("139","176","0","","",31),
new Array("202","176","7","10","",32),
new Array("264","176","0","","",33),
new Array("326","176","0","","",34),
new Array("388","176","0","","",35),

new Array("388","114","-14","11","",36),
new Array("326","114","-4","12","",37),
new Array("264","114","8","13","",38),
new Array("202","114","0","","",39),
new Array("139","114","0","","3",40),
new Array("77","114","0","","",41),
new Array("13","114","0","","",42),

new Array("13","52","-12","14","",43),
new Array("77","52","0","","",44),
new Array("139","52","0","","4",45),
new Array("202","52","0","","",46),
new Array("264","52","2","15","",47),
new Array("326","52","0","","",48),
new Array("388","52","0","","",49)
);

var Players = new Array(
new Array("0", PNL_PLAYER1, "", PNL_PLAYERPANEL1, PNL_PLAYERDICE1),
new Array("0", PNL_PLAYER2, "", PNL_PLAYERPANEL2, PNL_PLAYERDICE2),
new Array("0", PNL_PLAYER3, "", PNL_PLAYERPANEL3, PNL_PLAYERDICE3),
new Array("0", PNL_PLAYER4, "", PNL_PLAYERPANEL4, PNL_PLAYERDICE4)
);

var lPlayerTurn = 0;
var lNumPlayers = 2;

var lNumMoves;
var lDirection;
var lLastRoll;

var bMidTurn;

var sPlayerPanel;

// Text when landing on a special square
var SquareText= new Array(
"You find a vacant log under which you dig a burrow to live in and start establishing your territory. Move ahead 6 places.",
"An introduced species of bird is competing with you for precious termites. Move back 2 places.",
"You were trained at Perth Zoo to recognise predators and you run for cover when you hear the warning call of local birds. Move ahead 14 places.",
"Someone is taking fallen wood from the forest floor for fire-wood and you can't find a home! Move back 15 places.",
"The forest you live in is visited regularly by wild cats. It's dangerous to go out - day or night! Move back 9 places.",
"Rangers track you using the radio-collar fixed to your neck and discover that you have successfully found a mate. Move ahead 4 places.",
"You discover a fallen tree eaten away by termites. Mmm.. your favourite! Move ahead 11 places.",
"Your weren't successfully trained to recognise predators and a bird-of-prey nearly grabs you in its talons! Move back 23 places.",
"Foxes have eaten many numbats in your area. You can't find a mate. Move back 2 places.",
"A bushfire rages through the forest habitat where you were released. You made your home in the opposite direction to the fire and are saved!  Move ahead 12 place.",
"Your mate is pregnant, ensuring your genes will continue into the next generation. Move ahead 7 places.",
"A bushfire destroys your bush habitat.  You lose your home and your mate. Move back 14 places.",
"Someone has dumped a trailer-load of rubbish in the bush.  You get chemicals from an old car battery on your fur and become very ill when trying to lick it off.  Move back 4 places.",
"You recognise the looming shadow of a bird of prey in the sky and dart for the safety of a burrow. Move ahead 8 places.",
"Rangers track you to a log and then discover your collar is.. inside a python! Move back 12 places.",
"CALM staff remove your tracking collar after six months surviving in the wild. You are officially on your own. Move ahead 2 places."
);

// Text supporting pop up boxes above
var SupportText = new Array(
"Numbats live in hollow logs or in burrows in the ground. If you remove fallen logs from the bush, animals like Numbats have fewer places to make a nest.  Each Numbat needs its own territory in which to find food or mates. They protect this territory by scent marking.",
"Introduced (or 'feral') species have not naturally evolved in an ecosystem. Their sudden arrival in one can have an enormous effect on the survival of the native species.  Introduced species might prey on the Numbats or might compete with them for precious food sources.",
"Numbats have evolved to live in balance with native predators like pythons and birds-of-prey. Perth Zoo also runs a 'predator recognition training' program (think of it as 'Numbat School') where young Numbats learn to recognise and react appropriately to natural predators. This gives the newly released Numbats the best possible chance of survival in the wild.",
"Logs have life inside!  That chunk of wood you take to burn in your fire or to put in your water-feature might support dozens of creatures, from insects to birds to Numbats. No logs means no home and no termites for wild Numbats.",
"The 'Feral Peril' is a big threat to native wildlife, particularly the small species. Australia has no large native carnivores and so the wildlife here has evolved in relative safety from predators.  Just one wild cat can destroy an entire population of Numbats over time.  Just imagine how many wild cats there are out there!",
"The Department of Conservation & Land Management fixes radio-signal collars to all newly released Numbats. This means that Rangers can come back in a few weeks or months, locate the Numbat by tracking the signal from its collar, and check on its progress. The Numbat is caught after six months and the collar is removed.",
"Numbats only eat termites. A healthy forest full of fallen or decaying wood will also be full of termites.  The tiny bits of grit which the Numbat accidentally consumes is also very good roughage for its digestive system.",
"Numbats have evolved to live in balance with native predators like pythons and birds-of-prey. Perth Zoo also runs a 'predator recognition training' program (think of it as 'Numbat School') where young Numbats learn to recognise and react appropriately to natural predators. This gives the newly released Numbats the best possible chance of survival in the wild.",
"Perth Zoo works in cooperation with the Department of Conservation and Land Management to breed Numbats for release into wild habitat that has been protected from introduced predators. By protecting them from 'feral' predators (cats & foxes) and by protecting their forest habitat, this species is quite likely to build to healthy numbers without further help from us.",
"In 2003 a bushfire in the South-west of Western Australia burned out the forest block immediately next door to the site of recent captive-bred Numbat release.  An aerial tracking survey by the Department of Conservation and Land Management staff showed that all of the animals bred at the Zoo had made their way to the furthest corner of the release site and, thus, were saved.",
"Numbat mating season is in January/February. Numbats do not pair up for life but might choose several partners over the years in order to distribute their genes most effectively. The young marsupials are born in February/March about the size of a grain of rice and, amazingly, make their own way up to a pouch-like fold of skin on their mother's belly where they latch on to a teat and grow until large enough to fend for themselves.",
"Fires are common in the Australian bush (caused by lightning and other natural factors) and populations of native animals can re-build from near destruction in the years between fires.  However, overly-frequent fires can stop the Numbat populations from rebuilding at all.   Sparks from cars backfiring, cigarettes discarded out windows or careless campers can totally decimate a native bush population",
"Never, ever dump your rubbish in the bush.  Even something as simple as a coke bottle can cause problems for native wildlife.  Numbats only eat termites, but they are still vulnerable to injuries from broken glass, asphyxiation on plastic bags or getting tangled in wires or ropes.",
"Young Numbats trained to recognise predators at Perth Zoo quickly learn to fear the overhead threat of a bird-of-prey.  In nature, there are generally three ways of avoiding a predator:  'freeze', 'fight' or 'flight'.  In the case of Numbats, there is only a small chance of avoiding the keen eyes of a bird-of-prey by freezing and no chance at all of surviving a fight with their talons. Fleeing for shelter is the best response a Numbat can make to this predator.",
"Rangers remove the collars of released Numbats after about six months.  After that, they are on their own in the wild!",
"Congratulations - you have survived your first six months in the wild!   Download your certificate to remind yourself how important Numbats are to the Western Australian forest."
);

// Text to encourage the players
var EncouragingText = new Array(
"Survive and stay alive!",
"Great job - you're doing well in the wild!",
"Your training at Numbat School is certainly paying off!",
"Your tracking collar tells us you're doing great in the wild.",
"Can you remember what it was like growing up in the safety of Perth Zoo?"
);
//====================================================================================
//= Method:        Move x spaces
//= Author:        Dave Catley
//= Date Created:  2/12/2003
//=
//= Description:   Move players numbat x spaces.
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Madcat 2003
//====================================================================================
function MoveNumbat()
{
  var lPosition;
  
  // Ensure the message panel is hidden
  HidePanel("Message");
  HidePanel("SupportingMessage");
  
  // Get the players current position
  lPosition = parseInt(Players[lPlayerTurn][0]);
  
  // Check if the numbat is still moving
  if ((lNumMoves == 0) &&  (lPosition != (MAX_MOVES-1)))
  {
    // No need to move
    return;
  }

  // Check if we are at the end of the game
  if (lPosition == (MAX_MOVES-1))
  {
    document.getElementById("winner").value = Players[lPlayerTurn][2];
    document.getElementById("winningform").submit();
  }
  
  lPosition = lPosition + lDirection;
  
  if (lPosition < 0)
  {
    lNumMoves = 0;
    lPosition = 0;
  }
  else if (lPosition >= MAX_MOVES)
  {
    //lNumMoves = 0;
    lPosition = MAX_MOVES-1;
  }
  else
  {
    lNumMoves = lNumMoves - 1;
    // Hide the current displayed panel
    HidePanel(sPlayerPanel);
    
    if (lNumMoves > 0)
    {
      // Numbat is still moving
      sPlayerPanel = "Move";
    }
    else
    {
      // Numbat has stopped moving now
      sPlayerPanel = "";
    }
    
    // Check which direction the numbat should be facing
    if (((lPosition >= 0) && (lPosition < 7)) ||
        (lPosition > 13) && (lPosition < 21) ||
        (lPosition > 27) && (lPosition < 35) ||
        (lPosition > 41))
    {
      sPlayerPanel = sPlayerPanel + Players[lPlayerTurn][1];
    }
    else
    {
      sPlayerPanel = sPlayerPanel + "a" + Players[lPlayerTurn][1];
    }
    
    MovePlayer(sPlayerPanel, parseInt(GridSquare[lPosition][0]), parseInt(GridSquare[lPosition][1]));
    ShowPanel(sPlayerPanel);

  }
  
  Players[lPlayerTurn][0] = lPosition;
  
  if ((lNumMoves > 0) && (lPosition != (MAX_MOVES-1))) 
  {
    // Reset the timeout
    setTimeout("MoveNumbat()", 200);
  }
  else
  {
    // Check if the player landed on a special square
    setTimeout("SpecialSquare()", 200);
  }

}

//====================================================================================
//= Method:        SpecialSquare
//= Author:        Dave Catley
//= Date Created:  2/12/2003
//=
//= Description:   Checks if player landed on a special square.
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Madcat 2003
//====================================================================================
function SpecialSquare()
{
  var lPosition;
  var lMessagePosition;
  
  // Test to see if the player is at the end of the game
  lPosition = parseInt(Players[lPlayerTurn][0]);
  
  if (lPosition == (MAX_MOVES-1))
  {
    lNumMoves = 0;
    // The current player has won the game
    //MessageText.value = "Congratulations! " + Players[lPlayerTurn][2] + " - you have survived your first six months in the wild! You are the winner!";
    document.getElementById("MessagePanel").innerHTML = "Congratulations " + Players[lPlayerTurn][2] + " - you have survived your six months in the wild! You are the winner!";
    lMessagePosition = 30;
    MovePlayer("Message", parseInt(GridSquare[lMessagePosition][0]), parseInt(GridSquare[lMessagePosition][1]));
    ShowPanel("Message");
  }
  else
  {
    // Check if the player has landed on a special square
    if (parseInt(GridSquare[lPosition][2]) != 0) 
    {
      lNumMoves = parseInt(GridSquare[lPosition][2]);
      
      // Check the direction
      if (lNumMoves < 0)
      {
        lDirection = -1;
        lNumMoves = lNumMoves * lDirection;
      }
      else
      {
        lDirection = 1;
      }
      
      //Check for debug
      if (document.getElementById("MADDebug").checked == true) 
      {
        lNumMoves = 1;
        lDirection = 1;
      }

      // Display the warning box
      //MessageText.value = SquareText[parseInt(GridSquare[lPosition][3])];
      document.getElementById("MessagePanel").innerHTML = SquareText[parseInt(GridSquare[lPosition][3])];
      lMessagePosition = GetPanelPosition(lPosition);
      document.getElementById("SupportingMessage").innerHTML = SupportText[parseInt(GridSquare[lPosition][3])];
      MovePlayer("Message", parseInt(GridSquare[lMessagePosition][0])-8, parseInt(GridSquare[lMessagePosition][1]));
      ShowPanel("Message");
      ShowPanel("SupportingMessage");
      
    }
    else
    {
      if (GridSquare[lPosition][4] != "")
      {
        document.getElementById("MessagePanel").innerHTML = EncouragingText[parseInt(GridSquare[lPosition][4])];
        lMessagePosition = GetPanelPosition(lPosition);
        MovePlayer("Message", parseInt(GridSquare[lMessagePosition][0])-8, parseInt(GridSquare[lMessagePosition][1]));
        ShowPanel("Message");
      }

      // Set the next players turn as current turn is over
      lPlayerTurn = lPlayerTurn + 1;

      if (lPlayerTurn >= lNumPlayers)
      {
        lPlayerTurn = 0;
      }

      // Indicate whose turn it is now as current players turn is over
      ShowPanel(Players[lPlayerTurn][4]);
      
      // Indicate not in the middle of a turn
      bMidTurn = false;
      
    }
  }
  
}

//====================================================================================
//= Method:        CloseGameApplication
//= Author:        Dave Catley
//= Date Created:  2/12/2003
//=
//= Description:   Close the game application window.
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Madcat 2003
//====================================================================================
function CloseGameApplication()
{

  //Close the game application window
  window.close();

}

//====================================================================================
//= Method:        InitGameApplication
//= Author:        Dave Catley
//= Date Created:  2/12/2003
//=
//= Description:   Perform all necessary game application initialisation functions.
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Madcat 2002
//====================================================================================
function InitGameApplication()
{

  var i;
  var lPosition;
  var args = getArgs();
  var sPlayerName;
  
  SetRandomImage('TheImage');
  
  // Indicate that no one is in the middle of their turn
  bMidTurn = false;
  
  lNumPlayers = 0;
  
  // Show the players
  for (i = 1; i <= MAX_PLAYERS; i++)
  {
    sPlayerName = eval("args.Player" + (i));
    if (sPlayerName != "")
    {
      Players[lNumPlayers][2] = sPlayerName;
      lPosition = Players[lNumPlayers][0];
      MovePlayer(Players[lNumPlayers][1], GridSquare[lPosition][0], GridSquare[lPosition][1]);
      ShowPanel(Players[lNumPlayers][1]);
      document.getElementById(Players[lNumPlayers][3]).innerHTML = Players[lNumPlayers][2];
      ShowPanel(Players[lNumPlayers][3]);
      lNumPlayers++;
    }
  }
  
  lPlayerTurn = 0;
  lLastRoll = 0;
  
  // First player goes first so store its current displayed panel
  sPlayerPanel = Players[0][1];
  
  // Indicate whose turn it is now as current players turn is over
  //PlayerText.value = Players[lPlayerTurn][2] + "'s turn";
  // Set the button for the appropriate player
  ShowPanel(Players[lPlayerTurn][4]);
  
  //Check for IE 5 or later
  //if ((sBrowserName != BR_EXPLORER_NAME) || (sBrowserVersion < 6))
  //{
    //Need IE 5 or greater so display message
  //  alert("This Game Requires Internet Explorer V5 or later");
//    window.close();
 // }

}

//====================================================================================
//= Method:        PlayerTurn
//= Author:        Dave Catley
//= Date Created:  2/12/2003
//=
//= Description:   Perform the current players turn.
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Madcat 2003
//====================================================================================
function PlayerTurn()
{
  var lPosition;
  
  // Check if someone is having their turn
  if (bMidTurn)
  {
    return;
  }
  
  SetRandomImage('TheImage');

  // Disable the button till end of turn
  bMidTurn = true;
  
  // Get this players current displayed icon
  sPlayerPanel = Players[lPlayerTurn][1];
  lPosition = parseInt(Players[lPlayerTurn][0]);
  
  // Check which direction the numbat should be facing
  if (((lPosition > 6) && (lPosition < 14)) ||
      (lPosition > 20) && (lPosition < 28) ||
      (lPosition > 34) && (lPosition < 42))
  {
    sPlayerPanel = "a" + sPlayerPanel;
  }
  
  // Hide the current displayed dice if there is one
  HidePanel("Dice" + lLastRoll);
  
  // Remove the current dice
  HidePanel(Players[lPlayerTurn][4]);
  
  // Roll the dice for the player and set the number of spaces to move
  lNumMoves = RollDice();
  
  // Set the direction which will always be positive here
  lDirection = 1;
  
  // Set the timeout to move the player
  setTimeout("MoveNumbat()", 50);
  
}

//====================================================================================
//= Method:        Roll dice
//= Author:        Dave Catley
//= Date Created:  2/12/2003
//=
//= Description:   Roll the dice for the current player.
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Madcat 2003
//====================================================================================
function RollDice()
{
  var x;
  x = RandomNum(6)  
  // x = Math.random();
  // x = x*6;
  // x = Math.floor(x) + 1;
  // if (x>6)
  // {
  //  x = 6;
  // }
  //DiceResult.value=x;
  // Display the current Dice
  ShowPanel("Dice" + x);
  
  //Check for debug
  if (document.getElementById("MADDebug").checked == true) 
  {
    x = 1;
  }
  
  lLastRoll = x;
  
  return x;

}

//====================================================================================
//= Method:        HidePanel
//= Author:        Dave Catley
//= Date Created:  3/12/2003
//=
//= Description:   Hides the specified player
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Praetorium 2002
//====================================================================================
function HidePanel(sPanelName)
{
  
  //Hide panel
  document.getElementById(sPanelName).style.visibility = "hidden";
}

//====================================================================================
//= Method:        ShowPanel
//= Author:        Dave Catley
//= Date Created:  3/12/2003
//=
//= Description:   Shows the specified player
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Praetorium 2002
//====================================================================================
function ShowPanel(sPanelName)
{
  //Show panel 
  document.getElementById(sPanelName).style.visibility = "visible";
}
//====================================================================================
//= Method:        MovePlayer
//= Author:        Dave Catley
//= Date Created:  3/12/2003
//=
//= Description:   Move a specified panel
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Praetorium 2002
//====================================================================================
function MovePlayer(sPlayerName, lLeft, lTop)
{
  document.getElementById(sPlayerName).style.left = lLeft;
  document.getElementById(sPlayerName).style.top = lTop;
}

//====================================================================================
//= Method:        getArgs
//= Author:        Dave Catley
//= Date Created:  2/12/2003
//=
//= Description:   Extract the query string value pairs.
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Madcat 2002
//====================================================================================
function getArgs()
{
  var args = new Object();
  var query = location.search.substring(1);
  var pairs = query.split("&");
  var objRegExp;
  
  for (var i = 0; i<pairs.length; i++)
  {
    var pos = pairs[i].indexOf('=');
    if (pos == -1) continue;
    var argname = pairs[i].substring(0,pos);
    var value = pairs[i].substring(pos+1);
    objRegExp = /\)|\(|[+]/g;
    args[argname] = unescape(value).replace(objRegExp, " ");
  }
  
  return args;
}

//====================================================================================
//= Method:        InitWinningText
//= Author:        Dave Catley
//= Date Created:  2/12/2003
//=
//= Description:   Move players numbat x spaces.
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Madcat 2003
//====================================================================================
function InitWinningText()
{
  var args = getArgs();
  var sPlayerName;
  
  SetRandomImage('TheImage');

  // Get the winning player
  if (args.winner)
  {
    sPlayerName = args.winner;
  }
  else
  {
    sPlayerName = "Unknown Player";
  }
  
  // Display the certificate for the winning player
  document.getElementById("PlayerName").innerHTML = sPlayerName;
  document.getElementById("FullName").value = sPlayerName;
}

//====================================================================================
//= Method:        InitCertificate
//= Author:        Dave Catley
//= Date Created:  2/12/2003
//=
//= Description:   Move players numbat x spaces.
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Madcat 2003
//====================================================================================
function InitCertificate()
{
  var args = getArgs();
  var sPlayerName;
  
  //window.resizeTo(610,900);
  
  //Display random image
  InitialiseImages();
  
  // Get the winning player
  if (args.FullName)
  {
    sPlayerName = args.FullName;
  }
  else
  {
    sPlayerName = "Unknown Player";
  }
  
  // Display the certificate for the winning player
  document.getElementById("PlayerName").innerHTML = sPlayerName;
}

//====================================================================================
//= Method:        PrintCertificate
//= Author:        Dave Catley
//= Date Created:  2/12/2003
//=
//= Description:   Move players numbat x spaces.
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Madcat 2003
//====================================================================================
function PrintCertificate()
{
  //Show panel
  document.getElementById("certificate").style.visibility = "hidden";
  window.print();
  document.getElementById("certificate").style.visibility = "visible";
}

//====================================================================================
//= Method:        CheckPlayers
//= Author:        Dave Catley
//= Date Created:  2/12/2003
//=
//= Description:   Check each player has entered their name.
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Madcat 2003
//====================================================================================
function CheckPlayers()
{
  // Must be atleast one player
  //if (Intro.Player1.value == "" && Intro.Player2.value =="" && Intro.Player3.value == "" && Intro.Player4.value =="")
  if (document.getElementById("Player1").value == "" && document.getElementById("Player2").value == "" && document.getElementById("Player3").value == "" && document.getElementById("Player4").value == "")
  {
    alert("Please enter atleast one name");
    return;
  }
  
  document.getElementById("Intro").submit();
  
}

//====================================================================================
//= Method:        GetPanelPosition
//= Author:        Dave Catley
//= Date Created:  2/12/2003
//=
//= Description:   Determine best place for message panel.
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Madcat 2003
//====================================================================================
function GetPanelPosition(CurrentPosition)
{
    if (CurrentPosition < (MAX_MOVES/2))
    {
      return MAX_MOVES-9;
    }
    else
    {
      return MAX_MOVES/2 - 9.5;
    }
}

//====================================================================================
//= Method:        SetRandomImage
//= Author:        Dave Catley
//= Date Created:  2/12/2003
//=
//= Description:   Sets a specified image to a random image.
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Madcat 2003
//====================================================================================
//Sets a specified image to a random image
function SetRandomImage(sImageName)
{ 
 
  document.getElementById(sImageName).src = "gfx/NumbatRnd" + RandomNum(13) + ".jpg";
  
}
 
//====================================================================================
//= Method:        GetPanelPosition
//= Author:        Dave Catley
//= Date Created:  2/12/2003
//=
//= Description:   Generates and returns a random number between 1 and the specified number.
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Madcat 2003
//====================================================================================
//Generates and returns a random number between 1 and the specified number
function RandomNum(iNum)
{
 
  var iRandomNum;
  
  iRandomNum = Math.floor(Math.random()*(iNum));
  
  if (iRandomNum == iNum)
  {
    return iRandomNum;
  }
  else
  {
    return iRandomNum + 1;
  }
 
}

//====================================================================================
//= Method:        InitialiseImages
//= Author:        Dave Catley
//= Date Created:  2/12/2003
//=
//= Description:   Preloads the random images to prevent delays.
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Madcat 2003
//====================================================================================
//Generates and returns a random number between 1 and the specified number
function InitialiseImages()
{
  var image1;
  var image2;
  var image3;
  var image4;
  var image5;
  var image6;
  var image7;
  var image8;
  var image9;
  var image10;
  var image11;
  var image12;
  var image13;
  
  SetRandomImage('TheImage');
  
  image1 = new Image();
  image1.src = "gfx/NumbatRnd1.jpg";
  image2 = new Image();
  image2.src = "gfx/NumbatRnd2.jpg";
  image3 = new Image();
  image3.src = "gfx/NumbatRnd3.jpg";
  image4 = new Image();
  image4.src = "gfx/NumbatRnd4.jpg";
  image5 = new Image();
  image5.src = "gfx/NumbatRnd5.jpg";
  image6 = new Image();
  image6.src = "gfx/NumbatRnd6.jpg";
  image7 = new Image();
  image7.src = "gfx/NumbatRnd7.jpg";
  image8 = new Image();
  image8.src = "gfx/NumbatRnd8.jpg";
  image9 = new Image();
  image9.src = "gfx/NumbatRnd9.jpg";
  image10 = new Image();
  image10.src = "gfx/NumbatRnd10.jpg";
  image11 = new Image();
  image11.src = "gfx/NumbatRnd11.jpg";
  image12 = new Image();
  image12.src = "gfx/NumbatRnd12.jpg";
  image13 = new Image();
  image13.src = "gfx/NumbatRnd13.jpg";

}
//====================================================================================
//= Method:        ProcessWin
//= Author:        Dave Catley
//= Date Created:  2/12/2003
//=
//= Description:   Display certificate with name in a new window and close current one.
//=
//= Updated By:    <Author>
//= Date Updated:  <Date Updated>
//= Reason:        <Reason>
//=
//=                                 © Madcat 2003
//====================================================================================
function ProcessWin()
{
  var sPlayerName;
  
  sPlayerName = document.getElementById("FullName").value;
  
  window.close();
  
  window.open("certificate.htm?FullName=" + sPlayerName, "certificate","height=500,width=630,status=no,resizable=yes,scrollbars=yes");
  
}
