let calculatedCrCl = null; // ✅ Capture city & country automatically $.getJSON('https://api.db-ip.com/v2/free/self/', function (info) { console.log("DB-IP response:", info); $("#country").val(info.countryName || ""); $("#city").val(info.city || ""); }).fail(function () { $.getJSON('https://ipinfo.io/json?token=36c49d56a01964', function (info) { console.log("IPInfo response:", info); $("#country").val(info.country || ""); $("#city").val(info.city || ""); }).fail(function () { console.log('Both API requests failed'); $("#country").val(""); $("#city").val(""); }); }); // ✅ CrCl Calculation Function function calculateCrCl() { const age = parseFloat($('#age').val()); const weight = parseFloat($('#weight').val()); const creatinine = parseFloat($('#creatinine').val()); const sex = $('#sex').val(); if (!age || !weight || !creatinine) { $('#result').removeClass('category-normal category-low') .html('⚠️ Please fill in all values.'); return; } let crcl = ((140 - age) * weight) / (72 * creatinine); if (sex === 'female') crcl *= 0.85; calculatedCrCl = parseFloat(crcl.toFixed(1)); // ✅ If form was already submitted, skip modal entirely if (localStorage.getItem('pipedriveFormSubmitted') === 'true') { showResult(); } else { $('#emailModal').modal('show'); } } // ✅ Function to Show Result function showResult() { const resultEl = $('#result'); resultEl.removeClass('category-normal category-low'); const categoryClass = calculatedCrCl >= 90 ? 'category-normal' : 'category-low'; resultEl.addClass(categoryClass); let message = `
Estimated CrCl: ${calculatedCrCl} mL/min
`; if (calculatedCrCl < 90) { message += `⚠️ CrCl is below normal, may indicate reduced kidney function.`; } resultEl.html(message); } // ✅ Form Submission $('#pipedriveForm').on('submit', async function (e) { e.preventDefault(); // ✅ If already submitted, block resubmission if (localStorage.getItem('pipedriveFormSubmitted') === 'true') { alert("You have already submitted the form."); $('#emailModal').modal('hide'); showResult(); return; } const email = $('#email').val().trim(); const res = grecaptcha.getResponse(); if (!email) { alert("Please enter a valid email address."); return; } if (!res) { alert("Please complete the reCAPTCHA."); return; } const submitBtn = $(this).find('button[type="submit"]'); submitBtn.prop('disabled', true).text('Submitting...'); // ✅ Always pass all fields as strings const data = { name: $("#name").val() || "", email: email || "", phone: $("#phone").val() || "", lab_name: $("#lab_name").val() || "", country: $("#country").val() || "", city: $("#city").val() || "", "g-recaptcha-response": res || "", source: "Sales Tool" }; console.log("Payload to send:", data); try { await fetch('https://livehealth.solutions/v4/on-boarding/pipedrive_capture/', { method: 'POST', mode: 'no-cors', redirect: 'follow', headers: { "Content-Type": "application/json" }, body: JSON.stringify(data) }); // ✅ Mark as permanently submitted localStorage.setItem('pipedriveFormSubmitted', 'true'); $('#emailModal').modal('hide'); grecaptcha.reset(); showResult(); } catch (error) { console.error("Error submitting:", error); alert("Something went wrong. Please try again later."); } finally { submitBtn.prop('disabled', true).text('Submitted ✅'); } });