function analyzeChoices()
{
    #----------------------------------------------------------------------
    global $chosenCompetitionId, $chosenShow, $competitionCondition, $competitionDescription;
    $chosenCompetitionId = getNormalParam('competitionId');
    $chosenShow = getBooleanParam('show');
    $competitionCondition = competitionCondition();
    $competitionDescription = $chosenCompetitionId ? $chosenCompetitionId : "all competitions";
}
function analyzeChoices()
{
    #----------------------------------------------------------------------
    global $chosenEventId, $chosenCompetitionId, $chosenWhat, $chosenShow, $chosenWhich, $competitionCondition;
    $chosenEventId = getNormalParam('eventId');
    $chosenCompetitionId = getNormalParam('competitionId');
    $chosenWhat = getNormalParam('what');
    $chosenShow = getBooleanParam('show');
    $chosenWhich = "";
    $chosenWhich .= $chosenEventId ? $chosenEventId : "all events";
    $chosenWhich .= " in " . ($chosenCompetitionId ? $chosenCompetitionId : "all competitions");
    $competitionCondition = eventCondition() . competitionCondition();
}
function computeRegionalRecordMarkers($valueId, $valueName)
{
    #----------------------------------------------------------------------
    global $chosenAnything, $chosenCompetitionId, $differencesWereFound, $previousRecord, $pendingCompetitions, $startDate;
    # -----------------------------
    # Description of the main idea:
    # -----------------------------
    # Get all results that are potential regional records. Process them one
    # event at a time. Inside, process them one competition at a time, in
    # chronological order of start date. Each competition's results are only
    # compared against records of strictly previous competitions, not against
    # parallel competitions. For this, there are these main arrays:
    #
    # - $previousRecord[regionId] is a running collection of each region's record,
    #   covering all competitions *before* the current one.
    #
    # - $record[regionId] is based on $previousRecord and is used and updated
    #   inside the current competition.
    #
    # - $pendingCompetitions[regionId] holds $record of competitions already
    #   processed but not merged into $previousRecord. When a new competition is
    #   encountered, we merge those that ended before the new one into $previousRecord.
    #
    # - $baseRecord[eventId][regionId] is for when a user chose a specific
    #   competition to check. Then we quickly ask the database for the current
    #   region records from before that competition. This could be used for
    #   giving the user a year-option as well, but we don't have that (yet?).
    # -----------------------------
    #--- If a competition was chosen, we need all records from before it
    if ($chosenCompetitionId) {
        $startDate = getCompetitionValue($chosenCompetitionId, "year*10000 + month*100 + day");
        $results = dbQueryHandle("\n      SELECT eventId, result.countryId, continentId, min({$valueId}) value, event.format valueFormat\n      FROM Results result, Competitions competition, Countries country, Events event\n      WHERE {$valueId} > 0\n        AND competition.id = result.competitionId\n        AND country.id     = result.countryId\n        AND event.id       = result.eventId\n        AND year*10000 + if(endMonth,endMonth,month)*100 + if(endDay,endDay,day) < {$startDate}\n      GROUP BY eventId, countryId");
        while ($row = mysql_fetch_row($results)) {
            list($eventId, $countryId, $continentId, $value, $valueFormat) = $row;
            if (isSuccessValue($value, $valueFormat)) {
                foreach (array($countryId, $continentId, 'World') as $regionId) {
                    if (!isset($baseRecord[$eventId][$regionId]) || $value < $baseRecord[$eventId][$regionId]) {
                        $baseRecord[$eventId][$regionId] = $value;
                    }
                }
            }
        }
        mysql_free_result($results);
    } else {
        $competitions = dbQuery("\n      SELECT id, year*10000 + if(endMonth,endMonth,month)*100 + if(endDay,endDay,day) endDate\n      FROM   Competitions competition");
        foreach ($competitions as $competition) {
            $endDate[$competition['id']] = $competition['endDate'];
        }
    }
    #--- The IDs of relevant results (those already marked as region record and those that could be)
    $queryRelevantIds = "\n   (SELECT id FROM Results WHERE regional{$valueName}Record<>'' " . eventCondition() . competitionCondition() . ")\n   UNION\n   (SELECT id\n    FROM\n      Results result,\n      (SELECT eventId, competitionId, roundId, countryId, min({$valueId}) value\n       FROM Results\n       WHERE {$valueId} > 0\n       " . eventCondition() . competitionCondition() . "\n       GROUP BY eventId, competitionId, roundId, countryId) helper\n    WHERE result.eventId       = helper.eventId\n      AND result.competitionId = helper.competitionId\n      AND result.roundId       = helper.roundId\n      AND result.countryId     = helper.countryId\n      AND result.{$valueId}      = helper.value)";
    #--- Get the results, ordered appropriately
    $results = dbQueryHandle("\n    SELECT\n      year*10000 + month*100 + day startDate,\n      result.id resultId,\n      result.eventId,\n      result.competitionId,\n      result.roundId,\n      result.personId,\n      result.personName,\n      result.countryId,\n      result.regional{$valueName}Record storedMarker,\n      {$valueId} value,\n      continentId,\n      continent.recordName continentalRecordName,\n      event.format valueFormat\n    FROM\n      ({$queryRelevantIds}) relevantIds,\n      Results      result,\n      Competitions competition,\n      Countries    country,\n      Continents   continent,\n      Events       event,\n      Rounds       round\n    WHERE 1\n      AND result.id      = relevantIds.id\n      AND competition.id = result.competitionId\n      AND round.id       = result.roundId\n      AND country.id     = result.countryId\n      AND continent.id   = country.continentId\n      AND event.id       = result.eventId\n    ORDER BY event.rank, startDate, competitionId, round.rank, {$valueId}\n  ");
    #--- For displaying the dates, fetch all competitions
    $allCompetitions = array();
    foreach (dbQuery("SELECT * FROM Competitions") as $row) {
        $allCompetitions[$row['id']] = $row;
    }
    #--- Process each result.
    $currentEventId = $announcedEventId = $announcedRoundId = $announcedCompoId = NULL;
    while ($row = mysql_fetch_row($results)) {
        list($startDate, $resultId, $eventId, $competitionId, $roundId, $personId, $personName, $countryId, $storedMarker, $value, $continentId, $continentalRecordName, $valueFormat) = $row;
        #--- Handle failures of multi-attempts.
        if (!isSuccessValue($value, $valueFormat)) {
            continue;
        }
        #--- At new events, reset everything
        if ($eventId != $currentEventId) {
            $currentEventId = $eventId;
            $currentCompetitionId = false;
            $record = $previousRecord = isset($baseRecord[$eventId]) ? $baseRecord[$eventId] : array();
            $pendingCompetitions = array();
        }
        #--- Handle new competitions.
        if ($competitionId != $currentCompetitionId) {
            #--- Add the records of the previously current competition to the set of pending competition records
            if ($currentCompetitionId) {
                $pendingCompetitions[] = array($endDate[$currentCompetitionId], $record);
            }
            #--- Note the current competition
            $currentCompetitionId = $competitionId;
            #--- Prepare the records this competition will be based on
            $pendingCompetitions = array_filter($pendingCompetitions, "handlePendingCompetition");
            $record = $previousRecord;
        }
        #--- Calculate whether it's a new region record and update the records.
        $calcedMarker = '';
        if (!isset($record[$countryId]) || $value <= $record[$countryId]) {
            $calcedMarker = 'NR';
            $record[$countryId] = $value;
            if (!isset($record[$continentId]) || $value <= $record[$continentId]) {
                $calcedMarker = $continentalRecordName;
                $record[$continentId] = $value;
                if (!isset($record['World']) || $value <= $record['World']) {
                    $calcedMarker = 'WR';
                    $record['World'] = $value;
                }
            }
        }
        #--- If stored or calculated marker say it's some regional record at all...
        if ($storedMarker || $calcedMarker) {
            #--- Do stored and calculated agree? Choose colors and update list of differences.
            $same = $storedMarker == $calcedMarker;
            $storedColor = $same ? '999' : 'F00';
            $calcedColor = $same ? '999' : '0E0';
            if (!$same) {
                $selectedIds[] = $resultId;
                $differencesWereFound = true;
            }
            #--- If no filter was chosen, only show differences.
            if (!$chosenAnything && $same) {
                continue;
            }
            #--- Highlight regions if the calculated marker thinks it's a record for them.
            $countryName = $countryId;
            $continentName = substr($continentId, 1);
            $worldName = 'World';
            if ($calcedMarker) {
                $countryName = "<b>{$countryName}</b>";
            }
            if ($calcedMarker && $calcedMarker != 'NR') {
                $continentName = "<b>{$continentName}</b>";
            }
            if ($calcedMarker == 'WR') {
                $worldName = "<b>{$worldName}</b>";
            }
            #--- Recognize new events/rounds/competitions.
            $announceEvent = $eventId != $announcedEventId;
            $announcedEventId = $eventId;
            $announceRound = $roundId != $announcedRoundId;
            $announcedRoundId = $roundId;
            $announceCompo = $competitionId != $announcedCompoId;
            $announcedCompoId = $competitionId;
            #--- If new event, announce it.
            if ($announceEvent) {
                tableCaption(false, "{$eventId} {$valueName}");
                tableHeader(explode('|', 'Date|Competition|Round|Person|Event|Country|Continent|World|Value|Stored|Computed|Agree'), array(7 => "class='R2'"));
            }
            #--- If new round/competition inside an event, add a separator row.
            if (($announceRound || $announceCompo) && !$announceEvent) {
                tableRowEmpty();
            }
            #--- Prepare the checkbox.
            $checkbox = "<input type='checkbox' name='update{$valueName}{$resultId}' value='{$calcedMarker}' />";
            #--- Show the result.
            tableRow(array(competitionDate($allCompetitions[$competitionId]), competitionLink($competitionId, $competitionId), $roundId, personLink($personId, $personName), $eventId, $countryName, $continentName, $worldName, formatValue($value, $valueFormat), "<span style='font-weight:bold;color:#{$storedColor}'>{$storedMarker}</span>", "<span style='font-weight:bold;color:#{$calcedColor}'>{$calcedMarker}</span>", $same ? '' : $checkbox));
        }
    }
    mysql_free_result($results);
}