function doMove($post = "")
{
    global $board, $isPromoting, $doUndo, $history, $numMoves, $db, $db_prefix;
    if ($post != "") {
        $_POST = $post;
    }
    // if moving en-passant
    // (ie: if pawn moves diagonally without replacing anything)
    if (($board[$_POST['from_row']][$_POST['from_col']] & COLOR_MASK) == PAWN && $_POST['to_col'] != $_POST['from_col'] && $board[$_POST['to_row']][$_POST['to_col']] == 0) {
        // delete eaten pawn
        $board[$_POST['from_row']][$_POST['to_col']] = 0;
        if ($board[$_POST['from_row']][$_POST['to_col']] & BLACK) {
            $tmpColor = "black";
        } else {
            $tmpColor = "white";
        }
        $tmpPiece = getPieceName($board[$_POST['from_row']][$_POST['to_col']]);
    }
    // move piece to destination, replacing whatever's there
    if ($board[$_POST['to_row']][$_POST['to_col']] != 0) {
        if ($board[$_POST['to_row']][$_POST['to_col']] & BLACK) {
            $tmpColor = "black";
        } else {
            $tmpColor = "white";
        }
        $tmpPiece = getPieceName($board[$_POST['to_row']][$_POST['to_col']]);
    }
    $board[$_POST['to_row']][$_POST['to_col']] = $board[$_POST['from_row']][$_POST['from_col']];
    /* delete piece from old position */
    $board[$_POST['from_row']][$_POST['from_col']] = 0;
    /* if not Undoing, but castling */
    if ($doUndo != "yes" && ($board[$_POST['to_row']][$_POST['to_col']] & COLOR_MASK) == KING && $_POST['to_col'] - $_POST['from_col'] == 2) {
        /* castling to the right, move the right rook to the left side of the king */
        $board[$_POST['to_row']][5] = $board[$_POST['to_row']][7];
        /* delete rook from original position */
        $board[$_POST['to_row']][7] = 0;
    } elseif ($doUndo != "yes" && ($board[$_POST['to_row']][$_POST['to_col']] & COLOR_MASK) == KING && $_POST['from_col'] - $_POST['to_col'] == 2) {
        /* castling to the left, move the left rook to the right side of the king */
        $board[$_POST['to_row']][3] = $board[$_POST['to_row']][0];
        /* delete rook from original position */
        $board[$_POST['to_row']][0] = 0;
    }
    return true;
}
            // Flag the game 0
            // mysql_query("UPDATE games set gameok='0' WHERE gameID = ".$gameID);

            mysql_query("DELETE FROM pieces WHERE gameID = ".$gameID);
            for ($i = 0; $i < 8; $i++)
            {
                for ($j = 0; $j < 8; $j++)
                {

                    if ($newBoard[$i][$j] != 0)
                    {
                        if ($newBoard[$i][$j] & BLACK)
                            $tmpColor = "black";
                        else
                            $tmpColor = "white";
                        $tmpPiece = getPieceName($newBoard[$i][$j]);
                        mysql_query("INSERT INTO pieces (gameID, color, piece, row, col) VALUES ($gameID, '$tmpColor', '$tmpPiece', $i, $j)");
				    }
                }
            }
            /*  Flag the game 1*/
            // mysql_query("UPDATE games set gameok='1' WHERE gameID = ".$gameID);
            saveHistory($POST);
            doMove($POST);
            mysql_query("UPDATE games SET lastMove = NOW() WHERE gameID = '$gameID'");

            // Update players time
	        $ptime = mysql_query("SELECT * from history where curColor<>'$playersColor' AND gameID=".$_SESSION['gameID']." ORDER BY timeOfMove DESC limit 1");
            $rowtime = mysql_fetch_array($ptime);
            $cor = $rowtime['curColor'];
            $lastmove = $rowtime['timeOfMove'];
    function saveGame()
    {
        global $board, $playersColor,$db,$db_prefix;


        /*  Flag the game 0*/
        mysql_query("UPDATE {$db_prefix}games set game_ok='0' WHERE game_id = ".$_SESSION['game_id']) or die ("error setting game flag");

        /* clear old data */
        mysql_query("DELETE FROM {$db_prefix}pieces WHERE game_id = ".$_SESSION['game_id']);

        /* save new game data */
        /* for each row... */
        for ($i = 0; $i < 8; $i++)
        {
            /* for each col... */
            for ($j = 0; $j < 8; $j++)
            {
                /* if there's a piece at that pos on the board */
                if ($board[$i][$j] != 0)
                {
                    /* updated the database */
                    if ($board[$i][$j] & BLACK)
                    {
                        $tmpColor = "black";
                    }
                    else
                    {
                        $tmpColor = "white";
                    }

                    $tmpPiece = getPieceName($board[$i][$j]);
                    mysql_query("INSERT INTO {$db_prefix}pieces (game_id, color, piece, row, col) VALUES ('$game_id', '$tmpColor', '$tmpPiece', $i, $j)");
                }
            }
        }

        /* update lastMove timestamp */
        updateTimestamp();

        //Update players time
        $p = mysql_query("SELECT * from {$db_prefix}history where cur_color<>'$playersColor' AND game_id='$game_id' ORDER BY time_of_move DESC limit 1");
        $row = mysql_fetch_array($p);

        $cor = $row['cur_color'];
        $lastmove = $row['time_of_move'];

        if(empty($lastmove))
        {
          $lastmove = "0000-00-00 00:00:00";
        }
        //duracao:
        $v = explode(" ",$lastmove);
        $hora = explode(":",$v[1]);
        $data = explode("-",$v[0]);

        if ($lastmove == 0)
        {
            $inicio = mktime(date("H"),date("i"),date("s"),date("m"),date("d"),date("Y"));
        }
        else
        {
            $inicio = mktime($hora[0],$hora[1],$hora[2],$data[1],$data[2],$data[0]);
        }

        $fim = mktime(date("H"),date("i"),date("s"),date("m"),date("d"),date("Y"));

        $dif = $fim-$inicio;

        if ($playersColor == "white")
        {
            mysql_query("UPDATE {$db_prefix}games set time_white=time_white+$dif WHERE game_id=".$_SESSION['game_id']);
        }
        else
        {
            mysql_query("UPDATE {$db_prefix}games set time_black=time_black+$dif WHERE game_id=".$_SESSION['game_id']);
        }
        //OK, we have to set whose turn is next
        $sql = $db->Prepare("select white_player,black_player,whose_turn from {$db_prefix}games WHERE game_id=?");
        $sql_arr = array($_SESSION['game_id']);
        $res = $db->Execute($sql,$sql_arr);
        db_op_result($res,__LINE__,__FILE__);
        $turns = $res->fields;
        if($turns['white_player'] == $_SESSION['player_id'])
        {
            $whos_next = $turns['black_player'];
        }
        else
        {
           $whos_next = $turns['white_player'];
        }
        if($turns['whose_turn'] != $_SESSION['player_id'])
        {
           adminlog(3002,"POSSIBLE CHEAT! ".$turns['whose_turn']." does not match posting player ".$_SESSION['player_id']."in Game ID ".$_SESSION['game_id']);

        }

        //  Flag the game 1 - open it back up again
       $res =  mysql_query("UPDATE {$db_prefix}games set game_ok='1',whose_turn='$whos_next' WHERE game_id = ".$_SESSION['game_id']);


        //for some reason I reach THIS point only when accepting an invitation?? yeesh...
        //obviously I need to seriously re-do the chess board submission from JS ....

    }
function saveGame()
{
    global $CFG_TABLE;
    global $board, $playersColor;
    /* old PHP versions don't have _POST, _GET and _SESSION as auto_globals */
    if (!minimum_version("4.1.0")) {
        global $_POST, $_GET, $_SESSION;
    }
    /* clear old data */
    mysql_query("DELETE FROM " . $CFG_TABLE[pieces] . " WHERE gameID = " . $_SESSION['gameID']);
    /* save new game data */
    /* for each row... */
    for ($i = 0; $i < 8; $i++) {
        /* for each col... */
        for ($j = 0; $j < 8; $j++) {
            /* if there's a piece at that pos on the board */
            if ($board[$i][$j] != 0) {
                /* updated the database */
                if ($board[$i][$j] & BLACK) {
                    $tmpColor = "black";
                } else {
                    $tmpColor = "white";
                }
                $tmpPiece = getPieceName($board[$i][$j]);
                mysql_query("INSERT INTO " . $CFG_TABLE[pieces] . " (gameID, color, piece, row, col) VALUES (" . $_SESSION['gameID'] . ", '{$tmpColor}', '{$tmpPiece}', {$i}, {$j})");
            }
        }
    }
    /* update lastMove timestamp */
    updateTimestamp();
}
function drawboard()
{
global $board, $playersColor, $numMoves, $MSG_LANG, $confirm_move,$history,$db,$db_prefix;
//OK for this shit here, let's do it a little more elegantly-
//we have to add pref_confirm_move in player preference table first
//and fix the get_player_data query to reflect same
//maybe we should just load the whole nine yards....

if ($confirm_move == FALSE || $_SESSION['pref_confirm_move'] == 0)
        $confirm_move = 0;
elseif ($confirm_move == FALSE || $_SESSION['pref_confirm_move'] == 1)
        $confirm_move = 1;
elseif ($confirm_move == TRUE || $_SESSION['pref_confirm_move'] == 0)
        $confirm_move = 0;
else
        $confirm_move = 1;


        /* find out if it's the current player's turn */
        if (( (($numMoves == -1) || (($numMoves % 2) == 1)) && ($playersColor == "white"))
                || ((($numMoves % 2) == 0) && ($playersColor == "black")) )
            $isPlayersTurn = true;
        else
            $isPlayersTurn = false;

        /* determine who's perspective of the board to show */
        if ($_SESSION['isSharedPC'] && !$isPlayersTurn)
        {
            if ($playersColor == "white")
                $perspective = "black";
            else
                $perspective = "white";
        }
        else
        {
            $perspective = $playersColor;
        }

        /* NOTE: if both players are using the same PC, in a sense it's always the players turn */
        if ($_SESSION['isSharedPC'])
            $isPlayersTurn = true;

        /* determine if board is disabled */
        $isDisabled = isBoardDisabled();

        echo ("<table border='1' style='border-collapse: collapse' bordercolor='#111111' cellpadding='0' cellspacing='0'>\n");

        if ($isDisabled)
            echo ("<tr bgcolor='#DDDDDD'>");
        else
            echo ("<tr bgcolor='white'>");

        /* setup vars to show player's perspective of the board */
        if ($perspective == "white")
        {
            $topRow = 7;
            $bottomRow = 0;
            $rowStep = -1;

            $leftCol = 0;
            $rightCol = 7;
            $colStep = 1;
        }
        else
        {
            $topRow = 0;
            $bottomRow = 7;
            $rowStep = 1;

            $leftCol = 7;
            $rightCol = 0;
            $colStep = -1;
        }

        /* column headers */
        echo ("<th>&nbsp;</th>");

        /* NOTE: end condition is ($rightCol + $colStep) since we want to output $rightCol */
        for ($i = $leftCol; $i != ($rightCol + $colStep); $i += $colStep)
            echo ("<th>".chr($i + 97)."</th>");

        echo ("</tr>\n");

        /* for each row... */
        /* NOTE: end condition is ($bottomRow + $rowStep) since we want to output $bottomRow */
        for ($i = $topRow; $i != ($bottomRow + $rowStep); $i += $rowStep)
        {
            echo ("<tr>\n");
            if ($isDisabled)
                echo ("<th width='20' bgcolor='#DDDDDD'>".($i+1)."</th>\n");
            else
                echo ("<th width='20' bgcolor='white'>".($i+1)."</th>\n");

            /* for each col... */
            /* NOTE: end condition is ($rightCol + $colStep) since we want to output $rightCol */
            for ($j = $leftCol; $j != ($rightCol + $colStep); $j += $colStep)
            {
                echo ("   <td ");

                /* if board is disabled, show board in grayscale */
                if ($isDisabled)
                {
                    if (($j + ($i % 2)) % 2 == 0)
                        echo ("class='disabled-dark' bgcolor='#444444'>");
                    else
                        echo ("class='disabled-light' bgcolor='#BBBBBB'>");
                }
                else
                {
                    //#772222
                    //#CCBBBB
// Highlight last move:
$p = mysql_query("select highlight_last,highlight_color from {$db_prefix}player_preference where player_id = '".$_SESSION['player_id']."'");
$row = mysql_fetch_array($p);
$showHLight = $row['highlight_last'];
if ($showHLight == '1')
{

$showHLcolor = $row['highlight_color'];
if (($numMoves >=0)&&(($i == $history[$numMoves]['from_row'] && $j == $history[$numMoves]['from_col']) || ($i == $history[$numMoves]['to_row'] && $j == $history[$numMoves]['to_col'])))
{
$cor = "bgcolor='".$showHLcolor."'";
}
else
{
if (($j + ($i % 2)) % 2 == 0)
{
$cor = "class='enabled-dark' bgcolor='#955F22'";
}
else
{
$cor = "class='enabled-light' bgcolor='#E3C58C'";
}
}

echo ("$cor>");
}
else
{
if (($j + ($i % 2)) % 2 == 0)
echo ("class='enabled-dark' bgcolor='#955F22'>");
else
echo ("class='enabled-light' bgcolor='#E3C58C'>");
}
}


                /* if disabled or not player's turn, can't click pieces */
                if (!$isDisabled && $isPlayersTurn)
                {
                    echo ("<a href='JavaScript:squareClicked($confirm_move, $i, $j, ");
                    if ($board[$i][$j] == 0)
                        echo ("true,\"".$MSG_LANG["youdontplaywith"]."\",\"$MSG_LANG[confirmmove]\")'>");
                    else
                        echo ("false,\"".$MSG_LANG["youdontplaywith"]."\",\"$MSG_LANG[confirmmove]\")'>");
                }

                echo ("<img name='pos$i-$j' src='images/pieces/".$_SESSION['theme_set']."/");

                /* if position is empty... */
                if ($board[$i][$j] == 0)
                {
                    /* draw empty square */
                    $tmpALT="blank";
                }
                else
                {
                    /* draw correct piece */
                    if ($board[$i][$j] & BLACK)
                        $tmpALT = "black_";
                    else
                        $tmpALT = "white_";

                    $tmpALT .= getPieceName($board[$i][$j]);
                }

                echo($tmpALT.".gif' height='".$_SESSION['pref_board_size']."' width='".$_SESSION['pref_board_size']."' border='0' alt='".$tmpALT."'>");

                if (!$isDisabled && $isPlayersTurn)
                    echo ("</a>");

                echo ("</td>\n");
            }

            echo ("</tr>\n");
        }

        echo ("</table>\n\n");
    }