<?php
include("../include/session.php");  
include("user.class.php");
require_once("../include/checkFormat.php"); 
require_once("../include/accessCheck.php");
if(!$session->logged_in || $session->userlevel!=1)     //Not logged in or not Super Admin
{
    echo accessDeniedMsg;
    exit;
}
if (isset($_GET['page'])){ //find out what the sub page is
	$page = $_GET['page'];
	}
else{$page = 'display';}

global $pageNum;
if(isset($_GET['pageNum']))
        $pageNum=$_GET['pageNum']; //Get the current page number
    else
        $pageNum=1;
        
/**
* displayApps - Displays the app database table in
* a nicely formatted html table.
*/
function displayUsers(){
    global $pageNum;
//paging code below
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++      
$result=user::showAllusers();
$nrResults = mysql_numrows($result);

$limit=20; // Limit of result per page 
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
$result = user::showAllusers($pageNum,$limit);
$num_rows = mysql_numrows($result);

echo "<p><img  SRC=\"../icons/add.png\" >&nbsp;&nbsp;&nbsp;<a href='user.php?page=add'>Add New Administrator</a></p>"; 
if(!$result || ($num_rows < 0)){
echo "Error displaying info";
return;
}
if($num_rows == 0){
echo "Database table empty";
return;
}

/* Display table contents */
echo "<table  cellspacing=\"3\" cellpadding=\"2\">";
echo "<tr><th><b>Name</b></th><th><b>Email</b></th><th><b>Phone</b></th><th><b>User Name</b></th><th><b>Action</b></th></tr>\n";
for($i=0; $i<$num_rows; $i++){
    
$userID  = mysql_result($result,$i,"adminID");
$firstName  = mysql_result($result,$i,"firstName");
$lastName  = mysql_result($result,$i,"lastName");
$email  = mysql_result($result,$i,"email");
$phone  = mysql_result($result,$i,"phone");
$username  = mysql_result($result,$i,"username");

echo '<tr><td class="'.($i%2? "c1":"c2").'">'.$firstName."  ".$lastName.'</td>';

echo '<td class="'.($i%2? "c1":"c2").'">'.$email.'</td>';    

echo '<td class="'.($i%2? "c1":"c2").'">'.$phone.'</td>';    

echo '<td class="'.($i%2? "c1":"c2").'">'.$username.'</td>';    
?> 
<script type="text/javascript">

function submitForm(form)
{
    document.getElementById(form).submit();
    return true;  
}
</script>                   
<td align="center" class="<?php echo ($i%2? "c1":"c2") ?>">
    <div style="float: left; width:30px;"> 
            <a href="<?php echo $_SERVER['PHP_SELF'] . '?page=edit&adminID=' . $userID?>"><img SRC="../icons/Edit.png" title="Edit Administrator" ALT="Edit Administrator" border="0"/></a>
    </div>
    <!--    ||||||||||||||||||   --> 
    <div style="float: left; width:30px;">
            <a href="<?php echo $_SERVER['PHP_SELF'] . '?page=display&delUserID=' . $userID?>" onClick="javascript:return confirm('Are you sure you want to delete this Administrator?');"><img border="0" SRC="../icons/delete.gif" title="Delete Administrator" ALT="Delete Administrator"  ></a>
    </div>
</td>
<?php
echo "</tr>\n";
}
echo "</table><br>";
//Paging code below
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++    
user::displayPageNav($nrResults,$limit,$pageNum,$_SERVER['PHP_SELF']);

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
}

 //----------------------------------------------------------------------------------
 if(isset($_POST['addUser']))
 {  
    global $form;
		 $check=new checkFormat();
		 
		 $emailCheck=$check-> checkEmailFormat($_POST['email']);
		 $bemailCheck=$check-> checkEmailFormat($_POST['backupEmail']);
		 $phoneCheck=$check-> checkPhoneFormat($_POST['phone']);
		 $bphoneCheck=$check-> checkPhoneFormat($_POST['backupPhone']);
		 
		 
    $field = "firstName";  
    if(!isset($_POST['firstName']) || strlen(trim($_POST['firstName'])) == 0){
    $form->setError($field, "* Please enter a valid First Name.");
    }
	$field = "lastName";  
	if(!isset($_POST['lastName']) || strlen(trim($_POST['lastName'])) == 0){
    $form->setError($field, "* Please enter a valid Last Name.");
    }
	
    $field = "email";  
    if(!isset($_POST['email']) || strlen(trim($_POST['email'])) == 0 || !$emailCheck){
    $form->setError($field, "* Please enter a valid Email.");
    }
    $field = "phone";  
    if(!isset($_POST['phone']) || strlen(trim($_POST['phone'])) == 0 || !$phoneCheck){
    $form->setError($field, "* Please enter a valid Phone.");
    }
    $field = "password";  
    if(!isset($_POST['password']) || strlen(trim($_POST['password'])) == 0){
    $form->setError($field, "* Please enter a valid password.");
    }
    $field = "username";  
    if(!isset($_POST['username']) || strlen(trim($_POST['username'])) == 0){
    $form->setError($field, "* Please enter a valid User Name.");
    }
    /* Errors exist, have user correct them */
    if($form->num_errors > 0){
	
    $_SESSION['value_array'] = $_POST;
    $_SESSION['error_array'] = $form->getErrorArray();
    $page="add";
    }
    /* No errors, add the new application to the database table*/
    else
    {
       
       if(user::userEmailDuplicateCheck($_POST['email']))
       {
             $process_result="An Administrator with this Email already exists in the system.";
        
             
       }else if(user::userNameDuplicateCheck($_POST['username'])){
	   		 $process_result="An Administrator with this User Name already exists in the system.";
           
	   }
       else
	   //$firstName,$lastName,$adminComments, $email, $phone, $backupPhone, $backupEmail, $username, $password, $userLevel
       { 
           if(user::adduser($_POST['firstName'],$_POST['lastName'],$_POST['adminComments'],$_POST['email'],$_POST['phone'],$_POST['backupPhone'],$_POST['backupEmail'],$_POST['username'],$_POST['password'],"2"))
           {
                $process_result="Administrator added to the database successfully.";
                unset($_POST);
                $page="display";
           }    
            else
                $process_result="Failed to add the new Administrator to the database.";
				
       }

    }
 }

 //----------------------------------------------------------------------------------
 if(isset($_GET['delUserID']))
 {  
    if(user::queryUser($_GET['delUserID'])!=false)
    { 
        $deleteResult=user::deleteUser($_GET['delUserID']);
        
        if($deleteResult)
            $process_result="The selected Administrator has been removed successfully.";
        else
            $process_result="Failed to remove the Administrator from the database.";
    }

 }
  //----------------------------------------------------------------------------------

  
if(isset($_POST['editUser']))
 {  
 	
    global $form;
		 $check=new checkFormat();
         
         $emailCheck=$check-> checkEmailFormat($_POST['email']);
         $bemailCheck=$check-> checkEmailFormat($_POST['backupEmail']);
         $phoneCheck=$check-> checkPhoneFormat($_POST['phone']);
         $bphoneCheck=$check-> checkPhoneFormat($_POST['backupPhone']);
	
	
    $field = "firstName";
    if(!isset($_POST['firstName']) || strlen(trim($_POST['firstName'])) == 0 ){
    $form->setError($field, "* Please enter a valid First Name.");
    }
	$field = "lastName";  
	if(!isset($_POST['lastName']) || strlen(trim($_POST['lastName'])) == 0 ){
    $form->setError($field, "* Please enter a valid Last Name.");
    }
	
    $field = "email";  
    if(!isset($_POST['email']) || strlen(trim($_POST['email'])) == 0  || !$emailCheck){
    $form->setError($field, "* Please enter a valid Email.");
    }
    $field = "phone";  
    if(!isset($_POST['phone']) || strlen(trim($_POST['phone'])) == 0 || !$phoneCheck){
    $form->setError($field, "* Please enter a valid Phone.");
    }
 
    $field = "username";  
    if(!isset($_POST['username']) || strlen(trim($_POST['username'])) == 0){
    $form->setError($field, "* Please enter a valid User Name.");
    }
	
    /* Errors exist, have user correct them */
    if($form->num_errors > 0){
    $_SESSION['value_array'] = $_POST;
    $_SESSION['error_array'] = $form->getErrorArray();
	$page="edit";
    }
    /* No errors, update application information in the database*/
    else
    {
 
       if($_POST['email']!=$_POST['originEmail'] && user::userEmailDuplicateCheck($_POST['email']))
       {
             $process_result="An Administrator with this Email already exists in the system.";
			 $page="edit";
       }
	   else if($_POST['username']!=$_POST['originUsername'] && user::userNameDuplicateCheck($_POST['username'])){
	   		 $process_result="An Administrator with this User Name already exists in the system.";
			 $page="edit";
	   }
       else
       { 
           if(user::updateUser($_POST['adminID'],$_POST['firstName'],$_POST['lastName'],$_POST['adminComments'],$_POST['email'],$_POST['phone'],$_POST['backupPhone'],$_POST['backupEmail'],$_POST['username'],$_POST['password']))
           {
                $process_result=$_POST['firstName']."  ".$_POST['lastName'].": Administrator updated successfully. <a href='user.php'>Go Back</a>...";
                $page="edit"; 
           }    
            else{
                $process_result="Failed to update.".$_POST['firstName']."  ".$_POST['lastName'];
				}
				
       }

    }
 }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="../style.css"/>       


</head>
<body >

<div id="layout">
	<?php require('../include/header.php');   ?>
        
<?php    switch ($page){ //what page to load
		case 'display':  //begin display page?>
			<div id="displayApp" class="in"> 
            <P class=veryLargeBlue>Manage Admins</P>
				<?php if(isset($process_result)) {echo $process_result;}
				displayUsers();
				?>
			</div>
		<?php
		break; //end display page
		
		case 'edit': //begin edit page?>
            <div id="panel-left" class="box panel">
            <P class=veryLargeBlue>Update Administrator</P> 
                <?php
                if(isset($_REQUEST['adminID']))
                {
                $userInfo=user::queryUser($_REQUEST['adminID']);
				
                ?>
                <div id="editUser">
                    <?php if(isset($process_result)) {echo $process_result;}?>
                    <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
                        <table align="left" border="0" cellspacing="0" cellpadding="3">

                        <tr>
                            <td class="c1">First Name:*</td>
                            <td class="c1"><input type="text"  name="firstName" maxlength="100" size="40" value="<?php if(isset($_POST['firstName'])) echo $_POST['firstName']; else echo $userInfo->firstName; ?>" />
							<?php echo $form->error("firstName"); ?></td>
							
                            
                        </tr>
                        <tr>
                            <td class="c1">Last Name:*</td>
                            <td class="c1"><input type="text"  name="lastName" maxlength="100" size="40" value="<?php if(isset($_POST['lastName'])) echo $_POST['lastName']; else echo $userInfo->lastName; ?>" />
							<?php echo $form->error("lastName"); ?></td>
                            
                        </tr>
                         <tr>
                            <td class="c2">Email:*</td>
                            <td class="c2"><input type="text"  name="email" maxlength="100" size="40" value="<?php if(isset($_POST['email'])) echo $_POST['email']; else echo $userInfo->email; ?>">
                            <?php echo $form->error("email"); ?></td>
							<input type="hidden" name="originEmail" value="<?php echo $userInfo->email; ?>" />
                        </tr>

                        <tr>
                            <td class="c2">Backup Email:</td>
                            <td class="c2"><input type="text"  name="backupEmail" maxlength="100" size="40" value="<?php if(isset($_POST['backupEmail'])) echo $_POST['backupEmail']; else echo $userInfo->backupEmail; ?>">
                            <?php echo $form->error("backupEmail"); ?></td>
                        </tr>
                        <tr>
                            <td class="c2">Phone:*</td>
                            <td class="c2"><input type="text"  name="phone" maxlength="100" size="40" value="<?php if(isset($_POST['phone'])) echo $_POST['phone']; else echo $userInfo->phone; ?>">
                            <?php echo $form->error("phone"); ?></td>
                        </tr>
                        <tr>
                            <td class="c2">Backup Phone:</td>
                            <td class="c2"><input type="text"  name="backupPhone" maxlength="100" size="40" value="<?php if(isset($_POST['backupPhone'])) echo $_POST['backupPhone']; else echo $userInfo->backupPhone; ?>">
                            <?php echo $form->error("backupPhone"); ?></td>
                        </tr>
                        <tr>
                            <td class="c2">User Name:*</td>
                            <td class="c2"><input type="text"  name="username" maxlength="100" size="40" value="<?php if(isset($_POST['username'])) echo $_POST['username']; else echo $userInfo->username; ?>">
                            <?php echo $form->error("username"); ?></td>
                            <input type="hidden" name="originUsername" value="<?php echo $userInfo->username; ?>" /> 
                        </tr>
                        <tr>
                            <td class="c2">Password: (Optional)</td>
                            <td class="c2"><input type="password"  name="password" maxlength="100" size="40"><br>
                            Adding new password will replace old password.
                            <?php echo $form->error("password"); ?></td>
                        </tr>
                         <tr>
                            <td class="c1">Comment:</td>
                            <td class="c1"><textarea  name="adminComments" cols="70" rows="6" ><?php if(isset($_POST['adminComments'])) echo $_POST['adminComments']; else echo $userInfo->adminComments; ?></textarea>
                            </td>
                        </tr>
						<input type="hidden" name="editUser" value="1">
                        <input type="hidden" name="adminID" value="<?php echo $userInfo->adminID; ?>">
                        <tr>
                            <td></td>
                            <td>
                                <div style="float:left;"><button name="editApp" type="submit" >Update Administrator</button></div> 
                               
                                <div style="float:left;"><button type="button" onClick="window.location='user.php'">Cancel</button>
                            </td>
                        </tr>
   
                        </table>
                        </form>
           <?php } ?>
                </div>
            </div>
		<?php break; //end edit page
		case 'add':  //begin add page ?>
                <!--Add New Admin form START-->
                <div id="panel-left" class="box panel">
                <P class=veryLargeBlue>Add Administrator</P> 
                <div enctype="multipart/form-data" id="addApp">
                <?php if(isset($process_result)) {echo $process_result;} ?>
                
                
                    <form name="addForm" action="<?php $_SERVER['PHP_SELF'];?>" method="POST">
                    <table align="left" border="0" > 
                        <tr>
                            <td class="c1">First Name:*</td>
                            <td class="c1"><input type="text"  name="firstName" maxlength="100" size="40" value="<?php if(isset($_POST['firstName'])) echo $_POST['firstName'];?>" />
							<?php echo $form->error("firstName"); ?></td>
                            
                        </tr>
                        <tr>
                            <td class="c1">Last Name:*</td>
                            <td class="c1"><input type="text"  name="lastName" maxlength="100" size="40" value="<?php if(isset($_POST['lastName'])) echo $_POST['lastName'];?>" />
							<?php echo $form->error("lastName"); ?></td>
                
                         <tr>
                            <td class="c2">Email:*</td>
                            <td class="c2"><input type="text"  name="email" maxlength="100" size="40" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>">
                            <?php echo $form->error("email"); ?></td>
                        </tr>

                        <tr>
                            <td class="c2">Backup Email:</td>
                            <td class="c2"><input type="text"  name="backupEmail" maxlength="100" size="40" value="<?php if(isset($_POST['backupEmail'])) echo $_POST['backupEmail'];?>">
                            <?php echo $form->error("backupEmail"); ?></td>
                        </tr>
                        <tr>
                            <td class="c2">Phone:*</td>
                            <td class="c2"><input type="text"  name="phone" maxlength="100" size="40" value="<?php if(isset($_POST['phone'])) echo $_POST['phone'];?>">
                            <?php echo $form->error("phone"); ?></td>
                        </tr>
                        <tr>
                            <td class="c2">Backup Phone:</td>
                            <td class="c2"><input type="text"  name="backupPhone" maxlength="100" size="40" value="<?php if(isset($_POST['backupPhone'])) echo $_POST['backupPhone'];?>">
                            <?php echo $form->error("backupPhone"); ?></td>
                        </tr>
                        <tr>
                            <td class="c2">User Name:*</td>
                            <td class="c2"><input type="text"  name="username" maxlength="100" size="40" value="<?php if(isset($_POST['username'])) echo $_POST['username'];?>">
                            <?php echo $form->error("usename"); ?></td>
                        </tr>
                        <tr>
                            <td class="c2">Password:*</td>
                            <td class="c2"><input type="password"  name="password" maxlength="100" size="40">
                            <?php echo $form->error("password"); ?></td>
                        </tr>
                         <tr>
                            <td class="c1">Comment:</td>
                            <td class="c1"><textarea  name="adminComments" cols="70" rows="6" ><?php if(isset($_POST['adminComments'])) echo $_POST['adminComments'];?></textarea>
                            </td>
                        </tr>
						<input type="hidden" name="addUser" value="1">
                        
                        
                        <tr>
                            <td></td>
                            <td>
                                <div style="float:left;"><button name="addApp" type="submit" >Add Administrator</button></div> 
                                
                                <div style="float:left;"><button type="button" onClick="window.location='user.php'">Cancel</button></div>
                            </td>
                        </tr>
                        </table> 
                       </form>  
            </div>
        </div>
        <!--Add New App form END-->
	<?php break; } //end add page and end switch page?> 
<?php require('../include/footer.php'); ?>
</div>
<?php
 unset($_SESSION['error_array']); 
?>
</body>
</html>
