<?php
/**
 * @package WordPress
 * @subpackage coe
 */
/**
 * Template Name: Contact page
 */
if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

if ( $coe_options['recaptcha_enabled'] == 'yes' ) {

    if ( !function_exists('_recaptcha_qsencode') ) {
	require_once('scripts/recaptcha/recaptchalib.php');
    }

    $publickey = $coe_options['recaptcha_publickey']; // you got this from the signup page
    $privatekey = $coe_options['recaptcha_privatekey']; // you got this from the signup page
    $resp = null;
    $error = null;
    if( isset($_POST['submit']) ) {
	$resp = recaptcha_check_answer ($privatekey,
		    $_SERVER["REMOTE_ADDR"],
		    $_POST["recaptcha_challenge_field"],
		    $_POST["recaptcha_response_field"]
		);
	if ( !$resp->is_valid ) {
	    $rCaptcha_error = $resp->error;
	}
    }
}



get_header();

$content_position = ( $coe_options['contact_sidebar'] == 'left' ) ? 'grid_16 push_8' : 'grid_16';
if ( $coe_options['remove_contact_sidebar'] == 'yes' ) $content_position = 'grid_24';
$NA_phone_format = $coe_options['NA_phone_format'] ? '_NA_format' : '';

//If the form is submitted
if( isset($_POST['submit']) ) {
    // Get form vars
    $contact_name = strip_tags(trim(stripslashes($_POST['contact_name'])));
    $contact_email = trim($_POST['contact_email']);
    $contact_phone = trim($_POST["contact_phone{$NA_phone_format}"]);
    $contact_ext = trim($_POST["contact_ext{$NA_phone_format}"]);
    $contact_message = strip_tags(trim(stripslashes($_POST['contact_message'])));

    // Error checking if JS is turned off
    if( $contact_name == '' ) { //Check to make sure that the name field is not empty
	$nameError = __('Please enter a name', 'coe');
    } else if( strlen($contact_name) < 2 ) {
	$nameError = __('Your name must consist of at least 2 characters', 'coe');
    }

    if( $contact_email == '' ) {
	$emailError = __('Please enter a valid email address', 'coe');
    } else if( !is_email( $contact_email ) ) {
	$emailError = __('Please enter a valid email address', 'coe');
    }

    if( $NA_phone_format ) {
	if( !isPhoneNumberValid( $contact_phone ) || ( $contact_phone == '' && $contact_ext != '' ) ) {
	    $phoneError = __('phone number', 'coe');
	}
	if( !eregi("^[0-9]{0,5}$", $contact_ext) ) { // check if the extension consists of 1 to 5 digits, or empty
	    $extError = __('extension', 'coe');
	}
    }
    if( isset($phoneError) && isset($extError) ) {
	$phone_extError = sprintf(__('Please enter a valid %1$s and %2$s', 'coe'), $phoneError, $extError );
    } else if( isset($phoneError) ) {
	$phone_extError = sprintf(__('Please enter a valid %s', 'coe'), $phoneError );
    } else if( isset($extError) ) {
	$phone_extError = sprintf(__('Please enter a valid %s', 'coe'), $extError );
    }

    if( $contact_message == '' ) {
	$messageError = __('Please enter your message', 'coe');
    }

    if( !isset($nameError) && !isset($emailError) && !isset($messageError) && !isset($rCaptcha_error) ) {
	$ext = ( $contact_ext != '' ) ? __('ext.', 'coe').$contact_ext : '';
	$phone = ( $contact_phone != '' ) ? __('Phone: ', 'coe').$contact_phone.' '.$ext."\r\n" : '';
	// Send email
	$email_address_to = $coe_options['email_receipients'];
	$subject = sprintf(__('Contact Form submission from %s', 'coe'), wp_specialchars_decode( get_option('blogname') ) );
	$message_contents = __("Sender's name: ", 'coe') . $contact_name . "\r\n" .
			    __('E-mail: ', 'coe') . $contact_email . "\r\n" .
			    $phone ."\r\n" .
			    __('Message: ', 'coe') . $contact_message . " \r\n";

	$header = "From: $contact_name <".$contact_email.">\r\n";
	$header .= "Reply-To: $contact_email\r\n";
	$header .= "Return-Path: $contact_email\r\n";
	$emailSent = ( @wp_mail( $email_address_to, $subject, $message_contents, $header ) ) ? true : false;

	$contact_name_thx = $contact_name;

	// Clear the form
	$contact_name = $contact_email = $contact_phone = $contact_ext = $contact_message = '';
    }
}


//Contact Information Fields from the Admin Options
$contact_field_array = array(
    array(
	'desc' => $coe_options['contact_field_name1'],
	'value' => $coe_options['contact_field_value1'] ),
    array(
	'desc' => $coe_options['contact_field_name2'],
	'value' => $coe_options['contact_field_value2'] ),
    array(
	'desc' => $coe_options['contact_field_name3'],
	'value' => $coe_options['contact_field_value3'] ),
    array(
	'desc' => $coe_options['contact_field_name4'],
	'value' => $coe_options['contact_field_value4'] ),
    array(
	'desc' => $coe_options['contact_field_name5'],
	'value' => $coe_options['contact_field_value5'] ),
    array(
	'desc' => $coe_options['contact_field_name6'],
	'value' => $coe_options['contact_field_value6'] ),
    array(
	'desc' => $coe_options['contact_field_name7'],
	'value' => $coe_options['contact_field_value7']
    )
);


?>

<div id="content-container" class="container_24">
    <div id="main-content" class="<?php echo $content_position; ?>">
	<div class="main-content-padding">
<?php       coe_main_content_top( is_front_page() ); ?>
<?php       if (have_posts()) : while (have_posts()) : the_post();
                if($post->post_content != "") : ?>
                    <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<?php                   coe_entry_before(); ?>
                        <div class="entry">
<?php                       coe_entry_top(); ?>
<?php                       the_content(__('<p class="serif">Read the rest of this page &raquo;</p>', 'coe')); ?>
<?php                       coe_entry_bottom(); ?>
                        </div>
<?php                   coe_entry_after(); ?>
                    </div>
<?php           endif;
            endwhile; endif; ?>

            <div class="clear"></div>

<?php       // Contact Fields...
            if ( $coe_options['show_contact_fields'] ) : ?>
                <div id="contactInfo">
<?php               foreach( $contact_field_array as $field_array ) :
                        if( $field_array['value'] != '' ) : ?>
                            <div class="grid_4 contactFieldDesc"><?php echo $field_array['desc']; ?></div>
                            <div class="grid_11 contactFieldValue"><?php echo $field_array['value']; ?></div>
                            <div class="clear"></div>
<?php                   endif;
                    endforeach; ?>
                </div>
                <div class="clear"></div>
<?php       endif; ?>

            <div id="contact-wrapper">
<?php           // Message Area.  It shows a message upon successful email submission
                if( isset( $emailSent ) && $emailSent == true ) : ?>
                    <div class="success">
                        <div class="msg-box-icon">
                            <strong><?php esc_html_e('Email Successfully Sent!', 'coe'); ?></strong><br />
                            <?php printf(__('Thank you <strong>%s</strong> for using our contact form! Your email was successfully sent and we will be in touch with you shortly.', 'coe'), $contact_name_thx) ?>
                        </div>
                    </div>
<?php           elseif ( isset( $emailSent ) && $emailSent == false ) : ?>
                    <div class="erroneous">
                        <div class="msg-box-icon">
                            <?php esc_html_e('Failed to connect to mailserver!', 'coe'); ?>
                        </div>
                    </div>
<?php           endif; ?>

                <form id="contactForm" class="cmxform" method="post" action="<?php echo the_permalink(); ?>#contact-wrapper">
                    <strong><?php esc_html_e('Please use the form below to send us an email:', 'coe'); ?></strong>
                    <div id="contact-name-fld-1">
                        <label for="contact_name"><?php esc_html_e('Name', 'coe'); ?> </label><em><?php esc_html_e('(required, at least 2 characters)', 'coe'); ?></em><br />
                        <input id="contact_name" name="contact_name" size="30" class="required<?php if(isset($nameError)) echo ' error'; ?>" minlength="2" value="<?php echo esc_attr($contact_name); ?>" />
                        <input type="hidden" id="rules_contact_message" value="<?php esc_html_e( 'required', 'coe' ); ?>" />
                        <input type="hidden" id="contact_name_required" value="<?php esc_html_e( 'Please enter a name', 'coe' ); ?>" />
                        <input type="hidden" id="contact_name_min_length" value="<?php esc_html_e( 'Your name must consist of at least 2 characters', 'coe' ); ?>" />
<?php                   if(isset($nameError)) echo '<label class="error" for="contact_name" generated="true">'.$nameError.'</label>'; ?>
                    </div>
                    <div id="contact-email-address-fld-1">
                        <label for="contact_email"><?php esc_html_e('E-Mail', 'coe'); ?> </label><em><?php esc_html_e('(required)', 'coe'); ?></em><br />
                        <input id="contact_email" name="contact_email" size="30"  class="required email<?php if(isset($emailError)) echo ' error'; ?>" value="<?php echo esc_attr($contact_email); ?>" />
                        <input type="hidden" id="messages_contact_email" value="<?php esc_html_e( 'Please enter a valid email address', 'coe' ); ?>" />
<?php                   if(isset($emailError)) echo '<label class="error" for="contact_email" generated="true">'.$emailError.'</label>'; ?>
                    </div>
                    <div id="contact-phone-ext-fld-1">
                        <label for="contact_phone"><?php esc_html_e('Phone', 'coe'); ?> </label><em><?php esc_html_e('(optional)', 'coe'); ?></em><br />
                        <input id="contact_phone<?php echo $NA_phone_format; ?>" name="contact_phone<?php echo $NA_phone_format; ?>" size="14" class="phone<?php if(isset($phoneError)) echo ' error'; ?>" value="<?php echo esc_attr($contact_phone); ?>" maxlength="14" />
                        <label for="contact_ext"><?php esc_html_e('ext.', 'coe'); ?> </label>
                        <input id="contact_ext<?php echo $NA_phone_format; ?>" name="contact_ext<?php echo $NA_phone_format; ?>" size="5" class="ext<?php if(isset($extError)) echo ' error'; ?>" value="<?php echo esc_attr($contact_ext); ?>" maxlength="5" />
<?php                   if(isset($phone_extError)) echo '<label class="error" for="contact_phone" generated="true">'.$phone_extError.'</label>'; ?>
                    </div>
                    <div id="contact-message-box-fld-1">
                        <label for="contact_message"><?php esc_html_e('Your comment', 'coe'); ?> </label><em><?php esc_html_e('(required)', 'coe'); ?></em><br />
                        <textarea id="contact_message" name="contact_message" cols="70" rows="7" class="required<?php if(isset($messageError)) echo ' error'; ?>"><?php echo esc_attr($contact_message); ?></textarea>
                        <input type="hidden" id="messages_contact_message" value="<?php esc_html_e( '<br />Please enter your message', 'coe' ); ?>" />
<?php                   if(isset($messageError)) echo '<br /><label class="error" for="contact_message" generated="true">'.$messageError.'</label>'; ?>
                    </div>

<?php               if ( $coe_options['recaptcha_enabled'] == 'yes' ) : ?>
                        <script type="text/javascript">var RecaptchaOptions = {theme : '<?php echo $coe_options['recaptcha_theme']; ?>', lang : '<?php echo $coe_options['recaptcha_lang']; ?>'};</script>
                        <div class="reCAPTCHA-wrapper">
<?php                       if ( $rCaptcha_error ) {
                                printf( __('%1$sThe reCAPTCHA was not entered correctly. Please try it again%2$s', 'coe'), '<div class="error">', '</div>' );
                            } ?>
<?php                       echo recaptcha_get_html( $publickey, $rCaptcha_error ); ?>
                        </div>
<?php               endif; ?>

                    <div>
                        <input name="submit" class="submit" type="submit" value="<?php esc_attr_e('Submit', 'coe'); ?>"/>
                    </div>
                </form>

            </div><!-- end contact-wrapper -->
<?php       coe_main_content_bottom(); ?>
	</div><!-- end main-content-padding -->
    </div><!-- end main-content -->


<?php if( ( !$coe_options['remove_contact_sidebar'] == 'yes' ) && sidebar_exist('ContactSidebar') ) { get_sidebar('ContactSidebar'); } ?>

</div><!-- end content-container -->

<div class="clear"></div>

<?php

get_footer();




