#!/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 $abasth = $dbh->prepare("select id,status,house,agent,DATE_FORMAT(date, '%W %b %D, %Y'),start_timehour,start_timeminute,start_ap,end_timehour,end_timeminute,end_ap FROM openhouses WHERE status = 'a' AND date >= now() order by date ASC");
$abasth->execute;


print qq~
<html>
<head>
<title>Eagle Real Estate, Inc. - http://OpenHouses.MuncieMLS.com</title>
~;

&header;

print qq~
<td valign="top" width="581">
<img src="images/headers/open_houses.gif" width="581" height="33" alt="Eagle Real Estate, Inc. Open Houses"><br>
<img src="images/headers/monday_openhouses.jpg" width="581" height="118" alt="Eagle Monday Open Houses"><br>
<table border="0" cellpadding="2" cellspacing="0" width="100%">
<tr>
~;

$trcount = "1";
$totalperrow = "3";  # Number of agents per row

while (($id,$status,$house,$agent,$date,$start_timehour,$start_timeminute,$start_ap,$end_timehour,$end_timeminute,$end_ap) = $abasth->fetchrow_array()) {

$found = "yes";

my $aasth = $dbh->prepare("select Price,AddressNumber,AddressDirection,AddressStreet,City,Zip,MLSNUM,AgentID,Directions FROM listings_res3 where MLSNUM = '$house'");
$aasth->execute;
my $basth = $dbh->prepare("select agentid,firstname,lastname from agents WHERE agentid = '$agent'");
$basth->execute;

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

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

if (-e "/home/muncieml/public_html/pdf/$house\.pdf") {
$showpdf = "
<table border=\"0\" cellpadding=\"4\" cellspacing=\"0\">
<tr>
<td valign=\"top\">
<a href=\"pdf/$house.pdf\" target=\"_blank\"><img src=\"images/pdficon.gif\" border=\"0\"></a><br>
</td>
<td valign=\"middle\">
<a href=\"pdf/$house.pdf\" target=\"_blank\">View Full Color<br>PDF Brochure</a><br>
</td>
</tr>
</table>
<br>
";
}
else {
$showpdf = "";
}
		while (($agentid,$firstname,$lastname) = $basth->fetchrow_array()) {
			print qq~
				<td valign="top" width="193" nowrap>
				<center>
				<font size="2" face="Arial" color="red"><b>$date</b></font><br>
				<font size="3" color="red">
				<b>$start_timehour:$start_timeminute $start_ap to $end_timehour:$end_timeminute $end_ap</b></font>
				<br>
				<font size="2" face="Arial"><b><i>Hosted By:</i></b></font><br> <a href="agents.idx?action=viewagent&agent=$agentid">$firstname $lastname</a><br>
				<a href="http://$MLSNUM.MuncieMLS.com" target="_blank"><img src="$showpicture" border="0" alt="Click for Details"></a><br>
				<a href="http://$MLSNUM.MuncieMLS.com" target="_blank">$AddressNumber $AddressDirection $AddressStreet<br>$City, IN $Zip</a><br>
				<a href="showdetails_print.idx?mlsnumber=$house" target="_blank"><img src="images/print_data_flyer.gif" border="0"></a><br>
				</center>
				<font size="2"><b>Directions:</b> $Directions$DirectionsContinued</font><br>$showpdf
				<br>
			   </td>
			~;
		}

	if ($trcount eq $totalperrow) {
	print "</tr><tr>";
	$trcount = "1";
	}
	else {
	$trcount++;
	}

	}

	}
	if ($found ne "yes") {
		print qq~
		<td valign="top" width="100%">
		<font size="4"><b><center>
		Visit Our IDEAL Models!  See below:<br>
		</center></b></font>
		</td>
		~;
	}
print qq~
</tr>
<tr>
	<td width="581" valign="top" colspan="3">
		<a href="http://www.IDEAL-Builders.com" target="_blank"><img src="images/ideal_openhouses.gif" width="581" height="535" alt="Visit IDEAL Suburban Online" border="0"></a><br>
	</td>
</tr>
</table>
</td>
~;

&footer;