<!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");


//verifying  session for ADM
	session_start();
	if (isset($_SESSION['loginADM']))
	{
		$username = $_SESSION['loginADM'];
	}
	else{
		header("location: loginADM.php");
	}

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

//processing form in case of submit button pressed to REGISTER
if (isset($_POST['submitInformation'])){
	$firstName = sanitizeMySQL($_POST['firstName']);
	$lastName = sanitizeMySQL($_POST['lastName']);
	$username = sanitizeMySQL($_POST['username']);
	$password = sanitizeMySQL($_POST['password']);
	$passwordConfirmation = sanitizeMySQL($_POST['passwordConfirmation']);
	$email = sanitizeMySQL($_POST['email']);
	$today = date("Y-m-d H:i:s");

	//getting user from database (verifying if the username already exists on the database)
	$query = 'SELECT * FROM adm WHERE username = "'.$username.'"';
	$result = mysqli_query($db_connection, $query);
	if (!$result) echo "<script type='text/javascript' language='javascript'>alert('Database query failed');</script>"; 
	$row = mysqli_num_rows($result);
	
	if($row==0){
		//username doesnt exist in the database. Creating new record
		$query = 'INSERT INTO adm VALUES ("'.$username.'","'.$password.'","'.$firstName.'","'.$lastName.'","'.$email.'","'.$today.'");';
		$result = mysqli_query($db_connection, $query);
		if (!$result) echo "<script type='text/javascript' language='javascript'>alert('Database query failed');</script>"; 
		echo "<script language='javascript'> alert('Registration completed');</script>";
		$firstName = "";
		$lastName = "";
		$username = "";
		$password = "";
		$passwordConfirmation = "";
		$email = "";
	}
	else
	{
		//username already exists
		echo "<script language='javascript' type='text/javascript'> alert('Username already exists');</script>";
	}
}

?>
<script language="javascript" type="text/javascript">

// verifies if the inputs in the registration form are non-NULL, if the Username and Password length is from six to 20 characters and if the Password and Password confirmation are the same.
function validateForm() {
	
	//gets the information from the form 'informationForm'
    var firstName = document.forms["informationForm"]["firstName"].value;
	var lastName = document.forms["informationForm"]["lastName"].value;
	var username = document.forms["informationForm"]["username"].value;
	var password = document.forms["informationForm"]["password"].value;
	var passwordConfirmation = document.forms["informationForm"]["passwordConfirmation"].value;
	var email = document.forms["informationForm"]["email"].value;
	var flag = true;
	var message = "";
	var re = /\S+@\S+\.\S+/;

	//verifies if the firstname is non-NULL
    if (firstName == null || firstName == "") {
		document.forms["informationForm"]["firstName"].className = "error";
		message = message + "First name must be filled out\n";
        flag = false;
    }
	
	//verifies if the lastname is non-NULL
	if (lastName == null || lastName == "") {
		document.forms["informationForm"]["lastName"].className = "error";
        message = message + "Last name must be filled out\n";
        flag = false;
    }
	
	//verifies if the username is non-NULL
	if (username == null || username == "") {
		document.forms["informationForm"]["username"].className = "error";
        message = message + "Username must be filled out\n";
        flag = false;
    }
	
	//verifies the username length
	else if(username.length<6||username.length>20) {
		document.forms["informationForm"]["username"].className = "error";
        message = message + "Username must be 6 - 20 characters\n";
        flag = false;
    }
	
	//verifies if the password is non-NULL
	if (password == null || password == "") {
		document.forms["informationForm"]["password"].className = "error";
        message = message + "Password must be filled out\n";
        flag = false;
    }
	
	//verifies the password length
	else if(password.length<6||password.length>20) {
		document.forms["informationForm"]["password"].className = "error";
        message = message + "Password must be 6 - 20 characters\n";
        flag = false;
    }
	
	//verifies if the password and password confirmation are equal
	if (password != passwordConfirmation) {
		document.forms["informationForm"]["passwordConfirmation"].className = "error";
        message = message + "Password confirmation does not match\n";
        flag = false;
    }
	
	//verifies email format
	if (!re.test(email)) {
		document.forms["informationForm"]["email"].className = "error";
        message = message + "Email is incorrect";
        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 upper div creates the upper green block in the top of the webpage  -->
<div  id="upper" style="position:fixed;  top: 0; left:0;">

    <!--The following div creates the 'logout' button  -->
    <div style="position:relative;text-align:left;float: left;"> <input type="button" value="Logout" onclick="location.href='logout.php'" /> </div>
    
    <!--The following div creates the 'register new recipe' button  -->
    <div align="right" style="position:relative;text-align:right;float:right;"><input name="nextPage" type="button" value="Register new recipe" onclick="location.href='indexADM.php'" style=" margin-right:25px"/></div>
</div>

<!--The information div creates the green block in the left of the webpage  -->
	<div id="information">
	  <form id="informationForm" name="informationForm" method="post" action="" onsubmit="return validateForm()">
    <b> Register ADM</b><br />
	      <br />
	    <label for="firstName">
	      Name<br />
        </label>
	    <input type="text"  onfocus="setStyleFocus(this)" placeholder="First" name="firstName" id="firstName" style="width:46%" <?php echo 'value="'.$firstName.'" '?>/>
        <input type="text"  onfocus="setStyleFocus(this)" placeholder=" Last" name="lastName" id="lastName" style="width:46%; margin-left:4%" <?php echo 'value="'.$lastName.'" '?>/>
        <br />
        <br />
        <label for="username">Username<br />
        </label>
        <input type="text"  onfocus="setStyleFocus(this)"  name="username" id="username" placeholder= " 6 - 20 Characters"  style="width:99%" <?php echo 'value="'.$username.'" '?>/>
        <br />
        <br />
        <label for="password">Password<br />
        </label>
        <input type="password"  onfocus="setStyleFocus(this)"  name="password" id="password" placeholder= " 6 - 20 Characters"  style="width:99%" <?php echo 'value="'.$passwordConfirmation.'" '?>/>
        <br />
        <br />
        <label for="passwordConfirmation">Password confirmation<br />
        </label>
        <input type="password"  onfocus="setStyleFocus(this)"  name="passwordConfirmation" id="passwordConfirmation" style="width:99%" <?php echo 'value="'.$passwordConfirmation.'" '?>/>
        <label for="email"><br />
          <br />
          E-mail
          <br />
        </label>
        <input type="email" placeholder="me@example.com"  onfocus="setStyleFocus(this)"  name="email" id="email"  style="width:99%" <?php echo 'value="'.$email.'" '?>/>
        <br />
        <br />
<input type="submit" name="submitInformation" onfocus="setStyleFocus(this)"  id="submitInformation" value="Submit"   style="width:46%"/>
<input type="reset" name="resetInformation" onfocus="setStyleFocus(this)" id="resetInformation" value="Reset" style="width:47%; margin-left:4%"/>
<br />
      </form>
	</div>
</body>
</html>