<?php
include("../config.php");
session_start();

if ($_POST['fields']) {
	if ($_SESSION['form_builder']) {
	$smarty->display($skin.'/header.tpl');
	echo "Sorry, but you just sent an email moments ago. Please do not abuse the system. Thank you.<br />";
	$smarty->display($skin.'/footer.tpl');
	} else {

	require_once('../inc/recaptchalib.php');
	$resp = recaptcha_check_answer ($privatekey,
	                                $_SERVER["REMOTE_ADDR"],
	                                $_POST["recaptcha_challenge_field"],
	                                $_POST["recaptcha_response_field"]);

		if (!$resp->is_valid) {
		$smarty->display($skin.'/header.tpl');
		echo "Sorry but you put in the wrong answer for the math question. Try again please.";
		$smarty->display($skin.'/footer.tpl');
		} else {
		$yourName = 'Charlie Page';
		$subject = "Contact: ".$_POST['subject']."";
		$referringPage = $_SERVER['HTTP_REFERER'];

			while (list(, $i) = each ($_POST['fields'])) {
			$message .= $i.": ".$_POST[$i]."
			\r\n";
			}

			$headers = "From: ".stripslashes($_POST['name'])." <".$_POST['email'].">\r\n";
			$headers .= 'To: '.$_POST['receiver'].'>'."\r\n";
			$mail_it = mail($_POST['receiver'],$subject,$message,$headers);

			$smarty->display($skin.'/header.tpl');
			if ($mail_it == TRUE) {
			$_SESSION['form_builder'] = "yes";
			echo "Your e-mail has been sent out! We will respond shortly.<br />";
			} else {
			echo "Sorry but your e-mail could not be sent, we will work on the problem.<br />";
			}
			$smarty->display($skin.'/footer.tpl');
		}
	}
}
?>