<?php
/*
	Template Name: Contact
*/
?>

<?php

	// Do not edit anything from here unless you know what you are doing
	$contactErrors = array();

	// Load library recaptcha
	require(get_template_directory() . '/functions/library-recaptcha.php');	

	if ($_SERVER['REQUEST_METHOD'] == 'POST')
	{	
		// Set email to send messages to
		if (serifly_option('general_contact_email'))
		{
			$emailTo = serifly_option('general_contact_email');
		}
		else
		{
			$emailTo = get_option('admin_email');
		}
	
		if(trim($_POST['contactName']) === '')
		{
			$contactErrors['contactName'] = 'Your full name is required';
		}
		else
		{
			$contactName = trim($_POST['contactName']);
		}
		
		if(trim($_POST['contactEmail']) === '')
		{
			$contactErrors['contactEmail'] = 'Your email address is required';
		}
		else if (!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", trim($_POST['contactEmail'])))
		{
			$contactErrors['contactEmail'] = 'Your email address seems to be invalid';
		}
		else
		{
			$contactEmail = trim($_POST['contactEmail']);
		}
		
		if(trim($_POST['contactMessage']) === '')
		{
			$contactErrors['contactMessage'] = 'Your message is required';
		}
		else
		{
			if (function_exists('stripslashes'))
			{
				$contactMessage = stripslashes(trim($_POST['contactMessage']));
			}
			else
			{
				$contactMessage = trim($_POST['contactMessage']);
			}
		}
		
		if (serifly_option('general_recaptcha_private') && serifly_option('general_recaptcha_private'))
		{		
			if (!isset($_POST['recaptcha']) or $_POST['recaptcha'] === '')
			{
				$contactErrors['contactCaptcha'] = 'Captcha is required';
			}
			else
			{
				$recaptcha_response = recaptcha_check_answer(serifly_option('general_recaptcha_private'), $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha']);
				
				if (!$recaptcha_response->is_valid)
				{
					$contactErrors['contactCaptcha'] = 'Captcha is not correct';
				}
			}
		}
		
		if (empty($contactErrors) && trim($emailTo) !== '')
		{			
			$subject = '(Contact Form) From ' . $contactName;
			$body = "Name: $contactName \n\nEmail: $contactEmail \n\nMessage: $contactMessage";
			$headers = 'From: ' . $contactName . ' <' . $emailTo . '>' . "\r\n" . 'Reply-To: ' . $contactEmail;
			
			mail($emailTo, $subject, $body, $headers);
			$emailSent = true;
		}
	}

?>

<?php get_header(); ?>

<?php if (function_exists('dynamic_sidebar') && is_page() && is_active_sidebar('page-sidebar')): ?>
<div class="siteColumnLeft clearfix">
<?php endif; ?>

<div class="separator"></div>
<!-- Content -->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<!-- Contact Form -->
<?php if (isset($emailSent)): ?>
<p class="text"><em>Thank you, we have received your message and will reply as soon as possible.</em></p>
<?php else: ?>
<form id="contactForm" action="<?php the_permalink(); ?>" method="post">
	<div class="column">
		<label for="contactName"><?php if (isset($contactErrors['contactName'])) { echo $contactErrors['contactName']; } else { echo 'Full Name'; } ?></label>
		<input id="contactName" name="contactName" type="text"<?php if(isset($contactName)) echo ' value="' . $contactName . '"'; ?> />
	</div>
	<div class="column">
		<label for="contactEmail"><?php if (isset($contactErrors['contactEmail'])) { echo $contactErrors['contactEmail']; } else { echo 'Email Address'; } ?></label>
		<input id="contactEmail" name="contactEmail" type="text"<?php if(isset($contactEmail)) echo ' value="' . $contactEmail . '"'; ?> />
	</div>
	<div class="column">
		<label for="contactMessage"><?php if (isset($contactErrors['contactMessage'])) { echo $contactErrors['contactMessage']; } else { echo 'Message'; } ?></label>
		<textarea id="contactMessage" name="contactMessage" cols="10" rows="8"><?php if(isset($contactMessage)) echo $contactMessage; ?></textarea>
	</div>
	<?php if (serifly_option('general_recaptcha_private') && serifly_option('general_recaptcha_private')): ?>
	<div class="column recaptcha">
		<script type="text/javascript" >var RecaptchaOptions = { theme : 'custom', lang: 'en', custom_theme_widget: 'divrecaptcha' };</script>
		<label<?php if (isset($contactErrors['contactCaptcha'])) echo ' class="error"' ?> for="recaptcha_response_field"><?php if (isset($contactErrors['contactCaptcha'])) { echo $contactErrors['contactCaptcha']; } else { echo 'Captcha'; } ?></label>
		<a id="reload_recaptcha" href="javascript:Recaptcha.reload();">Reload</a>
		<div id="recaptcha_image">
		</div>
		<input id="recaptcha_response_field" name="recaptcha" type="text" />
		<?php		
			echo recaptcha_get_html(serifly_option('general_recaptcha_public'));
		?>
	</div>
	<?php endif; ?>
	<button type="submit" class="colorButton"><?php _e('Send Message', 'serifly') ?></button>
</form>
<?php endif; ?>
<?php endwhile; endif; ?>

<?php if (function_exists('dynamic_sidebar') && is_page() && is_active_sidebar('page-sidebar')): ?>
</div>
<?php endif; ?>

<?php get_sidebar(); ?>
<?php get_footer(); ?>