<?php
session_start();
$authFailedMessage = '';
if(array_key_exists('username', $_SESSION))
{
    if( $_SESSION['username'] != null)
        //redirect to admin page with the info we have.
        header('Location: admin.php');
}
else if(array_key_exists ('username', $_REQUEST) && array_key_exists ('password', $_REQUEST))
{
    if($_REQUEST['username'] != null &&  $_REQUEST['password'] != null)
    {
        //authenticate
        require_once 'admin_bizLayer.php';
        $biz = new admin_bizLayer();
        if($biz->IsUserAuthorized(trim($_REQUEST['username']), trim($_REQUEST['password'])))
        {
            $biz->CloseDatabaseConnection();
            header('Location: admin.php');
            exit();
        }
        $biz->CloseDatabaseConnection();
        $authFailedMessage = 'Authentication Failed. Please retry.';
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html>
    <head>
        <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'></meta>
        <title>Carview | VRoom 360</title>
    </head>
    <body style='text-align: center'>
        <h1>Carview | VRoom 360</h1>
        <form action='admin_login.php' method='post'>
        <table border='0' align='center'>
            <thead>
                <tr>
                    <th colspan='2'> Admin Login</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td colspan='2' style='text-align: center'><span style='color: red'><?php echo $authFailedMessage; ?></span></td>
                </tr>
                <tr>
                    <td>Username:</td>
                    <td><input type='text' name='username' id='username' /> </td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input type='password' name='password' id='password' /></td>
                </tr>
                <tr>
                    <td colspan='2' style='text-align: center'><input type='submit' name='sumbit' id='submit' value='Login' /></td>
                </tr>
            </tbody>
        </table>
        </form>
    </body>
</html>