<!-- 
// Memory Game
// JavaScript copyright (c) 1999-2002 by Karen M. Glatt
// This game is not freeware nor is it in the public domain.
// If you would like to use this game on your web site, 
// contact KMG Associates at www.kmgassociates.com.


// global variables
var numtiles = 24;           // # of tiles on board
var board = new Array(numtiles);  // board with shuffled tiles
var turns = 0;               // # of user turns
choice1 = -1;                // user's chosen tiles
choice2 = -1;
waiting = 1;                 // 1 during initialization and delays
waittime = 1500;             // milliseconds to wait for user to see images

// Preload some of the images into global variables
var tileblank = new Image();
tileblank.src = "tileblnk.gif";
var tileback = new Image();
tileback.src = "tileback.gif";
var winblank = new Image();
winblank.src = "winbuy.gif";	// Put up ad to license game
var winimage = new Image();
winimage.src = "winimage.gif";

// Images for the tiles
// Preload the tiles so they are in the browser's cache
var tmpimg0 = new Image();
tmpimg0.src = "tile0.gif";
var tmpimg1 = new Image();
tmpimg1.src = "tile1.gif";
var tmpimg2 = new Image();
tmpimg2.src = "tile2.gif";
var tmpimg3 = new Image();
tmpimg3.src = "tile3.gif";
var tmpimg4 = new Image();
tmpimg4.src = "tile4.gif";
var tmpimg5 = new Image();
tmpimg5.src = "tile5.gif";
var tmpimg6 = new Image();
tmpimg6.src = "tile6.gif";
var tmpimg7 = new Image();
tmpimg7.src = "tile7.gif";
var tmpimg8 = new Image();
tmpimg8.src = "tile8.gif";
var tmpimg9 = new Image();
tmpimg9.src = "tile9.gif";
var tmpimg10 = new Image();
tmpimg10.src = "tile10.gif";
var tmpimg11 = new Image();
tmpimg11.src = "tile11.gif";

// Clear the board position by displaying a blank image
function ShowBlank(pos)
{
    // Put the image on the screen
    document.images[eval('"tile' + pos + '"')].src = tileblank.src;
}

// Show the "back" of the tile
function ShowBack(pos)
{
    // Put the image on the screen
    document.images[eval('"tile' + pos + '"')].src = tileback.src;
}

// Show the tile image on the board where indicated
function ShowImage(pos, imgnum)
{
    // Put the image on the screen
    document.images[eval('"tile' + pos + '"')].src = eval('"tile' + imgnum + '.gif"');
}

// Mix up the tiles
function Shuffle()
{
    var i, n, x1, x2, temp;
    
    // shuffle the deck a random number of times to create different games
    n = Math.floor((Math.random() * numtiles) + 250);
    for (i = 0;  i < n; i++)
        {
        x1 = Math.floor(Math.random() * numtiles);
        x2 = Math.floor(Math.random() * numtiles);
        if (x1 != x2)
            {
            temp = board[x2];
            board[x2] = board[x1];
            board[x1] = temp;
            }
         }
}

// Initialize the game
function InitGame()
{
    var i, j;

    // Prevent the user from clicking until we are done
    waiting = 1;

    // Reset the global variables
    choice1 = -1;
    choice2 = -1;
    turns = 0;
    document.form1.fldturns.value = 0;

    // Erase the "winnning image" and set the board to the "backs" of the tiles
    document.gameover.src = winblank.src;
    for (i = 0; i < numtiles; ++i)
        {
        ShowBack(i);
        }

    // Initialize the board. Each tile must appear twice.
    j = 0;
    for (i = 0; i < numtiles; )
        {
        board[i] = j;
        board[i+1] = j;
        ++j;
        i += 2;
        }

    // Shuffle the board, otherwise the game would be boring!
    Shuffle();

    // OK, let the user play
    waiting = 0;    
}

// Check to see if all of the tiles have been matched. If so, display image.
function CheckWin()
{
    var i;

    for (i = 0; i < numtiles; ++i)
        {
        if (board[i] != -1)
            return;
        }

    document.gameover.src = winimage.src;
}

// The user has selected a tile
function TurnOver(pos)
{
    // If we are waiting, ignore the user
    if (waiting)
        return;

    // Ignore the click if it is already a blank spot on the board
    if (board[pos] == -1)
        return;

    // Is this the first pick? Display the hidden image
    if (choice1 == -1)
        {
        ShowImage(pos, board[pos]);
        choice1 = pos;
        return;
        }

    // Make sure the user did not pick the same tile twice
    if (choice1 == pos)
        return;

    // OK, legitimate 2 clicks.
    ++turns;
    document.form1.fldturns.value = turns;
    ShowImage(pos, board[pos]);
    choice2 = pos;

    // Delay so user can see choice
    waiting = 1;
    setTimeout("EndWait();", waittime);
}

// Delay is over. Decide if tiles match & what to do next.
function EndWait()
{
    // Did the tiles match?
    if (board[choice1] == board[choice2])
        {
        // we have a match!
        ShowBlank(choice1);             // clear tiles from board
        ShowBlank(choice2);
        board[choice1] = -1;
        board[choice2] = -1;
        CheckWin();
        }
    else
        {
        // images did not match
        ShowBack(choice1);
        ShowBack(choice2);
        }

    // Reset for next turn
    choice1 = -1;
    choice2 = -1;
    waiting = 0;
}

// End JavaScript -->

