<?php
  // Load Configuration Information
  require_once(__DIR__.'/private/config.php');
  
  
  // Variable Initialization
  $first_name = "";
  $last_name = "";
  $email = "";
  $ptf_code = "";
  
  
  // Error Variable Initialization
  $general_error    = false;
  $first_name_error = false;
  $last_name_error  = false;
  $email_error      = false;
  $school_error     = false;
  $password_error   = false;
  $ptf_code_error   = false;
  
  
  // Form Submission Routine
  if (isset($_POST['submit'])) {
    // First name
    $first_name_error = true;
    $first_name_help = "First name required.";
    if (isset($_POST['firstName'])) {
      $first_name = trim($_POST['firstName']);
      if ($first_name != "") {
        $first_name_error = false;
      }
    }
    
    // Last name
    $last_name_error = true;
    $last_name_help = "Last name required.";
    if (isset($_POST['lastName'])) {
      $last_name = trim($_POST['lastName']);
      if ($last_name != "") {
        $last_name_error = false;
      }
    }
    
    // Email
    $email_error = true;
    $email_help = "Valid university email required.";
    if (isset($_POST['email'])) {
      $email = trim($_POST['email']);
      if (User::emailValid($email)) {
        if (User::emailTaken($email)) {
          $email_help = "An existing user already associated with this email.";
        } else {
          $email_error = false;
        }
      }
    }
    
    // School/University
    $school = User::resolveSchoolFromEmail($email);
    $school_error = empty($school);
    $school_help = "Unrecognized university.";
    
    // Password
    $password_error = true;
    $password_help = "Password required.";
    if (isset($_POST['password'])) {
      $password = trim($_POST['password']);
      if ($password != "") {
        $password_error = false;
      }
    }
    if (!$password_error) {
      $password_error = true;
      $password_help = "Passwords do not match.";
      if (isset($_POST['passwordVerify'])) {
        $password_verify = trim($_POST['passwordVerify']);
        if ($password == $password_verify) {
          $password_error = false;
        }
      }
    }
    
    // PTF registration code
    $is_ptf = (isset($_POST['isPTF']) && ($_POST['isPTF'] == 'Y'));
    if ($is_ptf) {
      $ptf_code_error = true;
      $ptf_code_help = "Incorrect PTF registration code.";
      if (isset($_POST['ptf_code'])) {
        $ptf_code = $_POST['ptf_code'];
        if ($ptf_code == PTF_REGISTRATION_CODE) {
          $ptf_code_error = false;
        }
      }
    }
    
    // No errors -- create user
    $general_error = false;
    if (!($first_name_error || $last_name_error || $email_error ||
          $school_error || $password_error || $ptf_code_error)) {
      $user = User::create($first_name, $last_name, $email, $school, $password, $is_ptf);
      if (is_null($user)) {
        $general_error = true;
        $general_help = "Unable to create user. Connection to database may be down.";
      } else {
        header("Location: logout.php");
        exit();
      }
    }
  }
?>




<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    
    <title>Registration - My Digital Hand</title>
    
    <!-- Bootstrap -->
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- CSS -->
    <link href="css/rtptf.css" rel="stylesheet">
    <link href="css/splash.css" rel="stylesheet">
    
    <!-- Font -->
    <link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    
    <!-- Icon -->
    <link rel="shortcut icon" type="image/png" href="img/ptf-icon.png" />
    
    
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  
  
  
  
  <body>
    <div class="container-fluid">
      <div id="login">
        <!-- Title -->
        <h1>User Registration</h1>
        
        <p></p>
        
        <?php if (!empty($general_error)) { ?>
          <!-- Print Error Messages -->
          <div class="alert alert-danger alert-dismissible" role="alert">
          <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <strong>Error:</strong>
            <?php print($general_help); ?>
          </div>
        <?php } ?>
        
        
        <form method="POST" class="form-horizontal">
          <div class="form-group <?php print($first_name_error ? 'has-error' : '') ?>">
            <label for="firstName" class="control-label col-sm-3">First Name</label>
            <div class="col-sm-9">
              <input type="text" class="form-control" name="firstName" id="firstName" placeholder="Your first name" value="<?php print(htmlspecialchars($first_name)) ?>">
            </div>
            
            <?php if ($first_name_error) { ?>
              <div class="col-sm-9 col-sm-offset-3">
                <p class="text-danger">
                  <?php print($first_name_help) ?>
                </p>
              </div>
            <?php } ?>
          </div>
          
          
          <div class="form-group <?php print($last_name_error ? 'has-error' : '') ?>">
            <label for="lastName" class="control-label col-sm-3">Last Name</label>
            <div class="col-sm-9">
              <input type="text" class="form-control" name="lastName" id="lastName" placeholder="Your last name" value="<?php print(htmlspecialchars($last_name)) ?>">
            </div>
            
            <?php if ($last_name_error) { ?>
              <div class="col-sm-9 col-sm-offset-3">
                <p class="text-danger">
                  <?php print($last_name_help) ?>
                </p>
              </div>
            <?php } ?>
          </div>
          
          
          <div class="form-group <?php print($email_error ? 'has-error' : '') ?>">
            <label for="email" class="control-label col-sm-3">E-Mail</label>
            <div class="col-sm-9">
              <input type="text" class="form-control" name="email" id="email" placeholder="Your school email address" value="<?php print(htmlspecialchars($email)) ?>">
            </div>
            
            <?php if ($email_error) { ?>
              <div class="col-sm-9 col-sm-offset-3">
                <p class="text-danger">
                  <?php print($email_help) ?>
                </p>
              </div>
            <?php } ?>
            
            <?php if ($school_error) { ?>
              <div class="col-sm-9 col-sm-offset-3">
                <p class="text-danger">
                  <?php print($school_help) ?>
                </p>
              </div>
            <?php } ?>
          </div>
          
          
          <div class="form-group <?php print($password_error ? 'has-error' : '') ?>">
            <label for="password" class="control-label col-sm-3">Password</label>
            <div class="col-sm-9">
              <input type="password" class="form-control" name="password" id="password" placeholder="Desired password">
            </div>
          </div>
          
          
          <div class="form-group <?php print($password_error ? 'has-error' : '') ?>">
            <label for="passwordVerify" class="control-label col-sm-3">Verify Password</label>
            <div class="col-sm-9">
              <input type="password" class="form-control" name="passwordVerify" id="passwordVerify" placeholder="Retype desired password">
            </div>
            
            <?php if ($password_error) { ?>
              <div class="col-sm-9 col-sm-offset-3">
                <p class="text-danger">
                  <?php print($password_help) ?>
                </p>
              </div>
            <?php } ?>
          </div>
          
          
          <div class="form-group">
            <label for="isPTF" class="control-label col-sm-3">Are you a PTF?</label>
            <div class="col-sm-9 form-inline">
              <select id="isPTF" name="isPTF" class="form-control">
                <option value="N" <?php if (!$is_ptf) echo 'selected' ?>>No</option>
                <option value="Y" <?php if ($is_ptf)  echo 'selected' ?>>Yes</option>
              </select>
            </div>
          </div>
          
          
          <div class="form-group <?php echo $ptf_code_error ? 'has-error' : 'js-hidden' ?>">
            <label for="ptf_code" class="control-label col-sm-3">PTF Registration Code:</label>
            <div class="col-sm-9">
              <input type="text" class="form-control" name="ptf_code" id="ptf_code" value="<?php print(htmlspecialchars($ptf_code)) ?>">
            </div>
            
            <?php if ($ptf_code_error) { ?>
              <div class="col-sm-9 col-sm-offset-3">
                <p class="text-danger">
                  <?php print($ptf_code_help) ?>
                </p>
              </div>
            <?php } ?>
          </div>
          
          
          <div class="form-group">
            <div class="col-xs-12 text-right">
              <p>
                <button type="submit" class="btn btn-primary" name="submit" value="submit">Register</button>
              </p>
              <a href="login.php">Already have an account? Sign in.</a>
            </div>
          </div>
        </form>
      </div>
    </div>
    
    
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery-1.11.3.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="bootstrap/js/bootstrap.min.js"></script>
    <script src="js/register.js"></script>
  </body>
</html>