<?php
// define variables and set to empty values
$userName = $Password = $RepeatPassword = $firstName = $lastName = $gender = "";
$MaritalStatus = $DateofBirth = $AddressLine1 = $AddressLine2 = $City = "";
$ZipCode = $state = $PhoneNumber = $email = "";


// define variables and set to empty values for warning message.
$userNameWm = $PasswordWm = $firstNameWm = $lastNameWm = $genderWm = "";
$MaritalStatusWm = $DateofBirthWm = $AddressLine1Wm = $AddressLine2Wm = $CityWm = "";
$ZipCodeWm = $stateWm = $PhoneNumberWm = $emailWm = "";

if($_SERVER["REQUEST_METHOD"] == "POST")
{
	$userName = test_input($_POST["userName"]);
	$Password = test_input($_POST["Password"]);
	$RepeatPassword = test_input($_POST["RepeatPassword"]);
	$firstName = test_input($_POST["firstName"]);
	$lastName = test_input($_POST["lastName"]);
	$DateofBirth = test_input($_POST["DateofBirth"]);
	$AddressLine1 = test_input($_POST["AddressLine1"]);
	$AddressLine2 = test_input($_POST["AddressLine2"]);
	$City = test_input($_POST["City"]);
	$ZipCode = test_input($_POST["ZipCode"]);
	$state = test_input($_POST["state"]);
	$PhoneNumber = test_input($_POST["PhoneNumber"]);
	$email = test_input($_POST["email"]);

	$ccount = 0;

	if(isset($_POST["gender"]))
	{
		$gender = test_input($_POST["gender"]);
		$ccount++;
	}
	else
		$genderWm = "* Select your gender.";
	
	if(isset($_POST["MaritalStatus"]))
	{
		$MaritalStatus = test_input($_POST["MaritalStatus"]);
		$ccount++;
	}
	else
		$MaritalStatusWm = "* Select your Marital Status.";
	
	if($userName=="")
	{
		$userNameWm = "* Please Enter Your User Name.";
	}
	else if(strlen($userName)<6 || strlen($userName)>50)
	{
		$userNameWm = "* Username Max length 50 characters, minimum length 6 characters.";
	}
	else
	{
		$userNameWm = "";
		$ccount++;
	}
	//echo "Password : " . strlen($Password);
	$pf = false;
	if($Password=="")
	{
		$PasswordWm = "* Please Enter Your Password.";
	}
	else if(!preg_match("#.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\W).*$#", $Password))
	{
		$PasswordWm = "Password must be at least 8 characters in length and must contain at least one number, one upper case letter, one lower case letter and one special character.";
	}
	else
	{
		if($RepeatPassword=="")
		{
			$PasswordWm = "* Please Enter Your Repeat Password.";
		}
		else
		{
			$PasswordWm = "";
			$pf = true;
		}
	}
	
	if($pf==true)
	{
		if($Password!=$RepeatPassword)
		{
			$PasswordWm = "* Those passwords didn't match. Try again.";
		}
		else
		{
			$PasswordWm = "";
			$ccount++;
		}
	}
	
	
	if($firstName=="")
	{
		$firstNameWm = "* Please Enter Your First Name." . "<br>";
	}
	else if(strlen($firstName)>50)
	{
		$firstNameWm = "First Name Max length 50 characters.";
	}
	else
	{
		$firstNameWm = "";
		$ccount++;
	}
	
	if($lastName=="")
	{
		$lastNameWm = "* Please Enter Your Last Name." . "<br>";
	}
	else if(strlen($lastName)>50)
	{
		$firstNameWm = "Last Name Max length 50 characters.";
	}
	else
	{
		$lastNameWm = "";
		$ccount++;
	}
	
	if($DateofBirth=="")
	{
		$DateofBirthWm = "* Please Enter Your Date of Birth." . "<br>";
	}
	else
	{
		$DateofBirthWm = "";
		$ccount++;
	}
	
	if($AddressLine1=="")
	{
		$AddressLine1Wm = "* Please Enter Your Address Line 1." . "<br>";
	}
	else
	{
		$AddressLine1Wm = "";
		$ccount++;
	}
		
	if($City=="")
	{
		$CityWm = "* Please Enter Your City." . "<br>";
	}
	else
	{
		$CityWm = "";
		$ccount++;
	}
	if($ZipCode=="")
	{
		$ZipCodeWm = "* Please Enter Your Zip Code." . "<br>";
	}
	else
	{
		$ZipCodeWm = "";
		$ccount++;
	}
	
	if($state=="")
	{
		$stateWm = "* Please Enter Your State." . "<br>";
	}
	else
	{
		$stateWm = "";
		$ccount++;
	}
	
	if($PhoneNumber=="")
	{
		$PhoneNumberWm = "* Please Enter Your Phone Number." . "<br>";
	}
	else
	{
		$PhoneNumberWm = "";
		$ccount++;
	}
	
	if($email=="")
	{
		$emailWm = "* Please Enter Your Email." . "<br>";
	}
	else
	{
		$emailWm = "";
		$ccount++;
	}
	
	
	
	
	//echo $ccount;
	if($ccount==13)
	{
		
		$servername = "localhost";
		$username = "root";
		$password = "";
		$dbname = "project";

		// Create connection
		$conn = mysqli_connect($servername, $username, $password, $dbname);
		// Check connection
		if (!$conn) {
		  die("Connection failed: " . mysqli_connect_error());
		}

		$sql = "INSERT INTO registration (userName, password, firstName, lastName, address1, address2, city, state, zipCode, phone, email, gender, maritalStatus, dateOfBirth)
								VALUES ('$userName', '$Password', '$firstName','$lastName','$AddressLine1','$AddressLine2','$City','$state','$ZipCode','$PhoneNumber','$email','$gender','$MaritalStatus','$DateofBirth')";

		if (mysqli_query($conn, $sql))
		{
			header('Location: Confirmation.php');
			exit;
			//echo "New record created successfully";
		}
		else
		{
			echo "Error: " . $sql . "<br>" . mysqli_error($conn);
		}

		mysqli_close($conn);

		
	}
	/*
		echo $userName . "<br />";
		echo $Password . "<br />";
		echo $RepeatPassword . "<br />";
		echo $firstName . "<br />";
		echo $lastName . "<br />";
		echo $gender . "<br />";
		echo $MaritalStatus . "<br />"; 
		echo $DateofBirth . "<br />";
		echo $AddressLine1 . "<br />";
		echo $AddressLine2 . "<br />";
		echo $City . "<br />";
		echo $ZipCode . "<br />";
		echo $state . "<br />";
		echo $PhoneNumber . "<br />";
		echo $email . "<br />";	
	*/
}

function test_input($data)
{
	$data = trim($data);
	$data = stripslashes($data);
	$data = htmlspecialchars($data);
	return $data;
}
?>

<!DOCTYPE html>
<html>
<head>
	<title>Registration - Green Food</title>
	<link rel="shortcut icon" href="images/logo.png" type="image/x-icon">
	<meta name="viewport" content="width=device-width, initial-scale=1" />
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
	<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<section>

	<div id="header">
		<img src="images/logo.png" height="80px" />
	</div>
	<div class="topnav" id="myTopnav">
	  <a href="index.php">Home</a>
	  <a href="registration.php" class="active">Registration</a>
	  <a href="animation.php">Animation</a>
	  <a href="javascript:void(0);" class="icon" onclick="myFunction()">
		<i class="fa fa-bars"></i>
	  </a>
	</div>


	<div class="container">
		<div class="form-container">
			<h2>Register</h2>
			<p>Please fill in this form to create an account.</p>
			<hr />
			<form name="signup" id="signupform" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
			<div class="row">
				<div class="col">
					<label for="uname"><b>User Name</b></label>
				</div>
				<div class="col">
				  <input type="text" id="uname" name="userName" value="<?php echo $userName; ?>" placeholder="User name.." required>
				</div>
				<div class="alert" id="unameAlert"><?php echo $userNameWm; ?></div>
			</div>
			<div class="row">
				<div class="col">
				  <label for="Password"><b>Password</b></label>
				</div>
				<div class="col">
				  <input type="password" id="password" value="<?php echo $Password; ?>" name="Password" placeholder="Password" class="input-50 input-100" required>
				  <input type="password" id="repeatPassword" value="<?php echo $RepeatPassword; ?>" name="RepeatPassword" placeholder="Repeat Password" class="input-50 input-100" required>
				</div>
				<div class="alert" id="matchPasswordWM"><?php echo $PasswordWm; ?></div>
			</div>
			<div class="row">
				<div class="col">
				  <label for="fname"><b>Name</b></label>
				</div>
				<div class="col">
				  <input type="text" id="firstName" name="firstName" value="<?php echo $firstName; ?>" placeholder="First Name" class="input-50 input-100" required>
				  <input type="text" id="lastName" name="lastName" value="<?php echo $lastName; ?>" placeholder="Last Name" class="input-50 input-100" required>
				</div>
				<div class="alert" id="firstNameAlert"><?php echo $firstNameWm; ?></div>
				<div class="alert" id="lastNameAlert"><?php echo $lastNameWm; ?></div>
			</div>
			<div class="row">
				<div class="col">
				  <label for="Gender"><b>Gender : </b></label>
					<input type="radio" id="female" name="gender" value="female" <?php if(isset($_POST["gender"]) && $_POST["gender"]=="female") echo "checked"; else echo ""; ?> required>
					<label for="female">Female</label>
					<input type="radio" id="male" name="gender" value="male" <?php if(isset($_POST["gender"]) && $_POST["gender"]=="male") echo "checked"; else echo ""; ?> required>
					<label for="male">Male</label>
				</div>
				<div class="alert" id="genderWm"><?php echo $genderWm; ?></div>
			</div>
			<div class="row">
				<div class="col">
				  <label for="MaritalStatus"><b>Marital Status : </b></label>
					<input type="radio" id="single" name="MaritalStatus" value="single" <?php if(isset($_POST["MaritalStatus"]) && $_POST["MaritalStatus"]=="single") echo "checked"; else echo ""; ?> required>
					<label for="single">Married</label>
					<input type="radio" id="married" name="MaritalStatus" value="married" <?php if(isset($_POST["MaritalStatus"]) && $_POST["MaritalStatus"]=="married") echo "checked"; else echo ""; ?> required>
					<label for="married">Unmarried</label>
				</div>
				<div class="alert" id="genderWm"><?php echo $MaritalStatusWm; ?></div>
			</div>
			<div class="row">
				<div class="col">
				  <label for="DateofBirth"><b>Date of Birth</b></label>
				</div>
				<div class="col">
				  <input type="date" id="DateofBirth" value="<?php echo $DateofBirth; ?>" name="DateofBirth" required>
				</div>
				<div class="alert" id="DateofBirthWm"><?php echo $DateofBirthWm; ?></div>
			</div>
			<div class="row">
				<div class="col">
				  <label for="Address"><b>Address</b></label>
				</div>
				<div class="col">
				  <input type="text" id="AddressLine1" name="AddressLine1" value="<?php echo $AddressLine1; ?>" placeholder="Address line 1" required>
				  <input type="text" id="AddressLine2" name="AddressLine2" value="<?php echo $AddressLine2; ?>" placeholder="Address line 2">
				  <input type="text" id="City" name="City" placeholder="City" value="<?php echo $City; ?>" class="input-50 input-100" required>
				  <input type="text" id="ZipCode" name="ZipCode" placeholder="Zip Code" value="<?php echo $ZipCode; ?>" class="input-50 input-100" required>
				  <select id="country" name="state" required>
					<?php
						if($state!="")
							echo "<option value=" . $state . ">" . $state . "</option>";
						else
							echo "";
					?>
					<option value="">Select Your State</option>
					<option value="Maryland">Maryland</option>
					<option value="Mississippi">Mississippi</option>
					<option value="Missouri">Missouri</option>
				  </select>
				</div>
				
				<div class="alert" id="AddressLine1Wm"><?php echo $AddressLine1Wm; ?></div>
				<div class="alert" id="CityWm"><?php echo $CityWm; ?></div>
				<div class="alert" id="ZipCodeWm"><?php echo $ZipCodeWm; ?></div>
				<div class="alert" id="stateWm"><?php echo $stateWm; ?></div>
			</div>
			
			<div class="row">
				<div class="col">
				  <label for="PhoneNumber">Phone Number</label>
				</div>
				<div class="col">
				  <input type="text" id="PhoneNumber" name="PhoneNumber" value="<?php echo $PhoneNumber; ?>" placeholder="Phone Number" required>
				</div>
				<div class="alert" id="PhoneNumberWm"><?php echo $PhoneNumberWm; ?></div>
			</div>
			
			<div class="row">
				<div class="col">
					<label for="email">Email</label>
				</div>
				<div class="col">
					<input type="email" id="email" name="email" value="<?php echo $email; ?>" placeholder="Email" required>
				</div>
				<div class="alert" id="emailWm"><?php echo $emailWm; ?></div>
			</div>
			 
			<br>
			<div class="row">
				<input type="submit" value="Submit" name="submit"  onclick="validateForm()">
				<input type="reset" value="Reset"> 
			</div>
			</form>
		</div>
	</div>
	<div id="footer">
		<a href="https://www.facebook.com/" target="_blank" class="fa fa-facebook media"></a>
		<a href="https://www.google.com/" target="_blank" class="fa fa-google media"></a>
		<a href="https://www.youtube.com/" target="_blank" class="fa fa-youtube media"></a>
	</div>
</section>
<script>
function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
</script>
<script src="js/script.js"></script>


</body>
</html>
