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

session_start();

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

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

<html>

<head>
<title>Natty Notebooks Inc.</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>

<body>

<div id="container">

	<div id="header">
	<! header picture in css >
	</div>
	
	<div id="navigation">
		<?php include 'navigation-bar.php'; ?>
	</div>
	
<div id="aside">
	<!--Link 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>You can request a password reminder here if you forgot it.</p> 
	</fieldset><br>
</div>


<div id="content">

<?php


// define variables and set to empty values
$EmailErr = $Email = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{


	//input validation
	if (empty($_POST["Email"]))
	{
		$EmailErr = "(Email is required)";
	}
	else
	{
		$Email = test_input($_POST["Email"]);

		$con = mysql_connect("localhost","*****","*****");
		if (!$con)
		{
			die('Could not connect: ' . mysql_error());
		}


		mysql_select_db("university", $con);
			
		$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 $Email table row must be 1 row
		if($count==1)
		{
			$query=mysql_query("SELECT * FROM users WHERE Email='$Email'");
			$row=mysql_fetch_object($query);
			echo mysql_error();

			//get the value of each column and add to local variables
			$FullName = $row->FullName;
			$Phone = $row->Phone;
			$Pass = $row->Pass;
				
			//send confirmation email to customer

			$Message = "Dear, ". $FullName . "\r\n";
			$Message .= "--------------------------------" . "\r\n";
			$Message .= "Email: " . $Email . "\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 = 'Password reminder';

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

			echo "<br>";
			echo "<h2>We sent a password reminder email to you.</h2><hr/>";	
		}
		else 
		{
			echo "<br>";
			echo "<h2>Wrong email address</h2><hr/>";
		}

		//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:70%"><legend>PASSWORD REMINDER</legend>

<br><form  method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<!--password reminder form-->
Email address: 	<span class="error">*</span><span class="error"> <?php echo $EmailErr;?></span><br>
				<input type="text" name="Email" size="40" value="<?php echo $Email ;?>"><br><br>
			
<input type="submit" value="Send">


</fieldset>
<br>

</div>

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

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