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

<?php get_header(); ?>
<?php 
$t_id = 0;
global $current_user;
get_currentuserinfo(); 
?>
<?php if( have_posts() ) : while( have_posts() ) : the_post(); ?>


			<div id="content">
                
                <div class="article contact-intro">

					<h1><?php the_title(); ?></h1>
                    <?php edit_post_link( 'EDIT', '<div class="post-info"><span class="date">', '</span></div>' ); ?>
					<?php the_content(); ?>
                    
                </div>
                    
        <?php
		// form submitted
		if( isset( $_POST[ 'yps' ] ) && $_POST[ 'yps' ] == 'submitted' ) {
			$err_msg = '';
			// security error
			if( check_admin_referer( 'sofacontact' ) != 1 ) $err_msg .= '<h2>' . __( "Security Error!", "sofa_shoppr" ) . '</h2><p>' . __( "You don't have a permission to perform such action!", "sofa_shoppr" ) . '</p>';
			// name
			if( isset( $_POST[ 'yourname' ] ) && strlen( $_POST[ 'yourname' ] ) < 3 ) $err_msg .= '<h2>' . __( "Name Error!", "sofa_shoppr" ) . '</h2><p>' . __( "Name field is mandatory and can not contain less than 3 characters!", "sofa_shoppr" ) . '</p>';
			// message
			if( isset( $_POST[ 'message' ] ) && strlen( $_POST[ 'message' ] ) < 10 ) $err_msg .= '<h2>' . __( "Message Error!", "sofa_shoppr" ) . '</h2><p>' . __( "Message field is mandatory and can't contain less than 10 characters!", "sofa_shoppr" ) . '</p>';
			// validate human
			$sum = (int)$_POST[ 'spam' ];
			$v_1 = $_POST[ 'v_1' ]; 
			$v_1 = str_replace( 'sx', '', $v_1 );
			$v_2 = $_POST[ 'v_2' ]; 
			$v_2 = str_replace( 'sy', '', $v_2 );
			$sum_now = (int)$v_1 + (int)$v_2;
			if( $sum != $sum_now ) $err_msg .= '<h2>' . __( "Spam prevention Error!", "sofa_shoppr" ) . '</h2><p>' . __( "Sum is not correct, check it out once again!", "sofa_shoppr" ) . '</p>';
			// validate email
			if( !is_email( $_POST[ 'email' ] ) ) $err_msg .= '<h2>' . __( "Email Error!", "sofa_shoppr" ) . '</h2><p>' . __( "Email field is mandatory or unknown (incorrect) e-mail address format.", "sofa_shoppr" ) . '</p>';
			
			// test passed?
			if( $err_msg == '' ) {
		?>
        
		<?php
            //Who do you want to be the recipient of the contact form
            $owner = get_userdata( 1 ); // to admin
            $to = $owner->user_email;
        
            //This file is responsible for sending the email, you shouldn't need to edit it beyond here.
        
            //User info (DO NOT EDIT!)
            $name = stripslashes( $_POST[ 'yourname' ] ); //sender's name
            $email = stripslashes( $_POST[ 'email' ] ); //sender's email
            $entrysocial = stripslashes( $_POST[ 'entrysocial' ] ); // whatever
        
            //The subject
            $subject = '[ WEB Form - ' . get_bloginfo( 'name' ) . ' ]'; //Subject will include your blogs title for easy recognition.
            $message = stripslashes( $_POST[ 'message' ] );
        
            //The message you will receive in your mailbox
            //Each parts are commented to help you understand what it does exaclty.
            //YOU DON'T NEED TO EDIT IT BELOW BUT IF YOU DO, DO IT WITH CAUTION!
            
            // create proper headers so non-ascii characters are displayed properly
            $headers = "Content-Type: text/plain; charset=utf-8; format=flowed\r\n"
            . "Content-Transfer-Encoding: 8bit\r\n"
            . "MIME-Version: 1.0\r\n"
            . "From: $email\r\n"
            . "Reply-To: $email\r\n"
            . "Return-Path: $email\r\n";
        
            $msg  = "From : $name\r\n";  //add sender's name to the message
            $msg .= "e-Mail : $email\r\n";  //add sender's email to the message
            $msg .= "Subject : $subject\r\n\n"; //add subject to the message (optional! It will be displayed in the header anyway)
            $msg .= "---Message---\r\n$message\r\n\n";  //the message itself
        
            //Extras: User info (Optional!)
            //Delete this part if you don't need it
            //Display user information such as Ip address and browsers information...
            $msg .= "---User information---\r\n"; //Title
            $msg .= "User IP : " . $_SERVER[ "REMOTE_ADDR" ] . "\r\n"; //Sender's IP
            $msg .= "User other contact : " . ( ( $entrysocial ) ? $entrysocial : 'Not available' ) . "\r\n"; //Sender's contact
        
            if ( @mail( $to, $subject, $msg, $headers ) ) {
                echo '<div class="article contact-intro"><h2>' . __( "Message sent successfully!", "sofa_shoppr" ) . '</h2><p>' . __( "Thanks", "sofa_shoppr" ) . ' <b>' . $name . '</b>, ' . __( "your message has been sent. We'll get back with an answer as soon as possible.", "sofa_shoppr" ) . '</p></div>';
            }
            else {
                echo '<div class="article contact-intro"><h2>' . __( "Summary:", "sofa_shoppr" ) . '</h2><p>' . __( "Sorry", "sofa_shoppr" ) . ' <b>' . $name . '</b>, ' . __( "sending your message failed. Try again later!", "sofa_shoppr" ) . '</p></div>';
            }
        ?>
        
        <?php 
			} else {
				// test not passed
				echo '<div class="article contact-intro">';
				echo '<p style="color: red;">' . __( "Your message is not delivered because of the following reason(s):", "sofa_shoppr" ) . '</p>';
				echo $err_msg;
				echo '</div>';
			}
		} else { 
					$t_id = get_permalink( $post->ID );
		?>
                    
					<form id="contactform" method="post" action="<?php echo $t_id; ?>">
                    <p style="display: none;">
                    <?php wp_nonce_field( 'sofacontact' ); ?>
                    </p>
						<p>
							<label for="yourname">* <?php _e( "Name", "sofa_shoppr" ); ?></label>
							<input type="text" size="45" id="yourname" name="yourname" />
						</p>

						<p>
							<label for="email">* <?php _e( "E-mail address", "sofa_shoppr" ); ?></label>
							<input type="text" size="45" id="email" name="email" />
						</p>
						<p>
							<label for="entrysocial"><?php _e( "Phone/Cell/Skype", "sofa_shoppr" ); ?></label>
							<input type="text" size="45" id="entrysocial" name="entrysocial" />
						</p>
						<p>
							<label for="message">* <?php _e( "Message", "sofa_shoppr" ); ?></label>
							<textarea rows="6" cols="52" id="message" name="message"></textarea>
						</p>
                        <p>
                           <?php
						   $v_1 = rand( '1', '10' );
						   $v_2 = rand( '1', '10' );
						   ?>
	                       <label for="spam">* <?php echo $v_1 . ' + ' . $v_2 . ' = '; ?>
	                       <input type="text" name="spam" id="spam" size="2" value="" class="input" />
                           </label>
                        </p>
                        <p style="display: none;">
						   <input type="hidden" name="v_1" value="sx<?php echo $v_1; ?>" />
						   <input type="hidden" name="v_2" value="sy<?php echo $v_2; ?>" />
                           <input type="hidden" name="yps" id="yps" value="submitted" />
                        </p>
                        <p style="display: none;">
                        <input type="hidden" value="" name="comment_post_ID"/>
                        </p>
						<p class="submitMe">
							<button type="submit" value="Submit" id="submit" name="submit"><span><?php _e( "SUBMIT", "sofa_shoppr" ); ?></span></button>
						</p>
					</form>
                    
        <?php
		}
		?>
       
<?php endwhile; endif; ?>

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

<?php include( TEMPLATEPATH . '/sidebar_contact.php' ); ?> 

<?php get_footer(); ?>
		   