<?php
require('assignment_one_globals.php');
session_start();

function doError($error_msg){
echo <<<END
 <img src="images/error_one.jpg" alt="Error" /><br />
 <img src="images/divider_one.gif" alt="" height="3" width="220" /><br /><br />
 {$error_msg}
END;
exit;
}

if(isset($_GET['act']))
 isReferedCorrectly("humber.websurgeon.ca/php_projects/assignments/one/assignment_one.php", false);

foreach($input_array as $name => $input){
 if($_GET['act'] == 'retry')
  ${$name} = (isset($_SESSION['contact'][$name])) ? $_SESSION['contact'][$name] : null;
 else
  ${$name} = null;
}

if(!isset($_GET['act'])){
 $right_panel = <<<END
 Please fill out this form if you have a question or comment.<br /><br />
 All fields except the second address line are required to complete this process.<br /><br />
 We will get back to you as soon as possible.<br /><br />
 Thank you.
END;
} elseif($_GET['act'] == 'contacted'){
 $full_name   = $_SESSION['contact']['first_name'].' '.$_SESSION['contact']['last_name'];
 $right_panel = <<<END
 <img src="images/thanks_one.jpg" alt="Thank You for Contacting Us" /><br />
 <img src="images/divider_one.gif" alt="" height="3" width="220" /><br /><br />
 Hello {$full_name}, <br />
 Thank you for contacting us.<br /><br />
 We will get back to you as soon as possible.<br /><br />
  - Site Staff
END;
} elseif($_GET['act'] == 'retry'){
 $error_msg   = $_SESSION['error_msg'];
 $right_panel = <<<END
 <img src="images/error_one.jpg" alt="Error" /><br />
 <img src="images/divider_one.gif" alt="" height="3" width="220" /><br /><br />
 {$error_msg}
END;
}


function provinceMenu($province){
    global $province_array;

 $menu  = "<select name=\"contact[province]\" id=\"province\">\n";
 $menu .= " <option value=\"\">Select Province</option>\n";
 
 foreach($province_array as $code => $name){
  $selected  = ($province == $code) ? ' selected="selected"' : null;
  $menu     .= " <option value=\"$code\"$selected>$name</option>\n";
 }
 
 $menu .= "</select>\n";
 return $menu;
}

$province      = (isset($_SESSION['contact']['province']) && $_GET['act'] == 'retry') ? $_SESSION['contact']['province'] : null;
$province_menu = provinceMenu($province);

echo <<<END
<!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=iso-8859-1" />
 <title>Assignment One</title>
 <link href="styles.css" rel="stylesheet" type="text/css">
</head>
<body>

<form method="post" action="process_assignment_one.php">
 <input type="hidden" name="check_send" value="yes" />

<table cellpadding="3" width="600" cellspacing="0" border="0">
 <tr>
  <td class="contact_header" colspan="2">Contact Us</td>
 </tr>
 <tr>
  <td id="left_panel">

<table cellpadding="3" cellspacing="0" border="0">
 <tr>
  <td class="contact_left">First Name</td>
  <td class="contact_right"><input type="text" name="contact[first_name]" value="$first_name" /></td>
 </tr>
 <tr>
  <td class="contact_left">Last Name</td>
  <td class="contact_right"><input type="text" name="contact[last_name]" value="$last_name" /></td>
 </tr>
 <tr>
  <td class="contact_left">Email</td>
  <td class="contact_right"><input type="text" name="contact[email]" value="$email" /></td>
 </tr>
 <tr>
  <td class="contact_left">Street&nbsp;Address</td>
  <td class="contact_right"><input type="text" name="contact[street_one]" value="$street_one" /></td>
 </tr>
 <tr>
  <td class="contact_left">&nbsp;</td>
  <td class="contact_right"><input type="text" name="contact[street_two]" value="$street_two" /></td>
 </tr>
 <tr>
  <td class="contact_left">City</td>
  <td class="contact_right"><input type="text" name="contact[city]" value="$city" /></td>
 </tr>
 <tr>
  <td class="contact_left">Province</td>
  <td class="contact_right">
   $province_menu
  </td>
 </tr>
 <tr>
  <td class="contact_left">Postal Code</td>
  <td class="contact_right">
   <input type="text" name="contact[postal_one]" size="3" maxlength="3" value="$postal_one" style="width: 50px" />
   <input type="text" name="contact[postal_two]" size="3" maxlength="3" value="$postal_two" style="width: 50px" />
  </td>
 </tr>
 <tr>
  <td class="contact_left">Phone Number</td>
  <td class="contact_right"><input type="text" name="contact[phone]" value="$phone" /></td>
 </tr>
 <tr>
  <td class="contact_left" style="vertical-align: top">Message</td>
  <td class="contact_right"><textarea name="contact[message]" rows="10" cols="30">$message</textarea></td>
 </tr>
 <tr>
  <td colspan="2" class="contact_right" align="right"><input type="submit" id="submit_form" value="Submit Message" /></td>
 </tr>
</table>

  </td>
  <td id="right_panel">
    $right_panel
  </td>
 </tr>
</table>

</form>

</body>
</html>
END;
?>