#!/usr/bin/perl

####################################################################################################
## NetZone 10.0 
## index.cgi
##
## Author: Brian Walker, Webdirectionz Limited.
## Date: 30 October 2007
####################################################################################################
      
      
        use CGI qw/:standard/;
        use CGI::Carp qw(fatalsToBrowser carpout);
        
        use DBI;
        use Time::Local;
        use Image::Size;


####################################################################################################
# SPLIT QUERY STRING INTO VARIABLS AND VALUES
####################################################################################################
    
        require "cgi-lib.pl";
        &ReadParse(\%in,\%cgi_cfn);

####################################################################################################
# Query string variables
####################################################################################################


        $do		= $in{do};
	$agency		= $in{agency};
	$consultant	= $in{consultant};
	$paxname	= $in{paxname};
	$address	= $in{address};
	$email		= $in{email};
	$voucher	= $in{voucher};
	$price		= $in{price};
	$pass		= $in{pass};
	
	
        print "Content-type: text/html\n\n";
        require "sysconfig.cgi";
	
	if ($do eq "order") {
		if ($agency eq "" || $paxname eq "" || $address eq "" || $email eq "" || $voucher eq "" || $price eq "" || $pass eq "" || $consultant eq "") {
			$message = "Please complete all fields.<br/><br/>";
			&display_order_form;
		}
		else {
			&submit_order;
		}
	}
	else { &display_order_form }
	
	
                
####################################################################################################
# Order form
####################################################################################################
sub display_order_form {
	
	$sqlstmt = "select pass_id, pass_name from passes where pass_status='A' and (pass_group=1 or pass_group=2) order by pass_rank";
	$sth = $dbh->prepare($sqlstmt);
	$sth->execute || &display_error("Cannot find the selected page",$sqlstmt);
	
	while ($row=$sth->fetchrow_hashref) {
		$pass_id	= $row->{pass_id};
		$pass_name	= $row->{pass_name};
		
		$passlist=<<endhtml;
		$passlist
		<input type="checkbox" name="pass" value="$pass_id"> $pass_name<br/>
endhtml
	}
	$rc=$sth->finish;
	
	
	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=utf-8" />
	<title>Agent Booking</title>
	
	<style type="text/css">
	<!--
	html, body {background:transparent;font-family:'Arial',sans-serif;font-size:12px;}-->
	</style>

	</head>
	
	<body>Please complete the following booking form:<br/><br/>
	<form action="agentbookings.cgi" method="post" style="margin:0px;">
	<span style="color:#FF0000; font-weight:bold;">$message</span>
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
	  <tr>
	    <td width="180" height="30" valign="top">Agency Name</td>
	    <td height="30" valign="top"><input name="agency" type="text" id="agency"  style="width:250px;" value="$agency" maxlength="150" /></td>
	  </tr>
	  <tr>
	    <td width="180" height="30" valign="top">Consultant Name</td>
	    <td height="30" valign="top"><input name="consultant" type="text" id="consultant"  style="width:250px;" value="$consultant" maxlength="150" /></td>
	  </tr>
	  <tr>
	    <td width="180" height="95" valign="top">Agency address</td>
	    <td height="95" valign="top"><textarea name="address" id="address" style="width:250px; height:80px;">$address</textarea></td>
	  </tr>
	  <tr>
	    <td width="180" height="30" valign="top">Email address</td>
	    <td height="30" valign="top"><input name="email" type="text" id="email"  style="width:250px;" value="$email" maxlength="150" /></td>
	  </tr>
	  <tr>
	    <td width="180" height="30" valign="top">Voucher number</td>
	    <td height="30" valign="top"><input name="voucher" type="text" id="voucher"  style="width:100px;" value="$voucher" maxlength="30" /></td>
	  </tr>
	  <tr>
	    <td width="180" height="30" valign="top">Passenger name</td>
	    <td height="30" valign="top"><input name="paxname" type="text" id="paxname"  style="width:100px;" value="$paxname" maxlength="200" /></td>
	  </tr>
	  <tr>
	    <td width="180" height="30" valign="top">Price Paid</td>
	    <td height="30" valign="top"><input name="price" type="text" id="price"  style="width:100px;" value="$price" maxlength="30" /></td>
	  </tr>
	  <tr>
	    <td width="180" height="30" valign="top">Booked Passes</td>
	    <td height="30" valign="top">$passlist</td>
	  </tr>
	  <tr>
	    <td width="180" height="30" valign="top"></td>
	    <td height="30" valign="top"><br/><input name="submit" type="submit" id="submit" value="submit order"/></td>
	  </tr>
	</table>
	<input type="hidden" name="do" value="order">
	</form>
	</body>
	</html>
	
	
	~;
	exit;
	
}


####################################################################################################
# submit Order
####################################################################################################
sub submit_order {
	
	foreach(split(/\0/,$pass)) {
		$sqlstmt = "select pass_id, pass_name from passes where pass_id='$_'";
		$sth = $dbh->prepare($sqlstmt);
		$sth->execute || &display_error("Cannot find the selected page",$sqlstmt);
		
		while ($row=$sth->fetchrow_hashref) {
			$pass_name	= $row->{pass_name};
			
			$passlist=<<endhtml;
			$passlist
			$pass_name<br/>
endhtml
		}
		$rc=$sth->finish;
	}
	
	
	
	open( MAIL, "|$sendmail -t " ) || &display_error("can't open sendmail: $sendmail: $!\n");
	print MAIL "To: Feejee Experience Agent Bookings <enquiries\@feejeeexperience.com>\n";
	print MAIL "From: $agency <$email>\n";
	print MAIL "Subject: Feejee Experience Agent Booking\n";
	print MAIL "Mime-Version: 1.0\n";
	print MAIL "$contenttype";
	print MAIL <<ENDHTML;
	<!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>FJX Brochure Order</title>
		<style type="text/css">
		<!--
		body, td {
			font-family: Arial, Verdana, Tahoma;
			font-size: 12px;
		}
		-->
		</style>
		</head>
		
		<body>
		<p>The following agent bookings has been placed</p>
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
	  <tr>
	    <td width="180" height="30" valign="top">Agency Name</td>
	    <td height="30" valign="top">$agency</td>
	  </tr>
	  <tr>
	    <td width="180" height="30" valign="top">Consultant Name</td>
	    <td height="30" valign="top">$consultant</td>
	  </tr>
	  <tr>
	    <td width="180" height="95" valign="top">Agency address</td>
	    <td height="95" valign="top">$address</td>
	  </tr>
	  <tr>
	    <td width="180" height="30" valign="top">Email address</td>
	    <td height="30" valign="top">$email</td>
	  </tr>
	  <tr>
	    <td width="180" height="30" valign="top">Voucher number</td>
	    <td height="30" valign="top">$voucher</td>
	  </tr>
	  <tr>
	    <td width="180" height="30" valign="top">Passenger name</td>
	    <td height="30" valign="top">$paxname</td>
	  </tr>
	  <tr>
	    <td width="180" height="30" valign="top">Price Paid</td>
	    <td height="30" valign="top">$price</td>
	  </tr>
	  <tr>
	    <td width="180" height="30" valign="top">Booked Passes</td>
	    <td height="30" valign="top">$passlist</td>
	  </tr>

	</table>
	<p>&nbsp;</p>
	</body>
	</html>
		
	
ENDHTML
	close (MAIL);
	
	open( MAIL, "|$sendmail -t " ) || &display_error("can't open sendmail: $sendmail: $!\n");
	print MAIL "To: $agency <$email>\n";
	print MAIL "From: Feejee Experience <enquiries\@feejeeexperience.com>\n";
	print MAIL "Subject: Your Feejee Experience Booking\n";
	print MAIL "Mime-Version: 1.0\n";
	print MAIL "$contenttype";
	print MAIL <<ENDHTML;
	<!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>Feejee Experience Booking Details</title>
		<style type="text/css">
		<!--
		body, td {
			font-family: Arial, Verdana, Tahoma;
			font-size: 12px;
		}
		-->
		</style>
		</head>
		
		<body>
		<p>Thank you for booking with <strong>Feejee Experience</strong></p>
		<p>Your booking has been submitted and is on <strong>Free Sell</strong>. A Feejee Experience representative will confirm back in 24 hours with a PNR.</p>

		<table width="100%" border="0" cellspacing="0" cellpadding="0">
		  <tr>
		    <td width="180" height="30" valign="top">Agency Name</td>
		    <td height="30" valign="top">$agency</td>
		  </tr>
		  <tr>
		    <td width="180" height="30" valign="top">Consultant Name</td>
		    <td height="30" valign="top">$consultant</td>
		  </tr>
		  <tr>
		    <td width="180" height="95" valign="top">Agency address</td>
		    <td height="95" valign="top">$address</td>
		  </tr>
		  <tr>
		    <td width="180" height="30" valign="top">Email address</td>
		    <td height="30" valign="top">$email</td>
		  </tr>
		  <tr>
		    <td width="180" height="30" valign="top">Voucher number</td>
		    <td height="30" valign="top">$voucher</td>
		  </tr>
		  <tr>
		<td width="180" height="30" valign="top">Passenger name</td>
		<td height="30" valign="top">$paxname</td>
	      </tr>
		  <tr>
		    <td width="180" height="30" valign="top">Price Paid</td>
		    <td height="30" valign="top">$price</td>
		  </tr>
		  <tr>
		    <td width="180" height="30" valign="top">Booked Passes</td>
		    <td height="30" valign="top">$passlist</td>
		  </tr>
	
		</table>
	
		<p><img src="$siteurl/images/feejee-experience-logo.jpg"></p>
		<p><strong>PH:</strong> +679 672 3311<br />
		<strong>ADDRESS:</strong> PO Box 9612, Aerotown Mall, Nadi Airport, Fiji<strong> </strong><br />
		<strong>WEB: </strong><a href="http://www.touristtransportfiji.com/">www.touristtransportfiji.com</a>&nbsp;- <a href="http://www.coralsunfiji.com">www.coralsunfiji.com</a> - <a href="http://www.feejeeexperience.com/" title="http://www.feejeeexperience.com/">www.feejeeexperience.com</a><br />
		</p>
	<p>&nbsp;</p>
	</body>
	</html>
		
	
ENDHTML
	close (MAIL);
	
	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=utf-8" />
	<title>Brochure Order</title>
	
	<style type="text/css">
	<!--
	html, body {background:transparent;font-family:'Arial',sans-serif;font-size:12px;}-->
	</style>

	</head>
	
	<body>Thank you. Your booking has been submitted and is on <strong>Free Sell</strong>. A Feejee Experience representative will confirm back in 24 hours with a PNR.
	<br/><br/>A copy of your booking has been emailed to <strong>$email</strong>

	</body>
	</html>
	
	
	~;
	exit;
}