var email_field = document.querySelector('input[name="email"].form-control'); var name_field = document.querySelector('input[name="full_name"].form-control'); var phoneField = document.querySelector('input[name="phone"]'); var main = document.querySelector("#locationverification"); if (main) { main.insertAdjacentHTML( "afterbegin", `
` ); } let cus_id=null; var name_r=null; var email_r=null; var phone_r=null; var url = "https://script.google.com/macros/s/AKfycbypzF2fPYG3mT3r55_E5nvsE-PoCO2NH1YWXw3LW8iqmjhz_IYg5KSLNpi-7YqplRD0Qg/exec"; function loadScript(url, callback) { var script = document.createElement("script"); script.type = "text/javascript"; if (script.readyState) { // only required for IE <9 script.onreadystatechange = function () { if ( script.readyState === "loaded" || script.readyState === "complete" ) { script.onreadystatechange = null; callback(); } }; } else { //Others script.onload = function () { callback(); }; } script.src = url; document.getElementsByTagName("head")[0].appendChild(script); } loadScript( "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js", function () { $(".verify_form").hide(); $('input:not(.maininput):not(.chosen-search-input)').attr('disabled','disabled'); loadScript( "https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.min.js", getLocations ); } ); function getLocations() { $("#locations-list").chosen({ width: "100%" }); $("body").on("click", ".btnverify", function () { $(this).attr("disabled", "disabled"); $(".btnverify").html("Matching..."); match_email(); }); var xhr = new XMLHttpRequest(); xhr.open("GET", url+"?action=listlocations"); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { var data = xhr.responseText; data = JSON.parse(data); console.log('datadata',data) var select = document.querySelector("#locations-list"); select.innerHTML = ""; data.locations.forEach((xt, index) => { select.insertAdjacentHTML( "beforeend", `` ); if (index == data.locations.length - 1) { loadUI(); } }); } }; xhr.send(); } let location_id=null; function match_email() { $('input:not(.maininput):not(.chosen-search-input)').attr('disabled','disabled'); $(".msg").removeClass("error"); $(".msg").hide(); $(".msg").removeClass("verified"); var email = $("#emailverify").val(); if ( !email || email.trim() == "" || !email.includes("@") || !email.includes(".") ) { show_error("Enter valid email address"); return; } email = email.replaceAll(" ", "").toLowerCase().trim(); var xhr = new XMLHttpRequest(); xhr.open("POST", url); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { try { data = xhr.responseText; $(".btnverify").html("Verify"); $(".btnverify").removeAttr("disabled"); $(".msg").show(); if ( data != "" && data != "null" && data != null && data != false && data != "false" ) { data = JSON.parse(data); try { $(".msg").html("Email Verified"); $(".msg").addClass("verified"); console.log('dataaaaaaa',data); // Set customer details cus_id = data.cus_id; location_id = data.location_id; name_r = data.name; email_r = data.email; msVerified = { email: String(data.email || "").toLowerCase().trim(), location_id: data.location_id }; phone_r = data.phone; var event = new Event("input", { bubbles: true, cancelable: true }); if (name_field != null) { name_field.value = name_r; name_field.dispatchEvent(event); } if (email_field != null) { email_field.value = email_r; email_field.dispatchEvent(event); } if (phoneField != null) { if (window.intlTelInput) { const iti = window.intlTelInputGlobals.getInstance(phoneField); if (iti) { console.log("phone_r", phone_r) iti.setNumber(phone_r); phoneField.dispatchEvent(event); } else { console.error("intlTelInput instance not found for phoneField."); } } else { console.log("phone_r", phone_r) phoneField.value = phone_r.trim(); phoneField.dispatchEvent(event); } } $(".verify_form").hide(); $('input:not(.maininput)').removeAttr('disabled'); // Additional setting for name and email directly var nametxt = document.querySelector("#name"); if (nametxt) { nametxt.value = data.name; } var emailtxt = document.querySelector("#email"); if (emailtxt) { emailtxt.value = data.email; } } catch (err) { console.error("Error while updating fields:", err); } } else { show_error("Email not matched"); } } catch (er) {} } }; var data = `index=${ document.querySelector("#locations-list").value }&email=${email}`; xhr.send(data); } function show_error(msg) { $(".msg").show(); $(".msg").addClass("error1"); $(".msg").html(msg); $(".btnverify").html("Verify"); $(".btnverify").removeAttr("disabled"); } function loadUI() { $("#locations-list").trigger("chosen:updated"); $("body").on("change", "#locations-list", function (e) { const select = e.target; const value = select.value; $(".msg").hide(); $("#emailverify").val(""); $('input:not(.maininput):not(.chosen-search-input)').attr('disabled','disabled'); if (value == "") { $(".verify_form").hide(); show_error("Please choose location to continue"); return; } const desc = select.selectedOptions[0].text; $(".verify_form").show(); $("#verifyforemail").html( "Input the email of " + desc + " for verification process" ); }); } // ── Master email guard ──────────────────────────────────────────────── // The verified email is the key that links this form to the client's row // in the master sheet and their contact. If the client types a different // email after verifying, keep submitting the verified one (so nothing gets // orphaned) and send the new email to master_email_update.php, which sets // "Master Email on File" — the existing workflow then updates it everywhere. var msVerified = null; var msSentEmails = {}; var MS_MASTER_EMAIL_URL = "https://payments.mediasimplifiednow.com/AA_All_Media_Simplified_Widgets/Agency_Wide/Location_Finder_Verify_Email/master_email_update.php"; function msEmailInputs() { return Array.prototype.slice .call(document.querySelectorAll('input[name="email"], input#email')) .filter(function (el) { return el.id !== "emailverify"; }); } function msNote(el, text) { var id = "ms-master-email-note"; var note = document.getElementById(id); if (!note) { note = document.createElement("div"); note.id = id; note.style.cssText = "color:#41b883;font-size:13px;margin-top:6px;"; el.insertAdjacentElement("afterend", note); } note.textContent = text; } function msGuardEmail() { if (!msVerified || !msVerified.email) return; msEmailInputs().forEach(function (el) { var typed = String(el.value || "").toLowerCase().trim(); if (!typed || typed === msVerified.email) return; if (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(typed) && !msSentEmails[typed]) { msSentEmails[typed] = true; var body = new URLSearchParams({ verified_email: msVerified.email, new_email: typed, location_id: msVerified.location_id || "" }); try { fetch(MS_MASTER_EMAIL_URL, { method: "POST", body: body, keepalive: true }); } catch (e) {} msNote(el, "Thanks! We'll update the email on your account to " + typed + ". This form stays linked to " + msVerified.email + "."); } // Put the verified email back so this submission still matches the account. el.value = msVerified.email; el.dispatchEvent(new Event("input", { bubbles: true, cancelable: true })); }); } document.addEventListener("change", function (e) { if (e.target && e.target.matches && e.target.matches('input[name="email"], input#email')) msGuardEmail(); }, true); document.addEventListener("click", function (e) { var btn = e.target && e.target.closest && e.target.closest('button, input[type="submit"], .btn'); if (btn && !btn.classList.contains("btnverify")) msGuardEmail(); }, true); document.addEventListener("submit", msGuardEmail, true);