<?php 

/* 
Template Name: Contact Us 
*/ 

?>

<?php 
	// Fetch options stored in $data
	global $data;

 /**
//  * Sends mail by SMTP or mail(), depending on options
//  *
//  * @global array $data
//  * @param array $from array('name' => string, 'email' => string)
//  * @param array $to array('name' => string, 'email' => string)
//  * @param array $replyto array('name' => string, 'email' => string)
//  * @param string $subject
//  * @param string $body
//  */
// if (!function_exists('qns_mail')) {
// function qns_mail($from, $to, $replyto, $subject, $body) {
// 	// import options
// 	global $data;
// 	// determine whether to send by SMTP
// 	if (!empty($data['enable_smtp']) && !empty($data['smtp_host']) && !empty($data['smtp_port'])) {
// 	// convert encryption
// 	$smtp_encryption = '';
// 	switch($data['smtp_encryption']) {
// 		case 'No encryption': $smtp_encryption = ''; break;
// 		case 'SSL encryption': $smtp_encryption = 'ssl'; break;
// 		case 'TLS encryption': $smtp_encryption = 'tls'; break;
// 		default: $smtp_encryption = '';
// 	}
// 	// instantiate mailer
// 	global $phpmailer;
// 	if (!is_object($phpmailer) || !is_a($phpmailer, 'PHPMailer')) {
// 		require_once ABSPATH . WPINC . '/class-phpmailer.php';
// 		require_once ABSPATH . WPINC . '/class-smtp.php';
// 	}
// 	$phpmailer = new PHPMailer(true); // clear out any previous settings
// 	$phpmailer->isSMTP();
// 	$phpmailer->Host = $data['smtp_host'];
// 	$phpmailer->Port = $data['smtp_port'];
// 	$phpmailer->SMTPSecure = $smtp_encryption;
// 	// determine whether to send with authentication
// 	if (!empty($data['smtp_auth']) && !empty($data['smtp_username'])) {
// 		$phpmailer->SMTPAuth = TRUE;
// 		$phpmailer->Username = $data['smtp_username'];
// 		$phpmailer->Password = $data['smtp_password'];
// 	} else {
// 		$phpmailer->SMTPAuth = FALSE;
// 	}
// 	$phpmailer->From = $from['email'];
// 	$phpmailer->FromName = $from['name'];
// 	$phpmailer->clearAllRecipients();
// 	$phpmailer->AddAddress($to['email']);
// 	$phpmailer->AddReplyTo($replyto['email']);
// 	$phpmailer->Subject = $subject;
// 	$phpmailer->CharSet = 'UTF-8';
// 	$phpmailer->Body = $body;
// 	$phpmailer->IsHTML(TRUE);
// 
// 	// send
// 	try {
// 		$phpmailer->Send();
// 	} catch (Exception $ex) {
// 		wp_die($ex->getMessage());
// 	}
// 	unset($phpmailer);
// 	} else {
// 		$headers = <<<EOT
// MIME-Version: 1.0
// Content-type: text/html; charset=UTF-8
// From: "{$from['name']}" <{$from['email']}>
// Reply-To: {$replyto['email']}
// EOT;
// 		mail($to['email'], $subject, $body, $headers);
// 	}
// } // end function
// } // end if
// 
 ?>
 
 <?php
// 
	// Email Contact Form
// 	$name_error = '';
// 	$email_error = '';
// 	$contact_error = '';
// 	
// 	if(isset($_POST['sent'])) {
// 		
// 		if(trim($_POST['contact_name']) === '') {
// 			$name_error = __('Name is a required field', 'qns');
// 			$got_error = true;
// 		} 
// 	
// 		else {
// 			$name = trim($_POST['contact_name']);
// 		}
// 		
// 		if(trim($_POST['email']) === '')  {
// 			$email_error = __('Email is a required field', 'qns');
// 			$got_error = true;
// 		} 
// 	
// 		else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
// 			$email_error = __('Invalid email address', 'qns');
// 			$got_error = true;
// 		} 
// 	
// 		else {
// 			$email = trim($_POST['email']);
// 		}
// 			
// 		if(trim($_POST['message']) === '') {
// 			$contact_error = __('Message is a required field', 'qns');
// 			$got_error = true;
// 		} 
// 	
// 		else {
// 		
// 			if(function_exists('stripslashes')) {
// 				$message = stripslashes(trim($_POST['message']));
// 			} 
// 		
// 			else {
// 				$message = trim($_POST['message']);
// 			}
// 		
// 		}
// 			
// 		if(!isset($got_error)) {
// 		
// 			$email_to = $data['contact_email'];
// 		
// 			if (!isset($email_to) || ($email_to == '') ){
// 				$email_to = get_option('admin_email');
// 			}
// 		
// 		$subject = htmlspecialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) . __(' Contact Form','qns');
// 		$body = $message . "<br><br>" . __('IP Address','qns') . ": " . $_SERVER['REMOTE_ADDR'];
// 
// 		qns_mail(
// 			array('name' => $name, 'email' => $email_to), // from
// 			array('name' => '', 'email' => $email_to), // to
// 			array('name' => '', 'email' => $email), // replyto
// 			$subject,
// 			$body
// 		);
// 
// 		$emailSent = true;
// 		
// 	}
// 	
// } ?>

<?php get_header(); ?>
	
	<?php //Display Page Header
		global $wp_query;
		$postid = $wp_query->post->ID;
		echo page_header( get_post_meta($postid, 'qns_page_header_image', true) );
		wp_reset_query();
	?>
	
	<!-- BEGIN .section -->
	<div class="section clearfix">
		
		<!-- BEGIN .main-content -->
		<div class="<?php echo sidebar_position('primary-content'); ?>">
			
			<h2 class="page-title"><?php the_title(); ?></h2>
					
			<!-- BEGIN .page-content -->
			<div class="page-content">

				<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

					<?php 		
						$street_address = ''; 
						$phone_number = '';
						$email_address = '';
					
						if ( $data['street_address'] ) {
							$street_address = $data['street_address'];
						}
					
						if ( $data['phone_number'] ) {
							$phone_number = $data['phone_number'];
						}
					
						if ( $data['email_address'] ) {
							$email_address = $data['email_address'];
						}	
					?>
				
					<?php // No Content
						if ( $post->post_content == "" ) { $no_the_content = true; } else { $no_the_content = false; }
					?>
				
					<?php // No Contact Details
						if ( !$street_address && !$phone_number && !$email_address ) { $no_details = true; } else { $no_details = false; }
					?>
				
					<?php // Some Contact Details
						if ( $street_address !== '' or $phone_number !== '' or $email_address !== '' ) { $some_details = true; } else { $some_details = false; }
					?>
				
					<?php // No Content, Some Contact Details
						if ( $some_details == true && $no_the_content == true ) { ?>
				 		
							
						
							
					
						<?php }	?>
				
						<?php // Content, No Contact Details
						if ( $no_details == true && $no_the_content == false ) { 
							the_content(); 
						} ?>
				
						<?php // Content and Contact Details
							if ( $some_details == true && $no_the_content == false ) { ?>
					
								<div class="clearfix">
									
										<?php the_content(); ?>
									
							
									
								</div>
					
							<?php } ?>
				
						<?php endwhile; endif; ?>
		
						<?php 
							// If the Google Maps option is selected display the map
							if ( $data['map_address'] ) {
								
								$map_lat = $data['gmap-top-lat'];
								$map_lng = $data['gmap-top-lng'];
								
								if ( $data['gmap-top-content'] ) {
									$map_content = 'marker_html="' . $data['gmap-top-content'] . '"';
								}
								
								echo do_shortcode('[googlemap height="250px" latitude="' . $map_lat . '" longitude="' . $map_lng . '" marker_latitude="' . $map_lat . '" marker_longitude="' . $map_lng . '" ' . $map_content . ' marker_popup=false]');
							}

							
							
						?>

					<!-- END .page-content -->
					</div>	
					
				<!-- END .main-content -->
				</div>
			
			<?php get_sidebar(); ?>
		
		<!-- END .section -->		
		</div>

<?php get_footer(); ?>