<?
include "header.php";
if(isset($_GET['r'])) { $r = $_GET['r']; } elseif(isset($_POST['r'])) { $r = $_POST['r']; } else { $r = "login"; }
if(isset($_POST['task'])) { $task = $_POST['task']; } else { $task = "main"; }
if($r == "support") { $return = "../support.html"; } else { $return = "./login.php"; }


if($task == "forgot_do") {
  if($_POST['submit'] == "Cancel") {
  header("Location: $return"); 
  exit();
  }
$email_count = mysql_num_rows(mysql_query("SELECT * FROM ims WHERE email = '$_POST[forgotten_email]'"));

if($email_count != 1) {

echo $header;
echo "
<table width='520' cellpadding='0' cellspacing='0'>
<tr>
<form action='forgot.php' method='GET'>
<td valign='top'>
<b>Password Retrieval</b><br><font class='csmall'>
The email address you have provided is invalid or does not exist in our database.
<br><br></font>
<input type='submit' class='button' value='Try Again'>
</td>
</tr>
</form>
</table>";
echo $footer;
exit;

} else {
$user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE email = '$_POST[forgotten_email]'"));
mail($user_info[email], "Forgotten Login Information", "Hello, $user_info[fname] $user_info[lname]\n\nThis email is an automatic response to a forgotten username/password request from your Image Management Account.\n\nYour login information is as follows:\nEmail: $user_info[email]\nPassword: $user_info[password]\n\nThank You.", "From: $admin_info[email]");
echo $header;
echo "
<table width='520' cellpadding='0' cellspacing='0'>
<tr>
<form action='$return' method='GET'>
<td valign='top'>
<b>Password Retrieval</b><br><font class='csmall'>
Your account information has been emailed to your email address ($user_info[email]).
<br><br></font>
<input type='submit' class='button' value='Return To Login'>
</td>
</tr>
</form>
</table>";
echo $footer;
exit;
}}



echo $header;
echo "
<table width='520' cellpadding='0' cellspacing='0'>
<tr>
<form action='forgot.php?r=$r' method='POST'>
<td valign='top'>
<b>Password Retrieval</b><br>
Enter the email address which you used to create your account and your password<br> will be automatically sent to you.</font><br><br>
<b>Email Address</b><br>
<input type='text' size='25' name='forgotten_email'>
<input type='submit' name='submit' class='button' value='Retrieve Password' style='margin-bottom: 2px;'>
<input type='submit' name='submit' class='button' value='Cancel' style='margin-bottom: 2px;'>
<input type='hidden' name='task' value='forgot_do'>
</td>
</tr>
</form>
</table>";
echo $footer;
?>