<?php
session_start();
include "../conn.php";
include "../functions.php";

$userID = checkAdmin(session_id());

$ta = $userID;

$query = "select firstName, lastName from theusers where userID='$userID'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$name = $row[0] . ", " . $row[1];

$submit = $_POST["submit"];

if (isset($submit))
{
	$firstName = $_POST["firstName"];
	$lastName = $_POST["lastName"];
	$userName = $_POST["userName"];
	$password1 = $_POST["password1"];
	$password2 = $_POST["password2"];
	$userID = $_POST["userID"];
	$manager = $_POST["manager"];
	
	if ( ! isset($userName))
	{
		$err .= "<li><font color=red>Access Name is missing</font></li>";
	}
	
	else
	{
		$query = "select userName from theusers where userName='$userName' and userID <> '$userID'";
		$result = mysql_query($query) or die (mysql_error());
		
		if (mysql_num_rows($result) > 0)
		{
			$err .= "<li><font color=red>Access Name $userName already exists</font></li>";
		}
	}
	
	if (! $err)
	{
		if($password1 != $password2)
		{
			$err .= "<li><font color=red>Tow passwords do not match</font></li>";
		}
		else
		{
			$query = "update theusers set passwd='$password1' where userID='$userID'";
			mysql_query($query);
		}
	}
	
	if (! $err)
	{
		$query = "update theusers set firstName='$firstName', lastName='$lastName', userName='$userName', manager='$manager' where userID='$userID'";
		mysql_query($query) or die (mysql_error());
		
		if ($ta==1)
		{
			header ("location: administrators.php");
		}
		else
		{
			header ("location: index.php");
		}
	}
}

?>

<html>
<head>
<title>Administration Area</title>
</head>

<body>

<center>
<table width=80% border=0>
  <tr>
    <td width=1>
     <img src="../img/logo.jpg">
    </td>
    
    <td valign="bottom">
      <table border=0 width=100% height=100% cellpadding=13>
        <tr>
          <td align="center"><h2><b>Administration Module</b></h2></td>
        </tr>
        
        <tr>
          <td align="center"><a href="logoff.php">Log out</a>&nbsp;&nbsp;&nbsp;
          <a href="index.php">Main Menu</a></td>
        </tr>
      </table>
    </td>
    
    <td align="right" valign="bottom">
      <?php echo "<b>Welcome! $name</b>"; ?>
    </td>
    
  </tr>
</table>
</center>

<center>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<table width=80% border=0 cellpadding=5>
<tr>

<?php
if ($err)
{
	$err = "The following errors occured when adding new admin user" . $err;
	echo "<td colspan=2 align=\"center\" bgcolor=\"yellow\">";
	echo $err;
}
else
{
	echo "<td colspan=2 align=\"center\">";
	echo "&nbsp;";
}
$userID=$_POST["userID"];
$query = "select firstName, lastName, userName, manager from theusers where userID='$userID'";
//echo $query;
$result = mysql_query($query) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
	$firstName = $row["firstName"];
	$lastName = $row["lastName"];
	$userName = $row["userName"];
	$manager = $row["manager"];
}
?>
</td>
</tr>

<tr>
<td align="right">First Name:</td>
<td align="left"><input type="text" name="firstName" value="<?php echo $firstName; ?>"></td>
</tr>

<tr>
<td align="right">Last Name:</td>
<td align="left"><input type="text" name="lastName" value="<?php echo $lastName; ?>"></td>
</tr>

<tr>
<td align="right">Manager:</td>
<td align="left"><input type="checkbox" name="manager" value="1" <?php if ($manager == 1) echo "checked"; ?>></td>
</tr>

<tr>
<td align="right">Access Name:</td>
<td align="left"><input type="text" name="userName" value="<?php echo $userName; ?>"></td>
</tr>

<tr>
<td align="right">Password:</td>
<td align="left"><input type="password" name="password1" value="<?php echo $password1; ?>"></td>
</tr>

<tr>
<td align="right">Confirm Password:</td>
<td align="left"><input type="password" name="password2" value="<?php echo $password2; ?>"></td>
</tr>

<tr>
<td colspan="2" align="center"><input type="hidden" name="userID" value="<?php echo $userID; ?>"><input type="submit" name="submit" value="Modify">&nbsp;<input type="reset" value="cancel"></td>
</tr>
</table>
</form>
</center>


</body>
</html>