#!/usr/bin/perl

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

require "config.idx";

$long = $ENV{'QUERY_STRING'};

if ($long) {
	@pairs=split(/&/,$long);
	foreach $item(@pairs) {
		($name,$content)=split (/=/,$item,2);
		$content=~tr/+/ /;
		$content=~ s/%(..)/pack("c",hex($1))/ge;
		if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$content; }
		else { $INPUT{$name} = $content; }
	}
}
else {
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	@pairs = split(/&/, $buffer);
	foreach $pair (@pairs) {
		($name, $value) = split(/=/, $pair);
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$value; }
		else { $INPUT{$name} = $value; }
	}
}





use DBI;
my $dsn = "DBI:mysql:database=$d_name;host=localhost";
my $dbh = DBI->connect($dsn, $d_username, $d_pass, {RaiseError => 1}) || die $DBI::errstr;
my $pasth = $dbh->prepare("select * from listings_res3 where mlsnum = '$INPUT{mlsnumber}'");
#my $pasth = $dbh->prepare("select * from listings_res3 where mlsnum = '19155'");
my $iasth = $dbh->prepare("select * from listings_inv3 where mlsnum = '$INPUT{mlsnumber}'");
#my $iasth = $dbh->prepare("select * from listings_inv3 where mlsnum = '19155'");
$pasth->execute;
$iasth->execute;


while ($data_ref = $pasth->fetchrow_hashref()) {
%db = %$data_ref;
&pageformat;
}

while ($data_ref = $iasth->fetchrow_hashref()) {
%db = %$data_ref;
&pageformat;

}



$dbh->disconnect;






sub pageformat {


if ($db{MainLevelSqFt}) {
$db{MailLevelSqFt} = &commas($db{MainLevelSqFt});
}
else {
$db{MainLevelSqFt} = "N/A";
}

if ($db{UpperLevel1SqFt}) {
$db{UpperLevel1SqFt} = &commas($db{UpperLevel1SqFt});
}
else {
$db{UpperLevel1SqFt} = "N/A";
}

if ($db{UpperLevel2SqFt}) {
$db{UpperLevel2SqFt} = &commas($db{UpperLevel2SqFt});
}
else {
$db{UpperLevel2SqFt} = "N/A";
}

if ($db{LowerLevelSqFt}) {
$db{LowerLevelSqFt} = &commas($db{LowerLevelSqFt});
}
else {
$db{LowerLevelSqFt} = "N/A";
}

if ($db{TotalSqFt}) {
$db{TotalSqFt} = &commas($db{TotalSqFt});
}
else {
$db{TotalSqFt} = "N/A";
}

if ($db{BsmntFinSqFt}) {
$db{BsmntFinSqFt} = &commas($db{BsmntFinSqFt});
}
else {
$db{BsmntFinSqFt} = "N/A";
}

if ($db{BsmntUnfinSqFt}) {
$db{BsmntUnfinSqFt} = &commas($db{BsmntUnfinSqFt});
}
else {
$db{BsmntUnfinSqFt} = "N/A";
}

if ($db{BsmntSqFt}) {
$db{BsmntSqFt} = &commas($db{BsmntSqFt});
}
else {
$db{BsmntSqFt} = "N/A";
}

print <<"STARTPAGECONTENT";
<html>
<head>
<title>Square Footage Details</title>
<link rel="stylesheet" TYPE="text/css" href="includes/style.css">
</head>
<body bgcolor=white topmargin=0 leftmargin=0 marginwidth=0 marginheight=0>
<img src="images/mortgagecalcheader.gif" alt="MuncieMLS.com"><br>
<table border="0" cellspacing="0" cellpadding="3" width="100%">
<tr>
	<td valign="top" width="10%" bgcolor="#CFC595">&nbsp;<br></td>
	<td valign="top" width="80%" bgcolor="#CFC595"><font size="2"><font face="Arial"><font color="white"><b>Square Footage Details For MLS #$db{MLSNUM}</b></font></font></font><br></td>
	<td valign="top" width="10%" bgcolor="#CFC595">&nbsp;<br></td>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
	<td valign="top" colspan="4">

<table border="0" cellpadding="5" cellspacing="2" width="100%">
<tr>
<td>Main Level Square Footage</td><td><b>$db{MainLevelSqFt}</b></td>
</tr>
<tr>
<td>Upper Level 1 Square Footage</td><td><b>$db{UpperLevel1SqFt}</b></td>
</tr>
<tr>
<td>Upper Level 2 Square Footage</td><td><b>$db{UpperLevel2SqFt}</b></td>
</tr>
<tr>
<td>Lower Level Square Footage</td><td><b>$db{LowerLevelSqFt}</b></td>
</tr>
<tr bgcolor="#FFFFCC">
<td>Total Square Footage</td><td><b>$db{TotalSqFt}</b></td>
</tr>
<tr>
<td>Finished Basement Square Footage</td><td><b>$db{BsmntFinSqFt}</b></td>
</tr>
<tr>
<td>Unfinished Basement Square Footage</td><td><b>$db{BsmntUnfinSqFt}</b></td>
</tr>
<tr bgcolor="#FFFFCC">
<td>Total Basement Square Footage</td><td><b>$db{BsmntSqFt}</b></td>
</tr>
<tr>
<td>Source Of Records</td><td><b>$db{Source}</b></td>
</tr>
<tr>
<td colspan="2"><b><a href="javascript:self.close()">[ Close This Window ]</a></b><br></td>
</tr>
</table>

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

</body>
</html>


STARTPAGECONTENT





}




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




