<?php 
include('../includes/settings.php');
include('../includes/mysql_connect.php');
include('../includes/funcs.php');

$formHide = false;

if (isset($_POST['submit']))
{
	$formHide = false;
	$boolValidate = true;
	$strValidation = $strValidationB = '';

	$p_first_name = sanitize($_POST["first_name"]);
	$p_last_name = sanitize($_POST["last_name"]);
	$p_phone = sanitize($_POST["phone"]);
	
	$p_email = sanitize($_POST["email"]);
	
	$p_optInYN = sanitize($_POST["optInYN"]);

	if ($p_first_name == "")
	{
		$boolValidate = false;
		$strValidationB .= "<p>Please enter your first name</p>";
	}
	if ($p_last_name == "")
	{
		$boolValidate = false;
		$strValidationB .= "<p>Please enter your last name</p>";
	}
	
	if ($p_phone == "")
	{
		$boolValidate = false;
		$strValidationB .= "<p>Please enter your phone number</p>";
	}
	if ($p_email == "")
	{
		$boolValidate = false;
		$strValidationB .= "<p>Please enter your email address</p>";
	}
	
	if ($p_optInYN == "")
	{
		$boolValidate = false;
		$strValidationB .= "<p>You must accept the terms of use to proceed.</p>";	
	}

	if ($boolValidate == true)
	{

		$sql = "INSERT INTO tblKiosksMedicineHat2015 (strFirstName, strLastName, strEmailAddress, strPhoneNumber, optInYN, entryDate) VALUES ('$p_first_name','$p_last_name','$p_email','$p_phone','$p_optInYN',NOW())";

		$result = getFromDatabase($sql);

		$insertID = mysqli_insert_id($dbc);

		if ($insertID)
		{
			$formHide = true;
			$strValidation = "<h2>Thank you for entering</h2>";
			$strValidation .= '<p>Needs content</p>';
			$strValidation .= '<p>Needs content</p>';	
		}
		else
		{
			$strValidationB = '<p>Sorry but your contest entry did not save to the database. Please try again.</p>';
		}
	}
}
 ?>

<?php include('../includes/mh-header.php'); ?>

<div class="span6 centerContent">
<?php 
	if ($strValidationB) 
	{
		echo '<div class="strValidationB">'.$strValidationB.'</div>';
	}
	if ($strValidation) 
	{
		echo '<div class="strValidation">'.$strValidation.'</div>';
	} ?>

</div>
<?php 
if ($formHide == false)
{ ?>

	<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="regForm" class="form centerContent span6" enctype="multipart/form-data" >
		
		<label for="first_name">First Name <span class="required">*</span></label>
		<input type="text" name="first_name" id="first_name" value="<?php if ($p_first_name) echo $p_first_name; ?>" required>
		<label for="last_name">Last Name <span class="required">*</span></label>
		<input type="text" name="last_name" id="last_name" value="<?php if ($p_last_name) echo $p_last_name; ?>" required>
		
		<label for="phone">Phone Number <span class="required">*</span></label>
		<input type="tel" name="phone" id="phone" value="<?php if ($p_phone) echo $p_phone; ?>" required>
		
		<label for="email">Email Address <span class="required">*</span></label>
		<input type="email" name="email" id="email" value="<?php if ($p_email) echo $p_email; ?>" required>
		<table class="vTop">
			<tr>
				<td>
					<input type="checkbox" name="optInYN" id="optInYN" value="Yes" <?php if ($p_optInYN == 'Yes') echo ' checked="checked" '; ?> required >
				</td>
				<td>

					<label for="optInYN">Yes I would like to *********</label>
				</td>
			</tr>
		</table>		
		<div class="buttonPad">
			<input type="submit" value="Submit" name="submit" class="button">	
		</div>
	</form>
<?php } ?>

<?php include('../includes/mh-footer.php'); ?>