############################################################
#
# stuff_pages.pl
#
# Functions which are responsible for drawing an entire page
#
############################################################

require "stuff_html.pl";

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

sub write_help
{
    my $help_id = shift(@_);
    my $help_file = "$HELPDIR/welcome.txt";
    $help_file = "$HELPDIR/$help_id.txt" if ($help_id);
    my $helpTitle = "Welcome";

    stuff_title("Help Pages", "What's this nonsense all about, then?");

    print <<EOT;
        <table align="left" width="600" border="0"
               cellpadding="10" cellspacing="0"><tr>
         <td align="left" valign="top" width="150">
          <font face="verdana,arial" size="5" color="$colour[0]">
           Help Pages
          </font>
          <font face="verdana,arial" size="2"><br><br>
EOT
    open (HELPINDEX, "$HELP_FILE") || print "No index";
    while (<HELPINDEX>)
    {
        chop;
        next if (/^\s*$/);  # ignore blank lines
        next if (/^#/);     # ignore commented lines
        ($display, $idx, $name) = split /;/, $_;
        print "<a href=\"$THIS_CGI?p=help&a=$idx&$date_args\">$name</a><br>\n" if ($display eq "Y");
        $helpTitle = $name if ($idx eq $help_id);
    }
    close (HELPINDEX);

    print <<EOT;
          </font>
         </td>
         <td align="left" valign="top" width="450">
          <font face="verdana,arial" size="5" color="black">$helpTitle</font>
          <p>
          <font face="verdana,arial" size="2">
EOT

    open (HELPFILE, "$help_file") || print "No file: $help_file";

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

    if ($help_id eq "poem")
    {
        print "<p>\n";
        poems_opinion("todayspoem");
    }

    print "</font></td></tr></table>\n";

    user_form;
}

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

sub write_home

# Stuff Today Home Page
{
    my $prosp_size = scalar(@prospects);
    my $total = 0;
    my $primary = $greatList[0];

    stuff_title("Home Page");

    print <<EOT;
    <br clear="all">
    <table align="left" width="750" border="0"
           cellpadding="0" cellspacing="0"
       style="background-color: white;" />
     <tr>
      <td align="left" valign="top" width="180">
    
       <!---- Left Column --->
EOT
        stocks_menu();
    
        print <<EOT;
        <img src="$IMGDIR1/blort.gif" border="0"
             alt="Blort">
       </td>

       <!---- Middle Column --->
       <td align="left" valign="top" width="400">
EOT

    if ($b_yyyymmdd == 0)  # No DoB so ask user for it
    {
        print "<font face=\"verdana,arial\" size=\"2\" color=\"$colour[1]\"><b>";
        print "Enter your date of birth and find out ";
        print "what your prospects for the day <nobr>are . . .</nobr>";
        print "</b></font>";
    }
    else   # User has given DoB so show prospects
    {
        $total = calc_total ($b_yyyymmdd);
        show_stock_days;

        if ($total >= $prosp_size - 1) { $prosp = $prospects[$prosp_size - 1]; }
        else { $prosp = $prospects[$total]; }
        print <<EOT;
        <center>
          <font face="verdana,arial" size="2">Your   
           <a href="$THIS_CGI?p=prospects&$date_args">prospects</a>
            today are</font><br>
          <font face="times,verdana,arial" size="6" color=$colour[1]>
           <b><i>$prosp</i></b></font>
        </center>
EOT
    }

    user_form();

    print <<EOT;
    <table border="0" align="center" width="380" cellpadding="2" cellspacing="0">
     <tr>
      <td align="left" valign="top" colspan="2">
EOT
    shite_space();
    print <<EOT;
      </td>
     </tr>
     <tr>
      <td align="left" valign="top" width="190">
EOT
    thecull();
#    arseenders();
    print <<EOT;
      </td>
      <td align="left" valign="top" width="190">
EOT
    poo_club();
    print <<EOT;
      </td>
     </tr>
    </table>
    <br clear="all">
EOT

    quotation();
    new_word();
    passage();

print <<EOT;
  </td>
  <td align="left" valign="top" width="170">

   <!---- Right Column --->
EOT

    show_event ($u_yyyymmdd, 2, "50%", "left");

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

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

sub write_invalid_date
{
    my $error_type = shift(@_);
    my $message = "<b>Invalid Date!</b><p>";

    if ($error_type eq "birth")
    {
        $message .= "You have entered an invalid Date of Birth";
    }
    elsif ($error_type eq "todays")
    {
        $message .= "You have entered an invalid Today's Date";
    }
    elsif ($error_type eq "order")
    {
        $message .= "Date Of Birth must not be later than Today's Date.<br>";
        $message .= "Today's Date must not be in the future.";
    }

    print <<EOT;

   <!---- Invalid Date --->
   <table border="0" align="center"><tr>
    <td align="left">
     <font face="verdana,arial" size="2">
      $message
     </font>
    </td>
   </tr></table>
EOT

    user_form;
}

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

sub write_poem   # Show today's poem on a Stuff Today page
{
    my $colour0 = "brown";
    my $colour1 = "#f0f0d0";

    $colour0 = $colour[0];
    $colour1 = "white";
    stuff_title("Poem Of The Day", "Kindly donated by Shite Space");
    
    print <<EOT;

   <!---- Today's Poem --->
   <table align="left" border="0" cellspacing="20" cellpadding="0" width="800"
       style="background-color: white;" />
    <tr>
     <td align="left" valign="top" bgcolor="$colour1" width="250">
       <center>
        <font face="arial" size="2">
         <a href="$HOST/main/index.html">
          <img src="$IMGDIR1/shspace.gif"
               alt="Shite Space" border="0"></a>
         </font>
         <br clear="all">
         <font face="times,arial" size="3"><i>
          proudly presents
         </i></font>
         <p>
         <font face="times,arial" size="5"><i>
          Today's Poem
         </i></font>
         <p>
EOT

    if ($arg2 < 2)   # Display link to history page
    {
        print <<EOT;
         <font face="times" size="4" color="gray"><i>at a glance...</i></font><br>
         <font face="verdana,arial" size="2">
          Recent
          <a href="$THIS_CGI?p=todayspoem&a=$arg1&b=21&$date_args">history</a>
          of Today's Poems.
         </font>
         <p>
EOT
    }

    # Link to Shite Space
    print <<EOT;
         <hr width="60%"><br>
         <font face="arial,times" size="2">
          For more puerile gibberish enter<br>
          <nobr>the most appalling site on the net.</nobr><p>
          <a href="http://www.shite.org/main">
          <font face="times" size="5" color="black"><b>Shite</b><i>Space</i></font></a>
          <br>
          <font face="arial,courier" size="4" color="gray">
          www.shite.org</font>
         </font><p>

        <font face="arial,times,verdana" size="2">
         Or, for some top quality arsing about, visit<br>
        </font>
        <a href="$POO_CGI"><font face="verdana,arial,times" size="5" color="brown">pooclub</font></a>
EOT

    print <<EOT;
        </font>
       </center>
     </td>
     <td align="left" valign="top" width="50">
      &nbsp;
     </td>
     <td align="left" valign="top" width="500">
EOT

        if ($arg2 > 1)  # Show index of recent poems
        {
            $i_yyyymmdd = date_calc("-d-$arg2 $u_yyyymmdd");
            print <<EOT;
                <table border="0" bgcolor="$colour[3]" cellspacing="0" cellpadding="1">
EOT
            while ($i_yyyymmdd < $u_yyyymmdd)
            {
                $i_yyyymmdd = date_calc("-d1 $i_yyyymmdd");

                ($i_file, $i_title, $i_author) = split /;/, poem_of_the_day($i_yyyymmdd);
                ($io, $id, $im, $iy) = split /;/, parse_date ($i_yyyymmdd);
                $io = substr ($io, 0, 3);
                print <<EOT;
                 <tr>
                  <td align="left"><font face="arial" size="2">$io</font></td>
                  <td align="right"><font face="arial" size="2">$id</font></td>
                  <td align="left"><font face="arial" size="2">$im</font></td>
                  <td align="left"><font face="arial" size="2">&nbsp;&nbsp;
                   <a href="$THIS_CGI?p=todayspoem&a=$arg1&b=$arg2&ud=$id&um=$im&uy=$iy&bd=$b_mday&bm=$b_month&by=$b_year">$i_title</a></font></td>
                 </tr>
EOT
#                print "$io $id $im $i_title<br>\n";
            }
            print "</table><br clear=all><br>\n";
        }

        # Write Today's Poem
        print <<EOT; 
      <font face="arial" size="4" color="$colour0"><b>
       $poem_title
      </b></font>
      <br>
      <font size="2"><i>($poem_author)</i></font><br>
      <font face="arial" size="2">
EOT
        unless (open POEMFILE, "<$POEMDIR/$poem_file")
        {
            print "<br> Sorry.  Can't find today's poem.\n";
            print "<br> Please complain bitterly and send details to ";
            print "<a href=\"mailto:todayspoem\@shite.org\">todayspoem\@shite.org</a>.\n";
            print "<br> (file: $poem_file)\n";
        }
        while (<POEMFILE>) { print "<br> $_"; }
        close (POEMFILE);

        print "<br><hr align=\"left\" width=\"50%\">";

        poems_opinion("help");
        user_form();
 
        print <<EOT;
      </font>
     </td>
    </tr>
   </table>
EOT
}


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

sub write_whatisshite
{
    print <<EOT;
<html>
<head>
  <title>Shite Space: What Is Shite?</title>
  <meta name="Author" content="Mike Stolery">
  <meta name="Description" content="About The Poem Of The Day">
  <meta name="Keywords" content="mike two2sheds, kate 36c, horse, melon,
              shiterati, poetry, poems, humour, humor, literature">
</head>

<body bgcolor="white" text="black" link="brown" vlink="#403020">

<table width="500" border="0" cellspacing="0" cellpadding="10">
 <!---------------- First row (header) ------>
 <tr>
  <td align="left" valign="top" bgcolor="#ffffc0" width="100">
   <font face="times" size="4">
    <a href="../main/index.html"><b>Shite</b><i>Space</i></a>
   </font>
  </td>
  <td align="left" valign="top" width="20">
   &nbsp; 
  </td>
  <td align="left" valign="top" width="380">
   <font face="times,arial,verdana" size="6"><i>What Is Shite?</i></font>


  </td>
 </tr>

 <!---------------- First Recipe ------------>
 <tr>
  <td align="left" valign="middle" bgcolor="#ffffc0" width="100">
   <font face="arial,times,verdana" size="2">
*<font face="times" size="2" color="gray"><i>OK, they haven't yet, but they MIGHT be one day.</i></font>
    &nbsp;
   </font>    
  </td>
  <td align="left" valign="top" width="20">
   &nbsp; 
  </td>
  <td align="left" valign="top" width="380">
    <font face="arial,verdana,arial" size="4" color="brown">
     <b>The Poem Of The Day</b>
    </font>
    <font face="verdana,arial" size="3" color="black">
     <b> </b>
    </font>
   <p>
   <font face="verdana,arial" size="2">
Each day, Shite Space chooses from its vast repository
of Shite poetry the poem that it feels is most appropriate
to that day.  But what exactly IS Shite poetry?  What is it
that differentiates Shite from all other forms of poetry?
<p>
Many essays have been written on precisely this subject*
and many varied criteria have been put forward for defining
what constitutes Shite.
And although many of these ideas may have a certain degree
of validity, we shall dismiss them all here and reveal the
one overriding factor that separates Shite from all other
forms of poetry.
<p>
What truly distinguishes Shite is that it is not so much
important what the reader thinks of the poem, more what
the poem thinks of the reader.
Naturally, readers are still welcome to express their
opinions of the poem but at the end of the day such
criticisms are valueless and ineffectual.  We're still
going to keep churning out this stuff regardless of what
people think.
<p>
So, you're doubtlessly wondering how do you find out what
Today's Poem thinks of you.
Are you worthy enough even to read the poem?
Well, to find out, all you have to do is enter your date
of birth.
Shite Space is able to determine a great deal of information
from the very day you were born, and it is this information
that Today's Poem uses to determine its opinion of you.
<p>
And don't worry if Today's Poem doesn't like you very much,
come back the next day and find out what Tomorrow's Poem thinks
of you.
<br><br>
   
<hr width="40%">
EOT

    poems_opinion("front");
    user_form();
    print <<EOT;
   </font>
  </td>
 </tr>
</table>
<br clear="all">

EOT

    shitespace_menubar();
}

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

sub write_stock

# Displays properties of a specific stock
{
    $arg1 = $greatList[0] if $arg1 eq ""; # default to primary stock
    my $s = $stockHash->{$arg1};  # stock reference
    my $stock_id = "";
    my $stock_file = $s->{filename};
    my $stock_name = $s->{singular};
    my $The = $s->{article};
    my $the = lc($The);
    my $todays_stock = random_line ("$LISTDIR/$stock_file", $date_seed);
    my $birth_stock = "";
    my $points = 0;
    my $font0 = "</font>";
    my $font1 = "<font face=verdana,arial size=1 color=gray>";
    my $font2 = "<font face=verdana,arial size=2>";
    my $position = 0; # Place in stock list.
    my $bonus = 0;
    my $isGreat = isInList ($arg1, \@greatList);

    stuff_title("$stock_name Page", "Interesting facts about this fascinating stock");

    for $stock_id (@greatList)
    {
        last if ($stock_id eq $s->{id});
        $position++;
    }

    if ($age >= 0)
    {
        $birth_stock = random_line ("$LISTDIR/$stock_file", $birth_seed);
        $bs = "<p>Your birth $stock_name is $the <br>";
        $bs .= "<font face=verdana,arial size=4><b>$birth_stock</b></font>";
        if ($birth_stock eq $todays_stock)
        {
            $sd = "<p>Congratulations! It's your $stock_name Day!";
        }
    }

    $birth_stock = "(unknown)" if ($birth_stock eq "");
    print <<EOT;

   <br> 
   <table width="800" align="left" border="0" cellspacing="0" cellpadding="0">
    <tr>
     <td align="left" valign="top">
EOT

    stocks_menu();

    print <<EOT;
     </td>
     <td align="left" valign="top">
      &nbsp;&nbsp;&nbsp;&nbsp;
     </td>
     <td align="left" valign="top">
      <!---- Today's Thingy --->
      <font face="verdana,arial" size="2">
       <center>
         Today's $stock_name is $the<br>
        <font face="verdana,arial" size="4"><b>$todays_stock</b></font>
         $bs $sd
       <p>

      <!---- Properties Table --->
      <table align="left" border="0" bgcolor="$colour[5]"
             cellspacing="0" cellpadding="5">
       <tr bgcolor="$colour[4]">
        <td valign="bottom" bgcolor=white>&nbsp;</td>
        <td>$font1 Today's $stock_name $font0<br>
            $font2 <b>$todays_stock </b>$font0</td>
        <td>$font1 Birth $stock_name $font0<br>
            $font2 <b>$birth_stock </b>$font0</td>
       </tr>
EOT
    for $prop (@propList)
    {
        $prop_name = $propHash->{$prop}->{property};
        $todays_prop = favourite ($todays_stock, $prop);
        $birth_prop  = favourite ($birth_stock, $prop);
        $birth_prop = "N/A" if ($birth_stock eq "(unknown)");

        if ($todays_prop eq $birth_prop)
        {
            $points++;
            $bgcol = " bgcolor=$colour[3]";
        }
        else
        {
            $bgcol = "";
        }

        print "<tr>";
        print "<td bgcolor=white>$font2 $prop_name $font0</td>";
        print "<td $bgcol>$font2 $todays_prop $font0</td>";
        print "<td $bgcol>$font2 $birth_prop $font0</td>";
        print "</tr>";
    }

    print <<EOT;
      </table>
      <br clear="all"><br>
EOT
    $feels = $feelings[$points];
    $feels = "matches" if ($todays_stock eq $birth_stock);
    $bonus = $STOCK_BONUS - $position;
    $bonus = 1 if $bonus < 1;

    if ($bonus > 1)
    {
        print "\"$stock_name\" is today's $primaries[$position] stock. ";
        print "<nobr>(Bonus Factor: x $bonus)</nobr><p>";
    }

    if ($birth_stock eq "(unknown)")
    {
        print "Enter your Date of Birth to find out <i>your</i> birth $stock_name.";
    }
    else
    {
        $p = ($points > 0) ? "+ $points" : "$points";
        print "$The $todays_stock <i>$feels</i> $the $birth_stock. ";

        if ($isGreat)  # A Great stock contributes points to your prospects
        {
            print "<nobr>(Points: $p)</nobr>";

            $points *= $bonus;
            $p = ($points == 1) ? "point" : "points";
        
            if ($b_yyyymmdd > 0)
            {
                print <<EOT;
                      <p>
                      You have <b>$points</b> $stock_name $p towards your
                      <a href="$THIS_CGI?p=prospects&$date_args"><b>Prospects</b></a>
                      today.
EOT
            }
        }
    }

    user_form();

    print <<EOT;
       </center>
      </font>
     </td>
     <td align="left" valign="top">
      &nbsp;&nbsp;&nbsp;&nbsp;
     </td>
     <td align="left" valign="top">
EOT
    show_stock_list($arg1);   # Complete list of members of this stock

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

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

sub write_prospects
{
    my $points = 0;
    my $font0 = "</font>";
    my $font1 = "<font face=verdana,arial size=1 color=gray>";
    my $font2 = "<font face=verdana,arial size=2>";
    my $font3 = "<font face=times,verdana,arial size=3 color=gray>";
    my $points = 0;
    my $prosp = "";
    my $total = 0;
    my $bonus = 1;
    my @stockList = @greatList;
    my $prosp_size = scalar(@prospects);
    my $great_size = scalar(@greatList);
    my $greatStocksLabel = "&nbsp;";
    my $i = 0;

    stuff_title("Prospects Page", "What sort of a day can you expect today?");

    if ($arg2 eq "ALL")
    {
        push (@stockList, @lesserList);  # Want all stocks so append lesser stocks
        $greatStocksLabel = "<i> - Great Stocks</i>:";
        add_lesser_stock_days ($b_yyyymmdd);
    }

    print <<EOT;
   <!---- Today's Stock --->
   <table align="left" border="0" width="750" cellspacing="5" cellpadding="0">
    <tr>
     <td align="left" valign="top">
      <font face="arial,verdana" size="2">
EOT
    # Right hand column
    stocks_menu();

    print <<EOT;
        <br>
        <font face="times" size="4" color="gray"><i>at a glance...</i></font><br>
        <font face="verdana,arial" size="2">
         Recent
         <a href="$THIS_CGI?p=prospects&a=$arg1&b=14&$date_args">history</a>
         of your Prospects.
EOT
    if ($arg2 ne "ALL")
    {
        print <<EOT;
         <br>Show
         <a href="$THIS_CGI?p=prospects&a=$arg1&b=ALL&$date_args">all</a>
         your Stocks today.
EOT
    }
    print <<EOT;
        </font>
<!-----
        <br><br><br>
        <a href="$THIS_CGI?p=todayspoem&$date_args">Today's Poem</a><br>
----->
EOT
#    poems_opinion("help", "black", 180);
    print <<EOT;
      </font>
     </td>
     <td align="left" valign="top">
      <font face="verdana,arial" size="2">
       <center>
        <font face="verdana,arial" size="3">Your prospects today</font><br>
EOT

    if ($age >= 0)
    {
        $total = calc_total ($b_yyyymmdd);

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

        print <<EOT;
            <font face="verdana,arial" size="3">are</font><br>
            <font face="times,verdana" size="6" color="$colour[1]">
                <b><i>$prosp</i></b></font><br>
EOT
    }
    print "<br>";

    show_stock_days();
    print <<EOT;

      <!---- Stock Table --->
      <table align="left" border="0" bgcolor="$colour[5]"
             cellspacing="0" cellpadding="5">
       <tr bgcolor="$colour[4]">
        <td valign="bottom" bgcolor="white">$font2 $greatStocksLabel $font0</td>
        <td>$font2 <b>Today's Stock </b>$font0</td>
        <td>$font2 &nbsp; $font0</td>
        <td>$font2 <b>Birth Stock </b>$font0</td>
        <td bgcolor="white">$font2 &nbsp; $font0</td>
        <td bgcolor="white">$font2 &nbsp; $font0</td>
       </tr>
EOT
    $bonus = $STOCK_BONUS;

    for $stock (@stockList)
    {
        $stock_name = $stockHash->{$stock}->{singular};
        $stock_file = $stockHash->{$stock}->{filename};
        $todays_stock = random_line ("$LISTDIR/$stock_file", $date_seed);
        $b = "";

        if ($age >= 0)  # Date of birth has been set by user
        {
            $birth_stock = random_line ("$LISTDIR/$stock_file", $birth_seed);
            $points = calc_feelings ($todays_stock, $birth_stock);
            $p = $points;
            $p = "+$p" if ($points > 0);
            $b = " x&nbsp;$bonus" if ($bonus > 1 || $arg2 eq "ALL");
        }
        else   # No date of birth provided
        {
            $birth_stock = "(unknown)";
            $points = -1;
            $p = "";
        }

        if ($points < 0) { $feels = "&nbsp;"; }
        else
        {
            if ($todays_stock eq $birth_stock)
            {
                $feels = "matches";
                $bgcol = " bgcolor=$colour[3]";
            }
            else
            {
                $feels = $feelings[$points];
                $bgcol = "";
            }
        }
        if ($arg2 eq "ALL" && $i == $great_size)  # Boundary between Great and Lesser stocks
        {
            $bgcol        = "bgcolor=white";
            $stock_name   = "<br><i> - Lesser Stocks</i>:";
            $todays_stock = "&nbsp;";
            $feels        = "&nbsp;";
            $birth_stock  = "&nbsp;";
            $p = "&nbsp;";
            $b = "&nbsp;";
        }

        print "<tr>";
        print "<td bgcolor=white>$font2 $stock_name $font0</td>";
        print "<td $bgcol>$font2 $todays_stock $font0</td>";
        print "<td $bgcol>$font2 <i>$feels</i> $font0</td>";
        print "<td $bgcol>$font2 $birth_stock $font0</td>";
        print "<td bgcolor=white align=right>$font2 $p $font0</td>";
        print "<td bgcolor=white align=right>$font2 $b $font0</td>";
        print "</tr>";

        $bonus--;
        $bonus = 1 if ($bonus < 1);
        $bonus = 0 if ($i >= $great_size-1);
        $i++;
    }

    if ($age >= 0)  # Display total at bottom of table
    {
        print <<EOT;
           <tr bgcolor="white">
            <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
            <td align="right">$font2 Total: $font0</td>
            <td align=right>$font2 $total $font0</td>
            <td>&nbsp;</td>
           </tr>
EOT
    }
    print <<EOT;
      </table>
      <br clear="all">
EOT
    if ($age < 0)
    {
        print "<br>Enter your Date of Birth to find out <i>your</i> birth stocks<br>and prospects for today!";
    }
    else
    {
        goodday();
        print "<br clear=all>";
    }

    user_form();

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

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

sub write_prospects_history
{
    if ($arg2 < 2 || $b_yyyymmdd < 1)
    {
        write_prospects();
    }
    else
    {
        stuff_title("Prospects History Page", "What's your life been like recently?");
        $total = 0;
        $date_seed = date_calc("-d-$arg2 $u_yyyymmdd");

        print <<EOT;
         <br clear="all">
         <table align="left" width="600" border="0">
          <tr>
           <td align="left" valign="top">
            <font face="arial,verdana" size="2">
EOT
          # Right hand column
          stocks_menu();
      
          print <<EOT;
            <br>
            <font face="arial,verdana" size="2">
             Back to your <a href="$THIS_CGI?p=prospects&b=0&$date_args">Prospects today</a>.
            </font>
           </td>
           <td align="center" valign="top">
           <font face="verdana,arial" size="3">
            Your Prospects - Recent History<p>
            <table border="0" bgcolor="$colour[5]" cellspacing="0" cellpadding="5" align="center">
EOT
        while ($date_seed < $u_yyyymmdd)
        {
            $date_seed = date_calc("-d1 $date_seed");
            get_stock($date_seed);

            $points = calc_total($b_yyyymmdd);
            $prosp = get_prospects($points);
            $total += $points;
            ($io, $id, $im, $iy) = split /;/, parse_date ($date_seed);
            $io = substr ($io, 0, 3);
            print <<EOT;
             <tr>
              <td align="left"><font face="arial" size="2">$io</font></td>
              <td align="right"><font face="arial" size="2">$id</font></td>
              <td align="left"><font face="arial" size="2">$im</font></td>
              <td align="left"><font face="verdana,arial" size="2">&nbsp;$prosp</font></td>
              <td align="right"><font face="verdana,arial" size="2">$points</font></td>
              <td align="left" bgcolor="white"><font face="arial" size="1">&nbsp;
               <a href="$THIS_CGI?p=prospects&a=$arg1&ud=$id&um=$im&uy=$iy&bd=$b_mday&bm=$b_month&by=$b_year">view</a></font></td>
             </tr>
EOT
        }
        print "</table><br clear=all>\n";

        my $average = $total / $arg2;
        my $av = sprintf "Average over last %d days: %.1f", $arg2, $average;
        my $rating = "<nobr>Overall Rating: ";
        my $ONE_STAR = 15; # The rating you've got to achieve to get one star

        if ($average < $ONE_STAR)  # Any less than this and you get a black blob
        {
            $rating .= "<img src=\"$IMGDIR2/blackblob.gif\" alt=\"$av\">";
        }
        else
        {
            for ($i = $ONE_STAR; $i <= $average; $i++)  # More stars for higher ratings
            {
                $rating .= "<img src=\"$IMGDIR2/star.gif\" alt=\"$av\">";
            }
        }
        $rating .= "</nobr>";

        print <<EOT;
        <font face="verdana,arial" color="black" size="2"> $rating </font><p>
        <font face="verdana,arial" color="gray" size="2"> $av </font>
        </center>
EOT

    user_form();

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

    }
}

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

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 write_forth # Show forthcoming k-days and birthdays
{               # within a group
    my $record;
    my $daysOld = 0;
    my @memberList = ();
    my $file  = "$GROUPDIR/$groupHash->{$arg1}->{filename}";
    my $groupName  = "$groupHash->{$arg1}->{groupname}";
    my $f = "<font face=\"arial\" size=\"2\">";
    my $fe = "</font>";

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

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

        $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");
            $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);

    stuff_title("K-day Group \"$groupName\"", "Forthcoming K-days and birthdays");
    print <<EOT;
        <table border="0" align="left" width="600" bgcolor="white" cellspacing="0" cellpadding="0"><tr>
         <td align="center" valign="top" width="400"><br>
          <font face="verdana,arial" size="2">Group Occasions<p></font>
          <font face="verdana,arial" size="5">$groupName</font>
         </td>
         <td align="right" width="200">
          &nbsp;
         </td>
        </tr></table>
        <br clear="all">

        <table border="0" align="left" bgcolor="white">
         <tr>
          <td align="left" valign="top">
           <table border="0" align="left" cellspacing="0" cellpadding="4" bgcolor="$colour[5]">
            <tr bgcolor="white">
             <td align="center" colspan="4">
              <font face="times" size="4"><i>Forthcoming K-days</i></font>
             </td>
            </td>
            <tr bgcolor="$colour[4]">
             <td align="center">$f <b>When$fe</b></td>
             <td align="left">$f <b>Who$fe</b></td>
             <td align="left">$f <b>Will Be</b>$fe</td>
             <td align="center">$f <b>In</b>$fe</td>
            </tr>
EOT
    for $rec (sort sortbydaysToKday @memberList)
    {
        @parse = split /;/, parse_date $rec->{kday_yyyymmdd};
        $kday = sprintf "%s %2d %s %d", substr($parse[0], 0, 3),
                                        $parse[1],
                                        substr($parse[2], 0, 3),
                                        $parse[3];
        print <<EOT;
           <tr>
            <td align="right">$f$kday$fe</td>
            <td align="left">$f$rec->{name}$fe</td>
            <td align="right">$f$rec->{nextKday},000$fe</td>
            <td align="right">$f$rec->{daysToKday} days$fe</td>
           </tr>
EOT
#        print "$kday $rec->{name} $rec->{nextKday},000 $rec->{daysToKday} days ";
#        print "($rec->{dob_yyyymmdd} $rec->{daysOld} $rec->{kday_yyyymmdd})";
#        print "\n";
    }
    print <<EOT;
           </table>
          </td>
          <td width="40">&nbsp;</td>
          <td align="left">
           <table border="0" align="left" cellspacing="0" cellpadding="4" bgcolor="$colour[5]">
            <tr bgcolor="white">
             <td align="center" colspan="4">
              <font face="times" size="4"><i>Forthcoming Birthdays</i></font>
             </td>
            </td>
            <tr bgcolor="$colour[4]">
             <td align="center">$f <b>When$fe</b></td>
             <td align="left">$f <b>Who$fe</b></td>
             <td align="left">$f <b>Will Be</b>$fe</td>
             <td align="center">$f <b>In</b>$fe</td>
            </tr>
EOT

    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 %d", substr($parse[0], 0, 3),
                                        $parse[1],
                                        substr($parse[2], 0, 3),
                                        $parse[3];
        $daysToBday = date_calc("-c $u_yyyymmdd $rec->{bday_yyyymmdd}");
        print <<EOT;
           <tr>
            <td align="right">$f$bday$fe</td>
            <td align="left">$f$rec->{name}$fe</td>
            <td align="right">$f$rec->{age} years$fe</td>
            <td align="right">$f$daysToBday days$fe</td>
           </tr>
EOT
#        print "$rec->{name} $bdaymdd} $bday $daysToBday days";
#        print "($rec->{dob_yyyymmdd})";
#        print "\n";
    }

    print <<EOT;
           </table>
          </td>
         </tr>
        </table>
        <br clear="all"><br>
        <center>
         <font face="verdana,arial">View Group's
          <a href="$THIS_CGI?p=group&a=$arg1&$date_args">Prospects</a>
         </font>
        </center>
EOT
    user_form();
}

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

sub write_group  # Show a group's prospects
{
    my $record;
    my $pros = "";
    my @memberList = ();
    my @specialList = ();
    my $file  = "$GROUPDIR/$groupHash->{$arg1}->{filename}";
    my $name  = "$groupHash->{$arg1}->{groupname}";
    my $font0 = "</font>";
    my $font1 = "<font face=verdana,arial size=1 color=gray>";
    my $font2 = "<font face=verdana,arial size=2>";
    my $about = "";

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

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

        if (/^\!/)         # read "about" information
        {
            s/^\!//;
            $about .= " $_";
            next;
        }
    
        $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);

    stuff_title("Prospects Group \"$name\"", "Compare your prospects with your friends");

    print <<EOT;
   <!---- Group Prospects --->
   <br clear="all">
   <table align="left" border="0" cellspacing="5" cellpadding="0">
    <tr>
     <td align="left" valign="top">
        <font face="verdana,arial" size="2">
EOT
    # Right hand column
    stocks_menu();
                       # links
    print <<EOT;
      <font face="verdana,arial" size="3">
       View group's recent <br>prospects
       <a href="$THIS_CGI?p=group&a=$arg1&b=10&$date_args">winners</a>.
       <p>
       View group's
       <a href="$THIS_CGI?p=forth&a=$arg1&$date_args">forthcoming</a><br>
       k-days and birthdays
      </font>
     </td>
     <td align="left" valign="top">
      <font face="verdana,arial" size="2">
       <center>Group Prospects<p>
        <font face="verdana,arial" size="5">$name</font>
       </center><br><br>

      <!---- Group Table --->
      <table align="center" border="0" bgcolor="$colour[5]"
             cellspacing="0" cellpadding="5">
       <tr bgcolor="$colour[4]">
        <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=right>$font2 Date Of Birth $font0</td>
       </tr>
EOT
    my $i = 0;

    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});

        print <<EOT;
            <tr>
            <td bgcolor="white" align="right">$font2 $i $font0</td>
            <td>$font2 $g->{g_name} $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="right">$font1
            <a href="$THIS_CGI?$args"><nobr>$g->{g_mday} $g_mon $g->{g_year}</nobr></a>
            $font0</td>
            </tr>
EOT
    }

    my $special = "";

    for $g (sort sortbyspecial @specialList)
    {
        if ($special ne $g->{g_special})
        {
            print "<tr bgcolor=white><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
            print "<td>&nbsp;</td><td align=right valign=bottom><br>$font2 $g->{g_special} $font0</td></tr>";
        }
        $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});

        print <<EOT;
            <tr>
            <td bgcolor="white">&nbsp;</td>
            <td>$font2 <b>$g->{g_name}</b> $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="right">$font1
            <a href="$THIS_CGI?$args"><nobr>$g->{g_mday} $g_mon $g->{g_year}</nobr></a>
            $font0</td>
            </tr>
EOT

        $special = $g->{g_special};
    }

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

    if ($about ne "")  # Show information about the group
    {
        print <<EOT;
          <table border="0" width="300" align="center"><tr><td align="left">
              $about
          </td></tr></table>
          <br clear="all">
EOT
    }

    user_form();

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

}


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

sub write_group_history  # Table of daily winners of group's prospects
{
    if ($arg2 < 2)  # Need to go back more than 1 day
    {
        write_group();
    }
    else
    {
        stuff_title("Group History Page", "Who are this group's recent winners?");
        $total = 0;
        $date_seed = date_calc("-d-$arg2 $u_yyyymmdd");
        my $groupName = "$groupHash->{$arg1}->{groupname}";

        print <<EOT;
         <br>
         <table align="left" valign="top" width="600" border="0"><tr>
         <td align="left" valign="top">
          <font face="arial,verdana" size="2">
EOT
    
        # Right hand column
        stocks_menu();
    
        print <<EOT;
          <font face="verdana,arial" size="2">
           Back to today's <a href="$THIS_CGI?p=group&a=$arg1&b=0&$date_args">Group&nbsp;Prospects</a>.
          </font>
         </td>
         <td align="left" valign="top">
          &nbsp;
         </td>
          <td align="center" valign="top">
           <font face="verdana,arial" size="3">
            Group Prospects - Recent Winners<p>
            <font face="verdana,arial" size="5">$groupName</font>
            <br><br>
            <table border="0" bgcolor="$colour[5]" cellspacing="0" cellpadding="5" align="center">
EOT

        while ($date_seed < $u_yyyymmdd)   # for each day...
        {
            $topPoints = 0;
            $date_seed = date_calc("-d1 $date_seed");
            get_stock($date_seed);

            open (GROUPFILE, "$GROUPDIR/$arg1.dat");  # find group member with top points
            while (<GROUPFILE>)
            {
                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*$/)  # ignore "special" members
                {
                    $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;
                    }
                }

                $topProsp = get_prospects($topPoints);
                ($io, $id, $im, $iy) = split /;/, parse_date ($date_seed);
                $io = substr ($io, 0, 3);
            }

            print <<EOT;
             <tr>
              <td align="left" bgcolor="white"><font face="arial" size="2">$io</font></td>
              <td align="right" bgcolor="white"><font face="arial" size="2">$id</font></td>
              <td align="left" bgcolor="white"><font face="arial" size="2">$im</font></td>
              <td align="left"><font face="verdana,arial" size="2">&nbsp;$topName</font></td>
              <td align="left"><font face="verdana,arial" size="2">&nbsp;$topProsp</font></td>
              <td align="right"><font face="verdana,arial" size="2">$topPoints</font></td>
              <td align="left" bgcolor="white"><font face="arial" size="1">&nbsp;
               <a href="$THIS_CGI?p=prospects&a=$arg1&ud=$id&um=$im&uy=$iy&bd=$topMday&bm=$topMonth&by=$topYear">view</a></font></td>
             </tr>
EOT

        }

        print "</table><br clear=all>\n";
        user_form();
    
        print <<EOT;
           </font>
         </td>
        </tr></table>
EOT

    }
}

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

sub write_kday
{
    my $dow;
    my $zdate=$b_yyyymmdd;
    my $max_kday = $arg1;

    $max_kday = 30 if ($max_kday eq "" || $max_kday < 1);

    stuff_title("K-days Page", "All the K-days you will ever have");

    if ($b_yyyymmdd > 0)
    {
        # Do header message
        $date_diff = date_calc("-c $b_yyyymmdd $u_yyyymmdd");
    
        if ($date_diff == 0)
        {
            $message = "So, you're new born today?<br>Well, we wish you a good life.";
        }
        elsif ($date_diff < 0)
        {
            $d = 0 - $date_diff;
            $message = "Hmmm, it looks like you're due to be born ";
    
            if ($d == 1) {$message .= "tomorrow.";} else {$message .= "in $d days.";}
        }
        else
        {
            if ($date_diff > 9999)
            {
                $thousands = int($date_diff / 1000);
                $remainder = $date_diff % 1000;
                $age = sprintf "%d,%03d", $thousands, $remainder;
            }
            else
            {
                $age = $date_diff;
            }
            $message = "<b>You are $age days old.</b><p>";
            $since = $date_diff % 1000;
            $until = 1000 - $since;
            $nextkday = int($date_diff / 1000) + 1;
    
            if ($since == 0)
            {
                $message .= "Happy <i>k-day</i>!";
            }
            else
            {
                $th = nth($nextkday);
                ($nextyyyymmdd, $dow) = split / /, date_calc("-s -d$until $u_yyyymmdd");
                $y = substr($nextyyyymmdd, 0, 4);
                $m = substr($nextyyyymmdd, 4, 2);
                $d = substr($nextyyyymmdd, 6, 2);
                $mn= substr($monthList[$m - 1], 0, 3);
                $message .= "You will celebrate your ${nextkday}$th <i>k-day</i> ";
                if ($until == 1) {$message .= "tomorrow";} else {$message .= "in $until days";}
                $message .= "<br>on $dow $d $mn $y";
            }
        }
    
        # Now show table of k-days
    
        $dow = substr ($b_format, 0, 3);
    }
    else
    {
        $message = "<b>What On God's Earth Is A K-Day?</b>";
    }

    print <<EOT;
    <table border="0" width="600" align="left" cellspacing="0" cellpadding="10">
    <tr>
     <td align="center" valign="top" colspan="3" cellspacing="2" cellpadding="2">
      <font face="verdana,arial" size="3">
        k-Day Evaluator
        <p>
        $message
      </font>
     </td>
    </tr><tr>
     <td align="left" valign="top" width="350">
      <font face="verdana,arial,times" size="2">
           The birthday is a well established celebratory occasion occurring
           at regular and predictable times, i.e. once a year, on the same date
           <i>every</i> year.&nbsp;
           Another regular occasion is one which occurs every thousand days
           after your date of birth.&nbsp;
           This is known as your <i>k-day</i> and, because it occurs less
           frequently than your birthday and on different dates each time,
           it is therefore more important than a birthday.&nbsp;
           However, for some reason <i>k-days</i> are rarely commemorated.&nbsp;
           This is probably because (a) they are so damn difficult to work
           out, and (b) nobody has ever heard of them.&nbsp;
           <p>
           Well, that's all changed.&nbsp;
           You've now heard all about them and you also have a nice
           table showing the first 30 <i>k-days</i> of your life, which
           is probably about all you can expect.&nbsp;
           You should take a printout of this page and store it
           somewhere safe and get all your friends to help you
           celebrate your <i>k-day</i>s.&nbsp;
           <p>
           Maybe they'll buy you a drink!
      </font>
EOT
    user_form();

    print <<EOT;
     </td>
     <td align="left" valign="top" width="20">
      &nbsp;
     </td>
     <td align="left" valign="top" width="230">
EOT

    if ($b_yyyymmdd > 0)
    {
        print <<EOT;
      <font face="arial,times" size="2">

          <table border="0" cellpadding="3" cellspacing="0" bgcolor="$colour[6]">
           <tr>
            <td align="right" bgcolor="$colour[5]">
             <font face="arial" size="2">
              <nobr>&nbsp;&nbsp;<b>Date Of Birth:</b></nobr>
             </font>
            </td>
            <td cellpadding="10" bgcolor="$colour[5]">
             &nbsp; &nbsp;&nbsp;
            </td>
            <td bgcolor="$colour[5]">
             <font face="arial" size="2">
              <b>$dow</b>
             </font>
            </td>
            <td bgcolor="$colour[5]"><nobr>
EOT
   
        formatDate ($zdate, "b");
        print "</nobr></td></tr>\n";
       
        for ($kiloday = 1; $kiloday <= $max_kday; $kiloday++)
        {
            $res = date_calc("-s -d1000 $zdate");
            ($zdate, $dayOfWeek) = split(' ', $res);
            $dow = substr $dayOfWeek, 0, 3;
            $num = sprintf "%9d,000", $kiloday;

            print <<EOT;
                <tr><td align=right><font face="arial" size="2">$num</font></td>
                <td>&nbsp;</td>
                <td><font face="arial" size="2"> $dow </font></td><td>
EOT
    
            formatDate $zdate;
            print "</td></tr>\n";
        }
       
        print <<EOT;
            </table>
          </font>
EOT
    } # b_yyyymmdd
    else
    {
        print <<EOT;
        <center>
         <font face="times" size="5"><i>Will you be celebrating tonight?</i></font>
          <br><br><br><br>
         <font face="verdana,arial" size="4">
          Enter your Date of Birth and find out if today
          is YOUR k-day!
         </font>
        </center>
EOT
    }

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

##    user_form();
}

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

sub write_events
{
    my $day = $u_yyyymmdd;
    my $ndays = $arg1;
    $ndays = 8 if ($ndays eq "");  # Default: show next 8 days of events

    stuff_title("Events Page", "Forthcoming Events");

    print <<EOT;
        <br clear="all">
        <table border="0" width="500" bgcolor="white"
               cellspacing="5" cellpadding="5">
            <tr><td width="100" bgcolor="white">&nbsp;</td>
                <td width="400" bgcolor="white" align="center" valign="top">
                 <font face="times,arial" size="5"><i>Today's Events</i></font>
            </td></tr>
EOT
    for ($i = 0; $i < $ndays; $i++)
    {
        ($f, $fd, $fm, $fy, $fo) = split /;/, parse_date($day);
        print <<EOT;
            <tr>
             <td width="100" bgcolor="white">&nbsp;</td>
             <td width="400" align="center" valign="top" bgcolor="$colour[3]">
              <font face="arial" size=2" color="$colour[0]">$f $fd $fm</font>
            <br>
EOT
        show_event ($day, 2, "25%", "center");
        print <<EOT;
            </td></tr>
EOT

        if ($i == 0)
        {
            print <<EOT;
            <tr>
             <td width="100" bgcolor="white">&nbsp;</td>
             <td width="400" align="center" valign="top" bgcolor="white"><br>
                 <font face="times,arial" size="5"><i>Forthcoming Events</i></font>
            <br>
            </td></tr>
EOT
        }
        $day = date_calc("-d1 $day");
    }

    print <<EOT;
        </table>
EOT
    user_form();
}

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

sub write_test
{
    if ($age < 0) 
    {
        print "Need Date of Birth<br>";
        user_form;
        return;
    }

    my $points = 0;
    my $sum = $points;
    my $days = 0;
    my $average = 0;
    my $t_yyyymmdd = format_date (1, $b_month, $b_year);
    my $t_mm = substr ($t_yyyymmdd, 4, 2);

    print "<table>";

    while ($t_mm eq substr ($t_yyyymmdd, 4, 2))
    {
        $points = calc_total ($t_yyyymmdd);
        $sum += $points;
        $days++;
        print "<tr><td>User_$days</td><td>$t_yyyymmdd</td>";
        print "<td align=right>$points</td>";
        print "<td>$prospects[$points]</td></tr>\n";
        $t_yyyymmdd = date_calc ("-d1 $t_yyyymmdd");
    }

    $average = $sum / $days;
    $average = sprintf "%.1f", $average;
    print "<tr><td>&nbsp;</td><td align=right>average =";
    print "<td>$average</td><td>&nbsp;</td></tr>";
    print "</table>";
    user_form();
}

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

1;

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