<?php
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {

	//declare our assets 
	$name = stripcslashes($_POST['name']);
	$email = stripcslashes($_POST['email']);
	$recipient = stripcslashes(reveal_email($_POST['recipient']));
	$comment = stripcslashes($_POST['message']);
	$subject = stripcslashes($_POST['subject']);
	$auto_reply_msg = stripcslashes($_POST['auto_reply_msg']);
	
	$name_label = stripcslashes($_POST['name_label']);
	$email_label = stripcslashes($_POST['email_label']);
	$message_label = stripcslashes($_POST['message_label']);
	
	$date = date('r');
	
	$headers = "From: $email" . PHP_EOL .
		    "Reply-To: $email" . PHP_EOL .
		    "MIME-Version: 1.0" . PHP_EOL . 
		    "Content-type: text/plain; charset=UTF-8" . PHP_EOL .
		    "X-Mailer: FormSnap Jr.";
$msg =  
"
$date

$name_label:	$name

$email_label:	$email

$message_label:

$comment 

===================================
$_SERVER[REMOTE_ADDR]
$_SERVER[HTTP_HOST]$_SERVER[PHP_SELF]
===================================";
			
	//send the email 
	if(mail($recipient, '=?UTF-8?B?'.base64_encode($subject).'?=', $msg, $headers))
	{
		echo 'success'; 
	}else{
		echo 'error';
	}
	
	if($_POST['auto_reply'] == 'true')
	{
		$headers = "From: ".$recipient . PHP_EOL .
		    "Reply-To: ".$recipient . PHP_EOL .
		    "MIME-Version: 1.0" . PHP_EOL . 
		    "Content-type: text/plain; charset=UTF-8" . PHP_EOL .
		    "X-Mailer: FormSnap Jr.";
		    
		mail($email, '=?UTF-8?B?'.base64_encode('Re: '.$subject).'?=', $auto_reply_msg, $headers);
	}

}


function reveal_email($recipient) {
    //$replace = array('.', '@');
    $find = array('nopicking', '.formsnapjr');
    $recipient = str_replace($find, '', $recipient);
    return $recipient ;
}

?>