#!/usr/bin/perl

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

require "config.idx";

$long = $ENV{'QUERY_STRING'};
if ($long) {
	@pairs=split(/&/,$long);
	foreach $item(@pairs) {
		($name,$content)=split (/=/,$item,2);
		$content=~tr/+/ /;
		$content=~ s/%(..)/pack("c",hex($1))/ge;
		if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$content; }
		else { $INPUT{$name} = $content; }
	}
}
else {
	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;
		if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$value; }
		else { $INPUT{$name} = $value; }
	}
}

use DBI;
$dsn = "DBI:mysql:database=$d_name;host=localhost";
$dbh = DBI->connect($dsn, $d_username, $d_pass, {RaiseError => 1}) || die $DBI::errstr;

unless ($INPUT{action} eq "login") {

@rawCookies = split(/;/,$ENV{'HTTP_COOKIE'});
foreach (@rawCookies) {
	($cookieName, $cookieValue) = split(/=/,$_);
	$cookieName =~ s/ //gi;
	$Cookies{$cookieName}=$cookieValue;
}

if ($Cookies{userid} =~ /\|/) {
($Cook{userid},$Cook{email}) = split(/\|/,$Cookies{userid});
}
else {
($Cook{userid},$Cook{email}) = split(/\-/,$Cookies{userid});
}

if ($Cook{userid} eq "" || $Cook{userid} eq "none") {
print qq~

<html>
<head>
<title>Muncie Real Estate - Search Homes Located in Muncie, Ball State, Albany, Eaton, Dunkirk, Yorktown, Daleville, Gaston, Parker City, Winchester, Farmland, Cowan</title>
<meta NAME="Description" CONTENT="Specializing in Muncie real estate, Ball State, Yorktown, Albany, Eaton, Dunkirk, Hartford City, Winchester and Delaware County.">
<meta http-equiv="set-cookie" content="userid=; expires=; path=/"> 
~;
&header;
print qq~

		<td valign="top" width="581">
		<img src="images/headers/myhomes_account.gif" height="33" width="581" alt="MY HOMES Account"><br>
		<br>
		<table border="0" cellpadding="5" cellspacing="0" width="100%">
		<tr>
		<td valign="top">
			<font face="Arial, Helvetica, sans-serif"><font size="3" color="green"><b>You must have a MyHome account to access this feature.</b></font><br><font size="2" color="black">You may login to your existing account or create a <b>FREE</b> ERA Eagle Real Estate MuncieMLS.com MyHomes account below.</font></font><br><br><br>
<table>
<tr>
<td valign=top width="50%">
<form method=post action=register.idx>
<font size="3" face="Arial"><b>If you are new to ERA Eagle Real Estate MuncieMLS.com you must create a MyHomes account.</b></font><br><br><font size="2">A ERA Eagle Real Estate MuncieMLS.com <b>MyHomes</b> account will enable you to save listings, save searches, view additions and updates to listings, and receive e-mail and wireless notifications when new listings meet your search criteria all for FREE!</font><br><br>
<input type=submit value="Create An Account Now">
</form>
</td>
<td valign="top" width="50%">
<table border="0" cellpadding="8" cellspacing="3" bgcolor="#CCCCCC">
<tr>
	<form method=post action=myhomes.idx>
	<input type=hidden name=action value=login>
	<input type=hidden name=referrer value=$INPUT{referrer}>
<td valign="top" bgcolor="white">
<center><font size="2" face="Arial" color="#000066"><b>ALREADY HAVE A<br><b>MYHOMES</b> ACCOUNT?</b></font><br><font size="2" face="Arial">You may login below</font><br><br></center>
	<table border="0" cellpadding="2" cellspacing="0" width="100%">
	<tr>
	<td align="right">E-Mail Address:</td><td><input type="text" name=Email></td>
	</tr><tr>
	<td align="right">Password:</td><td><input type="password" name=Password></td>
	</tr>
	</table>	
	<center><a href="lostpassword.idx">Lost Your Password? Click Here</a><br><br><input type="submit" value="Login Now"></center>
</td>
</tr>
</form>
</table>


</td>
</tr>
</table>


		</td>
		</tr>
		</table>
</td>

~;
&footer;
exit 0;
}
}

if ($INPUT{action} eq "admin") { &admin; }
elsif ($INPUT{action} eq "view_day") { &view_day; }
elsif ($INPUT{action} eq "email_day") { &email_day; }
elsif ($INPUT{action} eq "addevent") { &addevent; }
elsif ($INPUT{action} eq "saveaddevent") { &saveaddevent; }
elsif ($INPUT{action} eq "deleteevent") { &deleteevent; }
elsif ($INPUT{action} eq "movenextday") { &movenextday; }
elsif ($INPUT{action} eq "savedeleteevent") { &savedeleteevent; }
elsif ($INPUT{action} eq "editevent") { &editevent; }
elsif ($INPUT{action} eq "saveeditevent") { &saveeditevent; }
elsif ($INPUT{action} eq "markdone") { &markdone; }
else { &main; }

sub movenextday {

my $akasth = $dbh->prepare("select todoid,dateto from todo where todoid = '$INPUT{todoid}'");
$akasth->execute;
while (($todoid,$dateto) = $akasth->fetchrow_array()) {
	my $bkasth = $dbh->prepare("select ADDDATE('$dateto', INTERVAL 1 DAY)");
	$bkasth->execute;
	while (($newdate) = $bkasth->fetchrow_array()) {
		@date = ($newdate,$todoid);
		$sql_query = "update todo set dateto = ? where todoid = ?";
		#$sql_query_count++;
		my $gasth = $dbh->do($sql_query, undef, @date);
	$status = "Date Successfully Changed To $newdate";
	&main;
	}
}

}

sub email_day {
$sql_query_count++;
my $akasth = $dbh->prepare("select mlsid,firstname,lastname,email from agents where mlsid = '$INPUT{agent}'");
$akasth->execute;
while (($mlsid,$firstname,$lastname,$email) = $akasth->fetchrow_array()) {

&load_settings;

$builddate = $INPUT{year} . '-' . $INPUT{month} . '-' . $INPUT{day};
$sql_query_count++;
my $kasth = $dbh->prepare("select * from todo where agent = $INPUT{agent} and dateto = ?");
$kasth->execute($builddate);

$get_month_name = $INPUT{month};
$get_month_name = $month_name{$get_month_name};

open(MAIL,"|/usr/sbin/sendmail -t");
print MAIL "To: $email\n";
print MAIL "From: info\@eagle-online.com\n";
print MAIL "Subject: $firstname $lastname To Do List For $INPUT{month}/$INPUT{day}/$INPUT{year}\n";
print MAIL "Content-type: text/html\n\n";
print MAIL qq~
<html>
<head>
<title>Official ERA Eagle Real Estate, Inc. E-Mail</title>
<link rel="stylesheet" TYPE="text/css" href="http://www.munciemls.com/includes/style.css">
</head>
<body bgcolor="white" leftMargin="0" topMargin="0" marginwidth="0" marginheight="0">
<br><br>
<center>
<table border="0" cellspacing="0" cellpadding="0" width="600">
<tr>
<td valign="top" width="600">
<img src="http://www.munciemls.com/images/emailheader.gif"><br>
<table border="0" cellspacing="0" cellpadding="15" width="100%">
<tr>
<td valign="top" width="100%">
<font size="2">
~;

&load_settings;

print MAIL qq~
<font face=Arial size=4>Activities for $get_month_name $INPUT{day}, $INPUT{year}</font>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.munciemls.com/images/todo_markdone.gif"> <b>Completed</b> <img src="http://www.munciemls.com/images/todo_notdone.gif"> <b>Not Completed</b><br><br>
<table border="0" cellspacing="1" cellpadding="3" width="100%" bgcolor="black">
<tr>
<td bgcolor="black">&nbsp;</td>
<td bgcolor="black"><font color="white"><b>Description</b></font></td>
<td bgcolor="black" nowrap><font color="white"><b>Completed By</b></font></td>
</tr>
~;

while (($todoid,$agent,$enteredby,$assignedto,$dateto,$description,$markdone) = $kasth->fetchrow_array()) {

$description =~ s/\n/<br>/g;
$theagent = $agent;

if ($markdone eq "y") {
print MAIL qq~
<tr>
<td bgcolor="#CCCCCC"><img src="http://www.munciemls.com/images/todo_markdone.gif"><br></td>
<td bgcolor="#CCCCCC"><b>$description</b><br></td>
<td bgcolor="#CCCCCC"><b>$assignedto</b><br></td>
</tr>
~;
}
else {
print MAIL qq~
<tr>
<td bgcolor="#FFFF33"><img src="http://www.munciemls.com/images/todo_notdone.gif"><br></td>
<td bgcolor="#FFFF33">$description<br></td>
<td bgcolor="#FFFF33">$assignedto<br></td>
</tr>
~;
}
}

print MAIL qq~
</table>

<br><br><br>
</font>
</td>
</tr>
</table>
You may add new activities by <a href="http://www.munciemls.com/todo.idx?agent=$theagent">clicking here</a>.<br>
<img src="http://www.munciemls.com/images/emailfooter.gif"><br><br><br>
</td>
</tr>
</table>
</center>
</body>
</html>
~;
close(MAIL);

}

$status = "E-Mail Successfully Sent!";

$dbh->disconnect;

&main;

}

sub markdone {

@markdone_vars = ($INPUT{agent},$INPUT{todoid});

$sql_query = "update todo set markdone = 'y' where agent = ? and todoid = ?";
$sql_query_count++;
my $gasth = $dbh->do($sql_query, undef, @markdone_vars);

$status = "To Do Has Successfully Been Marked Done!";

&main;

}




sub saveeditevent {

@update_vars = ($INPUT{dateto},$INPUT{enteredby},$INPUT{assignedto},$INPUT{description},$INPUT{markdone},$INPUT{agent},$INPUT{todoid});

$sql_query = "update todo set dateto = ?, enteredby = ?, assignedto = ?, description = ?, markdone = ? where agent = ? and todoid = ?";
$sql_query_count++;
$rasth = $dbh->do($sql_query, undef, @update_vars);

$status = "Changes saved for item# $INPUT{todoid}!";

&main;


}



sub editevent {

@editevent_vars = ($INPUT{todoid},$INPUT{agent});
$sql_query_count++;
my $hasth = $dbh->prepare("select * from todo where todoid = ? and agent = ?");
$hasth->execute(@editevent_vars);

while (($todoid,$agent,$enteredby,$assignedto,$dateto,$description,$markdone) = $hasth->fetchrow_array()) {

print qq~
<html>
<head>
<style>
.cal_item		{ border-left-style: solid; border-left-color: 202A68; border-left-width: 1px; border-top-style: solid; border-top-color: 202A68; border-top-width: 1px; }
</style>
<title>To Do List Overview >> ERA Eagle MuncieMLS.com</title>
</head>
~;
&header;
&load_settings;

print qq~
<td valign=top>
<img src="images/headers/transaction_manager.gif" width="581" height="33" alt="ERA Eagle Agents To Do List"><Br>
<br><img src="images/trans_sys/todolist.gif" alt="My To Do List and Daily Real Estate Planner"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>edit previous to-do activity below</b><br>

<br><br>
<form method=post action=todo.idx>
<input type=hidden name=action value=saveeditevent>
<input type=hidden name=todoid value="$todoid">
<input type=hidden name=agent value="$agent">
<font face=Verdana size=2>
<b>Entered by:</b> &nbsp; &nbsp; &nbsp; &nbsp;  <input type=text name=enteredby size=20 value="$enteredby"><br>
<b>Assigned to:</b>&nbsp; &nbsp;  <input type=text name=assignedto size=20 value="$assignedto"><br>
<b>Date:</b>&nbsp; &nbsp; &nbsp; &nbsp;  <input type=text name=dateto value="$dateto" size=10><br>
<b>Description:</b><br>
<textarea cols=50 rows=6 name=description>$description</textarea><br>
<b>Mark Done:</b> &nbsp; &nbsp; &nbsp; &nbsp; <input type=text name=markdone value="$markdone" size=1><br><br>
&nbsp; &nbsp; &nbsp; <input type=submit value="  Save Changes  ">
</form>


~;

}

$hasth->finish;

&footer;

}






sub saveaddevent {

$good_date = $INPUT{year} . '-' . $INPUT{month} . '-' . $INPUT{day};


@insert_vars = ($INPUT{agent},$INPUT{enteredby},$INPUT{assignedto},$good_date,$INPUT{description});

$sql_query = "insert into todo values ('',?,?,?,?,?,'n')";
$sql_query_count++;
$yasth = $dbh->do($sql_query, undef, @insert_vars);


$status = "New item added for $good_date";

&main;

}



sub addevent {

$sql_query_count++;
my $basth = $dbh->prepare("select now()");
$basth->execute;

$today_is_datetime = $basth->fetchrow_array();

($today_date,$today_time) = split(/ /,$today_is_datetime);

($t_year,$t_month,$t_day) = split(/\-/,$today_date);

print qq~
<html>
<head>
<style>
.cal_item		{ border-left-style: solid; border-left-color: 202A68; border-left-width: 1px; border-top-style: solid; border-top-color: 202A68; border-top-width: 1px; }
</style>
<title>To Do List Overview >> ERA Eagle MuncieMLS.com</title>
</head>
~;
&header;
&load_settings;

print qq~
<td valign=top>
<img src="images/headers/transaction_manager.gif" width="581" height="33" alt="ERA Eagle Agents To Do List"><Br>
<br><img src="images/trans_sys/todolist.gif" alt="My To Do List and Daily Real Estate Planner"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>enter new to-do activity below</b><br>

<br><br>
<form method=post action=todo.idx>
<input type=hidden name=action value=saveaddevent>
<input type=hidden name=agent value=$INPUT{agent}>
<font face=Verdana size=2>
<b>Entered by:</b> &nbsp; &nbsp; &nbsp; &nbsp;  <input type=text name=enteredby size=20><br>
<b>Assigned to:</b> &nbsp; &nbsp; &nbsp; &nbsp;  <input type=text name=assignedto size=20><br>
<b>Date:</b>&nbsp; &nbsp; &nbsp; &nbsp;  <select name=month><option value="$t_month" selected>$month_name{$t_month}</option><option value="">-----------</option><option value="01">January</option><option value="02">February</option><option value="03">March</option><option value="04">April</option><option value="05">May</option><option value="06">June</option><option value="07">July</option><option value="08">August</option><option value="09">September</option><option value="10">October</option><option value="11">November</option><option value="12">December</option></select> <input type=text name=day size=2 value="$t_day"> <select name=year><option value="$t_year" selected>$t_year</option><option value="">----</option><option value="2004">2004</option><option value="2005">2005</option><option value="2006">2006</option><option value="2007">2007</option><option value="2008">2008</option><option value="2009">2009</option><option value="2010">2010</option></select><br>
<b>Description:</b><br>
<textarea cols=50 rows=6 name=description></textarea><br><br>
&nbsp; &nbsp; &nbsp; <input type=submit value="  Post new item  ">
</form>

</td>
~;
&footer;

}






sub savedeleteevent {
$sql_query_count++;
my $hasth = $dbh->do("delete from todo where todoid = '$INPUT{todoid}'");


$status = "Event# $INPUT{todoid} deleted.";

&main;

}




sub deleteevent {

print qq~
<html>
<head>
<style>
.cal_item		{ border-left-style: solid; border-left-color: 202A68; border-left-width: 1px; border-top-style: solid; border-top-color: 202A68; border-top-width: 1px; }
</style>
<title>To Do List Overview >> ERA Eagle MuncieMLS.com</title>
</head>
~;
&header;
&load_settings;

print qq~
<td valign=top>
<img src="images/headers/transaction_manager.gif" width="581" height="33" alt="ERA Eagle Agents To Do List"><Br>


<br><br><center><font face=Verdana size=2>
Are you sure you want to delete event# $INPUT{todoid}?<br><br>
<a href="todo.idx?action=savedeleteevent&todoid=$INPUT{todoid}&agent=$INPUT{agent}">Yes</a> &nbsp; &nbsp; <a href="todo.idx?agent=$INPUT{agent}">No</a>
</td>
~;
&footer;

}

sub view_day {

&load_settings;

$builddate = $INPUT{year} . '-' . $INPUT{month} . '-' . $INPUT{day};
$sql_query_count++;
my $kasth = $dbh->prepare("select * from todo where agent = $INPUT{agent} and dateto = ?");
$kasth->execute($builddate);

$get_month_name = $INPUT{month};
$get_month_name = $month_name{$get_month_name};

print qq~
<html>
<head>
<style>
.cal_item		{ border-left-style: solid; border-left-color: 202A68; border-left-width: 1px; border-top-style: solid; border-top-color: 202A68; border-top-width: 1px; }
</style>
<title>To Do List Overview >> ERA Eagle MuncieMLS.com</title>
</head>
~;
&header;
&load_settings;

print qq~
<td valign=top>
<img src="images/headers/transaction_manager.gif" width="581" height="33" alt="ERA Eagle Agents To Do List"><Br>
<font face=Arial size=4>Activities for $get_month_name $INPUT{day}, $INPUT{year}</font> &nbsp; &nbsp; &nbsp; <a href="todo.idx?action=addevent&agent=$INPUT{agent}"><img src="images/todo_new.gif" border="0"></a><a href="todo.idx?action=email_day&agent=$INPUT{agent}&day=$INPUT{day}&month=$INPUT{month}&year=$INPUT{year}"><img src="images/todo_email.gif" border="0"></a><br><br>
<table border="0" cellspacing="1" cellpadding="3" width="100%" bgcolor="black">
<tr>
<td bgcolor="black">&nbsp;</td>
<td bgcolor="black"><font color="white"><b>Description</b></font></td>
<td bgcolor="black" nowrap><font color="white"><b>Assigned To</b></font></td>
<td bgcolor="black"><font color="white"><b>Actions</b></font></td>
</tr>
~;

while (($todoid,$agent,$enteredby,$assignedto,$dateto,$description,$markdone) = $kasth->fetchrow_array()) {

$description =~ s/\n/<br>/g;


if ($markdone eq "y") {
print qq~
<tr>
<td bgcolor="#CCCCCC"><img src="images/todo_markdone.gif"><br></td>
<td bgcolor="#CCCCCC"><b>$description</b><br></td>
<td bgcolor="#CCCCCC"><b>$assignedto</b><br></td>
<td bgcolor="#CCCCCC">
<a href="todo.idx?action=editevent&todoid=$todoid&agent=$INPUT{agent}"><font face=Verdana size=1>Edit</font></a><br>
</td>
</tr>
~;
}
else {
print qq~
<tr>
<td bgcolor="#FFFF33"><img src="images/todo_notdone.gif"><br></td>
<td bgcolor="#FFFF33">$description<br></td>
<td bgcolor="#FFFF33">$assignedto<br></td>
<td bgcolor="#FFFF33">
<a href="todo.idx?action=markdone&todoid=$todoid&agent=$INPUT{agent}&day=$INPUT{day}&month=$INPUT{month}&year=$INPUT{year}">Mark Done</a>
<a href="todo.idx?action=editevent&todoid=$todoid&agent=$INPUT{agent}"><font face=Verdana size=1>Edit</font></a><br>
</td>
</tr>
~;
}
}

print qq~
</table>
</td>
~;

$dbh->disconnect;

&footer;

}


sub load_settings {

$month_name{1} = "January";
$month_name{2} = "February";
$month_name{3} = "March";
$month_name{4} = "April";
$month_name{5} = "May";
$month_name{6} = "June";
$month_name{7} = "July";
$month_name{8} = "August";
$month_name{9} = "September";
$month_name{10} = "October";
$month_name{11} = "November";
$month_name{12} = "December";

}


sub main {

&load_settings;

$builddate = $INPUT{year} . '-' . $INPUT{month} . '-' . $INPUT{day};

if ($INPUT{year} && $INPUT{month} && $INPUT{day}) {
$search_for = $builddate;
}
else {
$sql_query_count++;
my $basth = $dbh->prepare("select now()");
$basth->execute;
$today_is_datetime = $basth->fetchrow_array();
($today_date,$today_time) = split(/ /,$today_is_datetime);
$search_for = $today_date;
}
$sql_query_count++;
my $kasth = $dbh->prepare("select * from todo where agent = ? and dateto = ?");
$kasth->execute($INPUT{agent},$search_for);


($pull_year,$pull_month,$pull_day) = split(/\-/,$search_for);
$get_month_name = $month_name{$pull_month};
$get_day = $pull_day;
$get_year = $pull_year;


print qq~
<html>
<head>
<style>
.cal_item	{ border-left-style: solid; border-left-color: 202A68; border-left-width: 1px; border-top-style: solid; border-top-color: 202A68; border-top-width: 1px; }
</style>
<title>To Do List Overview >> ERA Eagle MuncieMLS.com</title>
</head>
~;
&header;
&load_settings;

print qq~
<td valign=top>
<img src="images/headers/transaction_manager.gif" width="581" height="33" alt="ERA Eagle Agents To Do List"><Br>
<br><img src="images/trans_sys/todolist.gif" alt="My To Do List and Daily Real Estate Planner"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>effectively manage your daily real estate business</b><br><br>
<font face=Arial size=4>Activities for $get_month_name $pull_day, $pull_year</font> &nbsp; &nbsp; &nbsp; <a href="todo.idx?action=addevent&agent=$INPUT{agent}"><img src="images/todo_new.gif" border="0"></a><a href="todo.idx?agent=$INPUT{agent}"><img src="images/todo_home.gif" border="0"></a><a href="todo.idx?action=email_day&agent=$INPUT{agent}&day=$pull_day&month=$pull_month&year=$pull_year"><img src="images/todo_email.gif" border="0"></a><br><br>
<table border=0 cellpadding=1 cellspacing=1 width=579>
<tr>
<td>
<font size="4" color="red" face="Arial Narrow"><b>$status</b></font><br>
<br>
</td>
</tr>
<tr><td valign=top>

<table border="0" cellspacing="1" cellpadding="3" width="581" bgcolor="black">
<tr>
<td bgcolor="black">&nbsp;</td>
<td bgcolor="black"><font color="white"><b>Description</b></font></td>
<td bgcolor="black" nowrap><font color="white"><b>Assigned To</b></font></td>
<td bgcolor="black" nowrap><font color="white"><b>Actions</b></font></td>
</tr>
~;

while (($todoid,$agent,$enteredby,$assignedto,$dateto,$description,$markdone) = $kasth->fetchrow_array()) {

$description =~ s/\n/<br>/g;


if ($markdone eq "y") {
print qq~
<tr>
<td bgcolor="#CCCCCC"><img src="images/todo_markdone.gif"><br></td>
<td bgcolor="#CCCCCC"><b>$description</b><br></td>
<td bgcolor="#CCCCCC"><b>$assignedto</b><br></td>
<td bgcolor="#CCCCCC" nowrap>
- <a href="todo.idx?action=editevent&todoid=$todoid&agent=$INPUT{agent}">Edit</a><br>
- <a href="todo.idx?action=deleteevent&todoid=$todoid&agent=$INPUT{agent}">Del</a><br>
</td>
</tr>
~;
}
else {
print qq~
<tr>
<td bgcolor="#FFFF33"><img src="images/todo_notdone.gif"><br></td>
<td bgcolor="#FFFF33">$description<br></td>
<td bgcolor="#FFFF33">$assignedto<br></td>
<td bgcolor="#FFFF33" nowrap>
- <a href="todo.idx?action=editevent&todoid=$todoid&agent=$INPUT{agent}"><font face=Verdana size=1>Edit</font></a><br>
- <a href="todo.idx?action=movenextday&todoid=$todoid&agent=$INPUT{agent}">Move Task Next Day</a><br>
- <a href="todo.idx?action=markdone&todoid=$todoid&agent=$INPUT{agent}&day=$INPUT{day}&month=$INPUT{month}&year=$INPUT{year}">Mark Done</a><br>
- <a href="todo.idx?action=deleteevent&todoid=$todoid&agent=$INPUT{agent}">Delete</a><br>
</td>
</tr>
~;
}
}

print qq~
</table>

<br><br><br>
<table border=0 cellpadding=2 cellspacing=2 width=100%>
<tr>
~;

$counter = 0;

while ($counter <= 1) {

$get_month = &month_count('now()',$counter);
$get_year = &year_count('now()',$counter);
$show_month = $month_name{$get_month};

$counter++;
print qq~<td valign=top width=50%><center>~;
show_calendar_plot($counter);

print qq~
</td>

~;

}



print qq~</tr></table>~;

print "\n</td></tr></table>\n";

&footer;

}




$dbh->disconnect;
















sub month_count {

my($start_month,$end_month) = @_;
$sql_query_count++;
my $tasth = $dbh->prepare("select MONTH($start_month + INTERVAL $end_month MONTH)");
$tasth->execute;

$return_month = $tasth->fetchrow_array();

$tasth->finish;

return $return_month;

}





sub year_count {

my($start_month,$end_month) = @_;
$sql_query_count++;
my $tasth = $dbh->prepare("select YEAR($start_month + INTERVAL $end_month MONTH)");
$tasth->execute;

$return_year = $tasth->fetchrow_array();

$tasth->finish;

return $return_year;

}








sub show_calendar_plot {
my ($month_offset) = @_;

pull_month_events($get_month); 

my $checking_day;
$sql_query_count++;
my $hasth = $dbh->prepare("select DAYOFWEEK('$get_year\-$get_month\-01')");
$hasth->execute;

$first_day = $hasth->fetchrow_array();

$hasth->finish;
$sql_query_count++;
my $jasth = $dbh->prepare("SELECT EXTRACT(DAY FROM SUBDATE( ADDDATE('$get_year\-$get_month\-01', INTERVAL 1 MONTH), INTERVAL DAYOFMONTH( '$get_year\-$get_month\-01' ) DAY))");
$jasth->execute;

$count_to = $jasth->fetchrow_array();

$jasth->finish;

$sql_query_count++;
my $jasth = $dbh->prepare("SELECT EXTRACT(DAY FROM now())");
$jasth->execute;

$today_is = $jasth->fetchrow_array();

$jasth->finish;

$total_spot_count = 0;
$day_in_week_count = 1;
$total_printed_days = 1;
$total_count_for_today = 0;
$start_printing = "no";

$show_cool_month = cool_month("$show_month $get_year");

print qq~<table border=0 cellpadding=3 cellspacing=0 width=286 style="border-style: solid; border-color: 202A68; border-width: 1px;"><tr bgcolor=CCCCCC>\n~;
print qq~<td colspan=7 align=center><font face=Verdana size=2 color=navy><b>$show_cool_month</td></tr><tr bgcolor=202A68>~;
print qq~<td align=center><font face=Verdana style="font-size: 10px; font-style: normal;" color=white><b>Sun</td>~;
print qq~<td align=center><font face=Verdana style="font-size: 10px; font-style: normal;" color=white><b>Mon</td>~;
print qq~<td align=center><font face=Verdana style="font-size: 10px; font-style: normal;" color=white><b>Tue</td>~;
print qq~<td align=center><font face=Verdana style="font-size: 10px; font-style: normal;" color=white><b>Wed</td>~;
print qq~<td align=center><font face=Verdana style="font-size: 10px; font-style: normal;" color=white><b>Thu</td>~;
print qq~<td align=center><font face=Verdana style="font-size: 10px; font-style: normal;" color=white><b>Fri</td>~;
print qq~<td align=center><font face=Verdana style="font-size: 10px; font-style: normal;" color=white><b>Sat</td>~;
print qq~</tr><tr>~;



while ($total_spot_count <= 42) {
	if ($start_printing eq "no") {
		if ($day_in_week_count eq $first_day) {
			$start_printing = "yes";
		}
	}

$total_count_for_today++ if ($start_printing eq "yes");	

if ($counter eq "1" && $total_count_for_today eq "$today_is") {
$this_bg_color = qq~bgcolor="#FFFF33"~;
} elsif ($start_printing eq "no") {
$this_bg_color = qq~bgcolor="F7F7F7"~;
} else { $this_bg_color = ""; }


print qq~<td valign=top height=35 $this_bg_color $add_to_td class="cal_item"><font face=Verdana size=1>&nbsp;~;
if ($start_printing eq "yes" && $total_printed_days <= $count_to) {
$good_total_printed_days = $total_printed_days;
$good_total_printed_days = '0' . $good_total_printed_days if ($good_total_printed_days < 10);
$build_var = $get_year . '-' . $get_month . '-' . $good_total_printed_days;

print qq~<a href="todo.idx?agent=$INPUT{agent}&day=$total_printed_days&month=$get_month&year=$get_year"><font face=Verdana size=1 color="#202A68"><b>$total_printed_days</b></font></a><br>~;

@all_days_vars = split(/\|/, $days_events{$build_var});

$anything_today = "no";

foreach $va (@all_days_vars) {
($eve_eid,$eve_time,$eve_title) = split(/\~\~\~/,$va);
unless ($eve_time eq "00:00:00" || $eve_time eq "") {
$sql_query_count++;
	$kasth = $dbh->prepare("select date_format('0000-00-00 $eve_time', '%l:%i %p')");
	$kasth->execute;
	$new_time = $kasth->fetchrow_array();
	$new_time .= " ";
	$kasth->finish;
}

$anything_today = "yes";

#print qq~$new_time<a href="viewevent.cgi?action=viewevent&eid=$eve_eid"><font face=Verdana size=1 color=202A68>$eve_title</font></a><br><br>~;
}

print qq~<center><font size=2><b>&middot;</b></font></center>~ if ($anything_today eq "yes");

$total_printed_days++;

}
print qq~</td>~;


	if ($total_printed_days > $count_to) {
		$day_in_week_count++;
		while ($day_in_week_count < 8) {
		print qq~<td class="cal_item" bgcolor=F7F7F7>&nbsp;</td>~;
		$day_in_week_count++;
		}
	last;
	}


$day_in_week_count++;
	if ($day_in_week_count eq "8") {
		$day_in_week_count = "1";
		print qq~\n</tr><tr>\n~;
	}
$total_spot_count++;
}





print qq~</tr></table>~;




















}






sub pull_month_events {
my ($month) = @_;
$sql_query_count++;
my $hasth = $dbh->prepare("select * from todo where agent=$INPUT{agent} and MONTH(dateto) = ? order by dateto asc");
$hasth->execute($month);

while (my($todoid,$agent,$enteredby,$assignedto,$dateto,$description,$markdone) = $hasth->fetchrow_array()) {

$description = substr($title, 0, 50);

$days_events{$dateto} .= "$eid~~~$time_start~~~$title|";

}

$hasth->finish;

}





sub cool_month {
my ($month) = @_;

$cool_month = '';

@month_letters = split(//, $month);
foreach $let (@month_letters) {
$cool_month .= "$let  ";
}


$cool_month =~ s/\s\s/ \&nbsp;/g;
$cool_month =~ s/\s\s/ \&nbsp;/g;

return $cool_month;

}





sub load_settings {

$month_name{1} = "January";
$month_name{2} = "February";
$month_name{3} = "March";
$month_name{4} = "April";
$month_name{5} = "May";
$month_name{6} = "June";
$month_name{7} = "July";
$month_name{8} = "August";
$month_name{9} = "September";
$month_name{10} = "October";
$month_name{11} = "November";
$month_name{12} = "December";

}

