<?php

	include "library.php";

	$cxn = start_code();

    exit_if_search_bot();

    exit_if_session_expired( $cxn );
    


			/* get variables */

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



			/* 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 jobseeker_accept_login", $cxn );

    $login = true;

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


    if ($login)
	{
		$query = "SELECT * FROM jobseeker_profile WHERE username = '".$username."'";

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

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

			echo black( "There is no jobseeker with this username defined in the system<br><br>" );

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

			echo black( " to you then please " );

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

			if ($data['deleted'] == "yes")
			{
				page_header( "", $cxn );
				echo black( "Your profile has been deleted from the system.<br><br>" );
			}
			else
			if ($data['locked'] == "yes")
			{
				page_header( "", $cxn );
				
				echo black( "Your account is locked.<br><br>" );
				echo black( "Please contact site support on " );
				echo black( " <a href='mailto:".admin_email_address."'>".admin_email_address."</a><br><br>" );
			}
			else
			{
				if (trim(strtoupper( $password )) != trim(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", "jobseeker_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:".admin_email_address."'>".admin_email_address."</a><br><br>" );

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

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

            		$ip_country = get_ip_country( $cxn );

            		$ip_country = str_replace( "'", "''", $ip_country );
		                                           
					$num_logins = $data['number_of_logins'] + 1;


                    $last_login = date( "Y-m-d H:i:s" );

                    $date_today = date( "Y-m-d H:i:s" );
                    
					$query = "UPDATE jobseeker_profile SET number_of_logins = ".$num_logins.", last_login = '".$date_today."', login_attempts = 0 WHERE id = ".$data['id'];
					$result = run_query( $query, $cxn );


					$query = "INSERT INTO jobseeker_logins_log (jobseeker_id, login_datetime) VALUES (".$data['id'].", '".$date_today."')";
					$result = run_query( $query, $cxn );


					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 profile is invalid.<br><br>" );
                		echo black( " Please edit your profile and enter a valid email address that you check regularly.<br><br>" );
                		echo black( " Your email address is listed as " );
                		echo "<span class='gemail'><b>".$data['email_address']."</span></b><br><br><br>";
					}

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

	echo show_home_link();
	
	
	echo "</body>";
	echo "</html>";
?>