let calculatedEGFR = null;
// ---------- Capture City + Country ----------
$.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("");
});
});
// ------------------- Calculate eGFR -------------------
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');
$('.stage').removeClass('active');
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;
}
let A, B, C;
if (gender === 'female') {
A = 61.9;
B = (scr <= A) ? -0.329 : -1.209;
C = 1.018;
} else {
A = 79.6;
B = (scr <= A) ? -0.411 : -1.209;
C = 1.0;
}
const egfr = 142 * Math.pow(scr / A, B) * Math.pow(0.9938, age) * C;
calculatedEGFR = parseFloat(egfr.toFixed(2));
// ⛔ If already submitted, show result directly
if (localStorage.getItem('pipedriveFormSubmitted_eGFR') === 'true') {
showEGFRResult();
} else {
$('#emailModal').modal('show');
}
}
// ---------------- Show eGFR Result ----------------
function showEGFRResult() {
const resultEl = $('#result');
resultEl.removeClass('category-normal category-low');
let message = "";
let stageId = "";
let category = "";
if (calculatedEGFR >= 90) {
message = `eGFR: ${calculatedEGFR} mL/min/1.73m²
Normal kidney function (Stage 1)`;
category = "category-normal";
stageId = "stage1";
} else if (calculatedEGFR >= 60) {
message = `eGFR: ${calculatedEGFR} mL/min/1.73m²
Mild decrease (Stage 2)`;
category = "category-normal";
stageId = "stage2";
} else if (calculatedEGFR >= 30) {
message = `eGFR: ${calculatedEGFR} mL/min/1.73m²
Moderate decrease (Stage 3)`;
category = "category-low";
stageId = "stage3";
} else if (calculatedEGFR >= 15) {
message = `eGFR: ${calculatedEGFR} mL/min/1.73m²
Severe decrease (Stage 4)`;
category = "category-low";
stageId = "stage4";
} else {
message = `eGFR: ${calculatedEGFR} mL/min/1.73m²
Kidney failure (Stage 5)`;
category = "category-low";
stageId = "stage5";
}
resultEl.addClass(category).html(message);
$('#' + stageId).addClass('active');
}
// ----------------- Submit Pipedrive Form -----------------
$('#pipedriveForm').on('submit', async function (e) {
e.preventDefault();
if (localStorage.getItem('pipedriveFormSubmitted_eGFR') === 'true') {
alert("You have already submitted the form.");
$('#emailModal').modal('hide');
showEGFRResult();
return;
}
const email = $('#email').val().trim();
const captchaResponse = grecaptcha.getResponse();
if (!email) {
alert("Please enter your email.");
return;
}
if (!captchaResponse) {
alert("Please complete the reCAPTCHA.");
return;
}
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('pipedriveFormSubmitted_eGFR', 'true');
$('#emailModal').modal('hide');
grecaptcha.reset();
showEGFRResult();
} catch (err) {
console.error(err);
alert("Something went wrong. Please try again later.");
}
});