<?php

session_start();

include_once("config.php");
include_once("mysql_interface.php");

?>
<head>
	<title><?php echo "Operator Login"; ?></title>
	<style>
		.auth_user{
		background:url(images/optor.png) no-repeat #ffffff;
		border:1px solid #000000;
		background-position:1px 1px;
		padding-left:20px;
		vertical-align:center;
		font-size:16px;
	}
	.auth_pwd{
		background:url(images/optor_lock.png) no-repeat #ffffff;
		border:1px solid #000000;
		background-position:1px 1px;
		padding-left:20px;
		vertical-align:center;
		font-size:16px;
	}
	</style>
</head>
<body bgcolor=#eeffaa >
<script type="text/javascript" src="js/sha1.js"></script>

<?php
try{
	if(isset($_POST['opID'])&&isset($_POST['password'])&& ($_POST['opID'] != '') && ($_POST['password'] != ''))
	{

		$uni_db = new mydb();//create db obj
		if(!($uni_db->connect($_config['host'], $_config['dbuser'], $_config['passwd'], $_config['port'], $_config['dbname'])))//check db connection
		{
			throw new Exception("Connect database failed");
		}
		$sql = "select count(*) from OperatorInfo where OperatorID='".$_POST['opID']."' and Password='".$_POST['password']."'";
		
		if(!($result=$uni_db->query($sql)))
		{
			throw new Exception("SQL statement execute failed");
		}
		if(!($re=$uni_db->fetch_row($result)))
		{
			throw new Exception("Operator not exist or invalid password");
		}
		if(0 == $re[0])
		{
			throw new Exception("Invalid Operator Name or Password");
		}
		$_SESSION['valid_optor'] = $_POST['opID'];
		echo '<meta http-equiv = \'refresh\'content=0;URL=\'userinfo_manage.php\'>';
	}
	else if(isset($_POST['opID'])&&isset($_POST['password']))
	{
		throw new Exception("Please input operator name and password");
	}
	else
	{
		
		?>
		<font size="5" color=#000000><b>Operator Login</b></font>
		<hr>
		<table>
		<form name="form1" action=<?php echo $_SERVER['PHP_SELF'];?> method="post" onsubmit="h1();">
			<tr>
				<td width="30%">Operator name</td><td><input class="auth_user" type="text" name="opID" /></td>
			</tr>
			<tr>
				<td width="30%">Password</td><td><input class="auth_pwd" type="password" name="password" /></td>
			</tr>
			<tr height="10px"></tr>
			<tr>
				<td colspan="2"><input type="submit" value="Submit" />
			</tr>
		</form>
	</table>
	<?php
	}
}
catch(Exception $e)
{
	echo $e->GetMessage();
	echo '<meta http-equiv = \'refresh\'content=3;URL=\''.$_SERVER['PHP_SELF'].'\'>';
}
?>
