var allow_submit = false;

// Captcha CSS --
function CaptchError(bool) {
  const captchaSpan = document.getElementById("captcha-error");
  captchaSpan.style.display = bool ? "block" : "none";
}
CaptchError(false);

function captcha_filled() {
  allow_submit = true;
  CaptchError(false);
}

function captcha_expired() {
  allow_submit = false;
}

function check_captcha_filled(e) {
  if (!allow_submit) {
    // alert("ERROR: Please verify you are human by filling out the captcha");
    CaptchError(true);
    setTimeout(() => {
      CaptchError(false);
    }, 5000);
    return false;
  }
  captcha_expired();
  return true;
}

document.getElementById("contact_form").addEventListener("submit", (e) => {
  e.preventDefault();
  check_captcha_filled() && sendMail();
  return false;
});

async function sendMail() {
  const subBTN = document.getElementById("contact_submit");
  subBTN.innerText = "Sending... Message";
  subBTN.disabled = true;

  let params = {
    subject: "Clinchsoft Digital - New Contact-US Form Submitted Wooo!",
    to_name: "Clinchsoft Digital",
    name: document.getElementById("contact_name").value,
    email: document.getElementById("contact_email").value,
    mobile: document.getElementById("contact_phone").value,
    c_subject: document.getElementById("contact_subject").value,
    reply_to: document.getElementById("contact_email").value,
    message: document.getElementById("contact_message").value,
    ccmail: "amolbhosale98@gmail.com", // UPDATE CC MAIL HERE
  };

  await emailjs
    .send("service_xzpxwnx", "template_oh4vqvx", params) // UPDATE SERVICE ID and TEMPLATE ID
    .then(async () => {
      window.location.href = "/thank-you.html";
      await document.getElementById("contact_form").reset();
    })
    .catch((e) => alert("Try again, something went wrong!"))
    .finally(() => {
      subBTN.innerText = "Send Message";
      subBTN.disabled = false;
    });
}

// $(document).ready(function () {
//   $("form").submit(function (event) {
//     event.preventDefault();
//     alert("Form Submitted!!!");
//     window.history.pushState("Thank you", "Thank you", "/thank-you.html");
//   });
// });
