<?php
/**
 * Filename: index.php
 * Function: This file is the main root file for phpDivingLog.
 * Version:  phpDivingLog v1.9 2007-03-07
 * Last Modified: 2007-03-07
 * /***************************************************************************
 * 
 * phpDivingLog
 * Copyright (C) 2006 Lloyd Borrett - http://www.borrett.id.au
 * 
 * Adapted from code by Olaf van Zandwijk - http://enschede.vanzandwijk.net
 * 
 * For use with Diving Log by Sven Knoch - http://www.divinglog.de
 * 
 * This file and all dependant and otherwise related files are part of phpDivingLog.
 * 
 * phpDivingLog is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * phpDivingLog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * /**************************************************************************
 */

// include_once misc file
$misc_filename = "includes/misc.inc.php";
if (!file_exists($misc_filename)) {
    print "<p>Misc file '" . $misc_filename . "' not found.</p>";
    exit;
} 
include_once ($misc_filename);


$get_nr = check_number($_GET['nr']);
if ($get_nr != "") {

    // We have a dive number, so display the dive details

    // include the rtfclass file
    include ('includes/rtfclass.php');

    // Get the dive details from database
    $globals['divenr'] = $get_nr;
    $result = parse_mysql_query('onedive.sql');
    if (count($result) == 0) {
        header("Location: index.php");
    } 

    // Get the dive list from database
    $divelist = parse_mysql_query('divelist.sql');

    /*
	echo "<hr>\n<b>Result:</b><br>\n<code>\n";
	print_r($result);
	echo "</code>\n<br>\n<hr>\n<b>Divelist:</b><br>\n<code>\n";
	print_r($divelist);
	echo "</code>\n<br>\n<hr>\n";
*/

    // Calculate average depth and SAC for this dive
    $profile = $result[0]['Profile'];
    if (!$profile) {
        $averagedepth = "-";
        $sac = "-";
    } else {
        $length = (strlen($profile) / 12);
        $profileint = ($result[0]['ProfileInt'] / 60);
        $divetime = $profileint * $length;
        $start = 0;
        $ydata = 0;
        for ($i = 0; $i < $length; $i++) {
            $ydata = $ydata + (substr(substr($profile, $start, 12), 0, 5) / 100);
            $start += 12;
        } 
        $averagedepth = $ydata / $length;
        $sac = (($result[0]['PresS'] - $result[0]['PresE']) * $result[0]['Tanksize']) / ($divetime * ($averagedepth / 10 + 1));

        if ($_config['length']) {
            $averagedepth = MetreToFeet($averagedepth, 2) . "&nbsp;" . $_lang['unit_length_imp'];
        } else {
            $averagedepth = number_format($averagedepth, 2) . "&nbsp;" . $_lang['unit_length'];
        } 
        if ($_config['volume']) {
            $sac = LitreToCuft($sac, 1) . "&nbsp;" . $_lang['unit_rate_imp'];
        } else {
            $sac = number_format($sac, 2) . "&nbsp;" . $_lang['unit_rate'];
        } 
    } 

    for($i = 0; $i < count($divelist); $i++) {
        $divelist[$i] = $divelist[$i]['Number'];
    } 

    $thisdive = array_search($get_nr, $divelist);

    // Get the page header
    $pagetitle = $_lang['dive_details_pagetitle'] . $result[0]['Number'];
    include($_config['header']);

    echo "<table class=\"divetable\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">\n";

    // First, Previous, Next, Last links and Dive #
    $links = " <tr>\n";
    $links .= "  <td colspan=\"4\">\n";
    $links .= "   <table cellspacing=\"0\" cellpadding=\"0\" width=\"100%\"><tr>\n";

    // First, Previous
    $links .= "    <td width=\"25%\" align=\"left\">";
    if (array_search($get_nr, $divelist) != count($divelist)-1) {
        $links .= "\n     <div class=\"divelogcrumbs\">\n";
        $links .= "      <a href=\"index.php?nr=" . $divelist[count($divelist)-1];
        $links .= "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang['first_dive_linktitle'] . "\">" . $_lang['first'] . "</a>";
        $links .= "&nbsp;|&nbsp;";
        $links .= "<a\n";
        $links .= "       href=\"index.php?nr=" . $divelist[$thisdive + 1];
        $links .= "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang['previous_dive_linktitle'] . "\">" . $_lang['previous'] . "</a>\n";
        $links .= "     </div>\n    ";
    } else {
        $links .= "&nbsp;";
    } 
    $links .= "</td>\n";

    // Dive Log, Dive Sites, Dive Statistics
    $links .= "    <td width=\"50%\" align=\"center\">\n";
    $links .= "     <div class=\"divelogcrumbs\">\n";
    $links .= "      <a href=\"index.php\" class=\"divelogcrumbs\"\n";
    $links .= "       title=\"" . $_lang['dive_log_linktitle'] . "\">" . $_lang['dive_log'] . "</a> | \n";
    $links .= "      <a href=\"divesite.php\" class=\"divelogcrumbs\"\n";
    $links .= "       title=\"" . $_lang['dive_sites_linktitle'] . "\">" . $_lang['dive_sites'] . "</a><br>\n";
    $links .= "      <a href=\"equipment.php\" class=\"divelogcrumbs\"\n";
    $links .= "       title=\"" . $_lang['dive_equip_linktitle'] . "\">" . $_lang['dive_equip'] . "</a> | \n";
    $links .= "      <a href=\"divestats.php\" class=\"divelogcrumbs\"\n";
    $links .= "       title=\"" . $_lang['dive_stats_linktitle'] . "\">" . $_lang['dive_stats'] . "</a>\n";
    $links .= "     </div>\n";
    $links .= "    </td>\n";

    // Next, Last
    $links .= "    <td width=\"25%\" align=\"right\">";
    if (array_search($get_nr, $divelist) != 0) {
        $links .= "\n     <div class=\"divelogcrumbs\">\n";
        $links .= "      <a href=\"index.php?nr=" . $divelist[$thisdive-1];
        $links .= "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang['next_dive_linktitle'] . "\">" . $_lang['next'] . "</a>";
        $links .= "&nbsp;|&nbsp;";
        $links .= "<a\n";
        $links .= "       href=\"index.php?nr=" . $divelist[0];
        $links .= "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang['last_dive_linktitle'] . "\">" . $_lang['last'] . "</a>\n";
        $links .= "     </div>\n    ";
    } else {
        $links .= "&nbsp;";
    } 
    $links .= "</td>\n";

    $links .= "   </tr></table>\n";
    $links .= "  </td>\n";
    $links .= " </tr>\n";

    // Show the links
    echo $links;
    echo " <tr>\n  <td colspan=\"4\" class=\"spacing\">&nbsp;</td>\n </tr>\n";

    // Show Dive Information

    // Show main dive details
    echo " <tr class=\"divetitle\">\n";
    echo "  <td width=\"25%\">" . $_lang['logbook_divedate'] . "</td>\n";
    echo "  <td width=\"25%\">" . $_lang['logbook_entrytime'] . "</td>\n";
    echo "  <td width=\"25%\">" . $_lang['logbook_divetime'] . "</td>\n";
    echo "  <td width=\"25%\">" . $_lang['logbook_depth'] . "</td>\n";
    echo " </tr>\n";

    echo " <tr class=\"divecontent\">\n";
    if ($result[0]['Divedate'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . date($_lang['logbook_divedate_format'], strtotime($result[0]['Divedate'])) . "</td>\n";
    } 
    if ($result[0]['Entrytime'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . $result[0]['Entrytime'] . "</td>\n";
    } 
    if ($result[0]['Divetime'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . $result[0]['Divetime'] . "&nbsp;" . $_lang['unit_time'] . "</td>\n";
    } 
    if ($result[0]['Depth'] == "") {
        echo "  <td>-</td>\n";
    } else {
        if ($_config['length']) {
            echo "  <td>" . MetreToFeet($result[0]['Depth'], 0) . "&nbsp;" . $_lang['unit_length_imp'] . "</td>\n";
        } else {
            echo "  <td>" . $result[0]['Depth'] . "&nbsp;" . $_lang['unit_length'] . "</td>\n";
        } 
    } 
    echo " </tr>\n";

    // Show dive location details
    echo " <tr class=\"divetitle\">\n";
    echo "  <td>" . $_lang['logbook_place'] . "</td>\n";
    echo "  <td colspan=\"2\">" . $_lang['logbook_city'] . "</td>\n";
    echo "  <td>" . $_lang['logbook_country'] . "</td>\n";
    echo " </tr>\n";

    echo " <tr class=\"divecontent\">\n";
    if ($result[0]['Place'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td><a href=\"divesite.php?loc=" . $result[0]['PlaceID'] . "\"\n";
        echo "   title=\"" . $result[0]['Place'];
        echo " " . $_lang['logbook_place_linktitle'] . "\">" . $result[0]['Place'] . "</a></td>\n";
    } 
    if ($result[0]['City'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td colspan=\"2\">" . $result[0]['City'] . "</td>\n";
    } 
    if ($result[0]['Country'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . $result[0]['Country'] . "</td>\n";
    } 
    echo " </tr>\n";

    // Show buddy details
    if ($result[0]['Buddy'] != "") {
        echo " <tr class=\"divetitle\">\n";
        echo "  <td colspan=\"4\">" . $_lang['logbook_buddy'] . "</td>\n";
        echo " </tr>\n";

        echo " <tr class=\"divecontent\">\n";
        echo "  <td colspan=\"4\">" . $result[0]['Buddy'] . "</td>\n";
        echo " </tr>\n";
    } 

    // Dive Pictures
    $globals['logid'] = $result[0]['ID'];
    $divepics = parse_mysql_query('divepics.sql');
    $pics = count($divepics);

    if ($pics != 0) {
        echo " <tr><td colspan=\"4\" class=\"spacing\">&nbsp;</td></tr>\n";

        echo " <tr>\n  <td colspan=\"4\">\n";
        echo "   <center>\n";
        echo "   <a href=\"" . $_config['picpath_web'] . $divepics[0]['Path'];
        echo "\" rel=\"lightbox[others]\"\n   title=\"";
        echo $_lang['divepic_linktitle_pt1'] . "1" . $_lang['divepic_linktitle_pt2'] . $pics . $_lang['divepic_linktitle_pt3'] . $result[0]['Number'];
        echo "\">" . $_lang['divepic_pt1'] . $pics;
        if ($pics == 1) {
            echo $_lang['divepic_pt2s'];
        } else {
            echo $_lang['divepic_pt2p'];
        } 
        echo $_lang['divepic_pt3'] . "</a>\n";

        for($i = 1; $i < $pics; $i++) {
            echo "   <a href=\"" . $_config['picpath_web'] . $divepics[$i]['Path'];
            echo "\" rel=\"lightbox[others]\"\n";
            echo "   title=\"" . $_lang['divepic_linktitle_pt1'] . ($i + 1) . $_lang['divepic_linktitle_pt2'] . $pics;
            echo $_lang['divepic_linktitle_pt3'] . $result[0]['Number'] . "\"></a>\n";
        } 
        echo "   </center>\n";
        echo "  </td>\n </tr>\n";
    } 

    // Dive Profile
    $profile = $result[0]['Profile'];

    if ($profile) {
        echo " <tr><td colspan=\"4\" class=\"spacing\">&nbsp;</td></tr>\n";

        echo " <tr>\n  <td colspan=\"4\">\n";
        echo "   <center>\n";
        echo "   <img src=\"drawprofile.php?nr=" . $get_nr . "\"\n";
        echo "   alt=\"" . $_lang['dive_profile_title'] . $result[0]['Number'] . "\"\n";
        echo "   title=\"" . $_lang['dive_profile_title'] . $result[0]['Number'] . "\">\n";
        echo "   </center>\n";
        echo "  </td>\n </tr>\n";
    } 

    // Conditions
    echo " <tr><td colspan=\"4\" class=\"spacing\">&nbsp;</td></tr>\n";

    echo " <tr class=\"divesection\">\n";
    echo "  <td colspan=\"4\">" . $_lang['dive_sect_conditions'] . "</td>\n";
    echo " </tr>\n";

    // Show weather conditions
    echo " <tr class=\"divetitle\">\n";
    echo "  <td>" . $_lang['logbook_weather'] . "</td>\n";
    echo "  <td>" . $_lang['logbook_visibility'] . "</td>\n";
    echo "  <td>" . $_lang['logbook_altitude'] . "</td>\n";
    echo "  <td>" . $_lang['logbook_airtemp'] . "</td>\n";
    echo " </tr>\n";

    echo " <tr class=\"divecontent\">\n";
    if ($result[0]['Weather'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . $result[0]['Weather'] . "</td>\n";
    } 
    echo "  <td>";
    if (!$_lang['visibility'][($result[0]['Visibility'] - 1)]) {
        echo "-";
    } else {
        echo $_lang['visibility'][($result[0]['Visibility'] - 1)];
    } 
    echo "</td>\n";
    if ($result[0]['Altitude'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . $result[0]['Altitude'] . "</td>\n";
    } 
    if ($result[0]['Airtemp'] == "") {
        echo "  <td>-</td>\n";
    } else {
        if ($_config['temp']) {
            echo "  <td>" . CelsiusToFahrenh($result[0]['Airtemp'], 0) . "&nbsp;" . $_lang['unit_temp_imp'] . "</td>\n";
        } else {
            echo "  <td>" . $result[0]['Airtemp'] . "&nbsp;" . $_lang['unit_temp'] . "</td>\n";
        } 
    } 
    echo " </tr>\n";

    // Show water conditions
    echo " <tr class=\"divetitle\">\n";
    echo "  <td>" . $_lang['logbook_water'] . "</td>\n";
    echo "  <td>" . $_lang['logbook_surface'] . "</td>\n";
    echo "  <td>" . $_lang['logbook_uwcurrent'] . "</td>\n";
    echo "  <td>" . $_lang['logbook_watertemp'] . "</td>\n";
    echo " </tr>\n";

    echo " <tr class=\"divecontent\">\n";
    echo "  <td>";
    if (!$_lang['water'][($result[0]['Water'] - 1)]) {
        echo "-";
    } else {
        echo $_lang['water'][($result[0]['Water'] - 1)];
    } 
    echo "</td>\n";
    if ($result[0]['Surface'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . $result[0]['Surface'] . "</td>\n";
    } 
    if ($result[0]['UWCurrent'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . $result[0]['UWCurrent'] . "</td>\n";
    } 
    if ($result[0]['Watertemp'] == "") {
        echo "  <td>-</td>\n";
    } else {
        if ($_config['temp']) {
            echo "  <td>" . CelsiusToFahrenh($result[0]['Watertemp'], 0) . "&nbsp;" . $_lang['unit_temp_imp'] . "</td>\n";
        } else {
            echo "  <td>" . $result[0]['Watertemp'] . "&nbsp;" . $_lang['unit_temp'] . "</td>\n";
        } 
    } 
    echo " </tr>\n";

    // Breathing
    echo " <tr><td colspan=\"4\" class=\"spacing\">&nbsp;</td></tr>\n";

    echo " <tr class=\"divesection\">\n";
    echo "  <td colspan=\"4\">" . $_lang['dive_sect_breathing'] . "</td>\n";
    echo " </tr>\n";

    // Show tank details
    echo " <tr class=\"divetitle\">\n";
    echo "  <td>" . $_lang['logbook_tanktype'] . "</td>\n";
    echo "  <td>" . $_lang['logbook_tanksize'] . "</td>\n";
    echo "  <td>" . $_lang['logbook_gas'] . "</td>\n";
    echo "  <td>" . $_lang['logbook_avgdepth'] . "</td>\n";
    echo " </tr>\n";

    echo " <tr class=\"divecontent\">\n";
    echo "  <td>";
    if (!$_lang['tanktype'][($result[0]['Tanktype'] - 1)]) {
        echo "-";
    } else {
        echo $_lang['tanktype'][($result[0]['Tanktype'] - 1)];
    } 
    echo "</td>\n";
    if ($result[0]['Tanksize'] == "") {
        echo "  <td>-</td>\n";
    } else {
        if ($_config['volume']) {
            echo "  <td>" . LitreToCuft($result[0]['Tanksize'], 0) . "&nbsp;" . $_lang['unit_volume_imp'] . "</td>\n";
        } else {
            echo "  <td>" . $result[0]['Tanksize'] . " " . $_lang['unit_volume'] . "</td>\n";
        } 
    } 
    if ($result[0]['Gas'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . $result[0]['Gas'] . "</td>\n";
    } 
    if ($averagedepth == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . $averagedepth . "</td>\n";
    } 
    echo " </tr>\n";

    // Show pressure details
    echo " <tr class=\"divetitle\">\n";
    echo "  <td>" . $_lang['logbook_press'] . "</td>\n";
    echo "  <td>" . $_lang['logbook_prese'] . "</td>\n";
    echo "  <td>" . $_lang['logbook_presdiff'] . "</td>\n";
    echo "  <td>" . $_lang['logbook_sac'] . "</td>\n";
    echo " </tr>\n";

    echo " <tr class=\"divecontent\">";
    if ($result[0]['PresS'] == "") {
        echo "  <td>-</td>\n";
    } else {
        if ($_config['pressure']) {
            echo "  <td>" . BarToPsi($result[0]['PresS'], -1) . "&nbsp;" . $_lang['unit_pressure_imp'] . "</td>\n";
        } else {
            echo "  <td>" . $result[0]['PresS'] . "&nbsp;" . $_lang['unit_pressure'] . "</td>\n";
        } 
    } 
    if ($result[0]['PresE'] == "") {
        echo "  <td>-</td>\n";
    } else {
        if ($_config['pressure']) {
            echo "  <td>" . BarToPsi($result[0]['PresE'], -1) . "&nbsp;" . $_lang['unit_pressure_imp'] . "</td>\n";
        } else {
            echo "  <td>" . $result[0]['PresE'] . "&nbsp;" . $_lang['unit_pressure'] . "</td>\n";
        } 
    } 
    if (($result[0]['PresS'] == "") || ($result[0]['PresE'] == "")) {
        echo "  <td>-</td>\n";
    } else {
        $diff = intval($result[0]['PresS']) - intval($result[0]['PresE']);
        if ($_config['pressure']) {
            echo "  <td>" . BarToPsi($diff, -1) . "&nbsp;" . $_lang['unit_pressure_imp'] . "</td>\n";
        } else {
            echo "  <td>" . $diff . "&nbsp;" . $_lang['unit_pressure'] . "</td>\n";
        } 
    } 
    if ($sac == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . $sac . "</td>\n";
    } 
    echo " </tr>\n";

    // Dive Details
    echo " <tr><td colspan=\"4\" class=\"spacing\">&nbsp;</td></tr>\n";

    echo " <tr class=\"divesection\">\n";
    echo "  <td colspan=\"4\">" . $_lang['dive_sect_details'] . "</td>\n";
    echo " </tr>\n";

    echo " <tr class=\"divetitle\">\n";
    echo "  <td>" . $_lang['logbook_entry'] . "</td>\n";
    echo "  <td>" . $_lang['logbook_deco'] . "</td>\n";
    echo "  <td>" . $_lang['logbook_rep'] . "</td>\n";
    echo "  <td>" . $_lang['logbook_surfint'] . "</td>\n";
    echo " </tr>\n";

    echo " <tr class=\"divecontent\">\n";
    echo "  <td>";
    if (!$_lang['entry'][($result[0]['Entry'] - 1)]) {
        echo "-";
    } else {
        echo $_lang['entry'][($result[0]['Entry'] - 1)];
    } 
    echo "</td>\n";
    echo "  <td>" . ($result[0]['Deco'] == 'True' ? $_lang['yes'] : $_lang['no']) . "</td>\n";
    echo "  <td>" . ($result[0]['Rep'] == 'True' ? $_lang['yes'] : $_lang['no']) . "</td>\n";
    if ($result[0]['Surfint'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . $result[0]['Surfint'] . "</td>\n";
    } 
    echo " </tr>\n";

    if ($result[0]['Decostops']) {
        echo " <tr><td colspan=\"4\" class=\"spacing\">&nbsp;</td></tr>\n";

        echo " <tr class=\"divesection\">\n";
        echo "  <td colspan=\"4\">" . $_lang['logbook_decostops'] . "</td>\n";
        echo " </tr>";

        echo " <tr><td colspan=\"4\">\n";
        $r = $result[0]['Decostops'];
        $r = str_replace("\r\n", "<br>\n", $r);
        echo $r;
        echo "  </td></tr>\n";
    } 

    // Equipment
    echo " <tr><td colspan=\"4\" class=\"spacing\">&nbsp;</td></tr>\n";

    echo " <tr class=\"divesection\">\n";
    echo "  <td colspan=\"4\">" . $_lang['dive_sect_equipment'] . "</td>\n";
    echo " </tr>\n";

    echo " <tr class=\"divetitle\">\n";
    echo "  <td>" . $_lang['logbook_weight'] . "</td>\n";
    echo "  <td>" . $_lang['logbook_divesuit'] . "</td>\n";
    echo "  <td colspan=\"2\" >" . $_lang['logbook_computer'] . "</td>\n";
    echo " </tr>\n";

    echo " <tr class=\"divecontent\">\n";
    if ($result[0]['Weight'] == "") {
        echo "  <td>-</td>\n";
    } else {
        if ($_config['weight']) {
            echo "  <td>" . KgToLbs($result[0]['Weight'], 0) . "&nbsp;" . $_lang['unit_weight_imp'] . "</td>\n";
        } else {
            echo "  <td>" . $result[0]['Weight'] . "&nbsp;" . $_lang['unit_weight'] . "</td>\n";
        } 
    } 
    if ($result[0]['Divesuit'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . $result[0]['Divesuit'] . "</td>\n";
    } 
    if ($result[0]['Computer'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td colspan=\"2\" >" . $result[0]['Computer'] . "</td>\n";
    } 
    echo " </tr>\n";

    if ($result[0]['UsedEquip'] != "") {
        echo " <tr class=\"divetitle\">\n";
        echo "  <td colspan=\"4\">" . $_lang['logbook_usedequip'] . "</td>\n";
        echo " </tr>\n";

        echo " <tr class=\"divecontent\">\n";
        echo "  <td colspan=\"4\">";

        $globals['gearlist'] = $result[0]['UsedEquip'];
        $divegear = parse_mysql_query('divegearlist.sql');
        $num_equip = count($divegear);

        for($i = 0; $i < $num_equip; $i++) {
            if ($i != 0) {
                echo ",\n   ";
            } 
            echo "<a href=\"equipment.php?gear=" . $divegear[$i]['ID'] . "\"\n";
            echo "    title=\"" . $divegear[$i]['Object'];
            echo " " . $_lang['logbook_place_linktitle'] . "\">";
            echo $divegear[$i]['Object'] . "</a>";
        } 
        echo "</td>\n";
        echo " </tr>\n";
    } 

    echo " <tr><td colspan=\"4\" class=\"spacing\">&nbsp;</td></tr>\n";

    // Comments
    // Show them if we have them
    if ($result[0]['Comments'] != "") {

        echo " <tr class=\"divesection\">\n";
        echo "  <td colspan=\"4\">" . $_lang['dive_sect_comments'] . "</td>\n";
        echo " </tr>\n";

        echo " <tr><td colspan=\"4\">\n";

        $r = $result[0]['Comments'];
        $r = str_replace("\r\n", "<br>\n", $r);
        echo $r;
        echo "  </td></tr>\n";
    } 

    // Show the links again
    echo " <tr>\n  <td colspan=\"4\" class=\"spacing\">&nbsp;</td>\n </tr>\n";
    echo $links;

    echo "</table>\n";

} else {

    // We don't have a dive number, so display the Dive Log

    // Get start of the list
    $start = check_number($_GET['start']);
    if (!$start) {
        $start = 1;
    } 
    $globals['start'] = $start - 1;

    // Get number of dives to list
    $max = check_number($_GET['max']);
    if (!$max) {
        $max = $_config['max_list'];
    } 
    $globals['max'] = $max;

    // Get number of dives
    $count = parse_mysql_query('divecount.sql');
    $end = $count[0]['COUNT(*)'];

    // First, Previous, Next, Last links
    $links = " <tr>\n";
    $links .= "  <td colspan=\"5\">\n";
    $links .= "   <table cellspacing=\"0\" cellpadding=\"0\" width=\"100%\"><tr>\n";

    // First, Previous
    $links .= "    <td width=\"25%\" align=\"left\">";
    if ($start == 1) {
        $links .= "&nbsp;";
    } else {
        $links .= "\n     <div class=\"divelogcrumbs\">\n";
        $links .= "      <a href=\"index.php?start=1&amp;max=" . $max . "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang["first_group_linktitle"] . "\">" . $_lang['first'] . "</a>";
        $previous = $start - $max;
        if ($previous < 1) {
            $previous = 1;
        } 
        $links .= "&nbsp;|&nbsp;";
        $links .= "<a\n";
        $links .= "       href=\"index.php?start=" . $previous . "&amp;max=" . $max . "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang["previous_group_linktitle"] . "\">" . $_lang['previous'] . "</a>\n";
        $links .= "     </div>\n    ";
    } 
    $links .= "</td>\n";

    // Dive Sites, Dive Statistics
    $links .= "    <td width=\"50%\" align=\"center\">\n";
    $links .= "     <div class=\"divelogcrumbs\">\n";
    $links .= "      <a href=\"divesite.php\" class=\"divelogcrumbs\"\n";
    $links .= "       title=\"" . $_lang['dive_sites_linktitle'] . "\">" . $_lang['dive_sites'] . "</a> | \n";
    $links .= "      <a href=\"equipment.php\" class=\"divelogcrumbs\"\n";
    $links .= "       title=\"" . $_lang['dive_equip_linktitle'] . "\">" . $_lang['dive_equip'] . "</a> | \n";
    $links .= "      <a href=\"divestats.php\" class=\"divelogcrumbs\"\n";
    $links .= "       title=\"" . $_lang['dive_stats_linktitle'] . "\">" . $_lang['dive_stats'] . "</a>\n";
    $links .= "     </div>\n";
    $links .= "    </td>\n";

    // Next, Last
    $links .= "    <td width=\"25%\" align=\"right\">";
    if (($start + $max) >= $end) {
        $links .= "&nbsp;";
    } else {
        $next = $start + $max;
        $startlast = ((floor($end / $max) * $max) + 1);
        $links .= "\n     <div class=\"divelogcrumbs\">\n";
        $links .= "      <a href=\"index.php?start=" . $next . "&amp;max=" . $max . "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang["next_group_linktitle"] . "\">" . $_lang["next"] . "</a>";
        $links .= "&nbsp;|&nbsp;";
        $links .= "<a\n";
        $links .= "       href=\"index.php?start=" . $startlast . "&amp;max=" . $max . "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang["last_group_linktitle"] . "\">" . $_lang['last'] . "</a>\n";
        $links .= "     </div>\n    ";
    } 
    $links .= "</td>\n";

    $links .= "   </tr></table>\n";
    $links .= "  </td>\n";
    $links .= " </tr>\n";

    // Get the page header
    $pagetitle = $_lang['dive_log'];
    include($_config['header']);

    echo "<table class=\"divetable\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">\n";

    // Show the links
    echo $links;

    // Get the details of the dives to be listed
    $recentdivelist = parse_mysql_query('recentdivelist.sql');
    $count = count($recentdivelist);

    echo " <tr>\n  <td colspan=\"5\" class=\"spacing\">&nbsp;</td>\n </tr>\n";

    if ($count == 0) {
        echo " <tr class=\"divecontent\">\n";
        echo "  <td colspan=\"5\">" . $_lang['dlog_none'] . "</td>\n";
        echo " </tr>\n";
    } else {
        echo " <tr class=\"divelogtitle\">\n";
        echo "  <td width=\"8%\" valign=\"bottom\">" . $_lang['dlog_title_number'] . "</td>\n";
        echo "  <td width=\"15%\" valign=\"bottom\">" . $_lang['dlog_title_divedate'] . "</td>\n";
        echo "  <td width=\"10%\" valign=\"bottom\">" . $_lang['dlog_title_depth'] . "</td>\n";
        echo "  <td width=\"10%\" valign=\"bottom\">" . $_lang['dlog_title_divetime'] . "</td>\n";
        echo "  <td valign=\"bottom\">" . $_lang['dlog_title_location'] . "</td>\n";
        echo " </tr>\n";
        /*
	echo "<hr>\n<b>Recentdivelist:</b><br>\n<code>\n";
	print_r($recentdivelist);
	echo "</code>\n<br>\n<hr>\n";
*/
        for($i = 0; $i < $count; $i++) {
            echo " <tr class=\"divecontent\">\n";
            echo "  <td><a href=\"index.php?nr=" . $recentdivelist[$i]['Number'];
            echo "\" title=\"" . $_lang['dlog_number_title'] . $recentdivelist[$i]['Number'];
            echo "\">" . $recentdivelist[$i]['Number'] . "</a></td>\n";
            if ($recentdivelist[0]['Divedate'] == "") {
                echo "  <td>-</td>\n";
            } else {
                echo "  <td>" . date($_lang['dlog_divedate_format'], strtotime($recentdivelist[$i]['Divedate'])) . "</td>\n";
            } 
            if ($recentdivelist[$i]['Depth'] == "") {
                echo "  <td>-</td>\n";
            } else {
                echo "  <td>" . $recentdivelist[$i]['Depth'] . " " . $_lang['unit_length_short'] . "</td>\n";
            } 
            if ($recentdivelist[$i]['Divetime'] == "") {
                echo "  <td>-</td>\n";
            } else {
                echo "  <td>" . $recentdivelist[$i]['Divetime'] . " " . $_lang['unit_time_short'] . "</td>\n";
            } 
            $location = "";
            if ($recentdivelist[$i]['Place'] != "") {
                $location .= $recentdivelist[$i]['Place'];
            } 
            if ($recentdivelist[$i]['City'] != "") {
                if ($location != "") {
                    $location .= ", ";
                } 
                $location .= $recentdivelist[$i]['City'];
            } 
            if ($location == "") {
                $location .= "-";
            } 
            echo "  <td>" . $location . "</td>\n";
            echo " </tr>\n";
        } 
    } 
    // Show the links
    echo " <tr>\n  <td colspan=\"5\" class=\"spacing\">&nbsp;</td>\n </tr>\n";
    echo $links;

    echo "</table>\n";
} 

// Get the page footer
include ($_config['footer_index']);

?>
