<?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 = 'abiodun.oni@generatione.net';
	
	// Enter email subject below
	// This will be your message subject
	$msg_subject = "Benchmarking Report";
	
	$sendername = strip_tags(trim($_POST["sendername"]));	
	$senderemail = strip_tags(trim($_POST["senderemail"]));
	$sendernumber = strip_tags(trim($_POST["sendernumber"]));
	$senderaddress = strip_tags(trim($_POST["senderaddress"]));
	$sendercompany = strip_tags(trim($_POST["sendercompany"]));
	$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 username.";
			} elseif(strlen($sendername) < 1)  {
				$errors[] = "username 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 address
	if(isset($_POST["senderaddress"])){
			if (!$senderaddress) {
				$errors[] = "You must enter your address.";
			} elseif(strlen($senderaddress) < 1)  {
				$errors[] = "address is required.";
			}
	}
	
	//validate company
	if(isset($_POST["sendercompany"])){
			if (!$sendercompany) {
				$errors[] = "You must enter your company.";
			} elseif(strlen($sendercompany) < 1)  {
				$errors[] = "company is required.";
			}
	}
	
	if(isset($_POST["sendermessage"])){
			if (!$sendermessage) {
				$errors[] = "You must enter your request.";
			} elseif(strlen($sendermessage) < 1)  {
				$errors[] = "Request 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{
	
		echo '<div class="alert notification alert-error">The following errors occured:<br><ul>'. $errortext .'</ul></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;
	}
?>