// Check if user already submitted earlier
let formSubmitted = localStorage.getItem("fib4_form_submitted") === "true";
// Auto-fill country and city
function loadLocation() {
fetch("https://api.db-ip.com/v2/free/self/")
.then(res => res.json())
.then(info => {
$("#country").val(info.countryName || "null");
$("#city").val(info.city || "null");
})
.catch(() => {
fetch("https://ipinfo.io/json?token=36c49d56a01964")
.then(res => res.json())
.then(info => {
$("#country").val(info.country || "null");
$("#city").val(info.city || "null");
});
});
}
loadLocation();
// OPEN MODAL ONLY after FIB-4 is calculated
function calculateFIB4() {
const age = parseFloat($('#age').val());
const ast = parseFloat($('#ast').val());
const alt = parseFloat($('#alt').val());
const platelet = parseFloat($('#platelet').val());
const resultEl = $('#result');
if (!age || !ast || !alt || !platelet) {
resultEl.removeClass().html('
⚠️ Please fill in all values.
');
return;
}
const fib4 = (age * ast) / (platelet * Math.sqrt(alt));
const fib4Rounded = parseFloat(fib4.toFixed(2));
window.finalFibScore = fib4Rounded;
// If already submitted → DIRECTLY show result (no alert, no modal)
if (formSubmitted) {
displayFinalResult(window.finalFibScore);
return;
}
// otherwise, ask for email
$('#emailModal').modal('show');
}
// FORM SUBMISSION
$('#pipedriveForm').on('submit', function (e) {
e.preventDefault();
if (formSubmitted) return; // silent block
const email = $("#email").val();
const captcha = grecaptcha.getResponse();
if (!captcha) {
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": captcha,
source: "Sales Tool",
};
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)
});
// Permanently block resubmission on this device
formSubmitted = true;
localStorage.setItem("fib4_form_submitted", "true");
$('#emailModal').modal('hide');
setTimeout(() => {
displayFinalResult(window.finalFibScore);
}, 300);
});
// Show result
function displayFinalResult(score) {
const resultEl = $('#result');
let message = "";
let cssClass = "";
if (score < 1.3) {
cssClass = "category-low";
message = `FIB-4 Score: ${score}
Low risk of advanced fibrosis.`;
} else if (score >= 1.3 && score <= 2.67) {
cssClass = "category-indeterminate";
message = `FIB-4 Score: ${score}
Indeterminate — further testing may be needed.`;
} else {
cssClass = "category-high";
message = `FIB-4 Score: ${score}
High risk — consult a hepatologist.`;
}
resultEl.removeClass().addClass("result " + cssClass).html(message);
}