<?php session_start();
		if (isset($_SESSION['logged']) && $_SESSION['logged'] == 'yes'){
			include ('scripts/classes.php'); if ($_SESSION['lang']=="EN"){$lang="EN"; include ('lang/en.php');}elseif ($_SESSION['lang']=="GR"){$lang="GR"; include ('lang/gr.php');}else {$lang="EN";include ('lang/en.php');}
			$global = getGlobalSettings(); foreach ($global as $glo_settings){extract($glo_settings);}		
			$mods = getCLSModules('POS'); foreach ($mods as $modules){extract($modules);} if ($mod_name == true) $_POS = 1; //echo $_POS;	
			$page_title = $lbl_page_title_user_add; //Assign Page Title
			$mode = $lbl_mode_add_users; //Assign Mode
			
			if (isset($_POST['but_submit'])){
    			//$ID = $_POST['id'];
    			$NAME = $_POST['f1name'];
    			$USERNAME = $_POST['f1username'];
    			$EMAIL = $_POST['f1email'];
    			$PASSWORD = $_POST['f1password'];
    			$PASSWORD1 = $_POST['f1password1'];
    			$USERTYPE = $_POST['f1usertype'];
                $BRANCH = $_POST['f1branch'];
    			$REGISTERDATE = $_POST['f1registerdate'];	
    			$DEFAULT_PRINTER = $_POST['f1defaultprinter'];     
    			mysqli_query($mysqli, "SET NAMES 'utf8' " );
    			## CHECK FOR EXISTING USER
                $sql_15 = "SELECT username FROM users WHERE username = '$USERNAME'";
                $result_15 = mysqli_query($mysqli, $sql_15) or die(mysqli_error($mysqli));
                if (mysqli_num_rows($result_15) == 0){
                //check for field values
    			mysqli_query ($mysqli, "INSERT INTO users (id , name , username , email , password , usertype, registerDate, active, default_printer, branch_id) VALUES (NULL , '$NAME', '$USERNAME', '$EMAIL', PASSWORD('$PASSWORD'), '$USERTYPE', NOW(), '1', '$DEFAULT_PRINTER', '$BRANCH')") or die(mysqli_error($mysqli));
    			mysqli_close($mysqli); 
				?>
				<script type="text/javascript">window.location = "users.php"</script>
            <?php }else{ ?>
            	<script type="text/javascript">
					alert('<?php echo _USER_ALREADY_EXISTS; ?>');
                    window.location = "users.php"
				</script>
            <?php  } ?>
		<?php }else{
			
?>	
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $page_title ?></title>
<link rel="shortcut icon" href="img/favicon.png" />
<link href="cms1.css" rel="stylesheet" type="text/css" /> 
<script src="scripts/gen_validatorv4.js" type="text/javascript"></script>
</head>
<body>
<script>top.document.title=self.document.title;</script>
	<div>
		<fieldset>
		<legend><?php echo $mode ?></legend>
				<div class="critical" id="adduser_errorloc">
				</div>
			<div class="align-left">
   			<form name="adduser" id="adduser" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
				<table class="align-left">
				<tr><td class="align-left"><label class="labels"><?php echo $lbl_grid_username ?></label></td>
				<td class="align-left"><input name="f1username" type="text"  class="addform"/></td>
				<td class="align-left" colspan="2" class="login2"><?php echo $lbl_message_5_chars ?></td></tr>
				<tr> <td class="align-left"><label class="labels"><?php echo $lbl_grid_user_name ?></label></td>
				<td class="align-left"><input name="f1name" class="addform" /></td></tr>
				<tr><td class="align-left"><label class="labels"><?php echo $lbl_grid_user_email ?></label></td>
				<td class="align-left"><input name="f1email" type="text"  class="addform" /></td></tr>
				<tr><td class="align-left"><label class="labels"><?php echo $lbl_grid_user_password ?></label></td>
				<td class="align-left"><input name="f1password" type="password"  class="addform" /></td>
				<td class="align-left"><label class="labels"><?php echo $lbl_grid_user_password_confirm ?></label></td>
				<td class="align-left"><input name="f1password1" type="password"  class="addform" /></td>
				<td class="login2 align-left"><?php echo $lbl_message_5_chars ?></td>
				</tr>
				<tr><td class="align-left"><label class="labels"><?php echo $lbl_grid_usertype ?></label></td>
				<td class="align-left">
						<select name="f1usertype" class="addform_drop">
						<?php 
						//QUERY TO DATABASE - TABLE USERTYPES TP POPULATE DROPDOWN MENU
						$sql="SELECT * FROM usertypes WHERE mask<>0 ORDER BY mask ASC";
						$result=mysqli_query($mysqli, $sql) or die("Query failed : " . mysqli_error($mysqli));;
                            while($row = mysqli_fetch_assoc($result)){
	                           echo "<option value=\"{$row['id']}\">{$row['name']}</option>"; 
                            }
						?>
						 </select>
				</td></tr>
<?php if ($_POS ==1 and $print_receipt == 1){ //Enable POS module if installed ?>
				
				<tr><td class="align-left"><label class="labels"><?php echo $lbl_grid_default_printer ?></label></td>
				<td class="align-left">
    				<select name="f1defaultprinter" class="addform_drop">
        				<?php 
        				//QUERY TO DATABASE - TABLE CORE_PRINTERS TP POPULATE DROPDOWN MENU
        				$sql="SELECT * FROM core_printers WHERE active = 1 ORDER BY id ASC";
        				$result = mysqli_query($mysqli, $sql) or die("Query failed : " . mysqli_error($mysqli));
                            while($row = mysqli_fetch_assoc($result)){
        				echo "<option value=\"{$row['id']}\">{$row['alias']}</option>"; }
        				?>
    				</select>
				</td></tr>		
<?php } ?>	
				<tr><td class="align-left"><label class="labels"><?php echo _BRANCH ?></label></td>
				<td class="align-left">
        			<select name="f1branch" class="addform_drop">
            			<?php 
            			//QUERY TO DATABASE - TABLE CORE_PRINTERS TP POPULATE DROPDOWN MENU
            			$sql="SELECT * FROM cls_branches ORDER BY id ASC";
            			$result = mysqli_query($mysqli, $sql) or die("Query failed : " . mysqli_error($mysqli));
                            while($row=mysqli_fetch_assoc($result)){
                                echo "<option value=\"{$row['id']}\">{$row['branch_name']}</option>"; 
                            }
            			?>
        			</select>
				</td></tr>				
						<tr><td><br /></td></tr>
						<tr><td colspan="5" class="align-left">
						<input name="but_submit" type="submit" class="button" value="<?php echo $lbl_button_save ?>" />
						<input name="but_back" type="button" class="button" value="<?php echo $lbl_button_back ?>" onclick="window.location='users.php';" />
						</td></tr></table>
						</form>
						<script>
						 var frmvalidator  = new Validator("adduser");
							frmvalidator.EnableOnPageErrorDisplaySingleBox();
							frmvalidator.EnableMsgsTogether();							
							frmvalidator.addValidation("f1username","req", "<?php echo $lbl_error_username_prereq ?>");
							frmvalidator.addValidation("f1username", "minlen=5", "<?php echo $lbl_error_min_len_5 ?>");
							frmvalidator.addValidation("f1name","req","<?php echo $lbl_error_name_prereq ?>");
							frmvalidator.addValidation("f1email", "email", "<?php echo $lbl_error_email_format ?>");
							frmvalidator.addValidation("f1email", "req", "<?php echo $lbl_error_email_req ?>");						
							frmvalidator.addValidation("f1password","req","<?php echo $lbl_error_pass_req ?>");
							frmvalidator.addValidation("f1password","neelmnt=f1username","<?php echo $lbl_error_pass_no_username ?>");
							frmvalidator.addValidation("f1password","minlen=5","<?php echo $lbl_error_pass_min_len_5 ?>");
							frmvalidator.addValidation("f1password1","eqelmnt=f1password", "<?php echo $lbl_error_pass_confirm ?>");
							frmvalidator.addValidation("f1usertype","req","<?php echo $lbl_error_usertype_req?>");
							frmvalidator.addValidation("f1defaultprinter","req","<?php echo $lbl_error_default_printer?>");
                            frmvalidator.addValidation("f1branch","req","<?php echo _ERROR_BRANCH_MANDATORY ?>");
					   </script>
			</div>
		</fieldset>
	</div>
</body>
</html>
<?php
} //not submitted
} else {
    header('Location: login.php');
}
?>
