function getEventSpecsEventIds($eventSpecs)
{
    return array_keys(readEventSpecs($eventSpecs));
}
function showPreregForm()
{
    #----------------------------------------------------------------------
    global $chosenCompetitionId, $competition, $persons;
    $chosenCountry = $competition['countryId'];
    if (getBooleanParam('search')) {
        $chosenPattern = getMysqlParam('name');
        $chosenName = getHtmlParam('name');
        $nameCondition = '';
        foreach (explode(' ', $chosenPattern) as $namePart) {
            $nameCondition .= " AND name like '%{$namePart}%'";
        }
        $persons = dbQuery("SELECT name, id FROM Persons WHERE 1 {$nameCondition} AND subId='1' ORDER BY name");
        $matchingNumber = count($persons);
    } else {
        if (getBooleanParam('confirm')) {
            $chosenPersonId = getNormalParam('namelist');
            $chosenPerson = dbQuery("SELECT * FROM Persons WHERE id='{$chosenPersonId}' AND subId='1'");
            $chosenPerson = $chosenPerson[0];
            $chosenName = htmlEscape($chosenPerson['name']);
            $chosenCountry = $chosenPerson['countryId'];
            $chosenGender = $chosenPerson['gender'];
            $chosenYear = $chosenPerson['year'];
            $chosenMonth = $chosenPerson['month'];
            $chosenDay = $chosenPerson['day'];
            $chosenEmail = '';
            $chosenGuests = '';
            $chosenComments = '';
            $dontPrintDoB = $chosenYear != 0;
        } else {
            if (getBooleanParam('submit')) {
                $saveSucceeded = savePreregForm();
                $chosenPersonId = getHtmlParam('personId');
                $chosenName = getHtmlParam('name');
                $chosenCountry = getHtmlParam('countryId');
                $chosenGender = getHtmlParam('gender');
                $chosenYear = getHtmlParam('birthYear');
                $chosenMonth = getHtmlParam('birthMonth');
                $chosenDay = getHtmlParam('birthDay');
                $chosenEmail = getHtmlParam('email');
                $chosenGuests = getHtmlParam('guests');
                $chosenComments = getHtmlParam('comments');
                $dontPrintDoB = $chosenYear == '';
            }
        }
    }
    echo "<h1>Registration form</h1>";
    echo "<p style='width:90%;margin:1em auto 1em auto;'>Please note that the purpose of the preregistration is not only to reserve you a spot in the competition, but also very importantly to give the organizers a good estimation of the number of people they have to expect. Please don't wait until the very last minute to preregister, otherwise the organizers might not be able to offer enough room, food, etc.</p>";
    echo "<p style='width:90%;margin:1em auto 1em auto;'>If you already have participated in an official competition, you can use the search function which will fill the information stored in the database. You can then fill the rest.</p>";
    echo "<form method='POST'>";
    showField("competitionId hidden {$chosenCompetitionId}");
    showField("form hidden 1");
    echo "<table class='prereg'>";
    if (isset($chosenPersonId)) {
        showField("personId readonly {$chosenPersonId} 11 <b>WCA Id</b>");
    }
    if (!isset($chosenName)) {
        $chosenName = "";
    }
    if (getBooleanParam('new')) {
        showField("name text {$chosenName} 50 <b>Name</b>");
        echo "<tr><td>&nbsp;</td><td>Enter your name <b>correctly</b>, for example \"<span style='color:#393;font-weight:bold'>Stefan Pochmann</span>\". Not sloppily like \"<span style='color:#c00;font-weight:bold'>s pochman</span>\".</td></tr>";
    } else {
        showField("name name 50 <b>Name</b> {$chosenName}");
    }
    if (getBooleanParam('search')) {
        showField("namelist namelist <b>{$matchingNumber} names matching</b>");
        echo "<tr><td></td><td><input type='submit' id='new' name='new' value='I am new !' /></td></tr> ";
    } else {
        if (getBooleanParam('submit') && !$saveSucceeded || getBooleanParam('confirm') || getBooleanParam('new')) {
            showField("countryId country <b>Citizen&nbsp;of</b> {$chosenCountry}");
            showField("gender gender {$chosenGender} <b>Gender</b>");
            if (!$dontPrintDoB) {
                showField("birth date {$chosenDay} {$chosenMonth} {$chosenYear} <b>Date of birth</b>");
            }
            showField("email text {$chosenEmail} 50 <b>E-mail</b> address");
            showField("guests area 50 3 Names&nbsp;of&nbsp;the&nbsp;<b>guests</b>&nbsp;accompanying&nbsp;you {$chosenGuests}");
            ?>
<tr><td><b>Events</b><br /><br />Check the events you want to participate in.</td>
<td>
<?php 
            $eventSpecs = readEventSpecs($competition['eventSpecs']);
            foreach ($eventSpecs as $eventId => $eventSpec) {
                extract($eventSpec);
                if (!$personLimit) {
                    $personLimit = "0";
                }
                $chosenE = getBooleanParam("E{$eventId}");
                showField("E{$eventId} event {$personLimit} {$timeLimit} {$timeFormat} {$chosenE}");
            }
            echo "</td></tr>";
            showField("comments area 50 5 Room&nbsp;for&nbsp;<b>extra&nbsp;information</b> {$chosenComments}");
            showField("ip hidden " . $_SERVER["REMOTE_ADDR"]);
            echo "<tr><td>&nbsp;</td><td style='text-align:center'>";
            echo "<input type='submit' id='submit' name='submit' value='Preregister me!' style='background-color:#9F3;font-weight:bold' /> ";
            echo "<input type='reset' value='Empty form' style='background-color:#F63;font-weight:bold' />";
            echo "</td></tr>";
        } else {
            echo "<tr><td></td><td><input type='submit' id='new' name='new' value='I am new !' /></td></tr> ";
        }
    }
    echo "</table>";
    echo "</form>";
}