<?php

	include "library.php";

	$cxn = start_code();

	$cxn_common = db_common_login();

	
			/* get variables */

	$email = $_POST['email'];

	$first_name = $_POST['first_name'];
	$last_name = $_POST['last_name'];
//	$dob = $_POST['dob'];
  //      $date_format = $_POST['date_format'];


			/* start page */

	page_header( "", $cxn, $cxn_common );

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

	{
		$first_name = prep_for_sql( $first_name );
		$last_name = prep_for_sql( $last_name  );
		$email = prep_for_sql( $email );

//		$dob = prep_for_sql( $dob );


                if ($first_name != "" && $last_name != "" )
                {

//                $sql_date_of_birth = convert_date_of_birth( $dob, $date_format, $cxn );


		$query = "SELECT username, password FROM account WHERE first_name = '".$first_name."' AND surname = '".$last_name."' AND email_address = '".$email."'";

//                $query = "SELECT username, password FROM account WHERE email_address = '".$email."'";

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

		if (mysql_num_rows($result) != 0)
                {
                    $data = get_row( $result );

                    $password = rand(1000,9999);

                    $password = "pw".$password;

                    $query = "UPDATE account SET password = '".$password."' WHERE first_name = '".$first_name."' AND surname = '".$last_name."' AND email_address = '".$email."'";

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

                    echo "<table><tr><td>".blue( "Username" )."</td><td>".black( $data['username'] )."</td></tr>";

                    echo "<tr><td>".blue( "Password" )."</td><td>".black( $password )."</td></tr></table><br><br>";

                    echo "To change your password select 'Edit Account' on the main screen<br><br><br><br>";

//                    echo show_home_link();

                }
            }

//	if ($email == "")
//	    echo black( "Please enter an email address to have the handle and password emailed to you" );
//        else


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

		$email = prep_for_sql( $email );

		$query = "SELECT * FROM account WHERE first_name = '".$first_name."' AND surname = '".$last_name."' AND email_address = '".$email."'";
		//$query = "SELECT username, password FROM account WHERE email_address = '".$email."'";
		

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

		if (mysql_num_rows($result) == 0)
			echo black( "There is no account in the system with these details" );
		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 );

				$account_id = $data['account_id'];
				$first_name = $data['first_name'];
				$username = $data['username'];
				$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 .= "Username: ".$username."\n";
				$text_body .= "Password: ".$password."\n\n";

				if (mysql_num_rows($result) > 1)
				{
					$text_body .= "Please note, there is more than one account in the system with these details\n";

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


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

				if (mysql_num_rows($result) > 1)
				{
					$html_body .= "Please note, there is more than one account in the system with these details<br>";

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

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

				/* display success on the screen */

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

			echo show_home_link();
		}
	}
}

?>

