#!/usr/bin/perl

require "config.idx";

#%bad_agentnames_last	=	(
#				'Inactive Agent'	=>	1,
#				'Broker-comps'	=>	1,
#				'Inactive Sec'	=>	1,
#				'Subdivision'	=>	1,
#				'Office'		=>	1,
#				'Information'	=>	1
#				);

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

&checklogin;

if ($INPUT{action} eq "directory_view") { &directory_view; }
elsif ($INPUT{action} eq "directory_add") { &directory_add; }
elsif ($INPUT{action} eq "directory_adddo") { &directory_adddo; }
elsif ($INPUT{action} eq "directory_edit") { &directory_edit; }
elsif ($INPUT{action} eq "directory_editdo") { &directory_editdo; }
elsif ($INPUT{action} eq "directory_delete") { &directory_delete; }
elsif ($INPUT{action} eq "listnew_view") { &listnew_view; }
elsif ($INPUT{action} eq "listnew_save") { &listnew_save; }
elsif ($INPUT{action} eq "list_old") { &list_old; }
else { &main; }

sub list_old {

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;

$total_new_contacts_found = 0;

print qq~
<form method=post action=directoryadmin.idx>
<input type=hidden name=action value="listnew_save">
~;

my $gasth = $dbh->prepare("SELECT * FROM mlsusers");
$gasth->execute();

while ($db_agent = $gasth->fetchrow_hashref()) {
     $needs_updated = 0;

     %mls_agent = %$db_agent;

     my $kasth = $dbh->prepare("SELECT * FROM organizations WHERE officeid=?");
     $kasth->execute($mls_agent{officeid});

     $db_office = $kasth->fetchrow_hashref();
     %mls_office = %$db_office;

     my $tasth = $dbh->prepare("SELECT * FROM trans_boarddirectory WHERE firstname = ? AND lastname = ? LIMIT 1");
     $tasth->execute($mls_agent{firstname},$mls_agent{lastname});
     $db_directory = $tasth->fetchrow_hashref();
     %dir_agent = %$db_directory;

     if ($mls_office{officename} ne $dir_agent{company}) {
          $needs_updated = 1;
     }

	$mls_agent{phone} =~ s/\s+$//;
	
     if ($mls_agent{phone} ne $dir_agent{agent1phone}) {
          $needs_updated = 1;
     }

if ($needs_updated eq "1") { print qq~
EAGLE Directory: "$dir_agent{firstname}" "$dir_agent{lastname}" "$dir_agent{agent1phone}" "$dir_agent{company}"<br>
MEIAR Directory: "$mls_agent{firstname}" "$mls_agent{lastname}" "$mls_agent{phone}" "$mls_office{officename}"<br><br>
~; }

     ## etc.

} 


print qq~<br><br><input type=submit value="  Save New Contacts  "></form>~;
print qq~<br><br>Found $total_new_contacts_found total new contacts~;

}


sub directory_delete {


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{do} eq "yes") {
my $aasth = $dbh->do("DELETE FROM trans_boarddirectory where id = '$INPUT{id}'");
$status_printout = "Successfully removed person from database!";
&main;

exit 0;

}
else {

print qq~
<html>
<head>
<title>Delete Person from Office Directory - Eagle Real Estate, Inc. - Connect.EagleSold.com</title>
~;

&header;

print qq~
<form method="post" action="directoryadmin.idx">
<input type="hidden" value="directory_delete" name="action">
<input type="hidden" value="$INPUT{id}" name="id">
<input type="hidden" value="yes" name="do">
<td valign="top" width="581">
<img src="images/headers/transaction_manager.gif" height="33" width="581" alt="My Transaction Manager"><br><br>
<font size="4" face="Arial" color="red">Really Delete Person from Directory?<br><br>
<br><br>
</font>
<br>
Are you sure you want to remove?<br><a href="directoryadmin.idx">No [ Cancel ]</a> or <input type="submit" value="Yes [ Permanetly Delete ]">
</form>
~;

print qq~
</td>
~;

&footer;

}



}


sub directory_adddo {

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;

@variables = ($INPUT{type},$INPUT{firstname},$INPUT{lastname},$INPUT{agent1type},$INPUT{agent1phone},$INPUT{agent2type},$INPUT{agent2phone},$INPUT{agent3type},$INPUT{agent3phone},$INPUT{agentfax},$INPUT{email},$INPUT{companyname},$INPUT{coaddress},$INPUT{cocity},$INPUT{costate},$INPUT{cozip},$INPUT{cophone},$INPUT{showhide});
$sql_query = "INSERT INTO trans_boarddirectory values ('',?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
my $aasth = $dbh->do($sql_query, undef, @variables);

my $abasth = $dbh->prepare("select id from trans_boarddirectory ORDER BY id DESC LIMIT 0,1");
$abasth->execute;

while (($thedirectoryid) = $abasth->fetchrow_array()) {
$directoryid = $thedirectoryid;
}


print qq~
<html>
<head>
<title>Making Changes to Datebase</title>
<meta http-equiv="Refresh" content="1; URL=http://connect.eaglesold.com/directoryadmin.idx?action=directory_view&directoryid=$directoryid">
</head>
<body bgcolor="white">
<br><br><br><br>
<center><font size="3" face="Arial" color="Red">Please wait, Processing</font><br>You will be redirected to when finished.  If not <a href="http://connect..com/directoryadmin.idx?action=directory_view&directoryid=$INPUT{directoryid}">Click Here</a>.</center>
</body>
</html>
~;


}

sub directory_add {

print qq~
<html>
<head>
<title>Company Directory -- Eagle Real Estate, Inc. - Connect.EagleSold.com</title>
~;

&header;

print qq~

<td valign="top" width="581">
<img src="images/headers/transaction_manager.gif" height="33" width="581" alt="My Transaction Manager"><br><br>
<img src="images/trans_sys/company_directory.gif" alt="Eagle Company Directory"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Navigation:</b> <a href="directoryadmin.idx">Return To Directory Edit Search</a><br><br>
<br>
<img src="images/trans_sys/contact_information.gif" alt="Contact Information"><br>

<table border="0" cellpadding="3" cellspacing="1" width="100%">
<tr>
<td valign="top" colspan="2" bgcolor="black">
<center>
<font color="white"><b>Edit Contact Information</b></font>
</center>
</td>
</tr>
<form method="get" action="directoryadmin.idx">
<input type="hidden" name="action" value="directory_adddo">
<tr>
<td valign="top" width="40%"><b>Name</b></td>
<td valign="top" width="60%">
<b>Firstname</b><br>
<input type="text" name="firstname" size="35" class="form"><br>
<b>Lastname</b><br>
<input type="text" name="lastname" size="35" class="form"><br>
</td>
</tr>
<tr>
<td valign="top" bgcolor="#CCCCFF">
<b>Contact Information</b><br>
</td>
<td valign="top" bgcolor="#CCCCFF">
<b>Phone 1 Type</b><br>
<select name="agent1type" class="form">
<option value="">-- Select Type --</option>
<option value="">--------</option>
<option value="Mobile">Mobile</option>
<option value="Voicemail">Voicemail</option>
<option value="Home">Home</option>
<option value="Home Office">Home Office</option>
<option value="Office Fax">Office Fax</option>
<option value="Agent Fax">Agent Fax</option>
<option value="Pager">Pager</option>
</select> <b>Number</b> <input type="text" name="agent1phone" class="form"><br><br>
<b>Phone 2 Type</b><br>
<select name="agent2type" class="form">
<option value="">-- Select Type --</option>
<option value="">--------</option>
<option value="Mobile">Mobile</option>
<option value="Voicemail">Voicemail</option>
<option value="Home">Home</option>
<option value="Home Office">Home Office</option>
<option value="Office Fax">Office Fax</option>
<option value="Agent Fax">Agent Fax</option>
<option value="Pager">Pager</option>
</select> <b>Number</b> <input type="text" name="agent2phone" class="form"><br><br>
<b>Phone 3 Type</b><br>
<select name="agent3type" class="form">
<option value="">-- Select Type --</option>
<option value="">--------</option>
<option value="Mobile">Mobile</option>
<option value="Voicemail">Voicemail</option>
<option value="Home">Home</option>
<option value="Home Office">Home Office</option>
<option value="Office Fax">Office Fax</option>
<option value="Agent Fax">Agent Fax</option>
<option value="Pager">Pager</option>
</select> <b>Number</b> <input type="text" name="agent3phone" class="form"><br><br>
<b>Fax</b><br>
<select class="form">
<option>Primary Fax</option>
</select> <b>Number</b> <input type="text" name="agentfax" class="form"><br><br>

<b>E-Mail Address</b><br>
<input type="text" name="email" size="35" class="form"><br><br>
<b>Contact Type:</b> <select name="type" class="Form">
<option value="REALTOR">REALTOR</option>
<option value="Inspector">Inspector</option>
<option value="Appraiser">Appraiser</option>
<option value="Attorney">Attorney</option>
<option value="Contractor">Contractor</option>
<option value="Title Company">Title Company</option>
<option value="Support">Support</option>
<option value="Office Staff">Office Staff</option>
</select>
<br>
</td>
</tr>
<tr>
<td valign="top"><b>Company Information:</b><br></td>
<td valign="top">
<b>Company</b><br>
<input type="text" name="companyname" size="35" class="form"><br>
<b>Company Address</b><br>
<input type="text" name="coaddress" size="35" class="form"><br>
<b>City</b><br>
<input type="text" name="cocity" size="35" class="form"><br>
<b>State</b><br>
<input type="text" name="costate" size="35" class="form"><br>
<b>Zip</b><br>
<input type="text" name="cozip" size="5" class="form"><br>
<b>Company Phone</b><br>
<input type="text" name="cophone" size="35" class="form"><br><br>
</font>
</td>
</tr>
<tr>
<td valign="top" bgcolor="#CCCCFF">
<b>Is this contact active or inactive?</b><br>Making this contact inactive will hide from the office directory and showing setups.  If the contact has switched companies DO NOT fill out new company information.  There new information and company has already been transfered into the directory!!!
</td>
<td valign="top" bgcolor="#CCCCFF">
<b>Status</b><br>
<select name="showhide" class="form">
<option value="active">Active [ Show In Directory ]</option>
<option value="inactive">IN-Active [ DO NOT Show In Directory ]</option>
</select>
<tr>
<td colspan="2">
<center><input type="submit" value="Save Changes To This Person" class="form"> <a href="directoryadmin.idx">Cancel</a></center><br>
</td>
</tr>
</form>
</table>
~;

print qq~
</td>
~;

&footer;

}

sub directory_editdo {

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;

@variables = ($INPUT{type},$INPUT{firstname},$INPUT{lastname},$INPUT{agent1type},$INPUT{agent1phone},$INPUT{agent2type},$INPUT{agent2phone},$INPUT{agent3type},$INPUT{agent3phone},$INPUT{agentfax},$INPUT{email},$INPUT{companyname},$INPUT{coaddress},$INPUT{cocity},$INPUT{costate},$INPUT{cozip},$INPUT{cophone},$INPUT{showhide},$INPUT{directoryid});
$sql_query = "UPDATE trans_boarddirectory set type = ?, firstname = ?, lastname = ?, agent1type = ?, agent1phone = ?, agent2type = ?, agent2phone = ?, agent3type = ?, agent3phone = ?, agentfax = ?, email = ?, company = ?, coaddress = ?, cocity = ?, costate = ?, cozip = ?, cophone = ?, showhide = ? where id = ?";
my $aasth = $dbh->do($sql_query, undef, @variables);

print qq~
<html>
<head>
<title>Making Changes to Datebase</title>
<meta http-equiv="Refresh" content="1; URL=http://connect.EagleSold.com/directoryadmin.idx?action=directory_view&directoryid=$INPUT{directoryid}">
</head>
<body bgcolor="white">
<br><br><br><br>
<center><font size="3" face="Arial" color="Red">Please wait, Processing</font><br>You will be redirected to when finished.  If not <a href="http://connect.EagleSold.com/directoryadmin.idx?action=directory_view&directoryid=$INPUT{directoryid}">Click Here</a>.</center>
</body>
</html>
~;


}

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 $abasth = $dbh->prepare("select company from trans_boarddirectory GROUP BY company ASC");
$abasth->execute;

while (($company) = $abasth->fetchrow_array()) {
$buildcompany .= "<option value=\"$company\">$company</option>";
}

print qq~
<html>
<head>
<title>Company Directory -- Eagle Real Estate, Inc. - Connect.EagleSold.com</title>
~;

&header;


print qq~

<td valign="top" width="581">
<img src="images/headers/transaction_manager.gif" height="33" width="581" alt="My Transaction Manager"><br><br>
<img src="images/trans_sys/company_directory.gif" alt="Eagle Company Directory"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>view contacts and information the directory</b><br><br>
$status_printout
<br>
<font size="4" face="Arial">Find A Contact To Manage Below</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="directoryadmin.idx?action=directory_add">Add New Contact</a> | <a href="directoryadmin.idx?action=listnew_view" target="_blank">Import Contacts</a><br>

<table border="0" cellpadding="6" cellspacing="1" bgcolor="navy" width="100%">
<tr>
<td bgcolor="#CCCCFF">
<table border="0" cellpadding="3" cellspacing="0" width="100%">
<tr>
<td>
<b><font size="3">Search By Company</font></b><br>
</td>
<form method="get" action="directoryadmin.idx">
<td align="right">
<select name="company" class="form">
$buildcompany
</select> <input type="submit" value="Search!" class="form">
</td>
</form>
</tr>
</table>
</td>
</tr>
</table>
<table border="0" cellpadding="6" cellspacing="1" bgcolor="navy" width="100%">
<tr>
<td bgcolor="#CCCCFF">
<table border="0" cellpadding="3" cellspacing="0" width="100%">
<tr>
<td><b><font size="3">Search Company Directory By Lastname</font></b><br></td>
<td align="right">
<br>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor="white">
<center>
<a href="directoryadmin.idx?contact=%">All</a> 
<a href="directoryadmin.idx?contact=A">A</a> | 
<a href="directoryadmin.idx?contact=B">B</a> | 
<a href="directoryadmin.idx?contact=C">C</a> | 
<a href="directoryadmin.idx?contact=D">D</a> | 
<a href="directoryadmin.idx?contact=E">E</a> | 
<a href="directoryadmin.idx?contact=F">F</a> | 
<a href="directoryadmin.idx?contact=G">G</a> | 
<a href="directoryadmin.idx?contact=H">H</a> | 
<a href="directoryadmin.idx?contact=I">I</a> | 
<a href="directoryadmin.idx?contact=J">J</a> | 
<a href="directoryadmin.idx?contact=K">K</a> | 
<a href="directoryadmin.idx?contact=L">L</a> | 
<a href="directoryadmin.idx?contact=M">M</a> | 
<a href="directoryadmin.idx?contact=N">N</a> | 
<a href="directoryadmin.idx?contact=O">O</a> | 
<a href="directoryadmin.idx?contact=P">P</a> | 
<a href="directoryadmin.idx?contact=Q">Q</a> | 
<a href="directoryadmin.idx?contact=R">R</a> | 
<a href="directoryadmin.idx?contact=S">S</a> | 
<a href="directoryadmin.idx?contact=T">T</a> | 
<a href="directoryadmin.idx?contact=U">U</a> | 
<a href="directoryadmin.idx?contact=V">V</a> | 
<a href="directoryadmin.idx?contact=W">W</a> | 
<a href="directoryadmin.idx?contact=X">X</a> | 
<a href="directoryadmin.idx?contact=Y">Y</a> | 
<a href="directoryadmin.idx?contact=Z">Z</a>
</center><br>
~;

if ($INPUT{contact} or $INPUT{company}) {

if ($INPUT{contact}) {
$sql = "WHERE lastname LIKE '$INPUT{contact}%' ORDER by lastname";
$searchtype = "contact";
}
elsif ($INPUT{company}) {
$sql = "WHERE company = '$INPUT{company}' ORDER by company";
$searchtype = "company";
}
my $basth = $dbh->prepare("select id,firstname,lastname,company,showhide from trans_boarddirectory $sql ASC");
$basth->execute;

		$rowcolor{on} = "#FFFFFF";
		$rowcolor{off} = "#FFFF99";
		$color_count = "0";
		$row_switch = "on";

		print qq~
		<table border="0" cellpadding="2" cellspacing="1" width="100%" bgcolor="gray">
		<tr>
		~;

while (($directoryid,$firstname,$lastname,$company,$showhide) = $basth->fetchrow_array()) {

if ($showhide eq "inactive") {
$sshowhide = "<b><font color=\"red\">InActive</font></b>";
}
else {
$sshowhide = "";
}

$company = substr($company,0,11);
print qq~
<td bgcolor="$rowcolor{$row_switch}">
<img src="images/dude_small.gif"> $sshowhide <a href="directoryadmin.idx?action=directory_view&directoryid=$directoryid&searchtype=$searchtype&contact=$INPUT{contact}&company=$INPUT{company}">$lastname, $firstname - $company</a> <a href="directoryadmin.idx?action=directory_edit&directoryid=$directoryid&searchtype=$searchtype&contact=$INPUT{contact}&company=$INPUT{company}"><img src="images/trans_sys/button_edit.gif" border="0" alt="Edit"></a><br>
</td>
~;
	if ($color_count eq "1") {
	print qq~</tr><tr>~;
	$color_count = "0";
	}
	else {
	$color_count = "1";
	}

	if ($color_count eq "0") {
		if ($row_switch eq "on") {
			$row_switch = "off";
		}
		else {
			$row_switch = "on";
		}
	}

	
$ccfound = "yes";
}

print qq~<td bgcolor="$rowcolor{$row_switch}"> </td>~ if ($color_count eq "1");

		print qq~
		</tr>
		</table>
		~;

unless ($ccfound eq "yes") {
print qq~
<font color="red" size="3" face="Arial"><b>No Contacts Found, Click on the first letter in the alphabet above that corresponds with your contacts lastname to try another search.  If your contact is not listed you can create a new contact by clicking on the link above.</b></font><br>
~;
}
}
else {
print qq~
<font color="gray" size="3" face="Arial"><b>You may click on the first letter in the alphabet above that corresponds with your contacts lastname to search your contacts.</b></font><br>
~;
}

}

sub directory_view {

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 $basth = $dbh->prepare("select id,type,firstname,lastname,agent1type,agent1phone,agent2type,agent2phone,agent3type,agent3phone,agentfax,email,company,coaddress,cocity,costate,cozip,cophone,showhide from trans_boarddirectory WHERE id = '$INPUT{directoryid}'");
$basth->execute;



print qq~
<html>
<head>
<title>Company Directory -- Eagle Real Estate, Inc. - Connect.EagleSold.com</title>
~;

&header;

print qq~

<td valign="top" width="581">
<img src="images/headers/transaction_manager.gif" height="33" width="581" alt="My Transaction Manager"><br><br>
<img src="images/trans_sys/company_directory.gif" alt="Eagle Company Directory"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Navigation:</b> <a href="directoryadmin.idx">Return To Directory Edit Search</a><br><br>
<br>
<img src="images/trans_sys/contact_information.gif" alt="Contact Information"><br>
~;
while (($id,$type,$firstname,$lastname,$agent1type,$agent1phone,$agent2type,$agent2phone,$agent3type,$agent3phone,$agentfax,$email,$company,$coaddress,$cocity,$costate,$cozip,$cophone,$showhide) = $basth->fetchrow_array()) {
print qq~
<table border="0" cellpadding="5" cellspacing="1">
<tr>
<td valign="top" bgcolor="#CCCCFF">
<b><font size="3">Directory Contact Actions</font></b><br><br>
<a href="showingadmin.idx?action=quickload_1&loadcontact=yes&directoryid=$INPUT{directoryid}&name=$firstname $lastname from $company"> [ Setup A Showing ]</a><br><br>
<a href="directoryadmin.idx?action=directory_edit&directoryid=$INPUT{directoryid}">[ Edit Information ]</a><br><br>
<a href="directoryadmin.idx?action=directory_delete&id=$INPUT{directoryid}">[ Delete Contact ]</a><br>

</td>
<td valign="top">
<img src="images/admin/dude.gif"><br>
<font size="4" face="Arial">
Contact status: <b>$showhide</b><br>
$firstname $lastname<br>
$agent1type $agent1phone<br>
$agent2type $agent2phone<br>
$agent3type $agent3phone<br>
Fax: $agentfax<br>
E-Mail: $email<br><br><br>
Contact Type: $type<br><br>
</font>
<b>Company Information:</b><br>
<font size="4" face="Arial">
$company<br>
$coaddress<br>
$cocity, $costate $cozip<br>
Company Phone: $cophone<br><br><br>
</font>
</td>
</tr>
</table>
<br><br>
<font size="3" face="Arial"><b>Showings History</b></font><br>
<table border="0" cellpadding="2" cellspacing="0" width="100%">
<tr bgcolor="navy">
<td valign="top"><b><font color="white">Status</font></b></td>
<td valign="top"><b><font color="white">Property Address</font></b></td>
<td valign="top"><b><font color="white">MLS Number</font></b></td>
<td valign="top"><b><font color="white">Appointment</font></b></td>
</tr>
~;
my $aasth = $dbh->prepare("select status,mlsnumber,appointmentdate,timerange1,timerange2 from trans_boardshowings WHERE showingagent = '$id' ORDER BY appointmentdate desc");
$aasth->execute;

$trcolor = "1";
$trcolor2 = "2";

while (($status,$mlsnumber,$appointmentdate,$timerange1,$timerange2) = $aasth->fetchrow_array()) {

	my $aaaddress = $dbh->prepare("select addressnumber,addressdirection,addressstreet from trans_board where mlsnumber = '$mlsnumber' LIMIT 0,1");
	$aaaddress->execute;
	while (($addressnumber,$addressdirection,$addressstreet) = $aaaddress->fetchrow_array()) {
	$propertyaddress = "$addressnumber $addressdirection $addressstreet";
	}


	if ($trcolor eq $trcolor2) {
	$trcolor = "1";
	$setcolor = "#FFFFFF";
	}
	else {
	$setcolor = "#CCCCFF";
	$trcolor++;
	}

print qq~
<tr bgcolor="$setcolor">
<td valign="top"><b><center>$status</center></b></td>
<td valign="top"><b>$propertyaddress</b></td>
<td valign="top"><b><center>$mlsnumber</center></b></td>
<td valign="top"><b>$appointmentdate $timerange1-$timerange2</b></td>
</tr>
~;
$found = "yes";
}
unless ($found eq "yes") {
print qq~
<tr>
<td colspan="5">
<font color="red"><b>No Showings Found For This Contact</b></font>
</td>
</tr>
~;
}
print qq~
</table>
<br><br>
<font size="3" face="Arial"><b>Transaction(s) History as of December 1st, 2004</b></font><br>
<table border="0" cellpadding="2" cellspacing="0" width="100%">
<tr bgcolor="navy">
<td valign="top">&nbsp;</td>
<td valign="top"><b><font color="white">Address</font></b></td>
<td valign="top"><b><font color="white">City</font></b></td>
<td valign="top"><b><font color="white">Status</font></b></td>
<td valign="top"><b><font color="white">Sold Date</font></b></td>
<td valign="top"><b><font color="white">How Involved In Transaction</font></b></td>
</tr>
~;

my $casth = $dbh->prepare("select id,agent,status,selleragent,sellercoagent,buyeragent,buyercoagent,addressnumber,addressdirection,addressstreet,city,state,zip,solddate from trans_board WHERE (buyeragent = '$id' or buyercoagent = '$id' or sellercoagent = '$id' or selleragent = '$id') ORDER BY solddate desc");
$casth->execute;

$trcolor = "1";
$trcolor2 = "2";

while (($transid,$transagent,$status,$selleragent,$sellercoagent,$buyeragent,$buyercoagent,$addressnumber,$addressdirection,$addressstreet,$city,$state,$zip,$solddate) = $casth->fetchrow_array()) {

	if ($trcolor eq $trcolor2) {
	$trcolor = "1";
	$setcolor = "#FFFFFF";
	}
	else {
	$setcolor = "#CCCCFF";
	$trcolor++;
	}

if ($solddate eq '0000-00-00') {
$solddate = "Not Sold Yet";
}

if ($selleragent eq "$id") {
$transtype = "Seller Agent";
}
if ($sellercoagent eq "$id") {
$transtype = "Seller Co Agent";
}
if ($buyeragent eq "$id") {
$transtype = "Buyer Agent";
}
if ($buyercoagent eq "$id") {
$transtype = "Buyer Co Agent";
}
if ($selleragent eq $buyeragent) {
$transtype = "Buyer & Seller Agent";
}

print qq~
<tr bgcolor="$setcolor">
<td valign="top"><a href="transactions.idx?action=overview&transid=$transid&agent=$transagent">View</a></td>
<td valign="top"><a href="transactions.idx?action=overview&transid=$transid&agent=$transagent">$addressnumber $addressdirection $addressstreet</a></td>
<td valign="top"><a href="transactions.idx?action=overview&transid=$transid&agent=$transagent">$city</a></td>
<td valign="top"><b><center>$status</center></b></td>
<td valign="top"><b><center>$solddate</center></b></td>
<td valign="top"><b><center>$transtype</center></b></td>
</tr>
~;
$found = "yes";
}
unless ($found eq "yes") {
print qq~
<tr>
<td colspan="5">
<font color="red"><b>No History Found For This Contact</b></font>
</td>
</tr>
~;
}
print qq~
</table>
~;

print qq~
</td>
~;

&footer;
}
}

sub directory_edit {

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 $basth = $dbh->prepare("select id,type,firstname,lastname,agent1type,agent1phone,agent2type,agent2phone,agent3type,agent3phone,agentfax,email,company,coaddress,cocity,costate,cozip,cophone,showhide from trans_boarddirectory WHERE id = '$INPUT{directoryid}'");
$basth->execute;

print qq~
<html>
<head>
<title>Company Directory -- Eagle Real Estate, Inc. - Connect.EagleSold.com</title>
~;

&header;

print qq~

<td valign="top" width="581">
<img src="images/headers/transaction_manager.gif" height="33" width="581" alt="My Transaction Manager"><br><br>
<img src="images/trans_sys/company_directory.gif" alt="Eagle Company Directory"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Navigation:</b> <a href="directoryadmin.idx">Return To Directory Edit Search</a><br><br>
<br>
<img src="images/trans_sys/contact_information.gif" alt="Contact Information"><br>
~;
while (($id,$type,$firstname,$lastname,$agent1type,$agent1phone,$agent2type,$agent2phone,$agent3type,$agent3phone,$agentfax,$email,$company,$coaddress,$cocity,$costate,$cozip,$cophone,$showhide) = $basth->fetchrow_array()) {

if ($agent1type eq "") {
$getagent1type = "<option value=\"\">-- Select Type --</option>";
$getagent1type .= "<option value=\"\">--------</option>";
}
else {
$getagent1type = "<option value=\"$agent1type\">$agent1type</option>";
$getagent1type .= "<option value=\"\">--------</option>";
}

if ($agent2type eq "") {
$getagent2type = "<option value=\"\">-- Select Type --</option>";
$getagent2type .= "<option value=\"\">--------</option>";
}
else {
$getagent2type = "<option value=\"$agent2type\">$agent2type</option>";
$getagent2type .= "<option value=\"\">--------</option>";
}

if ($agent3type eq "") {
$getagent3type = "<option value=\"\">-- Select Type --</option>";
$getagent3type .= "<option value=\"\">--------</option>";
}
else {
$getagent3type = "<option value=\"$agent3type\">$agent3type</option>";
$getagent3type .= "<option value=\"\">--------</option>";
}

if ($showhide eq "active") {
$getshowhide = "<option value=\"$showhide\">Active [ Show In Directory ]</option>";
$getshowhide .= "<option value=\"\">--------</option>";
}
else {
$getshowhide = "<option value=\"inactive\">IN-Active [ DO NOT Show In Directory ]</option>";
$getshowhide .= "<option value=\"\">--------</option>";
}

print qq~
<table border="0" cellpadding="3" cellspacing="1" width="100%">
<tr>
<td valign="top" colspan="2" bgcolor="black">
<center>
<font color="white"><b>Edit Contact Information</b></font>
</center>
</td>
</tr>
<form method="get" action="directoryadmin.idx">
<input type="hidden" name="action" value="directory_editdo">
<input type="hidden" name="directoryid" value="$INPUT{directoryid}">
<tr>
<td valign="top" width="40%"><b>Name</b></td>
<td valign="top" width="60%">
<b>Firstname</b><br>
<input type="text" name="firstname" value="$firstname" size="35" class="form"><br>
<b>Lastname</b><br>
<input type="text" name="lastname" value="$lastname" size="35" class="form"><br>
</td>
</tr>
<tr>
<td valign="top" bgcolor="#CCCCFF">
<b>Contact Information</b><br>
</td>
<td valign="top" bgcolor="#CCCCFF">
<b>Phone 1 Type</b><br>
<select name="agent1type" class="form">
$getagent1type
<option value="Mobile">Mobile</option>
<option value="Voicemail">Voicemail</option>
<option value="Home">Home</option>
<option value="Home Office">Home Office</option>
<option value="Office Fax">Office Fax</option>
<option value="Agent Fax">Agent Fax</option>
<option value="Pager">Pager</option>
</select> <b>Number</b> <input type="text" name="agent1phone" value="$agent1phone" class="form"><br><br>
<b>Phone 2 Type</b><br>
<select name="agent2type" class="form">
$getagent2type
<option value="Mobile">Mobile</option>
<option value="Voicemail">Voicemail</option>
<option value="Home">Home</option>
<option value="Home Office">Home Office</option>
<option value="Office Fax">Office Fax</option>
<option value="Agent Fax">Agent Fax</option>
<option value="Pager">Pager</option>
</select> <b>Number</b> <input type="text" name="agent2phone" value="$agent2phone" class="form"><br><br>

<b>Phone 3 Type</b><br>
<select name="agent3type" class="form">
$getagent3type
<option value="Mobile">Mobile</option>
<option value="Voicemail">Voicemail</option>
<option value="Home">Home</option>
<option value="Home Office">Home Office</option>
<option value="Office Fax">Office Fax</option>
<option value="Agent Fax">Agent Fax</option>
<option value="Pager">Pager</option>
</select> <b>Number</b> <input type="text" name="agent3phone" value="$agent3phone" class="form"><br><br>

<b>Fax</b><br>
<select class="form">
<option>Primary Fax</option>
</select> <b>Number</b> <input type="text" name="agentfax" value="$agentfax" class="form"><br><br>

<b>E-Mail Address</b><br>
<input type="text" name="email" value="$email" size="35" class="form"><br><br>
<b>Contact Type:</b> <select name="type" class="Form">
~;

if ($type eq "MLS") {
print qq~<option value="MLS">MLS Locked</option>~;
}
else {
print qq~
<option value="Inspector">Inspector</option>
<option value="Appraiser">Appraiser</option>
<option value="Attorney">Attorney</option>
<option value="Contractor">Contractor</option>
<option value="Title Company">Title Company</option>
<option value="Support">Support</option>
<option value="MLS">SET TO MLS & LOCK</option>
~;
}
print qq~
</select>
<br>
</td>
</tr>
<tr>
<td valign="top"><b>Company Information:</b><br></td>
<td valign="top">
<b>Company</b><br>
<input type="text" name="companyname" value="$company" size="35" class="form"><br>
<b>Company Address</b><br>
<input type="text" name="coaddress" value="$coaddress" size="35" class="form"><br>
<b>City</b><br>
<input type="text" name="cocity" value="$cocity" size="35" class="form"><br>
<b>State</b><br>
<input type="text" name="costate" value="$costate" size="35" class="form"><br>
<b>Zip</b><br>
<input type="text" name="cozip" value="$cozip" size="5" class="form"><br>
<b>Company Phone</b><br>
<input type="text" name="cophone" value="$cophone" size="35" class="form"><br><br>
</font>
</td>
</tr>
<tr>
<td valign="top" bgcolor="#CCCCFF">
<b>Is this contact active or inactive?</b><br>Making this contact inactive will hide from the office directory and showing setups.  If the contact has switched companies DO NOT fill out new company information.  There new information and company has already been transfered into the directory!!!
</td>
<td valign="top" bgcolor="#CCCCFF">
<b>Status</b><br>
<select name="showhide" class="form">
$getshowhide
<option value="active">Active [ Show In Directory ]</option>
<option value="inactive">IN-Active [ DO NOT Show In Directory ]</option>
</select>
<tr>
<td colspan="2">
<center><input type="submit" value="Save Changes To This Person" class="form"> <a href="directoryadmin.idx">Cancel</a></center><br>
</td>
</tr>
</form>
</table>
~;
}

print qq~
</td>
~;

&footer;

}





sub listnew_save {

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;

$total_imported = 0;

@new_contacts = split(/\,/, $INPUT{newcontact});

foreach $con (@new_contacts) {

	my $gasth = $dbh->prepare("select * from mlsusers where agentid = ?");
	$gasth->execute($con);

	while ($db_agent = $gasth->fetchrow_hashref()) {
		%mls_agent = %$db_agent;

		my $kasth = $dbh->prepare("select * from organizations where officeid = ?");
		$kasth->execute($mls_agent{officeid});

		while ($db_office = $kasth->fetchrow_hashref()) {
			%mls_office = %$db_office;

	$mls_agent{phone} =~ s/\s+$//;

if ($mls_agent{lastname} =~ /Inactive/i) {
$enter_status = "inactive";
}
else {
$enter_status = "active";
}

			@variables = ($mls_agent{firstname},$mls_agent{lastname},$mls_agent{phone},$mls_office{officename},$mls_office{street},$mls_office{city},$mls_office{state},$mls_office{zip},$mls_office{phone},$enter_status);
			$sql_query = "INSERT INTO trans_boarddirectory values ('','MLS',?,?,'',?,'','','','','','',?,?,?,?,?,?,?)";

			my $vvaasth = $dbh->do($sql_query, undef, @variables);

			$total_imported++;

		}  ## end office loop


	}  ## end agent loop

}  ## end foreach new contact

print qq~$total_imported total imported contacts~;

}  ## end sub





sub listnew_view {

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;

$total_new_contacts_found = 0;

print qq~
<form method=post action=directoryadmin.idx>
<input type=hidden name=action value="listnew_save">
~;

my $gasth = $dbh->prepare("select * from mlsusers");
$gasth->execute;

while ($db_agent = $gasth->fetchrow_hashref()) {
	%mls_agent = %$db_agent;

	my $kasth = $dbh->prepare("select * from organizations where officeid = ?");
	$kasth->execute($mls_agent{officeid});

	while ($db_office = $kasth->fetchrow_hashref()) {
		%mls_office = %$db_office;
		$found_information = 0;

		my $casth = $dbh->prepare("select id from trans_boarddirectory where firstname = ? and lastname = ? and company = ?");
		$casth->execute($mls_agent{firstname},$mls_agent{lastname},$mls_office{officename});
		
		$id = $casth->fetchrow_array();
		$found_information = 1 if $id;

		my $to_show_it = 1;

		if ($found_information eq "0" && $mls_office{officename} !~ /FNISMLS/) {

			foreach $key (keys %bad_agentnames_last) {
				if ($mls_agent{lastname} =~ /$key/) {
					$to_show_it = 0;
				}
			}

			if ($to_show_it eq "1") {

				## new contact found
				print qq~<input type=checkbox name="newcontact" value="$mls_agent{agentid}"> $mls_agent{firstname} $mls_agent{lastname} - $mls_office{officename} &nbsp; &nbsp; $mls_agent{phone}<br>~;
				$total_new_contacts_found++;

			}
		}


	}  ## end office while loop


}  ## end agent while loop


print qq~<br><br><input type=submit value="  Save New Contacts  "></form>~;
print qq~<br><br>Found $total_new_contacts_found total new contacts~;

}  ## end sub







sub runsql {

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 $aaasth = $dbh->prepare("select agentid,officeid,lastname,firstname,phone from mlsusers");
$aaasth->execute;

while (($agentid,$officeid,$lastname,$firstname,$agentphone) = $aaasth->fetchrow_array()) {

my $baasth = $dbh->prepare("select officeid,officename,street,city,state,zip,phone from organizations where officeid = '$officeid'");
$baasth->execute;

while (($coofficeid,$officename,$costreet,$cocity,$costate,$cozip,$cophone) = $baasth->fetchrow_array()) {


### id,type,firstname,lastname,agent1type,agent1phone,agent2type,agent2phone,agent3type,agent3phone,agentfax,email,company,coaddress,cocity,costate,cozip,cophone ##


	@variables = ($firstname,$lastname,$agentphone,$officename,$costreet,$cocity,$costate,$cozip,$cophone);
	$sql_query = "INSERT INTO trans_boarddirectory values ('','mls',?,?,'',?,'','','','','','',?,?,?,?,?,?)";
	my $vvaasth = $dbh->do($sql_query, undef, @variables);

print qq~
	--- > Imported $firstname $lastname,$agentphone $coofficeid,$officename,$costreet,$cocity,$costate,$cozip,$cophone<br>
~;

}
}



}