
<?php

?>
<div id="contact">
		<div id="secondary" class="center">
			<div class="align-center">
				<h1 id="resume">-Contact Form-</h1>
				<p>Fill out the form below.</p>
				<?php 
if (isset($_REQUEST['submitted'])) {
// Initialize error array.
  $errors = array();
  // Check for a proper First name
  if (!empty($_REQUEST['firstname'])) {
  $firstname = $_REQUEST['firstname'];
  $pattern = "/^[a-zA-Z0-9\_]{2,20}/";// This is a regular expression that checks if the name is valid characters
  if (preg_match($pattern,$firstname)){ $firstname = $_REQUEST['firstname'];}
  else{ $errors[] = 'Your Name can only contain _, 1-9, A-Z or a-z 2-20 long.';}
  } else {$errors[] = 'You forgot to enter your First Name.';}
  
  // Check for a proper Last name
  if (!empty($_REQUEST['lastname'])) {
  $lastname = $_REQUEST['lastname'];
  $pattern = "/^[a-zA-Z0-9\_]{2,20}/";// This is a regular expression that checks if the name is valid characters
  if (preg_match($pattern,$lastname)){ $lastname = $_REQUEST['lastname'];}
  else{ $errors[] = 'Your Name can only contain _, 1-9, A-Z or a-z 2-20 long.';}
  } else {$errors[] = 'You forgot to enter your Last Name.';}
  
  //Check for a valid phone number
  if (!empty($_REQUEST['phone'])) {
  $phone = $_REQUEST['phone'];
  $pattern = "/^[0-9\_]{7,20}/";
  if (preg_match($pattern,$phone)){ $phone = $_REQUEST['phone'];}
  else{ $errors[] = 'Your Phone number can only be numbers.';}
  } else {$errors[] = 'You forgot to enter your Phone number.';}
  
  // Check for a proper email
  if (!empty($_REQUEST['email'])) {
  $email = $_REQUEST['email'];
  $pattern = "/^[a-zA-Z0-9\_\@]{2,20}/";// This is a regular expression that checks if the name is valid characters
  if (preg_match($pattern,$email)){ $email = $_REQUEST['email'];}
  else{ $errors[] = 'Your Email can only contain _, 1-9, A-Z or a-z & @ 2-20 long.';}
  } else {$errors[] = 'You forgot to enter your Email Address.';}
  
  // Check for a proper Message
  if (!empty($_REQUEST['cmessage'])) {
  $cmessage = $_REQUEST['cmessage'];
  $pattern = "/^[a-zA-Z0-9\_]{2,100}/";// This is a regular expression that checks if the name is valid characters
  if (preg_match($pattern,$cmessage)){ $cmessage = $_REQUEST['cmessage'];}
  else{ $errors[] = 'Your Message can only contain _, 1-9, A-Z or a-z 2-100 long.';}
  } else {$errors[] = 'You forgot to enter a message.';}
    } 
  //End of validation 

if (isset($_REQUEST['submitted'])) {
  if (empty($errors)) { 
  $from = "From: Skyneilson.com!"; //Site name
  // Change this to your email address you want to form sent to
  $to = "skylarjn@gmail.com"; 
  $subject = "Skylar Neilson - Our Site has a Comment from " . $name . "";
  
  $message = "Message from " . $firstname . " " . $lastname . "
  Phone: " . $phone . " 
  Email: " . $email . "
  Message: " . $cmessage ."";
  
  mail($to,$subject,$message,$from);
  }
}
?>
<?php
  //Print Errors
if (isset($_REQUEST['submitted'])) {
  // Print any error messages. 
  if (!empty($errors)) { 
  echo '<hr/><h3>The following occurred:</h3><ul>'; 
  // Print each error. 
  foreach ($errors as $msg) { echo '<li>'. $msg . '</li>';}
  echo '</ul><h3>Your mail could not be sent, please fix input errors.</h3><hr />';}
   else{echo '<hr /><h3 align="center">Your mail was sent. Thank you!</h3><hr /><p>Below is the message that you sent.</p>'; 
  echo "Message from " . $firstname . " " . $lastname . " <br />Phone: ".$phone." <br />";
  echo "<br />Email: " . $email . "";
  echo "<br />Message: " . $cmessage . "";
  }
  }
  ?>

				<form action="" method="post">
  					<label>First Name: <br />
  					<input name="firstname" type="text" value="- Enter First Name -" /><br /></label>
  					<label>Last Name: <br />
  					<input name="lastname" type="text" value="- Enter Last Name -" /><br /></label>
  					<label>Phone Number: <br />
  					<input name="phone" type="text" value="- Enter Phone Number -" /><br /></label>
  					<label>Email Address: <br />
  					<input name="email" type="text" value="- Enter Email Address -" /><br /></label>
					<label>Enter Message: <br />
  					<input name="cmessage" type="text" value="- Enter Message -" /><br /></label>
  					<input name="" type="reset" value="Reset Form" /><input name="submitted" type="submit" value="Submit" />
  				</form>
						
				<p><a href="https://www.facebook.com/skylarjn" title="www.facebook.com/skylarjn">Click Here</a> to also connect with me on Facebook.com</p>
			</div>
		</div>		
</div>