<?php
 # needed in the change from olm provider to TotalChoice... 
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
//extract data from the post
extract($_POST);

// includes
require("$DOCUMENT_ROOT/db/db_setup.inc");
include("$DOCUMENT_ROOT/objects/widgets/table.inc");
include("$DOCUMENT_ROOT/objects/user.inc");

session_name("theaterreview");

$stError = "";
$bShowForm = true; // eventually, successful user creation should go to login page.

if (strlen($submit) > 0)
{
  dbConnect();
  
  // Create a user.
  $oUser = new User();
  $bReturn;
  $bError = false;

  // Check if passwords match.
  if (strcmp($password, $password_again) != 0)
  {
    // Error condition
    $stError = "Passwords do not match.<br>";
    $bError = true;
  }

  // Check password for value
  if (strlen($password) == 0)
  {
    // Error condition
    $stError = "Please enter a password.<br>";
    $bError = true;
  }

  if ((strlen($first_name) == 0) || 
      (strlen($last_name) == 0) ||
      (strlen($email) == 0) ||
      (strlen($display_name) == 0))
  {
    $stError = "Please enter a value for every field.<br>";
    $bError = true;
  }

  if ($bError == false)
  {
    // Set user values
    $oUser->setDisplayName(trim($display_name));
    $oUser->setFirstName(trim($first_name));
    $oUser->setLastName(trim($last_name));
    $oUser->setEmail(trim($email));
    $oUser->setSafePassword($password);

    // Save user
    if ($oUser->isUserUnique($db))
    {
      $bReturn = $oUser->insertUser($db);
      if ($bReturn == false)
      {
        $stError = "Unable to save to the database.";
        $stExtraError = mysql_error($db);
        if (strlen($stExtraError) > 0)
          $stError .= " Database error: " . mysql_errno() . ": " . $stExtraError;
      }
      else
      {
        // Form was successfully saved.  No need to show it again.
        $bShowForm = false;
      }
    }
    else
    {
      $stError = "The user name '$display_name' is taken. Please choose a different user name.";
    }
  }
  else
  {
    // There was an error, so show the form.
    $bShowForm = true;
  }
} 

if ($bShowForm == true)
{
  echo "<FORM name=\"mainform\" method=POST ACTION=\"useradd.php3\">";
  echo "<INPUT TYPE=\"hidden\" NAME=\"entrystage\" VALUE=\"user\">";
  $oTable = new Table("cellpadding=5");
  $iRow = 0;

  if (strlen($stError) > 0)
  {
    $oTable->addContent("<font color=red>$stError</font>", $iRow++, 0, 1, 2, "align=center");
  }

  $oTable->addContent("First Name: ", $iRow, 0, 1, 1, "align=left");
  $oTable->addContent("<INPUT TYPE=\"text\" NAME=\"first_name\" SIZE=25>", $iRow++, 1, 1, 1, "" );

  $oTable->addContent("Last Name: ", $iRow, 0, 1, 1, "align=left");
  $oTable->addContent("<INPUT TYPE=\"text\" NAME=\"last_name\" SIZE=25>", $iRow++, 1, 1, 1, "" );

  $oTable->addContent("Email Address: ", $iRow, 0, 1, 1, "align=left");
  $oTable->addContent("<INPUT TYPE=\"text\" NAME=\"email\" SIZE=25>", $iRow++, 1, 1, 1, "" );

  $oTable->addContent("User Name: ", $iRow, 0, 1, 1, "align=left");
  $oTable->addContent("<INPUT TYPE=\"text\" NAME=\"display_name\" SIZE=25>", $iRow++, 1, 1, 1, "" );

  $oTable->addContent("Password: ", $iRow, 0, 1, 1, "align=left");
  $oTable->addContent("<INPUT TYPE=\"password\" NAME=\"password\" SIZE=15>", $iRow++, 1, 1, 1, "" );

  $oTable->addContent("Password Again: ", $iRow, 0, 1, 1, "align=left");
  $oTable->addContent("<INPUT TYPE=\"password\" NAME=\"password_again\" SIZE=15>", $iRow++, 1, 1, 1, "" );

  $oTable->addContent("<INPUT TYPE=\"submit\" name=\"submit\" VALUE=\"Create Account\">" .
                      "&nbsp;<INPUT TYPE=\"reset\">", $iRow++, 0, 1, 2, "align=center");
  
  $oTable->addContent("<font size=-1>All fields are required.</font>", $iRow++, 0, 1, 2, "");
  echo $oTable->draw();
  echo "</form>";
?>

<br>
About Privacy.<br>
<br>
TheaterReview.com certifies that we will not trade, distribute, or barter any information given to us here about our users.  We will use emails solely for theaterreview.com related communications, including notification of prizes.<br>
<br>
"Real" names will not be available to the general public in any fashion, only your display name will be available to theaterreview visitors.  The staff of theaterreview does have access to that information, but promises not to distribute it in any way.<br>
<br>
TheaterReview reserves the right to remove blatently slanderous or malicious postings in any section of our site, but respects the opinions of reviewers and will not remove or modify reviews for the benefit of a particular theater or the theater community at large.<br>
<br>
Reviews are as anonymous as the user wishes (as determined by the recognizabilty of your display name).<br>
<br>
Questions?  Please contact <a href="mailto:info@theaterreview.com">info@theaterreview.com</a><br>
<br>
<?php

}
else
{
  // redirect to login
  header("Location: /index.php3");
}
?>
