<?php
require_once('../config/config.php');
require_once(CLASS_DIR.'dbClass.php');
$objAdmin = new DBClass();
if(!isset($_GET['action'])){
	if(isset($_COOKIE['silverstar'])){
		$sql = "SELECT * FROM ".TBL_ADMIN." WHERE id = '".$_COOKIE['silverstar']."' AND status = 0 ";
		$rs = $objAdmin->executeQry($sql,$link);
		$rownum = $objAdmin->getrow();
		$row = $objAdmin->fetchrow();
		if($rownum > 0) {
			$_SESSION['user_id'] = $row['id'];
			$_SESSION['username'] = $row['uname'];
			setcookie("user_id", $row['id'], time()+3600);
			setcookie("username", $row['uname'], time()+3600);
			@header('Location: index.php');
		}
	}
}   
if(isset($_POST['submit']) && $_POST['submit'] == 'Login')
{
	$sql = "SELECT * FROM ".TBL_ADMIN." WHERE uname = '".$_POST['uname']."' AND pass = '".$_POST['upass']."' AND status = 0 ";
	$rs = $objAdmin->executeQry($sql,$link);
	$rownum = $objAdmin->getrow();
	$row = $objAdmin->fetchrow();
	if($rownum > 0) {     
		if(isset($_POST['remember']) && $_POST['remember'] == 'yes'){
              setcookie("silverstar",$row['id']);
		} 
		
		$_SESSION['user_id'] = $row['id'];
		$_SESSION['username'] = $row['uname'];
		setcookie("user_id", $row['id'], time()+3600);
		setcookie("username", $row['uname'], time()+3600);
		@header('Location: index.php');
	} else{
        	$error[] = "Username and password didn't match....!";
	}
}

if(isset($_GET['action']) && $_GET['action'] == 'logout'){
	session_destroy();
	setcookie("user_id", $row['id'], time()-3600);
	setcookie("username", $row['uname'], time()-3600);
	$info[] = "You are successfully logged out.";
}       

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="shortcut icon" href="../images/address_bar_logo.gif" />
<title>Coal Bazaar - Administrative Control Panel</title>
<link href="../css/style.css" rel="stylesheet" type="text/css" />
<SCRIPT TYPE="text/javascript" LANGUAGE="javascript">
<!-- PreLoad Wait - Script -->
function waitPreloadPage() { //DOM
if (document.getElementById){
document.getElementById('prepage').style.display='block';
}else{
document.prepage.style.display = 'none';
}
}
// End -->
</SCRIPT>
</head>
<body class="loginbg">
<div id="container">
	<div class="login_box">
		<div class="loginform">
<DIV id="prepage" style="position:absolute; display:none; padding-left:145px; padding-top:10px;"> 
<img src="ajax-loader.gif" />
</DIV>
			<div><?php MiniError($error); MiniInfo($info);?></div>
			<form name="loginform" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" id="loginform" onsubmit="waitPreloadPage();">
			<div class="formtxt">Username</div>
			<div class="txtfield"><input name="uname" type="text" /></div>
			<div class="clear"></div>
			<div class="formtxt">Password</div>
			<div class="txtfield"><input name="upass" type="password" /></div>
			<div class="clear"></div>
			<div class="formtxt"></div>
			<div class="txtfield2">
				<div class="remember">
					<input name="remember" type="checkbox" value="yes" />
					<strong>Remember Me</strong><br />
					<div class="forgot"></div>
				</div>
			<div class="btn_login"><input name="submit" type="image" src="../images/btn_login.PNG" /><input type="hidden" name="submit" value="Login" /></div>
			</form>
		</div>
		<div class="clear"></div>
		</div>
	</div>
</div>
</body>
</html>