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

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

if ($INPUT{script}) {
	if ($INPUT{action} eq "save") { &save; }
	else { &main; }
}
else {
	if ($INPUT{action} eq "save") { &listingsave; }
	elsif ($INPUT{action} eq "savelistingfinished") { &savelistingfinished; }
	else { &listingmain; }
}

sub savelistingfinished {
print <<"PAGE";
<html>
<head>
<title>Saved Listing</title>
<style>
		.bg			 { background-image: url(images/myhomes/windowbg.gif); background-repeat: repeat-x; }
</style>
<link rel="stylesheet" TYPE="text/css" href="includes/style.css">
</head>
<body bgcolor="white" class="bg">
<br>
<table border="0" cellpadding="10" cellspacing="0" width="100%">
<tr>
<td valign="top">
<font size="2" face="Arial"><b>Your Listing has been successfully saved to your MyHomes account.  You may access it by clicking on the <b>MyHomes</b> Link.</font><br><br><br><br>
<a href="javascript:self.close()">[ Close Window ]</a>
</td></tr></table>
</body>
</html>
PAGE

}


sub save {

$INPUT{name} =~ s/\'/CHR1/gi;
$INPUT{name} =~ s/\"/CHR2/gi;


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;

if ($INPUT{status} eq "on") {
$activatenotifications = "1";
my $pasth = $dbh->do("insert into search_fav values ('$Cook{userid}','','$INPUT{string}','$INPUT{name}','$INPUT{script}','$activatenotifications','$INPUT{email}','$INPUT{wireless}')");
}
else {
my $pasth = $dbh->do("insert into search_fav values ('$Cook{userid}','','$INPUT{string}','$INPUT{name}','$INPUT{script}','','','n')");
}

$dbh->disconnect;

print qq~

<html><head><title>Saving new search...</title>
<meta http-equiv="Refresh" content="3; URL=myhomes.idx?action=savedsearches">
</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>
</center>

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

~;



}



sub main {

&header;

print <<"THEPAGECONTENT";
<td valign="top" width="581">
<img src="images/headers/myhomes_account.gif" height="33" width="581" alt="MY HOMES Account"><br>
<img src="images/myhomes/menu/left.gif" width="61" height="20"><a href="myhomes.idx"><img src="images/myhomes/menu/summary.gif" width="86" height="20" border="0" alt="Summary"></a><img src="images/myhomes/menu/divider1.gif" width="3" height="20"><a href="myhomes.idx?action=savedlistings"><img src="images/myhomes/menu/savedlistings.gif" width="113" height="20" border="0" alt="My Saved Listings"></a><img src="images/myhomes/menu/divider1.gif" width="3" height="20"><a href="myhomes.idx?action=savedsearches"><img src="images/myhomes/menu/savedsearches_b.gif" width="120" height="20" border="0" alt="My Saved Searches"></a><img src="images/myhomes/menu/divider1.gif" width="3" height="20"><a href="myhomes.idx?action=notifications"><img src="images/myhomes/menu/notifications.gif" width="83" height="20" border="0" alt="My Notifications"></a><img src="images/myhomes/menu/divider1.gif" width="3" height="20"><a href="myhomes.idx?action=updateprofile"><img src="images/myhomes/menu/updateaccount.gif" width="106" height="20" border="0" alt="Update Account"></a>
<form method=get action=save.idx>
<input type=hidden name=action value=save>
<input type=hidden name=string value="$ENV{QUERY_STRING}">
<input type=hidden name=type value="$INPUT{type}">
<input type=hidden name=script value="$INPUT{script}">
	<table border="0" cellpadding="7" cellspacing="4" width="100%">
	<tr>
		<td valign="top" colspan="2"><b><font color="navy" size="2">Your almost done! Just follow the steps below to finish your new saved search!</font><br><br><font size="3">Save This Search</b></font><br></td>
	</tr>
	<tr>
		<td><b><font size="2">Name this search:</font></b></td><td><input type=text name=name size=40 value="$INPUT{City} $INPUT{display} $INPUT{MinPrice} - $INPUT{MaxPrice}"><br><br></td>
	</tr>
	<tr>
		<td valign="top" colspan="2"><font size="3"><b>Activate Notifications</b></font><br><b><font color="navy" size="2">Notifications may be turned on and off at a later time</font></b><br></td>
	</tr>
	<tr>
		<td colspan="2">
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr>
			<td valign="top"><input type="checkbox" name="status" checked></td>
			<td valign="top" width="100%"><b><font size="2">I would like to be notified by email when new listings meet this criteria</font></b></td>
			</tr>
			</table>
		</td>
	</tr>
	<tr>
	<td valign="top"><b>E-Mail notifications will be sent to</b></td>
	<td valign="top"><b><font color="navy"><u>$Cook{email}</u></font></b><input type=hidden name=email size=25 value="$Cook{email}"></td>
	</tr>
	<tr>
	<td valign="top" colspan="2">
	<input type="hidden" name="wireless" value="n">
	<center><input type=submit value="Setup Saved Search / Notifications Now!"></center>
	</td>
	</tr>
	</table>
</form>
</td>
THEPAGECONTENT


&footer;


}

sub listingsave {

$INPUT{notes} =~ s/\"/CHR2/gi;
$INPUT{notes} =~ s/\'/CHR1/gi;
$INUPT{notes} =~ s/\n/<br>/gi;

if ($INPUT{type} eq "res") {
$ftype = "res3";
}
if ($INPUT{type} eq "lan") {
$ftype = "lan3";
}
if ($INPUT{type} eq "com") {
$ftype = "com3";
}
if ($INPUT{type} eq "inv") {
$ftype = "inv3";
}

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;
$oasth = $dbh->do("insert into home_fav values ('$Cook{userid}','','$INPUT{mlsnumber}','$ftype','$INPUT{notes}')");
$dbh->disconnect;

print qq~

<html><head><title>Saving new home...</title>
<meta http-equiv="Refresh" content="3; URL=save.idx?action=savelistingfinished">
</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>
</center>

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

~;

}





sub listingmain {


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 $iasth = $dbh->prepare("select AddressNumber,AddressDirection,AddressStreet,City,Zip,MLSNUM,Price from listings_$INPUT{type}3 where MLSNUM = '$INPUT{mlsnumber}'");
$iasth->execute;

$dbh->disconnect;






while (($AddressNumber,$AddressDirection,$AddressStreet,$City,$Zip,$MLSNUM,$Price) = $iasth->fetchrow_array()) {

$Price = &commas($Price);

if (-e "/home/muncieml/public_html/images/photos/thumbnail/meiarmls$INPUT{mlsnumber}.jpg") {
$showpicture = "images/photos/thumbnail/meiarmls$INPUT{mlsnumber}.jpg";
}
else {
$showpicture = "images/photos/search_nophoto.gif";
}


print <<"THEPAGECONTENT";
<html>
<head>
<title>Save $MLSNUM</title>
<style>
		.bg			 { background-image: url(images/myhomes/windowbg.gif); background-repeat: repeat-x; }
</style>
</head>
<body bgcolor="white" class="bg">
<br>
<center>
<table border=0 cellpadding=4 cellspacing=0 width=50%>
<tr><td valign=top align=center><img src="$showpicture" height="53" width="70"></td><td valign=top align=left><font face="Verdana, Arial, Helvetica, sans-serif"><font size=1>
<form method=post action=save.idx>
<input type=hidden name=action value=save>
<input type=hidden name=mlsnumber value="$INPUT{mlsnumber}">
<input type=hidden name=type value="$INPUT{type}">
<b>$AddressNumber $AddressDirection $AddressStreet</b><br>
<b>$City, IN</b><br>
Property: <b>$MLSNUM</b><br>
Price: <b>\$$Price</b><br>
</td></tr>
<tr><td colspan=2>
<font face="arial" size="1"><b>Notes:</b></font><br>
<textarea name=notes cols=24 rows=6></textarea><br><br><center><input type=submit value="Save to MY HOMES"></form>

</td></tr></table>
</body>
</html>

THEPAGECONTENT

}

}





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


