sub PreCreateForm ()
{
   my $cTable;

   &SetField("lang", &GetAttr("Lang"));

   $cLookupInfo = $oSessionGL->getState("wwwdb_lookup,prefs");

   if($cLookupInfo)
   {
      my $cType;
      my $cHeader;

      ($cType, $cHeader) = split ";", $cLookupInfo;

      &SetField("key_name", "$cType");
      &MySetVal("Layout Field key_name", "Type", "Label")
          if $cType;
      &MySetVal("Header", "Title", "$cHeader")
          if $cHeader;

      my $cResult = &GetSortNrList();

      &MySetVal("Layout Field sort_nr", "Type", "Select");
      &MySetVal("Layout Field sort_nr", "Values", $cResult);
   }

}

sub PreBtnNew()
{

   &SetField("id_lookup",
             $oDbSessionGL->getMaxId("id_lookup", "wwwdb_lookup"));

}


sub Check_wwwdb_lookup ()
{
   # Set time-stamp
   &SetField("last_change",
             $oDbSessionGL->DateToDb(scalar(localtime())));

}

sub GetSortNrList($)
{
   my %hSortNr  = ();
   my $cResult = "";
   my $plSelect;

   $oDbSessionGL->setDebugLvl(10);
     $plSelect = $oDbSessionGL->SqlSelect
         ("SELECT
               t0.id_lookup,
               t0.label,
               t0.lang,
               t0.sort_nr
           FROM
               wwwdb_lookup t0
           WHERE
               t0.key_name     = ? AND
               t0.lang         = ?",
          &GetField("key_name"),
          &GetAttr("Lang"));

   {
      my $iLastSortNr = 0;

      foreach (@$plSelect)
      {
         if($_->[3] eq "" || $_->[3] == 0 || defined $hSortNr{$_->[3]})
         {
            # currently do nothing
         }
         else
         {
            $hSortNr{$_->[3]} = (sprintf("%05d: ", $_->[3]) .
                                 ($_->[1]));
         }
      }

      foreach (sort {$a <=> $b}
               keys %hSortNr)
      {
         my $iNewKey = int(($iLastSortNr + $_) / 2);

         $iLastSortNr = $_;

         $cResult .= sprintf ("%s\\,%s\n" x 2,
                              $iNewKey,
                              sprintf("%05d: ...", $iNewKey),
                              $_,
                              $hSortNr{$_});
      }

      $cResult .= sprintf ("%s\\,%s\n",
                              ($iLastSortNr + 10),
                              sprintf("%05d: ...", ($iLastSortNr + 10)));

   }

   print STDERR "YYYYYYYYYYYYY: $cResult\n";

   return $cResult;
}


1;
