<?php
error_reporting(E_ALL ^ E_NOTICE);
// Start a session to hold the first variable pages
session_start();

// Add products to the array that will be displayed on the first page
$products = Array(
Array("name"		=> "3 1/8” Thermal Paper",
	  "desc"		=> "Guest Check Paper - 50 Rolls - $89.00",
	  "form_name"	=> "product_1"),
	  
Array("name"		=> "3” One Ply Paper",
	  "desc"		=> "Kitchen Printer Paper (White) - 50 Rolls - $39.00",
	  "form_name"	=> "product_2"),
	  
Array("name"		=> "3” Two Ply Paper",
	  "desc"		=> "Kitchen Printer Paper (W/Y) - 50 Rolls - $63.00",
	  "form_name"	=> "product_3"),
	  
Array("name"		=> "3” Three Ply Paper",
	  "desc"		=> "Kitchen Printer Paper (W/Y/P) - 50 Rolls - $89.00",
	  "form_name"	=> "product_4"),
	  
Array("name"		=> "Remote Printer Ribbons",
	  "desc"		=> "Black/Red - 1 Dozen - $24.00",
	  "form_name"	=> "product_5"),
	  
Array("name"		=> "Manager Swipe Cards",
	  "desc"		=> "Swipe Cards for Controlled System Access - $2.00 Each<br /><span class='cards'>Portal orders qualify for discounted shipping which will be added to the order.<br /> Invoices will be processed per the account terms.</span>",
	  "form_name"	=> "product_6")
	  
);

// The email address this form will be sent to
//$sendEmails = Array("hunter@cervion.com", "chris@cervion.com");
$sendEmails = Array("paper@cervion.com", "hunterballen@gmail.com", "chris@cervion.com");
//$sendEmails = Array("chris@cervion.com");
$emailTemplate = <<<EOF
<html>
<head><title>Portal: Supply Order Form Submission</title></head>
<body>
<h2>Supply Order Form Submission</h2>
<div style="margin-top:30px;font-size:12pt;">
<b>Customer Name:</b> %name% <br />
<b>Customer Company:</b> %company% <br />
<b>Order Date:</b> %order_date%<br><br />
<b>Customer IP:</b> %ip% <br />
<strong>Submission Location:</strong> Dinerware Portal <br /><br />
<b>Contact before placing order:</b> %contact% 
<br />
</div>
<div style="margin-top:30px;font-size:12pt;">
<b><u>ORDER INFORMATION</u><br>
<br>
</b>
%product_order%
<br /><br /></div>
</body>
</html>
EOF;

/*
 * Function to send the email to the owner
 */
function sendMail() {
	global $products, $emailTemplate, $sendEmails;
	$name = $_POST['name'];	// Form submitters name
	$company = $_POST['company'];	// Form submitters company
	$contact = $_POST['contact'] ? "YES" : "NO";	// Contact the submitter 1=Yes / 0=No
	setcookie("company",$company,time()+(60*60*24*30));	// Set a cookie to remember the clients company name
	$date = date("m/d/Y g:i A", time());
	$ip = $_SERVER['REMOTE_ADDR'];	// Get the users IP address
	$productsList = Array();	// Array to hold the product list that was requested, and their quantity
	
	// Loop to retrieve the products select from the $_SESSION variable, and place them into the $productsList array
	foreach($_SESSION['form_data'] as $product => $qty) {
		if($qty > 0) {	// Makre sure quantity is bigger than zero
			
			$productKey = searchArray($products,$product);	// Find the product key in the $products variable, in order to fetch the data from the array
			$tempArray = Array("name" => $products[$productKey]["name"], "qty" => $qty);	// Create a temp array to push into $productsList
			
			array_push($productsList,$tempArray);	// Push the temp array
		}
	}
	
	// Create a product list to write into the email template
	$product_order = '<table style="border:0;width:300px;text-align:left;">
					  <tr align="left"><td style="text-align:"left;"><strong>Product:</strong></td><td style="width:80px;text-align:left;"><strong>QTY:</strong></td></tr>';
	foreach($productsList as $product) {
		$product_order .= '<tr><td>'.$product['name'].'</td><td>'.$product['qty'].'</td></tr>';
	}
	$product_order .= '</table>';
	

	// Replace the template values with the form values
	$emailTemplate = str_replace(array('%name%','%company%','%order_date%','%ip%','%contact%','%product_order%'), array($name,$company,$date,$ip,$contact,$product_order), $emailTemplate);
	
	// Implode the email address with a comma
	if(count($sendEmails) > 1) {
		$to = implode(",",$sendEmails);
	}
	else {
		$to = $sendEmails[0];
	}
	
	$subject = 'Portal: Supply Order Form Submission';
	
	$headers = "From: paper@cervion.com\r\n";
	$headers .= "Reply-To: paper@cervion.com\r\n";
	$headers .= "MIME-Version: 1.0\r\n";
	$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
	
	mail($to,$subject,$emailTemplate,$headers);
	
}

/*
 * Custom function to search our $products array for the form_name of the product, and returns its location in the array
 */
function searchArray($array,$searchFor) {
	$keyNum = 0;	// Initalize the array key at 0
	foreach($array as $key => $value) {
		if($value['form_name'] == $searchFor) {	// Check if the form_name matches our search criteria
			return $keyNum;	// If it does, return the location AKA key
		}
		$keyNum++;	// Else increase the key value
	}
}
?>