<?php
require_once("includes/config.inc.php");
$pageTitle = "Contact Page";
$pageDescription = "This page can be used to contact the Developer.";
$sideBar = "hobbies-sidebar.inc.php";

if($_SERVER['REQUEST_METHOD'] == "POST"){
	
	// Get the data entered by the user
	$firstName = isset($_POST['txtFirstName']) ? $_POST['txtFirstName'] : NULL;	
	$lastName = isset($_POST['txtLastName']) ? $_POST['txtLastName'] : NULL;	
	$email = isset($_POST['txtEmail']) ? $_POST['txtEmail'] : NULL;	
	$comments = isset($_POST['txtComments']) ? $_POST['txtComments'] : NULL;

	if(validateContactData($firstName, $lastName, $email, $comments)){

		// If the data is valid, then put it into a single string to send as an email 
		$msg = "Name: $firstName $lastName <br>";
		$msg .= "Email: $email <br>";
		$msg .= "Comments: $comments";  
		
		sendEmail(ADMIN_EMAIL, "Contact Form", $msg , "From: " . $email); // NOTE: we can uncomment the third param after we update the sendEmail() function!!!
		header("Location: " . PROJECT_DIR . "contact-confirmation.php");
		exit();	
		
	}else{

		// Foul play suspected (the client-side validation has been bypassed)!
		$msg = getAllSuperGlobals(); 
		sendEmail(ADMIN_EMAIL, "Security Warning!", $msg);
		header("Location: " . PROJECT_DIR . "error.php");
		exit();	
	}

}
require("includes/header.inc.php");
?>
		<main>

			<div class="content-frame">
			<script type="text/javascript" src="js/contact-form.js"></script>
				<h1>Contact Me</h1>
				
				<form id="contactForm" method="POST" action="contact-confirmation.php">
				    <table border="1">
				        <tr>
				            <td align="right" valign="bottom">First Name:</td>
				            <td>
				                <div class="validation-message" id="vFirstName"></div>
				                <input type="text" id="txtFirstName" name="txtFirstName">
				            </td>
				        </tr>
				        <tr>
				            <td align="right" valign="bottom">Last Name:</td>
				            <td>
				                <div class="validation-message" id="vLastName"></div>
				                <input type="text" id="txtLastName" name="txtLastName">
				            </td>
				        </tr>
				        <tr>
				            <td align="right" valign="bottom">Email:</td>
				            <td>
				                <div class="validation-message" id="vEmail"></div>
				                <input type="text" id="txtEmail" name="txtEmail">
				            </td>
				        </tr>
				        <tr>
				            <td align="right" valign="top">Comments:</td>
				            <td>
				                <div class="validation-message" id="vComments"></div>
				                <textarea id="txtComments" name="txtComments"></textarea>
				            </td>
				        </tr>
				        <tr>
				            <td align="right">&nbsp;</td>
				            <td>
				                <input type="submit" value="SUBMIT">
				            </td>
				        </tr>
				    </table>
				</form>

			</div>
			
		</main>
<?php
if(!empty($sideBar)){
	require("includes/" . $sideBar);
}

require("includes/footer.inc.php");







//
//functions
//

function validateContactData($firstName, $lastName, $email, $comments){

	if(empty($firstName) || empty($lastName) || empty($comments) || empty($email)){
		return false;
	}

	if(filter_var($email, Filter_VALIDATE_EMAIL) == false){
		return false;
	}

	return true;
}

?>


