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

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

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

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

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

<html><head><title></title>
<meta http-equiv="set-cookie" content="userid=; expires=; path=/"> 
</head>

~;
&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="#990000"><b>You must have a MyHomes 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> 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 MuncieMLS.com you must create a MyHomes account.</b></font><br><br><font size="2">A 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>
<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;
}

}


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;


$sql_query = qq~select SQL_CALC_FOUND_ROWS Price,AddressNumber,AddressDirection,AddressStreet,City,Zip,Status,MLSNUM,AgentID,AgentName,ListingOffice1Name,ListingOffice1Phone,ListingOffice1ID from listings_lan3 where ~;


if ($INPUT{City}) {
$counter = "0";
$searchcity = "\(";
@cities = split(/\,/,$INPUT{City});
	foreach $place (@cities) {
		$sql_query .= qq~City = '$place' OR ~;

	}
$sql_query =~ s/ OR $/\)/;
}
else {
	$sql_query .= qq~City LIKE '%'~;
}


if ($INPUT{County}) {
$searchcounty = "AND \(";
@whatcounty = split(/\,/,$INPUT{County});
	foreach $place (@whatcounty) {
		$sql_query .= qq~County = \'$place\' OR ~;
	}
$sql_query =~ s/ OR $/\)/;
}

if ($INPUT{MinPrice}) {
$sql_query .= qq~ AND Price >= '$INPUT{MinPrice}' ~;
}

if ($INPUT{MaxPrice}) {
$searchprice .= qq~ AND Price <= '$INPUT{MaxPrice}' ~;
}


if ($INPUT{citylimits}) {
$sql_query .= qq~ AND CityLimits = '$INPUT{citylimits}'~;
}

if ($INPUT{tillable}) {
$sql_query .= qq~ AND Tillable = '$INPUT{tillable}'~;
}

if ($INPUT{wooded}) {
$sql_query .= qq~ AND Wooded = '$INPUT{wooded}'~;
}

if ($INPUT{MinSF} && $INPUT{MinSF} ne "0") {
$sql_query .= qq~ AND TotalSqFt >= '$INPUT{MinSF}'~;
}

if ($INPUT{MaxSF}) {
$sql_query .= qq~ AND TotalSqFt <= '$INPUT{MaxSF}'~;
}

if ($INPUT{Address}) {
$INPUT{Address} =~ s/ /\%/gi;
$sql_query .= qq~ AND AddressStreet LIKE '$INPUT{Address}'~;
}

if ($INPUT{Remarks}) {
$INPUT{Remarks} =~ s/ /\%/gi;
$sql_query .= qq~ AND Remarks LIKE '%$INPUT{Remarks}%'~;
}

if ($INPUT{sortby}) {
$searchsort = "ORDER BY $INPUT{sortby}";
}

if ($INPUT{orderby}) {
$searchorder = " $INPUT{orderby}";
}



if ($INPUT{Page} eq "" || $INPUT{Page} eq "0") {
$thispage = "1";
$INPUT{Page} = "1";
}
else {
$thispage = "$INPUT{Page}";
}

$thispage--;

if ($thispage eq "0") {
$limitnumber = "0";
}
else {
$limitnumber = ($INPUT{Show} * $thispage);
}


$sql_query .= qq~ AND Status = '1'~;


$thelimitstatement = "$limitnumber\,$INPUT{Show}";
#print "$thelimitstatement<br>$thispage<br>";




if ($INPUT{showsqlquery} eq "yes") {
print qq~$sql_query AND Status = 'Active' $searchsort $searchorder limit $thelimitstatement
<br><br>
~;
}


my $uasth = $dbh->prepare("$sql_query $searchsort $searchorder limit $thelimitstatement");
$uasth->execute;

my $iasth = $dbh->prepare("select found_rows()");
$iasth->execute;


$dbh->disconnect;

while (($totalcount) = $iasth->fetchrow_array()) {
$savetotalcount = $totalcount;
}

$tempcount = ($savetotalcount / $INPUT{Show});
#print "Before: $tempcount<br>";
$beforecount = $tempcount;
#print "After: $tempcount<br>";
$tempcount = sprintf("%.0f",$tempcount);
#print "Changed: $tempcount<br>";
if ($tempcount < $beforecount) {
$tempcount++;
}

$nextpage = ($INPUT{Page}+1);

$previouspage = ($INPUT{Page}-1);



$searchstrings = "City=$INPUT{City}&County=$INPUT{County}&MinPrice=$INPUT{MinPrice}&MaxPrice=$INPUT{MaxPrice}&wooded=$INPUT{wooded}&tillable=$INPUT{tillable}&citylimits=$INPUT{citylimits}&Address=$INPUT{Address}&Remarks=$INPUT{Remarks}&Show=$INPUT{Show}&sortby=$INPUT{sortby}&orderby=$INPUT{orderby}&Page=";

$searchwithpage = $searchstrings;
$searchwithpage .= "$INPUT{Page}";

$nextlink = "$searchstrings$nextpage";
$prevlink = "$searchstrings$previouspage";




if ($INPUT{Page} eq "1") {
$showprevcolor = "";
$showprevlink = "<font color=c0c0c0><</font> <font color=c0c0c0>Previous</font>";
}
else {
$showprevcolor = "";
$showprevlink = "<font color=white><</font> <a class=navheader href=\"search_lan.idx?$prevlink\">Previous</a>";
}


if ($INPUT{Page} == $tempcount || $tempcount eq "0") {
$shownextcolor = "";
$shownextlink = "<font color=c0c0c0>Next <font color=c0c0c0>></font>";
}
else {
$shownextcolor = "";
$shownextlink = "<a class=navheader href=\"search_lan.idx?$nextlink\">Next</a> <font color=white>></font>";
}

$cleanorderby = "$INPUT{orderby}";
$cleansortby = $INPUT{sortby};



if ($cleanorderby eq "ASC") {
$cleanorderby = "Ascending";
}
else {
$cleanorderby = "Descending";
}

if ($cleansortby eq "TotalSqFt") {
$cleansortby = "SQFT.";
}


if ($tempcount eq "0") {
$INPUT{Page} = "\-";
$tempcount = "\-";
}





&header;

print qq~

<td valign=top width=581>
<img src="images/headers/lotsandland_propertysearch.gif" height="33" width="581" alt="Land Search"><Br>
<table border="0" cellpadding="0" cellspacing="0" width="581">
<tr>
<td valign="top" width="100%">
<font face="Arial, Helvetica, sans-serif"><font size="4">Search <b>Results</b></font></font><br>
<font face="MS Sans Serif, Verdana, Helvetica, sans-serif"><font size="2">We found <b>$savetotalcount</b> listings that meet your Search Criteria.<br></font></font>
<table border="0" cellpadding="3" cellspacing="0" width="581" bgcolor="#006699" background="images/searchbgopt1.gif">
<tr>
<td valign="middle" width="340">
<font face="MS Sans Serif, Verdana, Helvetica, sans-serif"><font size="2">
<font color="white">&nbsp;&nbsp;<b>Search Options:</font></b>&nbsp;&nbsp;<a class=navheader href="propertysearch.idx?type=Land">Change Criteria</a>&nbsp;&nbsp;<font color="white">|</font>&nbsp;&nbsp;<a class=navheader href=save.idx?script=lan\&$searchwithpage>Save</a>
</font></font>
</td>
<td valign="middle" width="241" bgcolor="#006699" background="images/searchbgopt3.gif">
<center><font face="MS Sans Serif, Verdana, Helvetica, sans-serif"><font size="2" color=white>$showprevlink
&nbsp;&nbsp;&nbsp;<font color="white"><b>$INPUT{Page}</b> of <b>$tempcount</b></font>&nbsp;&nbsp;&nbsp;<font color=white>
$shownextlink
</center></font></font>
</td>
</tr>
<form method="get" action="search_lan.idx">
<input type="hidden" name="display" value="Land">
<input type="hidden" name="City" value="$INPUT{City}">
<input type="hidden" name="County" value="$INPUT{County}">
<input type="hidden" name="Address" value="$INPUT{Address}">
<input type="hidden" name="Remarks" value="$INPUT{Remarks}">
<input type="hidden" name="MinPrice" value="$INPUT{MinPrice}">
<input type="hidden" name="MaxPrice" value="$INPUT{MaxPrice}">
<input type="hidden" name="MinBed" value="$INPUT{MinBed}">
<input type="hidden" name="MinBath" value="$INPUT{MinBath}">
<input type="hidden" name="Acreage" value="$INPUT{Acreage}">
<input type="hidden" name="MinSF" value="$INPUT{MinSF}">
<input type="hidden" name="MaxSF" value="$INPUT{MaxSF}">
<tr>
<td colspan="3" bgcolor="silver" background="images/searchbgchange.gif" width="100%"><center>View 
<select name="Show" class="gform">
<option value="$INPUT{Show}">$INPUT{Show}</option>
<option value="10">---</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
</select> Listings Per Page, Order By 
<select name="sortby" class="gform">
<option value="$INPUT{sortby}">$cleansortby</option>
<option value="Price">---</option>
<option value="Price">Price</option>
<option value="Acres">Acreage</option>
<option value="TotalSqFt">SQFT.</option>
<option value="Zip">Zip</option>
<option value="AddressStreet">Street</option>
</select> in <select name="orderby" class="gform">
<option value="$INPUT{orderby}">$cleanorderby</option>
</option>
<option value="DESC">---</option>
<option value="DESC">Descending</option>
<option value="ASC">Ascending</select> order <input type="submit" value="Change" class="gform"><br></center>
</td>
</tr>
</form>
</table>


<center>

~;


$resultcount = "0";

while (($Price,$AddressNumber,$AddressDirection,$AddressStreet,$City,$Zip,$Status,$MLSNUM,$AgentID,$AgentName,$ListingOffice1Name,$ListingOffice1Phone,$ListingOffice1ID) = $uasth->fetchrow_array()) {
$resultcount++;
$Price = &commas($Price);
$TotalSqFt = &commas($TotalSqFt);


if ($ListingOfficeID eq "130") {
$tablewidth = "100";
$showbr = "";
$showoffice = "<a href=\"http://www.eraeagleonline.com\" target=\"_black\"><img src=\"images/era.gif\" width=\"100\" height=\"75\" ALT=\"ERA\" Border=\"0\"></a>";
$showofficephone = $ListingOfficePhone;
}
else {
$tablewidth = "290";
$showbr = "<br><center>";
$showofficephone = "";
$showoffice = "<img src=\"images/br.gif\">";
}



$showpicture = &image_format("thumbnail","$MLSNUM");


if ($Cook{userid}) {
$showsavelisting = "<a href=\"javascript:save=window.open('save.idx?mlsnumber=$MLSNUM&type=lan','save','width=320, height=325','noscrolling'); save.focus();\">Save Listing To MyHomes</a>";
}
else {
$showsavelisting = "<a href=register.idx>Register To Save This Listing!</a>";
}



print qq|

<table border="0" cellpadding="3" cellspacing="0" width="581">
<tr>
<td colspan="4" background="images/searchbg.gif">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="5%">&nbsp;</td>
<td valign="middle" width="30%">
<font face="Arial, Helvetica, sans-serif"><font size="2"><b>\$$Price</b></font></font>
</td>
<td valign="middle" width="40%">$showsavelisting
</td>
<td valign="middle" width="20%" align="right" nowrap>
<font face="Arial, Helvetica, sans-serif"><font size="2"><b><a href="showdetails_land.idx?$searchwithpage\&mlsnumber=$MLSNUM">View Listing Details</a></b></font></font>
</td>
<td width="5%">&nbsp;</td>
</tr>
</table>
</td>
</tr>
<tr>
<td valign="top" width="105">
<center>
<a href="showdetails_land.idx?$searchwithpage\&mlsnumber=$MLSNUM">
<img src="$showpicture" border="0" width="105" height="80">
</a>

</center>
</td>
<td valign="top" width="186">
<font face="Verdana, Arial, Helvetica, sans-serif"><font size="1">
<b>
$AddressNumber $AddressDirection $AddressStreet<br>
$ShowCity{$City}, IN $Zip</b><br>
MLS <b>$MLSNUM</b><br>
</font></font>
</td>
<td valign="top"><img src="images/fill.gif" height="65" width="1"></td>
<td valign="top" width="290">
<!--START5-->
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="$tablewidth">$showbr$showoffice</td>
|;


if ($ListingOfficeID eq "130") {
print qq|

<td width="190" valign="middle">
<font face="Verdana, Arial, Helvetica, sans-serif"><font size="2">
<i>Offered By:</i><br>
<b>$ListingOfficeName</b><br>
<a href="http://www.eraeagleonline.com/agents.idx" target="_blank"><a href="directory.idx?query=viewperson&ID=$AgentID">$AgentName</a><br>
$showofficephone<br>
</font></font>
</td>

|;

}




print qq~

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




}

if ($resultcount eq "0") {

print "<br><br><br><center><font face=\"Verdana, Arial, Helvetica, sans-serif\"><strong>No Results Found.  Please go back and try again.";
}

print qq~


<table border="0" cellpadding="3" cellspacing="0" width="581" bgcolor="#006699" background="images/searchbgopt1.gif">
<tr>
<td valign="middle" width="340">
<font face="MS Sans Serif, Verdana, Helvetica, sans-serif"><font size="2">
<font color="white">&nbsp;&nbsp;<b>Search Options:</font></b>&nbsp;&nbsp;<a class=navheader href="propertysearch.idx?type=Land">Change Criteria</a>&nbsp;&nbsp;<font color="white">|</font>&nbsp;&nbsp;<a class=navheader href=save.idx?script=lan\&$searchwithpage>Save</a>
</font></font>
</td>
<td valign="middle" width="241" bgcolor="#006699" background="images/searchbgopt3.gif">
<center><font face="MS Sans Serif, Verdana, Helvetica, sans-serif"><font size="2" color=white>$showprevlink
&nbsp;&nbsp;&nbsp;<font color="white"><b>$INPUT{Page}</b> of <b>$tempcount</b></font>&nbsp;&nbsp;&nbsp;<font color=white>
$shownextlink
</center></font></font>
</td>
</tr>
<form method="get" action="search_lan.idx">
<input type="hidden" name="display" value="Residential">
<input type="hidden" name="City" value="$INPUT{City}">
<input type="hidden" name="County" value="$INPUT{County}">
<input type="hidden" name="Address" value="$INPUT{Address}">
<input type="hidden" name="Remarks" value="$INPUT{Remarks}">
<input type="hidden" name="MinPrice" value="$INPUT{MinPrice}">
<input type="hidden" name="MaxPrice" value="$INPUT{MaxPrice}">
<input type="hidden" name="MinBed" value="$INPUT{MinBed}">
<input type="hidden" name="MinBath" value="$INPUT{MinBath}">
<input type="hidden" name="Acreage" value="$INPUT{Acreage}">
<input type="hidden" name="MinSF" value="$INPUT{MinSF}">
<input type="hidden" name="MaxSF" value="$INPUT{MaxSF}">
<tr>
<td colspan="3" bgcolor="silver" background="images/searchbgchange.gif" width="100%"><center>View 
<select name="Show" class="gform">
<option value="$INPUT{Show}">$INPUT{Show}</option>
<option value="10">---</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
</select> Listings Per Page, Order By 
<select name="sortby" class="gform">
<option value="$INPUT{sortby}">$cleansortby</option>
<option value="Price">---</option>
<option value="Price">Price</option>
<option value="Acres">Acreage</option>
<option value="TotalSqFt">SQFT.</option>
<option value="Zip">Zip</option>
<option value="AddressStreet">Street</option>
</select> in <select name="orderby" class="gform">
<option value="$INPUT{orderby}">$cleanorderby</option>
</option>
<option value="DESC">---</option>
<option value="DESC">Descending</option>
<option value="ASC">Ascending</select> order <input type="submit" value="Change" class="gform"><br></center>
</td>
</tr>
</form>
</table>






~;


#print "</td>";
print "</td></tr></table></td>";
#print "</tr></table></td></tr></table><center>";

&footer;


sub commas {
	local($_)=@_;
	1 while s/(.*\d)(\d\d\d)/$1,$2/;
	$_;
}


