<?php 
$errors = '';
$myemail = 'service@irvingcarpetcleaningtx.com';//<-----Put Your email address here.
if(empty($_POST['name'])  || 
   empty($_POST['tele']))
{
    $errors .= "\n Error: all fields are required";
}

$name = $_POST['name']; 
$tele = $_POST['tele'];
$email = $_POST['email']; 
$city = $_POST['city'];
$zip = $_POST['zip'];
$special_request = $_POST['special_request']; 

if( empty($errors))
{
	$to = $myemail; 
	$email_subject = "Appointment Request From: $name";
	$email_body = "You have received a new appointment request. ".
	" Here are the details of the request:\n 
	
	Name : $name \n 
	Contact Number : $tele \n 
	E-Mail : $email \n
	City : $city \n
	Zip Code : $zip \n
	Special Request : \n $special_request"; 
	
	
	
	$headers = "From: $myemail\n"; 
	$headers .= "Reply-To: $email_address";
	
	mail($to,$email_subject,$email_body,$headers);
	//redirect to the 'thank you' page
	header('Location: contact-form-thank-you.html');
} 
# spam protection
if (isset($_POST["spam"]) && $_POST["spam"] == "") {
	mail( 'service@irvingcarpetcleaningtx.com', 'Contact Form', print_r($_POST,true) ); 
  } else {
	http_response_code(400);
	exit;
  }
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
  <!-- Google tag (gtag.js) -->
  <script async src="https://www.googletagmanager.com/gtag/js?id=G-4C2N97MTTF"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag() { dataLayer.push(arguments); }
    gtag('js', new Date());

    gtag('config', 'G-4C2N97MTTF');
  </script>
<title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
  <?php
echo nl2br($errors);
?>
<script>
        const dateToday = new Date();

        const date = new Date(dateToday);
        date.setDate(date.getDate() + 1);

        const monthName = date.toLocaleString('default', { month: 'long' });
        const currentYear = date.getFullYear();

        window.onload = function () {
            const dateSelect = document.getElementById('service_date');
            dateSelect.value = monthName;
            const yearElement = document.getElementById("service_year");
            yearElement.value = currentYear;
            AddMonthDays(monthName);
        }

        function AddMonthDays(monthNameValue) {
            let monthDays = 31;
            const currentYear = date.getFullYear();
            const dayOfMonth = date.getDate();
            if (monthNameValue == 'February') monthDays = 28;
            if (monthNameValue == 'February' && currentYear == '2028') monthDays = 29;
            if (monthNameValue == 'February' && currentYear == '2032') monthDays = 29;
            if (monthNameValue == 'February' && currentYear == '2036') monthDays = 29;
            if (monthNameValue == 'February' && currentYear == '2040') monthDays = 29;
            if (monthNameValue == 'February' && currentYear == '2044') monthDays = 29;
            if (monthNameValue == 'February' && currentYear == '2048') monthDays = 29;
            if (monthNameValue == 'April') monthDays = 30;
            if (monthNameValue == 'June') monthDays = 30;
            if (monthNameValue == 'September') monthDays = 30;
            if (monthNameValue == 'November') monthDays = 30;

            const selectElement = document.getElementById('service_day');
            selectElement.options.length = 0;
            const newOption = new Option('Pick a Day', '');
            selectElement.add(newOption);

            const monthDaysPlusOne = monthDays + 1;
            const SelectHtmlCode = '';
            for (let i = 1; i < monthDaysPlusOne; i++) {
                const newOption = new Option(i.toString(), i.toString());
                selectElement.add(newOption);
            }
            selectElement.value = dayOfMonth;
        }

        function validatePhoneNumber(phoneNumber) {
            const phoneNumberPattern = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
            const cleanedPhoneNumber = phoneNumber.replace(/\s/g, '');
            if (phoneNumberPattern.test(cleanedPhoneNumber)) {
                return;
            } else {
                alert('Please enter correct phone number format: ###-###-####');
            }
        }
    </script>
</body>
</html>