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


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



		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            
            $form = ($_POST["fform"]);	
            $token = ($_POST["ftoken"]);
            
            //echo "$form, $token<br>";
            
            
            if (verifyFormToken('quoteForm')) {
                
	$email = stripcleantohtml($_POST["femail"]);	
    $name = stripcleantohtml($_POST["fname"]);
    $phone = stripcleantohtml($_POST["fphone"]);
    $note = stripcleantohtml($_POST["ftxtArea"]);

    
	$noteS = strtolower($note);
                
                    
                 if(match($needles, $noteS)){

							 $url =  '../index.php';
							header("location: $url"); 
							exit(); 
                     //echo "note invalid";
	 }
                
                else {
                    
                    $phoneS = preg_replace("/[^0-9]/", '', $phone);
              $number_digits = count_digit($phoneS);
              $first_digit = substr($phoneS, 0, 1);
                    
                    //echo "$phoneS<br>";
                    
                    if((($number_digits==11) ) && ($first_digit!='1')){
                  
                            $to = 'ewa@webmastersdesktop.com';
							$subject = 'Martin the Roofer Quote 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 =  '../index.php';
							header("location: $url"); 
							exit(); 
                  
              }
                    else {
                   
                   if(empty($name) || ($name =='name')){
							
									$url =  '../index.php?qerror=qerrName#quotef';
									header("location: $url"); 
									exit();
						}

						
						if(empty($phone) || ($phone =='phone')){
							
									$url =  '../index.php?qerror=qerrPhone#quotef';
									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 =  '../index.php?qerror=qerrValPhone#quotef';
									header("location: $url"); 
									exit();
								}	
						}
              
              
              //////// validate email
              if(empty($email) || ($email =='email')){

								$url =  '../index.php?qerror=qerrEmail#quotef';
								header("location: $url"); 
								exit(); 
						} else {

						if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
									$url =  '../index.php?qerror=qerrValEmail#quotef';
										header("location: $url"); 
										exit(); 
								}
							}
              
              
              
              
              
              //// validate note
              
              if($note=='') {
                $url =  '../index.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 = 'Renaissance Contracting Quote Form Request';
							$headers  = 'MIME-Version: 1.0' . "\r\n";
							$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
                            $headers .= "Bcc: remik@webmastersdesktop.com,ewa@webmastersdesktop.com\r\n";

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

                
                
							mail($to, $subject, $message, $headers);
							
							$url = '../index.php?qerror=qval#quotef';
							header("location: $url"); 
							exit();	
              
              
                  
              
              
          } // end note validated
                    
                }
                    
            
            
            
            
            } //end spr toki
            
             
            else {

                writeLog('Formtoken');

            }
            
           
        }
				



?>