function analyzeChoices()
{
    #----------------------------------------------------------------------
    global $chosenNameHtml, $chosenNameMysql, $chosenCountryIdHtml, $chosenCountryIdMysql;
    $chosenNameHtml = getHtmlParam('name');
    $chosenNameMysql = getMysqlParam('name');
    $chosenCountryIdHtml = getHtmlParam('countryId');
    $chosenCountryIdMysql = getMysqlParam('countryId');
}
function analyzeChoices()
{
    #----------------------------------------------------------------------
    global $chosenEventId, $chosenRegionId, $chosenPatternHtml, $chosenPatternMysql;
    $chosenEventId = getNormalParam('eventId');
    $chosenRegionId = getNormalParam('regionId');
    $chosenPatternHtml = getHtmlParam('pattern');
    $chosenPatternMysql = getMysqlParam('pattern');
}
function analyzeChoices () {
#----------------------------------------------------------------------
  global $chosenId, $chosenConfirm, $chosenName, $chosenNameHtml, $chosenCountryId, $chosenGender, $chosenDay, $chosenMonth, $chosenYear, $chosenUpdate, $chosenFix;

  $chosenConfirm     = getBooleanParam( 'confirm'   );
  $chosenUpdate      = getBooleanParam( 'update'    );
  $chosenFix         = getBooleanParam( 'fix'       );
  $chosenId          = getNormalParam ( 'id'        );
  $chosenName        = getMysqlParam  ( 'name'      );
  $chosenNameHtml    = getHtmlParam   ( 'name'      );
  $chosenCountryId   = getMysqlParam  ( 'countryId' );
  $chosenGender      = getNormalParam ( 'gender'    );
  $chosenDay         = getNormalParam ( 'day'       );
  $chosenMonth       = getNormalParam ( 'month'     );
  $chosenYear        = getNormalParam ( 'year'      );

}
function analyzeChoices()
{
    #----------------------------------------------------------------------
    global $chosenEventId, $chosenRegionId, $chosenYears, $chosenPatternHtml, $chosenPatternMysql, $chosenList, $chosenMap;
    $chosenEventId = getNormalParam('eventId');
    $chosenRegionId = getNormalParam('regionId');
    $chosenPatternHtml = getHtmlParam('pattern');
    $chosenPatternMysql = getMysqlParam('pattern');
    $chosenYears = getNormalParam('years');
    $chosenList = getBooleanParam('list');
    $chosenMap = getBooleanParam('map');
    if (!$chosenList && !$chosenMap) {
        $chosenYears = "current";
    }
    if (!$chosenMap) {
        $chosenList = true;
    }
}
function analyzeChoices()
{
    #----------------------------------------------------------------------
    global $chosenId, $chosenConfirm, $chosenName, $chosenNameHtml, $chosenCountryId, $chosenGender, $chosenDay, $chosenMonth, $chosenYear, $chosenUpdate, $chosenFix;
    $chosenConfirm = getBooleanParam('confirm');
    $chosenUpdate = getBooleanParam('update');
    $chosenFix = getBooleanParam('fix');
    $chosenId = getNormalParam('id');
    // Gah, this is awful. We can't call getNormalParam because it refuses to
    // return anything if the parameter has a quote in it. We don't want to call
    // getMysqlParam, as we're using pdo_query throughout this file, and we don't
    // want double escaping to occur.
    $chosenName = getRawParamThisShouldBeAnException('name');
    $chosenNameHtml = getHtmlParam('name');
    $chosenCountryId = getNormalParam('countryId');
    $chosenGender = getNormalParam('gender');
    $chosenDay = getNormalParam('day');
    $chosenMonth = getNormalParam('month');
    $chosenYear = getNormalParam('year');
}
function analyzeChoices()
{
    #----------------------------------------------------------------------
    global $chosenCompetitionId, $chosenType, $chosenText, $chosenUri;
    global $chosenSubmitterName, $chosenSubmitterEmail, $chosenSubmitterComment;
    global $chosenTextHtml, $chosenUriHtml, $chosenSubmitterNameHtml, $chosenSubmitterEmailHtml, $chosenSubmitterCommentHtml;
    global $chosenRecaptchaChallenge, $chosenRecaptchaResponse;
    $chosenCompetitionId = getNormalParam('competitionId');
    $chosenType = getNormalParam('type');
    $chosenText = getMysqlParam('text');
    $chosenUri = getMysqlParam('uri');
    $chosenSubmitterName = getMysqlParam('submitterName');
    $chosenSubmitterEmail = getMysqlParam('submitterEmail');
    $chosenSubmitterComment = getMysqlParam('submitterComment');
    $chosenTextHtml = getHtmlParam('text');
    $chosenUriHtml = getHtmlParam('uri');
    $chosenSubmitterNameHtml = getHtmlParam('submitterName');
    $chosenSubmitterEmailHtml = getHtmlParam('submitterEmail');
    $chosenSubmitterCommentHtml = getHtmlParam('submitterComment');
    $chosenRecaptchaChallenge = getRawParamThisShouldBeAnException('recaptcha_challenge_field');
    $chosenRecaptchaResponse = getRawParamThisShouldBeAnException('recaptcha_response_field');
}
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>";
}