<?php   
require 'contactfunctions.php';
session_start();

///////////////  process form

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	
	$form = ($_POST["form"]);	
	$token = ($_POST["token"]);
	
	// echo $token .'<br>';
	// print_r( $_SESSION );
	// echo 'verifyFormToken =' . verifyFormToken('contactForm') ? 'true' : 'false';
	// echo '<br>verifyFormToken =' . verifyFormToken('contactForm') ? 'true' : 'false';
	
	
	if ( verifyFormToken('contactForm') ) {
		
		$fname = stripcleantohtml($_POST["firstname"]);	
		$email = stripcleantohtml($_POST["email"]);	
		$name = stripcleantohtml($_POST["name"]);
		$phone = stripcleantohtml($_POST["phone"]);
		$note = stripcleantohtml($_POST["txtArea"]);

		
		$noteS = strtolower($note);
			
		if($fname!='') {
			$url =  '../contact.php?error=errValC';
					header("location: $url"); 
					exit(); 
			
		}
		else {
				
			if(matchNotes($needles, $noteS)){
				$url =  '../contact.php?error=errValC';
				header("location: $url"); 
				exit(); 

			}
			else {
					
				////////////////// validate form fields
				
				/////// validate phone
				$phoneS = preg_replace("/[^0-9]/", '', $phone);
				$number_digits = count_digit($phoneS);
				$first_digit = substr($phoneS, 0, 1);
				
				
				if((($number_digits==11) ) && ($first_digit!='1')){
					
								$to = 'ewa@webmastersdesktop.com';
								$subject = 'Martin the Roofer Form Wrong Number';
								$headers  = 'MIME-Version: 1.0' . "\r\n";
								//$headers .= "Bcc: remik@webmastersdesktop.com\r\n";
								$message = "Wrong number is: $phone ";
					mail($to, $subject, $message, $headers);
					
					$url =  '../contact.php?error=errValPhone';
								header("location: $url"); 
								exit(); 
					
				}
				else {
						
						if(empty($name) || ($name =='name')){
							$url =  '../contact.php?error=errName#newsf';
							header("location: $url"); 
							exit();
						}

								
						if(empty($phone) || ($phone =='phone')){
							$url =  '../contact.php?error=errPhone#newsf';
							header("location: $url"); 
							exit();
						}
						else {
							if((!preg_match("/^[0-9]{10}$/", $phoneS)) && (!preg_match("/^[0-9]{7}$/", $phoneS)) && (!preg_match("/^[0-9]{11}$/", $phoneS))) {
								$url =  '../contact.php?error=errValPhone#newsf';
								header("location: $url"); 
								exit();
							}	
						}
					
					
						//////// validate email
						if(empty($email) || ($email =='email')){
							$url =  '../contact.php?error=errEmail#newsf';
							header("location: $url"); 
							exit(); 
						} else {

							if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
								$url =  '../contact.php?error=errValEmail#newsf';
								header("location: $url"); 
								exit(); 
							}
						}
					
						//// validate note
						
						if($note=='') {
							$url =  '../contact.php';
							header("location: $url"); 
							exit(); 
						}
					
					
					////////////////// email data
					
						date_default_timezone_set('America/New_York');
						$date = date("F j, Y, g:i a");
						
						$message = "Date: $date <br><br><hr>";
						$message .= "Name: $name <br>"; 
						$message .= "Email: $email <br>";  
						$message .= "Phone: $phone <br>"; 
						$message .= "Message: $note <br>";  
				
						$to = 'rencontractingllc@gmail.com';
						//$to = 'ewa@webmastersdesktop.com';
						$subject = 'Martin the Roofer Contact Form Request';
						$headers  = 'MIME-Version: 1.0' . "\r\n";
						$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
						$headers .= 'From: no-reply@martintheroofer.com' . "\r\n";
						$headers .= "Bcc: remik@webmastersdesktop.com,ewa@webmastersdesktop.com\r\n";

						$headers .= "From:   <".$email.">\r\n";

			// echo $message; die();
			
						mail($to, $subject, $message, $headers);
						
						$url = '../contact.php?error=val#newsf';
						header("location: $url"); 
						exit();	
					
				} // end note validated
			} //end digit validation
		} //end first name validated
	} //end spr toki
	else {
		// die('5');
		writeLog('Formtoken');

	}
}
				



?>