#!/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=\"eagle_transactions\_$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;





####### GET TRANSACTIONS CLIENT IS PART OF #####################
my $aasth = $dbh->prepare("select id,agent,type,status,addressnumber,addressdirection,addressstreet,city,state,zip from trans_board WHERE type = 'Listing' AND status = 'Active' ORDER BY type");
$aasth->execute;
while (($t_transid,$t_transagent,$t_type,$t_status,$t_addressnumber,$t_addressdirection,$t_addressstreet,$t_tcity,$t_tstate,$t_tzip) = $aasth->fetchrow_array()) {
$t_type =~ s/\,//gi;
$t_type =~ s/\r\n/\;/g;
$t_status =~ s/\,//gi;
$t_status =~ s/\r\n/\;/g;
$t_addressnumber =~ s/\,//gi;
$t_addressnumber =~ s/\r\n/\;/g;
$t_addressdirection =~ s/\,//gi;
$t_addressdirection =~ s/\r\n/\;/g;
$t_addressstreet =~ s/\,//gi;
$t_addressstreet =~ s/\r\n/\;/g;
$t_addresscity =~ s/\,//gi;
$t_addresscity =~ s/\r\n/\;/g;

### FIND SELLERS

my $cbbaasth = $dbh->prepare("select contactid,type from trans_boardtranstocontact WHERE transid = '$transid' and type = 'Seller' LIMIT 0,1");
$cbbaasth->execute;
while (($tempcontid) = $cbbaasth->fetchrow_array()) {

####### END GET TRANSACTIONS CLIENT IS PART OF #################


$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 id = '$tempcontid'~;
my $uasth = $dbh->prepare("$sql_query");

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;

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

#########################
$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";

}


$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 Transactions - 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 Transactions</font></b><br><br>
&nbsp;&nbsp;&nbsp;&nbsp;This will allow you to export your transactions 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="export_transactions.idx">
<input type="hidden" name="action" value="create">
<input type="hidden" name="agent" value="$INPUT{agent}">
<b>Choose transaction 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">
			<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" checked> <b><u>Buyer - Prospective</u></b></label><br>
			<label for="contacttype_sellerpast"><input id="contacttype_sellerpast" type="checkbox" name="contacttype" value="sellerprospective" checked> <b><u>Seller - Prospective</u></b></label><br>
			<label for="contacttype_sellerprospective"><input id="contacttype_sellerprospective" type="checkbox" name="contacttype" value="sellerpast" > <b><u>Seller - Past</u></b></label><br>
			<label for="contacttype_sphereofinfluence"><input id="contacttype_sphereofinfluence" type="checkbox" name="contacttype" value="sphereofinfluence" > <b><u>Sphere of Influence</u></b></label><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>
		</td>
		<td valign="top" bgcolor="white">
			<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;

}



