#!/usr/bin/perl

############################################
###                                        ##
###         WebCal (Config Script)         ##
###           by Darryl Burgdorf           ##
###       (e-mail burgdorf@awsd.com)       ##
###                                        ##
###             version:  2.0.1            ##
###         last modified: 07/04/19        ##
###           copyright (c) 2000           ##
###                                        ##
###    latest version is available from    ##
###   http://www.norgie.net/tools/webcal/  ##
###                                        ##
#############################################

# COPYRIGHT NOTICE:
#
# Copyright 2000 Darryl C. Burgdorf.
# Copyright 2007 Stefan Lemcke
# Copyright 2011 Mike.
#
# This program is free software.  You can redistribute it and/or 
# modify it under the terms of either:
#
# a) the GNU General Public License as published by the Free Software 
# Foundation, either version 1 or (at your option) any later version, 
#
# or 
#
# b) the "Artistic License" which comes with this program. 
#
# You should have received a copy of the Artistic License with this 
# module, in the file artistic.txt.  If you did not, I'll be glad to
# provide one. 
#
# You should have received a copy of the GNU General Public License 
# along with this program.  If you did not, write to the Free Software 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307.
#
# This program is distributed "as is" and without warranty of any
# kind, either express or implied.  (Some states do not allow the
# limitation or exclusion of liability for incidental or consequential
# damages, so this notice may not apply to you.)  In no event shall
# the liability of Darryl C. Burgdorf and/or Affordable Web Space
# Design for any damages, losses and/or causes of action exceed the
# total amount paid by the user for this software.

# VERSION HISTORY:
#
# 2.0.1 07/04/19  Fixed a bug in add range.  Fixed datafile in
#                 webcal.pl
# 2.0.0 11/01/11  Recoded to work with use strict.  Added links to
# 		  each page from every other page.  Tided up the 
# 		  delete page so that you can select date period.
# 1.21  01/04/00  Corrected add script's handling of 4-digit years
#                 Changed deliter used in delete script from :: to |
# 1.20  12/30/99  Allowed for use of multiple data files
#                 Made display of "day counter" optional
#                 Added (optional) calculation of lunar phases
#                 Changed "annual" code to "x" to allow 0 for 2000
#                 Short year entries now 1950-2049
#                 Extended one-year limit on "day each week" entries
#                 Squashed "day each week / MonSunWeek" entry bug
#                 Added check for entry dir validity before using
#                 Added line/paragraph parsing to "data dir" entries
#                 Added separate header/footer for "data dir" entries
#                 Improved checks on entry validity
#                 Replaced "reload to add another" with FORM button
#                 Added $CalendarTitle and $TableFont
#                 Added $HourOffset
#                 Lots of minor tweaks, after two years.... ;)
# 1.11  01/30/98  Corrected small problem with some versions of Perl 5
# 1.10  01/29/98  FIRST SHAREWARE RELEASE
#                 Moved configuration variables to separate file
#                 Added configurable table colors
#                 Added "Small Table" option (with or without text)
#                 Stripped "empty" entries from text listing
#                 Allowed for "Monday-Sunday" weeks
#                 Allowed for two-digit year entry (1900s)
#                 Allowed for "date range" entries
#                 Added option to allow HTML in calendar data entries
#                 Limited basic event entries to 80 characters
#                 Added optional "data dir" for more extensive entries
#                 Allowed disabling of user choice of style
#                 Fixed bug that made some entries "undeletable"
# 1.00  01/05/98  Initial "public" release

use strict;

my $cgiurl = "webcal.pl";
my $addcgiurl = "webcal.add.pl";
my $deletecgiurl = "webcal.delete.pl";

my @datafiles = (
  "File 1","/var/www/default/data.edit1.txt",
  );

my $DataDirPath = "data";
my $DataDirURL = "data";
my $DataDir_header = "";
my $DataDir_footer = "";
my $header_file = "/usr/foo/webcal/header.txt";
my $footer_file = "/usr/foo/webcal/footer.txt";
my $version = "2.0.1";
my $CalendarTitle = "Webcal $version";
my $DisplayCounter = 0;
my $DisplayPhases = 1;
my $DefaultType = "Table";
my $SmallTableText = 1;
my $PreviousLastOnly = 0;
my $AllowUserChoice = 1;
my $AllowHTML = 1;
my $AllowMultiDate = 1;
my $HourOffset = 0;
my $MonSunWeek = 1;
my ($DefaultUsed, $perp_dow, $weekday, $perp_days, $perp_togo, $perp_eom, $xshortmonth, $NextYear, $NextMonth, $perp_mon, $xday, $xshortday, $leftover, $perp_year, $LastYear, $perp_day, $perp_sofar, $LastMonth, $line, $ADVQuery, $title, @unsorteddata, @days, @shortdays, @header, @footer, %INPUT);

my @months =
  ('January','February','March','April','May','June',
  'July','August','September','October','November','December');
my @shortmonths =
  ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
if ($MonSunWeek) {
        @days =
          ('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
        @shortdays =
          ('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
}
else {
        @days =
          ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
        @shortdays =
          ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
}

my $time = time;
my ($mday,$month,$year) = (localtime($time+($HourOffset*3600)))[3,4,5];
$month = $month+1;
$year += 1900;

unless (defined $INPUT{'Month'}) {
        $INPUT{'Month'} = $month;
}
unless (defined $INPUT{'Year'}) {
        $INPUT{'Year'} = $year;
}
unless (defined $INPUT{'Type'}) {
        $INPUT{'Type'} = $DefaultType;
        $DefaultUsed = 1;
}

open(DF, ">/tmp/debug");
print DF "Month: $INPUT{'Month'}\nYear: $INPUT{'Year'}\nType: $INPUT{'Type'}\n";

# NOTHING BELOW THIS LINE NEEDS TO BE ALTERED!

print "Content-type: text/html\n\n";

my (%Text, %Table, %SmallTable);
#my $DefaultUsed = 0;

# Parse form data
my $buffer;
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
my @pairs = split(/&/, $buffer);
foreach my $pair (@pairs) {
	my ($name, $value) = split(/=/, $pair);
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$INPUT{$name} = $value;
}

# Adjust two digit data into four digits
if ($INPUT{'Year'} =~ /\D/) { $INPUT{'Year'} = 0; }
elsif ($INPUT{'Year'} < 50) { $INPUT{'Year'} += 2000; }
elsif ($INPUT{'Year'} < 100) { $INPUT{'Year'} += 1900; }

# Check if year input is within range (1601 to 2899)
if (($INPUT{'Year'} < 1601) || ($INPUT{'Year'} > 2899)) {
	&Header("Date Out of Range!");
	print "<H1>Date Out of Range!</H1>\n";
	print "<P ALIGN=CENTER>The date you provided is out of range.";
	print "<BR>It must be between 1601 and 2899 A.D.\n";
	&Footer;
	exit;
}

# Read in the data files
foreach my $datafile (@datafiles) {
	open (DATA,$datafile) || print DF "Failed opening datafile: $datafile $@\n";
	while (my $line=<DATA>) {
		push (@unsorteddata,$line);
	}
	close (DATA);
}

my @data = sort {$a <=> $b} (@unsorteddata);

foreach my $line (@data) {
	if ($line =~ /\n$/) { chop ($line); }
	my ($date,$desc,$URL) = split (/\|/, $line);
	my ($dateyear,$datemonth,$dateday) = 
	  $date =~ m#(\d\d\d\d)(\d\d)(\d\d)#o;
	if ((int($dateyear) == int($INPUT{'Year'}))
	  || (int($dateyear) < 1)) {
		if (int($datemonth) == int($INPUT{'Month'})) {
			my $textdesc = $desc;
			$textdesc =~ s/<([^>]|\n)*>//g;
			if ($URL) {
				$Table{int($dateday)} .= "<P><A HREF=\"$URL\">";
				$Table{int($dateday)} .= "$desc</A>";
				$SmallTable{int($dateday)} .= "<BR><HR NOSHADE WIDTH=25%><A ";
				$SmallTable{int($dateday)} .= "HREF=\"$URL\">";
				$SmallTable{int($dateday)} .= "$desc</A>";
				$Text{int($dateday)} .= "                    ";
				$Text{int($dateday)} .= "<A HREF=\"$URL\">";
				$Text{int($dateday)} .= "$textdesc</A>\n";
			}
			else {
				$Table{int($dateday)} .= "<P>$desc";
				$SmallTable{int($dateday)} .= "<BR><HR NOSHADE WIDTH=25%>$desc";
				$Text{int($dateday)} .= "                    ";
				$Text{int($dateday)} .= "$textdesc\n";
			}
		}
	}
	$SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;
	$Text{int($dateday)} =~ s/^                    //;
}

#&PerpetualCalendar(int($INPUT{'Month'}),1,int($INPUT{'Year'}));

### The perpetual calendar ###
($perp_mon,$perp_day,$perp_year) = (int($INPUT{'Month'}),"1",int($INPUT{'Year'}));
        my %day_counts =
          (1,0,2,31,3,59,4,90,5,120,6,151,7,181,
          8,212,9,243,10,273,11,304,12,334);
        $perp_days = (($perp_year-1601)*365)+(int(($perp_year-1601)/4));
        $perp_days += $day_counts{$perp_mon};
        $perp_days += $perp_day;
        $perp_sofar = $day_counts{$perp_mon};
        $perp_sofar += $perp_day;
        $perp_togo = 365-$perp_sofar;
        if (int(($perp_year-1600)/4) eq (($perp_year-1600)/4)) {
                $perp_togo++;
                if ($perp_mon > 2) {
                        $perp_days++;
                        $perp_sofar++;
                        $perp_togo -= 1;
                }
        }
        foreach my $key (1700,1800,1900,2100,2200,2300,2500,2600,2700) {
                if ((($perp_year == $key) && ($perp_mon > 2))
                  || ($perp_year > $key)) {
                        $perp_days -= 1;
                }
        }
        $perp_dow = $perp_days - (int($perp_days/7)*7);
        if ($perp_dow == 7) { $perp_dow = 0; }
        if ($MonSunWeek) {
                $perp_dow -= 1;
                if ($perp_dow == -1) { $perp_dow = 6; }
        }
        $perp_eom = 31;
        if (($perp_mon == 4) || ($perp_mon == 6)
          || ($perp_mon == 9) || ($perp_mon == 11)) {
                $perp_eom = 30;
        }
        if (($perp_mon == 2)) {
                $perp_eom = 28;
        }
        if ((int(($perp_year-1600)/4) eq (($perp_year-1600)/4))
          && ($perp_mon == 2)) {
                $perp_eom = 29;
        }
        foreach my $key (1700,1800,1900,2100,2200,2300,2500,2600,2700) {
                if ($perp_year == $key) {
                        if ($perp_mon == 1) {
                                $perp_togo -= 1;
                        }
                        elsif ($perp_mon == 2) {
                                $perp_togo -= 1;
                                $perp_eom = 28;
                        }
                        else {  
                                $perp_sofar -= 1;
                        }
                }
        }
### End perpetual calendar ###

my $xmonth = @months[int($INPUT{'Month'})-1];
my $heading = "$xmonth $INPUT{'Year'}";
&Header("$heading");

if ($CalendarTitle) {
	print "<H1>$CalendarTitle</H1>\n";
}

print DF "Type before if: $INPUT{'Type'}\n";
if ($INPUT{'Type'} eq "Text") {
	print DF "Printing text\n"; 
	print "<H1>$heading</H1>\n";
	print "<P><PRE>\n";
	foreach my $key (1..$perp_eom) {
		$weekday = ($key+$perp_dow)-(int(($key+$perp_dow)/7)*7);
		if ($weekday < 1) { $weekday = 7; }
		# Work out phases of the moon
		if ($DisplayPhases) {
			if (int((int((($perp_days-5.36945)/29.53031)+.5)*29.53031)+5.36945) == $perp_days) {
				if ($Text{$key}) { $Text{$key} .= "                    "; }
				$Text{$key} .= "New Moon\n";
			}
			if (int((int((($perp_days-12.75202)/29.53031)+.5)*29.53031)+12.75202) == $perp_days) {
				if ($Text{$key}) { $Text{$key} .= "                    "; }
				$Text{$key} .= "First Quarter Moon\n";
			}
			if (int((int((($perp_days-20.13460)/29.53031)+.5)*29.53031)+20.13460) == $perp_days) {
				if ($Text{$key}) { $Text{$key} .= "                    "; }
				$Text{$key} .= "Full Moon\n";
			}
			if (int((int((($perp_days-27.51718)/29.53031)+.5)*29.53031)+27.51718) == $perp_days) {
				if ($Text{$key}) { $Text{$key} .= "                    "; }
				$Text{$key} .= "Last Quarter Moon\n";
			}
		}
		$perp_days++;		
		if ($Text{$key}) {
			if ($key < 10) { print "0"; }
			print "$key ";
			$xshortmonth = @shortmonths[$INPUT{'Month'}-1];
			print "$xshortmonth ";
			print "$INPUT{'Year'} ";
			$xshortday = @shortdays[$weekday-1];
			print "($xshortday)   ";
			print "$Text{$key}";
		}
		if (($weekday == 7) && !($key == $perp_eom)) {
			print "\n                    ---------------\n\n";
		}
	}
	print "</PRE>\n";
}
elsif ($INPUT{'Type'} eq "Table") {
	print DF "Printing table\n";
	print "<P><CENTER><SMALL><TABLE CLASS=\"main\">\n";
	print "<TR CLASS=\"month\">\n";
	print "<TH CLASS=\"day\" COLSPAN=7>";
	print "<H1>$heading</H1></TH>\n";
	print "</TR>\n<TR CLASS=\"day\">\n";
	foreach my $key (1..7) {
		print "<TH>";
		$xday = @days[$key-1];
		print "$xday</TH>";
	}
	print "\n</TR><TR>\n";
	if ($perp_dow > 0) {
		print "<TD COLSPAN=$perp_dow>";
		print "<P>&nbsp</TD>";
	}
	print DF "End of month: $perp_eom\n";
	foreach my $key (1..$perp_eom) {
		print DF "Key: $key\n";
		if ($DisplayPhases) {
			if (int((int((($perp_days-5.36945)/29.53031)+.5)*29.53031)+5.36945) == $perp_days) {
				$Table{$key} .= "<P CLASS=\"small\">New Moon</P>";
			}
			if (int((int((($perp_days-12.75202)/29.53031)+.5)*29.53031)+12.75202) == $perp_days) {
				$Table{$key} .= "<P CLASS=\"small\">First Quarter Moon</P>";
			}
			if (int((int((($perp_days-20.13460)/29.53031)+.5)*29.53031)+20.13460) == $perp_days) {
				$Table{$key} .= "<P CLASS=\"small\">Full Moon</P>";
			}
			if (int((int((($perp_days-27.51718)/29.53031)+.5)*29.53031)+27.51718) == $perp_days) {
				$Table{$key} .= "<P CLASS=\"small\">Last Quarter Moon</P>";
			}
		}
		$perp_days++;		
		if ($Table{$key}) { print "<TD CLASS=\"used\">"; }
		else { print "<TD>"; }
		# Work out if we are printing todays date and if so, print in red
		if (($INPUT{'Year'} == $year)
		  && ($INPUT{'Month'} == $month)
		  && ($key == $mday)) {
			print "<P CLASS=\"today\">$key</P>";
		}
		else {
			print "<P CLASS=\"num\">$key</P>";
		}
		if ($DisplayCounter) {
			print "<BR><SMALL>($perp_sofar/$perp_togo)</SMALL>";
		}
		$perp_sofar++;
		$perp_togo -= 1;
		if ($Table{$key}) {
			print "$Table{$key}";
		}
		else { print "<P>&nbsp;"; }
		print "</TD>";
		$weekday = ($key+$perp_dow)-(int(($key+$perp_dow)/7)*7);
		if (($weekday == 0) && !($key == $perp_eom)) {
			print "\n</TR><TR>\n";
		}
	}
	if ($weekday > 0) {
		$leftover = 7-$weekday;
		print "<TD COLSPAN=$leftover>";
		print "<P>&nbsp;</TD>";
	}
	print "</TR></TABLE></SMALL></CENTER>\n";
}
else {
	print DF "Printing other\n";
	if ($SmallTableText) {
		print DF "With small text\n";
		print "<P><CENTER><TABLE CELLPADDING=12><TR>";
		print "<TD VALIGN=TOP>\n";
		print "<P><CENTER><TABLE CELLPADDING=3>\n";
		foreach my $key (1..$perp_eom) {
			if ($DisplayPhases) {
				if (int((int((($perp_days-5.36945)/29.53031)+.5)*29.53031)+5.36945) == $perp_days) {
					if ($SmallTable{$key}) { $SmallTable{$key} .= "<BR>"; }
					$SmallTable{$key} .= "New Moon\n";
				}
				if (int((int((($perp_days-12.75202)/29.53031)+.5)*29.53031)+12.75202) == $perp_days) {
					if ($SmallTable{$key}) { $SmallTable{$key} .= "<BR>"; }
					$SmallTable{$key} .= "First Quarter Moon\n";
				}
				if (int((int((($perp_days-20.13460)/29.53031)+.5)*29.53031)+20.13460) == $perp_days) {
					if ($SmallTable{$key}) { $SmallTable{$key} .= "<BR>"; }
					$SmallTable{$key} .= "Full Moon\n";
				}
				if (int((int((($perp_days-27.51718)/29.53031)+.5)*29.53031)+27.51718) == $perp_days) {
					if ($SmallTable{$key}) { $SmallTable{$key} .= "<BR>"; }
					$SmallTable{$key} .= "Last Quarter Moon\n";
				}
			}
			$perp_days++;		
			next unless ($SmallTable{$key});
			print "<TR><TD CLASS=\"date\">";
			$xshortmonth = @shortmonths[$INPUT{'Month'}-1];
			print "$key $xshortmonth ";
			print "$INPUT{'Year'} ";
			$weekday =
			  ($key+$perp_dow)-(int(($key+$perp_dow)/7)*7);
			if ($weekday < 1) { $weekday = 7; }
			$xshortday = @shortdays[$weekday-1];
			print "($xshortday):";
			print "</TD>";
			print "</TR>\n<TR>";
			print "<TD CLASS=\"event\">";
			print "$SmallTable{$key}</TD>";
			print "</TR>";
		}
		print "</TABLE></CENTER>";
		print "</TD><TD VALIGN=TOP>\n";
	}
	print "<P><CENTER><SMALL><TABLE CLASS=\"main\"><TR CLASS=\"month\">\n";
	print "<TH COLSPAN=7>";
	print "<BIG>$heading</BIG></TH>\n";
	print "</TR><TR>\n";
	foreach my $key (1..7) {
		print "<TH>";
		$xshortday = @shortdays[$key-1];
		print "$xshortday</TH>";
	}
	print "\n</TR><TR>\n";
	if ($perp_dow > 0) {
		print "<TD COLSPAN=$perp_dow>";
		print "<P>&nbsp</TD>";
	}
	foreach my $key (1..$perp_eom) {
		print "<TD";
		if ($SmallTable{$key}) { print " CLASS=\"used\""; }
		if (($INPUT{'Year'} == $year) && ($INPUT{'Month'} == $month) && ($key == $mday)) {
			print "><P CLASS=\"today\">$key";
		} else {
			print "><P CLASS=\"num\">$key";
		}
		print "</TD>";
		$weekday = ($key+$perp_dow)-(int(($key+$perp_dow)/7)*7);
		if (($weekday == 0) && !($key == $perp_eom)) {
			print "\n</TR><TR>\n";
		}
	}
	if ($weekday > 0) {
		$leftover = 7-$weekday;
		print "<TD COLSPAN=$leftover";
		print "<P>&nbsp;</TD>";
	}
	print "</TR></TABLE></SMALL></CENTER>\n";
}

print "<P><CENTER><TABLE><TR>\n";

$LastYear = int($INPUT{'Year'});
$LastMonth = int($INPUT{'Month'})-1;
if ($LastMonth == 0) {
	$LastMonth = 12;
	$LastYear -= 1;
}
print "<TD><FORM METHOD=POST ACTION=$cgiurl>";
print "<INPUT TYPE=HIDDEN NAME=Month VALUE=$LastMonth>";
print "<INPUT TYPE=HIDDEN NAME=Year VALUE=$LastYear>";
print "<INPUT TYPE=HIDDEN NAME=Type VALUE=\"$INPUT{'Type'}\">";
print "<INPUT TYPE=SUBMIT VALUE=\"Previous Month\">";
print "</FORM></TD>\n";

$NextYear = int($INPUT{'Year'});
$NextMonth = int($INPUT{'Month'})+1;
if ($NextMonth == 13) {
	$NextMonth = 1;
	$NextYear += 1;
}
print "<TD><FORM METHOD=POST ACTION=$cgiurl>";
print "<INPUT TYPE=HIDDEN NAME=Month VALUE=$NextMonth>";
print "<INPUT TYPE=HIDDEN NAME=Year VALUE=$NextYear>";
print "<INPUT TYPE=HIDDEN NAME=Type VALUE=\"$INPUT{'Type'}\">";
print "<INPUT TYPE=SUBMIT VALUE=\"Next Month\">";
print "</FORM></TD>\n";

print "</TR></TABLE></CENTER>\n";

&Footer;
close(DF);
exit;

sub PerpetualCalendar {
	# This perpetual calendar routine provides accurate day/date
	# correspondences for dates from 1601 to 2899 A.D.  It is based on
	# the Gregorian calendar, so be aware that early correspondences
	# may not always be historically accurate.  The Gregorian calendar
	# was adopted by the Italian states, Portugal and Spain in 1582,
	# and by the Catholic German states in 1583.  However, it was not
	# adopted by the Protestant German states until 1699, by England
	# and its colonies until 1752, by Sweden until 1753, by Japan
	# until 1873, by China until 1912, by the Soviet Union until 1918,
	# and by Greece until 1923.
	($perp_mon,$perp_day,$perp_year) = @_;
	my %day_counts =
	  (1,0,2,31,3,59,4,90,5,120,6,151,7,181,
	  8,212,9,243,10,273,11,304,12,334);
	$perp_days = (($perp_year-1601)*365)+(int(($perp_year-1601)/4));
	$perp_days += $day_counts{$perp_mon};
	$perp_days += $perp_day;
	$perp_sofar = $day_counts{$perp_mon};
	$perp_sofar += $perp_day;
	$perp_togo = 365-$perp_sofar;
	if (int(($perp_year-1600)/4) eq (($perp_year-1600)/4)) {
		$perp_togo++;
		if ($perp_mon > 2) {
			$perp_days++;
			$perp_sofar++;
			$perp_togo -= 1;
		}
	}
	foreach my $key (1700,1800,1900,2100,2200,2300,2500,2600,2700) {
		if ((($perp_year == $key) && ($perp_mon > 2))
		  || ($perp_year > $key)) {
			$perp_days -= 1;
		}
	}
	$perp_dow = $perp_days - (int($perp_days/7)*7);
	if ($perp_dow == 7) { $perp_dow = 0; }
	if ($MonSunWeek) {
		$perp_dow -= 1;
		if ($perp_dow == -1) { $perp_dow = 6; }
	}
	$perp_eom = 31;
	if (($perp_mon == 4) || ($perp_mon == 6)
	  || ($perp_mon == 9) || ($perp_mon == 11)) {
		$perp_eom = 30;
	}
	if (($perp_mon == 2)) {
		$perp_eom = 28;
	}
	if ((int(($perp_year-1600)/4) eq (($perp_year-1600)/4))
	  && ($perp_mon == 2)) {
		$perp_eom = 29;
	}
	foreach my $key (1700,1800,1900,2100,2200,2300,2500,2600,2700) {
		if ($perp_year == $key) {
			if ($perp_mon == 1) {
				$perp_togo -= 1;
			}
			elsif ($perp_mon == 2) {
				$perp_togo -= 1;
				$perp_eom = 28;
			}
			else {
				$perp_sofar -= 1;
			}
		}
	}
}

sub Header {
       ($title) = @_;
       print "<HTML>\n<HEAD>\n<TITLE>$title</TITLE>\n";
       print "<STYLE>\@import url(style.css);</STYLE>\n</HEAD>\n";
       print "<BODY>\n";
       if ($header_file) {
               open (HEADER,"$header_file");
               @header = <HEADER>;
               close (HEADER);
               foreach $line (@header) {
                       if ($line =~ /<!--InsertAdvert\s*(.*)-->/i) {
                               &insertadvert($1);
                       }
                       else {
                               print "$line";
                       }
               }
       }
}

sub Footer {
       unless ($PreviousLastOnly) {
               print "<P><HR WIDTH=75%>\n";
               print "<FORM METHOD=POST ACTION=$cgiurl>\n";
               print "<P><CENTER>Month: <SELECT NAME=Month>";
               foreach my $key (1..12) {
                       print "<OPTION VALUE=$key";
                       if ($INPUT{'Month'} eq $key) { print " SELECTED"; }
                       $xmonth = @months[$key-1];
                       print ">$xmonth";
               }
               print "</SELECT> ";
               print "Year: <INPUT TYPE=TEXT NAME=Year ";
               print "VALUE=$INPUT{'Year'} SIZE=4> ";
               if ($AllowUserChoice) {
                       print "Type: <SELECT NAME=Type>";
                       print "<OPTION VALUE=Table";
                       if ($INPUT{'Type'} eq "Table") {
                               print " SELECTED";
                       }
                       print ">Table";
                       print "<OPTION VALUE=\"Small Table\"";
                       if ($INPUT{'Type'} eq "Small Table") {
                               print " SELECTED";
                       }
                       print ">Small Table";
                       print "<OPTION VALUE=Text";
                       if ($INPUT{'Type'} eq "Text") {
                               print " SELECTED";
                       }
                       print ">Text";
                       print "</SELECT>";
               }
               print "\n";
               print "<P><INPUT TYPE='button' VALUE='Add to Calendar' onclick=\"document.location = 'webcal.add.pl'\" />\n";
               print "<INPUT TYPE=SUBMIT VALUE=\"View Calendar\">\n";
               print "<INPUT TYPE='button' VALUE='Remove from Calendar' onclick=\"document.location = 'webcal.delete.pl'\" /><br>\n";
               print "</CENTER></FORM>\n";
       }
       if (($INPUT{'Type'} eq "Small Table")
         && $SmallTableText && !($DefaultUsed)) {
               print "</TD></TR></TABLE></CENTER>\n";
       }
       print "<P><HR>\n<SMALL>\n<P ALIGN=CENTER>";
       print "Maintained with <STRONG>";
       print "<A HREF=\"http://www.norgie.net/tools/webcal/\">";
       print "WebCal $version</A></STRONG>.\n</SMALL>\n";
       if ($footer_file) {
               open (FOOTER,"$footer_file");
               @footer = <FOOTER>;
               close (FOOTER);
               foreach $line (@footer) {
                       if ($line =~ /<!--InsertAdvert\s*(.*)-->/i) {
                               &insertadvert($1);
                       }
                       else {
                               print "$line";
                       }
               }
       }
       print "</BODY></HTML>\n";
}

sub insertadvert {
       my $adzone = @_;
       my $ADVNoPrint = 1;
       if ($adzone) { $ADVQuery = "zone=$adzone"; }
       else { $ADVQuery = ""; }
       require "/usr/www/users/dburgdor/scripts/ads/ads.pl";
}
