#!/usr/bin/perl 
$color{F}="ff4444";
$color{R}="00aa88";
$color{C}="eeee00";
$color{E}="0000ff";
######### get personal info #########
open(SUS, "<../private/workfiles/emplist");
foreach(<SUS>)
{
    
    ($intl,$name,$career,$citiz,$location,$comment)=split('#',$_);
    $cit{uc $intl}=uc $citiz;
#    print"$intl $cit{$intl}\n";
}
close(SUS);
######## whats the date today? #######
@datetime=localtime(time);
$monimplant='';
$dayimplant='';
if($datetime[4]+1 < 10)
{
    $monimplant='0';
}
if($datetime[3]<10)
{
    $dayimplant='0';
}
$today=join('', ($datetime[5]+1900),$monimplant,($datetime[4]+1),$dayimplant,($datetime[3]));

########## set defalt values ########
$var{year}=substr $today,0,4;
$var{zone}="lt";
$var{startsearch}=$today -1;
$var{loc}=1;
   #get the query string

$query=$ENV{QUERY_STRING};
   #decode query string into variable list
#$question="?";
#($first,$second)=split("$question",$query);
#$query=join('#found?',$first,$second);

if (defined($query))
{
    @list=split('&', $query);
    foreach (@list)
    {
	my ($key,$value) = split /=/;
	$var{$key}=$value;
    }
}

####### find me 2 ######
if(defined($var{find}))
{
    $var{me}=uc $var{me};
    system"ls ../private/runs/ > junk";
    open(RUNS, "<junk");
    foreach(sort <RUNS>)
    {
	chomp;
	$run=$_;
	open(RUN,"<../private/runs/$_");
	foreach(<RUN>)
	{
	    chomp;
	    chomp $run;
	    ($date{lt},$moon{lt},$moon{ut},$length,$jday,$hires1,$hires2,$comment,@waste)=split /#/;
	    if((!defined($ocurrence))and($_=~m/$var{me}/)and($date{lt} > $var{startsearch}))
	    {
		$ocurrence=$date{lt};
		$var{whichrun}=$run;
	    }
	    
	    
	}
	close(RUN);
    }
    close(RUNS);
    if(!defined($ocurrence))
    {
	$found="No";
    }
}



print("Content-type: text/html\n\n");
print('<HTML><HEAD><TITLE>HiRes schedule</TITLE></HEAD><BODY bgcolor=eeeeee><TABLE>');
################ Help Bar #########################
open(HELP, "<helpbar");
foreach(<HELP>)
{
    chomp;
    if(!($_=~m/view/)or !($_=~m/href/))
    { 
	print"$_";
    }
    elsif($_ =~m/view/)
    {
	($first,$second)=split('>',$_);
	print"$first ><FONT color=ff0000>$second ></FONT>";
    }
}
close(HELP);


print'</FORM></TABLE>';
###############################################################################
##################### 1 initial web page, get location and date ###############
###############################################################################
if (!defined($var{whichrun}))
{   
    print'<TABLE><FORM method=GET ACTION="viewsched.pl">';
    print'<INPUT type="hidden" name="loc" value=1>';
    print('<TABLE><TR><TD>');
    print(`ls ../private/runs > junk`);
    print("Choose a run:<BR></TD><TD>");
    open(RUN, "junk");
    foreach(<RUN>)
    {
	chomp;
	$thisyear=substr $_,0,4;
	$startrun=substr $_,0,8;
	$endrun=substr $_,9,8;
	if($thisyear eq $var{year})
	{
	    $yy1=substr $_,0,4;
	    $mm1=substr $_,4,2;
	    $dd1=substr $_,6,2;
	    $yy2=substr $_,9,4;
	    $mm2=substr $_,13,2;
	    $dd2=substr $_,15,2;
	    $ym1=join('',$yy1,$mm1);
	    $ym2=join('',$yy2,$mm2);
	    print"\n";
	    print'<INPUT type="radio" name="whichrun" value=',$_;
	    #print'>';
	    print'>',"$yy1/$mm1/$dd1-$yy2/$mm2/$dd2";
	    if(($today<=$endrun)and($today>=$startrun))
	    {
		print'-current run!';
	    }
	    $todayym=substr $today,0,6;
	    if(($today>$lastendrun)and($today<$startrun)and(($ym1 == $todayym)or($ym1==$todayym+1)))
	    {
		print'-next run!';
	    }
	    $lastendrun=$endrun;
	    print"<BR>";
	}
    }
    close(RUN);
    print('<BR> <INPUT type=submit value="Take a look">');
################### find me #################
    

    print'</TD><TD><FORM method = GET ACTION="viewsched.pl#found">';
    print"Find my next appearence in the work schedule<BR>";
    print"Initils: <INPUT type=text name=me size=4>";
    print"<INPUT type=submit name=find value=Find>";
    print"</FORM>";

    
    print'</TD></TR></TABLE></FORM><TABLE>';
    print'---------------------------';
    print'<FORM method=GET ACTION="viewsched.pl">';
    print'<INPUT type="submit" name="year" value="',$var{year}-1,'">';
    print"<-$var{year}->";
    print'<INPUT type="submit" name="year" value="',$var{year}+1,'">';
    print'</FORM>';
    print'<FORM method=GET ACTION="viewsched.pl">';
    print"The selected year is $var{year}<BR>To select a different year, please enter it below<BR>";
    print'<INPUT type="text" name="year" maxlength="4" size="4">';
    print'<INPUT type="submit" value="Happy New Year">';
    print('</TABLE></FORM></BODY></HTML>');

}

###############################################################################
####################### 2 display work schedule ###############################
###############################################################################


###### create the basic HTML requirements #########
if(defined($var{whichrun})) 
{ 
    
    if($found eq "No")
    {
	print"<TABLE><H1> $var{me} Not found after $var{startsearch}</H1></TABLE>";
	print('</TABLE></BODY></HTML>');
    }

else
{
#### get next and previous runs ######
    


    system"ls ../private/runs/ > junk";
    open(RUNS, "<junk");
    foreach(<RUNS>)
    {
	chomp;
	if((substr $_,0,8)<(substr $var{whichrun},0,8))
	{
	    $previous=$_;
	}
	if(((substr $_,0,8)>(substr $var{whichrun},0,8))and(!defined($next)))
	{
	    $next=$_;
	}
    }
    close(RUNS);

print("<TABLE><TR><TD>Todays date is: $today<BR><BR></TD></TR>"); 
    #print('<FORM method=GET ACTION="viewsched.pl">');
    #print('<INPUT type=submit value="HiRes 1">'); 
    #print('<INPUT type="hidden" name="whichrun" value="',$var{whichrun},'">');
    #print"<INPUT type=hidden name=zone value=$var{zone}>";
    #print('<INPUT type="hidden" name="loc" value="1"></FORM></TD><TD></FORM></TD><TD>'); 

    #print('<FORM method=GET ACTION="viewsched.pl">');
    #print('<INPUT type=submit value="HiRes 2">'); 
    #print('<INPUT type="hidden" name="whichrun" value="',$var{whichrun},'">');
    #print"<INPUT type=hidden name=zone value=$var{zone}>";
    #print('<INPUT type="hidden" name="loc" value="2"></FORM></TD><TD></FORM></TD><TD>');

   print'<TR><TD><FORM method=GET ACTION="viewsched.pl"><INPUT type=hidden name="whichrun" value="',$previous,'">';
    print'<INPUT type="hidden" name="loc" value=',$var{loc},'>';
    print'<INPUT type=submit value="Previous Run"></FORM></TD>';

    print'<TD><FORM method=GET ACTION="viewsched.pl"><INPUT type=hidden name="whichrun" value="',$next,'">';
    print'<INPUT type="hidden" name="loc" value=',$var{loc},'>';
    print'<INPUT type=submit value="Next Run"></FORM></TD></TR>';
    print"</TABLE><TABLE><TR><TD>";
    

    print'</TD></TR>';



####### inquire about desired action ##############

    
    print"\n\n</TABLE><TABLE><CENTER><B>",(substr $var{whichrun},0,4),"/",(substr $var{whichrun},4,2),"/";
    print (substr $var{whichrun},6,2);
    print"-",(substr($var{whichrun},9,4)),"/",(substr $var{whichrun},13,2),"/",(substr $var{whichrun},15,2);
    print"</B></CENTER>";
#################################
######## color code #############
#################################
    print"</TABLE><TABLE><FONT color=$color{F}>FOREIGN NATIONAL</FONT><BR><FONT color=$color{E}>ESCORT</FONT><BR>";
    print"<FONT color=$color{R}>PERMANENT RESIDENT</FONT><BR><FONT color=$color{C}>CITIZEN</FONT></TABLE>";



################################################## 
###### decode the runs list file #################
##################################################

    print("</TABLE><TABLE>");
########### display HiRes 1
    if($var{loc}==1)
    {
	print("<TR><TD></TD><TD><h3>yyyymmdd </h3></TD><TD><h3> Day </h3></TD><TD><h3> Length </h3></TD><TD><B><h3><B>HiRes 1</h3></TD><TD><h3><B>HiRes 2</h3></TD><TD>Notes</TD></TR>\n");

	print'</TR>';
     
	open(RUN, "../private/runs/$var{whichrun}");    
	foreach(<RUN>)
	{
	  
	    ($date{lt},$moon{lt},$moon{ut},$length,$jday,$hires1,$hires2,$comment,@waste)=split /#/;
	    ($dw,$mnth,$dd,$yy,$hourmin,@rest)=split(' ',$moon{lt});
	    ($hour,$min)=split(':',$hourmin);
	    if($hour<12)
	    {
		if($dw eq "Mon"){$dw="SUN";}
		if($dw eq "Tue"){$dw="MON";}
		if($dw eq "Wed"){$dw="TUE";}
		if($dw eq "Thu"){$dw="WED";}
		if($dw eq "Fri"){$dw="THU";}
		if($dw eq "Sat"){$dw="FRI";}
		if($dw eq "Sun"){$dw="SAT";}
	    }
	    my(@hilist1)=split('/',$hires1);
	    my(@hilist2)=split('/',$hires2);
    
	    $yy=substr $date{lt},0,4;
	    $mm=substr $date{lt},4,2;
	    $dd=substr $date{lt},6,2;
	    print"<TR><TD>";
	    



	    if($date{lt} == $ocurrence)
	    {
		print"<FORM method=GET ACTION=viewsched.pl>";
		print"<INPUT type=hidden name=me value=$var{me}>";
		print"<INPUT type=hidden name=startsearch value=$ocurrence>";
		print"<INPUT type=hidden name=whichrun value=$var{whichrun}>";
		print'<INPUT type=submit name=find value="Find Next">';
		print'</FORM';
		print"<FONT color=ff0000><a name=found>--></a></FONT>";
	    }
	    
	    print'<FORM method=GET ACTION="moon.pl#',"$yy$mm$dd",'">';
	    print"<INPUT type=hidden name=run value=$var{whichrun}>";
	    print"<INPUT type=hidden name=day value=$yy$mm$dd>\n";
	    print"</TD><TD width=70><INPUT type=submit color=ff0000 value=$yy/$mm/$dd></TD><TD width=70>";
	    if($date{lt} eq $today)
	    {
		print"<FONT color=ff0000>";
	    }
	    print"$dw</TD><TD width=70>$length";
	    print"</TD><TD>";
	    print"</FORM>";
	    for($i=0; $i<@hilist1; $i++)
	    {
		print "\n<a href=emplist.pl#$hilist1[$i]?intl=$hilist1[$i]><FONT color=$color{$cit{$hilist1[$i]}}>";
		if(($date{lt} eq $ocurrence)and($hilist1[$i] eq $var{me}))
		{
		    print"</FONT><FONT color=00ffff>";
		}
		print" $hilist1[$i] </FONT></a>";
		
	    }
	    print "<BR></TD><TD>";
	    for($i=0; $i<@hilist2; $i++)
	    {
		print "\n<a href=emplist.pl#$hilist2[$i]?intl=$hilist2[$i]><FONT color=$color{$cit{uc $hilist2[$i]}}>";
		if(($date{lt} eq $ocurrence)and($hilist2[$i] eq $var{me}))
		{
		    print"</FONT><FONT color=00ff00>";
		}
		print" $hilist2[$i] </FONT></a>";
	    }
	    print"<TD>$comment</TD></TR>";
	}
	close (RUN);
    }
############ display HiRes 2   
    if($var{loc}==2)
    {
	print("<TR><TD><h3>yyyymmdd </h3></TD><TD><h3> Day </h3></TD><TD><h3> Length </h3></TD><TD><h3>#HiRes 2</h3></TD><TD>Notes</TD></TR>\n");
#	print("</TABLE><TABLE>");
	open(RUN, "../private/runs/$var{whichrun}");    
	foreach(<RUN>)
	{
	    ($date{lt},$moon{lt},$moon{ut},$length,$jday,$hires1,$hires2,$comment,@waste)=split /#/;
#	    $dw=substr $moon{lt},0,3;
	    ($dw,$mnth,$dd,$yy,$hourmin,@rest)=split(' ',$moon{lt});
	    ($hour,$min)=split(':',$hourmin);
	    if($hour<12)
	    {
		if($dw eq "Mon"){$dw="SUN";}
		if($dw eq "Tue"){$dw="MON";}
		if($dw eq "Wed"){$dw="TUE";}
		if($dw eq "Thu"){$dw="WED";}
		if($dw eq "Fri"){$dw="THU";}
		if($dw eq "Sat"){$dw="FRI";}
		if($dw eq "Sun"){$dw="SAT";}
	    }
	    my(@hilist1)=split('/',$hires1);
	    my(@hilist2)=split('/',$hires2);
	    $yy=substr $date{lt},0,4;
	    $mm=substr $date{lt},4,2;
	    $dd=substr $date{lt},6,2;
	    print"<TR><TD width=70>$yy/$mm/$dd</TD><TD width=70>$dw</TD><TD width=70>$length</TD><TD>";
	    
	    for($i=0; $i<@hilist2; $i++)
	    {
		print "\n<a href=emplist.pl#$hilist2[$i]><FONT color=$color{$cit{uc $hilist2[$i]}}> $hilist2[$i] </FONT></a>";
#		print(" $hilist2[$i] ");
	    }
	    print "<BR></TD><TD>$comment</TD></TR>";
	}
	close (RUN);
    }

########### end the HTML doc ##############################

}
print('</TABLE></BODY></HTML>');
}













