Årlig licens
document.addEventListener("DOMContentLoaded", function () {
// Scope everything to this form to avoid collisions
const form = document.querySelector("#tilmeld_licens");
if (!form) return;
const radioGroup = form.querySelector(".elementor-field-group-pakke");
const licensGroup = form.querySelector(".elementor-field-group-licensstoerrelse");
const selectField = form.querySelector("#form-field-licensstoerrelse");
const priceElement = document.getElementById("dynamic-price");
const priceInputField = form.querySelector("#form-field-pris");
const licensInputField = form.querySelector("#form-field-licens");
// Hide licens select until a radio is picked
function hideLicensSelect() {
if (licensGroup) licensGroup.style.display = "none";
}
function showLicensSelect() {
if (licensGroup) licensGroup.style.display = "";
}
hideLicensSelect();
// Tiers shown in the select
const TIERS = [
{ key: "single", label: "Enkeltbruger licens" },
{ key: "le5", label: "≤5 brugere" },
{ key: "le10", label: ">5 brugere" }
];
// Price table (DKK) from your screenshot
const PRICES = {
"Fuld pakke": {
single: 10000, le5: 30000, le10: null
},
"I gang med test": {
single: 5000, le5: 15000, le10: null
}
};
let currentPakke = null; // becomes "Fuld pakke" or "I gang med test"
// Build options for the select. We keep dataset.key so we can look up price later.
function buildOptions() {
// remember current selection by key if possible
const prevKey = selectField.selectedOptions[0]?.dataset?.key || TIERS[0].key;
selectField.innerHTML = "";
TIERS.forEach(t => {
const opt = document.createElement("option");
opt.textContent = t.label;
opt.dataset.key = t.key;
// When a package is chosen, we set the option value to the numeric price for that package
const price = currentPakke ? PRICES[currentPakke][t.key] : "";
opt.value = price !== undefined ? String(price) : "";
if (t.key === prevKey) opt.selected = true;
selectField.appendChild(opt);
});
}
// Format DKK nicely
function formatDKK(n) {
return new Intl.NumberFormat("da-DK", {
style: "currency",
currency: "DKK",
minimumFractionDigits: 0,
maximumFractionDigits: 0
}).format(n);
}
// Update display + hidden fields
function updateFields() {
// No package chosen yet -> message + clear hidden fields
if (!currentPakke) {
priceElement.innerHTML = "Vælg venligst en løsning for at se prisen.";
if (priceInputField) priceInputField.value = "";
if (licensInputField) licensInputField.value = "";
return;
}
const opt = selectField.selectedOptions[0];
const tierKey = opt?.dataset?.key;
const price = tierKey ? PRICES[currentPakke][tierKey] : null;
if (!price) {
priceElement.innerHTML = "Ønsker du at købe licens til mere end 5 brugere?
Kontakt os, så finder vi den bedste løsning.";
if (priceInputField) priceInputField.value = "";
} else {
priceElement.textContent = formatDKK(price);
if (priceInputField) priceInputField.value = price; // store raw number
}
if (licensInputField) licensInputField.value = opt ? opt.textContent : "";
}
// Radio listeners
radioGroup?.querySelectorAll('input[type="radio"]').forEach(r => {
r.addEventListener("change", function () {
currentPakke = this.value; // "Fuld pakke" or "I gang med test"
showLicensSelect();
buildOptions();
updateFields();
});
});
// Select listener
selectField?.addEventListener("change", updateFields);
// If page loads with a preselected radio, honor it
const preselected = radioGroup?.querySelector('input[type="radio"]:checked');
if (preselected) {
currentPakke = preselected.value;
showLicensSelect();
}
buildOptions();
updateFields();
});