<?php

	include "library.php";

	$cxn = start_code();

	$cxn_common = db_common_login();


	
			/* get variables */

	$email = val( $_POST, 'email' );

	$first_name = val( $_POST, 'first_name' );
	$last_name = val( $_POST, 'last_name' );
	$dob = val( $_POST, 'dob' );
        $date_format = val( $_POST, 'date_format' );


	if (! ($email != "" || ($first_name != "" && $last_name != "" && $dob != "" && $date_format != "")))
	{
		echo "You must enter an email address or the other fields on the 'forgotton password' screen.<br><br>";
		exit();
	}

			/* 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  );
	$dob = prep_for_sql( $dob );


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

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

		$query = "SELECT handle, password FROM profile WHERE first_name = '".$first_name."' AND surname = '".$last_name."' AND date_of_birth = '".$sql_date_of_birth."' ORDER BY number_of_logins DESC";

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

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

			$password = $data['password'];

//                    $password = rand(1000,9999);

//                    $password = "pw".$password;

//                    $query = "UPDATE profile SET password = '".$password."' WHERE handle = '".$data['handle']."'";

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


//                    echo "<br><br><table><tr><td>".blue( "Handle" )."</td><td>".black( $data['handle'] )."</td></tr>";
//                    echo "<tr><td>".blue( "Password" )."</td><td>".black( $password )."</td></tr></table><br><br>";
//                    echo "To change your password select 'Edit your Profile' on the main screen<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


	$email = prep_for_sql( $email );

	if ($email != "")
	{
		$query = "SELECT id, first_name, handle, password, email FROM profile WHERE email = '".$email."' ORDER BY number_of_logins DESC";	
	}
	else	
	{
		$query = "SELECT id, first_name, handle, password, email FROM profile WHERE first_name = '".$first_name."' AND surname = '".$last_name."' AND date_of_birth = '".$sql_date_of_birth."' ORDER BY number_of_logins DESC";
	}
	

//		$query = "SELECT id, first_name, handle, password FROM profile WHERE email = '".$email."' ORDER BY number_of_logins DESC";
//		$query = "SELECT id, first_name, handle, password, email FROM profile WHERE first_name = '".$first_name."' AND surname = '".$last_name."' AND date_of_birth = '".$sql_date_of_birth."' ORDER BY number_of_logins DESC";

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

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

//			for ($i=0; $i < query_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 ".$_SESSION['sitename']." login details are:\n\n\n";
		$text_body .= "Handle: ".$handle."\n\n";
		$text_body .= "Password: ".$password."\n\n\n";

		if (query_num_rows($result) > 1)
		{
			$text_body .= "Please note, there is more than one profile with these details\n\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 ".$_SESSION['sitename']." login details are:<br><br><br>";
		$html_body .= "Handle: ".$handle."<br><br>";
		$html_body .= "Password: ".$password."<br><br><br>";

		if (query_num_rows($result) > 1)
		{
			$html_body .= "Please note, there is more than one profile with these details<br><br>";

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

		send_external( $id, $subject, $text_body, $html_body, $cxn, $cxn_common, "live" );


			/* display success on the screen */

		echo black( "<br>Your handle and password have been emailed to you<br><br>" );
		
		echo black( "The email address that we have on file for you is " );
		
		echo "<span class='gemail'>".$data['email']."</span><br><br>";
		
		echo black( "If you have changed email address then you will need to contact site administration.<br><br><br>");

		echo show_home_link();
	}

?>