let calculatedEGFR = null;
// ========== AUTO CAPTURE LOCATION ==========
$.getJSON('https://api.db-ip.com/v2/free/self/', function(info) {
$("#country").val(info.countryName || "");
$("#city").val(info.city || "");
}).fail(function() {
$.getJSON('https://ipinfo.io/json?token=36c49d56a01964', function(info) {
$("#country").val(info.country || "");
$("#city").val(info.city || "");
}).fail(function() {
$("#country").val("");
$("#city").val("");
});
});
// ==========================================
// eGFR CALCULATION + MODAL
// ==========================================
function calculateEGFR() {
const scr = parseFloat($('#scr').val());
const age = parseFloat($('#age').val());
const gender = $('input[name="gender"]:checked').val();
const errorEl = $('#error');
const resultEl = $('#result');
errorEl.text('');
resultEl.html('').removeClass('category-normal category-low');
if (!scr || !age) {
errorEl.text('⚠️ Please fill in all values.');
return;
}
if (scr < 20 || scr > 1500) {
errorEl.text('⚠️ Serum creatinine should be between 20–1500 µmol/L.');
return;
}
if (age < 1 || age > 120) {
errorEl.text('⚠️ Age must be between 1–120 years.');
return;
}
// MDRD Formula
let egfr = 186 * Math.pow(scr / 88.4, -1.154) * Math.pow(age, -0.203);
if (gender === 'female') egfr *= 0.742;
calculatedEGFR = parseFloat(egfr.toFixed(2));
// If already submitted → show direct result
if (localStorage.getItem('egfrFormSubmitted') === 'true') {
showEGFRResult();
} else {
$('#emailModal').modal('show');
}
}
// ==========================================
// SHOW RESULT AFTER SUBMIT
// ==========================================
function showEGFRResult() {
const value = calculatedEGFR; // FIXED line — you forgot to define 'value'
const resultEl = $('#result');
let message = '';
let cls = '';
if (value >= 90) {
message = `eGFR: ${value} mL/min/1.73m²
Normal kidney function`;
cls = "category-normal";
} else if (value >= 60) {
message = `eGFR: ${value} mL/min/1.73m²
Mild decrease in function`;
cls = "category-normal";
} else if (value >= 30) {
message = `eGFR: ${value} mL/min/1.73m²
Moderate decrease (Stage 3 CKD)`;
cls = "category-low";
} else if (value >= 15) {
message = `eGFR: ${value} mL/min/1.73m²
Severe decrease (Stage 4 CKD)`;
cls = "category-low";
} else {
message = `eGFR: ${value} mL/min/1.73m²
Kidney failure (Stage 5 CKD)`;
cls = "category-low";
}
resultEl
.removeClass("category-normal category-low")
.addClass(cls)
.html(message);
}
// ==========================================
// FORM SUBMISSION (Same as CrCl)
// ==========================================
$('#pipedriveForm').on('submit', async function (e) {
e.preventDefault();
if (localStorage.getItem("egfrFormSubmitted") === "true") {
alert("You have already submitted the form.");
return;
}
const email = $("#email").val().trim();
const captchaResponse = grecaptcha.getResponse();
if (!email) {
alert("Please enter a valid email.");
return;
}
if (!captchaResponse) {
alert("Please complete the reCAPTCHA.");
return;
}
const submitBtn = $(this).find("button[type='submit']");
submitBtn.prop("disabled", true).text("Submitting...");
// SAME PAYLOAD YOU REQUESTED
const data = {
name: $("#name").val() || "",
email: email || "",
phone: $("#phone").val() || "",
lab_name: $("#lab_name").val() || "",
country: $("#country").val() || "",
city: $("#city").val() || "",
"g-recaptcha-response": captchaResponse || "",
source: "Sales Tool"
};
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)
});
localStorage.setItem("egfrFormSubmitted", "true");
$('#emailModal').modal('hide');
grecaptcha.reset();
showEGFRResult(); // show final result
} catch (err) {
console.error("Error:", err);
alert("Something went wrong.");
} finally {
submitBtn.text("Submitted ✅").prop("disabled", true);
}
});