#!/usr/bin/perl



require "config.idx";

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

$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 "viewagent") { &viewagent; }
else { &main; }

sub viewagent {

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 agentid,mlsid,firstname,lastname,title,phone1name,phone1number,phone2name,phone2number,phone3name,phone3number,phone4name,phone4number,email,homepage,profile,image,showsales from agents where display = '1' AND agentid = '$INPUT{agent}' LIMIT 1");
$pasth->execute;
while (($agentid,$mlsid,$firstname,$lastname,$title,$phone1name,$phone1number,$phone2name,$phone2number,$phone3name,$phone3number,$phone4name,$phone4number,$email,$homepage,$profile,$image,$showsales) = $pasth->fetchrow_array()) {


my $oasth = $dbh->prepare("select Price,AddressNumber,AddressDirection,AddressStreet,City,Zip,MLSNUM FROM listings_res3 where AgentID = '$mlsid' OR CoAgentID = '$mlsid' ORDER BY Price DESC");
$oasth->execute;
while (($Price,$AddressNumber,$AddressDirection,$AddressStreet,$City,$Zip,$MLSNUM) = $oasth->fetchrow_array()) {
$Price = &commas($Price);
$totallistings++;
$listingstuff .= "<center><b><A href=\"http://$MLSNUM.MuncieMLS.com\">$AddressNumber $AddressDirection $AddressStreet</a></b><br>\$$Price<br>$City, IN $Zip<br><font color=\"#666666\">MLS #$MLSNUM<br>Residential</font></center><br><br>";
}

my $qasth = $dbh->prepare("select Price,AddressNumber,AddressDirection,AddressStreet,City,Zip,MLSNUM from listings_com3 where AgentID = '$mlsid' ORDER BY Price DESC");
$qasth->execute;
while (($Price,$AddressNumber,$AddressDirection,$AddressStreet,$City,$Zip,$MLSNUM) = $qasth->fetchrow_array()) {
$Price = &commas($Price);
$totallistings++;
$listingstuff .= "<center><b><A href=\"http://$MLSNUM.MuncieMLS.com\">$AddressNumber $AddressDirection $AddressStreet</a></b><br>\$$Price<br>$City, IN $Zip<br><font color=\"#666666\">MLS #$MLSNUM<br>Commercial</font></center><br><br>";
}

my $rasth = $dbh->prepare("select MLSNUM,Price,AddressNumber,AddressDirection,AddressStreet,City,Zip from listings_inv3 where AgentID = '$mlsid' ORDER BY Price DESC");
$rasth->execute;
while (($MLSNUM,$Price,$AddressNumber,$AddressDirection,$AddressStreet,$City,$Zip) = $rasth->fetchrow_array()) {
$Price = &commas($Price);
$totallistings++;
$listingstuff .= "<center><b><A href=\"http://$MLSNUM.MuncieMLS.com\">$AddressNumber $AddressDirection $AddressStreet</a></b><br>\$$Price<br>$City, IN $Zip<br><font color=\"#666666\">MLS #$MLSNUM<br>Investment</font></center><br><br>";
}

my $sasth = $dbh->prepare("select MLSNUM,Price,AddressNumber,AddressDirection,AddressStreet,City,Zip from listings_lan3 where AgentID = '$mlsid' ORDER BY Price DESC");
$sasth->execute;
while (($MLSNUM,$Price,$AddressNumber,$AddressDirection,$AddressStreet,$City,$Zip) = $sasth->fetchrow_array()) {
$Price = &commas($Price);
$totallistings++;
$listingstuff .= "<center><b><A href=\"http://$MLSNUM.MuncieMLS.com\">$AddressNumber $AddressDirection $AddressStreet</a></b><br>\$$Price<br>$City, IN $Zip<br><font color=\"#666666\">MLS #$MLSNUM<br>Land</font></center><br><br>";
}

if ($totallistings eq "") {
$totallistings = "0";
}

if (-e "/home/muncieml/public_html/images/agentpics/$image") {
$showpicture = "images/agentpics/$image";
}
else {
$showpicture = "images/agentpics/nophoto\.gif";
}

if ($INPUT{referred} eq "agent") {
$printreturn = "";
}
else {
$printreturn = "<small><small><font face=\"Tahoma\">Return To:<br><a href=\"agents.idx\" class=\"main\">Meet Our Agents</a></center><br></font></small></small>";
}


print qq~
<html>
<head>
<LINK REL="SHORTCUT ICON" HREF="images/eaglerealestate.ico">
<title>Eagle Real Estate, Inc. >> Meet Our Agents >> $firstname $lastname</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="description" CONTENT="$firstname $lastname Eagle Real Estate MuncieMLS.com Online MLS Property Search Throughout East Central Indiana.  Albany, Eaton, Muncie, New Castle, Yorktown, Gaston, Redkey, Dunkirk, Parker City, Farmland, Selma, Hartford City, Cowan, Daleville, Chesterfield.  Looking to browse homes throughout east central indiana or contact an agent?  Visit us anytime 24 hours a day, 7 days a week.  We are "Always There For You!"   We specialize in residential, commercial, condominiums, and subdivison development throughout east central Indiana.">
<meta name="keywords" content="Tena Gorham Eagle Real Estate MuncieMLS.com Online MLS Property Search - Residential, Commercial, Investment, Lots & Land, Muncie Relocation, Muncie Homes, Muncie Real Estate, auction services, auction">
<META name="robots" content="index,follow">
<META NAME="revisit-after" CONTENT="30 days">
~;
&header;

print qq~
<td valign="top" width="582">
<img src="images/headers/meetouragents.gif" alt="Meet The Eagle Team" height="33" width="581"><Br>
<table border="0" width="582" cellspacing="5" cellpadding="0">
  <tr>
    <td width="107" valign="top"><center><img src="$showpicture" WIDTH="76" HEIGHT="105" border=0><br>
	$printreturn
</td>
    <td width="294" valign="top"><strong><font face="Arial Black"><big><big>$firstname $lastname</big></big></font><br>
    <font face="Tahoma">$title</font></strong><br>
    <br>
    </font><img src="images/meetouragents/contactinformation.gif" width="130" height="17" alt="My Contact Information">
<table border="0" cellpadding="0" cellspacing="4" width="100%">
      <tr>
        <td width="32%" valign="top" align="right" class="agents"><font face="Tahoma"><strong>$phone1name</strong></font></td>
        <td width="68%" valign="top" class="agents"><font face="Tahoma">$phone1number</font></td>
      </tr>
      <tr>
        <td width="32%" valign="top" align="right" class="agents"><strong><font face="Tahoma">$phone2name</font></strong></td>
        <td width="68%" valign="top" class="agents"><font face="Tahoma">$phone2number</font></td>
      </tr>
      <tr>
        <td width="32%" valign="top" align="right" class="agents"><strong><font face="Tahoma">$phone3name</font></strong></td>
        <td width="68%" valign="top" class="agents"><font face="Tahoma">$phone3number</font></td>
      </tr>
      <tr>
        <td width="32%" valign="top" align="right" class="agents"><strong><small><small><font face="Tahoma">$phone4name</font></strong></td>
        <td width="68%" valign="top" class="agents"><small><small><font face="Tahoma">$phone4number</font></td>
      </tr>
      <tr>
        <td width="32%" valign="top" align="right" class="agents"><strong><font face="Tahoma">E-Mail</font></strong></td>
        <td width="68%" valign="top"><font face="Tahoma" class="agents"><a href="mailto:$email" class="main">$email</a></font></td>
      </tr>
<tr><td width="32%" align="right" class="agents"><strong><font face=Tahoma>Homepage</font></strong></td>
<td width=68% class="agents"><font face=Tahoma><a href="$homepage" class="main">$homepage</a></td></tr>
    </table>
~;
############## START SAlES ##########################
#if ($showsales eq "1") {
#print qq~
#<br><b><font size="3">My Current Pending Sales</font><br><br>
#~;
#my $getpendingasth = $dbh->prepare("select agent,mlsnumber,type,status,addressnumber,addressdirection,addressstreet,city,state,zip,soldprice,DATE_FORMAT(pendingdate, '%M %D, %Y') from trans_board where agent = '$mlsid' AND status = 'Pending' ORDER BY pendingdate DESC");
#$getpendingasth->execute;
#while (($agent,$mlsnumber,$type,$status,$addressnumber,$addressdirection,$addressstreet,$city,$state,$zip,$soldprice,$pendingdate) = $getpendingasth->fetchrow_array()) {

#if (-e "/home/muncieml/public_html/images/photos/tiny/meiar$mlsnumber.jpg") {
#$showpicture = "images/photos/tiny/meiar$mlsnumber\.jpg";
#}
#else {
#$showpicture = "images/thumbnail_sold\.gif";
#}
#print qq~
#<table border="0" cellpadding="2" cellspacing="0">
#<tr>
#<td valign="top"><img src="http://www.munciemls.com/$showpicture"></td>
#<td valign="top">
#<font size="2">
#<b>$addressnumber $addressdirection $addressstreet<br>
#$city, $state $zip<br>
#</font>
#<font color="green"><b>Pending $pendingdate</b></font><br><br>
#</td>
#</tr>
#</table>
#~;
#}

#print qq~
#<b><font size="3">My Sold Homes</font><br>Since November 30th, 2004</b><br><br>
#~;

#my $getsoldasth = $dbh->prepare("select agent,mlsnumber,type,status,addressnumber,addressdirection,addressstreet,city,state,zip,soldprice,DATE_FORMAT(solddate, '%M %D, %Y') from trans_board where agent = '$mlsid' AND status = 'CLSD' ORDER BY solddate DESC");
#$getsoldasth->execute;
#while (($agent,$mlsnumber,$type,$status,$addressnumber,$addressdirection,$addressstreet,$city,$state,$zip,$soldprice,$solddate) = $getsoldasth->fetchrow_array()) {
#if (-e "/home/muncieml/public_html/images/photos/tiny/meiar$mlsnumber.jpg") {
#$showpicture = "images/photos/tiny/meiar$mlsnumber.jpg";
#}
#else {
#$showpicture = "images/thumbnail_sold.gif";
#}

#print qq~
#<table border="0" cellpadding="2" cellspacing="0">
#<tr>
#<td valign="top"><img src="http://www.munciemls.com/$showpicture"></td>
##<td valign="top">
#<font size="2">
#<b>$addressnumber $addressdirection $addressstreet<br>
#$city, $state $zip<br>
###</font>
#<font color="red"><b>SOLD $solddate</b></font><br><br>
#</td>
#</tr>
#</table>
#~;
#}
#}
############# END HIDDEN SALES ##################

print qq~
    <p><img src="images/meetouragents/profileaboutme.gif" width="130" height="17" alt="Profile"><br><font face="Tahoma">$profile</font><br>
~;

print qq~
</td>	
<td width="180" valign="top" bgcolor="#E2E2E2">
<img src="images/meetouragents/mylistings.gif" alt="My Listings"><br><center>
<font face="tahoma"><font size="1"><font color="green"><b>I currently have $totallistings active listings</font></font></font></center><br>$listingstuff<br>
</td>
  </tr>
</table>
</td>
~;

&footer;
}


$dbh->disconnect;

}




sub main {



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 agentid,mlsid,firstname,lastname,image,title from agents where display = '1' order by lastname asc");
$iasth->execute;

print qq~
<html>
<head>
<title>Eagle Real Estate >> Meet Our Agents</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="description" CONTENT="$firstname $lastname ERA Eagle Real Estate MuncieMLS.com Online MLS Property Search Throughout East Central Indiana.  Albany, Eaton, Muncie, New Castle, Yorktown, Gaston, Redkey, Dunkirk, Parker City, Farmland, Selma, Hartford City, Cowan, Daleville, Chesterfield.  Looking to browse homes throughout east central indiana or contact an agent?  Visit us anytime 24 hours a day, 7 days a week.  We are "Always There For You!"   We specialize in residential, commercial, condominiums, and subdivison development throughout east central Indiana.">
<meta name="keywords" content="Tena Gorham ERA Eagle Real Estate MuncieMLS.com Online MLS Property Search - Residential, Commercial, Investment, Lots & Land, Muncie Relocation, Muncie Homes, Muncie Real Estate, auction services, auction">
<META name="robots" content="index,follow">
<META NAME="revisit-after" CONTENT="30 days">
<LINK REL="SHORTCUT ICON" HREF="images/eaglerealestate.ico">
~;

&header;

print qq~
<td valign="top" width="582">
<img src="images/headers/meetouragents.gif" alt="Meet The Eagle Team" height="33" width="581"><br>
<table border="0" cellpadding="6" cellspacing="0" width="100%">
<tr>
~;

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

while (($agentid,$mlsid,$firstname,$lastname,$image,$title) = $iasth->fetchrow_array()) {

if (-e "/home/muncieml/public_html/images/agentpics/$image") {
$showpicture = "images/agentpics/$image";
}
else {
$showpicture = "images/agentpics/nophoto\.gif";
}

print qq~
<td valign="top">
	<table border="0" cellpadding="0" cellspacing="0">
	<tr>
	<td valign="top">
		<a href="agents.idx?action=viewagent&agent=$agentid"><img src="images/agentpics/$image" height="105" width="76" align="left" border="0"></a><br>
	</td>
	<td valign="top">
		<a href="agents.idx?action=viewagent&agent=$agentid" class="meetagents">$lastname, $firstname</a><br>$title<br>
		<a href="agents.idx?action=viewagent&agent=$agentid" class="main">More Info</a>
	</td>
	</tr>
	</table>
</td>

~;


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



}


print qq~

</tr>

</table>
</td>

~;

&footer;

$dbh->disconnect;

}




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