#!/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; }
	}
}


&checklogin;

if ($INPUT{action} eq "deletesearch") { &deletesearch; }
elsif ($INPUT{action} eq "login") { &login; }
elsif ($INPUT{action} eq "logoff") { &logout; }
else { &main; }

sub login {

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 $fasth = $dbh->prepare("select userid from users where email = '$INPUT{Email}' AND password = PASSWORD('$INPUT{Password}') AND agentid != ''");
$fasth->execute;


while (($uid) = $fasth->fetchrow_array()) {
$userid = $uid;

my $uasth = $dbh->do("update users set lastlogin = now() where userid = '$userid'");
my $pasth = $dbh->prepare("select DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 1 DAY), '%a\, %d\-%b\-%Y %T')");
$pasth->execute;

while (($nextmonthsdate) = $pasth->fetchrow_array()) {
$setcookiedate = "$nextmonthsdate";
}


print "Set-cookie: userid=$userid\|$INPUT{Email}; expires=$setcookiedate GMT\n";
print "Content-type: text/html\n\n";

print qq~

<html><head><title>Loading Your MY HOMES Data...</title>
<meta http-equiv="Refresh" content="3; URL=$theurl">
</head>
<body>
<br><br><br><br><br>
<center>
<table border="0" cellpadding="12" cellspacing="1" bgcolor="navy">
<tr>
<td valign="middle" bgcolor="white">
<br>
<center>
<img src="images/StatusBar4.gif" alt="Loading"><br>
<img src="images/nowloading.gif"><br>
</center>
</td>
</tr>
</table>
</center>
~;

$dbh->disconnect;

exit 0;

}

print "Set-cookie: userid=\n";
print "Content-type: text/html\n\n";

print qq~
<html><head><title>Connect.EagleSold.com</title>


~;

&header;

print qq~

		<td valign="top" width="581">
		<br>
		<table border="0" cellpadding="5" cellspacing="0" width="100%">
		<tr>
		<td valign="top">
		<font face="Arial, Helvetica, sans-serif"><font size="4" color="Red">Sorry, An Authorization Error Has Occured....</b></font></font><br><br>
<center>
<table border="0" cellspacing="1" cellpadding="3" bgcolor="#CC0000">
	<form method=post action=index.idx>
	<input type=hidden name=action value=login>
<tr>
<td valign="top" colspan="2" bgcolor="white"><center><font size="3" color="maroon" face="Arial"><b>Connect.EagleSold.com</b></font><br><b><i>Access Center</i></b></center></td>
</tr>
<tr>
<td valign="middle" align="right" bgcolor="white"><b>Login</b></td>
<td valign="middle" bgcolor="white"><input type="text" name="Email" value="$INPUT{Email}" size="30" class="form"></td>
</tr>
<tr>
<td valign="middle" align="right" bgcolor="white"><b>Password</b></td>
<td valign="middle" bgcolor="white"><input type="password" name="Password" size="30" class="form"></td>
</tr>
<tr>
<td valign="top">&nbsp;</td>
<td valign="top"><input type="submit" value="Login" class="form"><br></td>
</tr>
</table>
</center>
		</td>
		</tr>
		</table>
		</td>




~;


&footer;

}

sub logout {


print "Set-cookie: userid=\n";
print "Content-type: text/html\n\n";

print qq~


<html><head><title>Logging out...</title>
<meta http-equiv="Refresh" content="2; URL=index.idx">
</head>
<body>
<br><br><br><br><br>
<center>
<table border="0" cellpadding="12" cellspacing="1" bgcolor="navy">
<tr>
<td valign="middle" bgcolor="white">
<br>
<center><font face=Arial size=3>
<img src="images/StatusBar4.gif" alt="Loading"><br>
You Are Now Being Logged out....
</center>
</td>
</tr>
</table>
</center>
~;
}

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;

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

my $aasth = $dbh->prepare("select id,agent,status,type,listingclass,mlsnumber,addressnumber,addressdirection,addressstreet,city,zip,listingprice,showinginstructions,listingdate,expiredate from trans_board WHERE (status = 'Active' or status = 'Pending') and (type = 'Listing' or type = 'BSP')  ORDER BY addressstreet ASC");
$aasth->execute;
$trcolor = "1";
$trcolor2 = "2";
while (($id,$agent,$status,$type,$class,$mlsnumber,$addressnumber,$addressdirection,$addressstreet,$city,$zip,$listingprice,$showinginstructions,$listingdate,$expiredate  ) = $aasth->fetchrow_array()) {

my $casth = $dbh->prepare("select firstname,lastname from agents WHERE mlsid = '$agent' order by agentid ASC LIMIT 0,1");
$casth->execute;
while (($firstname,$lastname) = $casth->fetchrow_array()) {
$agentname = "$firstname $lastname";
}


my $basth = $dbh->prepare("select Bedrooms,FullBaths,HalfBaths,TotalSqFt,YearBuilt,EstimatedAnnualTax from listings_res3 WHERE MLSNUM = '$mlsnumber'");
$basth->execute;
while (($bedrooms,$fullbaths,$halfbaths,$sqft,$yrbuilt,$esttaxes) = $basth->fetchrow_array()) {
$beds = $bedrooms;
$fbaths = $fullbaths;
$hbaths = $halfbaths;
$tsqft = $sqft;
$yrb = $yrbuilt;
$taxes = $esttaxes;
}


###RUN CHECKS
unless ($status) { $status = "N/A"; }
unless ($type) { $type = "N/A"; }
unless ($agent) { $agent = "N/A"; }
unless ($class) { $class = "N/A"; }
unless ($beds) { $beds = "N/A"; }
unless ($fbaths || $hbaths) { $baths = "N/A"; } else { $baths = "$fbaths Full / $hbaths Half"; }
unless ($tsqft) { $tsqft = "N/A"; }
unless ($yrb) { $yrb = "N/A"; }
unless ($taxes) { $taxes = "N/A"; }

	if ($trcolor eq $trcolor2) {
	$trcolor = "1";
	$setcolor = "#FFFF99";
	}
	else {
	$setcolor = "#FFFFFF";
	$trcolor++;
	}

$print_list .=qq~
<tr bgcolor="$setcolor">
<td nowrap>$id</td>
<td nowrap>$status</td>
<td nowrap>$type</td>
<td nowrap>$class</td>
<td nowrap>$mlsnumber</td>
<td nowrap>$addressnumber $addressdirection $addressstreet</td>
<td nowrap>$city</td>
<td nowrap>\$$listingprice</td>
<td nowrap>$beds</td>
<td nowrap>$baths</td>
<td nowrap>$tsqft</td>
<td nowrap>$yrb</td>
<td nowrap>$taxes</td>
<td nowrap>$agentname ($agent)</td>
<td nowrap>$listingdate</td>
<td nowrap>$expiredate</td>
<td nowrap>$showinginstructions</td>
</tr>
~;
##CLEAR OUT VARIBLES
$agentname = "";
$beds = "";
$fbaths = "";
$hbaths = "";
$tsqft = "";
$yrb = "";
$taxes = "";

}


print qq~
<html>
<head>
<title>Showing Report - Connect.EagleSold.com</title>
<style>
	td			{ font-family: tahoma; font-weight:bold; font-size: 10px; color: #000066; text-decoration: none; }
	table			{ font-family: tahoma; font-size: 10px; color: #000066; text-decoration: none; }
	body 			{ font-family: tahoma; font-size: 10px; color: #000066; text-decoration: none; }

	.header			{ font-family: tahoma; font-weight:bold; font-size: 10px; color: #FFFFFF; background-color: #000066; text-decoration: none; }
</style>
<body bgcolor="white" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
~;

#&header;

print qq~
<center>
<font size="5" face="Arial Black">The ORR Home Selling Team RE/MAX -  Showing & Listing Information</font><br>
<font size="3" face="Arial" color="RED">CONFIDENTIAL FOR ORR TEAM AGENT & STAFF USE ONLY!</font><br><font size="3" face="Arial">Updated As Of $today</font><br>
<br>
<table border="0" cellpadding="3" cellspacing="1" width="100%" bgcolor="#000066">
<tr>
<td valign="top" class="header" nowrap>ID</td>
<td valign="top" class="header" nowrap>Status</td>
<td valign="top" class="header" nowrap>Type</td>
<td valign="top" class="header" nowrap>Class</td>
<td valign="top" class="header" nowrap>MLS #</td>
<td valign="top" class="header" nowrap>Address</td>
<td valign="top" class="header" nowrap>City</td>
<td valign="top" class="header" nowrap>Price</td>
<td valign="top" class="header" nowrap>Beds</td>
<td valign="top" class="header" nowrap>Baths</td>
<td valign="top" class="header" nowrap>SQFT</td>
<td valign="top" class="header" nowrap>Year</td>
<td valign="top" class="header" nowrap>Taxes</td>
<td valign="top" class="header" nowrap>Agent</td>
<td valign="top" class="header" nowrap>Listing Date</td>
<td valign="top" class="header" nowrap>Expire Date</td>
<td valign="top" class="header" nowrap>Showing Instructions</td>
</tr>
$print_list
</table>
</center>
</td>
</tr>
</table>
~;

}




