function sendEmail() {

  const form = document.getElementById('contact-us');
  let formData = new FormData(form);
  const xhr = new XMLHttpRequest();

  document.querySelector('.loading').classList.add('d-block');
  document.querySelector('.error-message').classList.remove('d-block');
  document.querySelector('.sent-message').classList.remove('d-block');

  const url = 'assets/js/mail.php'; // Replace with the URL of your server-side script
  xhr.open('POST', url);
  
  xhr.onload = function() {
    document.querySelector('.loading').classList.remove('d-block');
    if (xhr.status === 200) {
      console.log('Email sent successfully');
      document.querySelector('.sent-message').classList.add('d-block');
    } else {
      console.log('Error sending email');
      displayError('Form submission failed');
    }
  };
  console.log(formData);
  xhr.send(formData);
}

const form = document.getElementById('contact-us');
form.addEventListener('submit', function(event) {
  event.preventDefault(); // Prevent the form from submitting normally
  sendEmail(); // Call the sendEmail function
});

function displayError(error) {
  document.querySelector('.loading').classList.remove('d-block');
  document.querySelector('.error-message').innerHTML = error;
  document.querySelector('.error-message').classList.add('d-block');
}