<?php
include("../include/session.php");  
include("account.class.php");
require_once("../include/checkFormat.php"); 
require_once("../include/accessCheck.php");

if (isset($_GET['page'])){ //find out what the sub page is
	$page = $_GET['page'];
	}
else{$page = 'edit';}
 
 //----------------------------------------------------------------------------------

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 = "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.");
    }
	
    /* 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'] && account::userEmailDuplicateCheck($_POST['email']))
       {
             $process_result="This email belongs to another user in the system. Please correct it";
			 $page="edit";
	   }
       else
       { 
           if(account::updateUser($_POST['adminID'],$_POST['email'],$_POST['phone'],$_POST['backupPhone'],$_POST['backupEmail']))
           {
                $process_result="Account informaiton updated successfully.";
                $page="edit"; 
           }    
            else{
                $process_result="Failed to update account information";
				}
				
       }

    }
 }
if(isset($_POST['changePass']))
 {  
     
    global $form;
         $check=new checkFormat();
         
         $passCheck=$check-> checkPasswordFormat($_POST['password']); 
         
    $field = "password";
    if(!isset($_POST['password']) || strlen(trim($_POST['password'])) == 0 || !$passCheck ){
    $form->setError($field, "* Please enter a valid password.");
    }
    $field = "confirm_password";
    
    if(!isset($_POST['confirm_password']) || strlen(trim($_POST['confirm_password'])) == 0 ){
    $form->setError($field, "* Please enter a valid password.");
    }
    
    if(strcmp($_POST['confirm_password'],$_POST['password']) !=0 ){
    $form->setError($field, "* The two passwords entered don't match.");
     $process_result =   "The two passwords entered don't match. Please try again";
    }
    
    /* Errors exist, have user correct them */
    if($form->num_errors > 0){
    $_SESSION['value_array'] = $_POST;
    $_SESSION['error_array'] = $form->getErrorArray();
    $page="changePass";
    }
    /* No errors, update application information in the database*/
    else
    {
  
           if(account::updateUserPassword($_POST['adminID'],$_POST['password']))
           {
                $process_result="Password changed successfully.";
                $page="changePass"; 
           }    
            else{
                $process_result="Failed to update password.";
                $page="changePass"; 
                }
                
  

    }
 }
?>
<!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 'edit': //begin edit page?>
            <div id="panel-left" class="box panel">
            <P class=veryLargeBlue>Update Account Information</P> 
                <?php
                if(isset($session->userinfo['adminID']))
                {
                $userInfo=account::queryUser($session->userinfo['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="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="c1">Backup Email:</td>
                            <td class="c1"><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="c1">Backup Phone:</td>
                            <td class="c1"><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>

						<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="updateAccount" type="submit" >Update Account</button></div> 

                            </td>
                        </tr>
   
                        </table>
                        </form>
           <?php } ?>
                </div>
            </div>
		<?php break; //end edit page
case 'changePass': //begin change password page?>
            <div id="panel-left" class="box panel">
            <P class=veryLargeBlue>Change Password</P> 
                <?php
                if(isset($session->userinfo['adminID']))
                {
                $userInfo=account::queryUser($session->userinfo['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="c2">Password Requirements:</td>
                            <td class="c2">
                                    <ul>
                                        <li>Password can only contain alphabets (a - z or A - Z) and numbers</li>
                                        <li>Minimum password length is 6 characters</li>
                                        <li>Maximum password length is 100 characters</li>
                                    </ul>
                            </td>
                        </tr>
                        <tr>
                            <td class="c1">New Password:</td>
                            <td class="c1"><input type="password"  name="password" maxlength="100" size="40">
                            <?php echo $form->error("password"); ?></td>
                        </tr>
                               <tr>
                            <td class="c1">Confirm Password:</td>
                            <td class="c1"><input type="password"  name="confirm_password" maxlength="100" size="40">
                            <?php echo $form->error("confirm_password"); ?></td>
                        </tr>
                        
                        <input type="hidden" name="changePass" value="1">
                        <input type="hidden" name="adminID" value="<?php echo $userInfo->adminID; ?>">
                        <tr>
                            <td></td>
                            <td>
                                <div style="float:left;"><button name="changePassword" type="submit" >Change Password</button></div> 
                               
                                <div style="float:left;"><button type="button" onClick="window.location='account.php'">Cancel</button>
                            </td>
                        </tr>
   
                        </table>
                        </form>
           <?php } ?>
                </div>
            </div>

	<?php break; } //end password change page and end switch page ?> 
<?php require('../include/footer.php'); ?>
</div>
<?php
 unset($_SESSION['error_array']); 
?>
</body>
</html>
