<?php 

	if (!isset($_SESSION)) session_start(); 
	if(!$_POST) exit;
	
	// Enter your name or company name below
	$receiver_name = "Trapezoid Limited";
	
	// Enter email address below for receiving the form
	// All Contact messages will be sent there
	$receiver_email = 'info@trapezoidlimited.com';
	
	// Enter email subject below
	// This will be your message subject
	$msg_subject = "Contact Us";
	
	$sendername = strip_tags(trim($_POST["sendername"]));	
	$senderemail = strip_tags(trim($_POST["senderemail"]));
	$sendernumber = strip_tags(trim($_POST["sendernumber"]));
	$sendermessage = strip_tags(trim($_POST["sendermessage"]));
    //$securitycode = strip_tags(trim($_POST["securitycode"]));
	
	/*
	========================================
	Start server side validation
	========================================
	*/ 
	$errors = array();
	 //validate name
	if(isset($_POST["sendername"])){
	 
			if (!$sendername) {
				$errors[] = "You must enter a name.";
			} elseif(strlen($sendername) < 1)  {
				$errors[] = "Name is required.";
			}
	 
	}
	//validate email address
	/*if(isset($_POST["senderemail"])){
		if (!$senderemail) {
			$errors[] = "You must enter an email.";
		} else if (!validEmail($senderemail)) {
			$errors[] = "Your must enter a valid email.";
		}
	}*/
	
	//validate subject
	if(isset($_POST["sendernumber"])){
			if (!$sendernumber) {
				$errors[] = "You must enter a phone number.";
			} elseif(strlen($sendernumber) < 1)  {
				$errors[] = "Enter a VALID Phone number.";
			}
	}
	
	//validate message / comment
	if(isset($_POST["sendermessage"])){
		if (strlen($sendermessage) < 1) {
			if (!$sendermessage) {
				$errors[] = "You must enter a message.";
			} else {
				$errors[] = "Message is required.";
			}
		}
	}
	
	//validate security captcha
	//if(isset($_POST["securitycode"])){
	//	if (!$securitycode) {
	//		$errors[] = "You must enter the security code";
	//	} else if (md5($securitycode) != $_SESSION['smartCheck']['securitycode']) {
	//		$errors[] = "The security code you entered is incorrect.";
	//	}
	//}
	
	if ($errors) {
		//Output errors in a list
		$errortext = "";
		foreach ($errors as $error) {
			$errortext .= '<li>'. $error . "</li>";
		}
	
		echo '<div class="alert notification alert-error">The following errors occured:<br><ul>'. $errortext .'</ul></div>';
	
	} else{
	
		require "PHPMailerAutoload.php";
		require "smartmessage.php";
			
		$mail = new PHPMailer();
		$mail->isMAIL();
		$mail->IsHTML(true);
		$mail->From = $senderemail;
		$mail->CharSet = "UTF-8";
		$mail->FromName = $sendername;
		$mail->Encoding = "base64";
		$mail->Timeout = 200;
		$mail->ContentType = "text/html";
		$mail->addAddress($receiver_email, $receiver_name);
		$mail->Subject = $msg_subject;	
		$mail->Body = $message;
		$mail->AltBody = "Use an HTML compatible email client";
				
		// For multiple email recepients from the form 
		// Simply change recepients from false to true
		// Then enter the recipients email addresses
		// echo $message;
		$recipients = false;
		if($recipients == true){
			$recipients = array(
				"info@trapezoidlimited.com" => "Recipient Name",
				"address@example.com" => "Recipient Name",
			);
			
			foreach($recipients as $email => $name){
				$mail->AddBCC($email, $name);
			}	
		}
		
		if($mail->Send()) {
		  echo '<div class="alert notification alert-success">Thank you for your message!!! We will get in touch with you. </div> '; 
		} else {
		  echo '<div class="alert notification alert-error">Sorry! an error occurred while sending!</div> ';
		}
	}
	// end error array if	
	// ultimate email validation function
	function validEmail($senderemail) {
		$isValid = true;
		$atIndex = strrpos($senderemail, "@");
		if (is_bool($atIndex) && !$atIndex) {
			$isValid = false;
		} else {
			$domain = substr($senderemail, $atIndex + 1);
			$local = substr($senderemail, 0, $atIndex);
			$localLen = strlen($local);
			$domainLen = strlen($domain);
			if ($localLen < 1 || $localLen > 64) {
				// local part length exceeded
				$isValid = false;
			} else if ($domainLen < 1 || $domainLen > 255) {
				// domain part length exceeded
				$isValid = false;
			} else if ($local[0] == '.' || $local[$localLen - 1] == '.') {
				// local part starts or ends with '.'
				$isValid = false;
			} else if (preg_match('/\\.\\./', $local)) {
				// local part has two consecutive dots
				$isValid = false;
			} else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
				// character not valid in domain part
				$isValid = false;
			} else if (preg_match('/\\.\\./', $domain)) {
				// domain part has two consecutive dots
				$isValid = false;
			} else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\", "", $local))) {
				// character not valid in local part unless
				// local part is quoted
				if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\", "", $local))) {
					$isValid = false;
				}
			}
			if ($isValid && !(checkdnsrr($domain, "MX") || checkdnsrr($domain, "A"))) {
				// domain not found in DNS
				$isValid = false;
			}
		}
		return $isValid;
	}
?>