<?php
session_start();
require_once "main.php";
Logger::configure('log4php.xml');
$log = Logger::getLogger('userinfo');
global $db, $aclevel;
if ($_SESSION['access']<2){
	if ($_GET['submitType']=='cust') {
	$result = bill_get_user($_SESSION['comnum'], $_GET['id'])
?>
	<script>
		function setType(type) {
			//formName is the name of your form
			document.accinform.submitType.value = type;
		}
	</script>
	<form name="accinform" action="#" onsubmit="jQuery.ajax({url:'includes/userinfo.php', data: jQuery(this).serialize(), cache: false, success: function(data){jQuery('#setinfo').html(data);}});return false;">
	    <table border="1" cellpadding="2" cellspacing="0">
	        <tr>
	            <td><label for="fname">First Name</label></td>
	            <td><input type="text" name="fname" id="fname" value="<?php echo $result[0]['fname']?>"></td>
	        </tr>
	        <tr>
	            <td><label for="lname">Last Name</label></td>
	            <td><input type="text" name="lname" id="lname" value="<?php echo $result[0]['lname']?>"></td>
	        </tr>
	        <tr>
	            <td><label for="uname">User Name*</label></td>
	            <td><input type="text" name="uname" id="uname" value="<?php echo $result[0]['uname']?>" readonly="readonly"></td>
	        </tr>
	        <tr>
	        	<td>
	        		<input type="hidden" name="comp" id="comp" value="<?php echo $result[0]['company']?>">
	    			<label for="access">Access Level</label>
				        <button title="Access Levels?"  onClick="alert(
 'Administrator has full access to all features available on the account.\n\
Manager has access to everything but company wide settings and other user settings.\n\
Clerk has access to everything but any type of modification of settings including \n\
	rate codes, paymnet and charge types etc. Can enter payments or credits and \n\
	readings and can print reports.\n\
Info Only level only has view access to the customer information and can print the \n\
	customer detail and audit reports.  All other reports require at least a Clerk and \n\
	accepting any type of payment also requires a Clerk level.');return false;">?</button>
	            </td>
	            <td>
	    			<select name="access" id="access" tabindex=24>
<?php
					if ($result[0]['access'] == 4) {
						echo '<option value="2">Manager</option>';
						echo '<option value="3">Clerk</option>';
						echo '<option value="4">Info Only</option>';
						echo '<option selected="selected" value="4">Info Only</option>';
					} else if ($result[0]['access'] == 2) {
						echo '<option value="1">Administrator</option>';
						echo '<option value="3">Clerk</option>';
						echo '<option value="4">Info Only</option>';
						echo '<option selected="selected" value="2">Manager</option>';
					} else if ($result[0]['access'] == 3) {
						echo '<option value="1">Administrator</option>';
						echo '<option value="2">Manager</option>';
						echo '<option value="4">Info Only</option>';
						echo '<option selected="selected" value="3">Clerk</option>';
					} else {
						echo '<option value="2">Manager</option>';
						echo '<option value="3">Clerk</option>';
						echo '<option value="4">Info Only</option>';
						echo '<option selected="selected" value="1">Administrator</option>';
					}
?>
    				</select>                   
	             </td>
	        </tr>
	        <tr>
	            <td><label for="active">User Active</label></td>
	            <td>
	    <select name="active" id="active">
	<?php
		if ($result[0]['active'] == 1) {
			echo '<option value="0">Not Active</option>';
			echo '<option selected="selected" value="1">Active</option>';
		} else {
			echo '<option selected="selected" value="0">Not Active</option>';
			echo '<option value="1">Active</option>';
		}
	        ?>
	    </select>
	            </td>
	        </tr>
	        <tr>
	            <td><label for="fsize">Font Size</label></td>
	            <td><input type="text" name="fsize" id="fsize" value="<?php echo $result[0]['fontsize']?>"></td>
	        </tr>
	        <tr>
	            <td><label for="email">Email*</label></td>
	            <td><input type="text" name="email" id="email" value="<?php echo $result[0]['email']?>" readonly="readonly"></td>
	        </tr>
	        <tr>
	            <td>
			    <input type="hidden" name="submitType">
			    <input type="hidden" name="id" id="id" value="<?php echo $result[0]['id']?>">
			    <input type="submit" title="Any change made above will be updated by pressing this button." onclick="setType('uUpdate')" value="Update User"/>
	            </td>
	            <td><input type="submit" title="Click here to change password or email." onclick="setType('xUpdate')" value="Change Password or Email"/></td>
	        </tr>
    	    </table>
    	    <p>* May not be edited on this screen!</p>
	</form>
<?php
	} else if ($_GET['submitType']=='newuser') {
?>
		<script>
			function setType(type) {
				//formName is the name of your form
				document.accinform.submitType.value = type;
			}
		</script>
		<p>Add a new user:</p>
		<form name="accinform" action="#" onsubmit="jQuery.ajax({url:'includes/userinfo.php', data: jQuery(this).serialize(), cache: false, success: function(data){jQuery('#setinfo').html(data);}});return false;">
		    <table border="1" cellpadding="2" cellspacing="0">
		        <tr>
		            <td><label for="fname">First Name</label></td>
		            <td><input type="text" name="fname" id="fname"></td>
		        </tr>
		        <tr>
		            <td><label for="lname">Last Name</label></td>
		            <td><input type="text" name="lname" id="lname"></td>
		        </tr>
		        <tr>
		            <td><label for="uname">User Name</label></td>
		            <td><input type="text" name="uname" id="uname"></td>
		        </tr>
		        <tr>
		            <td> 
		    		<input type="hidden" name="comp" id="comp" value="<?php echo $_SESSION['comnum']?>">
		    		<label for="access">Access Level</label>
    				<button title="Access Levels?" onClick="alert(
'Administrator has full access to all features available on the account.\n\
Manager has access to everything but company wide settings and other user settings.\n\
Clerk has access to everything but any type of modification of settings including \n\
	rate codes, payment and charge types etc. Can enter payments or credits and \n\
	readings and can print reports.\n\
Info Only level only has view access to the customer information and can print the \n\
	customer detail and audit reports.  All other reports require at least a Clerk and \n\
	accepting any type of payment also requires a Clerk level.');return false;">?</button>
		            </td>
		            <td>
				    <select name="access" id="access" tabindex=24>
					       <option value="2">Manager</option>
					       <option value="3">Clerk</option>
					       <option value="4">Info Only</option>
					       <option selected="selected" value="1">Administrator</option>
				    </select>
			    </td>
			</tr>
			<tr>
			     <td><label for="active">Active</label></td>
			     <td>
				    <select name="active" id="active" tabindex=25>
					     <option value="0">Not Active</option>
					     <option selected="selected" value="1">Active</option>
				    </select>
			      </td>
			  </tr>
			  <tr>
			  	<td><label for="fsize">Font Size in %</label></td>
			        <td><input title="If you pick a lower percentage your font on your screen will shrink.  Requires a logout and login after a change" type="text" name="fsize" id="fsize" value="95"></td>
			  </tr>
			  <tr>
			        <td>
					    <input type="hidden" name="submitType">
					    <input type="submit" title="Any change made above will be updated by pressing this button." onclick="setType('nUpdate')" value="Add this User"/>
			       </td>
			       <td>
			 </tr>
    		   </table>
		</form>
<?php
	} else if ($_GET['submitType']=='xUpdate') {
?>
		<script>
			function setType(type) {
				//formName is the name of your form
				document.accinform.submitType.value = type;
			}
		</script>
		<p><?php echo $_GET['uname']?></p>
		<form name="accinform" action="#" onsubmit="jQuery.ajax({url:'includes/userinfo.php', data: jQuery(this).serialize(), cache: false, success: function(data){jQuery('#setinfo').html(data);}});return false;">
		    <table border="1" cellpadding="2" cellspacing="0">
                        <tr>
                            <td><label for="pwd1">Password</label></td>
                            <td><input type="password" name="pwd1" id="pwd1"></td>
                        </tr>
                        <tr>
                            <td><label for="pactive">Reenter Password</label></td>
                            <td><input type="password" name="pactive" id="pactive"></td>
                        </tr>
                        <tr>
                            <td><label for="email">Email</label></td>
                            <td><input type="text" name="email" id="email" value="<?php echo $_GET['email']?>"></td>
                        </tr>
                        <tr>
                            <td><input type="hidden" name="submitType">
		    <input type="hidden" name="id" id="id" value="<?php echo $_GET['id']?>">
		    <input type="submit" title="Any change made above will be updated by pressing this button." onclick="setType('zUpdate')" value="Update User"/></td><td>
		    </tr></table>
		</form>
<?php
	} else if ($_GET['submitType']=='uUpdate') {
		if ($ddemo==1){
                    $test = bill_update_userd($_GET['fname'], $_GET['lname'], $_GET['fsize'], $_GET['id']);
?>
		<script>alert('Demo mode does not allow changes on access level, active, email, or password.');</script>
<?php
		} else {
			
		$test = bill_update_user($_GET['fname'], $_GET['lname'], $_GET['access'],
                        $_GET['active'], $_GET['fsize'], $_GET['id']);
                }
		$log->info($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum'].' = '.$_GET['id']);
?>
                <script>dynamic_Select('includes/userinfo.php', '#setinfo','list', <?php echo $_GET['cid']; ?>,<?php echo $_SESSION['comnum']?>);</script>
<?php
	} else if ($_GET['submitType']=='list') {

		$result = bill_get_users($_SESSION['comnum']);
                
?>
		<p>Please click on the User Name to edit or change the user.</p>
		<table border="1" cellpadding="2" cellspacing="0">
                    <tr>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>User Name</th>
                        <th>Email</th>
                        <th>Access Level
                        <button title="Access Levels?" onClick="alert(
 'Administrator has full access to all features available on the account.\n\
Manager has access to everything but company wide settings and other user settings.\n\
Clerk has access to everything but any type of modification of settings including \n\
	rate codes, paymnet and charge types etc. Can enter payments or credits and \n\
	readings and can print reports.\n\
Info Only level only has view access to the customer information and can print the \n\
	customer detail and audit reports.  All other reports require at least a Clerk and \n\
	accepting any type of payment also requires a Clerk level.');return false;">?</button>
                        </th>
                        <th>Active</th>
                        <th>Font Size</th>
                    </tr>
                    
<?php		
                extract($result);
		for ($i=0;$i<count($result);$i++) {
			$res1 = $result[$i];
			echo '<tr><td>' . $res1['fname'].'</td><td>'.$res1['lname'].'</td><td>';
?>
                        <a href="#" style="font-size:18px" onclick="dynamic_Select('includes/userinfo.php',
                            '#setinfo','cust', <?php echo $res1['id'];
                            ?>);return false;"><font color="blue"><?php echo $res1['uname']; ?></font></a>
<?php
			echo '<td>' . $res1['email'] . '</td><td>';
			if ($result[0]['access'] == 2) {
				echo '<font color="yellow"><b>Manager</b></font>';
			}
			if ($result[0]['access'] == 3) {
				echo '<font color="blue"><b>Clerk</b></font>';
			}
			if ($result[0]['access'] == 4) {
				echo '<font color="green"><b>Info Only</b></font>';
			} else {
				echo '<font color="red"><b>Administrator</b></font>';
			}
			echo '</td><td>';
			if ($result[0]['active'] == 1) {
				echo '<font color="green">Active</b></font>';
			} else {
				echo '<font color="red"><b>Not Active</b></font>';
			}
			echo '</td><td>' . $res1['fontsize'] . '</td></tr>';
			}
			echo '</table>';
	} else if ($_GET['submitType']=='zUpdate') {
	if (!$ddemo==1){
		$result = bill_get_user($_SESSION['comnum'], $_GET['id']);
		if (!($_GET['pwd1']==NULL || $_GET['pwd1']=='')){
			if ($_GET['pwd1']==$_GET['pactive']){
				$test = bill_update_pwd($_GET['id'], $_GET['pwd1']);
			} else {
				echo '<script type=text/javascript>alert("The passwords do not match. '.$_GET['pwd1'].'-'.$_GET['pactive'].' Please try again!");</script>';
			}
		}
		if (!($_GET['email']==NULL || $_GET['email']=='')){

			$result = bill_check_email($_GET['email']);
			if (!$result>0){

				$test = bill_update_email($_GET['id'], $_GET['email']);
			} else {
				echo '<script type=text/javascript>alert("The email address is already in use!");</script>';
			}
		}
		$log->info($_GET['submitType'].' - pwdxemail = '.$_SESSION['user'].' - '.$_SESSION['comnum'].' = '.$_GET['id']);

		$result = bill_get_users($_SESSION['comnum']);
                
?>
		<p>Please click on the User Name to edit or change the user.</p>
		<table border="1" cellpadding="2" cellspacing="0">
                    <tr>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>User Name</th>
                        <th>Email</th>
                        <th>Access Level
                        <button title="Access Levels?" onClick="alert(
 'Administrator has full access to all features available on the account.\n\
Manager has access to everything but company wide settings and other user settings.\n\
Clerk has access to everything but any type of modification of settings including \n\
	rate codes, paymnet and charge types etc. Can enter payments or credits and \n\
	readings and can print reports.\n\
Info Only level only has view access to the customer information and can print the \n\
	customer detail and audit reports.  All other reports require at least a Clerk and \n\
	accepting any type of payment also requires a Clerk level.');return false;">?</button>
                        </th>
                        <th>Active</th>
                        <th>Font Size</th>
                    </tr>
                    
<?php		
                extract($result);
		for ($i=0;$i<count($result);$i++) {
			$res1 = $result[$i];
			echo '<tr><td>'.$res1['fname'].'</td><td>'.$res1['lname'].'</td><td>';
?>
			<a href="#" style="font-size:18px" onclick="dynamic_Select('includes/userinfo.php',
                            '#setinfo','cust', <?php echo $res1['id']; ?>,<?php echo $_SESSION['comnum'];
                            ?>);return false;"><font color="blue"><?php echo $res1['uname']; ?></font></a>
<?php 		
			echo $res1['uname'] . "</button>";
			echo '<td>' . $res1['email'] . '</td><td>';
			if ($result[0]['access'] == 2) {
				echo '<font color="yellow"><b>Manager</b></font>';
			}
			if ($result[0]['access'] == 3) {
				echo '<font color="blue"><b>Clerk</b></font>';
			}
			if ($result[0]['access'] == 4) {
				echo '<font color="green"><b>Info Only</b></font>';
			} else {
				echo '<font color="red"><b>Administrator</b></font>';
			}
			echo '</td><td>';
			if ($result[0]['active'] == 1) {
				echo '<font color="green">Active</b></font>';
			} else {
				echo '<font color="red"><b>Not Active</b></font>';
			}
			echo '</td><td>' . $res1['fsize'] . '</td></tr>';
		} //end for loop
			echo '</table>';
?>
		<button type="button" value="axrates" onClick="dynamic_Select('includes/userinfo.php', '#setinfo','newuser', <?php echo $res1['id']; ?>,<?php echo $_SESSION['comnum']; ?>)">Add User</button>
<?php
	} else {
?>
		<script>alert('Demo mode does not allow changes on access level, active, email, or password.');dynamic_Select('includes/userinfo.php', '#setinfo','list', <?php echo $_GET['cid']; ?>,<?php echo $_SESSION['comnum']?>);</script>
<?php
}
	} else if ($_GET['submitType']=='nUpdate') {
		$result = bill_check_uname($_GET['uname']);
		if (!$result>0){

			$test = bill_insert_user($_GET['fname'], $_GET['lname'], $_GET['uname'], $_SESSION['comnum'],
                                $_GET['access'], $_GET['active'], $_GET['fsize']);
		} else {
			echo '<script type=text/javascript>alert("The user name is already in use, please try again!");</script>';
		}
		$log->info($_GET['submitType'].' - nuser = '.$_SESSION['user'].' - '.$_SESSION['comnum']);
		//print_r($_SESSION);

		$result = bill_get_users($_SESSION['comnum']);
                
?>
		<p>Please click on the User Name to edit or change the user.</p>
		<table border="1" cellpadding="2" cellspacing="0">
                    <tr>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>User Name</th>
                        <th>Email</th>
                        <th>Access Level
                        <button title="Access Levels?" onClick="alert(
 'Administrator has full access to all features available on the account.\n\
Manager has access to everything but company wide settings and other user settings.\n\
Clerk has access to everything but any type of modification of settings including \n\
	rate codes, paymnet and charge types etc. Can enter payments or credits and \n\
	readings and can print reports.\n\
Info Only level only has view access to the customer information and can print the \n\
	customer detail and audit reports.  All other reports require at least a Clerk and \n\
	accepting any type of payment also requires a Clerk level.');return false;">?</button>
                        </th>
                        <th>Active</th>
                        <th>Font Size</th>
                    </tr>
                    
<?php		
                extract($result);
		for ($i=0;$i<count($result);$i++) {
			$res1 = $result[$i];
			echo '<tr><td>'.$res1['fname'].'</td><td>'.$res1['lname'].'</td><td>';
?>
			<a href="#" style="font-size:18px" onclick="dynamic_Select('includes/userinfo.php',
                            '#setinfo','cust', <?php echo $res1['id']; ?>,<?php echo $_SESSION['comnum'];
                            ?>);return false;"><font color="blue"><?php echo $res1['uname']; ?></font></a>
<?php 
			echo '<td>' . $res1['email'] . '</td><td>';
			if ($result[0]['access'] == 2) {
				echo '<font color="yellow"><b>Manager</b></font>';
			}
			if ($result[0]['access'] == 3) {
				echo '<font color="blue"><b>Clerk</b></font>';
			}
			if ($result[0]['access'] == 4) {
				echo '<font color="green"><b>Info Only</b></font>';
			} else {
				echo '<font color="red"><b>Administrator</b></font>';
			}
			echo '</td><td>';
			if ($result[0]['active'] == 1) {
				echo '<font color="green">Active</b></font>';
			} else {
				echo '<font color="red"><b>Not Active</b></font>';
			}
			echo '</td><td>' . $res1['fontsize'] . '</td></tr>';
		} //end for loop
		echo '</table>';
		}
?>
		<button type="button" value="axrates" onClick="dynamic_Select('includes/userinfo.php','#setinfo','newuser', <?php echo $res1['id']; ?>)">Add User</button>
<?php
} else {
?>
	<script>
		alert('You do not have a high enough access level to post payments\n\
			 or charges!\n\
			 Please see your administrator.');
	</script>
<?php
}
?>