/**
 * history_markup()
 * See  decode_history  in  game_logic.php , it was the template for this
 * function. The coded history string is analyzed 2 chars at a time, because
 * the main portion of content will be from-to codes for moves on the board.
 * If a from-code is a round left paranthesis, these 2 bytes and the following
 * 2 contain information of a promotion. Those 4 bytes are considered an "empty"
 * or not counted round, regarding the output move list. 
 */
function history_markup($board, $history, $href_this, $name_white, $name_black, $prompt)
{
    $history .= '__';
    // Add code representing the "?" prompt
    $goto = get_parameter(GET_GOTO);
    $link = '';
    $class = HISTORY_BREAK_UL ? '' : ' class="scrolling"';
    $ret = "<h2><strong>{$name_white}</strong>" . " vs. <strong>{$name_black}</strong></h2>\n" . "<ul{$class}>\n";
    $current_move = 0;
    //... Newly introduced. Routine might need cleanup.
    $promotions = 0;
    // Nr. of promotions, each leading to 4 chars of
    //  $history  being skipped
    $length = strlen($history);
    for ($i = 0; $i < $length; $i += 2) {
        // Extract 2 char instruction from history
        // (Usually from-field and to-field codes or possibly
        // promotion of a pawn into another piece)
        $from_code = substr($history, $i, 1);
        $to_code = substr($history, $i + 1, 1);
        if (HISTORY_BREAK_UL && $i > 0 && ($i - 4 * $promotions) % 80 == 0) {
            $ret .= "</ul><ul>\n";
        }
        switch ($from_code) {
            case '_':
                // Last item, show prompt /////////////////////////
                if ($i % 4 == 0) {
                    if (HISTORY_LINK_BACK) {
                        $link = $href_this;
                    }
                    $ret .= "\t<li>";
                    $nr = i_to_round($history, $i);
                    $ret .= $nr . ': ';
                    if (HISTORY_LINK_BACK) {
                        $ret .= "<a href=\"{$link}\">";
                    } else {
                        $ret .= '<span>';
                    }
                }
                $ret .= $prompt;
                break;
            case '(':
                // Pawn Promotion /////////////////////////////////
                //... This section used to be larger and is now almost
                //... obsolete, integrate into "Move" section!
                $i += 2;
                $promotions++;
                break;
            default:
                // Move ////////////////////////////////////////////
                $new_move = '';
                $current_move++;
                $current_color = $current_move % 2 == WHITES_MOVE;
                $opponent_color = !$current_color;
                $from_field = decode_field($from_code);
                $to_field = decode_field($to_code);
                list($f_row, $f_col) = field_to_rowcol($from_field);
                list($t_row, $t_col) = field_to_rowcol($to_field);
                if ($i % 4 == 0) {
                    if (HISTORY_LINK_BACK) {
                        if ($i < $length) {
                            $link = history_link($href_this, $current_move, WHITES_MOVE);
                        } else {
                            $link = $href_this;
                        }
                    }
                    // Highlight history entry
                    $class = $current_move == $goto ? ' class="selected"' : '';
                    $nr = i_to_round($history, $i);
                    $new_move .= "\t<li>{$nr}: ";
                    if (HISTORY_LINK_BACK) {
                        $new_move .= "<a href=\"{$link}\"{$class}>";
                    } else {
                        $new_move .= '<span>';
                    }
                }
                $piece = $board[$f_row][$f_col];
                $new_move .= piece_glyph($piece);
                $new_move .= strtolower($from_field);
                if ($board[$t_row][$t_col] != '') {
                    $new_move .= ' &#215; ' . piece_glyph($board[$t_row][$t_col]);
                } else {
                    $new_move .= ' &ndash; ';
                }
                $new_move .= strtolower($to_field);
                // Update the board
                $board = apply_move($board, $f_row, $f_col, $t_row, $t_col);
                // Promotion
                if (substr($history, $i + 2, 1) == '(') {
                    $piece = substr($history, $i + 4, 1);
                    $new_move .= piece_glyph($piece);
                    $board[$t_row][$t_col] = substr($history, $i + 4, 1);
                }
                // Do we threaten the other king?
                if (king_in_check($board, !$current_color)) {
                    $new_move .= '+';
                }
                if ($i % 4 == 0) {
                    if (HISTORY_LINK_BACK) {
                        if ($i + 4 < $length) {
                            $link = history_link($href_this, $current_move, BLACKS_MOVE);
                        } else {
                            $link = $href_this;
                        }
                    }
                    $class = $current_move + 1 == $goto ? ' class="selected"' : '';
                    $new_move .= ',';
                    if (HISTORY_LINK_BACK) {
                        $new_move .= "</a> <a href=\"{$link}\"{$class}>";
                    } else {
                        $new_move .= '</span> <span>';
                    }
                } else {
                    if (HISTORY_LINK_BACK) {
                        $new_move .= '</a>';
                    } else {
                        $new_move .= '</span>';
                    }
                    $new_move .= "\n";
                }
                // Reduce opening pawn moves
                $new_move = simplify_history($new_move);
                $ret .= $new_move;
        }
        // switch $from_code
    }
    // for $i
    if (HISTORY_LINK_BACK) {
        $ret .= '</a>';
    } else {
        $ret .= '</span>';
    }
    if (substr($ret, -1, 1) != "\n") {
        $ret .= "\n";
    }
    return $ret . "</ul>\n";
}
/**
 * history_markup()
 * See  decode_history  in  game_logic.php , it was the template for this
 * function.
 */
function history_markup($board, $history, $href_this, $name_white, $name_black, $prompt)
{
    $history .= '__';
    $goto = get_parameter(GET_GOTO);
    $class = HISTORY_BREAK_UL ? '' : ' class="scrolling"';
    $ret = "<h2><strong>{$name_white}</strong>" . " vs. <strong>{$name_black}</strong></h2>\n" . "<ul{$class}>\n";
    $current_move = 0;
    //... Newly introduced. Routine might need cleanup.
    $skipped_turns = 0;
    $length = strlen($history);
    for ($i = 0; $i < $length; $i += 2) {
        $from_code = substr($history, $i, 1);
        $to_code = substr($history, $i + 1, 1);
        $from_field = decode_field($from_code);
        $to_field = decode_field($to_code);
        list($f_row, $f_col) = field_to_rowcol($from_field);
        list($t_row, $t_col) = field_to_rowcol($to_field);
        if (HISTORY_BREAK_UL && $i > 0 && ($i - 4 * $skipped_turns) % 80 == 0) {
            $ret .= "</ul><ul>\n";
        }
        switch ($from_code) {
            case '_':
                // Last item, show prompt /////////////////////////
                if ($i % 4 == 0) {
                    $link = $href_this;
                    $ret .= "\t<li>";
                    $nr = i_to_round($history, $i);
                    $ret .= $nr . ': ';
                    $ret .= "<a href=\"{$link}\">";
                }
                $ret .= $prompt;
                break;
            case '(':
                // Pawn Promotion /////////////////////////////////
                $i += 2;
                $skipped_turns++;
                break;
            default:
                // Move ////////////////////////////////////////////
                $new_move = '';
                $current_move++;
                $current_color = $current_move % 2 == WHITES_MOVE;
                $opponent_color = !$current_color;
                if ($i % 4 == 0) {
                    if ($i < $length) {
                        $link = history_link($href_this, $current_move, WHITES_MOVE);
                    } else {
                        $link = $href_this;
                    }
                    // Highlight history entry
                    $class = $current_move == $goto ? ' class="selected"' : '';
                    $nr = i_to_round($history, $i);
                    $new_move .= "\t<li>{$nr}: <a href=\"{$link}\"{$class}>";
                }
                $piece = $board[$f_row][$f_col];
                $new_move .= piece_glyph($piece);
                $new_move .= strtolower($from_field);
                if ($board[$t_row][$t_col] != '') {
                    $new_move .= ' &#215; ' . piece_glyph($board[$t_row][$t_col]);
                } else {
                    $new_move .= ' &ndash; ';
                }
                $new_move .= strtolower($to_field);
                // Update the board
                $board = apply_move($board, $f_row, $f_col, $t_row, $t_col);
                // Promotion
                if (substr($history, $i + 2, 1) == '(') {
                    $piece = substr($history, $i + 4, 1);
                    $new_move .= piece_glyph($piece);
                    $board[$t_row][$t_col] = substr($history, $i + 4, 1);
                }
                // Do we threaten the other king?
                if (king_in_check($board, !$current_color)) {
                    $new_move .= '+';
                }
                if ($i % 4 == 0) {
                    if ($i + 4 < $length) {
                        $link = history_link($href_this, $current_move, BLACKS_MOVE);
                    } else {
                        $link = $href_this;
                    }
                    $class = $current_move + 1 == $goto ? ' class="selected"' : '';
                    $new_move .= ",</a> <a href=\"{$link}\"{$class}>";
                } else {
                    $new_move .= "</a>\n";
                }
                // Reduce opening pawn moves
                $new_move = simplify_history($new_move);
                $ret .= $new_move;
        }
        // switch $from_code
    }
    // for $i
    $ret .= '</a>';
    if (substr($ret, -1, 1) != "\n") {
        $ret .= "\n";
    }
    return $ret . "</ul>\n";
}