<?Php
/*
* @author Istvan Franko
* @copyright 2014
*/

session_start();

if (!isset($_SESSION['cart'])) 
{
  $_SESSION['cart']=array(); 
}

if (!isset($_SESSION['loggedin'])) 
{
  $_SESSION['loggedin'] = '0'; 
}
?>

<html>

<?php include 'head.php'; ?>

<div id="aside">
	<!--Links for navigation-->
	<a href="product-list.php"><p style="text-align:center">Back to list</a></p>	

	<fieldset style="width:85%"><legend>HELP</legend>
	<p>If your register to our page you can review your order history.</p> 
	<p>We also send emails about our special offers to our registered customers.</p>
</div>

<div id="content">

<?php

 // define variables and set to empty values
$nameErr = $emailErr = $userErr = $passErr = $cpassErr = "";
$name = $email = $user = $pass = $cpass = $phone ="";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
	$validations = 0; //reference variable for validations

	$phone = $_POST['phone'];
  
	if (empty($_POST["name"]))
    {
		$nameErr = "(Name is required)";
		$validations++;
	}
	else
    {
	$name = test_input($_POST["name"]);
		// check if name only contains letters and whitespace
		if (!preg_match("/^[a-zA-Z ]*$/",$name))
		{
			$nameErr = "Only letters and white space allowed";
			$validations++;
		}
	}
	
	if (empty($_POST["email"]))
    {
		$emailErr = "(Email is required)";
		$validations++;
	}
	else
    {
	$email = test_input($_POST["email"]);
		// check if e-mail address syntax is valid
		if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
		{
		$emailErr = "Invalid email format";
		$validations++;
		}
	}

	if (empty($_POST["user"]))
    {
		$userErr = "(User is required)";
		$validations++;
	}
	else
    {$user = test_input($_POST["user"]);}
	
	if (empty($_POST["pass"]))
    {
		$passErr = "(Password is required)";
		$validations++;
	}
	else
    {$pass = test_input($_POST["pass"]);}	

	if (empty($_POST["cpass"]))
    {
		$cpassErr = "(Password confirm required)";
		$validations++;
	}
	else
    {$cpass = test_input($_POST["cpass"]);}		



	if ($name != "" AND $email != "" AND $user != "" AND $pass != "" AND $cpass != "")
	{
		$con = mysql_connect("localhost","*****","*****");
		if (!$con)
		{
		die('Could not connect: ' . mysql_error());
		}


		mysql_select_db("university", $con);

		$sql="SELECT * FROM users WHERE UserName='$user' ";
		$result=mysql_query($sql);

		// Mysql_num_row is counting table row
		$count=mysql_num_rows($result);

		// If result matched $user and $password, table row must be 1 row
		if($count==1)
		{
			echo "<h2>SORRY.. User name has been used...<br></h2>";
			$validations++;			
		}
		
		$sql="SELECT * FROM users WHERE Email='$email' ";
		$result=mysql_query($sql);

		// Mysql_num_row is counting table row
		$count=mysql_num_rows($result);

		// If result matched $user and $password, table row must be 1 row
		if($count==1)
		{
			echo "<h2>SORRY.. Email address has been used...<br></h2>";
			$validations++;			
		}


		if ($pass != $cpass)
		{
			echo "<h2>Your Password and Confirm didn't match</h2>";
			$validations++;	
		}
		
		if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
		{
			echo "<h2>Invalid email format</h2>";
			$validations++;	
		}
		
		
		if (!preg_match("/^[a-zA-Z ]*$/",$name))
		{
			echo "<h2>Invalid name format</h2>";
			$validations++;	
		}

		if ($validations > 0)
		{	
			echo "<h2>PLEASE TRY AGAIN.....<br><hr></h2>";;
		}
		else
		{
		
			//replace single quotes from text field to insert to sql
			$user2 = str_replace("'", "''", $user);
			$email2 = str_replace("'", "''", $email);
			$phone2 = str_replace("'", "''", $phone);
			$pass2 = str_replace("'", "''", $pass);
			
			
			$query = "INSERT INTO users (FullName, UserName, Email, Phone, Pass) 
			VALUES ('$name', '$user2', '$email2', '$phone2', '$pass2')";
			$data = mysql_query ($query)or die(mysql_error());
			if($data)
			{
				echo "<h2>YOUR REGISTRATION IS COMPLETED... <br><hr></h2>";
				
				//send confirmation email to customer

				$Message = "Dear, ". $name . "\r\n";
				$Message .= "--------------------------------" . "\r\n";
				$Message .= "Email: " . $email . "\r\n";
				$Message .= "\r\n";
				$Message .= "Thank you for an account with us." . "\r\n";
				$Message .= "\r\n";
				$Message .= "Your user name is: " . $user . "\r\n";
				$Message .= "\r\n";
				$Message .= "Your password is: " . $pass . "\r\n";
				$Message .= "\r\n";
				$Message .= 'Your Sincerely Natty Noteboooks Inc.' . "\r\n";

				$Headers = 'From: ' . 'Natty Notebooks Inc.';
				$Headers .= 'Email: istvan.franko@live.co.uk';


				$Subject = 'User registration';

				mail($email,$Subject,$Message,$Headers);

				echo "<p>Thank you for an account with us.</p><hr/><br>";
				echo "<p>We sent a confirmation email to you.</p>";	
			}
		}
	
	//close the connection to the database.
	mysql_close($con);
	}
}

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

<br>
<fieldset style="width:60%"><legend>REGISTRATION FORM</legend>

<?php
if ($_SESSION['loggedin'] > 0)
	{
		echo "<br>";
		echo "<h2> You are logged in </h2><br>";
	}
?>

<!--sign up form-->
<p><span class="error">* required fields</span></p>
<form  method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

Name: <span class="error">*</span><span class="error"> <?php echo $nameErr;?> </span>
<br><input type="text" name="name" size="40" value="<?php echo $name; ?>"><br><br>

UserName: <span class="error">*</span><span class="error"> <?php echo $userErr;?> </span>
<br><input type="text" name="user" size="30" value="<?php echo $user; ?>"><br><br>

Email: <span class="error">*</span><span class="error"> <?php echo $emailErr;?> </span>
<br><input type="text" name="email" size="50" value="<?php echo $email; ?>"><br><br>

Phone: 
<br><input type="text" name="phone" size="50" value="<?php echo $phone; ?>"><br><br>

Password: <span class="error">*</span><span class="error"> <?php echo $passErr;?> </span>
<br><input type="password" name="pass" size="30" ><br><br>

Confirm Password: <span class="error">*</span><span class="error"> <?php echo $cpassErr;?> </span>
<br><input type="password" name="cpass" size="30" ><br><br>

<input id="button" type="submit" name="submit" value="Sign-Up">
</form></fieldset><br>

</div>

<div id="footer">
<p align="center">Copyright &#169; Istvan Franko, 2014</p>
</div>
	

</div>
</body>
</html>

