#!/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; }
	}
}

if ($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~

<html>
<head>
<title>Login to Connect.EagleSold.com</title>
<link rel="stylesheet" TYPE="text/css" href="http://www.munciemls.com/includes/style.css">
<meta http-equiv="set-cookie" content="userid=; expires=; path=/"> 
</head>
<body bgcolor="white">
<center>
<table border="0" cellspacing="1" cellpadding="3" bgcolor="#CC0000">
	<form method=post action=index.idx>
	<input type=hidden name=action value=login>
<tr>
<td valign="top" colspan="2" bgcolor="white"><center><font size="3" color="maroon" face="Arial"><b>Connect.EagleSold.com</b></font><br><b><i>Access Center</i></b></center></td>
</tr>
<tr>
<td valign="middle" align="right" bgcolor="white"><b>Login</b></td>
<td valign="middle" bgcolor="white"><input type="text" name="Email" size="30" class="form"></td>
</tr>
<tr>
<td valign="middle" align="right" bgcolor="white"><b>Password</b></td>
<td valign="middle" bgcolor="white"><input type="password" name="Password" size="30" class="form"></td>
</tr>
<tr>
<td valign="top">&nbsp;</td>
<td valign="top"><input type="submit" value="Login" class="form"><br></td>
</tr>
</table>
</center>
</body>
</html>
~;
exit 0;
}

}


if ($INPUT{action} eq "create") {

$divider = "$INPUT{divider}";
$fileextension = "$INPUT{fileextension}";

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


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



@days   = ('Sunday','Monday','Tuesday','Wednesday',
               'Thursday','Friday','Saturday');
    @months = ('January','February','March','April','May','June','July',
	         'August','September','October','November','December');


    ($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
    $time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);
if ($hour eq "13") {
$hour = "1";
$pm = "y";
}if ($hour eq "14") {
$hour = "2";
$pm = "y";
}if ($hour eq "15") {
$hour = "3";
$pm = "y";
}if ($hour eq "16") {
$hour = "4";
$pm = "y";
}if ($hour eq "17") {
$hour = "5";
$pm = "y";
}if ($hour eq "18") {
$hour = "6";
$pm = "y";
}if ($hour eq "19") {
$hour = "7";
$pm = "y";
}if ($hour eq "20") {
$hour = "8";
$pm = "y";
}if ($hour eq "21") {
$hour = "9";
$pm = "y";
}if ($hour eq "22") {
$hour = "10";
$pm = "y";
}if ($hour eq "23") {
$hour = "11";
$pm = "y";
}if ($hour eq "0" || $hour eq "" || $hour eq "24") {
$hour = "12";
}
$newyear = $year+1900;
$time = sprintf("%02d:%02d",$hour,$min);
$tdate = "$days[$wday], $months[$mon] $mday, $newyear";
$mon = $mon + 1;



if ($pm eq "y") {
$tm = "pm";
}
else {
$tm = "am";
}

if ($mon < "10") {
$mon = "0$mon";
}

if ($mday < "10") {
$mday = "0$mday";
}

$actdate = "$newyear\_$mon\_$mday";

print "Content-type: application/octetstream\n";
print "Content-Disposition: filename=\"muncieml\_$actdate\.$fileextension\"\n";
print "Pragma: no-cache\n";
print "Expires: 0\n\n";

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;

$sql_query =qq~select id,agent,type,firstname,lastname,address,spouse,city,state,zip,phone1type,phone1number,phone2type,phone2number,phone3type,phone3number,faxnumber,email,emailtwo,contacttype,DATE_FORMAT(initial_contactdate, '%b %D, %Y'),DATE_FORMAT(last_contactdate, '%b %D, %Y'),DATE_FORMAT(next_contactdate, '%b %D, %Y'),source,notes from trans_boardcontact where agent = '$INPUT{agent}'~;

@search_vars = ();

if ($INPUT{contacttype}) {
$sql_query .= "AND (";
	(@contacttype) = split(/\,/, $INPUT{contacttype});
	foreach $contact (@contacttype) {
	$sql_query .= "contacttype LIKE ? $INPUT{matchtype} ";
	#push @search_vars, $contact;
	push @search_vars, '%' . $contact . '%';
	}
$sql_query .= "ENDOFFIELD";
$sql_query =~ s/ $INPUT{matchtype} ENDOFFIELD//gi;
$sql_query .= ") ";
}

my $uasth = $dbh->prepare("$sql_query");
$uasth->execute(@search_vars);

while (($id,$agent,$type,$firstname,$lastname,$address,$spouse,$city,$state,$zip,$phone1type,$phone1number,$phone2type,$phone2number,$phone3type,$phone3number,$faxnumber,$email,$emailtwo,$contacttype,$initial_contactdate,$last_contactdate,$next_contactdate,$source,$notes) = $uasth->fetchrow_array()) {

$firstname =~ s/\,//gi;
$lastname =~ s/\,//gi;
$spouse =~ s/\,//gi;
$city =~ s/\,//gi;
$state =~ s/\,//gi;
$zip =~ s/\,//gi;
$phone1number =~ s/\,//gi;
$phone2number =~ s/\,//gi;
$phone3number =~ s/\,//gi;
$faxnumber =~ s/\,//gi;
$email =~ s/\,//gi;
$notes =~ s/\,//gi;
$notes =~ s/\r\n/\;/g;
#$notes =~ s/\r\n/\=0D\=0A/g;

if ($spouse ne "") {
$spouse = " and $spouse";
}
else {
$spouse = "";
}

my $basth = $dbh->prepare("select noteid,contactid,DATE_FORMAT(datetime, '%b %D, %Y at %h\:%i%p'),addedby,note from trans_boardcontactnotes WHERE contactid = '$id' ORDER by datetime DESC");
$basth->execute;
while (($noteid,$contactid,$datetime,$addedby,$note) = $basth->fetchrow_array()) {
$note =~ s/\,//gi;
$note =~ s/\r\n/\;/g;
$addedby =~ s/\,//gi;
$addedby =~ s/\r\n/\;/g;
$datetime =~ s/\,//gi;
$datetime =~ s/\r\n/\;/g;
$fullnotes .= "$note Added by $addedby on $datetime -- ";
}

####### GET TRANSACTIONS CLIENT IS PART OF #####################
my $cbbaasth = $dbh->prepare("select transid,type from trans_boardtranstocontact WHERE contactid = '$id'");
$cbbaasth->execute;
while (($temptransid,$temptype) = $cbbaasth->fetchrow_array()) {
my $aasth = $dbh->prepare("select id,agent,selleragent,sellercoagent,buyeragent,buyercoagent,type,status,addressnumber,addressdirection,addressstreet,city,state,zip,pendingdate,estimatedclosingdate from trans_board WHERE id = '$temptransid' ORDER BY type");
$aasth->execute;
while (($transid,$transagent,$selleragent,$sellercoagent,$buyeragent,$buyercoagent,$type,$status,$addressnumber,$addressdirection,$addressstreet,$tcity,$tstate,$tzip,$pendingdate,$estimatedclosingdate) = $aasth->fetchrow_array()) {
$type =~ s/\,//gi;
$type =~ s/\r\n/\;/g;
$status =~ s/\,//gi;
$status =~ s/\r\n/\;/g;
$addressnumber =~ s/\,//gi;
$addressnumber =~ s/\r\n/\;/g;
$addressdirection =~ s/\,//gi;
$addressdirection =~ s/\r\n/\;/g;
$addressstreet =~ s/\,//gi;
$addressstreet =~ s/\r\n/\;/g;
$addresscity =~ s/\,//gi;
$addresscity =~ s/\r\n/\;/g;
$temptype =~ s/\,//gi;
$temptype =~ s/\r\n/\;/g;
$fulltrans .= "$type $status $addressnumber $addressdirection $addressstreet $tcity Involved As $temptype";
}
}
####### END GET TRANSACTIONS CLIENT IS PART OF #################

#########################
$fieldformat = "$firstname$spouse$divider$lastname$divider$address$divider$city$divider$state$divider$zip$divider$phone1number$divider$phone2number$divider$phone3number$divider$faxnumber$divider$email$divider Auto Transaction System Controlled$divider ::::FILE DETAILS::::   NOTES: $notes -- $fullnotes  --  TRANSACTION HISTORY: $fulltrans  --  ::::END OF FILE::::\r\n";

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



print "$fieldformat";
$fullnotes = "";
$fulltrans = "";

}



$dbh->disconnect;

}
else {

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


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;

print qq~
<html>
<head>
<title>Eagle Real Estate, Inc. - Export Contacts - 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>
<b><font size="4">Export Your Contacts</font></b><br><br>
&nbsp;&nbsp;&nbsp;&nbsp;This will allow you to export your contacts so you may import them into other contact management programs such as Microsoft Outlook.  Once you click on Build Export File Now button you will be asked to save or open the new file.  You may then save the file, load your contact management program and Import your information.
<form method="get" action="new_export_contacts.idx">
<input type="hidden" name="action" value="create">
<input type="hidden" name="agent" value="$INPUT{agent}">
<b>Choose contact types to export</b><br>
		<table border="1" cellpadding="3" cellspacing="1" width="100%" bgcolor="black">
		<tr>
		<td valign="top" bgcolor="white">
		<table border="0" cellpadding="1" cellspacing="0" width="100%">
		<tr>
		<td valign="top" bgcolor="white">
			<b>Include In:</b><br>
			<label for="contacttype_sphereofinfluence"><input id="contacttype_sphereofinfluence" type="checkbox" name="contacttype" value="sphereofinfluence"> <b><u>Sphere of Influence</u></b></label><br>
			<br>
			<b>Common Categories</b><br>
			<label for="contacttype_familymember"><input id="contacttype_familymember" type="checkbox" name="contacttype" value="familymember"> <b><u>Family Member</u></b></label><br>
			<label for="contacttype_friend"><input id="contacttype_friend" type="checkbox" name="contacttype" value="friend"> <b><u>Friend</u></b></label><br>
			<br>
			<b>Buyer Categories</b><br>
			<label for="contacttype_buyeractive"><input id="contacttype_buyeractive" type="checkbox" name="contacttype" value="buyeractive"> <b><u>Buyer - Active</u></b></label><br>
			<label for="contacttype_buyerpast"><input id="contacttype_buyerpast" type="checkbox" name="contacttype" value="buyerpast"> <b><u>Buyer - Past</u></b></label><br>
			<label for="contacttype_buyerprospective"><input id="contacttype_buyerprospective" type="checkbox" name="contacttype" value="buyerprospective"> <b><u>Buyer - Prospective</u></b></label><br>
			<label for="contacttype_buyercreditwork"><input id="contacttype_buyercreditwork" type="checkbox" name="contacttype" value="buyercreditwork"> <b><u>Buyer - Credit Work</u></b></label><br>
			<br>
			<b>Seller Categories</b><br>
			<label for="contacttype_selleractive"><input id="contacttype_selleractive" type="checkbox" name="contacttype" value="selleractive"> <b><u>Seller - Active</u></b></label><br>
			<label for="contacttype_sellerprospective"><input id="contacttype_sellerprospective" type="checkbox" name="contacttype" value="sellerprospective"> <b><u>Seller - Prospective</u></b></label><br>
			<label for="contacttype_sellerpast"><input id="contacttype_sellerpast" type="checkbox" name="contacttype" value="sellerpast"> <b><u>Seller - Past SOLD</u></b></label><br>
			<label for="contacttype_sellerpastns"><input id="contacttype_sellerpastns" type="checkbox" name="contacttype" value="sellerpastns"> <b><u>Seller - Past NOT SOLD</u></b></label><br>
		</td>
		<td valign="top" width="50%">
			<b>Info Line Call</b><br>
			<label for="contacttype_infoline_ideal"><input id="contacttype_infoline_ideal" type="checkbox" name="contacttype" value="infoline_ideal"> <b><u>InfoLine - IDEAL</u></b></label><br>
			<label for="contacttype_infoline_buyer"><input id="contacttype_infoline_buyer" type="checkbox" name="contacttype" value="infoline_buyer"> <b><u>InfoLine - Buyer</u></b></label><br>
			<label for="contacttype_infoline_seller"><input id="contacttype_infoline_seller" type="checkbox" name="contacttype" value="infoline_seller"> <b><u>InfoLine - Seller</u></b></label><br>
			<label for="contacttype_infoline_property"><input id="contacttype_infoline_property" type="checkbox" name="contacttype" value="infoline_property"> <b><u>InfoLine - Property For Sale</u></b></label><br>
			<br>
			<b>Recieve Mailings</b><br>
			<label for="contacttype_newsletter"><input id="contacttype_newsletter" type="checkbox" name="contacttype" value="newsletter"> <b><u>Newsletter</u></b></label><br>
			<br>
			<b>Professional Category</b><br>
			<label for="contacttype_appraiser"><input id="contacttype_appraiser" type="checkbox" name="contacttype" value="appraiser"> <b><u>Appraiser</u></b></label><br>
			<label for="contacttype_inspector"><input id="contacttype_inspector" type="checkbox" name="contacttype" value="inspector"> <b><u>Inspector</u></b></label><br>
			<label for="contacttype_insuranceagent"><input id="contacttype_insuranceagent" type="checkbox" name="contacttype" value="insuranceagent"> <b><u>Insurance Agent</u></b></label><br>
			<label for="contacttype_lender"><input id="contacttype_lender" type="checkbox" name="contacttype" value="lender"> <b><u>Lender</u></b></label><br>
			<label for="contacttype_attorney"><input id="contacttype_attorney" type="checkbox" name="contacttype" value="attorney"> <b><u>Attorney</u></b></label><br>
			<label for="contacttype_titleagent"><input id="contacttype_titleagent" type="checkbox" name="contacttype" value="titleagent"> <b><u>Title Agent</u></b></label><br>
 		</td>
		</tr>
		</table>
		<b><font size="2">Results Must Match <select name="matchtype" class="form"><option value="OR">ANY</option><option value="AND">ALL</option></select> of the types checked above.</font></b>
		</td>
		</tr>
		</table><br>
<b>Choose how you want your file created</b><br>
		<table border="1" cellpadding="3" cellspacing="1" width="100%" bgcolor="black">
		<tr>
		<td valign="top" bgcolor="white">
		<table border="0" cellpadding="1" cellspacing="0" width="100%">
		<tr>
		<td valign="top" bgcolor="white">
		<font size="2"><b>File Type</b> </font><select name="fileextension" class="form"><option value="csv">[ Excel ] *.csv</option><option value="txt">[ Text ] *.txt</option></select><br><br>
		<font size="2"><b>Divider Type</b> </font><select name="divider" class="form"><option value="\,">[ Comma Seperated Values ] Ex: firstname,lastname</option><option value="\|">[ Pipe Delimited ] Ex: firstname|lastname</option><option value="\\t">[ Tab Delimited ] Ex: firstname	lastname</option></select><br><br>
		</td>
		</tr>
		</table>
		</td>
		</tr>
		</table><br>
<input type="submit" value="Build Your Export File Now" class="form">
</form>

</td>
~;
&footer;

}



