#!/usr/bin/perl
##############################################################################
# Incluyo la biblioteca de rutinas
##############################################################################
do '/home/delacole/public_html/cgi-perl/actividades/webcal.config.pl';;

# NOTHING BELOW THIS LINE NEEDS TO BE ALTERED!

$DefaultUsed = 0;

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

if ($INPUT{'Year'} =~ /\D/) { $INPUT{'Year'} = 0; }
elsif ($INPUT{'Year'} < 50) { $INPUT{'Year'} += 2000; }
elsif ($INPUT{'Year'} < 100) { $INPUT{'Year'} += 1900; }

if (($INPUT{'Year'} < 1601) || ($INPUT{'Year'} > 2899)) {
	&Header("Date Out of Range!");
	print "<H1 ALIGN=CENTER>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;
}

foreach $datafile (@datafiles) {
	open (DATA,$datafile);
	while ($line=<DATA>) {
		push (@unsorteddata,$line);
	}
	close (DATA);
}

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

foreach $line (@data) {
	if ($line =~ /\n$/) { chop ($line); }
	($date,$desc,$URL) = split (/\|/, $line);
	($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'})) {
			$textdesc = $desc;
			$textdesc =~ s/<([^>]|\n)*>//g;
			if ($URL) {
				$Table{int($dateday)} .= "<P><A HREF=\"$URL\" CLASS=\"negro\">";
				$Table{int($dateday)} .= "$desc</A>";
				$SmallTable{int($dateday)} .= "<A ";
				$SmallTable{int($dateday)} .= "HREF=\"$URL\" CLASS=\"negro\">";
				$SmallTable{int($dateday)} .= "$desc</A><BR>";
				$Text{int($dateday)} .= "                    ";
				$Text{int($dateday)} .= "<A HREF=\"$URL\" CLASS=\"negro\">";
				$Text{int($dateday)} .= "$textdesc</A>\n";
			}
			else {
				$Table{int($dateday)} .= "<P>$desc";
				$SmallTable{int($dateday)} .= "$desc<BR>";
				$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'}));

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

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

	print "<TABLE WIDTH=\"95%\" CELLPADDING=1 bgcolor=\"#FFFFFF\" align=\"center\"><TR><TD COLSPAN=\"2\">";

	print "<!-- ca-pub-9168368605450379/DelaCole_Banner728x90Agenda -->\n";
	print "<script type='text/javascript'>\n";
	print "GA_googleFillSlot(\"DelaCole_Banner728x90Agenda\");\n";
	print "</script>\n";
	print "<br />\n";
		
	print "</TD></TR><TR>";
	print "<TD width=\"500px\" VALIGN=TOP>\n";
	print "<P><CENTER><TABLE  WIDTH=\"100%\" CELLPADDING=1>\n";
	
	### ADSENSE RECTANGULAR A LA IZQUIERDA
	
	foreach $key (1..$perp_eom) {
		$perp_days++;
		next unless ($SmallTable{$key});
		print "<TR><TD BGCOLOR=\"#FFFFFF\" ALIGN=\"left\">";
		print "<BR><A NAME=\"$key\"></A><FONT COLOR=\"#000000\" FACE=\"$TableFont\" size=\"2\"><b>";
		$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 "</b></FONT></TD>";
		print "</TR><TR>";
		print "<TD BGCOLOR=\"#$bgcolor_special\" align=\"left\" valign=\"top\">";
		print "<FONT COLOR=\"#$textcolor_special\" FACE=\"$TableFont\" SIZE=\"2\">";
		print "$SmallTable{$key}</FONT></TD>";
		print "</TR>";
	}
	print "</TABLE></CENTER>";
	print "</TD><TD VALIGN=TOP width=\"336\">\n";

print "<P><CENTER><SMALL><TABLE BORDER=3 CELLPADDING=3 BORDERCOLOR=\"#CCCCCC\" WIDTH=\"336\"><TR>\n";
print "<TH COLSPAN=7 ";
print "BGCOLOR=\"#EEEEEE\">";
print "<FONT COLOR=\"#$textcolor_header\" FACE=\"$TableFont\">";
print "<BIG>$heading</BIG></FONT></TH>\n";
print "</TR><TR>\n";
foreach $key (1..7) {
	print "<TH ";
	print "BGCOLOR=\"#$bgcolor_normal\">";
	print "<FONT COLOR=\"#$textcolor_normal\" FACE=\"$TableFont\" SIZE=\"1\">";
	$xshortday = @shortdays[$key-1];
	print "$xshortday</FONT></TH>";
}
print "\n</TR><TR>\n";
if ($perp_dow > 0) {
	print "<TD COLSPAN=$perp_dow ";
	print "BGCOLOR=\"#$bgcolor_normal\">";
	print "<P>&nbsp</TD>";
}
foreach $key (1..$perp_eom) {
			if (($INPUT{'Year'} == $year)
	&& ($INPUT{'Month'} == $month)
	&& ($key == $mday)) {
		print "<TD VALIGN=TOP ALIGN=RIGHT BGCOLOR=\"#FF0000\">";
		print "<FONT COLOR=\"#$textcolor_today\" FACE=\"$TableFont\"  SIZE=\"1\">";
		print "<A HREF=\"#$key\" CLASS=\"blanco\">$key</a>";
	}
	else {
		print "<TD VALIGN=TOP ALIGN=RIGHT BGCOLOR=\"#EEEEEE\">";
		print "<FONT COLOR=\"#$textcolor_normal\" FACE=\"$TableFont\" SIZE=\"1\">";
		print "<A HREF=\"#$key\" CLASS=\"negro\">$key</a>";
	}
	print "</FONT></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 "BGCOLOR=\"#$bgcolor_normal\">";
	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=\"Anterior\">";
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=\"Siguiente\">";
print "</FORM></TD></TR>\n";


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

&Footer;
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) = @_;
	%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 $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 $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;
			}
		}
	}
}

