<?php
require_once 'admin_topbar.php';
?>

<!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 Create Admin</title>
        <link rel="stylesheet" href="admin_main.css" type="text/css" />
    </head>
    <body>
        <?php
        require_once 'admin_bizLayer.php';
        $biz = new admin_bizLayer();
        $username = '';
        $password = '';
        $reenterPassword = '';
        $email = '';
        $usernameError = '';
        $passwordError = '';
        $reenterPasswordError = '';
        $emailError = '';
        $createStatus = '';
        
        function ValidateCreateAdmin($username, $password, $reenterPassword, $email)
        {
            global $biz;
            //fields should not be empty
            $usernameError = strlen($username) == 0 ? 'Username is required.' : '';
            $passwordError = strlen($password) == 0 ? 'Password field cannot be empty.' : '';
            $reenterPasswordError = strlen($reenterPassword) == 0 ? 'Re-enter Password field cannot be empty.' : '';
            
            $emailError = strlen($email) == 0 ? 'Email field cannot be empty.' : '';
            
            //now check for logical errors if fields are not empty
            if(strlen($usernameError) == 0)
            {
                $usernameError = $biz->IsUsernameAvailable($username) ? '' : 'This id is not available.';
            }
            
            if(strlen($emailError) == 0)
            {
                $emailError = preg_match('/^[a-z0-9._%+-]+@(?:[a-z0-9.]+\.)+(?:[a-z]{2}|aero|arpa|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|org|pro|tel|travel|xxx)$/', $email) == 0 ? 'Invalid Email.' : '';
            }
            
            if(strlen($reenterPasswordError) == 0)
            {
                $reenterPasswordError = $password != $reenterPassword ? 'Passwords do not match. They should be same.' : '';
            }
            
            return array($usernameError, $passwordError, $reenterPasswordError, $emailError);
        }
        
        //do stuff only if create button has been hit.
        if(array_key_exists('create', $_REQUEST))
        {
            $username = trim($_REQUEST['username']);
            $password = trim($_REQUEST['password']);
            $reenterPassword = trim($_REQUEST['reenterPassword']);
            $email = trim(strtolower($_REQUEST['email']));
        
            //Validations
            list ($usernameError, $passwordError, $reenterPasswordError, $emailError) = ValidateCreateAdmin($username, $password, $reenterPassword, $email);

            if(strlen($usernameError) == 0 && strlen($emailError) == 0 && strlen($passwordError) == 0)
            {
                //do a database hit now that there isn't any error
                if($biz->AddAdmin($username, $password, $email))
                {
                    $createStatus =  '<span class=\'success\'>Admin successfully created!</span>';
                    $username = '';
                    $password = '';
                    $reenterPassword = '';
                    $email = '';
                }
                else
                {
                    $createStatus =  '<span class=\'error\'>An error occured while creating admin.</span>';
                    $password = '';
                    $reenterPassword = '';
                }
            }
        }
        $biz->CloseDatabaseConnection();
        //check for return
        PrintTopbar('Create Admin');
        echo "<div id='content' class='mainContent'>
        <p class='helpText'>Upon successful registration, an email will be sent to the one entered below to notify user of their credentials.<p>
        <form action='admin_user_create.php' method='post'>
        <table border='0' align='left'>
            <tbody>
                <tr>
                    <td>Username:</td>
                    <td><input type='text' name='username' id='username' value='$username' />&nbsp;&nbsp;<span class='error'>$usernameError</span> </td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input type='password' name='password' id='password' />&nbsp;&nbsp;<span class='error'>$passwordError</span></td>
                </tr>
                <tr>
                    <td>Re-enter Password:</td>
                    <td><input type='password' name='reenterPassword' id='reenterPassword' />&nbsp;&nbsp;<span class='error'>$reenterPasswordError</span></td>
                </tr>
                <tr>
                    <td>Email:</td>
                    <td><input type='text' name='email' id='email' value='$email'/>&nbsp;&nbsp;<span class='error'>$emailError</span></td>
                </tr>
                <tr>
                    <td colspan='2'>$createStatus</td>
                </tr>
                <tr>
                    <td colspan='2'><input type='submit' name='create' id='create' value='Create' /></td>
                </tr>
            </tbody>
        </table>
        </form>
        </div>";
        ?>
    </body>
</html>