#!/usr/bin/perl

$d_name = "muncieml_db1";
$d_username = "muncieml_db1";
$d_pass = "\@rockdb1";

$e_name = "muncieml_db1";
$e_username = "muncieml_db1";
$e_pass = "\@rockdb1";

$oh_name = "orrhomes_db1";
$oh_username = "orrhomes_db1";
$oh_pass = "\@rockdb1";

$n_name = "muncieml_pdb";
$n_username = "muncieml_pdb";
$n_pass = "MDJIQUUUSUA22811";

$photo_url_pre = "http://media2.idxnow.com/meiar";

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

if ($Cookies{userid} =~ /\|/) {
($Cook{userid},$Cook{email},$Cook{agentid}) = split(/\|/,$Cookies{userid});
}
else {
($Cook{userid},$Cook{email},$Cook{agentid}) = split(/\-/,$Cookies{userid});
}

unless ($INPUT{action} eq "login") {
if ($Cook{userid}) {
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 $vasth = $dbh->prepare("select firstname,lastname,access,agentid from users where userid = '$Cook{userid}' AND email = '$Cook{email}'");
$vasth->execute;

while (($firstname,$lastname,$access,$mymlsid) = $vasth->fetchrow_array()) {

my $avasth = $dbh->prepare("select agentid,mlsid from agents where mlsid = '$mymlsid' ORDER by agentid asc limit 0,1");
$avasth->execute;
while (($aagent_agentid,$aagent_mlsid) = $avasth->fetchrow_array()) {
$agent_agentid = $aagent_agentid;
}

$firstname =~ s/CHR1/\'/gi;
$lastname =~ s/CHR2/\"/gi;

if ($access eq "officestaf") { $access_name = "Office Staff"; }
if ($access eq "admin") { $access_name = "Administrator"; }
if ($access eq "eagleagent") { $access_name = "Eagle Agent"; }

$navbarcode .=qq~
<b>You are Logged in As</b><br>
<b>$firstname $lastname</b><br>
$Cook{email}<br>
System Login ID: $Cook{agentid}<br>
<b>$access_name Permissions</b><br>
<br>~;


#if ($Cook{email} =~ /orr/) {
#$navbarcode .=qq~
#gmail found<br>
#~;
#}

if ($access eq "officestaf" || $access eq "admin") {
$navbarcode .=qq~ 
<div id="navmenuoffice">
	<ul class="vert_header">
		<li>OFFICE Staff Area</li>
	</ul>
	<ul class="vert">
		<li><a href=transactionsadmin.idx>Transaction Center</a></li>
		<li><a href=showingadmin.idx>Showing Center</a></li>
		<li><a href=prospectadmin.idx>Prospect Admin <font color=red>old</font></a></li>
		<li><a href=directoryadmin.idx>Directory Center</a></li>
		<li><a href=openhouse.idx>Open House Admin</a></li>
		<li><a href=new_export_transactions.idx>Export Transactions</a></li>
		<li><a href=tasks.idx?action=tasks_default>Task Manager Defaults</a></li>
	</ul>
</div>
<div class="spacer"></div>
~;
}

if ($access eq "eagleagent" || $access eq "officestaf" || $access eq "admin") {
$logincode =qq~<b><font color="white">Logged in as <i>$firstname $lastname</i>  [ <a href=index.idx?action=logoff class=navheader>Logout</a> ]</font></b><br>~;
$navbarcode .=qq~
<div id="navmenu">
	<ul class="vert_header">
		<li>Navigation</li>
	</ul>
	<ul class="vert">
		<li><a href=index.idx>Home</a></li>
		<li><a href=callsleads.idx>Calls and Leads</a></li>
		<li><a href=transactions.idx>Transactions</a></li>
		<li><a href=contacts.idx?agent=$mymlsid>Contacts</a></li>
		<li><a href=prospecting.idx?agent=$mymlsid>Prospecting NEW!</a></li>
		<li><a href=prospects.idx?agent=$mymlsid>Prospects<font color=red>old</font></a></li>
		<li><a href=mortgageapps.idx>Mortgage Apps</a></li>
		<li><a href=tasks.idx>Action Plans</a></li>
		<li><a href=nocall.idx>Do Not Call List</a></li>
	</ul>
</div>
<div class="spacer"></div>
<div id="navmenu">
	<ul class="vert_header">
		<li>MuncieMLS Connection</li>
	</ul>
	<ul class="vert">
		<li><a href=webleads.idx?agent=$mymlsid>Web Clients/Leads</a></li>
		<li><a href=webleads.idx?action=viewclients&agent=$mymlsid>View Web Clients</a></li>
		<li><a href=webleads.idx?action=viewleads&agent=$mymlsid>View Web Leads</a></li>
		<li><a href=webleads.idx?action=massemail&agent=$mymlsid>MASS E-Mailer</a></li>
		<li><a href=webleads.idx?action=templateeditor&agent=$mymlsid>E-Mail Templates</a></li>
		<li><a href=webleads.idx?action=agentedit&agent=$mymlsid class=leftmenu>Update Web Profile</a></li>
	</ul>
</div>
<div class="spacer"></div>
<div id="navmenu">
	<ul class="vert_header">
		<li>Tools / Features</li>
	</ul>
	<ul class="vert">
		<li><a href=transactions.idx?action=directory>Office Directory</a></li>
		<li><a href=count.idx?agent=$mymlsid>Character Counter</a></li>
		<li><a href=http://www.orrhomes.com/showcase_payments.idx target=_blank>Payment Calc.</a></li>
		<li><a href=pull_emails.idx>Pull Agent E-mails</a></li>
		<li><a href=new_export_contacts.idx?agent=$mymlsid>Export My Contacts</a></li>
		<li><a href=new_export_prospects.idx?agent=$mymlsid>Export My Prospects</a></li>
		<li><a href=site_updates.idx?agent=$mymlsid>Site Change Log</a></li>
	</ul>
</div>
<div class="spacer"></div>
<div id="navmenu">
	<ul class="vert_header">
		<li>Eagle Merchandise</li>
	</ul>
	<ul class="vert">
		<li><a href=http://www.queensboro.com/eaglere>Eagle Embroidary</a></li>
		<li><a href=http://www.cafepress.com/eaglerealestate>Apparel,Hats, Etc.</a></li>
	</ul>
</div>
<br> ~;
$loggedin = "yes";
}
else {
print qq~ NO ACCESS! ~;
}
}

unless ($navbarcode) {

#### email address doesnt match userid record
#### 
#### normally caused by updating profile and changing the email address on file

$navbarcode = "<b>Welcome Agent!<br>Not Logged In</b><br><br><img src=images/top_icon.gif><a href=index.idx class=leftmenu>Login Now</a><br><br><b>I Am A New User</b><br><img src=images/top_icon.gif><a href=register.idx class=leftmenu>Register Free</a><br>";

print "Set-cookie: userid=\n";
print "Content-type: text/html\n\n";
&header;
print qq~

		<td valign="top" width="830">
		<br>

		<table border="0" cellpadding="5" cellspacing="0" width="100%">
		<tr>
		<td valign="top">
		<font face="Arial, Helvetica, sans-serif"><font size="3" color="Red">Please login using the above form.  If you just changed your email address, please login using the new one.</b></font></font><br><br>
</td></tr></table>

~;
&footer;

exit 0;

}

$dbh->disconnect;
}
else {

$navbarcode = "<b>Welcome Guest!<br>Not Logged In</b><br><br><b>I Have An Account</b><br><img src=images/top_icon.gif><a href=index.idx class=leftmenu>Login Now</a><br><br><b>I Am A New User</b><br><img src=images/top_icon.gif><a href=register.idx class=leftmenu>Register Free</a><br>";

}
}



print "";

sub header {

if ($display_full) {
open(HEADER,"includes/mainpage_full.txt");
@header = <HEADER>;
close(HEADER);
}
else {
open(HEADER,"includes/mainpage.txt");
@header = <HEADER>;
close(HEADER);
}

unless ($navbarcode) {
$navbarcode = "<b>Welcome Guest!<br>Not Logged In</b><br><br><b>I Have An Account</b><br><img src=images/top_icon.gif><a href=index.idx class=leftmenu>Login Now</a><br><br><b>I Am A New User</b><br><img src=images/top_icon.gif><a href=register.idx class=leftmenu>Register Free</a><br>";
}

if ($loggedin eq "yes") {
	$topcode = "<img src=\"images/line_spacer.gif\" height=\"1\" width=\"141\">";
}
else {
$topcode = "	<input type=hidden name=action value=login>
	<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">
	<tr>
		<td>
			<img src=\"images/quicklogin/top.gif\"><br>
		</td>
	</tr>
	<tr>
		<td>
			<center><input type=\"text\" name=\"Email\" style=\"color: #000000; font-family: Arial; font-size: 10px; border: 1px solid #000000; padding-left: 4; padding-right: 4; padding-top: 1; padding-bottom: 1; background-color: #FFFFFF\"></center>
		</td>
	</tr>
	<tr>
		<td>
			<img src=\"images/quicklogin/pass.gif\"><br>
		</td>
	</tr>
	<tr>
		<td>
			<center><input type=\"password\" name=\"Password\" style=\"color: #000000; font-family: Arial; font-size: 10px; border: 1px solid #000000; padding-left: 4; padding-right: 4; padding-top: 1; padding-bottom: 1; background-color: #FFFFFF\"></center>
		</td>
	</tr>
	<tr>
		<td>
			<center><input type=\"image\" src=\"images/quicklogin/button.gif\"></center>
		</td>
	</tr>
	</table>";

}


if ($sponsor_info{$site_sponsor}{image_url}) {
$image_url_for_top_right_of_site =qq~

					<td width="490" height="125" valign="top">
						<img border="0" src=" $sponsor_info{$site_sponsor}{image_url}" height="125"><br>
					</td>
~;
}
else {
$image_url_for_top_right_of_site =qq~ 

					<td width="112" height="125" valign="top">
						<img border="0" src="images/eaglelogo_top.gif" width="112" height="125"><br>
					</td>
					<td valign="top" width="365" height="125">
						<img border="0" src="images/top.gif" width="365" height="125"><br>
					</td>
~;
}


foreach $line (@header) {
$line =~ s/\$logincode/$logincode/g;
$line =~ s/\$navbarcode/$navbarcode/g;
$line =~ s/\<\!\-\-INSERTTOPHEADEREND-->/$topcode/gi;
$line =~ s/<!--INSERTYARDSPONSORIMAGE-->/$image_url_for_top_right_of_site/gi;
	if ($INPUT{query}) {
		$line =~ s/<!--search_code1-->/$INPUT{query}/gi;
		$line =~ s/<!--search_code2-->/$INPUT{query}/gi;
	}
	else {
		$line =~ s/<!--search_code1-->/Enter Search/gi;
		$line =~ s/<!--search_code2-->//gi;
	}
print "$line";
}


}



sub footer {

open(FOOTER,"includes/footer.txt");
@footer = <FOOTER>;
close(FOOTER);

foreach $line (@footer) {
print "$line";
}

#print qq~<br><br>SQL Queries: $sql_query_count~ if $sql_query_count;

}

sub showfeatured {
print <<"top";

<table border="0" cellpadding="3" cellspacing="0" width="121" bgcolor="\white">
<tr>
<td valign="middle">
<b><font color="Red">Featured Property</font></b>
</td>
</tr>
</table>
<table border="0" cellpadding="6" cellspacing="0" width="121"> 
top

$d_name = "muncieml_db1";
$d_username = "muncieml_db1";
$d_pass = "\@rockdb1";


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 $uasth = $dbh->prepare("select Price,AddressNumber,AddressDirection,AddressStreet,City,Zip,Status,MLSNUM,Bedrooms,FullBaths,HalfBaths,TotalSqFt,Acres,AgentID,AgentName,CoAgentID,CoAgentName,ListingOffiice1Name,ListingOffice1Phone,ListingOffice1ID,Remarks from listings_res where ListingOffice1ID = '130' order by RAND() limit 1");
$uasth->execute;

$dbh->disconnect;

while (($Price,$AddressNumber,$AddressDirection,$AddressStreet,$City,$Zip,$Status,$MLSNUM,$Bedrooms,$FullBaths,$HalfBaths,$TotalSqFt,$Acres,$AgentID,$AgentName,$CoAgentID,$CoAgentName,$ListingOffiice1Name,$ListingOffice1Phone,$ListingOffice1ID,$Remarks) = $uasth->fetchrow_array()) {


open(DBFILE,"/home/muncieml/public_html/remarksdb.txt");
@dbfile = <DBFILE>;
close(DBFILE);

foreach $dbline (@dbfile) {
$dbline =~ s/\n//gi;
if ($dbline) {
$Remarks =~ s/(.*?)$dbline//gi;
}
#$makestuff .= "$dbline\n";
}

$Remarks =~ s/CHR1/\'/gi;
$Remarks =~ s/CHR2/\"/gi;

if (-e "/home/muncieml/public_html/images/photos/thumbnail/meiar$MLSNUM\.jpg") {
$photourl = "images/photos/thumbnail/meiar$MLSNUM\.jpg";
}
else {
$photourl = "http://www.eagle-online.com/images/featured_nophoto.gif";
}

$Price = &commas($Price);

print qq~

<tr>
<td valign="top">
<b>$AddressNumber $AddressDirection $AddressStreet<br>
$City, IN $Zip<br></b>
<b>Offered at \$$Price</b><br><br>
<b>Presented By:</b><br>
<a href="agents.idx?action=viewagent&agent=$AgentID">$AgentName</a><br><br>
<center><a href="showdetails.idx?mlsnumber=$MLSNUM"><img src="$photourl" border="0"></a></center><br>
<center><font size="2"><b><a href="showdetails.idx?mlsnumber=$MLSNUM">View Listing Details</a></b></font></center><br>
<b>Remarks:</b> $Remarks
</font>
<br><br>
</td>
</tr>
~;
}


print <<"TheEND";
</table>
TheEND
}

sub checklogin {

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(/=/,$_);
	$cookieName =~ s/ //gi;
	$Cookies{$cookieName}=$cookieValue;
}

if ($Cookies{userid} =~ /\|/) {
($Cook{userid},$Cook{email}) = split(/\|/,$Cookies{userid});
}
else {
($Cook{userid},$Cook{email}) = split(/\-/,$Cookies{userid});
}

if ($Cook{userid} eq "" || $Cook{userid} eq "none") {
print qq~
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>Login to Eagle Real Estate, Inc. CONNECT Real Estate Management System</title>
<link rel="stylesheet" href="includes/login.css" type="text/css" media="all" />
</head>
<body>
	<div id="container">
			<div id="logo"></div>
			<div id="connectlogo"></div>
			<div id="content">
				<form method="post" action="index.idx">
				<input type="hidden" name="action" value="login"/>
				email address<br/>
				<input type="text" name="Email" class="form"/><br/>
				password<br/>
				<input type="password" name="Password"/><br/>
					<div id="animated"></div>
				<input type="submit" value="login" class="button"/><br/>
				</form>
			</div>
		<span class="cl"></span>
		<div id="footer">&copy; Copyright 2008 Eagle Real Estate, Inc.  For Office Staff and Agent use only!</div>
	</div>
</body>
</html>
~;
exit 0;
}

}


}

sub image_format {

use DBI;
my $e_dsn = "DBI:mysql:database=$d_name;host=localhost";
my $e_dbh = DBI->connect($e_dsn, $e_username, $e_pass, {RaiseError => 1}) || die $DBI::errstr;

my($cat,$mlsnumber) = @_;

$kpsth = $e_dbh->prepare("select photo_number,file_name from photos where mls_number = ? order by photo_number asc limit 1");
$kpsth->execute($mlsnumber);

($data_ret) = $kpsth->fetchrow_hashref();
my %photo_d = %$data_ret;
my $show_picture;

$photo_url_pre = "http://media-meiar.idxnow.com";

if ($photo_d{file_name} ne "") {
   $show_picture = "$photo_url_pre/$cat/$photo_d{file_name}";
}
else {
   $show_picture = "";
}

return $show_picture;

}

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


sub Log {
my($message_log) = @_;

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 $vasth = $dbh->prepare("select firstname,lastname from users where userid = '$Cook{userid}' AND email = '$Cook{email}'");
$vasth->execute;
while (($firstname,$lastname) = $vasth->fetchrow_array()) {
$name = "$firstname $lastname";
}

$ipadd = $ENV{REMOTE_ADDR};
$client = $ENV{HTTP_USER_AGENT};

@variables = ($Cook{agentid},$message_log,$name,$ipadd,$client);
$sql_query = "INSERT INTO trans_boardactivity values ('',?,now(),?,?,?,?)";
my $aasth = $dbh->do($sql_query, undef, @variables);

}

sub pull_photo_data_for_property_details_page_print {
my($mlsnumber,$show_per_row) = @_;

#######################################
#######################################

# pull all photos from database with MLS#
# image file names are returned as an array
@property_photos = &image_format_full($mlsnumber);

# 0 references the first item in the array.
# pull first image and use as big photo on the page
if ($property_photos[0]) {
   $showpicture = "$photo_url_pre/full/".$property_photos[0];
}
else {
   $showpicture = "images/nophoto.gif";
}

# loop through all of the available photos for this property.
# $photo becomes the file name of the property photo
# $photo_url_pre is the base URL to the photo storage system
foreach $photo (@property_photos) {
   $anypictures = "yes";
   $totalpictures++;
unless ($totalpictures == 1) {
   $buildscript .= qq~	testurl[$totalpictures]="$photo_url_pre/full/$photo"\n~;
   $showthumbs .= qq~<td width="182" bgcolor="white"><a href="javascript: showupdate($totalpictures)"><img src="$photo_url_pre/full/$photo" height="135" width="182" border=0></a><br></td>~;

   if ($tablecount == $show_per_row) {
      # make a new row after 10 photos have been printed to current row
      $showthumbs .= "</tr>\n\n<tr>";
      $tablecount = "0";
   }
   else {
      $tablecount++;
   }
}
}


#######################################
#######################################


}



sub image_format_full {
my($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;

#if {
#AND (photo_number = '4' OR photo_number = '3' OR photo_number = '1')
#}


if ($INPUT{dis_photos}) {
$dis_sql_query .= "AND (";
	(@dis_photos) = split(/\,/, $INPUT{dis_photos});
	foreach $temp_dis_photos (@dis_photos) {
	$dis_sql_query .= "photo_number = '$temp_dis_photos' OR ";
	}
$dis_sql_query .= "ENDOFFIELD";
$dis_sql_query =~ s/ OR ENDOFFIELD//gi;
$dis_sql_query .= ") ";
}

print "$sql_query";

$gpsth = $dbh->prepare("select photo_number,file_name from photos where mls_number = ? $dis_sql_query $photos_to_show limit 9");
$gpsth->execute($mlsnumber);

while ($data_ret = $gpsth->fetchrow_hashref()) {
   my %photo_d = %$data_ret;
   push @property_photos, $photo_d{file_name};
}

return @property_photos;

}