   boxes  = new Array(10);

   for (i=0; i<10; i++) {
       boxes[i]  = new Array(10);
   }
   var gridSize = 10;
   var numBoxes = 10;
   var moveX = 'X';
   var moveY = 'Y';
   var gameStarted = false;
   var canMove = true;
   var isHole = 9;
   var moveTimer;

   var startX;
   var startY;
   var endX;
   var endY;
   var pivotX;
   var pivotY;
   var diffY;
   var curX;
   var curY;
   var curBox;
   var dropY;
   var dropX;
   var curScore;
   var numBoxesCleared;
   var hintX;
   var hintY;
   var hintCount;

   var digits = new Array(10);
   for (i=0; i<10; i++) {
      digits[i] = new Image(64,64);
      digits[i].src = 'w-' + i + '.gif';
   }
   var boxImgs = new Array(10);
   for (i=0; i<numBoxes; i++) {
      boxImgs[i] = new Image(64,64);
      boxImgs[i].src = 'b' + i + '.gif';
   }
   blankBox = new Image(30,30);
   blankBox.src = 'blank.gif';
   buttonBeg = new Image(100,28);
   buttonBeg.src = 'beginButton.gif';
   buttonEnd = new Image(100,28);
   buttonEnd.src = 'endButton.gif';
   function newGame() {
      gameStarted = true;
      for (i=0; i<10; i++) {
         for (j=0; j<10; j++) {
            var g = Math.floor(Math.random() * (numBoxes*1 - 1));
            boxes[i][j] = g;
            document.images['box'+i+j].src = boxImgs[g].src;
         }
      }
      curScore = 0;
      moveTimer = 20;
      canMove = true;
      displayTimer();
      displayScore();
   }
   function clickButton() {
        if (gameStarted == true) {
           endGame();
        } else {
           document.forms[0].beginButton.value = 'End Game';
           document.start.stop();
           document.start.play();
           setTimeout(newGame,1000);
        }
   }
   function clickBox(y, x) {
        if (canMove == false) {
           return;
        }
        canMove = false;
        //alert("in click box y=" + y + ", x=" + x);
        pivotX = x;
        pivotY = y;
        curBox = boxes[y][x];
        if (curBox == isHole) {
           document.elecshot.stop();
           document.elecshot.play();
           canMove = true;
           return;
        }
        var validMove = false;
        if ((x < (gridSize*1-1) && boxes[y][x*1+1] == curBox) || (x > 0 && boxes[y][x*1-1] == curBox)) {
             validMove = true;
        } else {
           if ((y < (gridSize*1-1) && boxes[y*1+1][x] == curBox) || (y > 0 && boxes[y*1-1][x] == curBox)) {
               validMove = true;
           }
        }
        if (validMove == false) {
           document.drop.stop();
           document.drop.play();
           //alert("Not a legal move");
           canMove = true;
           return;
        }
        moveTimer = 20;
        startX  = x;
        comp = false;
        for(cx =x*1-1; cx >= 0 && comp == false; cx--) {
            if (boxes[y][cx] == curBox) {
               startX = cx;
            } else {
               comp = true;
            }
        }
        comp = false;
        startY = y;
        for (cy = y; cy<gridSize && comp == false; cy++) {
            if (boxes[cy][x] == curBox) {
               startY = cy;
            } else {
               comp = true;
            }
        }
        curX = startX;
        curY = startY;
        numBoxesCleared = 0;
        setTimeout(dropBoxes, 200);
    }
    function dropBoxes() {
        //alert("drop boxes, curY=" + curY + ", curX=" + curX);
        endX = startX;
        for (cx = startX; cx < gridSize && boxes[pivotY][cx] == curBox; cx++) {
            document.images['box' + pivotY + cx].src = blankBox.src;
            numBoxesCleared++;
            endX = cx;
        }
        if (startX != endX) {
           dropY = pivotY;
           curDir  = moveX;     
        } else {
           endY = startY;
           for (cy = startY; cy >= 0 && boxes[cy][pivotX] == curBox; cy--) {
               document.images['box' + cy + pivotX].src = blankBox.src;
               endY = cy;
           }
           curDir = moveY;
           dropY = startY;
           diffY = startY * 1 - endY * 1 + 1;
        }
        setTimeout(dropABox,150);
     }
  function dropABox() {
        //alert("drop a box, dropy=" + dropY + ", startx=" + startX + ", endx=" + endX);
        var bi;
        var ncy;
        if (curDir == moveX) {
           ncy = dropY * 1 -1;
           for (cx = startX; cx <= endX; cx++) {
                 if (dropY == 0) {
                    boxes[0][cx] = Math.floor(Math.random() * numBoxes);
                    bi = boxes[0][cx];
                 } else {
                    boxes[dropY][cx] = boxes[ncy][cx];
                    bi = boxes[dropY][cx];
                    document.images['box' + ncy + cx].src = blankBox.src;
                 }
                 document.images['box' + dropY + cx].src = boxImgs[bi].src;
                 playSound = true;
                 //alert("drop a box, dropy=" + dropY + ", cx=" + cx + ", bi=" + bi);
           }
           if (dropY > 0) {
              dropY = ncy;
              document.hammer.stop();
              document.hammer.play();
              setTimeout(dropABox, 150);
           } else {
              if (boxes[startY][pivotX] == curBox) {
                 for (cy = startY; cy >= 0 && boxes[cy][pivotX] == curBox; cy--) {
                    document.images['box' + cy + pivotX].src = blankBox.src;
                    endY = cy;
                    numBoxesCleared++;
                 }
                 curDir = moveY;
                 dropY = startY;
                 diffY = startY * 1 - endY * 1 + 1;
                 setTimeout(dropABox, 150);
               } else {
                  endMove()
               }
           }
        } else {
           //alert("drop a box Y, dropy=" + dropY + ", px=" + pivotX + ", endY=" + endY + ", diffY=" + diffY);
           if (dropY* 1 - diffY*1 < 0) {
                if (dropY >= 0) {
                   //alert("drop a box, dropy=" + dropY + ", px=" + cx + ", bi=" + bi);
                    boxes[dropY][pivotX] = Math.floor(Math.random() * numBoxes);
                    bi = boxes[dropY][pivotX];
                    document.hammer.stop();
                    document.hammer.play();
                    document.images['box' + dropY + pivotX].src = boxImgs[bi].src;
                    dropY = dropY * 1 - 1;
                    setTimeout(dropABox, 150);
                } else {
                    endMove();
                }
           } else {
                ncy = dropY* 1 - diffY*1;
                boxes[dropY][pivotX] = boxes[ncy][pivotX];
                bi = boxes[dropY][pivotX];
                document.images['box' + ncy + pivotX].src = blankBox.src;
                document.hammer.stop();
                document.hammer.play();
                document.images['box' + dropY + pivotX].src = boxImgs[bi].src;
                setTimeout(dropABox, 150);
                dropY = dropY * 1 - 1;
           }
        }
   }
   function endMove() {
       curScore = curScore * 1 + numBoxesCleared;
       if (numBoxesCleared > 5) {
          curScore = curScore *1 + 10;
       } else if (numBoxesCleared > 4) {
          curScore = curScore *1 + 5;
       } else if (numBoxesCleared > 3) {
          curScore = curScore *1 + 3;
       } else if (numBoxesCleared > 2) {
          curScore = curScore *1 + 1;
       }
       displayScore();
       canMove = true;
       moveTimer = 20;
       moreMoves();
   }
   function displayScore () {
        var dig1 = Math.floor(curScore / 100);
        var dig2 = Math.floor(curScore / 10) % 10;
        var dig3 = curScore % 10;
        document.images['udig1'].src = digits[dig1].src;
        document.images['udig2'].src = digits[dig2].src;
        document.images['udig3'].src = digits[dig3].src;
   }
   function displayTimer () {
        if (gameStarted == false) {
            return;
        }
        var dig1 = Math.floor(moveTimer / 10);
        var dig2 = moveTimer % 10;
        document.images['cdig1'].src = digits[dig1].src;
        document.images['cdig2'].src = digits[dig2].src;
        if (moveTimer <= 0) {
             document.buzzer.play();
             moveTimer = 20;
             showHint();
        } else {
              moveTimer = moveTimer * 1 - 1;
        }
        setTimeout(displayTimer, 1000);
   }
   function moreMoves() {
       var isMove = false;
       var toCheck;
       toCheck = gridSize * 1 - 1;
       for(i=0; i<gridSize && isMove == false; i++) {
           for (j=0; j<toCheck && isMove == false; j++) {
               if (boxes[i][j] != isHole && boxes[i][j] == boxes[i][j+1]) {
                   isMove = true;
               }
           }
       }
       for(i=0; i<gridSize && isMove == false; i++) {
           for (j=0; j<toCheck && isMove == false; j++) {
               if (boxes[j][i] != isHole && boxes[j][i] == boxes[j+1][i]) {
                   isMove = true;
               }
           }
       }
       if (isMove == false) {
           endGame();
       }
   }
   function showHint() {
       isMove = false;
       var toCheck;
       toCheck = gridSize * 1 - 1;
       for(i=0; i<gridSize && isMove == false; i++) {
           for (j=0; j<toCheck && isMove == false; j++) {
               if (boxes[i][j] != isHole && boxes[i][j] == boxes[i][j+1]) {
                   hintX = j;
                   hintY = i;
                   //alert ("Found hint hintY=" + hintY + ", hintX=" + hintX);
                   isMove = true;
               }
           }
       }
       for(i=0; i<gridSize && isMove == false; i++) {
           for (j=0; j<toCheck && isMove == false; j++) {
               if (boxes[j][i] != isHole && boxes[j][i] == boxes[j+1][i]) {
                   hintX = i;
                   hintY = j;
                   //alert ("Found hint hintY=" + hintY + ", hintX=" + hintX);
                   isMove = true;
               }
           }
       }
       if (isMove == true) {
           canMove = false;
           hintCount = 0;
           flashHint();
       }
   }
   function flashHint() {
         //alert ("flash hint hintY=" + hintY + ", hintX=" + hintX);
         var bi = boxes[hintY][hintX];
         hintCount = hintCount * 1 + 1;
         if (hintCount > 8) {
              canMove = true;
              document.images['box' + hintY + hintX].src = boxImgs[bi].src;
              curScore = curScore * 1 - 3;
              if (curScore < 0) {
                 curScore = 0;
              }
              displayScore();
         } else {
              if (hintCount % 2 == 0) {
                  document.images['box' + hintY + hintX].src = boxImgs[bi].src;
              } else {
                  document.images['box' + hintY + hintX].src = blankBox.src;
              }
              setTimeout(flashHint, 200);
         }
   }
   function endGame() {
       document.forms[0].beginButton.value = 'Begin Game';
       canMove = false;
       gameStarted = false;
       alert("Game Over!");
   }

