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

$mlsnumber = "$INPUT{referrer}";

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") {

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 $yasth = $dbh->prepare("select MLSNUM,Price,AddressNumber,AddressDirection,AddressStreet,City,Zip,Bedrooms,FullBaths,HalfBaths,TotalSqFt from listings_res3 where MLSNUM = '$mlsnumber'");
$yasth->execute;
while (($MLSNUM,$Price,$AddressNumber,$AddressDirection,$AddressStreet,$City,$Zip,$Bedrooms,$FullBaths,$HalfBaths,$TotalSqFt) = $yasth->fetchrow_array()) {

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

if ($photourl =~ /sorry/) {
    $showpicture = "http://www.munciemls.com/images/nophoto.gif";
}


$Price = &commas($Price);
$TotalSqFt = &commas($TotalSqFt);
$printlisting = qq~
<table border="0" cellpadding="2" cellspacing="0">
<tr>
<td valign="top">
<img src="$showpicture" width="200" height="150"><br>
</td>
<td valign="top">
<font size="4"><b>
$AddressNumber $AddressDirection $AddressStreet<br>
$City, IN $Zip<br>
MLS \#$MLSNUM<br>
</b></font>
<font size="2">
<b>Price</b> \$$Price<br>
<b>Bedrooms</b> $Bedrooms<br>
<b>Full Baths</b> $FullBaths<br>
<b>Half Baths</b> $HalfBaths<br>
<b>Total SQFT.</b> $TotalSqFt<br>
</font>
</td>
</tr>
</table>
~;
}



my $yasth = $dbh->prepare("select MLSNUM,Price,AddressNumber,AddressDirection,AddressStreet,Address2,City,Zip,AgentID FROM listings_lan3 where MLSNUM = '$mlsnumber'");
$yasth->execute;
while (($MLSNUM,$Price,$AddressNumber,$AddressDirection,$AddressStreet,$Address2,$City,$Zip) = $yasth->fetchrow_array()) {

if (-e "/home/muncieml/public_html/images/photos/full/meiarmls$MLSNUM\.jpg") {
$showpicture = "http://www.munciemls.com/images/photos/full/meiarmls$MLSNUM\.jpg";
}
else {
$showpicture = "http://www.munciemls.com/images/nophoto.gif";
}

$Price = &commas($Price);

$printlisting = qq~
<table border="0" cellpadding="2" cellspacing="0">
<tr>
<td valign="top">
<img src="$showpicture" width="200" height="150"><br>
</td>
<td valign="top">
<font color="red"><b>Lots & Land</b></font><br>
<font size="4"><b>
$AddressNumber $AddressDirection $AddressStreet<br>
$City, IN $Zip<br>
MLS \#$MLSNUM<br>
</b></font>
<font size="2">
<b>Price</b> \$$Price<br>
</font>
</td>
</tr>
</table>
~;
}



my $yasth = $dbh->prepare("select MLSNUM,Price,AddressNumber,AddressDirection,AddressStreet,City,Zip,TotalSqFt FROM listings_com3 where MLSNUM = '$mlsnumber'");
$yasth->execute;
while (($MLSNUM,$Price,$AddressNumber,$AddressDirection,$AddressStreet,$City,$Zip,$TotalSqFt) = $yasth->fetchrow_array()) {

if (-e "/home/muncieml/public_html/images/photos/full/meiarmls$MLSNUM\.jpg") {
$showpicture = "http://www.munciemls.com/images/photos/full/meiarmls$MLSNUM\.jpg";
}
else {
$showpicture = "http://www.munciemls.com/images/nophoto.gif";
}

$Price = &commas($Price);
$TotalSqFt = &commas($TotalSqFt);

$printlisting = qq~
<table border="0" cellpadding="2" cellspacing="0">
<tr>
<td valign="top">
<img src="$showpicture" width="200" height="150"><br>
</td>
<td valign="top">
<font size="4"><b>
$AddressNumber $AddressDirection $AddressStreet<br>
$City, IN $Zip<br>
MLS \#$MLSNUM<br>
</b></font>
<font size="2">
<b>Price</b> \$$Price<br>
<b>Total SQFT.</b> $TotalSqFt<br>
</font>
</td>
</tr>
</table>
~;
}





my $yasth = $dbh->prepare("select MLSNUM,Price,AddressNumber,AddressDirection,AddressStreet,City,State,Zip,TotalSqFt FROM listings_inv3 where MLSNUM = '$mlsnumber'");
$yasth->execute;
while (($MLSNUM,$Price,$AddressNumber,$AddressDirection,$AddressStreet,$City,$State,$Zip,$TotalSqFt) = $yasth->fetchrow_array()) {
if (-e "/home/muncieml/public_html/images/photos/full/meiarmls$MLSNUM\.jpg") {
$showpicture = "http://www.munciemls.com/images/photos/full/meiarmls$MLSNUM\.jpg";
}
else {
$showpicture = "http://www.munciemls.com/images/nophoto.gif";
}

$Price = &commas($Price);
$TotalSqFt = &commas($TotalSqFt);

$printlisting = qq~
<table border="0" cellpadding="2" cellspacing="0">
<tr>
<td valign="top">
<img src="$showpicture" width="200" height="150"><br>
</td>
<td valign="top">
<font size="4"><b>
$AddressNumber $AddressDirection $AddressStreet<br>
$City, IN $Zip<br>
MLS \#$MLSNUM<br>
</b></font>
<font size="2">
<b>Price</b> \$$Price<br>
<b>Total SQFT.</b> $TotalSqFt<br>
<b>Number Of Units</b> $NUMUnits<br>


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

$dbh->disconnect;

print qq~

<html><head><title>Eagle Real Estate, Inc. - MuncieMLS.com</title>
<meta http-equiv="set-cookie" content="userid=; expires=; path=/"> 

~;
&header;
print qq~

		<td valign="top" width="581">
		<img src="images/headers/welcome.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 be a MuncieMLS.com member to view the full listing sheet.</b></font><br><font size="2" color="black">You may login to your existing account or create a <b>FREE</b> Eagle Real Estate MuncieMLS.com MyHomes account below the basic listing sheet.</font></font><br><br><br>
		$printlisting
<br><br>
<table>
<tr>
<td colspan="2">
<font size="3" face="Arial" color="navy"><b>Show Me The Full MLS Listing Data For MLS #$INPUT{referrer} NOW!</b></font><br><br><font size="2"><b>How it works!</b> If you are new to MuncieMLS.com you may press the <b>Create An Account Now</b> button below and register to view the full MLS details sheet on this property and gain access to almost all other MLS listings! <b>If have previously setup a MuncieMLS.com account you may login below.</font><br><br>
</td>
</tr>
<tr>
<td valign=top width="50%">
<font size="3" face="Arial" color="#000066"><b>I Am New To MuncieMLS.com And Have Not Registered And Want To Join Now!</b></font><br>
<form method=post action=register.idx>
<input type=hidden name=referrer value=$INPUT{referrer}>
<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="3" face="Arial" color="#000066"><b>I Have Previously Registered<br><b>And Want To Log Back In</b></font><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;

my $yasth = $dbh->prepare("select MLSNUM from listings_res3 where MLSNUM = '$mlsnumber'");
$yasth->execute;

while (($fakenumber) = $yasth->fetchrow_array()) {
$extension = "";
$found = "yes";
}


my $yasth = $dbh->prepare("select MLSNUM from listings_com3 where MLSNUM = '$mlsnumber'");
$yasth->execute;

while (($fakenumber) = $yasth->fetchrow_array()) {
$extension = "\_commercial";
$found = "yes";
}


my $yasth = $dbh->prepare("select MLSNUM from listings_inv3 where MLSNUM = '$mlsnumber'");
$yasth->execute;

while (($fakenumber) = $yasth->fetchrow_array()) {
$extension = "\_investment";
$found = "yes";
}


my $yasth = $dbh->prepare("select MLSNUM from listings_lan3 where MLSNUM = '$mlsnumber'");
$yasth->execute;

while (($fakenumber) = $yasth->fetchrow_array()) {
$extension = "\_land";
$found = "yes";
}

$dbh->disconnect;


if ($found eq "yes") {
print <<"HERETHIS";


<html><head><title>Finding property...</title>
<meta http-equiv="Refresh" content="0; URL=http://www.munciemls.com/showdetails$extension.idx?mlsnumber=$mlsnumber">
</head>
<body>


HERETHIS

exit 0;
}
else {

print qq~
<html>
<head>
<title>Muncie Real Estate - Search MLS 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, MLS Search, 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">

<table border="0" cellpadding="3" width="446" cellspacing="0">
<tr>
<td valign="top" width="100%">
<center>
<table border="0" cellpadding="0" cellspacing="0" width="391">
<tr>
<td valign="top" colspan="2">
<font face="Arial, Helvetica, sans-serif"><font size="4" color="Red">MLS Number No Longer In Our System!</font><br><br>Possible Reasons May Include<br><br>
&nbsp;&nbsp;&nbsp;&nbsp;Listing may have become Pending, Sold, Expired, or Withdrawn from the MLS.<br>
&nbsp;&nbsp;&nbsp;&nbsp;MLS number may have been incorrectly typed.<br><br><br>
You may search other listings on our homepage.   If you are already a member of MuncieMLS you may <a href="myhomes.idx">Login Here</a> or if you are new to MuncieMLS.com  you may <a href="register.idx">Register here</a> to gain MLS access.</font>
</td></tr></table></td></tr></table>

~;

&footer;

}

