var email = document.getElementById('id_inquiry-email');
var phone = document.getElementById('id_inquiry-phone');
if (!phone) {
    //if phone is undefined, email will be, too
    phone = document.getElementById('id_schedule_tour-phone');
    email = document.getElementById('id_schedule_tour-email');
}

function checkPhoneAndEmail() {
    if (email.value == "" && phone.value == "") {
      phone.setCustomValidity("Either a phone number or an email address must be provided.");
    } else if (phone.validity.patternMismatch) {
        phone.setCustomValidity('Phone number should consist of 10 digits');
    } else if (email.validity.patternMismatch) {
        email.setCustomValidity('Email should follow the format "test@example.com"');
    } else {
      phone.setCustomValidity("");
      email.setCustomValidity("");
    }
}

jQuery.fn.preventDoubleSubmission = function() {
  $(this).on('submit',function(e){ 
    e.stopImmediatePropagation();
    var $form = $(this);
    if ($form.data('submitted') === true) {
      // Previously submitted - don't submit again
      e.preventDefault();
    } else {
      // Mark it so that the next submit can be ignored
      $form.data('submitted', true);
    }
    // disable the submit button 
    $("#btnSubmit").attr("disabled", true);
  });

  // Keep chainability
  return this;
};

window.onload = function() {
  checkPhoneAndEmail();
  $('.form').preventDoubleSubmission();
  phone.addEventListener("input", checkPhoneAndEmail);
  email.addEventListener("input", checkPhoneAndEmail);
  phone.addEventListener("focusout", checkPhoneAndEmail);
  email.addEventListener("focusout", checkPhoneAndEmail);
}