<!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
	$n203 = "";
	$n204 = "";
	$n205 = "";
	$n255 = "";
	$n291 = "";
	
//processing form in case of submit button pressed to INSERT NEW FOOD
if (isset($_POST['submitInformation'])){
	$FdGrp_Cd = sanitizeMySQL($_POST['FdGrp_Cd']);
	$Long_Desc = sanitizeMySQL($_POST['Long_Desc']);
	$n203 = sanitizeMySQL($_POST['n203']);
	$n204 = sanitizeMySQL($_POST['n204']);
	$n205 = sanitizeMySQL($_POST['n205']);
	$n255 = sanitizeMySQL($_POST['n255']);
	$n291 = sanitizeMySQL($_POST['n291']);

	
	if ($n203 == ""){
	$n203 = 0; 
	}
	if ($n204 == ""){
	$n204 = 0; 
	}
	if ($n205 == ""){
	$n205 = 0; 
	}
	if ($n255 == ""){
	$n255 = 0; 
	}
	if ($n291 == ""){
	$n291 = 0; 
	}
	
	//insert information
	$query = 'INSERT INTO food_des (FdGrp_Cd,Long_Desc,`203`,`204`,`205`,`255`,`291`) VALUES ("'.$FdGrp_Cd.'","'.$Long_Desc.'","'.$n203.'","'.$n204.'","'.$n205.'","'.$n255.'","'.$n291.'");';
	$result = mysqli_query($db_connection, $query);
	if (!$result) echo "<script type='text/javascript'>alert('Database query failed');</script>"; 
	echo "<script type='text/javascript'>alert('Food registered');</script>";
}
?>
<script language="javascript" type="text/javascript">

// verifies if the inputs in the registration form are different than NULL and if the nutrient amounts are valid numbers.
function validateForm() {
	
	//gets the information from the form 'informationForm'
    var Long_Desc = document.forms["informationForm"]["Long_Desc"].value;
	var n203 = document.forms["informationForm"]["n203"].value;
    var n204 = document.forms["informationForm"]["n204"].value;
    var n205 = document.forms["informationForm"]["n205"].value;
    var n255 = document.forms["informationForm"]["n255"].value;
    var n291 = document.forms["informationForm"]["n291"].value;

	var flag = true;
	var message = "";
	var re = /\S+@\S+\.\S+/;
	
	//verifies if the long description is non-NULL
    if (Long_Desc == null || Long_Desc == "") {
		document.forms["informationForm"]["Long_Desc"].className = "error";
		message = message + "Food Description must be filled out\n";
        flag = false;
    }
	
	//verifies if the protein value (n203) is a valid number
	else if (isNaN(n203)) {
		document.forms["informationForm"]["n203"].className = "error";
        message = message + "Protein value must be a number\n";
        flag = false;
    }
	else if(n203<0)
	{
		document.forms["informationForm"]["n203"].className = "error";
        message = message + "Protein value must be a number greater than or equal to 0\n";
		flag = false;
	}
	
	//verifies if the lipid value (n204) is a valid number
	else if (isNaN(n204)) {
		document.forms["informationForm"]["n204"].className = "error";
        message = message + "Lipid value must be a number\n";
        flag = false;
    }
	else if(n204<0)
	{
		document.forms["informationForm"]["n204"].className = "error";
        message = message + "Lipid value must be a number greater than or equal to 0\n";
		flag = false;
	}
	
	//verifies if the Carbohydrate value (n205) is a valid number
	else if (isNaN(n205)) {
		document.forms["informationForm"]["n205"].className = "error";
        message = message + "Carbohydrate value must be a number\n";
        flag = false;
    }
	else if(n205<0)
	{
		document.forms["informationForm"]["n205"].className = "error";
        message = message + "Carbohydrate value must be a number greater than or equal to 0\n";
		flag = false;
	}
	
	//verifies if the Water value (n255) is a valid number
	else if (isNaN(n255)) {
		document.forms["informationForm"]["n255"].className = "error";
        message = message + "Water value must be a number\n";
        flag = false;
    }
	else if(n255<0)
	{
		document.forms["informationForm"]["n255"].className = "error";
        message = message + "Water value must be a number greater than or equal to 0\n";
		flag = false;
	}
	
	//verifies if the Fiber value (n291) is a valid number
	else if (isNaN(n291)) {
		document.forms["informationForm"]["n291"].className = "error";
        message = message + "Fiber value must be a number\n";
        flag = false;
    }
	else if(n291<0)
	{
		document.forms["informationForm"]["n291"].className = "error";
        message = message + "Fiber value must be a number greater than or equal to 0\n";
		flag = false;
	}
	
	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;">

    <!--creates the 'Logout' button -->
	<div style="position:relative;text-align:left;float: left;"> <input type="button" value="Logout" onclick=	"location.href='logout.php'" /> </div>
	<!--creates the 'Next Page and Update Food Information' buttons -->
	<div align="right" style="position:relative;text-align:right;float:right;"><input name="nextPage" type="button" value="Register new ADM" onclick="location.href='registerADM.php'" style=" margin-right:25px"/><input name="updateFood" type="button" value="Update Food Information" onclick="location.href='updateFood.php'" style=" margin-right:25px"/></div>

</div>

<!--The information div creates the green block in the center of the webpage  -->
	<div id="information" style=" width: 60%">
	  <form id="informationForm" name="informationForm" method="post" action="" onsubmit="return validateForm()">
  <b> Register New Food</b><br />
	      <br />
	    <label for="Long_Desc">
	      Food Description<br />
        </label>
	    <input type="text"  onfocus="setStyleFocus(this)"  name="Long_Desc" id="Long_Desc" style="width:100%"/>
	    <br />
        <br />
        <label for="FdGrp_Cd">Food Group<br />
        </label>
        <select size="1" onfocus="setStyleFocus(this)"  name="FdGrp_Cd" id="FdGrp_Cd" style="width:99%;height:100%">
        
		<?php
		
			//creating the food categories option box
			$query = 'SELECT * FROM fd_group ';
			$result = mysqli_query($db_connection, $query);
			if (!$result) echo "<script type='text/javascript'>alert('Database query failed');</script>"; 
			while($row = mysqli_fetch_assoc($result))
			{
					echo '<option  value="'.$row['FdGrp_Cd'].'"  >'.$row['FdGrp_Desc'].'</option>';
			}
		?>
        </select>
        <br />
        <br />
        Nutricional information<br /><br />

        <table align="center">
        <?php
			//getting nutrients information
			$query = 'SELECT * FROM nutr_def WHERE Nutr_No = 203 OR Nutr_No = 204 OR Nutr_No = 205 OR Nutr_No = 255 OR Nutr_No = 291';
			$result = mysqli_query($db_connection, $query);
			if (!$result) echo "<script type='text/javascript'>alert('Database query failed');</script>"; 
			
			//creating form inputs for nutrients
			while($row = mysqli_fetch_assoc($result))
			{
				echo '<tr><td align="right" >'.$row['NutrDesc'].' ('.$row['Units'].')</td>
					  <td><input type="number" step="any"  id=n'.$row['Nutr_No'].' name=n'.$row['Nutr_No'].' ></td>';
				$row = mysqli_fetch_assoc($result);	 
				if($row!=''){ 
			    echo  '<td align="right" >'.$row['NutrDesc'].' ('.$row['Units'].')</td>
					  <td><input type="number" step="any"  id=n'.$row['Nutr_No'].' name=n'.$row['Nutr_No'].' ></td></tr>';
				}
			}
		?>
        </table>
        <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>