############################################################
#
# pooclub_html.pl
#
# Functions which generate HTML for parts of the poopages
#
############################################################

require "stuff_funcs.pl";

sub sortnumeric { $a <=> $b; }
sub sortbyscore { 0 - ($a->{g_score} <=> $b->{g_score}); }
sub sortbytotal { 0 - ($a->{g_total} <=> $b->{g_total}); }
sub sortbypoints { 0 - ($a->{g_points} <=> $b->{g_points}); }
sub sortbyspecial { $a->{g_special} cmp $b->{g_special}; }
sub sortbydaysToKday { $a->{daysToKday} <=> $b->{daysToKday}; }
sub sortbyBday { $a->{bday_yyyymmdd} <=> $b->{bday_yyyymmdd}; }


############################################################

sub pooclub_menus
{
    my $priv=$UBERACC{'PRIVILEGE'};
    if (is_logged_in())
    {
        my $whoami = username_and_priv();
        $whoami = "Login: " . $whoami if (! $whoami =~ /\[/);

        print qq(
        <div style="text-align: left; font-size: 10px;">$whoami
         | <a href="${THIS_SCRIPT}?page=account">edit</a>
        );

        print qq(
         | <a href="${THIS_SCRIPT}?page=admin">manage</a>
        ) if ($priv > 1);

        print qq(
         | <a href="${THIS_SCRIPT}?page=logout">logout</a>
        </div>
        );
    }
    else
    {
        print qq(
        <div style="text-align: left; font-size: 10px;">
         <a href="${THIS_SCRIPT}?page=login">login</a> |
         <a href="${THIS_SCRIPT}?page=signup">sign up</a>
        </div>
        );
    }

#    if ($priv > 1) # manager or above
#    {
#        print qq(<p />
#        <div style="text-align: left; font-size: 10px;">
#         <a href="${THIS_SCRIPT}?page=admin_logs&type=actions">Actions Log</a>
#        </div>
#        );
#    }

    my $menuFile = "$POO_MENUS_FILE";

    unless (open (MENUFILE, "$menuFile"))
    {
        pooclub_menu();  # No menu file so show hardcoded menu
        return;
    }

    print <<EOT;
    <table border="0" width="100" align="left" cellspacing="0" cellpadding="0">
     <tr>
      <td align="left" valign="top">
        <font face="verdana,arial,times" size="5" color="brown">
         poopages
        </font><br>
EOT
#    pooclub_small_slogan();
    my $menuCount = 0;
    my $membersOnly = 0;

    while (<MENUFILE>)
    {
        chop;
        next if (/^#/);
        next if (/^\s*$/);
        ($recType, $link, $title, $format) = split /;/, $_;

        if ($recType eq "T")  # Start of a menu
        {
            if ($menuCount > 0)  # Close previous menu before starting new one
            {
                print <<EOT;
          </table>
         </td></tr>
        </table>
EOT
            }

            print <<EOT;
        <br clear="all">
        <font face="verdana,arial,times" size="4" color="brown">
         <a href="${POO_CGI}?$link">$title</a>
        </font>
        <table class="poomenu" cellpadding="1" cellspacing="1" border="0" width="120" bgcolor="#cc6600" >
         <tr align="center"><td align="left">
          <table class="poomenu" cellpadding="1" cellspacing="0" border="0" width="118" bgcolor="#e8e8e8" >
EOT
            $menuCount++;
        }
        else  # It's a menuItem
        {
            $endlink = "";
            $asterisk = "";
            $indentation = "";

            if ($recType eq "A")   # Call pooclub server using args
            {
                $link = "$POO_CGI?" . $link;
            }
            elsif ($recType eq "S")   # Call StuffToday server using args
            {
                $link = "$STUFF_CGI?" . $link;
            }

            if ($link ne "")
            {
                $link = "<a class=poomenu href=\"" . $link . "\">";
                $endlink = "</a>";
            }

            if ($format =~ /m/)
            {
                $asterisk = "*";
                $membersOnly++;
            }

            $indentation = "&nbsp;&nbsp;" if ($format =~ /i/);

            print <<EOT;
           <tr>
            <td>&nbsp;</td> <td class="poomenu">
<!----
             <font face="arial,helvetica" size="2">
----->
              $indentation$link$title$endlink$asterisk
<!------
             </font>
------>
            </td>
            <td>&nbsp;</td>
           </tr>
EOT
        }
    }

    close (MENUFILE);


    print <<EOT;
          </table>
         </td></tr>
        </table>
        <br clear="all">
EOT
    if ($membersOnly > 0)
    {
        print <<EOT;
        <font face="times,arial,helvetica" size="1" color="gray">
         *members only
        </font>
        <p>
EOT
    }

    print qq(
        <div style="text-align: left; font-size: 13px;">
         <a href="${THIS_SCRIPT}?page=contactus">Contact Us</a>
         <p />
        </div>
    );

#    if ($priv > 1) # manager or above
#    {
#        my $logfile = $SCRIPT_ID . "_" . $YYYYMMDD . ".csv";
#        print qq(<p />
#        <div style="text-align: left; font-size: 10px;">
#         <a href="${THIS_SCRIPT}?page=admin_logfile&type=actions&f=$logfile">Today's Actions Log</a>
#         <a href="${THIS_SCRIPT}?p=postas">Post as...</a>
#         <p />
#        </div>
#        );
#    }

    print qq(
        <font face="verdana,arial,times" size="4" color="brown">
         <a href="http://groups.google.com/group/pooclub/subscribe">Join pooclub</a>
        </font>
    );
    pooclub_medium_slogan();
    print <<EOT;
        <div class="itemText">
         <a href="http://groups.google.com/group/pooclub/subscribe"><b>Subscribe to our mailing list</b></a>.
         <font face="arial,times,verdana" size="2" color="gray">
          <i>Guaranteed no spam!</i></font>
       </div>
      </td>
     </tr>
    </table>
    <br clear="all">
EOT
}

############################################################

sub pooclub_menu  # Old type - hardcoded menu
{
    print <<EOT;
    <table border="0" width="100" align="left" cellspacing="0" cellpadding="0">
     <tr>
      <td align="left" valign="top">
        <font face="verdana,arial,times" size="7" color="brown">
         pooclub
        </font>
        <br>
EOT
    pooclub_small_slogan();
    print <<EOT;
        <p>
        <font face="verdana,arial,times" size="4" color="brown">
         poopages
        </font>
    <!---------------------------------->
        <table cellpadding="1" cellspacing="1" border="0" width="100%" bgcolor="#cc6600" >
         <tr align="center"><td align="left">
          <table cellpadding="1" cellspacing="0" border="0" width="100%" bgcolor="#e8e8e8" >
           <tr>
            <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
             <font face="arial,helvetica" size="2">
             <a href="$THIS_CGI?p=today">Pooclub Today</a>
            </td>
            <td>&nbsp;</td> </tr>
           <tr>
            <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
             <font face="arial,helvetica" size="2">
             <a href="$THIS_CGI?p=clubnews">Club News</a>
            </td>
            <td>&nbsp;</td> </tr>
           <tr>
            <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
             <font face="arial,helvetica" size="2">
             <a href="$THIS_CGI?p=editorial">Editorial</a>
            </td>
            <td>&nbsp;</td> </tr>
           <tr>
            <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
             <font face="arial,helvetica" size="2">
             <a href="$THIS_CGI?p=patron">Our Patron</a>
            </td>
            <td>&nbsp;</td> </tr>
           <tr>
            <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
             <font face="arial,helvetica" size="2">
             <a href="$THIS_CGI?p=hosts">Your Hosts</a>
            </td>
            <td>&nbsp;</td> </tr>
           <tr>
            <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
             <font face="arial,helvetica" size="2">
             <a href="$THIS_CGI?p=thesaurus">Thesaurus</a>
            </td>
            <td>&nbsp;</td> </tr>
           <tr>
            <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
             <font face="arial,helvetica" size="2">
             <a href="$THIS_CGI?p=chat&c=phrases">Phrases & Their Origins</a>
            </td>
            <td>&nbsp;</td> </tr>
           <tr>
            <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
             <font face="arial,helvetica" size="2">
             <a href="$THIS_CGI?p=problems">Problems Page</a>
            </td>
            <td>&nbsp;</td> </tr>
           <tr>
            <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
             <font face="arial,helvetica" size="2">
             <a href="$THIS_CGI?p=classics">Classic Poos</a>
            </td>
            <td>&nbsp;</td> </tr>
           <tr>
            <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
             <font face="arial,helvetica" size="2">
             <a href="$THIS_CGI?p=about">About Pooclub</a>
            </td>
            <td>&nbsp;</td> </tr>
           <tr>
            <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
             <font face="arial,helvetica" size="2">
             <a href="$THIS_CGI?p=faq">Pooclub FAQ</a>
            </td>
            <td>&nbsp;</td> </tr>
          </table>
         </td></tr>
        </table>
        <br clear="all">


        <font face="verdana,arial,times" size="4" color="brown">
         pooboard
        </font>
    <!---------------------------------->
        <table cellpadding=1 cellspacing=1 border=0 width="100%" bgcolor="#cc6600" >
         <tr align="center"><td align="left">
          <table cellpadding=1 cellspacing=0 border=0 width="100%" bgcolor="#e8e8e8" >
          <tr>
          <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
          <font face=arial,helvetica size="2">
          <a href="http://groups.yahoo.com/group/Pooclub">Home</a>
          </td>
          <td>&nbsp;</td> </tr>
          <tr>
          <td>&nbsp;</td>
          <td nowrap valign="middle" align="left" >
          <font face=arial,helvetica size="2">
          <a href="http://groups.yahoo.com/group/Pooclub/messages">Messages</a>*
          </td>
          <td>&nbsp;</td> </tr>
          <tr>
          <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
          &nbsp;&nbsp;    <font face=arial,helvetica size="2">
          <a href="http://groups.yahoo.com/group/Pooclub/post">Post</a>*
          </td>
          <td>&nbsp;</td> </tr>
          <tr>
          <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
          <font face=arial,helvetica size="2">
          <a href="http://groups.yahoo.com/group/Pooclub/chat">Chat</a>*
          </td>
          <td>&nbsp;</td> </tr>
          <tr>
          <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
          <font face=arial,helvetica size="2">
          <a href="http://groups.yahoo.com/group/Pooclub/files">Files</a>
          </td>
          <td>&nbsp;</td> </tr>
          <tr>
          <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
          <font face=arial,helvetica size="2">
          <a href="http://groups.yahoo.com/group/Pooclub/links">Bookmarks</a>
          </td>
          <td>&nbsp;</td> </tr>
          <tr>
          <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
          <font face=arial,helvetica size="2">
          <a href="http://groups.yahoo.com/group/Pooclub/database">Database</a>*
          </td>
          <td>&nbsp;</td> </tr>
          <tr>
          <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
          &nbsp;&nbsp;    <font face=arial,helvetica size="2">
          <a href="http://groups.yahoo.com/group/Pooclub/database?method=reportRows&tbl=10&sortBy=1&sortDir=down&startAt=&prntRpt=1">Who's Poo</a>*
          </td>
          <td>&nbsp;</td> </tr>
          <tr>
          <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
          &nbsp;&nbsp;    <font face=arial,helvetica size="2">
          <a href="http://groups.yahoo.com/group/Pooclub/database?method=reportRows&tbl=12">POTM Nominations</a>*
          </td>
          <td>&nbsp;</td> </tr>
          <tr>
          <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
          <font face=arial,helvetica size="2">
          <a href="http://groups.yahoo.com/group/Pooclub/polls">Polls</a>*
          </td>
          <td>&nbsp;</td> </tr>
          <tr>
          <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
          <font face=arial,helvetica size="2">
          <a href="http://groups.yahoo.com/group/Pooclub/members">Members</a>*
          </td>
          <td>&nbsp;</td> </tr>
          </table>
          </td>
        </tr>
       </table>

<!---------------------------------->
        <br clear="all">
        <font face="verdana,arial,times" size="4" color="brown">
         poostuff
        </font>
        <table cellpadding="1" cellspacing="1" border="0" width="100%" bgcolor="#cc6600" >
         <tr align="center"><td align="left">
          <table cellpadding="1" cellspacing="0" border="0" width="100%" bgcolor="#e8e8e8" >
           <tr>
            <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
             <font face="arial,helvetica" size="2">
             <a href="$CGI_BIN/tripe.cgi?p=forth&a=pooclubbers">Birthdays & K-days</a>
            </td>
            <td>&nbsp;</td> </tr>
           <tr>
            <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
             <font face="arial,helvetica" size="2">
             <a href="$CGI_BIN/tripe.cgi?p=group&a=pooclubbers">Pooclub Prospects</a>
            </td>
            <td>&nbsp;</td> </tr>
           <tr>
            <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
             <font face="arial,helvetica" size="2">
             <a href="$CGI_BIN/tripe.cgi?p=group&a=poopals">Poopals Prospects</a>
            </td>
            <td>&nbsp;</td> </tr>
           <tr>
            <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
             <font face="arial,helvetica" size="2">
             <a href="$CGI_BIN/tripe.cgi?p=group&a=pookids">Pookids Prospects</a>
            </td>
            <td>&nbsp;</td> </tr>
           <tr>
            <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
             <font face="arial,helvetica" size="2">
             <a href="http://groups.yahoo.com/group/notnearmyhouse">Not Near My House</a>
            </td>
            <td>&nbsp;</td> </tr>
           <tr>
            <td>&nbsp;</td> <td nowrap valign="middle" align="left" >
             <font face="arial,helvetica" size="2">
             <a href="http://groups.yahoo.com/group/arseenders">ArseEnders</a>
            </td>
            <td>&nbsp;</td> </tr>
          </table>
         </td></tr>
        </table>
        <br clear="all">

        <font face="times,arial,helvetica" size="1" color="gray">
         *members only
        </font>
        <p>
        <font face="times,verdana,arial,helvetica" size="3" color="brown">
         <i>Want to be a part of Pooclub?</i>
        </font>
        <font face="verdana,arial,helvetica" size="2" color="brown">
         <a href="mailto:pooclub-subscribe\@yahoogroups.com">Subscribe</a>&nbsp;here.
        </font>

      </td>
     </tr>
    </table>
    <br clear="all">

EOT
}

############################################################

sub pooclub_event
{
    # Displays todays event (beneath the calendar on the
    # welcome page).  
    # Events are found either in the "events.dat" file
    # or in the "events" directory under filenames
    # "January01", "January02", ... , "December31".
    # All events matching the paramterised date are shown.
    # If no events are found, nothing is displayed.

    my $day   = shift(@_);
    my $mday  = $u_mday;
    my $month = $u_month;
    my $fname = "";
    my $size  = 2;
    my $hrwidth = "50%";
    my $align = "center";
    my $events = 0;


    if ($day)  # Use arg's date instead of today's date
    {
        ($not_used1, $mday, $month, $not_used2) = split /;/, parse_date ($day);
    }
    else
    {
        $day = $u_yyyymmdd;
    }

    $fname = sprintf "%s%02d", $month, $mday;

        print <<EOT;
        <table border="0" align="$align"><tr>
         <td align="left" valign="top">
          <font face="arial,helvetica,times" size="$size">
EOT
    if (open (EVENTFILE, "$EVENTS_FILE"))
    {
        while (<EVENTFILE>)
        {
            chop;
            ($new, $monthDD, $text) = split /;/, $_, 3;
            if ($monthDD eq $fname)
            {
                if ($events > 0) {print "<hr width=\"$hrwidth\">\n";}
                $events++;
                print "$text\n";
            }
        }
        close (EVENTFILE);
    }

    if (open DATEFILE, "$EVENTDIR/$fname")
    {
        if ($events > 0) {print "<hr width=\"$hrwidth\">\n";}

        while (<DATEFILE>) { print $_; }
        close (DATEFILE);
        $events++;
    }
    if ($events > 0) {print "<p>";}

    my $forecast = random_line ("$LISTDIR/pooclubweather.list", $day);

    print <<EOT;
          </font>
          <font face="arial,times,verdana" size="2">
           Weather: Today it will be ${forecast}.
          </font><p>

<!--
          <center>
           <font face="arial,times,verdana" size="1">
            <a href="$THIS_CGI?p=events">Forthcoming<br>Events And Weather</a>
           </font>
          </center>
-->
         </td>
        </tr></table>
EOT
}

############################################################

sub pooclub_todays_date
{
    my ($not_used, $u_day) = split / /, date_calc("-s");
    my $dayOfWeek = $u_day;
    if ($dayOfWeek eq "Saturday")   # Link to SaTURDay page
    {
        $dayOfWeek = "Sa<a href=\"$THIS_CGI?p=html&a=saturday\"><font face=\"verdana\" size=\"2\" color=\"brown\">TURD</font></a>ay";
    }

print <<EOT;
   <table align="center" cellpadding=1 cellspacing=1 border=0 width="80" bgcolor="brown">
    <tr><td align="center">
     <table align="center" cellpadding=1 cellspacing=0 border=0 width="100%" bgcolor="#e8e8e8">
      <tr><td align="center">
       <font face="arial" size="2" color="black">
        $dayOfWeek<br>
       <font face="verdana,arial" size="6"><b><i>
        $u_mday
       </i></b></font>
         <br>
        <b>$u_month</b>
         <br>
        $u_year
       </font>
      </td></tr>
     </table>
    </td></tr>
   </table>
EOT
}

############################################################

sub pooclub_slogan
{
    my ($r_sec,$r_min,$r_hour,$r_mday,$r_mon,$r_year,$r_wday,$r_yday,$r_isdst)
        = localtime(time);
    my $seed = $r_sec + $r_yday;
    my $slogan = random_line ("$LISTDIR/pooclubslogans.list", $seed);
    $slogan =~ s/\s+$//;

    print <<EOT;
       <center>
       <font face="times,verdana,arial" size="5" color="gray">
        <i>"$slogan"</i>
       </font>
       </center>
       <p>
EOT
}

############################################################

sub pooclub_medium_slogan
{
    my ($r_sec,$r_min,$r_hour,$r_mday,$r_mon,$r_year,$r_wday,$r_yday,$r_isdst)
        = localtime(time);
    my $seed = $r_sec + $r_yday;
    my $slogan = random_line ("$LISTDIR/pooclubslogans.list", $seed);
    $slogan =~ s/\s+$//;

    print <<EOT;
       <font face="arial,times,verdana" size="2" color="gray">
        "$slogan"
       </font>
EOT
}

############################################################

sub pooclub_small_slogan
{
    my ($r_sec,$r_min,$r_hour,$r_mday,$r_mon,$r_year,$r_wday,$r_yday,$r_isdst)
        = localtime(time);
    my $seed = $r_sec + $r_yday;
    my $slogan = random_line ("$LISTDIR/pooclubslogans.list", $seed);
    $slogan =~ s/\s+$//;

    print <<EOT;
       <center>
       <font face="arial,times,verdana" size="1" color="brown">
        $slogan
       </font>
       </center>
EOT
}

############################################################

sub zz_your_accounts
{
    print qq(

        <div style="text-align: left; font-size: 10px;">
Your accounts: 
<a href="http://groups.google.com/group/pooclub">Forum</a> |
<a href="http://pooclub.pbwiki.com">Wiki</a> |
Poopages
);
    if (is_logged_in())
    {
        my $whoami = username_and_priv();
        $whoami = "Login: " . $whoami if (! $whoami =~ /\[/);
        print qq(
         <a href="${THIS_SCRIPT}?page=account">edit</a> -
         <a href="${THIS_SCRIPT}?page=logout">logout</a>
         $whoami
        );
    }
    else
    {
        print qq(
         <a href="${THIS_SCRIPT}?page=login">login</a> -
         <a href="${THIS_SCRIPT}?page=signup">sign up</a>
);
    }
    print qq(</div>);
}

############################################################

sub your_accounts
{
    print qq(

        <span style="text-align: right; font-size: 10px;">
Your accounts: 
<a href="http://groups.google.com/group/pooclub">Forum</a> |
<a href="${THIS_SCRIPT}?page=account">Poopages</a> |
<a href="http://pooclub.pbwiki.com">Wiki</a>
        </span>
);
}

############################################################

sub pooclub_banner
{
    my ($r_sec,$r_min,$r_hour,$r_mday,$r_mon,$r_year,$r_wday,$r_yday,$r_isdst)
        = localtime(time);
    my $seed = $r_sec + $r_yday;
    my @bgColourList = ("green", "green", "green", "green", "blue", "blue", "brown");
    my $bgColour = $bgColourList[randy(0, scalar(@bgColourList)-1, $seed)];

    my $r_month = $monthList[$r_mon];
###    my $slogan = pooclub_slogan();

        my $fname = "$POODIR/banner_pooclub.html";
###        my $myRan = randy(0, 4, $seed);
###        $fname = "$POODIR/banner_cuntlip_tidy.html" if ( $myRan == 4);  # one chance in five
    print qq(
<table border="0" width="100%" cellspacing="0" cellpadding="8" bgcolor="#f0f0c0">
 <tr>
  <td align="left">
   <font face="verdana,arial,times" size="6" color="brown">
    pooclub
   </font>

);
    your_accounts();
    print qq(
  </td>
 </tr>
</table>
);
}

############################################################
# Old style 'Fake Ads' banner

sub pooclub_banner_fakeads
{
    my ($r_sec,$r_min,$r_hour,$r_mday,$r_mon,$r_year,$r_wday,$r_yday,$r_isdst)
        = localtime(time);
    my $seed = $r_sec + $r_yday;
    my @bgColourList = ("green", "green", "green", "green", "blue", "blue", "brown");
    my $bgColour = $bgColourList[randy(0, scalar(@bgColourList)-1, $seed)];

    my $r_month = $monthList[$r_mon];
    my @bdayList = ();

    # Find pooclubber's birthday
    my $groupFile = "$GROUPDIR/pooclubbers.dat";
    open (GROUPFILE, "$groupFile");
    while (<GROUPFILE>)
    {
        next if (/^#/);
        next if (/^\s*$/);
        chop;
        ($name, $b_mday, $b_month, $b_year) = split /;/, $_;
        if ($b_mday == $r_mday && $b_month eq $r_month)
        {
            push @bdayList, $name;
        }
    }

    if (scalar @bdayList == 0)  # No birthday's today so show fakeAd
    {
        my $fname = "$POODIR/banner_fakeads.html";
        my $myRan = randy(0, 4, $seed);

        $fname = "$POODIR/banner_cuntlip_tidy.html" if ( $myRan == 4);  # one chance in five
    
        open (FD, $fname);
        while (<FD>)
        {
            s/\$RANDOM_BG_COLOUR/$bgColour/g;
            s/\$IMGSHITEDIR/$IMGSHITEDIR/g;
            print $_;
        }
        close (FD);
        print "<p>";
    }
    else
    {
        print <<EOT;
<table border="0" width="100%" cellspacing="0" cellpadding="8" bgcolor="$bgColour">
 <tr>
  <td align="center">
   <font face="times,verdana,arial" size="7" color="white"><i>
EOT

        for $n (@bdayList)
        {
            print "<nobr>Happy Birthday, $n!</nobr><br>\n";
        }
        print <<EOT;
   </i></font>
  </td>
  </tr>
</table>
<p>
EOT
    }
}

############################################################

sub pooclub_todays_poem
{
    my $poem_record = todays_poem($u_yyyymmdd);
    my ($shiteId, $poemTitle, $poemAuthor, $poemType, $imageCode) = split /;/, $poem_record;

    print <<EOT;
   <table align="center" cellpadding=1 cellspacing="0" border=0 width="100%" bgcolor="brown">
    <tr><td align="center">
     <table align="center" cellpadding="0" cellspacing=0 border=0 width="100%" bgcolor="#e8e8e8">
      <tr><td align="center">

       <div class="barlogo" align="center">
        <b class="barlogo">Shite</b>space
       </div>
       <font face="arial,verdana,times" size="2" color="black">
         proudly presents the<br>
         Poem Of The Day:<br>
       </font>
       <font face="arial,verdana,times" size="3" color="brown">
         <a href="${STUFF_CGI}?p=rate"><b>$poemTitle</b></a>.
       </font>
      </td></tr>
     </table>
    </td></tr>
   </table>
EOT
}

############################################################

sub pooclub_todays_poem_OLD
{
    my $poem_record = todays_poem($u_yyyymmdd);
    my ($shiteId, $poemTitle, $poemAuthor, $poemType, $imageCode) = split /;/, $poem_record;

    print <<EOT;
   <table align="center" cellpadding=1 cellspacing=1 border=0 width="100%" bgcolor="brown">
    <tr><td align="center">
     <table align="center" cellpadding=4 cellspacing=0 border=0 width="100%" bgcolor="#e8e8e8">
      <tr><td align="center">
       <a href="${STUFF_CGI}?p=shite"><img src="${IMGDIR2}/logo.gif" alt="Shitespace" border="0"></a>
       <br clear="all">
       <font face="arial,verdana,times" size="2" color="black">
         proudly presents the
        <a href="$STUFF_CGI?p=shite#todays_poem">Poem</a> Of The Day:<br>
       </font>
       <font face="arial,verdana,times" size="3" color="brown">
         <b>$poemTitle</b>.
       </font>
      </td></tr>
     </table>
    </td></tr>
   </table>
EOT
}

############################################################
sub pooclub_daysold
{
   my $pooday = "20000727";
   my $daysold = date_calc("-c $pooday $u_yyyymmdd");

    print <<EOT;
       <font face="verdana,arial,helvetica" size="3" color="black">
        Poodate: 
        <a href="$THIS_CGI?p=faq#founded">$daysold</a>
       </font>
EOT
}

############################################################

sub pooclub_prospects
{
    my $pooclub_seed = "20000727";
    my $prosp_size = scalar(@prospects);
    my $total = calc_total ($pooclub_seed);
    my $a = "a";

    if ($total >= $prosp_size - 1) { $prosp = $prospects[$prosp_size - 1]; }
    else { $prosp = $prospects[$total]; }

    my $p = substr ($prosp, 0, 1);
    if ($p =~ /a|e|i|o|u/i) {$a = "an";}

    print <<EOT;
       <font face="times,verdana,arial,helvetica" size="4" color="black">
        <i>It is $a "$prosp" day for pooclub.</i>
       </font>
       <div align="right">
       <font face="arial,times,verdana" size="1">see
        <a href="http://www.shite.org/pooclubbers">why</a>.
       </font>
       </div>
EOT
}

############################################################

sub pooclub_goodday
{
    my $today = shift(@_);
    my $pooday = "20000727";
    my $pooseed = $today + $pooday;
    my @gooddayList = ();
    open (INFILE, "$LISTDIR/goodday.list");

    while (<INFILE>)
    {
        chop;
        next if (/^\s*$/);  # ignore blank lines
        next if (/^#/);     # ignore commented lines

        push @gooddayList, $_;
    }
    close (INFILE);

    random_sort(\@gooddayList, $pooseed);
#    print <<EOT;
#       <font face="arial,verdana,times" size="2" color="black">
#       Today is a good day to $gooddayList[0]
#       and a bad day to $gooddayList[4].
#       </font>
#EOT
    print qq(
       Today is a good day to $gooddayList[0]
       and a bad day to $gooddayList[4].
    );
}

############################################################

sub pooclub_header_very_old
{
    print <<EOT;
Content-type: text/html

<html>
<head>
  <title>Pooclub</title>
  <meta name="Author" content="Mike Two-Sheds">
  <meta name="Description" content="The most fabulous place to be on the internet">
  <meta name="Keywords" content="mike, kate, prospects, k-days, kday, events,
              poetry, poems, humour, humor, literature">
</head>
<body text="black" link="blue" vlink="brown">
EOT
}

############################################################

sub pooclub_header
{
    my ($title, $description) = @_;
    $title       = "Pooclub: " . $title if (! ($title =~ /^Pooclub:/));
    $description = "The most fabulous place to be on the internet" if (description eq "");

#    print <<EOT;
#Content-type: text/html
#
    print <<EOT;
<html>
<head>
  <title>$title</title>
  <meta name="Author" content="Mike Two-Sheds">
  <meta name="Description" content="$description">
  <meta name="Keywords" content="mike, kate, brian, ian, rachel, sarah, karl, jim, mollusc,
              events, weather, poetry, poems, humour, humor, literature,
              grimsby, leeds, london, shed, shenanigans">
  <link type="text/css" rel="stylesheet" href="${CSSDIR}/tripe_style.css" />	
</head>

<body text="black" link="blue" vlink="brown"
 style="background-color: #f0ece0;" />
EOT
}

#<style type="text/css">
#h1 {color: brown;
#    font-size: 14pt;
#    font-family: arial,verdana}
#h2 {color: black;
#    font-size: 18pt;
#    font-family: times;
#    font-style: italic}
#p {color: black;
#    font-size: 10pt;
#    font-family: verdana}   
#</style>

#
############################################################

sub pooclub_header_not
{
}

############################################################

sub copyright_20091102
{
    my $names = shift (@_);
    my $years = $r_year;
    if ($names eq "") { $names = "Pooclub Ltd"; }
    $years = "1995-$years" if ($names =~ /^Shitespace/);
    $years = "2000-$years" if ($names =~ /^Pooclub/);

    print <<EOT;
<!--- top right-hand corner --->
<table width="100%" border="0">
 <tr><td align="left">
  <font size="1" face="verdana,arial,geneva,helvetica" color="#777777">
   <a href="http://pooclub.pbwiki.com">&nbsp;</a>
  </font>
 </td></tr>
</table><br clear="all">
    <!--- Copyright --->
    <br clear="all"><hr>
<table width="100%" border="0">
 <tr><td align="left">
<font size="1" face="verdana,arial,geneva,helvetica" color="#777777">
<a href="http://lilyvillas.com?ref=pc">Luxury Private Holiday Villas in Bodrum, Turkey</a>
</font>
 </td>
 <td align="center">
<font size="1" face="verdana,arial,geneva,helvetica" color="#777777">
</font>
</td>
<td align="center">
<font size="1" face="verdana,arial,geneva,helvetica" color="#777777">
<a href="http://pooclub.pbwiki.com">&nbsp;</a>
</font>
</td><td align="right">
<font size="1" face="verdana,arial,geneva,helvetica" color="#777777">
</font>
 </td><td align="right">
<font size="1" face="verdana,arial,geneva,helvetica" color="#777777">
<a href="http://lilyvillas.com?ref=pc">Cheap Holiday Villas To Rent</a>
</font>
 </td></tr>
</table>
<hr>
    <center>
     <font size="1" face="verdana,arial,geneva,helvetica"
           color="#777777">
     <nobr>Copyright &copy; $years $names.</nobr>
     <nobr>All Rights Reserved.</nobr>
     </font>
    </center><br>
 </body>
</html>
EOT
}

############################################################

sub copyright
{
    my $names = shift (@_);
    my $years = $r_year;
    if ($names eq "") { $names = "Pooclub Ltd"; }
    $years = "1995-$years" if ($names =~ /^Shitespace/);
    $years = "2000-$years" if ($names =~ /^Pooclub/);

    print <<EOT;

    <!--- Copyright --->
    <br clear="all" />
    <center>
     <font size="1" face="verdana,arial,geneva,helvetica"
           color="#777777">
     <nobr>Copyright &copy; $years $names.</nobr>
     <nobr>All Rights Reserved.</nobr>
     </font>
    </center>

<table width="100%" border="0">
 <tr><td align="left">
  <font size="1" face="verdana,arial,geneva,helvetica" color="#777777">
   <a href="http://lilyvillas.com?ref=pc">Luxury Private Holiday Villas in Bodrum, Turkey</a>
  </font>
 </td>
 <td align="center">
  <font size="1" face="verdana,arial,geneva,helvetica" color="#777777">
  </font>
 </td>
 <td align="center">
  <font size="1" face="verdana,arial,geneva,helvetica" color="#777777">
   <a href="http://pooclub.pbwiki.com">&nbsp;</a>
  </font>
 </td><td align="right">
  <font size="1" face="verdana,arial,geneva,helvetica" color="#777777">
  </font>
 </td><td align="right">
  <font size="1" face="verdana,arial,geneva,helvetica" color="#777777">
   <a href="http://lilyvillas.com?ref=pc">Cheap Holiday Villas To Rent</a>
  </font>
 </td></tr>
</table>


 </body>
</html>
EOT
}

############################################################

sub poopeople
{
    my $arg1 = shift(@_);
    my $file  = "$GROUPDIR/$groupHash->{$arg1}->{filename}";
    my $groupName  = "$groupHash->{$arg1}->{groupname}";
    my $date_seed = $u_yyyymmdd;
    my $points = 0;
    my $topPoints = 0;
    my $topName = "";
    my $topMday = "";
    my $topMonth = "";
    my $topYear = "";
    my $stockDayMsg = "";
    my $numStockDays = 0;

    open (INF, "$file") || return -1;

    while (<INF>)
    {
        chop;
        next if (/^\s*$/);  # ignore blank lines
        next if (/^#/);     # ignore commented lines
        next if (/^!/);     # ignore "about" lines

        ($name, $dob_mday, $dob_month, $dob_year, $special) = split /;/, $_;

        if ($special =~ /^\s*$/)
        {
            $dob_yyyymmdd = format_date ($dob_mday, $dob_month, $dob_year);
            $points = calc_total($dob_yyyymmdd);

            if ($points > $topPoints)
            {
                $topPoints = $points;
                $topName   = $name;
                $topMday   = $dob_mday;
                $topMonth  = $dob_month;
                $topYear   = $dob_year;
            }

            for $i (@stock_days)
            {
                $stockDayMsg .= "<font face=\"verdana,arial,times\" size=\"2\" color=\"black\">";
                $stockDayMsg .= "Happy $stockHash->{$i}->{singular} Day, ";
                $stockDayMsg .= "<a href=\"$CGI_BIN/tripe.cgi?p=prospects&bd=${dob_mday}&bm=${dob_month}&by=${dob_year}\"><font face=\"verdana,arial,times\" size=\"2\" color=\"black\">$name</font></a>!";
                $stockDayMsg .= "</font> <br>";
                $numStockDays++;
            }
        }
    }

    my $poostarName = $topName;
    $poostarName =~ s/\s+/_/g;
    my ($member, $sex, $name, $file) = split /;/, get_trooper_rec($poostarName);
    $poostarFile = "$IMGTROOPDIR/$file";

    my $altTag = $poostarFile;

#    my $poostarFile = "$IMGTROOPDIR/stor_" . lc($poostarName) . ".gif";
#    my $isBinary = -B $poostarFile;
#    my $altTag = "Today's Poostar is $topName";

#    if (! $isBinary)  # Got no picture of today's poostar so choose a random one
#    {
#        my $mod = $topPoints % 5;
#        $poostarFile = "$IMGTROOPDIR/trooper${mod}.gif";
#        $altTag = "Not really $topName - we're still waiting for a picture";
#    }

    print <<EOT;
     <table border="0" cellspacing="0" cellpadding="5" align="left" width="100%">
      <tr><td align="left" valign="top" width="10%">
       <img src="$poostarFile" alt="$altTag" border="0">
      </td><td align="left" valign="top" width="90%"><br>
       <font face="verdana,arial,times" size="2" color="black">
        <img src="$IMGDIR2/star.gif" alt="Top Pooclubber" border="0">
         Today's Poostar is
        <a href="$CGI_BIN/tripe.cgi?p=prospects&bd=${topMday}&bm=${topMonth}&by=${topYear}"><font face="verdana,arial,times" size="2" color="black">$topName</font></a>
with $topPoints points.  $prospects[$topPoints]<br>
EOT

    if ($numStockDays > 0)
    {
        print "<br>$stockDayMsg\n";
    }

    print "<br>";
    show_forth("pooclubbers", 5, 4);
    show_forth("poopals", 2, 2);
    show_forth("pookids", 4, 3);
    show_forth("celebrities", 2, 2);

    print <<EOT;
       </td></tr></table>
      <br clear="all">
EOT
}

############################################################

sub show_forth # Show forthcoming k-days and birthdays
{              # within a group
    my ($arg1, $kDaysAhead, $bDaysAhead) = @_;
    my $record;
    my $daysOld = 0;
    my @memberList = ();
    my $file  = "$GROUPDIR/$groupHash->{$arg1}->{filename}";
    my $groupName  = "$groupHash->{$arg1}->{groupname}";
    my $groupLink  = "<a href=\"$CGI_BIN/tripe.cgi?p=forth&a=$arg1\">$groupName</a>:<br>";

    open (INF, "$file") || return -1;

    while (<INF>)
    {
        chop;
        next if (/^\s*$/);  # ignore blank lines
        next if (/^#/);     # ignore commented lines
        next if (/^!/);     # ignore "about" lines

        $record = {};
        ($name, $dob_mday, $dob_month, $dob_year, $special) = split /;/, $_;

        if ($special =~ /^\s*$/)
        {
            $record->{name}  = $name;
            $dob_yyyymmdd = format_date ($dob_mday, $dob_month, $dob_year);
            $record->{dob_yyyymmdd} = $dob_yyyymmdd;

            # Calculate next k-day
            $daysOld    = date_calc("-c $dob_yyyymmdd $u_yyyymmdd");
            $thousands  = int($daysOld / 1000);
            $remainder  = $daysOld % 1000;
            $daysToKday = 0;

            if ($remainder > 0)  # It's not your k-day today
            {
                $thousands++;
                $daysToKday = (1000 * $thousands) - $daysOld;
            }
            $record->{daysOld}    = $daysOld;
            $record->{daysToKday} = $daysToKday;
            $record->{nextKday}   = $thousands;
            $record->{kday_yyyymmdd} = date_calc("-d$daysToKday $u_yyyymmdd");

            # Calculate next birthday
            $dd = substr ($dob_yyyymmdd, 6, 2);
            $mm = substr ($dob_yyyymmdd, 4, 2);
            $yyyy = substr ($u_yyyymmdd, 0, 4);
            $bday = "$yyyy$mm$dd";

            if ($bday < $u_yyyymmdd)  # birthday has already happened this year
            {                         # so next birthday will be next year
                $yyyy++;
                $bday = "$yyyy$mm$dd";
            }
            $record->{age} = $yyyy - substr($dob_yyyymmdd, 0, 4);
            $record->{bday_yyyymmdd} = $bday;

            push @memberList, $record;
        }
    }
    close (INF);

    my $count = 0;
    for $rec (sort sortbydaysToKday @memberList)
    {
        $nextKday = $rec->{kday_yyyymmdd};
        @parse = split /;/, parse_date($nextKday);
        $kday = sprintf "%s %2d %s", substr($parse[0],0,3),
                                        $parse[1],
                                        $parse[2];
        if ($rec->{daysToKday} == 0)
        {
            print "$groupLink" if ($count++ == 0);
            print "Happy K-day $rec->{name}! $rec->{nextKday},000 days old today!<br>\n";
        }
        elsif ($rec->{daysToKday} < $kDaysAhead)
        {
            print "$groupLink" if ($count++ == 0);
            print "$rec->{name} will be $rec->{nextKday},000 days old on $kday<br>\n";
        }
    }

    for $rec (sort sortbyBday @memberList)
    {
        @parse = split /;/, parse_date($rec->{bday_yyyymmdd});

        if ($parse[0] eq "INVALID_DATE") # It's 29th Feb on a non-leap year
        {
            $rec->{bday_yyyymmdd}--;
            @parse = split /;/, parse_date($rec->{bday_yyyymmdd});
        }
        $bday = sprintf "%s %2d %s", substr($parse[0],0,3),
                                        $parse[1],
                                        $parse[2];
        $daysToBday = date_calc("-c $u_yyyymmdd $rec->{bday_yyyymmdd}");

        if ($daysToBday == 0)
        {
            print "$groupLink" if ($count++ == 0);
            print "Happy Birthday $rec->{name}! $rec->{age} today!<br>\n";
        }
        elsif ($daysToBday < $bDaysAhead)
        {
            print "$groupLink" if ($count++ == 0);
            print "$rec->{name} will be $rec->{age} years old on $bday<br>\n";
        }
    }
}

############################################################

sub show_poo_prospects
{
    my ($groupId, $shiteHashRef, $shiteListRef) = @_;
    my $record;
    my $pros = "";
    my @memberList = ();
    my @specialList = ();
    my $file  = "$GROUPDIR/$groupHash->{$groupId}->{filename}";
    my $name  = "$groupHash->{$groupId}->{groupname}";
    my $font0 = "</font>";
    my $font1 = "<font face=verdana,arial size=1 color=gray>";
    my $font2 = "<font face=verdana,arial size=1>";
    my $bgcolour1 = $colour[4];
    my $bgcolour2 = $colour[5];
    $bgcolour1 = "white";
    $bgcolour2 = "white";

    open (INF, "$file") || return -1;

    while (<INF>)
    {
        chop;
        next if (/^\s*$/);  # ignore blank lines
        next if (/^#/);     # ignore commented lines
        next if (/^\!/);    # ignore "about" lines

        $record = {};
        ($g_name, $g_mday, $g_month, $g_year, $g_special) = split /;/, $_;
    
        $record->{g_name}  = $g_name;
        $record->{g_mday}  = $g_mday;
        $record->{g_month} = $g_month;
        $record->{g_year}  = $g_year;
        $record->{g_special} = $g_special;
        $record->{g_yyyymmdd} = format_date ($g_mday, $g_month, $g_year);
        $record->{g_points}  = calc_total ($record->{g_yyyymmdd});

        if ($g_special =~ /^\s*$/)
        {
            push @memberList, $record;
        }
        else
        {
            push @specialList, $record;
        }
    }
    close (INF);

    print <<EOT;
   <!---- Group Prospects --->
   <table align="left" border="0" cellspacing="4" cellpadding="0">
    <tr>
     <td align="left" valign="top">
      <font face="verdana,arial" size="2"><b>$name Prospects</b>
       <center>
       <br>

      <!---- Group Table --->
      <table align="center" border="0" bgcolor="$bgcolour2"
             cellspacing="0" cellpadding="1">
       <tr bgcolor="$bgcolour1">
        <td valign="bottom" bgcolor=white>&nbsp;</td>
        <td>$font2 <b>Name</b>$font0</td>
        <td>$font2 <b>Prospects</b> $font0</td>
        <td>$font2 <b>Points</b>$font0</td>
        <td bgcolor="white">$font2 &nbsp; $font0</td>
        <td bgcolor="white" align="left">$font2 <b>Recommended Reading</b> $font0</td>
       </tr>
EOT
    my $i = 0;
    my $seed = 0;

#    my $nameType = randy (1, 15, $u_yyyymmdd);  # Which list of poonames shall we use today?
#    $nameType = 1 if ($nameType > 7);
#
#    if (substr ($u_yyyymmdd, 4, 4) eq "0214")
#    {
#        $nameType = 2;  # Use soppy list on Valentine's Day
#    }
    
#    my @poonamesList1 = ();
#    my @poonamesList2 = ();
#    my @poonamesList3 = ();
#    my $poonamesFile1 = "$LISTDIR/poonames_${nameType}_1.list";
#    my $poonamesFile2 = "$LISTDIR/poonames_${nameType}_2.list";
#    my $poonamesFile3 = "$LISTDIR/poonames_${nameType}_3.list";
#    my $poonamesCount1 = all_lines ($poonamesFile1, \@poonamesList1);
#    my $poonamesCount2 = all_lines ($poonamesFile2, \@poonamesList2);
#    my $poonamesCount3 = all_lines ($poonamesFile3, \@poonamesList3);

    my $shiteListSize = scalar(@$shiteListRef);

    for $g (sort sortbypoints @memberList)
    {
        $i++;
        $g_mon = substr ($g->{g_month}, 0, 3);
        $args = "p=prospects&ud=$u_mday&um=$u_month&uy=$u_year";
        $args .= "&bd=$g->{g_mday}&bm=$g->{g_month}&by=$g->{g_year}";
        $pros = get_prospects ($g->{g_points});

        $seed = get_seed($g->{g_name}) + $u_yyyymmdd;
#        $pooname1 = $poonamesList1[randy(0, $poonamesCount1-1, $seed+1)];
#        $pooname2 = $poonamesList2[randy(0, $poonamesCount2-1, $seed+2)];
#        $pooname3 = $poonamesList3[randy(0, $poonamesCount3-1, $seed+3)];
#
#        if ($nameType eq "1")
#        {
#            $pooname2 =~ s/\s+$//;
#            $pooname = "$pooname1 $pooname2$pooname3";
#        }
#        else
#        {
#            $pooname = "$pooname1 $pooname2 $pooname3";
#        }

        $shiteNum = randy(0, $shiteListSize - 1, $seed);
        $readShiteId = $$shiteListRef[$shiteNum];
        $readPageId  = $$shiteHashRef->{$readShiteId}->{pageId};
        $readTitle   = $$shiteHashRef->{$readShiteId}->{title};
###printf "FISH %-10s seed=%8d num=%3d size=%3d %s\n", $g->{g_name}, $seed, $shiteNum, $shiteListSize, $readShiteId;

        print <<EOT;
            <tr>
            <td bgcolor="white" align="right">$font2 ${i}&nbsp;&nbsp;&nbsp;$font0</td>
            <td>$font2 <a href="$CGI_BIN/tripe.cgi?p=prospects&bd=$g->{g_mday}&bm=$g->{g_month}&by=$g->{g_year}">$g->{g_name}</a>&nbsp;&nbsp;$font0</td>
            <td>$font2 $pros $font0</td>
            <td align="right">$font2 $g->{g_points} $font0</td>
            <td bgcolor="white">&nbsp;</td>
            <td bgcolor="white" align="left">$font2
            <a href="$STUFF_CGI?p=$readPageId#$readShiteId">$readTitle</a>
            $font0</td>
            </tr>
EOT
    }

    print "</table>";

    print <<EOT;
     </td>
    </tr>
   </table>
   <br clear="all"><br>
EOT

}

############################################################

sub show_poo_league
{
    my ($groupId, $days, $yyyymmdd) = @_;

    $groupId = "pooclubbers" if ($groupId eq "");
    $days     = 5            if ($days eq "" || $days < 1);
    $yyyymmdd = $u_yyyymmdd  if ($yyyymmdd eq "");

    my $tmp_yyyymmdd;
    my $points = 0;
    my $average = 0;
    my $score =  0;
#    my $stretchingFactor =  5; # For stretching average prospects points over prospects names list
    my $prospectsMedian  = 14; # Actually, this is data dependent but it's just too tricky to work out
                               # so let's hardcode it here.
    my $prospectsAverage = 17; # (As above)
    my @pointsList = ();       # For calculating median
    my $champScore = 1;
    my $champTotal = -100000;  # Top scoring prospects total
    my $chumpTotal = 1000000;  # Bottom scoring prospects total
    my $champRec;
    my $chumpRec;
    my $poostarRec;
    my $star = "";

    my $record;
    my $poostarPoints = 0;
    my $rating = "";
    my $ratingsListRef = "";
    my $comment = "";
    my @memberList = ();
    my @specialList = ();
    my $file  = "$GROUPDIR/$groupHash->{$groupId}->{filename}";
    my $name  = "$groupHash->{$groupId}->{groupname}";
    my $font0 = "</font>";
    my $font1 = "<font face=verdana,arial size=2 color=gray>";
    my $font2 = "<font face=verdana,arial size=2>";
    my $bgcolour1 = $colour[4];
    my $bgcolour2 = $colour[5];
    $bgcolour1 = "white";
    $bgcolour2 = "white";
    my $midGrey   = "#d0d0d0";
    my $paleGrey  = "#e8e8e8";
    my $midBrown  = "#e0d0a0";
    my $paleBrown = "#f0e0b0";
    my $white = "white";

    open (INF, "$file") || return -1;

    while (<INF>)
    {
        chop;
        next if (/^\s*$/);  # ignore blank lines
        next if (/^#/);     # ignore commented lines
        next if (/^\!/);    # ignore "about" lines

        $record = {};
        ($g_name, $g_mday, $g_month, $g_year, $g_special, $g_sex) = split /;/, $_;
    
        $record->{g_name}     = $g_name;
        $record->{g_mday}     = $g_mday;
        $record->{g_month}    = $g_month;
        $record->{g_year}     = $g_year;
        $record->{g_special}  = $g_special;
        $record->{g_sex}      = $g_sex;
        $record->{g_yyyymmdd} = format_date ($g_mday, $g_month, $g_year);
        $record->{g_total}    = 0;         # total points
        $record->{g_min}      = 1000000;   # minimum points
        $record->{g_max}      = -100000;   # maximum points
        $record->{g_med}      = 0;         # median points

        $tmp_yyyymmdd = date_calc ("-d-$days $yyyymmdd");   # start $days ago
        @pointsList = ();

        while ($tmp_yyyymmdd < $yyyymmdd)
        {
            $tmp_yyyymmdd = date_calc("-d1 $tmp_yyyymmdd"); # increment date counter
            $date_seed = $tmp_yyyymmdd;
            get_stock($date_seed);
            $points = calc_total ($record->{g_yyyymmdd});
            $record->{g_total} += $points;
            $record->{g_min} = $points if ($points < $record->{g_min});
            $record->{g_max} = $points if ($points > $record->{g_max});
            push @pointsList, $points;
        }

        $record->{g_today} = $points;  # today's points
        $record->{g_med}   = (sort sortnumeric @pointsList)[scalar(@pointsList)/2];
        $record->{g_mean}  = $record->{g_total} / $days;
        $record->{g_score} = ($record->{g_mean} - 10);

        if ($record->{g_total} > $champTotal)  # He's the champ!
        {
            $champTotal = $record->{g_total};
            $champScore = $record->{g_score};
            $champRec   = $record;
        }

        if ($record->{g_total} < $chumpTotal)  # He's the chump
        {
            $chumpTotal = $record->{g_total};
            $chumpRec   = $record;
        }

        if ($record->{g_today} > $poostarPoints)  # He's today's poostar
        {
            $poostarPoints = $record->{g_today};
            $poostarRec    = $record;
        }

        if ($g_special =~ /^\s*$/)
        {
            push @memberList, $record;
        }
        else
        {
            push @specialList, $record;
        }
    }
    close (INF);

    print <<EOT;
      <font face="verdana,arial" size="2">$name prospects over last $days days
       <br clear="all"><br>

      <!---- Group Table --->
      <table align="center" border="0" bgcolor="$white"
             cellspacing="1" cellpadding="4">
       <tr bgcolor="$white">
        <td bgcolor="$white" valign="bottom">&nbsp;</td>
        <td bgcolor="$midBrown">$font2 <b>Name</b>$font0</td>
        <td bgcolor="$midBrown">$font2 <b>Rating</b> $font0</td>
        <td bgcolor="$midBrown">$font2 <b>Average</b>$font0</td>
        <td bgcolor="$midGrey">$font2 <b>Min</b>$font0</td>
        <td bgcolor="$midGrey">$font2 <b>Med</b>$font0</td>
        <td bgcolor="$midGrey">$font2 <b>Max</b>$font0</td>
        <td bgcolor="$midGrey">$font2 <b>Today</b>$font0</td>
        <td bgcolor="$white">$font2 &nbsp; $font0</td>
       </tr>
EOT

    my $i = 0;

    for $g (sort sortbyscore @memberList)
    {
        $i++;
        $g_mon = substr ($g->{g_month}, 0, 3);
        $args  = "p=prospects&ud=$u_mday&um=$u_month&uy=$u_year";
        $args .= "&bd=$g->{g_mday}&bm=$g->{g_month}&by=$g->{g_year}";
        $star  = "";

        if ($g->{g_name} eq $poostarRec->{g_name})
        {
            $star = "<img src=\"$IMGDIR2/star.gif\" alt=\"Today's Poostar\" border=\"0\">";
        }

        if ($g->{g_sex} =~ /F/)
        {
            $ratingsListRef = \@femaleRatings;
        }
        else
        {
            $ratingsListRef = \@maleRatings;
        }

        $ratingPts = $g->{g_score} * scalar(@$ratingsListRef) / $champScore;
        $rating    = get_rating ($ratingPts, $ratingsListRef);
        $average   = sprintf "%.1f", $g->{g_mean};
        $score     = sprintf "%.1f", $g->{g_score};
        $ratingPts = sprintf "%.1f", $ratingPts;
#        $comment   = "<nobr>$score -> $ratingPts</nobr>";

        print <<EOT;
           <tr>
            <td bgcolor="$white" align="right">$font2 ${i}&nbsp;&nbsp;&nbsp;$font0</td>
            <td bgcolor="$paleBrown">$font2 <a href="$STUFF_CGI?p=prospects&b=$days&bd=$g->{g_mday}&bm=$g->{g_month}&by=$g->{g_year}">$g->{g_name}</a>&nbsp;$star&nbsp;$font0</td>
            <td bgcolor="$paleBrown" align="left"> $font2 $rating  $font0</td>
            <td bgcolor="$paleBrown" align="right">$font2 $average $font0</td>
            <td bgcolor="$paleGrey" align="right">$font2 $g->{g_min} $font0</td>
            <td bgcolor="$paleGrey" align="right">$font2 $g->{g_med} $font0</td>
            <td bgcolor="$paleGrey" align="right">$font2 $g->{g_max} $font0</td>
            <td bgcolor="$paleGrey" align="right">$font2 $g->{g_today} $font0</td>
            <td bgcolor="$white">$comment</td>
           </tr>
EOT
    }

    print "</table>";

    my $prosp_size   = scalar(@prospects);
    my $bottomRating = get_prospects(0);
    my $medianRating = get_prospects($prospectsMedian);
    my $topRating    = get_prospects($prosp_size);

#    my $champName    = $champRec->{g_name};
#    my ($member, $sex, $name, $file) = split /;/, get_trooper_rec($champName);
#    $file = "$IMGTROOPDIR/$file";

    print <<EOT;
   <br clear="all"><br>
   <font face="verdana,arial,times" size="2">
    So, what's this all about, then?
    <p>
    You've probably already seen a fair few prospects tables both in
    pooclub and Stuff Today.
    You may even have explored Stuff Today's 
    <a href="$STUFF_CGI?p=stock&a=fish">stocks</a>
    pages and seen how the prospects are calculated.
    And now you're presented with yet another table of incomprehensible data
    that you're forced to make some sort of sense of.
    <p>
    Well, here we see some interesting stats on the group's prospects over
    the last $days days.
    Basically, we want to see what everyone's average prospects have been
    and how they compare with other members of the group, and that is
    exactly what we see in the "Average" column.
    <p>
    However, in the crazy world of statistics things are never quite as
    straightforward as they should be.
    If you've been following the prospects over time you will have seen that
    $prospectsMedian is a good pivotal point, equal numbers of prospects appear 
    above $prospectsMedian as below, and this is why this value has been
    rated as "$medianRating".
    <p>
    But this is not the average.
    <p>
    The distribution curve of prospects is not symmetrical as on a particularly
    good day you can have prospects up in the 60's or 70's, but a bad day can
    only go as low as zero.
    The added weight of a really good day pushes the average prospects up to
    around $prospectsAverage, but the Great Stocks have chosen not take this
    as the halfway point between good and bad prospects because that would give
    people more bad prospects than good ones.
    Instead, the boundary has been set at $prospectsMedian which is known in
    the world of statistics as the "median".
    <p>
    You get the median of a series of numbers by sorting the numbers in
    increasing (or decreasing, it doesn't really matter) order and picking the
    middle one.
    This guarantees that there will be just as many numbers higher than this
    value as below (well, to within plus or minus one 'cos if you've got an
    even number of numbers in your series you've got a choice of two middle ones).
    So, we decided that in the league table, we would show you the median
    (in the "Med" column) as well as the average even though this adds further
    complexity (or is it <i>because</i> it adds further complexity?).
    <p>
    Now, this gives rise to the question, which column should we use to
    determine your rating and hence your position in the league table?
    Well, despite all this talk about medians, we've opted to go with the
    average because it's what people understand (or feel they
    understand) better.
    You'll just have to understand that the halfway mark in the league table
    is around $prospectsAverage whereas in the prospects table it's around
    $prospectsMedian.
    <p>
EOT
    show_knickknack(20);

    print <<EOT;
    We're sorry about all this confusion but it's just the way statistics work
    and we don't like it any more than you do.
    To be honest, I dropped my stats courses at university at the earliest
    possible opportunity because they annoyed the nads off me.
    By the way, if you want to see the full list of prospects points which have been
    used to compile the league table data, click on your name and you'll be transported
    to another table of data on a Stuff Today page which will make things
    even clearer than they are now.
    <p>
    Thank you for your time and consideration.
   </font>
EOT

    return "$champRec->{g_name};$poostarRec->{g_name}";
}

############################################################

sub setfont
{
    my $id = shift(@_);

    if ($id eq "text")
    {
        print <<EOT;
            <font face="verdana,arial,times" size="2" color="black">
EOT
    }
}

############################################################

sub display_poo_event
{
    my $day   = shift(@_);
    my $mday  = $u_mday;
    my $month = $u_month;
    my $year  = $u_year;
    my $fname = "";
    my $events = 0;
    my $size = 2;
    my $hrwidth = "25%";
    my $align = "center";

    if ($day)  # Use arg's date instead of today's date
    {
        ($not_used1, $mday, $month, $year) = split /;/, parse_date ($day);
    }
    $fname = sprintf "%s%02d", $month, $mday;

        print <<EOT;
        <table border="0" align="$align"><tr>
         <td align="left" valign="top">
          <font face="arial" size="$size">
EOT
    if (open (EVENTFILE, "$EVENTS_FILE"))
    {
        while (<EVENTFILE>)
        {
            chop;
            ($eYear, $monthDD, $text) = split /;/, $_, 3;
            $eYear =~ s/\s+//g;

            if ($monthDD eq $fname && ($eYear eq "Y" || $eYear eq "" || $eYear eq $year))
            {
                if ($events > 0) {print "<hr width=\"$hrwidth\" align=\"center\">\n";}
                $events++;
                print "<center>$text</center>\n";
            }
        }
        close (EVENTFILE);
    }

    if (open DATEFILE, "$EVENTDIR/$fname")
    {
        if ($events > 0) {print "<hr width=\"$hrwidth\" align=\"center\">\n";}

        while (<DATEFILE>) { print $_; }
        close (DATEFILE);

    }
    print <<EOT;
          </font>
         </td>
        </tr></table>
EOT
}

############################################################

sub list_luckydays
{
    my $count = 0;
    my $days = 0;
    my $i_yyyymmdd = $u_yyyymmdd;
    my $missedit = " <font face=\"times,arial,verdana\" size=\"3\" color=\"gray\">Missed it!</font>";

    print <<EOT;
    <font face="times,arial,verdana" size="5"><i>Lucky And Unlucky Days</i></font>
    <p>
    <font face="verdana,arial,times" size="2">
EOT

    while ($count < 12 && $months < 36)
    {
#print qq(count=$count months=$months i_yyyymmdd=$i_yyyymmdd<br>);
        ($io, $id, $im, $iy) = split /;/, parse_date ($i_yyyymmdd);
        $thirteenth = substr ($i_yyyymmdd, 0, 6) . "13";
        $twentysecond = substr ($i_yyyymmdd, 0, 6) . "22";
        ($badday, $spare) = split /;/, parse_date ($thirteenth);
        ($goodday, $spare) = split /;/, parse_date ($twentysecond);
#print qq(io=$io id=$id im=$im iy=$iy thirteenth=$thirteenth twentysecond=$twentysecond badday=$badday goodday=$goodday<br>);
        if ($badday eq "Friday")
        {
            print <<EOT;
                <font face="verdana,arial,times" size="2" color="red">
                $badday 13 $im $iy</font>
EOT
            print $missedit if ($u_yyyymmdd > $thirteenth);
            print "<br>";
            $count++;
        }

        if ($goodday eq "Tuesday")
        {
            print <<EOT;
                <font face="verdana,arial,times" size="2" color="black">
                <b>$goodday 22 $im $iy</b></font>
EOT
            print $missedit if ($u_yyyymmdd > $twentysecond);
            print "<br>";
            $count++;
        }

        $i_yyyymmdd = date_manip ("-l $i_yyyymmdd"); # end of month

#print qq(end of month i_yyyymmdd=$i_yyyymmdd<br>);
        $i_yyyymmdd = date_manip ("-d1 $i_yyyymmdd"); # next month

#print qq(next month i_yyyymmdd=$i_yyyymmdd<br>);
        $months++;
    }

    print "</font>";
}

############################################################

sub next_luckyday
{
    # Find the next date on which the 22nd falls on a Tuesday

    my $count = 0;
    my $i_yyyymmdd = $u_yyyymmdd;
    my $result;

    while ($count < 1)
    {
        $iy = substr($i_yyyymmdd, 0, 4);
        $im = substr($i_yyyymmdd, 4, 2);
        $twentysecond = $iy . $im . "22";
        ($dow, $spare) = split /;/, parse_date ($twentysecond);


        if ($dow eq "Tuesday")
        {
            if ($u_yyyymmdd <= $twentysecond) # Don't increment if we've just
            {                                 # missed a lucky day this month
                $count++; 
                $result = $twentysecond;      # Found it!
            }
        }

        $i_yyyymmdd = date_calc ("-m $i_yyyymmdd"); # end of month
        $i_yyyymmdd = date_calc ("-d1 $i_yyyymmdd"); # next month
    }

    my $days_until = date_calc ("-c $u_yyyymmdd $twentysecond");
    return "${result};$days_until";
}

############################################################

sub troop_table
{
    my ($memberListRef, $fileHashRef, $locHashRef, $columns) = @_;
    $columns = 5 if ($columns eq "");
    my $count = 0;
    my $fname;
    my $location;
    my $member;

    print <<EOT;
       <table align="center" border="0" cellpadding="2" cellspacing="2">
        <tr>
EOT

    random_sort($memberListRef, $r_sec);

    for $member (@$memberListRef)
    {
        if ($count > 0 && $count % $columns == 0)
        {
            print "</tr><tr>";
        }

        $fname = "$IMGTROOPDIR/$$fileHashRef->{$member}";
        $location = "$$locHashRef->{$member}";

        print <<EOT;
         <td align="center" valign="bottom">
          <font face="verdana,arial,times" size="2">
          <div class="inlineimage">
           <img src="$fname" alt="$member" border="0">
           <div class="caption"><medium><nobr>$member</nobr></medium><br><small><nobr>$location</nobr></small></div>
          </div>
          </font>
         </td>
EOT
        $count++;
    }


    print <<EOT;
        </tr>
       </table>
       <br clear="all">
EOT
}

############################################################

sub poopage_title
{
    my $title = shift(@_);
    return if ($title eq "");

    print <<EOT;
        <font face="verdana,arial,times" size="4" color="brown">
         <b>$title</b></font><p>
EOT
}

############################################################

sub poopage_subtitle
{
    my $subtitle = shift(@_);
    return if ($subtitle eq "");

    print <<EOT;
        <font face="times,verdana,arial" size="5" color="black">$subtitle</font><p>
EOT
}

############################################################

sub show_knickknack
{
    my $seed_offset = shift(@_);
    my ($r_sec,$r_min,$r_hour,$r_mday,$r_mon,$r_year,$r_wday,$r_yday,$r_isdst)
        = localtime(time);
    my $seed = $r_sec + $r_yday + $seed_offset;
    my $knickknack = random_line ("$LISTDIR/knickknacks.list", $seed);
    $knickknack =~ s/\s+$//;
    $knickknack = "${IMGKKDIR}/kk_${knickknack}";

    print <<EOT;
        <a href="${knickknack}">
         <img src="${knickknack}" alt="Knick knack" width="80" border="0" align="center"></a>
EOT
}

############################################################
#
# called by:
# \sub show_topic_dates()
# in data/poo/pooclub_topic.html

sub zz_show_topic_dates
{
    my @args = @_;

    print qq(
   <a href="${THIS_CGI}?page=topic">
    <img src="${IMGPOODIR}/topic3.jpg" border="0" zwidth="150" align="right"
         alt="Today's Topic"></a>
    );

    for my $arg (@args)
    {
        print qq(arg="$arg"<br>);
    }

    my $yyyymmdd = $YYYYMMDD;
    my $days_ahead = 10;
    my $last_date = date_manip("-d$days_ahead $YYYYMMDD");
    my $hh = substr($HHMMSS, 0, 2);
    my $date;

    require("uber/uber_date.pl");

    # Can't post for today if it's gone mid-day
    $yyyymmdd = date_manip("-d1 $yyyymmdd") if ($hh > 11);

    while($yyyymmdd < $last_date)
    {
        $user = "";
        $fname = "${SHAREDROOT}/topics/scheduled_${yyyymmdd}.txt";
        $exists = 0;
        open(TOPIC, "$fname");
        if ($user = <TOPIC>)
        {
            $exists = 1;
            chop($user);
        }
        close(TOPIC);
        $yyyy = substr($yyyymmdd, 0, 4);
        $mm = substr($yyyymmdd, 4, 2) - 1;
        $dd = substr($yyyymmdd, 6, 2);
        $date = qq($dd $MonthList[$mm] $yyyy);
        ($thingy, $dow) = split / /, date_calc("-s $yyyymmdd");
        $dow = substr($dow, 0, 3);

        if (($user ne "") && ($user ne $UBERACC{'USERNAME'}))
        {
            print qq(&nbsp;&nbsp;<font color="gray">$dow $date</font> taken<br />);
        }
        else
        {
            print qq(
            <nobr>&nbsp;&nbsp;<a href="${THIS_CGI}?page=topic&d=$yyyymmdd">$dow $date</a> $user</nobr><br />
            );
        }
        $yyyymmdd = date_manip("-d1 $yyyymmdd");
    }
    print qq(<br clear="all" />);
}

############################################################
#
# called by:
# \sub show_param(date)
# in data/poo/pooclub_topicthankyou.html

sub show_param
{
    my ($name) = @_;
    my $param = $cgi->param("$name");
    print qq($param);
}

############################################################

sub roll_of_honour
{
    print qq(
            <b>Roll Of Honour</b><br /><ul>
    );
    for my $rec (read_voty_winners())
    {
        ($year, $veg) = split /;/,$rec;
        print qq(
            <li>$year $veg</li>
        ) if (! $year =~ /^\s*$/);
    }

    print qq(</ul>);
}
############################################################
#
# used by data/poo/pooclub_topichistory.html

sub show_topic_history
{
    my @history;
    my $date;
    my $yyyymmdd;
    my $subject;
    my $user;
    my $message;

#print qq(fishy<br>);
    # show selected historical topic

    my $f = $cgi->param('f');
#print qq(f=$f<br>);
    my $fid = basename("$f");
#print qq(fid=$fid<br>);
    my $fname = make_topic_filename("$fid", "posted");
#print qq(fname=$fname<br>);

    if (-e $fname) # file exists so display its contents
    {

        $found = read_topic_file(\$user, \$subject, \$message, $fname);
        if ($found ne "")
        {
            my $posted = $cgi->param('d');
#            $message =~ s/\n/FISH/;
            print qq(<p />
                Posted: $posted<br />
                Subject: <b>$subject</b><br />
     <form method="POST" 
           action="$THIS_SCRIPT" 
           enctype="application/x-www-form-urlencoded">

      <table class="basic_c" style="width: 400;">
       <tr><td class="basic">
<textarea name="message" cols="60" rows="20">$message</textarea>
</td></tr></table>
                </form>
            );
        }
    }

    # read topics from history of posted topics
    my $history_file = "${SHAREDROOT}/topics/topic_history.dat";
    open (HISTORY, "$history_file") or log_error("Cannot read topic history file: $history_file");
#log_info("FISH Read topic history file: $history_file");
    while(<HISTORY>)
    {
        chop;
#log_info("FISH line=$_");
        push @history, $_;
    }
    close(HISTORY);

    print qq(
<div align="right">Back to <a href="${THIS_CGI}?p=topic">Today's Topic</a> page</div>
<p /><table align="left">);

    for my $record (reverse @history)
    {

#log_info("FISH record=$record");
        ($yyyymmdd, $subject, $user, $fname) = split /;/, $record;

#log_info("FISH yyyymmdd=$yyyymmdd subject=$subject user=$user fname=$fname");
        next if ($yyyymmdd =~ /^\s*$/);
        $date = date_manip("-fDOW_DD_MON_YYYY $yyyymmdd");

#log_info("FISH date=$date");
        ($dow, $dd, $mon, $year) = split / /, $date;
#        $subject =~ s/^\s*Today.*s\s*Topic//i;
#        $subject =~ s/^\W+//;
#log_info("FISH dow=$dow dd=$dd mon=$mon year=$year");
        $subject =~ s/Theme:/<b>Theme:<\/b>/;
        print  qq(
              <tr>
               <a name="$yyyymmdd" />
               <td class="itemText" style="text-align: left">$dow</td>
               <td class="itemText" style="text-align: right">$dd</td>
               <td class="itemText" style="text-align: left">$mon</td>
               <td class="itemText" style="text-align: right">$year</td>
               <td class="itemText">
                <a href="${THIS_CGI}?p=topichistory&f=$fname&d=$date">$subject</a>
               </td>
              </tr>
        );
    }
    print qq(</table><br clear="all" />);
}


############################################################

sub show_bookmarks
{
    my $p = $cgi->param('p');
    my $c = $cgi->param('c');
    my $i = $cgi->param('i');
    my $url = "http://shite.org/cgi-bin/pooclub.cgi?p=$p&c=$c&i=$i";

    my @headerList = (
      "Go on, give us a write up",
      "Report us - see if we care",
      "Read it? Spread it",
      "You can't make this stuff up, you know",
      "Tell your friends.  Better still, tell your enemies.",
      "Infest the world with pooclub!",
      "Pretend you enjoyed this page... please..."
    );

    my $ran = random_seed() % scalar(@headerList);
    my $header = $headerList[$ran];

    my @siteList = (
      "stumbleupon;Stumbleupon;bookmark_stumbleupon.jpg;http://www.stumbleupon.com/submit?url=;",
      "delicious;Delicious;bookmark_delicious.gif;http://del.icio.us/post?url=;",
      "reddit;Reddit;bookmark_reddit.png;http://reddit.com/submit?url=;",
      "digg;Digg;bookmark_digg.jpg;http://digg.com/remote-submit?phase=2&url=",
      "facebook;Facebook;bookmark_facebook.png;http://www.facebook.com/sharer.php?u=;",
      "twitter;Twatter;bookmark_twitter.jpg;http://www.twitter.com/?status=",
      "bollocks;Bollocks;bookmark_bollocks.png;?p=bollocks&url="
    );
    my @randomSiteList = (
      "worldface;WorldFace;bookmark_worldface.jpg;http://www.teamfishcake.co.uk/worldface;",
      "sproutlore;The Brentford Mercury;bookmark_sproutlore.png;http://www.sproutlore.com?ref=;",
      "scientology;Church of Scientology;bookmark_scientology.jpg;http://www.teamfishcake.co.uk/submit?url=;",
      "police;London Metropolitan Police;bookmark_police.jpg;http://www.teamfishcake.co.uk/submit?url=;"
    );
    $ran = random_seed() % scalar(@randomSiteList);
    push @siteList, $randomSiteList[$ran]; # add one random site to list

    print qq(
<hr>
<div style="font-weight: bold; font-size: 11px">$header</div>
<table border="0"><tr>
    );

    my $count = 0;
    for my $site (@siteList)
    {
        ($id, $title, $img, $link) = split /;/, $site;
        print qq(
 <td style="text-align: left; vertical-align: top; font-size: 11px">
  <a id="$id" title="Post this page to $title" 
     href="$link$url">
   <img src="${IMGPOODIR}/$img" height="20" border="0"/> 
  </a>
 </td>
 <td style="text-align: left; vertical-align: bottom; font-size: 11px">
  <a id="$id" title="Post this page to $title" 
     href="$link$url">
   $title
  </a>
 </td>
        );

        $count++;
        print qq(</tr><tr>) if (($count % 4) == 0); # new line every fourth item
    }



    print qq(
     </tr></table>
    );
}

############################################################

sub show_drivel
{
  my ($yyyymmdd) = @_;
  $yyyymmdd = $YYYYMMDD if (($yyyymmdd =~ /^\s*$/) || ($yyyymmdd eq "drivel"));
  my $drivel_file = "${SHAREDROOT}/drivel/drivel_${yyyymmdd}.txt";
log_info("show_drivel(yyyymmdd=$yyyymmdd) drivel_file=$drivel_file");

  print qq(<pre>);
  open (DRIVEL, "$drivel_file") or print qq(No drivel for $yyyymmdd YYYYMMDD=$YYYYMMDD drivel_file=$drivel_file);
  while(<DRIVEL>)
  {
    print $_;
  }
  close(DRIVEL);
  print qq(</pre>);
}

############################################################

sub show_drivel_history
{
    my $h_yyyymmdd = $cgi->param('h_yyyymmdd');

    if ($h_yyyymmdd)
    {
        show_drivel($h_yyyymmdd);
    }
    else
    {
        print qq(
Now, we know you're not going to want to wade through a
load of archaic drivel, but this page exists solely
to provide links to lots of content for search
engine optimisation purposes.
<p>
Yes, shameless, aren't we?
<p>
        );
    }

    for my $fname (reverse <${SHAREDROOT}/drivel/drivel_*.txt>) # glob
    {
        $bname = basename($fname);
        $bname =~ s/.txt$//;
        ($drivel, $yyyymmdd, $others) = split /_/, $bname;
        $yyyy = substr($yyyymmdd, 0, 4);
        $mm = substr($yyyymmdd, 4, 2);
        $dd = substr($yyyymmdd, 6, 2);
    
        $date = date_manip("-fDOW_DD_MON_YEAR $yyyymmdd");
        print qq(<a href="${THIS_CGI}?p=drivel_history&h_yyyymmdd=$yyyymmdd">$date</a><br>);
    }
}

############################################################

sub show_next_drivel_dates
{
    my $yyyymmdd;
    my $showcat;
    my $start_yyyymmdd = date_manip("-d1 $YYYYMMDD");
    my @categoryList = ("Event", "Forecast", "News", "Word-Of-The-Day",
        "Rogers-Profanisaurus", "Viz-Top-Tips",
        "Quote", "Joke", "Thought", "Zen", "Did-You-Know...", "Poem-Of-The-Day");

    print qq(
        <table align="center">
         <tr>
          <td style="font-size: 10px; font-weight: bold">
           Next available dates
          </td>
         </tr>
    );
    for my $category (@categoryList)
    {
        $yyyymmdd = find_next_available_drip_date($category);
        $yyyy = substr($yyyymmdd, 0, 4);
        $mm = substr($yyyymmdd, 4, 2);
        $dd = substr($yyyymmdd, 6, 2);
        $date = date_manip("-fDOW_DD_MON_YEAR $yyyymmdd");
        $showcat = $category;
        $showcat =~ s/-/ /g;

        print qq(
         <tr>
          <td style="font-size: 10px">
            $date
          </td>
          <td style="font-size: 10px">
           <a href=${THIS_CGI}?page=add_drivel&category=$category&yyyy_drivel=$yyyy&mm_drivel=$mm&dd_drivel=$dd>
            $showcat
           </a>
          </td>
          <td style="font-size: 10px">
           &nbsp;
          </td>
         </tr>
        );    
    }
    print qq(</table>);
}

############################################################

sub show_pending_drivel
{
#    my $category = $cgi->param('category');
#    my $dd = $cgi->param('dd_drivel');
#    my $mm = $cgi->param('mm_drivel');
#    my $yyyy = $cgi->param('yyyy_drivel');
#    my $yyyymmdd = sprintf "%04d%02d%02d", $yyyy, $mm, $dd;
#    my $print_date = date_manip("-fDAYOFWEEK_DD_MONTH_YEAR $yyyymmdd");
#    my $fname = "${SHAREDROOT}/drivel/${yyyymmdd}_${category}_$UBERACC{'USERNAME'}.txt";
#    my $disabled="";
#    my $text = "";

#    open (INF, "$fname");
#    while(<INF>) {$text .= $_;}
#    close(INF);

    my $opt = $cgi->param('o');
    my $me = $UBERACC{'USERNAME'};
    my $showuser;

    if ($UBERACC{'PRIVILEGE'} < 1) # not logged in
    {
        $disabled = qq(disabled="disabled");
        print qq(
         <p />
         <table style="text-align: center">
          <tr><td style="font-size: 10px; color: red">
           You need to be logged in to see your drivel.
          </td></tr>
          <tr><td>
           <a href="${THIS_SCRIPT}?page=login">Login here</a>
          </td></tr>
         </table>
        );

        return;
    }

    # links to 'all drivel' or 'just mine' options
    my $all_link = qq(<b>all drivel</b>);
    my $my_link = qq(<b>just mine</b>);
    if ($opt eq "all")
    {
        $my_link = qq(<a href="${THIS_SCRIPT}?p=drivel_pending&o=mine">just mine</a>);
    }
    else
    {
        $all_link = qq(<a href="${THIS_SCRIPT}?p=drivel_pending&o=all">all drivel</a>);
    }
    print qq(<p>$all_link | $my_link</p>);

    print qq(<table>);
    my $prevdate;
    my $showdate;
    my $showtext;
    my $text;
    my $count = 0;

    for my $fname (<${SHAREDROOT}/drips/*.txt>) # glob
    {
        $bname = basename($fname);
        $bname =~ s/.txt$//;
        ($yyyymmdd, $category, $user) = split /_/, $bname;
        $yyyy = substr($yyyymmdd, 0, 4);
        $mm = substr($yyyymmdd, 4, 2);
        $dd = substr($yyyymmdd, 6, 2);
        $showcat = $category;
        $showcat =~ s/-/ /g;
        $showcat =~ s/Of The Day//;
        $date = date_manip("-fDOW_DD_MON_YEAR $yyyymmdd");
        $showdate = "";
        $showdate = $date if ($date ne $prevdate);
        $text = "";

        if (($user eq $me) || ($UBERACC{'PRIVILEGE'} > 2))
        {
#            $date = qq(<a href=${THIS_CGI}?page=add_drivel&category=$category&yyyy_drivel=$yyyy&mm_drivel=$mm&dd_drivel=$dd>$date</a>);
            $showcat = qq(<a href=${THIS_CGI}?page=add_drivel&category=$category&yyyy_drivel=$yyyy&mm_drivel=$mm&dd_drivel=$dd>$showcat</a>);
            $showuser = qq([$user]);
            open (INF, $fname);
            $text = <INF>;
            close(INF);
        }
        else
        {
            $showuser = qq([]);
        }

        next if (($user ne $me) && ($opt ne "all")); # show only my drips
        $showtext = substr($text, 0, 32);

        print qq(
         <tr>
          <td style="font-size: 10px; vertical-align: top">
           <nobr>$showdate</nobr>
          </td>
          <td style="font-size: 10px; vertical-align: top">
           <nobr>$showcat</nobr>
          </td>
          <td style="font-size: 10px; vertical-align: top">
           <nobr>$showuser</nobr>
          </td>
          <td style="font-size: 10px; vertical-align: top">
           $showtext
          </td>
         </tr>
        );
        $prevdate = $date;
        $count++;
    }

    print qq(
        </table>
    );

    print qq(<p>$all_link | $my_link</p>) if ($count > 6);
}

############################################################
1;

############################################################
# END OF FILE
############################################################
