<div id="login" class="boxed">
	<h2 class="title">Prihlásenie do systému </h2>
	<div class="side_content">
		
	<?php
	if (isset($_POST["username"]) && (trim($_POST["username"]) != "") && isset($_POST["password"]) && (trim($_POST["password"]) != "") ) {
		$link = connect_db();
		$sql = "SELECT * FROM users WHERE username='".$_POST['username']."' AND password='".md5( $_POST['password'])."'";
		$result = mysql_query($sql, $link);
		if ($result && (mysql_num_rows($result) == 1) ) {
			$row = mysql_fetch_assoc($result);
			if ($row[status]==1) {
				echo '<p>Boli ste úspešne prihlásený do systému. </p>';
				$_SESSION['uid'] = $row['uid'];
				$_SESSION['user'] = $_POST['username'];
				$_SESSION['name'] = $row['name'];
				$_SESSION['surname'] = $row['surname'];
				$_SESSION['email'] = $row['email'];
				$_SESSION['admin'] = $row['admin'];
				$_SESSION['status'] = $row['status'];
				$_SESSION['date_registered'] = $row['date_registered'];
				
				check_orders();
				
				//if ($_SESSION['admin'] == 1) {
				//	$page = $_SERVER['PHP_SELF'];
				//	$sec = "3";
				//	header("Refresh: $sec; url=$page");
				//}
				
			} else {
				echo '<p id="red">Vaše konto je dočasne blokované.</p>';
			}
			
			mysql_free_result($result);
		} else {
				echo '<p id="red">Nesprávne meno alebo heslo.</p>';
		}
		mysql_close($link);
			
	} elseif (isset($_POST['logout'])) {
			$_SESSION = array();
			session_destroy();
			
			if ($_SESSION['admin'] == 1) {
				$page = $_SERVER['PHP_SELF'];
				$sec = "3";
				header("Refresh: $sec; url=$page");
			}
	}
	
	if (isset($_SESSION['user'])) {
	?>
		
		<p>Prihláseny: <br /><strong><?php echo $_SESSION['name'] . ' ' . $_SESSION['surname']; ?></strong>.</p>
		<br />
		<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form1"> 
		  <p> 
			<input name="logout" type="submit" id="logout" value="Odhlás ma" /> 
		  </p> 
		</form>
			
	<?php	 
	} else {
	?>
			
		<form id="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
			<fieldset>
				<legend>Login</legend>
				<label>Prihlasovacie meno:</label>
				<input name="username" type="text" value="<?php if (isset($_POST["username"])) echo $_POST["username"]; ?>" /><br />
				<label>Heslo:</label><br />
				<input name="password" type="password" value="<?php if(isset($_POST['password'])) echo $_POST['password']; ?>" />
				<input name="submit" type="submit" id="submit" value="Prihlás ma" />
				<p><a href="forgot_pass.php">Zabudol si svoje heslo?</a></p>
			</fieldset>
		</form>
		<p><a href="register.php">[Vytvoriť nový účet]</a></p>
			
	<?php
	}
	?>

	</div>
</div>