#!/usr/bin/perl

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

&checklogin;

if ($INPUT{action} eq "deletesearch") { &deletesearch; }
elsif ($INPUT{action} eq "myclientview") { &myclientview; }
elsif ($INPUT{action} eq "myclientremove") { &myclientremove; }
elsif ($INPUT{action} eq "myclientviewprint") { &myclientviewprint; }
elsif ($INPUT{action} eq "myemailer") { &myemailer; }
elsif ($INPUT{action} eq "myemailer_send") { &myemailer_send; }
elsif ($INPUT{action} eq "agentedit") { &agentedit; }
elsif ($INPUT{action} eq "agentdoedit") { &agentdoedit; }
elsif ($INPUT{action} eq "templateeditor") { &templateeditor; }
elsif ($INPUT{action} eq "massemail") { &massemail; }
elsif ($INPUT{action} eq "view_email") { &view_email; }
elsif ($INPUT{action} eq "overview_notes_show") { &overview_notes_show; }
else { &main; }

sub view_email {
	my $emailsasth = $dbh->prepare("select emaildatetime,emailfrom,subject,message from emailstousers where emailid = '$INPUT{emailid}'");
	$emailsasth->execute;
	while (($emailtimedate,$emailfrom,$subject,$message) = $emailsasth->fetchrow_array()) {
		print qq~
		<b>Sent on $emailtimedate by $emailfrom<br>Subject: $subject</b><br>
		$message
		~;
	}
}

sub massemail {

################### TOP EMAIL PART ###################################################

	my $mesth = $dbh->prepare("select id,name from emailer_templates where type = 'agent' AND accessid = '$INPUT{agent}'");
	$mesth->execute;
	while (($id,$name) = $mesth->fetchrow_array()) {
		$printmessage .= "<option value=\"$id\">$name</option>\n";
	}

	my $bmesth = $dbh->prepare("select id,name from emailer_templates where id = '$INPUT{message}'");
	$bmesth->execute;
	while (($id,$name) = $bmesth->fetchrow_array()) {
		$rmessage = "<option value=\"$id\">$name</option>\n<option value=\"\">-----</option>\n";
	}

	if ($INPUT{searchtype} eq "clients") {
	$rsearchtype = "<option value=\"clients\">Clients</option>\n<option value=\"\">-----</option>\n";
	}
	elsif ($INPUT{searchtype} eq "leads") {
	$rsearchtype = "<option value=\"leads\">Leads</option>\n<option value=\"\">-----</option>\n";
	}

print qq~
<html>
<head>
<title>Eagle Real Estate, Inc. - Web Leads 2007 ver:1.1 - Connect.EagleSold.com</title>
<script language="JavaScript" src="calendar1.js"></script>
<script language="JavaScript" src="calendar2.js"></script>
~;
&header;
print qq~
<td valign="top" width="581">
<img src="images/headers/webleads.gif" height="33" width="581" alt="Web Leads"><br><br>
<a href="webleads.idx?action=templateeditor&agent=$INPUT{agent}">Launch Template Editor</a><br>
<form method="get" action="webleads.idx">
<input type="hidden" name="action" value="massemail">
<input type="hidden" name="agent" value="$INPUT{agent}">
<b><u>Email</u></b> <select name="searchtype" class="smallform">$rsearchtype<option value="clients">Clients</option><option value="leads">Leads</option></select> <b>this message </b></u> 
<select name="message" class="smallform">
$rmessage
$printmessage
</select> <b>and </b>
<select name="do" class="smallform">
<option value="Preview">Preview</option>
</select>
<input type="submit" value="Build Preview" class="smallform">
</form>
~;

################### END EMAIL PART ###################################################



if ($INPUT{searchtype} eq "clients") {
$sql = "select userid,email,firstname,lastname,datestamp,lastlogin,emailsent_stamp from users WHERE myERAAgent = '$INPUT{agent}' AND users.email_block != 'n'";
$csql = "select COUNT(userid) from users WHERE myERAAgent = '$INPUT{agent}' AND users.email_block != 'n'";
}
elsif ($INPUT{searchtype} eq "leads") {
$sql = "select users.userid,users.email,users.firstname,users.lastname FROM users,leadtracking WHERE users.userid = leadtracking.userid and leadtracking.agentassigned = '$INPUT{agent}' and leadtracking.agentassigned != users.myeraagent AND users.email_block != 'n'";
$csql = "select count(users.userid) from users,leadtracking where users.userid = leadtracking.userid and users.myeraagent != leadtracking.agentassigned and leadtracking.agentassigned = '$INPUT{agent}' AND users.email_block != 'n'";
}

my $basth = $dbh->prepare("$csql");
$basth->execute;
while (($countuserid) = $basth->fetchrow_array()) {
$totalcount = $countuserid;
}


if ($INPUT{do} eq "Preview") {
print qq~
<form method="post" action="webleads.idx">
<input type="hidden" name="action" value="massemail">
<input type="hidden" name="searchtype" value="$INPUT{searchtype}">
<input type="hidden" name="agent" value="$INPUT{agent}">
<input type="hidden" name="message" value="$INPUT{message}">
<input type="hidden" name="do" value="Send">
<input type="submit" class="adminform" value="Send E-Mails Each Person Now!">
</form>
<font size="3" color="red">
<b>$totalcount E-Mails Will Be Sent!</b><br><br>
</font>
~;
my $aasth = $dbh->prepare("$sql");
$aasth->execute;

while (($userid,$email,$firstname,$lastname,$datestamp,$lastlogin,$emailsent_stamp) = $aasth->fetchrow_array()) {

	my $masth = $dbh->prepare("select id,template,subject from emailer_templates where id = '$INPUT{message}'");
	$masth->execute;

	while (($id,$template,$subject) = $masth->fetchrow_array()) {
		$template =~ s/\$cemail/$email/g;
		$template =~ s/\$cfirstname/$firstname/g;	
		$template =~ s/\$clastname/$lastname/g;
		$subject =~ s/\$cemail/$email/g;
		$subject =~ s/\$cfirstname/$firstname/g;	
		$subject =~ s/\$clastname/$lastname/g;
		print qq~
		<b>To:</b> $email<br>
		<b>From:</b> $Cook{email}<br>
		<b>Subject:</b> $subject<br>
		<b>Body Message:</b><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">
		$template
		<br><br><br>
		</font>
		</td>
		</tr>
		</table>
		You may block further e-mails from this mailing by <a href="http://www.munciemls.com/unsubscribe.idx?type=email_block&userid=$email">clicking here</a>.<br>
		<img src="http://www.munciemls.com/images/emailfooter.gif"><br><br><br>
		</td>
		</tr>
		</table>
		</center>
~;
	}

}
print qq~
<form method="post" action="webleads.idx">
<input type="hidden" name="action" value="massemail">
<input type="hidden" name="searchtype" value="$INPUT{searchtype}">
<input type="hidden" name="agent" value="$INPUT{agent}">
<input type="hidden" name="message" value="$INPUT{message}">
<input type="hidden" name="do" value="Send">
<input type="submit" class="adminform" value="Send E-Mails Each Person Now!">
</form>
~;
}
elsif ($INPUT{do} eq "Send") {

print qq~
<font size="3" face="Arial">
<b>$totalcount E-Mails Successfully Sent!  <a href="webleads.idx?action=massemail&agent=$INPUT{agent}">Return To Mass Mail Center</a></b>
</font><br>
~;
my $aasth = $dbh->prepare("$sql");
$aasth->execute;

while (($userid,$email,$firstname,$lastname,$datestamp,$lastlogin,$emailsent_stamp) = $aasth->fetchrow_array()) {

	my $masth = $dbh->prepare("select id,template,subject from emailer_templates where id = '$INPUT{message}'");
	$masth->execute;

	while (($id,$template,$subject) = $masth->fetchrow_array()) {
		$template =~ s/\$cemail/$email/g;
		$template =~ s/\$cfirstname/$firstname/g;	
		$template =~ s/\$clastname/$lastname/g;
		$subject =~ s/\$cemail/$email/g;
		$subject =~ s/\$cfirstname/$firstname/g;	
		$subject =~ s/\$clastname/$lastname/g;

			open(MAIL,"|/usr/sbin/sendmail -t");
			print MAIL "To: $email\n";
			#print MAIL "To: ryan.orr\@eaglesold.com\n";
			print MAIL "From: $Cook{email}\n";
			print MAIL "Subject: $subject\n";
			print MAIL "Content-type: text/html\n\n";
			print MAIL qq~
			<html>
			<head>
			<title>Official 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">
				$template
			<br><br><br>
			</font>
			</td>
			</tr>
			</table>
			You may block further e-mails from this mailing by <a href="http://www.munciemls.com/unsubscribe.idx?type=email_block&userid=$email">clicking here</a>.<br>
			<img src="http://www.munciemls.com/images/emailfooter.gif"><br><br><br>
			</td>
			</tr>
			</table>
			</center>
			</body>
			</html>
			~;
			close(MAIL);

			$send_body =qq~
			<html>
			<head>
			<title>Official 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">
				$template
			<br><br><br>
			</font>
			</td>
			</tr>
			</table>
			You may block further e-mails from this mailing by <a href="http://www.munciemls.com/unsubscribe.idx?type=email_block&userid=$email">clicking here</a>.<br>
			<img src="http://www.munciemls.com/images/emailfooter.gif"><br><br><br>
			</td>
			</tr>
			</table>
			</center>
			</body>
			</html>
			~;

			######## RECORD COPY OF EMAIL IN DATABASE #################
			@evariables = ("$userid","$Cook{email}","Mass E-Mail -> $subject","$send_body");
			$esql_query = "INSERT INTO emailstousers values ('',?,now(),?,?,?)";
			my $zzaasth = $dbh->do($esql_query, undef, @evariables);
			###########################################################

		print "  --> Sent To <b>$email</b> - <b>$firstname $lastname</b> and recorded for future lookups.<br>";
	}

}

}
else {
print "<b>You need to build e-mail previews!</b>";
}

$dbh->disconnect;

print "</td></tr></table>";
&footer;
}

sub templateeditor {

print qq~
<html>
<head>
<title>Eagle Real Estate, Inc. - Web Leads 2007 ver:1.1 - Connect.EagleSold.com</title>
<script language="JavaScript" src="calendar1.js"></script>
<script language="JavaScript" src="calendar2.js"></script>
~;
&header;
print qq~
<td valign="top" width="581">
<img src="images/headers/webleads.gif" height="33" width="581" alt="Web Leads"><br><br>
~;

my $aasth = $dbh->prepare("select agentid,mlsid,firstname,lastname,title,phone1name,phone1number,phone2name,phone2number,phone3name,phone3number,phone4name,phone4number,email,homepage,profile,image,leadsystem,display from agents WHERE mlsid = '$INPUT{agent}' LIMIT 0,1");
$aasth->execute;


if ($INPUT{save}) {
	@variables = ($INPUT{name},$INPUT{display},$INPUT{subject},$INPUT{id});
	$sql_query = "update emailer_templates set name = ?, template = ?, subject = ? WHERE id = ?";
	my $aasth = $dbh->do($sql_query, undef, @variables);
	print qq~
	<font size="3" face="Arial"><b>E-Mail Templates</b></font><br>
	<font color="red">Successfully Saved $INPUT{name}</font><br><br>
	<a href="webleads.idx?action=templateeditor&agent=$INPUT{agent}">Return To Template Center</a><br>
	</td>
	</tr>
	</table>
	~;
	&footer;
exit 0;
}
elsif ($INPUT{newsave}) {
	@variables = ($INPUT{agent},$INPUT{type},$INPUT{name},$INPUT{subject},$INPUT{display});
	$sql_query = "INSERT INTO emailer_templates values ('',?,?,?,?,?)";
	my $aasth = $dbh->do($sql_query, undef, @variables);
	print qq~
	<font size="3" face="Arial"><b>E-Mail Templates</b></font><br>
	<font color="red">Successfully Created $INPUT{name}</font><br><br>
	<a href="webleads.idx?action=templateeditor&agent=$INPUT{agent}">Return To Template Center</a><br>
	</td>
	</tr>
	</table>
	~;
	&footer;
exit 0;
}
elsif ($INPUT{launch}) {
	my $masth = $dbh->prepare("select id,accessid,type,name,subject,template from emailer_templates where id = '$INPUT{launch}'");
	$masth->execute;
	print qq~
	<script language="Javascript1.2"><!-- // load htmlarea
	_editor_url = "/htmlarea/";                     // URL to htmlarea files
	var win_ie_ver = parseFloat(navigator.appVersion.split("MSIE")[1]);
	if (navigator.userAgent.indexOf('Mac')        >= 0) { win_ie_ver = 0; }
	if (navigator.userAgent.indexOf('Windows CE') >= 0) { win_ie_ver = 0; }
	if (navigator.userAgent.indexOf('Opera')      >= 0) { win_ie_ver = 0; }
	if (win_ie_ver >= 5.5) {
	 document.write('<scr' + 'ipt src="' +_editor_url+ 'editor.js"');
	 document.write(' language="Javascript1.2"></scr' + 'ipt>');  
	} else { document.write('<scr'+'ipt>function editor_generate() { return false; }</scr'+'ipt>'); }
	// --></script>
	~;
	while (($id,$accessid,$type,$name,$subject,$template) = $masth->fetchrow_array()) {
	print qq~
	<font size="3" face="Arial"><b>Viewing Template $name</b></font><br>
	<b>Special Codes To Insert In Email To Show Actual User Information:<br></b>
	\$cemail \$cfirstname \$clastname <br><br>
	<form method="post" action="webleads.idx">
	<input type="hidden" value="templateeditor" name="action">
	<input type="hidden" value="yes" name="save">
	<input type="hidden" value="$id" name="id">
	<input type="hidden" value="$INPUT{agent}" name="agent">
	<b>Template Filename:</b><br><input type="text" value="$name" name="name" size="30" class="adminform"><br><br>
	<b>Subject:</b><br><input type="text" value="$subject" name="subject" size="80" class="adminform"><br><br>
	<script language="JavaScript1.2" defer>
	editor_generate('display');
	</script>
	<b>Message:</b><br>
	<textarea rows="26" name="display" cols="72">$template</textarea><br>
	<input type="submit" value="Save" class="adminform"><br>
	</form>
	</td>
	</tr>
	</table>
	~;
	}
	&footer;
exit 0;
}
elsif ($INPUT{delete}) {
	my $aasth = $dbh->do("DELETE FROM emailer_templates where id = '$INPUT{delete}'");
	print qq~
	<font size="3" face="Arial"><b>E-Mail Templates</b></font><br>
	<font color="red">Successfully Deleted!</font><br><br>
	<a href="webleads.idx?action=templateeditor&agent=$INPUT{agent}">Return To Template Center</a><br>
	</td>
	</tr>
	</table>
	~;
	&footer;
exit 0;
}



print qq~
<b>Agent E-Mail Template Center<br><br>
~;

if ($INPUT{new}) {
	print qq~
	<script language="Javascript1.2"><!-- // load htmlarea
	_editor_url = "/htmlarea/";                     // URL to htmlarea files
	var win_ie_ver = parseFloat(navigator.appVersion.split("MSIE")[1]);
	if (navigator.userAgent.indexOf('Mac')        >= 0) { win_ie_ver = 0; }
	if (navigator.userAgent.indexOf('Windows CE') >= 0) { win_ie_ver = 0; }
	if (navigator.userAgent.indexOf('Opera')      >= 0) { win_ie_ver = 0; }
	if (win_ie_ver >= 5.5) {
	 document.write('<scr' + 'ipt src="' +_editor_url+ 'editor.js"');
	 document.write(' language="Javascript1.2"></scr' + 'ipt>');  
	} else { document.write('<scr'+'ipt>function editor_generate() { return false; }</scr'+'ipt>'); }
	// --></script>
	
	<script language="JavaScript1.2" defer>
		editor_generate('display');
	</script>

	<font size="3" face="Arial"><b>Creating New $INPUT{new} Template</b></font><br><br><b>Codes To Insert In Emails</b><br><br>  - \$cfirstname = Inserts clients firstname<br>  - \$clastname = Inserts clients lastname<br>  - \$cemail = Inserts clients email<br>
	<form method="post" action="webleads.idx">
	<input type="hidden" name="action" value="templateeditor">
	<input type="hidden" name="newsave" value="1">
	<input type="hidden" name="agent" value="$INPUT{agent}">
	<input type="hidden" name="type" value="agent"><br>
	<b>File Name Of Template:</b><br><input type="text" name="name" class="adminform" size="20"><br><br>
	<b>E-Mail Subject</b><br><input type="text" name="subject" class="adminform" size="80"><br><br>
	<b>Message:</b><br>
	<textarea rows="26" name="display" cols="72"></textarea><br>
	<input type="submit" value="Save New Template" class="adminform"><br>
	</form>
	~;
	}
else {
	print qq~
	<table border="0" cellpadding="3" cellspacing="0" width="100%">
	<tr>
	<td valign="top" width="45%" bgcolor="#FFFF99"><b>Create New</b><br></td>
	<td valign="top" width="5%" valign="middle"><font size="4"><b>OR</b></font><br></td>
	<td valign="top" width="50%" bgcolor="#FFCC33"><b>Edit Existing Templates</b><br></td>
	</tr>
	<tr>
	<td valign="top" width="45%"><a href="webleads.idx?action=templateeditor&new=agent&agent=$INPUT{agent}">New Template</a></td>
	<td valign="top" width="5%">&nbsp;</td>
	<td valign="top" width="50%">
	~;

	my $baasth = $dbh->prepare("select id,name,subject,template from emailer_templates where type = 'agent' AND accessid = '$INPUT{agent}' ORDER BY id ASC");
	$baasth->execute;
	while (($id,$name,$subject,$template) = $baasth->fetchrow_array()) {	
	$templatesfound = "yes";
	print qq~
	 <b>$name</b> <a href="webleads.idx?action=templateeditor&launch=$id&agent=$INPUT{agent}">Edit</a> <a href="webleads.idx?action=templateeditor&delete=$id&agent=$INPUT{agent}">Delete</a><br> 
	~;
	}

	if ($templatesfound ne "yes") {
	print qq~
	<b><font color="red">No Templates Setup Yet</font></b>
	~;
	}

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

print qq~
</td>
</tr>
</table>
~;
&footer;
}


sub agentdoedit {

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

$mlsid = $INPUT{mlsid};
$newmlsid = $INPUT{newmlsid};
$firstname = $INPUT{firstname};
$lastname = $INPUT{lastname};
$title = $INPUT{title};
$phone1name = $INPUT{phone1name};
$phone1number = $INPUT{phone1number};
$phone2name = $INPUT{phone2name};
$phone2number = $INPUT{phone2number};
$phone3name = $INPUT{phone3name};
$phone3number = $INPUT{phone3number};
$phone4name = $INPUT{phone4name};
$phone4number = $INPUT{phone4number};
$email = $INPUT{email};
$homepage = $INPUT{homepage};
$profile = $INPUT{profile};
$image = $INPUT{image};
$enrollment = $INPUT{enrollment};
$display = $INPUT{display};


$firstname =~ s/\'//gi;
$firstname =~ s/\"//gi;
$lastname =~ s/\'//gi;
$lastname =~ s/\"//gi;
$title =~ s/\'//gi;
$title =~ s/\"//gi;
$phone1name =~ s/\'//gi;
$phone1number =~ s/\"//gi;
$phone2name =~ s/\'//gi;
$phone2number =~ s/\"//gi;
$phone3name =~ s/\'//gi;
$phone3number =~ s/\"//gi;
$phone4name =~ s/\'//gi;
$phone4number =~ s/\"//gi;
$email =~ s/\'//gi;
$email =~ s/\"//gi;
$homepage =~ s/\'//gi;
$homepage =~ s/\"//gi;
$profile =~ s/\'//gi;
$profile =~ s/\"//gi;
$image =~ s/\'//gi;
$image =~ s/\"//gi;
$enrollment =~ s/\'//gi;
$enrollment =~ s/\"//gi;

my $aasth = $dbh->do("update agents set mlsid = '$newmlsid', firstname = '$firstname', lastname = '$lastname', title = '$title', phone1name = '$phone1name', phone1number = '$phone1number', phone2name = '$phone2name', phone2number = '$phone2number', phone3name = '$phone3name', phone3number = '$phone3number', phone4name = '$phone4name', phone4number = '$phone4number', email = '$email', homepage = '$homepage', profile = '$profile', image = '$image', leadsystem = '$enrollment', display = '$INPUT{display}', showsales = '$INPUT{showsales}' WHERE agentid = '$INPUT{agentid}'");
print qq~
<html>
<head>
<title>Eagle Real Estate, Inc. - Web Leads 2007 ver:1.1 - Connect.EagleSold.com</title>
<meta http-equiv="Refresh" content="2; URL=http://connect.eaglesold.com">
</head>
<body bgcolor="white">
<font size="3" face="Arial Narrow" color="Red">Please wait, Updating your agent profile!</font><br>
</body>
</html>
~;
}

sub agentedit {

my $aasth = $dbh->prepare("select agentid,mlsid,firstname,lastname,title,phone1name,phone1number,phone2name,phone2number,phone3name,phone3number,phone4name,phone4number,email,homepage,profile,image,leadsystem,display from agents WHERE mlsid = '$INPUT{agent}'  LIMIT 0,1");
$aasth->execute;

print qq~
<html>
<head>
<title>Eagle Real Estate, Inc. - Web Leads 2007 ver:1.1 - Connect.EagleSold.com</title>
<script language="JavaScript" src="calendar1.js"></script>
<script language="JavaScript" src="calendar2.js"></script>
~;
&header;
print qq~
<td valign="top" width="581">
<img src="images/headers/webleads.gif" height="33" width="581" alt="Web Leads"><br><br>
~;

while (($agentid,$mlsid,$firstname,$lastname,$title,$phone1name,$phone1number,$phone2name,$phone2number,$phone3name,$phone3number,$phone4name,$phone4number,$email,$homepage,$profile,$image,$leadsystem,$display) = $aasth->fetchrow_array()) {

if ($phone2name) {
$checkphone2name = "<option value=\"$phone2name\">$phone2name</option>";
}
if ($phone3name) {
$checkphone3name = "<option value=\"$phone3name\">$phone3name</option>";
}
if ($phone4name) {
$checkphone4name = "<option value=\"$phone4name\">$phone4name</option>";
}

print qq~
<script language="Javascript1.2"><!-- // load htmlarea
	_editor_url = "/htmlarea/";                     // URL to htmlarea files
	var win_ie_ver = parseFloat(navigator.appVersion.split("MSIE")[1]);
	if (navigator.userAgent.indexOf('Mac')        >= 0) { win_ie_ver = 0; }
	if (navigator.userAgent.indexOf('Windows CE') >= 0) { win_ie_ver = 0; }
	if (navigator.userAgent.indexOf('Opera')      >= 0) { win_ie_ver = 0; }
	if (win_ie_ver >= 5.5) {
	 document.write('<scr' + 'ipt src="' +_editor_url+ 'editor.js"');
	 document.write(' language="Javascript1.2"></scr' + 'ipt>');  
	} else { document.write('<scr'+'ipt>function editor_generate() { return false; }</scr'+'ipt>'); }
	// --></script>

			<script language="JavaScript1.2" defer>
			editor_generate('profile');
			</script>

<font size="4">Update MuncieMLS.com Profile</font><font size="2">You may update the following form and press the save button at the bottom of this page when you are done updating.<br><br>
<form method="post" action="webleads.idx">
<input type="hidden" name="action" value="agentdoedit">
<input type="hidden" name="mlsid" value="$INPUT{agent}">
<table border="0" cellpadding="3" cellspacing="1" border="0" bgcolor="navy">
<tr>
<td colspan="2" bgcolor="navy"><font color="white"><b>Update $firstname $lastname\'s Agent Directory</b></font></td>
</tr>
<tr>
	<td bgcolor="white">AgentID</td>
	<td bgcolor="white">
		<input type="hidden" name="agentid" value="$agentid"><b>ERA Eagle $agentid</b>
	</td>
</tr>
<tr>
	<td bgcolor="white">MLSID</td>
	<td bgcolor="white">
		<input type="hidden" name="newmlsid" value="$mlsid"><b>MEIAR $mlsid</b>
	</td>
</tr>
<tr>
	<td bgcolor="white">FirstName</td>
	<td bgcolor="white"><input type="text" name="firstname" value="$firstname"></td>
</tr>
<tr>
	<td bgcolor="white">LastName</td>
	<td bgcolor="white"><input type="text" name="lastname" value="$lastname"></td>
</tr>
<tr>
	<td bgcolor="white">Title</td>
	<td bgcolor="white">
		<input type="hidden" name="title" value="$title"><b>$title</b> [ Administrator Must Change ]<br>
	</td>
</tr>
<tr>
	<td bgcolor="white">Phone 1 Type<br>
	</td>
	<td bgcolor="white">
		<select name="phone1name">
		<option value="Office">Office</option>
		</select> 
		<select name="phone1number">
		<option value="765-741-9111">765-741-9111</option>
		</select>
	</td>
</tr>
<tr>
	<td bgcolor="white">Phone 2 Type<br>
	</td>
	<td bgcolor="white">
		<select name="phone2name">
		$checkphone2name
		<option value="">-- No Number --</option>
		<option value="Home Office">Home Office</option>
		<option value="Mobile">Mobile</option>
		<option value="Voicemail">Voicemail</option>
		<option value="Home">Home</option>
		<option value="Pager">Pager</option>
		</select> 
		<input type="text" name="phone2number" value="$phone2number"><br><b>[ Shows As Primary Number On Eagle Flyers ]</b>
	</td>
</tr>
<tr>
	<td bgcolor="white">Phone 3 Type<br>
	</td>
	<td bgcolor="white">
		<select name="phone3name">
		$checkphone3name
		<option value="">-- No Number --</option>
		<option value="Home Office">Home Office</option>
		<option value="Mobile">Mobile</option>
		<option value="Voicemail">Voicemail</option>
		<option value="Home">Home</option>
		<option value="Pager">Pager</option>
		</select> 
		<input type="text" name="phone3number" value="$phone3number">
	</td>
</tr>
<tr>
	<td bgcolor="white">Phone 4 Type<br>
	</td>
	<td bgcolor="white">
		<select name="phone4name">
		$checkphone4name
		<option value="">-- No Number --</option>
		<option value="Home Office">Home Office</option>
		<option value="Mobile">Mobile</option>
		<option value="Voicemail">Voicemail</option>
		<option value="Home">Home</option>
		<option value="Pager">Pager</option>
		</select> 
		<input type="text" name="phone4number" value="$phone4number">
	</td>
</tr>
<tr>
	<td bgcolor="white">E-Mail Address</td>
	<td bgcolor="white">
		<input type="text" name="email" value="$email" size="40">
	</td>
</tr>
<tr>
	<td bgcolor="white">Website Address</td>
	<td bgcolor="white">
		<input type="text" name="homepage" value="$homepage" size="40">
	</td>
</tr>
<tr>
	<td bgcolor="white">Profile About You</td>
	<td bgcolor="white">
		  <textarea rows="15" name="profile" cols="55">$profile</textarea>	
	</td>
</tr>
<tr>
	<td bgcolor="white">Image</td>
	<td bgcolor="white">
		<input type="hidden" name="image" value="$image"><img src="images/agentpics/$image"> [ Administrator Must Change ]
	</td>
</tr>
<tr>
	<td bgcolor="white">Lead Enrollment</td>
	<td bgcolor="white">
		<input type="hidden" name="enrollment" value="$leadsystem"><b>$leadsystem</b> [ Administrator Must Change ]
	</td>
</tr>
<tr>
	<td bgcolor="white">Display Agent</td>
	<td bgcolor="white">
		<input type="hidden" name="display" value="$display"><br><b>Currently $display</b> [ 1 = Showing, 0 = Not Showing, Admin Must Change ]
	</td>
</tr>
<tr>
	<td bgcolor="white">Show Sales On Profile</td>
	<td bgcolor="white">
		<select name="showsales" class="adminform">
		<option value="1">Yes [ Pendings & Solds ]</option>
		<option value="0">No [ Hide ]<option>
		</select>
	</td>
</tr>
<tr>
	<td colspan="2">
		<center>
		<input type="submit" value="Edit Agent Info Now">
		</center>
	</td>
</tr>
~;
}
print qq~
</table>
</form>
</td>
</tr>
</table>
~;
&footer;
}

sub myemailer_send {

$mailto = $INPUT{mailto};
$mailfrom = $INPUT{mailfrom};
$subject = $INPUT{subject};
$message = $INPUT{message};

$temp_message = $message;
$temp_message =~ s/\"/\&quot;/g;

unless ($INPUT{send} eq "yes") {

print qq~
<html>
<head>
<title>Eagle Real Estate, Inc. - Web Leads 2007 ver:1.1 - Connect.EagleSold.com</title>
<script language="JavaScript" src="calendar1.js"></script>
<script language="JavaScript" src="calendar2.js"></script>
~;
&header;
print qq~
<td valign="top" width="581">
<img src="images/headers/webleads.gif" height="33" width="581" alt="Web Leads"><br><br>

<form method="post" action="webleads.idx">
<input type="hidden" name="action" value="myemailer_send">
<input type="hidden" name="send" value="yes">
<input type="hidden" name="agent" value="$INPUT{agent}">
<input type="hidden" name="userid" value="$INPUT{userid}">
<input type="hidden" name="mailto" value="$mailto">
<input type="hidden" name="mailfrom" value="$mailfrom">
<input type="hidden" name="subject" value="$subject">
<input type="hidden" name="message" value="$temp_message">
<input type="submit" value="Send This E-Mail NOW!">
</form>


PREVIEW OF EMAIL BEING SENT<br><br>
To: $mailto<br>
From: $mailfrom<br>
Subject: $subject<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">
$message
<br><br><br>
</font>
</td>
</tr>
</table>
<img src="http://www.munciemls.com/images/emailfooter.gif"><br><br><br>
</td>
</tr>
</table>
</center>

</td>
</tr>
</table>
~;

&footer;

}





if ($INPUT{send} eq "yes") {

$message =~ s/\&quot;/\"/g;

open(MAIL,"|/usr/sbin/sendmail -t");
print MAIL "To: $mailto\n";
#print MAIL "To: ryan.orr\@eaglesold.com\n";
print MAIL "From: $mailfrom\n";
print MAIL "Subject: $subject\n";
print MAIL "Content-type: text/html\n\n";
print MAIL qq~
<html>
<head>
<title>Official 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">
$message
<br><br><br>
</font>
</td>
</tr>
</table>
You may block further e-mails from this mailing by <a href="http://www.munciemls.com/unsubscribe.idx?type=email_block&userid=$mailto">clicking here</a>.<br>
<img src="http://www.munciemls.com/images/emailfooter.gif"><br><br><br>
</td>
</tr>
</table>
</center>
</body>
</html>
~;
close(MAIL);

$send_body =qq~
<html>
<head>
<title>Official 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">
$message
<br><br><br>
</font>
</td>
</tr>
</table>
You may block further e-mails from this mailing by <a href="http://www.munciemls.com/unsubscribe.idx?type=email_block&userid=$mailto">clicking here</a>.<br>
<img src="http://www.munciemls.com/images/emailfooter.gif"><br><br><br>
</td>
</tr>
</table>
</center>
</body>
</html>
~;

		######## RECORD COPY OF EMAIL IN DATABASE #################

		use DBI;
		my $dsn = "DBI:mysql:database=$d_name;host=localhost";
		my $dbh = DBI->connect($dsn, $d_username, $d_pass, {RaiseError => 1}) || die $DBI::errstr;
		@eevariables = ("$INPUT{userid}","$mailfrom","Direct E-Mail -> $subject","$send_body");
		$eesql_query = "INSERT INTO emailstousers values ('',?,now(),?,?,?)";
		my $zzzaasth = $dbh->do($eesql_query, undef, @eevariables);
		###########################################################

print qq~
<html>
<head>
<title>Eagle Real Estate, Inc. - Web Leads 2007 ver:1.1 - Connect.EagleSold.com</title>
<script language="JavaScript" src="calendar1.js"></script>
<script language="JavaScript" src="calendar2.js"></script>
~;
&header;
print qq~
<td valign="top" width="581">
<img src="images/headers/webleads.gif" height="33" width="581" alt="Web Leads"><br><br>
Email Was sent to $mailto<br><br>
<a href="webleads.idx?action=myclientview&agent=$INPUT{agent}&userid=$INPUT{userid}&return=viewclients">Return to client overview</a><br>
</td>
</tr>
</table>
~;
&footer;
}



}


sub myemailer {

	my $baasth = $dbh->prepare("select COUNT(mlsid) from agents where mlsid = '$INPUT{agent}'");
	$baasth->execute;
	while (($mlsidc) = $baasth->fetchrow_array()) {
	$mlsidcount = "$mlsidc";
	}

	my $aasth = $dbh->prepare("select mlsid,firstname,lastname,email from agents where mlsid = '$INPUT{agent}'");
	$aasth->execute;

	my $casth = $dbh->prepare("select email,firstname,lastname from users where userid = '$INPUT{userid}' AND email_block != 'n'");
	$casth->execute;
	while (($email,$firstname,$lastname) = $casth->fetchrow_array()) {
	$emailblocked = "yes";
	$cemail = "$email";
	$cfirstname = "$firstname";
	$clastname = "$lastname";
	}


	if ($emailblocked ne "yes") {
	$cemail = "<b><font size=6 color=red>THIS USER HAS BLOCKED EMAILS FROM MuncieMLS.com!  Your E-Mail Will Not Be Received By This User!!!!!</font></b>";
	}	

print qq~
<html>
<head>
<title>Eagle Real Estate, Inc. - Web Leads 2007 ver:1.1 - Connect.EagleSold.com</title>
<script language="JavaScript" src="calendar1.js"></script>
<script language="JavaScript" src="calendar2.js"></script>
~;
&header;
print qq~
<td valign="top" width="581">
<img src="images/headers/webleads.gif" height="33" width="581" alt="Web Leads"><br><br>
<script language="Javascript1.2"><!-- // load htmlarea
_editor_url = "/htmlarea/";                     // URL to htmlarea files
var win_ie_ver = parseFloat(navigator.appVersion.split("MSIE")[1]);
if (navigator.userAgent.indexOf('Mac')        >= 0) { win_ie_ver = 0; }
if (navigator.userAgent.indexOf('Windows CE') >= 0) { win_ie_ver = 0; }
if (navigator.userAgent.indexOf('Opera')      >= 0) { win_ie_ver = 0; }
if (win_ie_ver >= 5.5) {
 document.write('<scr' + 'ipt src="' +_editor_url+ 'editor.js"');
 document.write(' language="Javascript1.2"></scr' + 'ipt>');  
} else { document.write('<scr'+'ipt>function editor_generate() { return false; }</scr'+'ipt>'); }
// --></script>

<script language="JavaScript1.2" defer>
	editor_generate('message');
</script>
~;
	my $bcasth = $dbh->prepare("select COUNT(id) from emailer_templates where type = 'agent' AND accessid = '$INPUT{agent}'");
	$bcasth->execute;
	while (($idcount) = $bcasth->fetchrow_array()) {
	$counttemp = "$idcount";
	}

	print qq~
	<font size="3" face="Arial"><b>We found $counttemp agent email templates for you.  You may choose office or agent templates below to load into email now!</b></font><br><br>
	~;

#### PULL AVAILABLE TEMPLATES

print qq~
<table border="0" cellpadding="2" cellspacing="1" bgcolor="navy">
<tr>
<td valign="top" bgcolor="navy">
<font color="white" face="Arial" size="2">Choose Your Template To Load</font><br>
</td>
</tr>
~;

	my $acasth = $dbh->prepare("select id,name,subject,template from emailer_templates where type = 'agent' AND accessid = '$INPUT{agent}' OR id = '1' ORDER BY id ASC");
	$acasth->execute;
	while (($id,$name,$subject,$template) = $acasth->fetchrow_array()) {
		print qq~
			<tr>
			<td valign="top" bgcolor="white">
			<a href="webleads.idx?action=myemailer&agent=$INPUT{agent}&userid=$INPUT{userid}&template=$id">$name</a><br>
			</td>
			</tr>
		~;
	}
print qq~
</table>
<br>
<form method="post" action="webleads.idx">
<input type="hidden" name="action" value="myemailer_send">
<input type="hidden" name="mailto" value="$cemail">
<input type="hidden" value="no" name="send">
<input type="hidden" name="agent" value="$INPUT{agent}">
<input type="hidden" name="userid" value="$INPUT{userid}">
~;
if ($mlsidcount > "1") {
print qq~
	<b>E-Mail To:<br> $cemail</b><br><br>
	<b>E-Mail From:</b><br>
	<select name="mailfrom"><br><br>
	~;
}

	while (($mlsid,$firstname,$lastname,$email) = $aasth->fetchrow_array()) {
	
        $afirstname = "$firstname";
        $alastname = "$lastname";
        $aemail = "$email";

	if ($mlsidcount > "1") {
	print qq~
	<option value="$email">$email -- $firstname $lastname</option>
	~;
	}
	else {
	print qq~
		<input type="hidden" value="$email" name="mailfrom">
		<b>E-Mail To:<br>$cfirstname $clastname <$cemail></b><br><br>
		<b>E-Mail From:<br> $email</b><br><br>
~;
	if ($INPUT{template}) {
	my $sbcasth = $dbh->prepare("select id,name,subject,template from emailer_templates where id = '$INPUT{template}'");
	$sbcasth->execute;
	while (($wid,$wname,$wsubject,$wtemplate) = $sbcasth->fetchrow_array()) {
	$wsubject =~ s/\$cfirstname/$cfirstname/g;
	$wsubject =~ s/\$clastname/$clastname/g;
	$wsubject =~ s/\$cemail/$cemail/g;
	$wsubject =~ s/\$afirstname/$afirstname/g;
	$wsubject =~ s/\$alastname/$alastname/g;
	$wsubject =~ s/\$aemail/$aemail/g;
	$pwsubject = "$wsubject";
	}
	}
	else {
	$pwsubject = "Hello $cfirstname $clastname -- From MuncieMLS.com";
	}

print qq~
		<b>Subject:<br></b> <input type="text" name="subject" maxlength="60" size="60" value="$pwsubject"><br><br>
		<b>Message:</b><br> <textarea rows="18" name="message" cols="80">
	~;

if ($INPUT{template}) {
	my $bcasth = $dbh->prepare("select id,name,subject,template from emailer_templates where id = '$INPUT{template}'");
	$bcasth->execute;
	while (($wid,$wname,$wsubject,$wtemplate) = $bcasth->fetchrow_array()) {
	$wtemplate =~ s/\$cfirstname/$cfirstname/g;
	$wtemplate =~ s/\$clastname/$clastname/g;
	$wtemplate =~ s/\$cemail/$cemail/g;
	$wtemplate =~ s/\$afirstname/$afirstname/g;
	$wtemplate =~ s/\$alastname/$alastname/g;
	$wtemplate =~ s/\$aemail/$aemail/g;
	print qq~
	$wtemplate
	~;
	}
}
else {
print qq~
Hello $cfirstname $clastname<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
~;
}
print qq~
</textarea><br>
	~;
	}

	}

if ($mlsidcount > "1") {
print qq~
	</select><br><br>
		<b>Subject:</b><br>
~;

	if ($INPUT{template}) {
	my $sbcasth = $dbh->prepare("select id,name,subject,template from emailer_templates where id = '$INPUT{template}'");
	$sbcasth->execute;
	while (($wid,$wname,$wsubject,$wtemplate) = $sbcasth->fetchrow_array()) {
	$wsubject =~ s/\$cfirstname/$cfirstname/g;
	$wsubject =~ s/\$clastname/$clastname/g;
	$wsubject =~ s/\$cemail/$cemail/g;
	$wsubject =~ s/\$afirstname/$afirstname/g;
	$wsubject =~ s/\$alastname/$alastname/g;
	$wsubject =~ s/\$aemail/$aemail/g;
	$pwsubject = "$wsubject";
	}
	}
	else {
	$pwsubject = "Hello $cfirstname $clastname -- From MuncieMLS.com";
	}

print qq~
		<input type="text" name="subject" maxlength="60" size="60" value="$pwsubject"><br><br>
		<b>Message:</b><br>
		<textarea rows="18" name="message" cols="80">
~;
if ($INPUT{template}) {
	my $bcasth = $dbh->prepare("select id,name,subject,template from emailer_templates where id = '$INPUT{template}'");
	$bcasth->execute;
	while (($wid,$wname,$wsubject,$wtemplate) = $bcasth->fetchrow_array()) {
	$wtemplate =~ s/\$cfirstname/$cfirstname/g;
	$wtemplate =~ s/\$clastname/$clastname/g;
	$wtemplate =~ s/\$cemail/$cemail/g;
	$wtemplate =~ s/\$afirstname/$afirstname/g;
	$wtemplate =~ s/\$alastname/$alastname/g;
	$wtemplate =~ s/\$aemail/$aemail/g;
	print qq~
	$wtemplate
	~;
	}
}
else {
print qq~
Hello $cfirstname $clastname<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
~;
}
}
print qq~
</textarea><br><br>
<input type="submit" Value="Next - Preview Message">
</form>
</td>
</tr>
</table>
~;
&footer;
}







sub myclientremove {

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


if ($INPUT{confirm}) {
my $aasth = $dbh->prepare("UPDATE users set myERAAgent = '' WHERE userid = '$INPUT{userid}'");
$aasth->execute;
my $baasth = $dbh->prepare("UPDATE leadtracking set agentassigned = '' WHERE userid = '$INPUT{userid}'");
$baasth->execute;

print qq~

<html>
<head>
<title>REMOVING CLIENT</title>
<meta http-equiv="Refresh" content="2; URL=http://connect.eaglesold.com/webleads.idx?action=viewclients">
</head>
<body bgcolor="white">
<br><br><br><br>
<center><font size="3" face="Arial" color="Red">Please wait, Removing user as one of your clients or leads.</font><br>You will be redirected back to your MyHomes when finished..</center>
</body>
</html>
~;
}
else {
print qq~
<html>
<head>
<title>REALLY REMOVE CLIENT/LEAD?</title>
</head>
<body bgcolor="white">
<br><br><br><br>
<center><font size="3" face="Arial" color="Red">Are you sure you want to remove?</font><br><a href="webleads.idx?action=myclientremove&agent=$INPUT{agent}&userid=$INPUT{userid}&return=$INPUT{viewclients}&confirm=yes">Yes</a></center>
</body>
</html>
~;
}




}




sub overview_notes_show {

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

if ($INPUT{addnote} eq "1") {

	my $usrdasth = $dbh->prepare("select firstname,lastname from users where userid = '$Cook{userid}'");
	$usrdasth->execute;
	while (($afirstname,$alastname) = $usrdasth->fetchrow_array()) {
	$generateaddedby = "$afirstname $alastname";
	}

	@variables = ($INPUT{agentid},$INPUT{userid},$generateaddedby,$INPUT{notes},$INPUT{followup_date});
	$sql_query = "INSERT INTO leadtracking_notes values ('',?,?,now(),?,?,?)";
	my $aasth = $dbh->do($sql_query, undef, @variables);

}



my $basth = $dbh->prepare("select noteid,agentid,userid,DATE_FORMAT(datetime, '%b %D, %Y at %h\:%i%p'),addedby,note,DATE_FORMAT(followup, '%b %D %Y') from leadtracking_notes WHERE userid = '$INPUT{userid}' ORDER by datetime DESC");
$basth->execute;

print qq~
<html>
<head>
<title>Notes</title>
<link rel="stylesheet" TYPE="text/css" href="includes/style.css">
</head>
<body bgcolor="white" leftMargin="0" topMargin="0" marginwidth="0" marginheight="0">
<table border="0" cellpadding="4" cellspacing="0" width="100%">
~;
$trcolor = "1";
$trcolor2 = "2";

while (($noteid,$agentid,$userid,$datetime,$addedby,$note,$followup) = $basth->fetchrow_array()) {

if ($followup eq "0000-00-00" || $followup eq "") {
$followup = "<font color=\"red\">Not Entered</font>";
}

	if ($trcolor eq $trcolor2) {
	$trcolor = "1";
	$setcolor = "#FFFFFF";
	}
	else {
	$setcolor = "#CCCCFF";
	$trcolor++;
	}
print qq~
<tr bgcolor="$setcolor">
<td valign="top">
<b>$note</b><br><i>Added by $addedby on $datetime.   <b>Next Followup $followup</b></i>
</td>
</tr>
~;
$found = "yes";
}

unless ($found eq "yes") {
print qq~
<tr>
<td>
<font color="red"><b>No notes have been added yet!  You may use the textbox to the right to save notes.</b></font>
</td>
</tr>
~;
}
print qq~
</table>
</body>
</html>
~;
}


sub myclientview {

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

	my $date = $dbh->prepare("select DATE_FORMAT(now(), '%Y-%m-%d')");
	$date->execute;
	while (($newdate) = $date->fetchrow_array()) {
	$today = "$newdate";
	}

print qq~
<html>
<head>
<title>Eagle Real Estate, Inc. - Web Leads 2008 ver:1.2 - Connect.EagleSold.com</title>
<script language="JavaScript" src="calendar1.js"></script>
<script language="JavaScript" src="calendar2.js"></script>
~;
&header;
print qq~
<td valign="top" width="581">
<img src="images/headers/webleads.gif" height="33" width="581" alt="Web Leads"><br><br>
~;

my $aasth = $dbh->prepare("select userid,email,password,firstname,lastname,address,city,state,zipcode,timetomove,realtor,DATE_FORMAT(datestamp, '%b %D %Y'),DATE_FORMAT(lastlogin, '%b %D %Y'),FoundBy,UserType,myERAAgent,Phone,access from users where userid = '$INPUT{userid}'");
$aasth->execute;
while (($userid,$email,$password,$firstname,$lastname,$address,$city,$state,$zipcode,$timetomove,$realtor,$datestamp,$lastlogin,$FoundBy,$UserType,$myERAAgent,$Phone,$access) = $aasth->fetchrow_array()) {

unless ($timetomove) { $timetomove = "<font color=\"red\">unspecified</font>"; } unless ($access) { $access = "<font color=\"red\">unspecified</font>"; } unless ($Foundby) { $Foundby = "<font color=\"red\">unspecified</font>"; } unless ($Phone) { $Phone = "<font color=\"red\">Phone # Not Given</font>"; }

### STRIP PHONE NUMBER ###

$length_Phone = length($Phone);
if ($length_Phone<=8) {
$Phone = "765-$Phone";
}

$phone_stripped = $Phone;
$phone_stripped =~ s/\-//gi;
$phone_stripped =~ s/\ //gi;
$phone_stripped =~ s/\(//gi;
$phone_stripped =~ s/\)//gi;
$phone_stripped =~ s/\_//gi;
$Phone1 = substr("$phone_stripped", 0, 3);
$Phone2 = substr("$phone_stripped", 3, 3);
$Phone3 = substr("$phone_stripped", 6, 4);

if ($Phone1 eq "765" || $Phone1 eq "219" || $Phone1 eq "260" || $Phone1 eq "317" || $Phone1 eq "812") {
$check_dnc = "<a href=\"http://connect.eaglesold.com/nocall.idx?action=search&area=$Phone1&three=$Phone2&four=$Phone3\" target=\"_blank\">[ Check DNC ]</a>";
}


print qq~
<script language="JavaScript" src="calendar1.js"></script>
<script language="JavaScript" src="calendar2.js"></script>
<table border="0" cellpadding="1" cellspacing="3" width="100%">
<tr>
	<td valign="top" width="50%">
		<table border="0" cellpadding="1" cellspacing="0">
		<tr>
		<td valign="top">
			<img src="images/admin/dude.gif"><br>
		</td>
		<td valign="top">
			<font size="2" color="navy">
			<b>$firstname $lastname<br>
			<b>$address<br>
			$city, $state $zipcode</b><br>
			<b>$Phone</b> $check_dnc<br>
			<a href="webleads.idx?action=myemailer&agent=$INPUT{agent}&userid=$INPUT{userid}&template=1">$email</a><br>
			I am a <b>$UserType</b><br>
			</font>
		</td>
		</tr>
		</table>
	</td>
	<td valign="top" width="50%">
		<font size="2">
		Next Transaction Time <b>$timetomove</b><br>		
		Last Login <b>$lastlogin</b><br>
		Register Date <b>$datestamp</b><br><br>
			<table border="0" cellpadding="3" cellspacing="1" width="100%" bgcolor="navy">
			<tr>
			<td valign="top"><font color="white"><b>OPTIONS</b></font><br></td>
			</tr>
			<tr>
				<form method="post" action="webleads.idx">
				<input type="hidden" name="action" value="myclientremove">
				<input type="hidden" name="agent" value="$INPUT{agent}">
				<input type="hidden" name="userid" value="$INPUT{userid}">
				<input type="hidden" name="return" value="$INPUT{viewclients}">
			<td valign="top" bgcolor="white">
				<b>+</b> <a href="webleads.idx?action=myemailer&agent=$INPUT{agent}&userid=$INPUT{userid}&template=1">Send E-Mail To Client</a><br><br>
				<input type="submit" value="Remove as client or lead"><br>
			</td>
				</form>
			</tr>
			</table>
	</td>
</tr>
</table>
<br><br>
	<table border="0" cellpadding="1" cellspacing="0" width="100%">
	<tr>
	<td valign="top" width="65%">
		<table border="0" cellpadding="3" cellspacing="0" width="100%">
			<tr>
				<td valign="top" bgcolor="navy" nowrap><font color="white" size="2"><b>My Notes</b></font><br></td>
				<td valign="top" bgcolor="navy" align="right">
					<table border="0" cellspacing="1" cellpadding="2" bgcolor="white">
						<tr>
							<td valign="top" bgcolor="silver">
								User View Notes Below<br>
							</td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
		<iframe src="webleads.idx?action=overview_notes_show&userid=$INPUT{userid}" width="100%" height="125" name="notes_view" noresize="noresize" scrolling ="yes" frameborder="0" marginwidth="3" marginheight="3"></iframe>
	</td>
	<td valign="top" width="35%">
		<form method="get" action="webleads.idx" target="notes_view" name="leadtracking">
		<input type="hidden" name="action" value="overview_notes_show">
		<input type="hidden" name="agentid" value="$INPUT{agentid}">
		<input type="hidden" name="userid" value="$INPUT{userid}">
		<input type="hidden" name="addnote" value="1">
		<textarea cols="35" rows="8" name="notes" class="form"></textarea><br>
		<input type="text" name="followup_date" value="$today" size="10" class="form"><a href="javascript:cal1.popup();"><img src="images/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date"></a> <b>Choose Followup Date:</b> <br>
		<input type="submit" value="Save Note" class="form">
		</form>		
	</td>
	</tr>
	</table>
~;
}
print qq~
<table border="0" cellspacing="0" cellpadding="2" width="100%">
	<tr>
		<td valign="top" bgcolor="navy" width="100%">
			<b><font color="white" size="2">User Saved Favorite Searches</font></b>
		</td>
	</tr>
</table>
~;
my $basth = $dbh->prepare("select * from search_fav where userid = '$INPUT{userid}' order by favid asc");
$basth->execute;
while (($userid,$favid,$searchstring,$searchname,$searchtype,$status) = $basth->fetchrow_array()) {
$foundsearch = "Yes";
	if ($searchtype eq "res") { $extension = ""; } else { $extension = "\_$searchtype"; }
	if ($status eq "0") { $showstatus = "<font color=\"red\"><b>Stopped</b></font>"; $showtoggle = "Start"; $showstyle = "green"; }
	else { $showstatus = "<font color=\"green\"><b>Running</b></font>"; $showtoggle = "Stop"; $showstyle = "red"; } print qq~<b>$searchname | $favid | $showstatus</b><br><br>~;}

unless ($foundsearch) { print qq~ <font color="red"><b>No Saved Searches</b></font><br><br> ~; }

print qq~
<table border="0" cellspacing="0" cellpadding="2" width="100%">
	<tr>
		<td valign="top" bgcolor="navy" width="100%">
			<b><font color="white" size="2">User Saved Favorite Homes</font></b><br>
		</td>
	</tr>
</table>
<table border="0" cellpadding="2" cellspacing="0" width="579">
<tr>
~;
my $casth = $dbh->prepare("select * from home_fav where userid = '$INPUT{userid}' order by favid asc");
$casth->execute;
$startcount = "1";
while (($userid,$favid,$mlsnumber,$listingtype,$notes) = $casth->fetchrow_array()) {
	$jasth = $dbh->prepare("select AddressNumber,AddressDirection,AddressStreet,City,Zip,State,Remarks from listings_$listingtype where MLSNUM = '$mlsnumber'");
	$jasth->execute;
	while (($AddressNumber,$AddressDirection,$AddressStreet,$City,$Zip,$state,$remarks_public) = $jasth->fetchrow_array()) {

$foundhomes = "yes";
if ($startcount eq "2") { $startcount = "1"; $bgcolor = "FFFF99"; }
else { $startcount++; $bgcolor = "white"; } $notes =~ s/CHR1/\'/gi; $notes =~ s/CHR2/\"/gi; if ($listingtype eq "res") { $extension = ""; }
elsif ($listingtype eq "comm") { $extension = "\_commercial"; } elsif ($listingtype eq "inv") { $extension = "\_investment"; } elsif ($listingtype eq "lan") { $extension = "\_land"; }
print qq~
<tr bgcolor="$bgcolor">
	<td valign="top">
		$AddressNumber $AddressDirection $AddressStreet<br>
	</td>
	<td valign="top">
		$City, $state $Zip<br>
	</td>
	<td valign="top">
		MLS #<a href="http://www.munciemls.com/showdetails$extension.idx?mlsnumber=$mlsnumber" target="_blank">$mlsnumber</a>
	</td>
	</tr>
	<tr bgcolor="$bgcolor">
	<td valign="top" width="275" colspan="3">
		<b>User Notes: $notes</b><br>
	</td>
</tr>
~;
}
}
unless ($foundhomes) {
print qq~
<tr>
<td valign="top">
<font color="red"><b>No Saved Homes</b></font><br>
</td>
</tr>
~;
}
print qq~
</table>
<br><br>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
	<tr>
		<td valign="top" bgcolor="navy" width="100%" colspan="3">
			<b><font color="white" size="2">E-Mails Sent History Since Feb 14th, 2005</font></b>
		</td>
	</tr>
	<tr>
		<td><b>Date/Time Sent</b></td>
		<td><b>From</b></td>
		<td><b>Subject</b></td>
	</tr>
~;
my $emailsasth = $dbh->prepare("select emailid,emaildatetime,emailfrom,subject from emailstousers where userid = '$INPUT{userid}' order by emailid desc");
$emailsasth->execute;
while (($emailid,$emailtimedate,$emailfrom,$subject) = $emailsasth->fetchrow_array()) {
print qq~
<tr>
<td valign="top">Sent $emailtimedate</td>
<td valign="top">$emailfrom</td>
<td valign="top"><a href="webleads.idx?action=view_email&emailid=$emailid" target="_blank">$subject</a></td>
</tr>
~;
}
print qq~
</table>
~;
print qq~
</td>
			<script language="JavaScript">
			<!-- 
				var cal1 = new calendar1(document.forms['leadtracking'].elements['followup_date']);
				cal1.year_scroll = true;
				cal1.time_comp = false;

			//-->
			</script>
</td>
</tr>
</table>
~;
&footer;

$dbh->disconnect;
}



### UPDATED TO CONNECT.EAGLE ####

sub main {

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


print qq~
<html>
<head>
<title>Eagle Real Estate, Inc. - Web Leads 2007 ver:1.1 - Connect.EagleSold.com</title>
<script language="JavaScript" src="calendar1.js"></script>
<script language="JavaScript" src="calendar2.js"></script>
~;
&header;
print qq~
<td valign="top" width="581">
<img src="images/headers/webleads.gif" height="33" width="581" alt="Web Leads"><br><br>
~;

$agentid = $INPUT{agent};

unless ($agentid < "1") {
my $dasth = $dbh->prepare("select count(users.userid) from users,leadtracking where users.userid = leadtracking.userid and users.myeraagent != leadtracking.agentassigned and leadtracking.agentassigned = '$agentid' order by lastname desc");
$dasth->execute;
while (($leadid) = $dasth->fetchrow_array()) {
$leadcount = "$leadid";
}


my $wasth = $dbh->prepare("");

#my $easth = $dbh->prepare("select COUNT(userid) FROM users WHERE myeraagent = '$agentid'");
my $easth = $dbh->prepare("select count(users.userid) from users,leadtracking where users.userid = leadtracking.userid and users.myeraagent = leadtracking.agentassigned and users.myeraagent = '$agentid' order by lastname desc");
$easth->execute;
while (($userid) = $easth->fetchrow_array()) {
$useridcount = "$userid";
}


	if ($INPUT{action} eq "viewclients") {
	$clientbutton = "<a href=\"webleads.idx?agent=$INPUT{agent}\">Hide</a><img src=\"images/myhomes/hide.gif\">";
	}
	else {
	$clientbutton = "<a href=\"webleads.idx?action=viewclients&agent=$INPUT{agent}\">Show</a><img src=\"images/myhomes/show.gif\">";
	}

	if ($INPUT{action} eq "viewleads") {
	$leadbutton = "<a href=\"webleads.idx?agent=$INPUT{agent}\">Hide</a><img src=\"images/myhomes/hide.gif\">";
	}
	else {
	$leadbutton = "<a href=\"webleads.idx?action=viewleads&agent=$INPUT{agent}\">Show</a><img src=\"images/myhomes/show.gif\">";
	}


print qq~
<table border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="white">
	<tr>
		<td width="100%" colspan="2" bgcolor="navy"><b><font size="3" color="white">$savefirstname\'s Agent Center</font></b><br>
			<table border="0" cellpadding="3" cellspacing="0" width="100%" bgcolor="white">
			<tr>
			<td valign="top">
				<b>My Clients</b><br><font size="3" color="navy"><a href="webleads.idx?action=viewclients&agent=$INPUT{agent}">( $useridcount )</a> $clientbutton</font><br>
			</td>
			<td valign="top">
				<b>My Leads</b><br><font size="3" color="navy"><a href="webleads.idx?action=viewleads&agent=$INPUT{agent}">( $leadcount )</a> $leadbutton</font><br>
			</td>
			<td valign="top">
~;

my $gidasth = $dbh->prepare("select agentid,mlsid from agents where mlsid = '$agentid' LIMIT 0,1");
$gidasth->execute;
while (($realagentid,$mlsid) = $gidasth->fetchrow_array())  {
print qq~
				<b>Agent Options</b><br><b><img src="images/myhomes/show.gif"><a href="webleads.idx?action=agentedit&mlsid=$realagentid">Edit My Profile</a></b><br>
~;
}
print qq~
				<b><img src="images/myhomes/show.gif"><a href="webleads.idx?action=massemail&agent=$INPUT{agent}">MASS E-Mailer</a></b><br>
				<b><img src="images/myhomes/show.gif"><a href="webleads.idx?action=templateeditor&agent=$INPUT{agent}">E-Mail Templates</a></b><br>
			</td>
			</tr>
			</table>
		</td>
	</tr>
~;
	if ($INPUT{action} eq "viewclients") {

		if ($INPUT{sortby}) {
		$sortby_value="$INPUT{sortby}";
		}
		else {
		$sortby_value = "DESC"
		}

		if ($INPUT{orderby}) {
		$orderby_value="$INPUT{orderby}";
		}
		else {
		$orderby_value = "users.lastlogin"
		}			
		
		print qq~
		<tr>
		<form method="get" action="webleads.idx">
		<input type="hidden" name="action" value="$INPUT{action}">
		<input type="hidden" name="agent" value="$INPUT{agent}">
		<td colspan="3" bgcolor="green">
		<font color="white">		
		Search Filter:  <b>Order By 
		<select name="orderby" class="gform">
		<option value="$orderby_value">$orderby_value</option>
		<option value="users.lastname">---</option>
		<option value="users.lastname">Lastname</option>
		<option value="users.datestamp ">Date Joined</option>
		<option value="users.lastlogin">Last Login</option>
		</select> in <select name="sortby" class="gform">
		<option value="$sortby_value">$sortby_value</option>
		</option>
		<option value="DESC">---</option>
		<option value="DESC">Descending</option>
		<option value="ASC">Ascending</select> order <input type="submit" value="Update" class="gform"><br>
		</font>
		</td>
		</form>
		</tr>
		<tr>
		<td colspan="3" bgcolor="navy"><font color="white" size="2"><b>Users Logged In within the last week.</b></font></td>
		</tr>
		~;
		print "<tr>\n<td valign=\"top\" colspan=\"3\">\n";
		my $aeasth = $dbh->prepare("select users.userid,users.firstname,users.lastname,users.email FROM users,leadtracking WHERE users.userid = leadtracking.userid and users.myeraagent = '$agentid' and leadtracking.agentassigned = users.myeraagent AND (to_days(now()) - to_days(users.lastlogin)) < '7' order by $orderby_value $sortby_value");
		$aeasth->execute;

		$trcount = "1";
		$totalperrow = "3";
		$rowcolor1 = "1";
		$rowcolor2 = "2";

		print qq~
		<table border="0" cellpadding="2" cellspacing="1" width="100%" bgcolor="gray">
		<tr bgcolor="#FFFFFF">
		~;
		$userjscount = "1";
		while (($userid,$firstname,$lastname,$email) = $aeasth->fetchrow_array()) {

			if ($rowcolor1 eq $rowcolor2) {
			$color = "#FFFFFF";
			$rowcolor1 = "1";
			}
			else {
			$color = "yellow";
			$rowcolor1++;
			}
		print qq~
			<td nowrap><b><a href="webleads.idx?action=myclientview&userid=$userid&return=$INPUT{action}&agent=$INPUT{agent}">$lastname, $firstname</a></b><br></td>
		~;

		if ($trcount eq $totalperrow) {
		print "</tr><tr bgcolor=\"$color\">";
		$trcount = "1";
		}
		else {
		$trcount++;
		}

		}
		print qq~
		</tr>
		</table>
		</td></tr>
		<tr>
		<td colspan="3" bgcolor="navy"><font color="white" size="2"><b>Users not logged in within the last week</b></font></td>
		</tr>
		~;
		print "<tr>\n<td valign=\"top\" colspan=\"3\">\n";
		my $easth = $dbh->prepare("select users.userid,users.firstname,users.lastname,users.email FROM users,leadtracking WHERE users.userid = leadtracking.userid and users.myeraagent = '$agentid' and leadtracking.agentassigned = users.myeraagent  AND (to_days(now()) - to_days(users.lastlogin)) > '7' order by $orderby_value $sortby_value");
		$easth->execute;

		$trcount = "1";
		$totalperrow = "3";
		$rowcolor1 = "1";
		$rowcolor2 = "2";

		print qq~
		<table border="0" cellpadding="2" cellspacing="1" width="100%" bgcolor="gray">
		<tr bgcolor="#FFFFFF">
		~;
		$userjscount = "1";
		while (($userid,$firstname,$lastname,$email) = $easth->fetchrow_array()) {

			if ($rowcolor1 eq $rowcolor2) {
			$color = "#FFFFFF";
			$rowcolor1 = "1";
			}
			else {
			$color = "#99CCFF";
			$rowcolor1++;
			}
		print qq~
			<td nowrap><b><a href="webleads.idx?action=myclientview&agent=$INPUT{agent}&userid=$userid&return=$INPUT{action}">$lastname, $firstname</a></b><br></td>
		~;

		if ($trcount eq $totalperrow) {
		print "</tr><tr bgcolor=\"$color\">";
		$trcount = "1";
		}
		else {
		$trcount++;
		}

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

	}

	if ($INPUT{action} eq "viewleads") {

		if ($INPUT{sortby}) {
		$sortby_value="$INPUT{sortby}";
		}
		else {
		$sortby_value = "DESC"
		}

		if ($INPUT{orderby}) {
		$orderby_value="$INPUT{orderby}";
		}
		else {
		$orderby_value = "users.lastlogin"
		}		
		
		print qq~
		<tr>
		<form method="get" action="webleads.idx">
		<input type="hidden" name="action" value="$INPUT{action}">
		<input type="hidden" name="agent" value="$INPUT{agent}">
		<td colspan="3" bgcolor="green">
		<font color="white">		
		Search Filter:  <b>Order By 
		<select name="orderby" class="gform">
		<option value="$orderby_value">$orderby_value</option>
		<option value="users.lastname">---</option>
		<option value="users.lastname">Lastname</option>
		<option value="users.datestamp ">Date Joined</option>
		<option value="users.lastlogin">Last Login</option>
		</select> in <select name="sortby" class="gform">
		<option value="$sortby_value">$sortby_value</option>
		</option>
		<option value="DESC">---</option>
		<option value="DESC">Descending</option>
		<option value="ASC">Ascending</select> order <input type="submit" value="Update" class="gform"><br>
		</font>
		</td>
		</form>
		</tr>
		<tr>
		<td colspan="3" bgcolor="navy"><font color="white" size="2"><b>Leads Logged In within the last week.</b></font></td>
		</tr>
		~;
		print "<tr>\n<td valign=\"top\" colspan=\"3\">\n";
		my $gaeasth = $dbh->prepare("select users.userid,users.firstname,users.lastname,users.email FROM users,leadtracking WHERE users.userid = leadtracking.userid and leadtracking.agentassigned = '$agentid' and leadtracking.agentassigned != users.myeraagent  AND (to_days(now()) - to_days(users.lastlogin)) < '7' order by $orderby_value $sortby_value");
		$gaeasth->execute;

		$trcount = "1";
		$totalperrow = "3";
		$rowcolor1 = "1";
		$rowcolor2 = "2";

		print qq~
		<table border="0" cellpadding="2" cellspacing="1" width="100%" bgcolor="gray">
		<tr bgcolor="#FFFFFF">
		~;
		$userjscount = "1";
		while (($l_userid,$l_firstname,$l_lastname,$l_email) = $gaeasth->fetchrow_array()) {

			if ($rowcolor1 eq $rowcolor2) {
			$color = "#FFFFFF";
			$rowcolor1 = "1";
			}
			else {
			$color = "yellow";
			$rowcolor1++;
			}
		print qq~
			<td nowrap><b><a href="webleads.idx?action=myclientview&agent=$INPUT{agent}&userid=$l_userid&return=$INPUT{action}">$l_lastname, $l_firstname</a></b><br></td>
		~;

		if ($trcount eq $totalperrow) {
		print "</tr><tr bgcolor=\"$color\">";
		$trcount = "1";
		}
		else {
		$trcount++;
		}

		}
		print qq~
		</tr>
		</table>
		</td></tr>
		<tr>
		<td colspan="3" bgcolor="navy"><font color="white" size="2"><b>Leads not logged in within the last week</b></font></td>
		</tr>
		~;
		print "<tr>\n<td valign=\"top\" colspan=\"3\">\n";

		my $gasth = $dbh->prepare("select users.userid FROM users,leadtracking WHERE users.userid = leadtracking.userid and leadtracking.agentassigned = '$agentid' and leadtracking.agentassigned != users.myeraagent AND (to_days(now()) - to_days(users.lastlogin)) > '7' order by $orderby_value $sortby_value");
		$gasth->execute;

		$trcount = "1";
		$totalperrow = "3";
		$rowcolor1 = "1";
		$rowcolor2 = "2";

		print qq~
		<table border="0" cellpadding="2" cellspacing="1" width="100%" bgcolor="gray">
		<tr bgcolor="#FFFFFF">
		~;
		$userjscount = "1";
		while (($userid) = $gasth->fetchrow_array()) {
			my $hasth = $dbh->prepare("select firstname,lastname FROM users WHERE userid = '$userid'");
			$hasth->execute;
			while (($firstname,$lastname) = $hasth->fetchrow_array()) {

			if ($rowcolor1 eq $rowcolor2) {
			$color = "#FFFFFF";
			$rowcolor1 = "1";
			}
			else {
			$color = "#99CCFF";
			$rowcolor1++;
			}
		print qq~
			<td nowrap><b><a href="webleads.idx?action=myclientview&agent=$INPUT{agent}&userid=$userid&return=$INPUT{action}">$lastname, $firstname</a></b><br></td>
		~;

		if ($trcount eq $totalperrow) {
		print "</tr><tr bgcolor=\"$color\">";
		$trcount = "1";
		}
		else {
		$trcount++;
		}

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

	}

print qq~
			<tr>
			<td valign="top" colspan="3">
<b>Client/Prospect Follow Ups</b><br>
		~;

	my $dateaasth = $dbh->prepare("select DATE_FORMAT(DATE_ADD(now(), INTERVAL -7 DAY), '%Y-%m-%d')");
	$dateaasth->execute;
	while (($newbackdate) = $dateaasth->fetchrow_array()) {
	$findsdate = "$newbackdate";
	}

	my $datebasth = $dbh->prepare("select DATE_FORMAT(DATE_ADD(now(), INTERVAL 7 DAY), '%Y-%m-%d')");
	$datebasth->execute;
	while (($newstartdate) = $datebasth->fetchrow_array()) {
	$findldate = "$newstartdate";
	}

my $basth = $dbh->prepare("select noteid,userid,DATE_FORMAT(datetime, '%b %D %Y'),addedby,note,DATE_FORMAT(followup, '%b %D %Y') from leadtracking_notes WHERE agentid = '$agentid' and (followup BETWEEN '$findsdate' AND '$findldate') ORDER by datetime DESC");
$basth->execute;

$trcolor = "1";
$trcolor2 = "2";

while (($noteid,$userid,$datetime,$addedby,$note,$followup) = $basth->fetchrow_array()) {

	my $usrdasth = $dbh->prepare("select firstname,lastname from users where userid = '$userid'");
	$usrdasth->execute;
	while (($pc_firstname,$pc_lastname) = $usrdasth->fetchrow_array()) {

print qq~
$followup | <a href="webleads.idx?action=myclientview&agent=$INPUT{agent}&userid=$userid&return=viewleads">$pc_firstname $pc_lastname :: $note</a><br>
~;
$found = "yes";
}
}
unless ($found eq "yes") {
print qq~
<b><font color="red">No Follow ups Found</font></b><br>
~;
}

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




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

&footer;

}
