let gfc = { settings: { enableTrace: true, enableDebug: true }, handleWhereHappened: function() { gfc.setCharacterLimit("cqc_partoftheservicerelevant", 300); }, // handleContactDetails: function() { // $('#NextButton').on("click", function(event) { // if ($('#cqc_emailaddress').val() == "" && $('#cqc_uktelephonenumber').val() == "") { // event.preventDefault(); // $('#cqc_emailaddress').closest("tr").addClass('govuk-form-group--error'); // $('#cqc_uktelephonenumber').closest("tr").addClass('govuk-form-group--error'); // $('#cqc_uktelephonenumber').closest("tr").css({'margin-top':'0px', 'padding-top':'32px'}); // $('.description').append('
'); // } // }); // }, // When did this happen handleWhenHappened: function() { gfc.setCharacterLimit("cqc_whendideventshappen", 100); }, // Contact details handleContactDetails: function() { gfc.requireAtLeastOne("cqc_emailaddress", "cqc_uktelephonenumber","Enter either an email address or a telephone number"); $("#cqc_uktelephonenumber-counter").remove(); }, requireAtLeastOne: function (field1, field2, errorMessage) { if (typeof Page_Validators === "undefined") return; const message = errorMessage || "Enter at least one contact detail"; // Apply required styling ONLY to the Email label (field1) $("#" + field1 + "_label").closest(".info").addClass("required"); // Create validator object var validator = document.createElement("span"); validator.style.display = "none"; validator.id = field1 + "_" + field2 + "_AtLeastOneValidator"; // Attach validator ONLY to field1 (Email) validator.controltovalidate = field1; validator.initialvalue = ""; validator.validationGroup = ""; // Error message appears ONLY under Email validator.errormessage = '' + message + ""; // Validation logic checks BOTH fields validator.evaluationfunction = function () { const v1 = $("#" + field1).val()?.trim(); const v2 = $("#" + field2).val()?.trim(); // Valid if at least one has value return !!(v1 || v2); }; // Prevent duplicates const existing = Page_Validators.some(v => v.id === validator.id); if (!existing) Page_Validators.push(validator); }, // Can We Contact You? addNoConsentNote: function() { $('.govuk-radios__item').on("change", function() { if (this.children[0].checked && this.children[1].innerText == "No") { if ($("#contact-hint").length == 0) { $(this).closest(".control").append(`Note: It is more likely we can use what you have told us if we can contact you.
`); } else { $("#contact-hint").show(); } } else { $("#contact-hint").hide(); } }); }, charityCache: {}, charityLoaded: false, handleCharity: async function () { const select = document.getElementById("cqc_charitypartner"); if (!select) return; select.style.display = "none"; gfc.buildCharityRadios(select); const field = document.getElementById("cqc_charity"); field.closest("td.form-control-cell").style.display = "none"; await gfc.prefetchCharities(); gfc.applyCharityToggle(select.value); select.addEventListener("change", () => { gfc.applyCharityToggle(select.value); }); }, prefetchCharities: async function () { if (gfc.charityLoaded) return; const url = `/_api/cqc_charities?$select=cqc_charityid,cqc_anothercharity`; try { const response = await fetch(url, { headers: { "Accept": "application/json", "OData-Version": "4.0", "OData-MaxVersion": "4.0" } }); if (!response.ok) { console.error("Failed to fetch charities:", response.status); return; } const data = await response.json(); data.value.forEach(item => { gfc.charityCache[item.cqc_charityid] = item.cqc_anothercharity; }); gfc.charityLoaded = true; } catch (err) { console.error("Prefetch error:", err); } }, buildCharityRadios: function (select) { const wrapper = select.closest(".cqc-form-start") || select.parentElement; let container = document.getElementById("charityRadios"); if (!container) { container = document.createElement("div"); container.id = "charityRadios"; container.className = "govuk-form-group"; container.innerHTML = ` `; wrapper.insertAdjacentElement("beforeend", container); } const radioBox = container.querySelector(".govuk-radios"); Array.from(select.options).forEach(opt => { if (!opt.value) return; const div = document.createElement("div"); div.className = "govuk-radios__item"; const r = document.createElement("input"); r.type = "radio"; r.className = "govuk-radios__input"; r.name = select.id + "_radios"; r.value = opt.value; r.id = select.id + "_" + opt.value; if (opt.selected) r.checked = true; const label = document.createElement("label"); label.className = "govuk-label govuk-radios__label"; label.textContent = opt.text; label.htmlFor = r.id; div.appendChild(r); div.appendChild(label); radioBox.appendChild(div); r.addEventListener("change", () => { select.value = r.value; select.dispatchEvent(new Event("change")); }); }); $('#NextButton').hide(); $('#NextButton').parent().prepend(''); $('#UpdateButton').hide(); $('#UpdateButton').parent().prepend(''); }, applyCharityToggle: function (charityId) { const field = document.getElementById("cqc_charity"); if (!field) return; const wrapper = field.closest("td.form-control-cell"); if (!charityId) { gfc.makeNotRequired("cqc_charity"); wrapper.style.display = "none"; field.value = ""; return; } const requireOtherName = gfc.charityCache[charityId]; if (requireOtherName === true) { wrapper.style.display = ""; gfc.makeRequired("cqc_charity", "Enter the name of the charity"); } else { wrapper.style.display = "none"; gfc.makeNotRequired("cqc_charity"); } $('#NextButton_Charity, #UpdateButton_Charity').on('click', function () { if (!$('#cqc_charity').closest('td').is(':visible')) { $('#cqc_charity').val(""); } $('#NextButton, #UpdateButton').click(); }); }, setCharacterLimit: function(fieldId, limit, counterSuffix = "-counter") { const $field = $("#" + fieldId); if (!$field.length) return; $field.attr("maxlength", limit); // Short version — no need to check for existence $("#" + fieldId + counterSuffix).text(`You can enter up to ${limit} characters`); }, makeRequired: function (fieldName, errorMsg) { $("#" + fieldName).prop("required", true); $("#" + fieldName + "_label").closest(".info").addClass("required"); // Remove existing validator if present Page_Validators.splice( Page_Validators.findIndex(v => v.id === fieldName + "Validator"), 1 ); const val = document.createElement("span"); val.style.display = "none"; val.id = fieldName + "Validator"; val.controltovalidate = fieldName; val.errormessage = `${errorMsg}`; val.evaluationfunction = () => { const value = $("#" + fieldName).val(); return !(value == null || value.trim() === ""); }; Page_Validators.push(val); }, makeNotRequired: function (fieldName) { $("#" + fieldName).prop("required", false); $("#" + fieldName + "_label").closest(".info").removeClass("required"); const idx = Page_Validators.findIndex(v => v.id === fieldName + "Validator"); if (idx >= 0) Page_Validators.splice(idx, 1); }, }