<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Register</title>
<?php
include("functions.php");
include("login.php");

//initializing form fields
	$firstName = "";
	$lastName = "";
	$username = "";
	$password = "";
	$passwordConfirmation = "";
	$email = "";

//processing form in case of submit button pressed to LOGIN
if (isset($_POST['submitLogin'])){
	$login = sanitizeMySQL($_POST['login']);
	$pass = sanitizeMySQL($_POST['pass']);
	
	//getting adm from database
	$query = 'SELECT password FROM adm WHERE username = "'.$login.'"';
	$result = mysqli_query($db_connection, $query);
	if (!$result) echo "Database query failed:";
	else if($row = mysqli_fetch_assoc($result))
	{
		//comparing password from database and password typed 
		if ($pass == $row['password']){
			
			//password is correct. Creating session
			session_start();
			$_SESSION['loginADM'] = $login;
			$_SESSION['passwordADM'] = $pass;
			$_SESSION['firstNameADM'] = $row['firstName'];
			$_SESSION['lastNameADM'] = $row['lastName'];
			$_SESSION['emailADM'] = $row['email'];
			header("Location: indexADM.php");
		}
		else{
			//password is incorrect
			echo "<script type='text/javascript'>alert('Invalid username/password combination');</script>";
		}
	}
	else{
		//user wasnt find in the database
		echo "<script type='text/javascript'>alert('Invalid username/password combination');</script>";
	}
}
	
?>
<script language="javascript" type="text/javascript">

//verifies if the Login and Password inputs in the login administrator form are non-NULL. 
function validateForm2() {
	
	//gets the information from the form 'loginForm'
    var login = document.forms["loginForm"]["login"].value;
	var pass = document.forms["loginForm"]["pass"].value;
	var flag = true;
	var message = "";
	var re = /\S+@\S+\.\S+/;
	
	//verifies if the username is non-NULL
    if (login == null || login == "") {
		document.forms["loginForm"]["login"].className = "error";
		message = message + "Username must be filled out\n";
        flag = false;
    }
	
	//verifies if the password is non-NULL
	if (pass == null || pass == "") {
		document.forms["loginForm"]["pass"].className = "error";
		message = message + "Password must be filled out\n";
        flag = false;
    }
	
	//display error message if necessary
	if(!flag) alert(message);
	return flag;
	
}

//set class name to focus (normal style to describe non-error)
function setStyleFocus(element)
{
	element.className="focus";
}
</script>
<link rel="stylesheet" type="text/css" href="format.css"></head>

<body >

<!--The information div creates the green block in the left of the webpage  -->
<div id="result">
<form id="loginForm" name="loginForm" method="post" action="loginADM.php" onsubmit="return validateForm2()">
<b> Login ADM</b><br />
        <br />
        <label for="login">Username<br />
        </label>
        <input type="text"  onfocus="setStyleFocus(this)"  name="login" id="login"  style="width:40%"/>
        <br />
        <br />
        <label for="pass">Password<br />
        </label>
        <input type="password"  onfocus="setStyleFocus(this)"  name="pass" id="pass"  style="width:40%"/>
        <br />
        <br />
        <input type="submit" name="submitLogin" onfocus="setStyleFocus(this)"  id="submitLogin" value="Login"   style="width:40%"/>
        <br />
      </form>
    </div>
</body>
</html>