document.addEventListener("DOMContentLoaded", function () {
const CRM_WEBHOOK_URL =
"https://services.leadconnectorhq.com/hooks/Hos8ivxz9pckl81XqJHw/webhook-trigger/63b7b203-d472-4fea-825b-28add4567277";
const form = document.getElementById("hfVppForm");
if (!form) {
console.warn("HighFlow VPP form not found (#hfVppForm).");
return;
}
/* CHIP STYLING */
function setupChipGroup(id) {
const group = document.getElementById(id);
if (!group) return;
group.addEventListener("change", function () {
const labels = group.querySelectorAll(".hf-vpp-chip");
labels.forEach(l => l.classList.remove("hf-vpp-chip-checked"));
const checked = group.querySelector("input:checked");
if (checked && checked.parentElement) {
checked.parentElement.classList.add("hf-vpp-chip-checked");
}
});
}
setupChipGroup("hf_has_solar_group");
setupChipGroup("hf_battery_group");
setupChipGroup("hf_inverter_internet_group");
/* DYNAMIC SHOW/HIDE */
const solarGroup = document.getElementById("hf_has_solar_group");
const solarSizeBlock = document.getElementById("hf_solar_size_block");
if (solarGroup && solarSizeBlock) {
solarGroup.addEventListener("change", function (e) {
if (e.target && e.target.name === "has_solar") {
solarSizeBlock.classList.toggle("hf-vpp-hidden", e.target.value !== "yes");
}
});
}
const batteryGroup = document.getElementById("hf_battery_group");
const batteryDetails = document.getElementById("hf_battery_details");
const batteryWarning = document.getElementById("hf_battery_warning");
if (batteryGroup && batteryDetails && batteryWarning) {
batteryGroup.addEventListener("change", function (e) {
if (!e.target || e.target.name !== "battery_installed") return;
const v = e.target.value;
if (v === "yes") {
batteryDetails.classList.remove("hf-vpp-hidden");
batteryWarning.classList.add("hf-vpp-hidden");
batteryWarning.innerHTML = "";
} else if (v === "no") {
batteryDetails.classList.add("hf-vpp-hidden");
batteryWarning.classList.remove("hf-vpp-hidden");
batteryWarning.innerHTML =
"A battery is required to join the HighFlow VPP.";
} else {
batteryDetails.classList.add("hf-vpp-hidden");
batteryWarning.classList.add("hf-vpp-hidden");
batteryWarning.innerHTML = "";
}
});
}
const oneBillOnly = document.getElementById("hf_one_bill_only");
const bill2Field = document.getElementById("hf_bill2_field");
const bill2Input = document.querySelector("input[name='bill2']");
if (oneBillOnly && bill2Field && bill2Input) {
oneBillOnly.addEventListener("change", function () {
bill2Field.classList.toggle("hf-vpp-hidden", oneBillOnly.checked);
bill2Input.required = !oneBillOnly.checked;
if (oneBillOnly.checked) bill2Input.value = "";
});
}
/* ELIGIBILITY */
function checkEligibility(formEl) {
const hasSolar = formEl.querySelector("input[name='has_solar']:checked")?.value;
const bat = formEl.querySelector("input[name='battery_installed']:checked")?.value;
const avg = parseFloat(formEl.average_daily_usage?.value || "0");
const inv = parseFloat(formEl.inverter_battery_size?.value || "0");
if (hasSolar !== "yes") return false;
if (bat !== "yes") return false;
if (!avg || !inv) return false;
return inv >= avg / 2;
}
/* SEND TO CRM */
function sendToCRM(formEl, eligible) {
const fd = new FormData(formEl);
const payload = Object.fromEntries(fd.entries());
payload.eligibility_status = eligible ? "approved" : "further_assessment";
fetch(CRM_WEBHOOK_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload)
}).catch(err => console.error("CRM webhook error:", err));
}
/* SUBMIT HANDLER */
form.addEventListener("submit", function (e) {
e.preventDefault();
if (!form.checkValidity()) {
form.reportValidity();
return;
}
const eligible = checkEligibility(form);
sendToCRM(form, eligible);
if (eligible) {
alert(
"Congratulations! You are eligible for HighFlow Energy’s AI-driven VPP.\n\n" +
"It’s time to stop just saving and start making money from your solar and battery."
);
} else {
alert(
"Your application will need further assessment.\n\n" +
"Our team will review your details and contact you shortly."
);
}
form.reset();
});
});