#!/usr/local/bin/perl
require "/home/todd/toddweb.env";
require "/home/roger/lib/weblib.lib";
#use DB_File;
&setfeatures;
$tables=$VIEWER_FEATURE{TABLES};
$BODYENHANCE=" bgcolor=#FFFFFF";
($sec,$min,$hour,$mday,$mon,$year,$wday)=localtime(time);
@month=(January,February,March,April,May,June,July,August,September,October,November,December);
@monthlen=(31,28,31,30,31,30,31,31,30,31,30,31);
$monthnum=0;
foreach $month (@month){$month{$month}=$monthnum++}
$month=$month[$mon];
$months=join(":",@month);
dbmopen(%CAL,"$PAGEHOMEDIR/db/birthdaycal",0644);
&ReadParse;
foreach $date(keys %in){
	next unless $in{$date};
	($key)=($date=~/^D(\d+)/);
	if($in{$date} eq " "){delete $CAL{$key};}
	else{$CAL{$key}=$in{$date};}
}
$edit=($ENV{PATH_INFO}=~s#/edit##);
if($ENV{PATH_INFO}){
	($junk,$mon,$year)=split(m#/#,$ENV{PATH_INFO});
	if($mon>12){$year=$mon;$full=1;$monbegin=0;$monend=11;}
	else{$mon=$mon-1;$monbegin=$mon;$monend=$mon}
	$year=$year>1900?$year-1900:$year;
}
else{$monbegin=$mon;$monend=$mon;}
$caption="$month[$mon] ".($year+1900);
&htmlopen(($year+1900)."|") if $full;
&htmlopen("$caption|") if !$full;
#print "<BASEFONT SIZE=4>";
print "<form action=$weblib::URL/edit$ENV{PATH_INFO} method=POST>" if $edit;
for($mon=$monbegin;$mon<=$monend;$mon++){
$caption="$month[$mon] ".($year+1900);
$limit=$monthlen[$mon];
$limit+=1 if $mon==1&&leap($year+1900);
$wday=dow($year+1900,$mon+1,1);
for($i=0;$i<$limit;$i++){
$key=sprintf("%02d%02d",$mon+1,$i+1);
if($edit){
	$value[$i]="<textarea name=D$key cols=15 rows=6 wrap=soft>$CAL{$key}</textarea>";
}
else{
($value=$CAL{$key})=~s/;/<hr>/g if $tables;
($value=$CAL{$key})=~s/;/<br>/g if !$tables;
$value=~s/^(\d\d):/19\1:/g;
$value[$i]="$value";
}
}
$cal=&htmlcal($wday,$limit,@value);
if($monbegin==$monend){print $cal;}
else{push(@cal,$cal);}
}
if(@cal){
	print &htmltable($year+1900);
	for($i=0;$i<@cal;$i+=3){
		print &htmlrowdata(@cal[$i..$i+2]);
	}
	print &htmltableend;
}
if($edit){
	print "<input type=submit></form>\n";
}
print "<hr><center>| ";
for($month=1;$month<13;$month++){
	print "<a href=$weblib::URL/$month/$year>$month[$month-1]</a> | ";
}
print "</center>\n";
print $PAGETAIL;
&htmlclose;
sub htmlcal{
local($wday,$limit,@value)=@_;
local($i,$j,$k,@week);
local($cal);
$cal=&htmltable("$caption","border");
$cal.=&htmlrowhead("Sun","Mon","Tue","Wed","Thu","Fri","Sat") if $tables;
opendir(GIFS,"$PAGEHOMEDIR/www/pictures/calicons");
@img=grep(!/^\./,readdir(GIFS));
closedir(GIFS);
srand;
for($i=0;$i<6;$i++){
	@week=@week2=();
	for($j=0;$j<7;$j++){
		$k=$i*7+$j-($wday-1);
		#last if $k>$limit;
		if($tables){
			if($k<1||$k>$limit){
				$imgindex=int(rand($#img+1));
				$img=$img[$imgindex];
				splice(@img,$imgindex,1);
				$img=$edit?"":"<center><img src=/$HTMLPATH/pictures/calicons/$img></center>";
				push(@week,"");
				push(@week2,$img);
			}
			else{
				push(@week,"<b>$k</b>");
				push(@week2,$value[$k-1]);
			}
		}
		else{push(@week,$k<1||$k>$limit?"":"<hr><b>$k</b><br>$value[$k-1]");}
	}
	$cal.=&htmlrowdata(@week);
	$cal.=&htmlrowdata(@week2) if $tables;
	last if $k>=$limit;
}
$cal.=&htmltableend;
return $cal;
}
sub dow{
local($y,$m,$d)=@_;
(int(23*$m/9)+$d+4+($m<3?$y--:$y-2)+int($y/4)-int($y/100)+int($y/400))%7;
}
sub leap{
	local($year)=@_;
	($year%4==0&&($year%100!=0||$year%400==0));
}
