<?php

	include "library.php";

	$cxn = start_code();


			/* 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 );

//	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 != "")
    {
        $sql_date_of_birth = convert_date_of_birth( $dob, $date_format, $cxn );

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

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

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

			$password = $data['password'];
    	}
    }


	$email = prep_for_sql( $email );

	if ($email != "")
	{
		$query = "SELECT id, first_name, username, password, email_address, returned_mail FROM account WHERE email_address = '".$email."' ORDER BY number_of_logins DESC";	
	}
	else	
	{
		$query = "SELECT id, first_name, username, password, email_address, returned_mail FROM account WHERE first_name = '".$first_name."' AND surname = '".$last_name."' ORDER BY number_of_logins DESC";
	}
	

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

	if (query_num_rows($result) == 0)
		echo black( "There is no account 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 );
    
		if ($data['returned_mail'] == "yes")
		{
			page_header( "", $cxn );
			
    		echo black( "<br>We have received a bounced email indicating that the email address that you have on file in your account is invalid.<br><br>" );
    		echo black( " Please edit your profile and enter a valid email address that you check regularly.<br><br><br>" );
//    		echo black( " Your email address is listed as " );
//    		echo "<span class='gemail'><b>".$data['email']."</span></b><br><br><br>";

    		$_SESSION['account_id'] = $data['id'];
    		
			echo show_link( "Edit Profile", "edit_profile.php", "newprofile", "false" )."<br><br>";
		}
		else
        {
    
    		$id = $data['id'];
    		$first_name = $data['first_name'];
    		$username = $data['username'];
    		$password = $data['password'];
    
    		$subject = sitename." site login details";
    
    		$text_body = "Dear ".$first_name.",\n\n";
    		$text_body .= "Your ".sitename." login details are:\n\n\n";
    		$text_body .= "Username: ".$username."\n\n";
    		$text_body .= "Password: ".$password."\n\n\n";
    
    		if (query_num_rows($result) > 1)
    		{
    			$text_body .= "Please note, there is more than one account with these details\n\n";
    
    //					if ($i == 0)
    				$text_body .= "This profile is the most-used account of these\n";
    		}
    
    
    		$html_body = "Dear ".$first_name.",<br><br>";
    		$html_body .= "Your ".sitename." login details are:<br><br><br>";
    		$html_body .= "Username: ".$username."<br><br>";
    		$html_body .= "Password: ".$password."<br><br><br>";
    
    		if (query_num_rows($result) > 1)
    		{
    			$html_body .= "Please note, there is more than one account with these details<br><br>";
    
    //					if ($i == 0)
    				$html_body .= "This profile is the most-used account<br>";
    		}
    
    		send_external( $id, $subject, $text_body, $html_body, $cxn, "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_address']."</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();
        }
	}

?>