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

require '../PHPMailer-master/PHPMailerAutoload.php';

session_start();

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

$p_first_name = '';
$p_last_name = '';
$p_phone = '';
$p_city = '';

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_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>";
	}

	$p_phone = preg_replace("/[^0-9,.]/", "", $p_phone);
	if (is_numeric($p_phone))
	{
		$first = substr($p_phone, 0, 3);
		$second = substr($p_phone, 3, 3);
		$third = substr($p_phone, 6, 4);	

		$p_phone = $first . '-' .$second . '-'.$third;
	}
	else
	{
		$boolValidate = false;
		$strValidationB .= '<p>Please enter a valid phone number</p>';
	}
	
	if ($p_city == "")
	{
		$boolValidate = false;
		$strValidationB .= "<h3>Please enter your city</h3>";
	}

	if ($boolValidate == true)
	{
		foreach (explode(',', $insertID) as $curInsertID)
		{
			$sql = "UPDATE ".$g_KioskTableName." set strFirstName = '$p_first_name', strLastName = '$p_last_name', strCity = '$p_city', strPhone = '$p_phone' where intEntryID = '$curInsertID'";
			$result = getFromDatabase($sql);
		}

		$p_first_name = strtolower($p_first_name);
		$p_first_name = ucwords($p_first_name);

		$p_last_name = strtolower($p_last_name);
		$p_last_name = ucwords($p_last_name);

		$body = file_get_contents(__DIR__ . '/includes/mail/thankyou.html');
		$body = str_replace("[FIRSTNAME]", $p_first_name, $body);
		$body = str_replace("[LASTNAME]", $p_last_name, $body);

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

		$mail->SetFrom($g_EmailFrom, $g_MallName);

		$mail->AddAddress($email, $p_first_name . ' ' . $p_last_name);
		// $mail->AddBCC("michelle@digitaltea.com","Michelle @ DT");

		$mail->Subject = $g_ContestName;

		$mail->Body = $body;

		if (!$mail->Send())
		{
		   if ($g_isLocalHost)
			  	header('Location: thankyou.php');
		   else
		   	$strValidationB .= "Mailer Error: " . $mail->ErrorInfo;
		}
		else
		{
		  	header('Location: thankyou.php');
		}
	}
}
 ?>

<?php include('includes/header.php'); ?>
<body class="internal">
<script type="text/javascript">
	mostRecentPhoneKeyCode = 0; // 8 = backspace
	$(document).ready(function()
	{
			//$("#first_name").focus();
			setTimeout(function(){$("#first_name").focus();}, 200);

		$("#phone").on('keydown', function(evt)
		{
			mostRecentPhoneKeyCode = evt.keyCode;
		});
		$("#phone").on('input', function(evt)
		{
			//console.log("keycode = " + evt.keyCode);
			var field = $(this);
			$("#phone").css('background-color', '#ffffff');
			var fval = field.val();
			var ndx = fval.length - 1;
			
			if (fval.length>12)
			{
				field.val(field.val().substr(0,12));
				return;
			}

			if (isNaN(fval[ndx]))
			{
				field.val(fval.substr(0,ndx));
				return;
			}
			
			if (ndx == 2 && mostRecentPhoneKeyCode != 8) // 8 = backspace
			{
				field.val(fval + "-");
				return;
			}
			
			if (ndx == 6 && mostRecentPhoneKeyCode != 8) // 8 = backspace
			{
				field.val(fval + "-");
				return;
			}
		});
	});			
</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" id="formTable">
				<tr>
					<td>
						<label for="first_name">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 colspan="2">
						<label id="first_name-error" class="error" for="first_name"></label>
					</td>
				</tr>
				<tr>
					<td>
						<label for="last_name">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 colspan="2">
						<label id="last_name-error" class="error" for="last_name"></label>
					</td>
				</tr>
				<tr>
					<td>
						<label for="city">City</label>
					</td>
					<td>
						<input type="text" name="city" id="city" value="<?php if ($p_city) echo $p_city; ?>" required>
					</td>
				</tr>
				<tr>
					<td colspan="2">
						<label id="city-error" class="error" for="city"></label>
					</td>
				</tr>
				<tr>
					<td>
						<label for="phone">Phone No </label>
					</td>
					<td>
						<input type="text" name="phone" id="phone" value="<?php if ($p_phone) echo $p_phone; ?>" required placeholder="555-555-5555">		
					</td>
				</tr>
				<!--
				<tr>
					<td colspan="2">
						<label id="phone-error" class="error" for="phone"></label>
					</td>
				</tr>
				-->
				<tr>
					<td colspan="2">
					<div class="span12 textRight column">
							<input type="submit" value="NEXT" name="submit" class="button">	
					</div>
					</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>
		</footer>
	</form>
</body>
</html>