<link rel="stylesheet" href="stylesheets/precision_tracking.css" type="text/css" />
<style>body {background-image:none;}</style>

<?php

include('../pg-connect.php'); // Good practice to keep db connection files out of web root


// Form for entering username to reset password
$form = '
    <p>Please enter your email address below and we will send you your username.</p>
    <form action="forgot_username.php" method="post">
    <input type="text" name="email" autofocus placeholder="Please enter your email address" style="margin-bottom:5px;width:200px;" />
    <button type="submit" name="submit">Send</button>
    </form>
';



if ($_SERVER['REQUEST_METHOD'] == 'POST') // Handle the form submission.
{ 
    
    // Validate and secure the form data:
	$problem = FALSE;

    // If nothing or doesn't contain '@' then there is a problem
    if (empty($_POST['email']) || (substr_count($_POST['email'], '@') != 1) )
	{
		$problem = TRUE;
		print $form;
		print '<p class="error">Please enter your email address</p>';
	}
    
    else     
	{
		$email = trim(strip_tags($_POST['email'])); // Strip HTML and potentially dangerous scripts
	}

    
    
    // Execute if there are no problems with the form
    if (!$problem) 
	{
	
		// Retrieve username from supplied email address
		$query = "SELECT username, companyid FROM Client WHERE email = '".$email."'";
		$r = pg_query($dbc,$query); // Run the query
	
		// If email address is not on file
		if (pg_num_rows($r) < 1) 
			{
				print $form.'<p class="error">We are unable to match a username to your email address. Please <a href="http://www.precisiontracking.co.nz/contact_us.html" target="_blank">contact us</a> by phone on 0800 GPS 001 or via email at <a href="mailto:admin@precisiontracking.co.nz">admin@precisiontracking.co.nz</a></p>.';
			}
	 
		// If email address has a username ...
		else
		{

		   // Prepare to email username
		   while ($row = pg_fetch_array($r)) 
			{ 
				$username = $row['username'];
				$company_id = $row['companyid'];
		   
				// Construct HTML email message
				$html_message = '
				<p>Precision Tracking received a request to send your username to you.</p>
				<p>Your username is <strong>'.$username.'</strong>.</p>
				<a href="http://www.precisiontracking.co.nz/client_login.php"><button style="padding:10px;background-color:#51b759;color:#fff;cursor:pointer;font-weight:bold;">Proceed to Login</button></a>
				<p>If you experience any difficulties, or you did not request your username to be sent to you, please do not hesitate to <a href="http://www.precisiontracking.co.nz/contact_us.html" target="_blank">contact us</a> by phone on 0800 GPS 001 or via email at <a href="mailto:admin@precisiontracking.co.nz">admin@precisiontracking.co.nz</a>.
				';
		   
				// Insert email send details into email_outbox table where it will automatically be sent
				$email_query = "INSERT INTO email_outbox ( src_address, dest_address, company_id, subject, text_message, html_message )
								VALUES ( 'info@precisiontracking.co.nz','$email','$company_id','Your username','','$html_message')";
								
				$email_insert = pg_query($dbc, $email_query);
               
                if (pg_affected_rows($email_insert) == 1)
                    
                {
				    // Success message for screen
				    print '<p>Your username has been emailed to <strong>';
				    print $email;
				    print '</strong>.</p><p>If you experience any difficulties, please do not hesitate to <a href="contact_us.html" target="_blank">contact us</a>.</p>';
                }
               
               else
               {
                   print '<p class="error">We are unable to retreive your username at the moment. Please <a href="http://www.precisiontracking.co.nz/contact_us.html" target="_blank">contact us</a> by phone on 0800 GPS 001 or via email at <a href="mailto:admin@precisiontracking.co.nz">admin@precisiontracking.co.nz</a>.</p>';
               }
			
			} // End while
		}
	
	} // No problem!

} // End of form submission IF.
else // Print form as user has loaded page for first time
{
	print $form;
}


?>
