<?php

	include "library.php";

	$cxn = start_code();
	

			/* get variables */

	$nextpage = val( $_SESSION, "nextpage" );


    		/* system stats logging */

	if (val( $_POST, "screen_width" ) != "")
		log_screen_size( $_POST['screen_width'], $_POST['screen_height'], $cxn );

	if (val( $_POST, "screen_width" ) != "")
	{
		log_screen_size( $_POST['screen_width'], $_POST['screen_height'], $cxn );

		$_SESSION['screen_height'] = $_POST['screen_height'];
	}


			/* search for the user and password */

	$username = prep_for_sql( val( $_POST, 'username' ) );
	$password = prep_for_sql( val( $_POST, 'password' ) );

	crash_if_missing( $nextpage, "nextpage not set in accept_login", $cxn );

    $login = true;


	if ($username == "")
	{
		page_header( "", $cxn );
		echo black( "The username entered is blank<br><br>" );
                $login = false;
	}
	else
	if ($password == "")
	{
		page_header( "", $cxn );
		echo black( "The password entered is blank<br><br>" );
                $login = false;
	}


	{
		$query = "SELECT * FROM account WHERE username = '".$username."'";

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

		if (query_num_rows($result) == 0)
		{
			page_header( "", $cxn );

			echo black( "There is no account with this user name defined in the system<br><br>" );

			echo black( "If you have forgotten your user name and password details and would like to have them emailed" );

			echo black( " to you then please " );

			echo show_link( "click here", "forgotten_password.php" )."<br><br>";
		}
		else
		{
			$data = get_row( $result );

			if ($data['deleted'] == "yes")
			{
				page_header( "", $cxn );
				echo black( "Your account has been deleted from the system.<br><br>" );
			}
			else
			{
				if ((strtoupper( $password ) != strtoupper( $data['password'] )))
				{
					page_header( "", $cxn );

					echo black( "Password Incorrect<br><br><br>" );

					echo black( "If you have forgotten your password and would like to have it emailed to you then please " );

					echo show_link( "click here", "forgotten_password.php" )."<br><br><br>";

					if ($data['login_attempts'] >= 10)
					{
						echo black( "Your account has been locked.<br><br>" );
						echo black( "Please contact site support on" );
						echo black( " <a ref='mailto:mark.mcilroy@outlook.com'>mark.mcilroy@outlook.com</a><br><br>" );

						$query = "UPDATE account SET locked = 'yes' WHERE id = ".$data['id'];
						$result = run_query( $query, $cxn );
					}
					else
					{
						$attempts = $data['login_attempts'] + 1;

						$query = "UPDATE account SET login_attempts = ".$attempts." WHERE id = ".$data['id'];
						$result = run_query( $query, $cxn );
					}
				}
				else
				{
					$_SESSION['account_id'] = $data['id'];


					$query = "UPDATE account SET login_attempts = 0 WHERE id = ".$data['id'];
					$result = run_query( $query, $cxn );


//					if ($data['returned_mail'] == "yes")
//					{
//						page_header( "", $cxn, "" );
//						
//						echo red( "The email address that you have entered in your profile is not a valid email address.<br><br>" );
//						echo red( "Mail was returned from this address as undeliverable.<br><br>" );
//						echo red( "Your email address is listed as <h2>".$data['email']."</h2>" );
//						echo red( "Please edit your profile and enter a valid email address.<br><br><br>" );
//						echo show_link( "Edit Profile", "edit_profile.php", "newprofile", "false" )."<br><br>";
//					}
//					else

						header( "Location: ".$nextpage );
				}
			}
		}
	}

	echo show_home_link();
?>