<?php

if(isset($_POST['Send']) || isset($_POST['ajax'])){
	$errorC = false;
	
	$name = $_POST['inputName'];
	$subject = $_POST['inputSubject'];
	$email = $_POST['inputEmail'];
	$content = nl2br($_POST['textareaContent']);
	
	$already_used = array('inputName','inputSubject','inputEmail','textareaContent','ajax','Send');
	$attach = '';
	foreach ($_POST as $key => $field)
	{
		if(!in_array($key,$already_used))
		{
			$attach.= $key.": ".$field."<br /> \n";
		}
	}
	$attach.= "<br /> \n";
	
	if($name == "")
	{
		$errorC = true;
		$nameClass = "has-error";
	}else{
		$nameClass = "has-success";
	}
	
	if($subject == "")
	{
		$errorC = true;
		$subjectClass = "has-error";
	}else{
		$subjectClass = "has-success";
	}
	
	if($content == "")
	{
		$errorC = true;
		$contentClass = "has-error";
	}else{
		$contentClass = "has-success";
	}
	
	if(!checkmymail($email))
	{
		$errorC = true;
		$emailClass = "has-error";
	}else{
		$emailClass = "has-success";
	}
	
	if($errorC == false) {
		$name = $_POST['inputName'];
		$subject = $_POST['inputSubject'];
		$email = $_POST['inputEmail'];
		$content = nl2br($_POST['textareaContent']);

		$to = 'sekretariat@bdeconstans.pl';

		$header  = 'MIME-Version: 1.0' . "\r\n";
		$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
		$header .= 'From:'. $email  . " \r\n";

		$message = "<html>
		<head>
			<title>$subject</title>
		</head>
		<body>
		Nowa wiadomość od  $name <br/>
		E-mail: $email<br />";

		$message .= "Temat wiadomości: $subject
		Wiadomość:<br />$content<br />
		</body>
		</html>";

		mail($to,
		$subject,
		$message,
		$header);
		
		if(isset($_POST['ajax'])){
		echo"<h3>Twoja wiadomość została wysłana!</h3><p> Dziękujemy!</p>";
		}
	}
}

function checkmymail($mailadresse){
	$email_flag=preg_match("!^\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$!",$mailadresse);
	return $email_flag;
}
?>