<?php session_start();?>
<?php include 'header.php'; ?>

		<?php 
		//$message = '<font color=red>Please insert username and password</font>';
		if (isset($_POST['login'])) {
			$username = trim(addslashes($_POST['username']));
			$password = trim(addslashes($_POST['password']));
			
			if ($username != '' && $password != '') {
				include 'open_connection.php';
				$sql = "SELECT user_name, user_password, user_section, user_access  FROM user
						WHERE user_name = '$username' AND user_password = '$password'";
																		
				$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
				$row = mysql_fetch_array($result, MYSQL_ASSOC);
				
				if (mysql_num_rows($result) == 1) {
					$_SESSION['isLogged_in'] = true;
					$_SESSION['username'] = $row['user_name'];
					$_SESSION['section'] = $row['user_section'];
					$_SESSION['access'] = $row['user_access'];
					//$_GET['kodpengguna'] = $row['kodpengguna'];
					//$kodpengguna = $_GET['kodpengguna'];
					
					if( $_SESSION['section'] == 'humancapital' &&  $_SESSION['access'] == 'admin' )
						{	
							// echo "<script language='javascript'>window.location='home_admin.php?kodpengguna=$kodpengguna'/script>";
							echo "<script language='javascript'>window.location='staff_admin.php'</script>";
							}
					else if($_SESSION['section'] == 'finance'  &&  $_SESSION['access'] == 'admin' )
						{
						echo "<script language='javascript'>window.location='vendor_admin.php'</script>";
						
						}
					else if($_SESSION['access'] == 'user')
						{
						echo "<script language='javascript'>window.location='staff_search.php'</script>";
						}
				}
				
				else
					    $message = '<script type="text/javascript">
						window.alert("Invalid username and password")
						</script>';
				 }
				 else
					    $message = '<script type="text/javascript">
						window.alert("Please enter username and password")
						</script>';
	       }	
		?>
	
	<table width="100%" align="center">
	<tr>
	<td align="center"><img src="images/login.jpg" width="150" height="90">
	</td>
	
	<tr>
	<td align="center">
			<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
            <p><font face="Arial, Helvetica, sans-serif">Username : </font><font face="Century Gothic, Comic Sans MS, Courier, Curlz MT, Franklin   Gothic Demi, Franklin Gothic Demi Cond, Goudy Stout, Lucida Sans Typewriter, Lucida Sans Unicode"> 
              <input name="username" type="text">
              <br>
			  <br>
              <font face="Arial, Helvetica, sans-serif">Password</font><font face="Arial, Helvetica, sans-serif">&nbsp;:</font> 
              <input name="password" type="password">
              </font>
            <br>
            </p>
                 <!--<td width="17"><input type="hidden" name="kodpengguna" value="<?php // echo $kodpengguna; ?>"></td>-->  
				  <br>
				  <input name="login" type="submit" value="Login">
                <!--  <td width="64"> -->
				<input type="reset" name="Reset" value="Reset">
  </form>

</td>
</tr>
</tr>
</table>

		  
     <?php  echo $message ?> 
        
		</td>
    </tr>
  </table>

<br>

<?php include ('footer.php'); ?>
