function showUnfinishedPersons () {
#----------------------------------------------------------------------
  global $personsFromPersons, $personsFromResultsWithoutId, $birthdates;

  #--- Pre-compute the candidate tuples: (id, name, countryId, romanName, romanNameSimilarityPlaceHolder, countryIdSimilarityPlaceHolder)
  $candidates = array();
  foreach( $personsFromPersons as $person ){
    list( $id, $name, $countryId ) = $person;
    $candidates[] = array( $id, $name, $countryId, extractRomanName($name), 0, 0 );
  }

  #--- Begin the form and table.
  echo "<form action='persons_finish_unfinished_ACTION.php' method='post'>";
  tableBegin( 'results', 8 );
  tableHeader( explode( '|', '|personName|countryId|personId|birthdate|personName|countryId|personSemiId' ),
               array( 6=>'class="6"' ) );

  #--- Walk over all persons from the Results table.
  $caseNr = 0;
  foreach( $personsFromResultsWithoutId as $person ){
    list( $name, $countryId, $firstYear ) = $person;
    
    #--- Try to compute the semi-id.
    $quarterId = removeUglyAccentsAndStuff( extractRomanName( $name ));
    $quarterId = preg_replace( '/[^a-zA-Z ]/', '', $quarterId );
    $quarterId = strtoupper( substr( preg_replace( '/(.*)\s(.*)/', '$2$1', $quarterId ), 0, 4 ));
    if ( strlen ( $quarterId ) == 0 ) {
      // if the name comes empty, invent a quarterId
      $quarterId = 'XXXX';
    } else if ( strlen( $quarterId ) < 4 ) {
      // make sure the quarterId is 4-letter long
      while ( strlen( $quarterId ) < 4 ) {
        $quarterId .= $quarterId;
      }
      $quarterId = substr( $quarterId, 0, 4 );
    }
    $semiId = $firstYear . $quarterId;

    #--- Html-ify name and country.
    $nameHtml = htmlEscape( $name );
    $countryIdHtml = htmlEscape( $countryId );

    #--- Hidden field describing the case.
    $caseNr++;
    tableRowFull( "&nbsp;<input type='hidden' name='oldNameAndCountry$caseNr' value='$nameHtml|$countryIdHtml' />" );
    
    #--- Show the person.
    # Note that we set this input to checked, but if there's a better match
    # lower on, then it will take precendence.
    tableRowStyled( 'font-weight:bold', array(
      "<input type='radio' name='action$caseNr' value='new' checked='checked' />",
      visualize( $name ),
      visualize( $countryId ),
      peekLink( $name, $countryId ),
      'mm/dd/yyyy',
      "<input type='text' name='name$caseNr' value='$nameHtml' size='20' />",
      "<input type='text' name='country$caseNr' value='$countryIdHtml' size='20' />",
      "<input type='text' name='semiId$caseNr' value='$semiId' size='10' maxlength='8' />",
    ));

    #--- Show most similar persons.
    $similarsCtr = 0;
    foreach( getMostSimilarPersonsMax( extractRomanName($name), $countryId, $candidates, 10 ) as $similarPerson ){
      list( $other_id, $other_name, $other_countryId ) = $similarPerson;
      
      #--- If name and country match the unfinished persons, pre-select it.
      $checked = ($other_name==$name && $other_countryId==$countryId)
        ? "checked='checked'" : '';
        
      #--- Skip the unfinished person itself. 
      if( $checked && !$other_id )
        continue;

      #--- Html-ify.
      $nameHtml = htmlEscape( $other_name );
      $countryHtml = htmlEscape( $other_countryId );
      $idHtml = htmlEscape( $other_id );
      
      #--- Use "name|country|id" as action.
      $action = "$nameHtml|$countryHtml|$idHtml";
      
      #--- Show the other person.
      tableRow( array(
        "<input type='radio' name='action$caseNr' value='$action' $checked />",
#        ($other_id ? personLink( $other_id, $other_name ) : $other_name),
        visualize( $other_name ),
        visualize( $other_countryId ),
        ($other_id ? "<a class='p' href='../p.php?i=$other_id' target='_blank'>$other_id</a>" : peekLink( $other_name, $other_countryId )),
        $birthdates[ $other_id ],
        '', #sprintf( "%.2f", $similarity ),
        '',
        '',
      ));
      
      #--- Stop after five similar persons.
      if( ++$similarsCtr == 5 )
        break;
    }

    #--- Offer an explicit skip.
    tableRow( array(
      "<input type='radio' name='action$caseNr' value='skip' />",
      'I\'m not sure yet', '', '', '', '', '', ''
    ));
    
    #--- Don't show more than 20 unfinished persons.
    if( $caseNr == 20 )
      break;
  }

  #--- Show 'Update' button, finish table and form.
  tableRowEmpty();
  tableRowFull( "<input type='submit' value='Update' />" );
  tableEnd();
  echo "</form>";
}
function showUnfinishedPersons()
{
    #----------------------------------------------------------------------
    global $personsFromPersons, $personsFromResultsWithoutId, $birthdates;
    #--- Pre-compute the candidate tuples: (id, name, countryId, romanName, romanNameSimilarityPlaceHolder, countryIdSimilarityPlaceHolder)
    $candidates = array();
    foreach ($personsFromPersons as $person) {
        list($id, $name, $countryId) = $person;
        $candidates[] = array($id, $name, $countryId, extractRomanName($name), 0, 0);
    }
    #--- Begin the form and table.
    echo "<form action='persons_finish_unfinished_ACTION.php' method='post'>";
    tableBegin('results', 8);
    tableHeader(explode('|', '|personName|countryId|personId|birthdate|personName|countryId|personSemiId'), array(6 => 'class="6"'));
    #--- Walk over all persons from the Results table.
    $caseNr = 0;
    $availableSpots = array();
    // array of semiIds in progress
    foreach ($personsFromResultsWithoutId as $person) {
        list($name, $countryId, $firstYear) = $person;
        #--- Try to compute the semi-id.
        $paddingLetter = 'U';
        $neatName = strtoupper(preg_replace('/[^a-zA-Z ]/', '', removeUglyAccentsAndStuff(extractRomanName($name))));
        $nameParts = explode(' ', $neatName);
        $lastName = $nameParts[count($nameParts) - 1];
        $restOfName = implode(array_slice($nameParts, 0, count($nameParts) - 1));
        // follows a simple trick that prevents us from empty or too short restOfNames and provides the appropriate padding
        $restOfName = str_pad($restOfName, 4, $paddingLetter);
        $lettersToShift = max(0, 4 - strlen($lastName));
        $cleared = false;
        while (!$cleared && $lettersToShift <= 4) {
            $quarterId = substr($lastName, 0, 4 - $lettersToShift) . substr($restOfName, 0, $lettersToShift);
            $semiId = $firstYear . $quarterId;
            // update array of persons in progress
            if (!array_key_exists($semiId, $availableSpots)) {
                $lastIdTaken = dbQuery("SELECT id FROM Persons WHERE id LIKE '{$semiId}__' ORDER BY id DESC LIMIT 1");
                if (!count($lastIdTaken)) {
                    $counter = 0;
                } else {
                    $counter = intval(substr($lastIdTaken[0]['id'], 8, 2), 10);
                }
                $availableSpots[$semiId] = 99 - $counter;
            }
            // is there a spot available?
            if ($availableSpots[$semiId]) {
                $availableSpots[$semiId]--;
                $cleared = true;
            } else {
                $lettersToShift++;
            }
        }
        /* The script has tried all the possibilities and none of them was valid.
         * If we reach here with $cleared set to false (something that is not going to happen in centuries) then
         * the person posting will receive an error in persons_finish_unfinished_ACTION.php and the software team
         * of the future will have work to do.
         */
        if (!$cleared) {
            // if we didn't clear a spot we stick with the first combination
            $lettersToShift = max(0, 4 - strlen($lastName));
            $semiId = $firstYear . substr($lastName, 0, 4 - $lettersToShift) . substr($restOfName, 0, $lettersToShift);
            $availableSpots[$semiId] = 0;
        }
        #--- Html-ify name and country.
        $nameHtml = htmlEscape($name);
        $countryIdHtml = htmlEscape($countryId);
        #--- Hidden field describing the case.
        $caseNr++;
        tableRowFull("&nbsp;<input type='hidden' name='oldNameAndCountry{$caseNr}' value='{$nameHtml}|{$countryIdHtml}' />");
        #--- Show the person.
        # Note that we set this input to checked, but if there's a better match
        # lower on, then it will take precendence.
        tableRowStyled('font-weight:bold', array("<input type='radio' name='action{$caseNr}' value='new' checked='checked' />", visualize($name), visualize($countryId), peekLink($name, $countryId), 'mm/dd/yyyy', "<input type='text' name='name{$caseNr}' value='{$nameHtml}' size='20' />", "<input type='text' name='country{$caseNr}' value='{$countryIdHtml}' size='20' />", "<input type='text' name='semiId{$caseNr}' value='{$semiId}' size='10' maxlength='8' />"));
        #--- Show most similar persons.
        $similarsCtr = 0;
        foreach (getMostSimilarPersonsMax(extractRomanName($name), $countryId, $candidates, 10) as $similarPerson) {
            list($other_id, $other_name, $other_countryId) = $similarPerson;
            #--- If name and country match the unfinished persons, pre-select it.
            $checked = $other_name == $name && $other_countryId == $countryId ? "checked='checked'" : '';
            #--- Skip the unfinished person itself.
            if ($checked && !$other_id) {
                continue;
            }
            #--- Html-ify.
            $nameHtml = htmlEscape($other_name);
            $countryHtml = htmlEscape($other_countryId);
            $idHtml = htmlEscape($other_id);
            #--- Use "name|country|id" as action.
            $action = "{$nameHtml}|{$countryHtml}|{$idHtml}";
            #--- Show the other person.
            tableRow(array("<input type='radio' name='action{$caseNr}' value='{$action}' {$checked} />", visualize($other_name), visualize($other_countryId), $other_id ? "<a class='p' href='../p.php?i={$other_id}' target='_blank'>{$other_id}</a>" : peekLink($other_name, $other_countryId), $birthdates[$other_id], '', '', ''));
            #--- Stop after five similar persons.
            if (++$similarsCtr == 5) {
                break;
            }
        }
        #--- Offer an explicit skip.
        tableRow(array("<input type='radio' name='action{$caseNr}' value='skip' />", 'I\'m not sure yet', '', '', '', '', '', ''));
        #--- Don't show more than 20 unfinished persons.
        if ($caseNr == 20) {
            break;
        }
    }
    #--- Show 'Update' button, finish table and form.
    tableRowEmpty();
    tableRowFull("<input type='submit' value='Update' />");
    tableEnd();
    echo "</form>";
}