<?php

	include "library.php";

	$cxn = start_code();

	
			/* get variables */

	$email = $_POST['email'];


			/* start page */

	page_header( "", $cxn );

	if ($email == "")
		echo black( "You must enter an email address" );
	else
	{
		$email = prep_for_sql( $email );

		$query = "SELECT id, first_name, handle, password FROM profile WHERE email = '".$email."' ORDER BY number_of_logins DESC";

		$result = run_query( $query, $cxn );

		if (mysql_num_rows($result) == 0)
			echo black( "There is no member in the system with this email address" );
		else
		{
				/* send an email to the member(s) with this email address */

			for ($i=0; $i < mysql_num_rows($result); $i++)
			{
				$data = get_row( $result );

				$id = $data['id'];
				$first_name = $data['first_name'];
				$handle = $data['handle'];
				$password = $data['password'];

				$subject = $_SESSION['sitename']." site login details";

				$text_body = "Dear ".$first_name."\n\n";
				$text_body .= "Your login details are:\n\n";
				$text_body .= "Handle: ".$handle."\n";
				$text_body .= "Password: ".$password."\n\n";

				if (mysql_num_rows($result) > 1)
				{
					$text_body .= "Please note, there is more than one profile with this email address\n";

					if ($i == 0)
						$text_body .= "This profile is the most-used profile\n";
				}


				$html_body = "Dear ".$first_name."<br><br>";
				$html_body .= "Your login details are:<br><br>";
				$html_body .= "Handle: ".$handle."<br>";
				$html_body .= "Password: ".$password."<br><br>";

				if (mysql_num_rows($result) > 1)
				{
					$html_body .= "Please note, there is more than one profile with this email address<br>";

					if ($i == 0)
						$html_body .= "This profile is the most-used profile<br>";
				}

				send_external( $id, $subject, $text_body, $html_body, $cxn );
			}

				/* display success on the screen */

			echo black( "Your handle and password has been emailed to you<br><br>" );

			echo show_home_link();
		}
	}

?>

