<?php

require_once "../common/arsenal.php5";
if (logged_in()) logout();

if (get_request('register')) {
     $username = get_request('username');
     if (strlen($username) < 3) $errors[] = "Your username must be at least 3 characters.";
          
     $password = get_request('password');
     if (strlen($password) < 5) $errors[] = "Your password must be at least 6 characters.";
     else if ($password != get_request('password2')) $errors = "Your confirmation password doesn't match.";
     
     $email = get_request('email');
     if (strlen($email) < 7) $errors[] = "Please provide a valid email address.";
     else if (!substr_count($email, '@') || !substr_count($email, '.')) $errors[] = "Please provide a valid email address.";
     
     if (empty($errors)) {
          require_once "../common/library/User.php5";
          $user = new User();
          $result = $user->createUser($username, $password, $email);
          if ($result['error']) $errors[] = $result['error'];
          else {    // The login was successful
               $_SESSION['username'] = $username;
               $_SESSION['user_id'] = $user->user_id;
               $_SESSION['email'] = $email;
               header ("Location: thanks.php5");
          }
     } // empty($errors)
} // end if the are submitting
     

drawHeader('Register');
?>

<script language="javascript">
<!--
     function validateForm() {
          if (registerForm.username.value.length < 3) {
               alert ('Your username must be at least 3 characters.');
               return false;
          } else if (registerForm.password.value.length < 5) {
               alert ('Your password must be at least 6 characters.');
               return false;
          } else if (registerForm.password2.value != registerForm.password.value) {
               alert ('Your password and confirmation do not match.');
               return false;
          } else if (registerForm.email.value.length < 7) {
               alert ('You must provide a valid email address.');
               return false;
          } else if (registerForm.email.value.match('@') != '@') {
               alert ('Please enter a valid email address.');
               return false;
          }
     }
-->
</script>

<p class="title">Register with LaterMail</p>

<?php 
drawErrors($errors);
?>

<form method="post" name="registerForm" action="register.php5">
<table border="0" align="center" cellspacing="4" cellpadding="2">
<tr>
     <td align="right"><label for="username" accesskey="U"><b>Username: </b></label></td>
     <td><input class="input" type="text" maxlength="18" name="username" value="<?php echo get_request('username'); ?>" id="username"></td>
</tr>
<tr>
     <td align="right"><label for="password" accesskey="P"><b>Password: </b></label></td>
     <td><input class="input" type="password" maxlength="18" name="password" value="<?php echo get_request('password'); ?>" id="password"></td>
</tr>
<tr>
     <td align="right"><label for="password2" accesskey="2"><b>Confirm Password: </b></label></td>
     <td><input class="input" type="password" maxlength="18" name="password2" value="<?php echo get_request('password2'); ?>" id="password2"></td>
</tr>
<tr>
     <td align="right"><label for="email" accesskey="E"><b>Email Address: </b></label></td>
     <td><input class="input" type="text" maxlength="128" name="email" value="<?php echo get_request('email'); ?>" id="email"></td>
</tr>
<tr>
     <td colspan="2" align="center">
          <input type="submit" name="register" value="Register" onclick="return validateForm();">
     </td>
</tr>
</table>
</form>

<?php
drawFooter();

?>