<?php 
include('../includes/settings.php');
include('../includes/mysql_connect.php');
include('../includes/funcs.php');
require '../PHPMailer-master/PHPMailerAutoload.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_address = sanitize($_POST['address']);
	$p_city = sanitize($_POST['city']);
	$p_province = sanitize($_POST['province']);
	$p_postal = sanitize($_POST['postal']);
	$p_gender = sanitize($_POST['gender']);

	$p_optInYN = sanitize($_POST["optInYN"]);
	$p_ofAge = sanitize($_POST['ofAge']);


	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_address == "")
	{
		$boolValidate = false;
		$strValidationB .= "<p>Please enter your address</p>";
	}
	if ($p_city == "")
	{
		$boolValidate = false;
		$strValidationB .= "<p>Please enter your city</p>";
	}
	if ($p_province == "")
	{
		$boolValidate = false;
		$strValidationB .= "<p>Please select your province</p>";
	}
	if ($p_postal == "")
	{
		$boolValidate = false;
		$strValidationB .= "<p>Please enter your postal code</p>";
	}

	
	if ($p_optInYN == "")
	{
		$boolValidate = false;
		$strValidationB .= "<p>You must accept the terms of use to proceed.</p>";	
	}
	if ($p_ofAge == "")
	{
		$boolValidate = false;
		$strValidationB .= "<p>Please must be over 18 and have read and accepted the rules and regulations</p>";
	}

	// see if already registered today
	$sql = "SELECT intEntryID from tblKiosksKingsway where strEmailAddress = '$p_email' and DATE(entryDate) = CURDATE()";

	// echo "$sql";
	$result = getFromDatabase($sql);
	$num_rows = mysqli_num_rows($result);
	mysqli_free_result($result);

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

	if ($boolValidate == true)
	{

		$sql = "INSERT INTO tblKiosksKingsway (strFirstName, strLastName, strEmailAddress, strPhone, ynOptIn, entryDate, strGender, strCity, strProvince, strPostal, ynOfAge) VALUES ('$p_first_name','$p_last_name','$p_email','$p_phone','$p_optInYN',NOW(), '$p_gender','$p_city','$p_province','$p_postal','$p_ofAge')";

		$result = getFromDatabase($sql);

		$insertID = mysqli_insert_id($dbc);

		if ($insertID)
		{
			$formHide = true;
			


			// send email
			$body = "NEED CONTENT, NEED CONTENT, NEED CONTENT";	

			$mail = new PHPMailer();
			$mail->IsHTML(true);

			$mail->SetFrom("noreply@kingswaymall.com","Kingsway Mall");

			$mail->AddAddress($p_email);
			$mail->AddBCC("michelle@digitaltea.com","Michelle @ DT");

			$mail->Subject = "Kingsway Mall Back to School Contest";

			$mail->Body = $body;

			if (!$mail->Send())
			{
			   $strValidationB .= "Mailer Error: " . $mail->ErrorInfo;
			}
			else
			{
			  	// $strValidation = "<h2>Thank you for entering</h2>";
			  	// $strValidation .= '<p>An email will be sent to you to confirm your entry and details. Please listen to Bounce 91.7FM daily for other Back-to-School giveaways from Kingsway Mall!</p>';

			  	header('Location: thankyou.php');

			}
		}
		else
		{
			$formHide = false;
			$strValidationB = '<p>Sorry but your contest entry did not save to the database. Please try again.</p>';
		}
	}
}
 ?>

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

<div class="span10 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 span10" enctype="multipart/form-data" >
		
		<table>
			<tr>
				<td>
					<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>
				</td>
				<td>
					<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>			
				</td>
			</tr>
			<tr>
				<td>
					<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>
				</td>
				<td>
					<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>
				</td>
			</tr>
			<tr>
				<td>
					<label for="address">Address <span class="required">*</span></label>
					<input type="text" name="address" id="address" value="<?php if ($p_address) echo $p_address; ?>" required>
				</td>
				<td>
					<label for="city">City <span class="required">*</span></label>
					<input type="text" name="city" id="city" value="<?php if ($p_city) echo $p_city; ?>" required>	
				</td>
			</tr>
			<tr>
				<td>
					<label for="province">Province <span class="required">*</span></label>
					<!-- <input type="text" name="province" id="province" value="<?php if ($p_province) echo $p_province; ?>" required>	 -->
					<select name="province" id="province" required>
						<option value="">Please select a province</option>
					<?php 
						$prov_sql = "SELECT * from tblProvince order by intProvince";
						$prov_result = getFromDatabase($prov_sql);
						while ($prov_row = mysqli_fetch_array($prov_result) )
						{
							$intProvince = $prov_row['intProvince'];
							$strProvince = $prov_row['strProvince'];

							echo '<option value="'.$intProvince.'" ' ;

								if ($intProvince == $p_province)
									echo ' selected ';

							echo '>'.$strProvince.'</option>';
						}
						mysqli_free_result($prov_result);
					?>	
					</select>
				</td>
				<td>
					<label for="postal">Postal Code <span class="required">*</span></label>
					<input type="text" name="postal" id="postal" value="<?php if ($p_postal) echo $p_postal; ?>" required>	
				</td>
			</tr>
			<tr>
				<td>
					<label for="gender">Gender</label>
					<select id="gender" name="gender" required>
						<option value="">Please select your gender</option>
						<option value="male" <?php if ($p_gender == 'male') echo " selected "; ?>>Male</option>
						<option value="female" <?php if ($p_gender == 'female') echo " selected "; ?>>Female</option>
						<option value="na" <?php if ($p_gender == 'na') echo " selected "; ?>>Prefer not to answer</option>
					</select>
				</td>
				<td>
					&nbsp;
				</td>
			</tr>

		</table>
		
		<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">Please email me about other events, contest and special offer</label>
				</td>
			</tr>

			<tr>
				<td>
					<input type="checkbox" name="ofAge" id="ofAge" value="Yes" <?php if ($p_ofAge == 'Yes') echo ' checked="checked" '; ?> required >
				</td>
				<td>

					<label for="p_ofAge">I am over 18 and have read and accepted the <a href="#rules" class="fancybox" >rules and regulations!</a></label>	
					<div id="rules">
						<?php include('rules.php') ?>
					</div>			
				</td>
			</tr>


		</table>		
		<div class="buttonPad">
			<input type="submit" value="Submit" name="submit" class="button">	
		</div>
	</form>
<?php } ?>

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