#!/usr/bin/perl

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 "logoff" || $INPUT{action} eq "login") {
print "Content-type: text/html\n\n";
}

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;
}

}

if ($INPUT{'action'} eq "verify") { &verify; }
elsif ($INPUT{'action'} eq "delete") { &delete; }
else { &html; }



sub html {

&header;


print qq~
<td valign="top" align="right" width="581">
<img src="images/headers/tos.gif" width="581" height="33" alt="Terms of Service"><br>
<table border="0" cellpadding="4" cellspacing="0" width="581">
<tr>
<td valign="top" width="581">
<br>
<font size="4">Please Accept Updated Terms of Service to Proceed.</font>
<form method="post" action="tos.idx">
<input type="hidden" name="action" value="verify">
<input type="hidden" name="accept" value="yes">
<input type="submit" value="I Agree [ Continue ]">
</form>
<iframe src="terms_popup.html" width="580" height="500" noresize="noresize" scrolling ="yes" frameborder="0" marginwidth="3" marginheight="3" name="tos_terms"></iframe>
<br>
<form method="post" action="tos.idx">
<input type="hidden" name="action" value="verify">
<input type="hidden" name="accept" value="yes">
<input type="submit" value="I Agree [ Continue ]">
</form>

<form method="post" action="tos.idx">
<input type="hidden" name="action" value="delete">
<input type="submit" value="I Do Not Agree [ Delete My Account - All Saved Searched and Properties will be removed ]">
</form>

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

~;

&footer;

}


sub verify {
	my $uasth = $dbh->do("update users set tos_agreement = now() where userid = '$Cook{userid}'");
print qq~
	<html>
	<head>
	<title>Loading MyHomes...</title>
	<meta http-equiv="Refresh" content="3; URL=http://www.MuncieMLS.com/myhomes.idx">
	</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>
	<img src="images/LoadingMyHomes.gif"><br>
	</center>
	</td>
	</tr>
	</table>
	</center>
	</body>
	</html>
	~;
}

sub delete {

my $aasth = $dbh->do("delete from users where userid = '$Cook{userid}'");
$pusers = "Cleaned up users table!";
my $basth = $dbh->do("delete from search_fav where userid = '$Cook{userid}'");
$psearch_fav = "Cleaned up search_fav table!";
my $casth = $dbh->do("delete from home_fav where userid = '$Cook{userid}'");
$phome_fav = "Cleaned up home_fav table!";
my $dasth = $dbh->do("delete from leadtracking where userid = '$Cook{userid}'");
$pleadtracking = "Cleaned up leadtracking table!";
my $dasth = $dbh->do("delete from emailstousers where userid = '$Cook{userid}'");
$pemails = "Cleaned up saved emails table!";
$dbh->disconnect;

print qq~
	<html>
	<head>
	<title>Loading MyHomes...</title>
	<meta http-equiv="Refresh" content="3; URL=http://www.MuncieMLS.com">
	</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>
	<img src="images/LoadingMyHomes.gif"><br>
	</center>
	</td>
	</tr>
	</table>
	</center>
	</body>
	</html>
~;
}
