<?php
  require_once "../lib/init.php";

  // process form
  if (@$_REQUEST['submitForm']) {

    // error checking
    $errorsAndAlerts = "";
    if (!@$_REQUEST['fullname'])                { $errorsAndAlerts .= "You must enter your full name!<br/>\n"; }
    if (!@$_REQUEST['email'])                   { $errorsAndAlerts .= "You must enter your email!<br/>\n"; }
    else if(!isValidEmail(@$_REQUEST['email'])) { $errorsAndAlerts .= "Please enter a valid email (example: user@example.com)<br/>\n"; }
    if (!@$_REQUEST['message'])                { $errorsAndAlerts .= "You must enter a message!<br/>\n"; }
    if (!@$_REQUEST['rating'])                 { $errorsAndAlerts .= "You must select a rating!<br/>\n"; }

    // send email user
    if (!$errorsAndAlerts) {
      $from    = $_REQUEST['email'];
      $to      = "dave@interactivetools.com";
      $subject = "Email form submitted from {$_SERVER['HTTP_HOST']}";
      $message = <<<__TEXT__
You've received an email from the email form here:
http://{$_SERVER['HTTP_HOST']}{$_SERVER['SCRIPT_NAME']}

Full name: {$_REQUEST['fullname']}
Email:     {$_REQUEST['email']}
Message:   {$_REQUEST['message']}

The user who sent this message had the IP address {$_SERVER['REMOTE_ADDR']}.
__TEXT__;
      // Note: The above line must be flush left or you'll get an error
      // This is a PHP heredoc.  See: http://ca2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

      // send message
      $mailResult = @mail($to, $subject, $message, "From: $from");
      if (!$mailResult) { die("Mail Error: $php_errormsg"); }

      //
      $errorsAndAlerts = "Thanks!  We've sent your email.";
      $_REQUEST = array(); // clear form values
    }

  }

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
  body, td { font-family: arial }
</style>
</head>
<body>

<form method="POST" action="">
<input type="hidden" name="submitForm" value="1" />

<h1>Sample Email Form</h1>

<?php if (@$errorsAndAlerts): ?>
  <div style="color: red; font-weight: bold; font-size: 16px; font-family: arial;"><br/>
    <?php echo $errorsAndAlerts; ?><br/><br/>
  </div>
<?php endif ?>

<table border="0" cellspacing="0" cellpadding="2">
 <tr>
  <td width="150">Full Name</td>
  <td><input type="text" name="fullname" value="<?php echo htmlspecialchars(@$_REQUEST['fullname']); ?>" size="50" /></td>
 </tr>
 <tr>
  <td width="150">Email</td>
  <td><input type="text" name="email" value="<?php echo htmlspecialchars(@$_REQUEST['email']); ?>" size="50" /></td>
 </tr>
 <tr>
  <td width="150">Message</td>
  <td><textarea name="message" rows="5" cols="55"><?php echo htmlspecialchars(@$_REQUEST['message']); ?></textarea></td>
 </tr>
  <tr>
  <td width="150"></td>
  <td>
    <input <?php checkedIf(@$_REQUEST['rating'], '5'); ?> type="radio" name="rating" value="5" />Amazing</td>
    <input <?php checkedIf(@$_REQUEST['rating'], '4'); ?> type="radio" name="rating" value="4" />Good</td>
    <input <?php checkedIf(@$_REQUEST['rating'], '3'); ?> type="radio" name="rating" value="3" />OK</td>
    <input <?php checkedIf(@$_REQUEST['rating'], '2'); ?> type="radio" name="rating" value="2" />Bad</td>
    <input <?php checkedIf(@$_REQUEST['rating'], '1'); ?> type="radio" name="rating" value="1" />Never Again.</td>
 </tr>
 <tr>
  <td colspan="2" align="center">
    <br/>
    <input type="submit" name="submit" value="Send Message &gt;&gt;">
  </td>
 </tr>
</table>


</body>
</html>
