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

if (!$_SESSION['randomString'] || $_SESSION['randomString'] == "")
{
	header('Location:contest_page1.php');
}
else
{
	// $randomString = $_SESSION['randomString'];
	$eligibleYN = $_SESSION['eligible'];
}

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_city = sanitize($_POST['city']);

	// $p_optInYN = sanitize($_POST["optInYN"]);

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


	// check to see if have already registered once today

	$sql = "SELECT intEntryID from tblKiosksParkPlace2015 where strEmailAddress = '$p_email' and DATE(entryDate) = CURDATE()";

	// echo "$sql";
	$result = getFromDatabase($sql);
	$num_rows = mysqli_num_rows($result);
	if ($num_rows != 0)
	{
		$strValidationB = '<h3>Sorry only one entry per email per day</h3>';
		$boolValidate = false;
	}

	if ($boolValidate == true)
	{

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

		$result = getFromDatabase($sql);

		$insertID = mysqli_insert_id($dbc);

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

<?php include('../includes/pp-header.php'); ?>
<body class="pp-top mainForm">
	<script type="text/javascript" src="../assets/scripts/check.js"></script>

	<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="regForm" class="form" enctype="multipart/form-data" >
		<header>
			<a href='index.php' class='button home'>HOME</a>
		</header>
		<main class="container">
			<div class="pinkText span10 centerContent">
			<?php 
				if ($strValidationB) 
				{
					echo '<div class="strValidationB">'.$strValidationB.'</div>';
				}
				if ($strValidation) 
				{
					echo '<div class="strValidation">'.$strValidation.'</div>';
				} ?>

			</div>
			
			<table class="centerContent span10 vMiddle">
				<tr>
					<td width="200px">
						<label for="email" class="greenText">Email</label>
					</td>
					<td>
						<input type="email" name="email" id="email" value="<?php if ($p_email) echo $p_email; ?>" required>		
					</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>
						<label id="email-error" class="error" for="email"></label>
					</td>
				</tr>
				<!-- <tr>
					<td colspan="2">&nbsp;</td>
				</tr> -->
				<tr>
					<td>
						<label for="first_name" class="mediumBlueText">First Name </label>
					</td>
					<td>
						<input type="text" name="first_name" id="first_name" value="<?php if ($p_first_name) echo $p_first_name; ?>" required>		
					</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>
						<label id="first_name-error" class="error" for="first_name"></label>
					</td>
				</tr>
				<tr>
					<td>
						<label for="last_name" class="yellowText">Last Name </label>
					</td>
					<td>
						<input type="text" name="last_name" id="last_name" value="<?php if ($p_last_name) echo $p_last_name; ?>" required>
					</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>
						<label id="last_name-error" class="error" for="last_name"></label>
					</td>
				</tr>
				<!-- <tr>
					<td colspan="2">&nbsp;</td>
				</tr> -->
				<tr>
					<td>
						<label for="city" class="lightBlueText">City</label>
					</td>
					<td>
						<input type="tel" name="city" id="city" value="<?php if ($p_city) echo $p_city; ?>" required>
					</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>
						<label id="city-error" class="error" for="city"></label>
					</td>
				</tr>
				<tr>
					<td>
						<label for="phone" class="pinkText">Phone No </label>
					</td>
					<td>
						<input type="tel" name="phone" id="phone" value="<?php if ($p_phone) echo $p_phone; ?>" required>		
					</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>
						<label id="phone-error" class="error" for="phone"></label>
					</td>
				</tr>
			</table>
			
		</main>		
		<footer class="clearfix span11 centerContent">
			<div class="span5 column">
				<a href="#rules" class="fancybox" >CONTEST RULES AND REGULATIONS</a>
				<div id="rules">
					<?php include('rules.php') ?>
				</div>
			</div>
			<div class="span6 textRight column">
				<input type="submit" value="NEXT" name="submit" class="button">	
			</div>
		</footer>
	</form>
</body>
</html>