<?php
    if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $phone = $_POST['phone'];
    $contact_person = $_POST['contact-person'];
    $address = $_POST['address'];
    $acquisition = $_POST['acquisition'];
    $acquiring_property = $_POST['acquiring-property'];
    $how_many_property = $_POST['how-many-property'];
    $financing = $_POST['financing'];
    $closing = $_POST['closing'];
   

    $to = "info@luvanexgroup.com";
    $email_form = 'sellyourhouse@luvanexgroup.com';
    $email_subject = "Investor Form";
    $email_body = "User Name: $name.\n".
                  "user Email: $visitor_email.\n".
                  "Phone: $phone.\n".
                  "Contact Person/Manager: $contact_person. \n".
                  "Address: $address.\n". 
                  "Preferred City: $acquisition. \n".
                  "Acquiring property for: $acquiring_property. \n".
                  "How many Properties can you purchase each month: $how_many_property. \n".
                  "Type of Financing: $financing. \n".
                  "How soon can you close: $closing. \n";


    
    $headers = "From: $email_form \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";

    
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    function post_captcha($user_response) {
        $fields_string = '';
        $fields = array(
            'secret' => '6LcNlegUAAAAAGKkIqihFlnl6-iyeyi3g0EAs7dj
            ',
            'response' => $user_response
        );
        foreach($fields as $key=>$value)
        $fields_string .= $key . '=' . $value . '&';
        $fields_string = rtrim($fields_string, '&');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

        $result = curl_exec($ch);
        curl_close($ch);

        return json_decode($result, true);
    }
        
    // Call the function post_captcha
    $res = post_captcha($_POST['g-recaptcha-response']);

    if ($res['success']) {
    // What happens when the CAPTCHA wasn't checked            
                
       
        mail($to,$email_subject,$email_body,$headers);
        
        $to      = $visitor_email;
        $email_form = 'info@luvanexgroup.com';
        $subject = 'Thanks for Contacting Us';
        $message = "Hello $name \n" .
                    "Thanks for filling our form, we will contact you within one or two working days." ;

        $headers = "Reply-To: $visitor_email \r\n";
        
        mail($to, $subject, $message, $headers);
            {
                header("Location: Thanks.html");
            } 
    
    } else {
    // If CAPTCHA is successfully completed...

    // Paste mail function or whatever else you want to happen here!

    echo "<script>
    alert('Invalid Captcha, please try again');
    window.location.href='investor.html';
    </script>";
    }
}
    }
?>