﻿/* Written by Greg Thatcher, http://www.GregThatcher.com */

    // number of pixels to move a shuttle
    var theShuttleMovement = 24;
    var theSelectedPeg = null;
    var theirBoard = new Board();
    var theGameOver = false;
            
    theirBoard.Initialize();

    function CheckIfRedWon()
    {
        var redCount = 0;
        for (i = 0; i < theRedPieceCount; i++)
        {
            if (theirBoard.redPieces[i].shuttle == 8)
            {
                redCount++;
            }
        }
        return (redCount == theRedPieceCount);
    }
    function CheckIfGreenWon()
    {
        var greenCount = 0;
        for (i = 0; i < theGreenPieceCount; i++)
        {
            if (theirBoard.greenPieces[i].shuttle == 0)
            {
                greenCount++;
            }
        }
        return (greenCount == theGreenPieceCount);
    }
    /* For some reason, I get an error when the postback tries to call this
    as a method of Board, so I'm making it a global function */
    function CheckGameOver()
    {
        if (CheckIfGreenWon())
        {
            theGameOver = true;
            monsterSaysComputerWon();
            alert ("I win!");
            return true;
        }
        else if (CheckIfRedWon())
        {
            theGameOver = true;
            monsterSaysYouWon();
            alert ("You win!");
            return true;
        }
        
        return false;
    }

    function showFrames (currentFrame, endFrame, speed)
    {
        if (currentFrame <= endFrame)
        {
            showFrame(currentFrame);
            currentFrame++;
            var functionCall = "showFrames(" + currentFrame + "," + endFrame+ "," + speed  + ")";
            // alert (functionCall);
            setTimeout(functionCall, speed);
        }
    }

    var theSpeed = 100;
    var theHalPegMove;
    var theHalShuttleMove;
    function timerPegMove ()
    {
        var xOffset;
        var yOffset;
        if (theHalPegMove.currentFrame <= theHalPegMove.endFrame)
        {
            switch (theHalPegMove.state)
            {
                case 1: // picking up peg
                    xOffset = theHalPegMove.startPegLeft - theHalPegMove.originalPegLeft;
                    yOffset = theHalPegMove.startPegTop - theHalPegMove.originalPegTop;
                    if (theHalPegMove.usingRightArm)
                    {
                        AddChannelOffset (38, 0, yOffset); //hand
                        AddChannelOffset (39, 0, yOffset); //arm
                    }
                    else
                    {
                        AddChannelOffset (37, 0, yOffset); //hand
                        AddChannelOffset (40, 0, yOffset); //arm
                    }
                    showFrame(theHalPegMove.currentFrame, xOffset);
                    if (theHalPegMove.currentFrame == theHalPegMove.pickupAndGrabFrame)
                    {
                        theHalPegMove.pegDiv.style.visibility = "hidden";
                    }

                    theHalPegMove.currentFrame++;
                    if (theHalPegMove.currentFrame > theHalPegMove.endPickupFrame)
                    {
                        theHalPegMove.state++;
                        // skip 2 frames
                        theHalPegMove.currentFrame = theHalPegMove.startPutDownFrame;
                    }
                    break;
                case 2: // picked up peg, now put it down
                    xOffset = theHalPegMove.destPegLeft - theHalPegMove.originalDestPegLeft;
                    yOffset = theHalPegMove.destPegTop - theHalPegMove.originalDestPegTop;
                    if (theHalPegMove.usingRightArm)
                    {
                        AddChannelOffset (38, 0, yOffset); //hand
                        AddChannelOffset (39, 0, yOffset); //arm
                    }
                    else
                    {
                        AddChannelOffset (37, 0, yOffset); //hand
                        AddChannelOffset (40, 0, yOffset); //arm
                    }
                    showFrame(theHalPegMove.currentFrame, xOffset);
                    if (theHalPegMove.currentFrame == theHalPegMove.startPutDownFrame)
                    {
                        moveComputerPeg (theHalPegMove.fromShuttle, theHalPegMove.fromHole, theHalPegMove.toShuttle, theHalPegMove.toHole);
                        theHalPegMove.oldZIndex = theHalPegMove.destPegDiv.style.zIndex;
                        theHalPegMove.destPegDiv.style.zIndex = 349;
                    }
                    theHalPegMove.currentFrame++;
                    if (theHalPegMove.currentFrame > theHalPegMove.endFrame)
                    {
                        theHalPegMove.destPegDiv.style.zIndex = theHalPegMove.oldZIndex;
                        theHalPegMove.state++;
                    }
                    break;
                
                default:
                    showFrame(theHalPegMove.currentFrame);
                    theHalPegMove.currentFrame++;
                    break;
            }
            var functionCall = "timerPegMove()";
            // alert (functionCall);
            setTimeout(functionCall, theSpeed);
        }
        else
        {
            // monsterSaysItsYourMove();        
            // reset pegDiv to be visible again so future pegs that move there will be visible
            theHalPegMove.pegDiv.style.visibility = "visible";
            theWaitForComputerMove = false;
            CheckGameOver();
            if (!theGameOver)
            {
                monsterSaysItsYourMove();        
            }
        }
    
    }


    function timerShuttleMove (shuttle, shift, currentFrame, endFrame, speed)
    {
        var newPos;
        if (theHalShuttleMove.currentFrame <= theHalShuttleMove.endPushFrame)
        {
            xOffset = theHalShuttleMove.startShuttleLeft - theHalShuttleMove.originalShuttleLeft;
            yOffset = theHalShuttleMove.startShuttleTop - theHalShuttleMove.originalShuttleTop;
            if (theHalShuttleMove.shift > 0) // pushing from Monster Right
            {
                AddChannelOffset (38, 0, yOffset); //hand
                AddChannelOffset (39, 0, yOffset); //arm
            }
            else
            {
                AddChannelOffset (37, 0, yOffset); //hand
                AddChannelOffset (40, 0, yOffset); //arm
            }
            
            showFrame(theHalShuttleMove.currentFrame, xOffset);
            if (theHalShuttleMove.currentFrame >= theHalShuttleMove.endPushFrame-1)
            {
                newPos = parseInt(theHalShuttleMove.shuttleDiv.style.left) + theHalShuttleMove.shift * 8;
                theHalShuttleMove.shuttleDiv.style.left = newPos + "px";
                
            }
            theHalShuttleMove.currentFrame++;
            var functionCall = "timerShuttleMove()";
            // alert (functionCall);
            setTimeout(functionCall, theHalShuttleMove.speed);
        }
        else
        {
            // monsterSaysItsYourMove();        
            // moveComputerShuttle (theHalShuttleMove.shuttle, theHalShuttleMove.shift)
            newPos = parseInt(theHalShuttleMove.shuttleDiv.style.left) + theHalShuttleMove.shift * 8;
            theHalShuttleMove.shuttleDiv.style.left = newPos + "px";
            moveComputerShuttle2(theHalShuttleMove.shuttle, theHalShuttleMove.shift);
            theWaitForComputerMove = false;
            CheckGameOver();
            if (!theGameOver)
            {
                monsterSaysItsYourMove();        
            }
        }
    
    }
    
    function HalShuttleMove (shuttle, shift)
    {
        this.shuttle = shuttle;
        this.shift = shift;
        this.speed = theSpeed;
        if (shift > 0)
        {
            this.startPushFrame = theStartPushRightFrame;
            this.endPushFrame = theEndPushRightFrame;
        }
        else
        {
            this.startPushFrame = theStartPushLeftFrame;
            this.endPushFrame = theEndPushLeftFrame;
        }
        // top:293px; left:190px    
        this.originalShuttleTop = 293;
        this.originalShuttleLeft = 190;
        this.shuttle_id = "shuttle_" + this.shuttle;
        this.shuttleDiv = document.getElementById(this.shuttle_id)
        
        this.startShuttleTop  = parseInt(this.shuttleDiv.style.top);
        this.startShuttleLeft = parseInt(this.shuttleDiv.style.left);

        this.currentFrame = this.startPushFrame;        
        
    }

    function makeHalShuttleMove (shuttle, shift)
    {
        theHalShuttleMove = new HalShuttleMove(shuttle, shift);
        timerShuttleMove ();
    }

    function HalPegMove (fromshuttle, fromhole, toshuttle, tohole)
    {
        this.fromShuttle = fromshuttle;
        this.fromHole = fromhole;
        this.toShuttle = toshuttle;
        this.toHole = tohole;
        this.state = 1;
        
        this.peg_id = "peg_" + fromshuttle + "_" + fromhole;
        this.pegDiv = document.getElementById(this.peg_id);
        
        this.destPeg_id = "peg_" + toshuttle + "_" + tohole;
        this.destPegDiv = document.getElementById(this.destPeg_id);
        
        if (/*toshuttle == 0 ||*/ toshuttle == 8)
        {
            if (tohole >= 2)
            // if (tohole > -1)
            {
                this.startFrame = theLeftArmPickupPegStartFrame;
                this.endPickupFrame = theLeftArmPickupPegEndFrame
                this.pickupAndGrabFrame = theLeftArmPickupAndGrabPeg;
                this.startPutDownFrame = theLeftArmPutDownPegStartFrame;
                this.endFrame   = theLeftArmPutDownPegEndFrame;
                this.originalPegLeft = 385;
                this.originalPegTop = 290;
                this.originalDestPegLeft = 409;
                this.originalDestPegTop = 290;
                this.usingRightArm = false;
            }
            else
            {
                this.startFrame = theRightArmPickupPegStartFrame;
                this.pickupAndGrabFrame = theRightArmPickupAndGrabPeg;
                this.startPutDownFrame = theRightArmPutDownPegStartFrame;
                this.endPickupFrame = theRightArmPickupPegEndFrame
                this.endFrame   = theRightArmPutDownPegEndFrame;
                this.originalPegLeft = 265;
                this.originalPegTop = 290;
                this.originalDestPegLeft = 241;
                this.originalDestPegTop = 290;
                this.usingRightArm = true;
            }
            this.startPegTop = parseInt(this.pegDiv.style.top);
            this.startPegLeft = parseInt(this.pegDiv.style.left);
            this.destPegTop = parseInt(this.destPegDiv.style.top);
            this.destPegLeft = parseInt(this.destPegDiv.style.left);
        }
        else
        {
            if (tohole >= 4)
            // if (tohole > -1)
            {
                this.startFrame = theLeftArmPickupPegStartFrame;
                this.pickupAndGrabFrame = theLeftArmPickupAndGrabPeg;
                this.startPutDownFrame = theLeftArmPutDownPegStartFrame;
                this.endPickupFrame = theLeftArmPickupPegEndFrame
                this.endFrame   = theLeftArmPutDownPegEndFrame;
                this.originalPegLeft = 385;
                this.originalPegTop = 290;
                this.originalDestPegLeft = 409;
                this.originalDestPegTop = 290;
                this.usingRightArm = false;
            }
            else
            {
                this.startFrame = theRightArmPickupPegStartFrame;
                this.pickupAndGrabFrame = theRightArmPickupAndGrabPeg;
                this.startPutDownFrame = theRightArmPutDownPegStartFrame;
                this.endPickupFrame = theRightArmPickupPegEndFrame
                this.endFrame   = theRightArmPutDownPegEndFrame;
                this.originalPegLeft = 265;
                this.originalPegTop = 290;
                this.originalDestPegLeft = 241;
                this.originalDestPegTop = 290;
                this.usingRightArm = true;
            }
            this.startPegTop = parseInt(this.pegDiv.style.top) + parseInt(this.pegDiv.parentNode.style.top);
            this.startPegLeft = parseInt(this.pegDiv.style.left) + parseInt(this.pegDiv.parentNode.style.left);
            this.destPegTop = parseInt(this.destPegDiv.style.top) + parseInt(this.destPegDiv.parentNode.style.top);
            this.destPegLeft = parseInt(this.destPegDiv.style.left) + parseInt(this.destPegDiv.parentNode.style.left);
        }
        this.currentFrame = this.startFrame;        
        
    }
    
    function makeHalPegMove (fromshuttle, fromhole, toshuttle, tohole)
    {
    
        // alert ("Left " + originalPegLeft + " Top " + originalPegTop);
        
        theHalPegMove = new HalPegMove (fromshuttle, fromhole, toshuttle, tohole);
        timerPegMove ();
        // showFrames(startFrame, endFrame, 1000);    
        // moveComputerPeg (fromshuttle, fromhole, toshuttle, tohole);
    }

    function moveComputerPeg (fromshuttle, fromhole, toshuttle, tohole)
    {
        var peg_id;
        var pegDiv;

        theirBoard.computerMovedMiddleShuttle = false;

        peg_id = "peg_" + fromshuttle + "_" + fromhole;
        pegDiv = document.getElementById(peg_id);
        pegDiv.innerHTML = "";
        
        peg_id = "peg_" + toshuttle + "_" + tohole;
        pegDiv = document.getElementById(peg_id);
        
        
        // alert (iOldPegIndex);
        if (showShortPeg(toshuttle, tohole))
        {
            //pegDiv.innerHTML = '<img src="../../Games/Shuttles/art/pegs/A42.gif" alt="GreenPeg"  onclick="return clickPeg(this);"/>';
            pegDiv.innerHTML = '<img src="../../Games/Shuttles/art/pegs/A42.gif" alt="GreenPeg"  />';
        }
        else
        {
            // pegDiv.innerHTML = '<img src="../../Games/Shuttles/art/pegs/A37.gif" alt="GreenPeg"  onclick="return clickPeg(this);"/>';
            pegDiv.innerHTML = '<img src="../../Games/Shuttles/art/pegs/A37.gif" alt="GreenPeg"  />';
        }
        
        var iOldPegIndex = -1;        
        var i;
        for (i = 0; i < theGreenPieceCount; i++)
        {
            if (theirBoard.greenPieces[i].shuttle == fromshuttle &&
                theirBoard.greenPieces[i].hole == fromhole)
            {
                iOldPegIndex = i;
                break;
            }
        }
        //alert ("Old Peg Index" + iOldPegIndex);
        theirBoard.MovePeg(true, iOldPegIndex, toshuttle, tohole);
        // This causes Javascript error -- why?
        // ShowDescription ();
        //alert ("Done Old Peg Index" + iOldPegIndex);
        /*
        var greenPieceCount = 0;
        for (i = 0; i < theGreenPieceCount; i++)
        {
            if (theirBoard.greenPieces[i].shuttle == 0)
            {
                greenPieceCount++;
            }
        }
        if (greenPieceCount == theGreenPieceCount)
        {
            theGameOver = true;
            monsterSaysComputerWon();
            alert ("I win!");
        }
        */
    }
    function moveComputerShuttle2 (shuttle, shift)
    {
        theirBoard.computerMovedMiddleShuttle = false;
        if (shuttle == 4)
        {
            theirBoard.computerMovedMiddleShuttle = true;
        }
        theirBoard.MoveShuttle (shuttle, shift);
    }
    function moveComputerShuttle (shuttle, shift)
    {
        var divName = "shuttle_" + shuttle;
        // alert (divName);
        var div = document.getElementById(divName);
        var xPos = parseInt(div.style.left);
        xPos += (shift * theShuttleMovement);
        div.style.left = (xPos + "px");
        
        theirBoard.computerMovedMiddleShuttle = false;
        if (shuttle == 4)
        {
            theirBoard.computerMovedMiddleShuttle = true;
        }
        theirBoard.MoveShuttle (shuttle, shift);

        // this causes JavaScript error; Why?
        //ShowDescription ();
    }
    
    function ShowDescription ()
    {
        //document.getElementById("TextAreaDescription").value = theirBoard.GetDescription();
    }
    function NotifyUserToWait ()
    {
        alert ("Please wait for computer opponent to complete it's move.");
        return;
    }
    function clickPeg(image)
    {
        if (theWaitForComputerMove)
        {
            NotifyUserToWait ();
            return;
        }
        if (CheckGameOver())
        {
            return;
        }
        clearSelectedPeg ();
        var selectorDiv = document.getElementById("pegSelector");
        selectorDiv.style.visibility = "visible";
        image.parentNode.appendChild(selectorDiv);
        theSelectedPeg = image;
        
        // do I need this for IE???
        // event.cancelBubble=true;
        return false;
    }
    
    function moveShuttleLeft(div_id, shuttle)
    {
        if (theWaitForComputerMove)
        {
            NotifyUserToWait ();
            return;

        }
        if (CheckGameOver())
        {
            return;
        }
        clearSelectedPeg ();
        var div = document.getElementById(div_id);
        var xPos = parseInt(div.style.left);
        xPos -= theShuttleMovement;
        div.style.left = (xPos + "px");
        
        theirBoard.MoveShuttle (shuttle, -1);

        ShowDescription ();
        SendMove();
    }
    
    function moveShuttleRight(div_id, shuttle)
    {
        if (theWaitForComputerMove)
        {
            NotifyUserToWait ();
            return;

        }
        if (CheckGameOver())
        {
            return;
        }
        
        clearSelectedPeg ();
        var div = document.getElementById(div_id);
        var xPos = parseInt(div.style.left);
        xPos += theShuttleMovement;
        div.style.left = (xPos + "px");
        
        theirBoard.MoveShuttle(shuttle, 1);        
            
        ShowDescription ();
        SendMove();
        
    }
    function showShortPeg (shuttle, hole)
    {
       //alert ("Shuttle*" + shuttle + "*hole*" + hole);

        switch (shuttle)
        {
            case 5:
                switch (hole)
                {
                    case 2:
                        return true;
                    case 3:
                        return true;
                    case 6:
                        return true;
                    case 7:
                        return true;
                    default:
                        return false;
                }
                break;
            case 6:
                switch (hole)
                {
                    case 0:
                        return true;
                    case 3:
                        return true;
                    case 4:
                        return true;
                    case 8:
                        return true;
                    default:
                        return false;
                }
                break;
            case 7:
                switch (hole)
                {
                    case 2:
                        return true;
                    case 5:
                        return true;
                    case 6:
                        return true;
                    default:
                        return false;
                }
                break;
             default:
                break;
        }
        return false;
    }
    
    function GetRedPegIndexFromImageID(oldParentID)
    {
        var re = new RegExp("peg_(\\d)_(\\d)");
        var myString = oldParentID;
        myString = myString.replace(re, "$1,$2");
        //alert (myString);
        var myValues = myString.split(",");
        //alert (myValues[0]);
        //alert (myValues[1]);
            
        for (i = 0; i < theRedPieceCount; i++)
        {
            var pos = theirBoard.redPieces[i];
            if (pos.shuttle == myValues[0] && pos.hole == myValues[1])
            {
                return i;
            }
        }
    }
    function isHoleOccupied (shuttle, hole)
    {
        for (i = 0; i < theRedPieceCount; i++)
        {
            var pos = theirBoard.redPieces[i];
            if (pos.shuttle == shuttle && pos.hole == hole)
            {
                return true;
            }
        }
        for (i = 0; i < theGreenPieceCount; i++)
        {
            var pos = theirBoard.greenPieces[i];
            if (pos.shuttle == shuttle && pos.hole == hole)
            {
                return true;
            }
        }
        
        return false;
    }
        
    function clickHole (shuttle, hole)
    {
        var peg_id = "peg_" + shuttle + "_" + hole;
        var pegDiv = document.getElementById(peg_id);
        
        if (theSelectedPeg != null)
        {
            if (isHoleOccupied (shuttle, hole))
            {
                alert ("That is not a legal move!  Please read rules below.");
                clearSelectedPeg ();
                return;
            }
            var oldParentNode = theSelectedPeg.parentNode;
            var iOldPegIndex = GetRedPegIndexFromImageID(oldParentNode.id);
            // alert (iOldPegIndex);
            if (showShortPeg(shuttle, hole))
            {
                pegDiv.innerHTML = '<img src="../../Games/Shuttles/art/pegs/A41.gif" alt="RedPeg"  onclick="return clickPeg(this);"/>';
            }
            else
            {
                pegDiv.innerHTML = '<img src="../../Games/Shuttles/art/pegs/A38.gif" alt="RedPeg"  onclick="return clickPeg(this);"/>';
            }
            
            clearSelectedPeg ();
            // must do this after clearSelectedPeg, so we don't erase "pegSelector"
            oldParentNode.innerHTML = "";
            
            theirBoard.MovePeg(false, iOldPegIndex, shuttle, hole);
            
            ShowDescription ();
            if (CheckGameOver())
            {
                return;
            }

            SendMove();

        }
        
    }
    function clearSelectedPeg ()
    {
        if (theSelectedPeg != null)
        {
                theSelectedPeg = null;
                var mainDiv = document.getElementById("main");
                var selectorDiv = document.getElementById("pegSelector");
                mainDiv.appendChild(selectorDiv);
                selectorDiv.style.visibility = "hidden";
                return true;
        }
        return false;
    }

    function GetValueFromXmlDoc (xmlDoc, key)
    {
        var nodes = xmlDoc.getElementsByTagName(key); 
        return nodes[0].firstChild.nodeValue;
    }

    function UndoLastMove ()
    {

        if (theirBoard.lastMove.isShuttleMove == true)
        {
            var divName = "shuttle_" + theirBoard.lastMove.shuttle;
            var div = document.getElementById(divName);
            var xPos = parseInt(div.style.left);
            xPos -= (theirBoard.lastMove.shiftBy * theShuttleMovement);
            div.style.left = (xPos + "px");
            theirBoard.MoveShuttle (theirBoard.lastMove.shuttle, -theirBoard.lastMove.shiftBy);
        }
        else
        {
            var peg_id;
            var pegDiv;

            var fromshuttle = theirBoard.lastMove.toShuttle;
            var toshuttle   = theirBoard.lastMove.fromShuttle;
            var fromhole    = theirBoard.lastMove.toHole;
            var tohole      = theirBoard.lastMove.fromHole;

            peg_id = "peg_" + fromshuttle + "_" + fromhole;
            pegDiv = document.getElementById(peg_id);
            pegDiv.innerHTML = "";
        
            peg_id = "peg_" + toshuttle + "_" + tohole;
            pegDiv = document.getElementById(peg_id);
        
            if (showShortPeg(toshuttle, tohole))
            {
                pegDiv.innerHTML = '<img src="../../Games/Shuttles/art/pegs/A41.gif" alt="RedPeg"  onclick="return clickPeg(this);"/>';
            }
            else
            {
                pegDiv.innerHTML = '<img src="../../Games/Shuttles/art/pegs/A38.gif" alt="RedPeg"  onclick="return clickPeg(this);"/>';
            }
        
            var iOldPegIndex = -1;        
            var i;
            for (i = 0; i < theRedPieceCount; i++)
            {
                if (theirBoard.redPieces[i].shuttle == fromshuttle &&
                    theirBoard.redPieces[i].hole == fromhole)
                {
                    iOldPegIndex = i;
                    break;
                }
            }
            
            //alert ("Old Peg Index" + iOldPegIndex);
            theirBoard.MovePeg(false, iOldPegIndex, toshuttle, tohole);
            clearSelectedPeg ();
        }
        
    }
    
    // client callback
    function ProcessMove (output, context) 
    {
        var xmlDoc = createXmlDocument(output);
        var movetype;
        var shuttle;
        var shift;
        var fromshuttle;
        var fromhole;
        var toshuttle;
        var tohole;
        /* <moveresult><status>Success</status><computermove><movetype>shuttle</movetype><shuttle>1</shuttle><shift>1</shift></computermove></moveresult>*/
        var status = GetValueFromXmlDoc (xmlDoc, "status");
        //document.getElementById("TextAreaDescription").value = output;
        if (status == "success")
        {
            movetype = GetValueFromXmlDoc (xmlDoc, "movetype");
            if (movetype == "shuttle")
            {
                shuttle = parseInt(GetValueFromXmlDoc (xmlDoc, "shuttle"));
                shift = parseInt(GetValueFromXmlDoc (xmlDoc, "shift"));
                //alert ("shift = " + shift);
                //moveComputerShuttle (shuttle, shift)
                makeHalShuttleMove (shuttle, shift);
                //alert ("Moved a Shuttle!");
            }
            else if (movetype == "peg")
            {
                fromshuttle = parseInt(GetValueFromXmlDoc (xmlDoc, "fromshuttle"));
                fromhole = parseInt(GetValueFromXmlDoc (xmlDoc, "fromhole"));
                toshuttle = parseInt(GetValueFromXmlDoc (xmlDoc, "toshuttle"));
                tohole = parseInt(GetValueFromXmlDoc (xmlDoc, "tohole"));
                //alert("Before moving Computer Peg");
                //moveComputerPeg (fromshuttle, fromhole, toshuttle, tohole);
                makeHalPegMove (fromshuttle, fromhole, toshuttle, tohole);
                //alert("After moving Computer Peg");
                //alert ("Moved a Peg!");
            }
            else
            {
                alert ("There is a problem with the system.  Please try again later.");
                UndoLastMove ();
                theWaitForComputerMove = false;
                if (!theGameOver)
                {
                    monsterSaysItsYourMove();        
                }
            }
        }
        else
        {
            alert ("That is not a legal move.  Please read rules below.");
            UndoLastMove ();
            theWaitForComputerMove = false;
            if (!theGameOver)
            {
                monsterSaysItsYourMove();        
            }

        }
        
        //alert (x.childNodes[0].childNodes[0].nodeValue);
    }
    // client error callback
    function ShowProcessMoveError (error, context)
    {
        alert ("An error occured!");
    }
