#!/usr/bin/perl

#prepare the subs for the Header and Footer
require 'HeadFoot.lib';

#print  as HTML content (send to browser)
print "Content-Type: text/html\n\n"; 

#Split up the query string.
@values = split(/&/,$ENV{'QUERY_STRING'});
foreach $i (@values)
{
($varname, $mydata) = split(/=/,$i);
if ($varname eq "schedule")
{
	$schedule=$mydata;
}
}

#call subs for the page Header, Ledger Tables and Footer
&printHeader();

printschedule();

&printFooter();

exit;


#subroutine to build Schedule Tables

sub printschedule {

$date_command = "/bin/date";
$date = `$date_command +"%A, %B %d, %Y at %T (%Z)"`; chop($date);
$minidate = `$date_command +"%D"`; chop($shortdate);
$shortdate = `$date_command +"%m"`; chop($shortdate);
$yeardate = `$date_command +"%Y"`; chop($yeardate);
if ($schedule){
	$yeardate = $schedule
	}
;


#decide which Data File to open(from Query String)
open(INF,"Schedule.txt") || &dienice("Can't open Schedule.txt for reading.");

#Confirm correct Data File to User
$lastYear=($yeardate-1);
$nextYear=($yeardate+1);
print <<EndOfSched;
	

<tr><td colspan=9>
	 <center><h1>$yeardate Events Schedule</h1><font size=-1>Today's Date: $minidate</font><br>
	 <a href="schedule.pl?schedule=$lastYear"><font size=-1>Last Year</a>
	 <a href="schedule.pl?schedule=$nextYear">  |  Next Year</font></a><br>
	
</td></tr></table>
</td></tr></table>
<table cellspacing=0><tr><td align=center>

EndOfSched
;
#Offer Filter options

#=============Deleted Hold in case we need event types=====================

#print <<EndOfFilter;
#	Show only: <b><font size=-1>
#	<a href="meetings.pl?schedule=$schedule"><font color="black">Meetings | </a>
#	<a href="parades.pl?schedule=$schedule"><font color="green">Parades </font></a>|
#	<a href="musters.pl?schedule=$schedule"><font color="blue">Musters </font></a>|
#	<a href="reenact.pl?schedule=$schedule"><font size=+1 color="red"><i>Reenactments </i></font></a> | 
#	<a href="schedule.pl?schedule=$schedule">Show All</a></b></font><br>
#EndOfFilter
;
#==========================================


#Print Head of Schedule Table
print <<EndOfIntro;
<center>
<TABLE WIDTH=900 BORDER=1 background="../images/bkgd.jpg">
 <tbody>
    <tr>
      <td width=15%><b>Dates</b></td>
      <td width=48%><b>Event</b></td>
      <td width=7%><b>From</b></td>
      <td width=7%><b>Until</b></td>
      <td width=10%><b>Map or Link</b></td>
      <td width=3%><b>Info</b></td>
    </tr>
 <tr>
    </tr>

EndOfIntro
;

#assign Data to Array "@scheduleData"
@scheduleData = <INF>;
@scheduleData=sort(@scheduleData);

#Split up lines of data to separate scalars
foreach $line (@scheduleData) {

($year,$date,$thru,$event,$line,$step,$map,$type,$info) = split(/,/,$line);



#Print Table Row
if ($year eq $yeardate){
if ($date >= $shortdate){
if ($type eq "black") {
print <<EndOfBlock;
	<tr>
	<td background="../images/bkgd.jpg"><font color="$type">$date - $thru</td>
	<td background="../images/bkgd.jpg"><font color="$type">$event</td>
	<td background="../images/bkgd.jpg"><font color="$type">$line</td>
	<td background="../images/bkgd.jpg"><font color="$type">$step</td>

EndOfBlock
;

#add link to map

if ($map) {
print <<EndOfMap;

	<td background="../images/bkgd.jpg"><font color="$type"><a href=$map target=_window>Map</a></td>


EndOfMap
;
}


else {
print<<EndOfMap;
	<td background="../images/bkgd.jpg">NoMap</td>


EndOfMap
;
}

#add link to info.txt
if ($info) {
print <<EndOfInfo;

	<td background="../images/bkgd.jpg"><font color="$type"><a onclick="javascript:window.open('info.pl?file=$info','InfoWdw','scrollbars,width=800,height=400')"
	href="schedule.pl?schedule=$schedule">Info</a></td>
	</tr>

EndOfInfo
;
}

else {
print<<EndOfInfo;
	<td background="../images/bkgd.jpg">NoInfo</td>
	</tr>
	</center>


	
EndOfInfo
;
}
}
}
}
}

}

#Prints ERROR on failure to open data file
sub dienice {
	($msg) = @_;
	print "<h2>Error</h2>";
	print $msg;
	exit;
}

