<?php
	require_once('common.php');
	checkUser();
	include("header.php");
	include("constants.php");
?>
		<!-- content -->
		<div id="content">
			<!-- table -->
			<div class="box">
				<!-- box / title -->
				<div class="title">
					<h5>Jimmy Clean's - Add Email Addres</h5>
		
				</div>
				<!-- end box / title -->
				<div class="box">
					<?php
				if (isset($_POST['submit'])) {
				$first_name = $_POST['firstname'];
				$last_name = $_POST['lastname'];
				$email = $_POST['email'];
				$output_form = 'no';

					if (empty($first_name) || empty($last_name) || empty($email)) {
					echo '
					<div class="messages">
						<div id="message-error" class="message message-error">
							<div class="image">
								<img src="images/icons/error.png" alt="Error" height="32" />
							</div>
							<div class="text">
								<h6>Error Message</h6>
								<span>Please Fille Out All Required Information</span>
							</div>
							<div class="dismiss">
								<a href="#message-error"></a>
							</div>
						</div>
					</div>';
					$output_form = 'yes';
					}
				}
  
				else {
					$output_form = 'yes';
				}

				if (!empty($first_name) && !empty($last_name) && !empty($email)) {
				$dbc = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME)
				or die('Error connecting to MySQL server.');

				$query = "INSERT INTO " . DB_TABLE . " (first_name, last_name, email)  VALUES ('$first_name', '$last_name', '$email')";
				mysqli_query($dbc, $query)
				or die ('Data not inserted.');

				echo '<div class="messages">
						<div id="message-success" class="message message-success">
							<div class="image">
								<img src="images/icons/success.png" alt="Success" height="32" />
							</div>
							<div class="text">
								<h6>Success</h6>
								<span>Email address has been added<br/><br/></span>
								<span>You are being re-directed</span>
								<meta http-equiv="refresh" content="2;URL=add_email.php"> 							
							</div>
							<div class="dismiss">
								<a href="#message-success"></a>
							</div>
			</div>
			</div>';
				

				mysqli_close($dbc);
				}

				if ($output_form == 'yes') {
				?>
					<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
						<div class="form">
						<div class="fields">
						<div class="field">
							<div class="label">
								<label for="firstname">First name:</label>
							</div>
							<div class="input">
								<input class="medium" type="text" id="firstname" name="firstname" /><br />
							</div>
						</div>
						<div class="field">
							<div class="label">
								<label for="lastname">Last name:</label>
							</div>
							<div class="input">
								<input class="medium" type="text" id="lastname" name="lastname" /><br />
							</div>
						</div>
						<div class="field">
							<div class="label">
								<label for="email">Email:</label>
							</div>
							<div class="input">
								<input class="medium" type="text" id="email" name="email" /><br />
							</div>
						</div>
								
						<div class="buttons">
			
							<div class="highlight">
<input type="submit" id="submit-button" class="add_email" name="submit" value="Submit">
							</div>
						</div>						
										
												
												</form>
												  
<?php
}
?>
				</div>
			</div>
			</div>
			</div>
			<!-- end table -->
			
		
			
			
			<!-- end box / right -->
		<!-- end content -->
		<?php
include("footer.php");
?>