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

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

$mlsnumber = "$INPUT{mlsnumber}";

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;

	print qq~
		<html><head><title>Finding property...</title>
		<meta http-equiv="Refresh" content="0; URL=http://$mlsnumber.MuncieMLS.com/">
		</head>
		<body>
	~;

exit 0;
}
else {
print qq~

<html>
<head>
<title>MLSNumber Search</title>
</head>
<body bgcolor="white">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top">
<form method="get" action="mlsnumber.idx">
<input type="hidden" name="action" value="search">
<input type="text" name="mlsnumber" maxlength="5" length="5">
<input type="submit" value="Go!">
</form>
</td>
</tr>
</table>

</body>
</html>

~;
}
exit 0;