(function (/* root, doc */) { //validate revised 2 // ****** Declare functions ****** // Function to add or remove danger class function toggleDangerClass(element) { if (element.type === "checkbox" || element.type === "radio") { const parent = element.parentElement; if (parent) { if (element.validity.valid) { parent.classList.remove("uk-form-danger"); } else { parent.classList.add("uk-form-danger"); } } } else if (element.tagName.toLowerCase() === "select") { const selectedIndex = element.selectedIndex; const selectedOption = element.options[selectedIndex]; if (selectedIndex === 0 || selectedOption.disabled) { element.classList.add("uk-form-danger"); } else { element.classList.remove("uk-form-danger"); } } else { if (element.validity.valid) { element.classList.remove("uk-form-danger"); } else { element.classList.add("uk-form-danger"); } } } // Function to get all required inputs, checkboxes, and selects function getRequiredInputs(form) { let requiredInputs = Array.from( form.querySelectorAll( "input[required], textarea[required], select[required]" ) ); let requiredCheckboxes = Array.from( form.querySelectorAll('input[type="checkbox"][required]') ); let requiredRadioButtons = Array.from( form.querySelectorAll('input[type="radio"][required]') ); requiredInputs.push(...requiredCheckboxes, ...requiredRadioButtons); return requiredInputs; } // Validate email function function validateEmail(email) { const regex = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/; return regex.test(email); } // This function sets the form name based on the URL of the page. function setFormName() { // Create a new URL object from the passed URL string. var url = new URL(window.location.href); // Creates a new URL object from the current URL // Get the base URL (origin + pathname), excluding parameters (query and hash). var baseUrl = url.origin + url.pathname; // Initialize formName to an empty string. var formName = ""; // Define a map (object) that associates base URLs to form names. var urlFormMap = { "https://cloud.my.silhouette-group.com/silhouette-forms-contact": "silhouette_contact_form", "https://cloud.my.silhouette-group.com/silhouette-forms-newsletter": "silhouette_newsletter_form", "https://cloud.my.silhouette-group.com/silhouette-forms-journal": "silhouette_journal_form", "https://cloud.my.silhouette-group.com/evileye-forms-newsletter": "evileye_newsletter_form", "https://cloud.my.silhouette-group.com/evileye-forms-contact": "evileye_contact_form", "https://cloud.my.silhouette-group.com/test-newsletter-form": "test_silhouette_newsletter_form", "https://mc8l3vqcb3v96r54rp-mdkskx0y1.pub.sfmc-content.com/cmaaz5zlqga": "silhouette_retailer_contact_form_buy_local", "https://cloud.my.silhouette-group.com/silhouette-forms-retailer-search-buy-local": "silhouette_retailer_contact_form_buy_local", "https://cloud.my.silhouette-group.com/silhouette-forms-retailer-search": "silhouette_retailer_contact_form", "https://cloud.my.silhouette-group.com/evileye-forms-retailer-search": "evileye_retailer_contact_form" }; // Check if the base URL is a key in the urlFormMap. if (urlFormMap.hasOwnProperty(baseUrl)) { // If it is, set formName to the corresponding value. formName = urlFormMap[baseUrl]; } else { // If it's not, log a message to the console stating that the URL is not recognized. console.log("URL not recognized: " + baseUrl); } // Return the form name. return formName; } /*START OF DBUSF-411 #1 : Added marketing consent checkbox in data layer*/ //This function sets the marketing consent checkbox based on the checkbox is checked or not. function getMarketingConsentCheckboxValue() { var marketingConsentCheckboxValue = "" var marketingConsentCheckboxInformation = document.querySelectorAll('input[type="checkbox"][name="marketing_consent"]'); var marketingConsentCheckbox = marketingConsentCheckboxInformation[0]; if (marketingConsentCheckbox.checked) { marketingConsentCheckboxValue = true } else { marketingConsentCheckboxValue = false } //Return marketing consent checkbox value return marketingConsentCheckboxValue; } /*END OF DBSUF-411*/ // Function for form validation function submitHandler(event) { event.preventDefault(); // Get all required inputs right before form submission const requiredInputs = getRequiredInputs(event.target); let formIsValid = true; let marketingCheckBox = document.querySelectorAll('input[id="cform__marketing_consent"]:checked')[0]; requiredInputs.forEach((input) => { if (input.type === "email" && !validateEmail(input.value)) { formIsValid = false; toggleDangerClass(input); } if (input.tagName.toLowerCase() === "select" && document.getElementById('cform__marketing_consent').checked && !input.validity.valid) { formIsValid = false; toggleDangerClass(input); } else if (!(input.tagName.toLowerCase() === "select") && !input.validity.valid) { formIsValid = false; toggleDangerClass(input); } if (input.tagName.toLowerCase() === "select") { const selectedIndex = input.selectedIndex; const selectedOption = input.options[selectedIndex]; if (selectedIndex === -1 || selectedOption.disabled) { formIsValid = false; toggleDangerClass(input); } } input.addEventListener("input", () => toggleDangerClass(input)); }); // If the form is valid, disable the submit button and show a loader if (formIsValid) { const submitButton = event.target.querySelector('input[type="submit"]'); submitButton.disabled = true; // Add spinner to the button or to the form submitButton.insertAdjacentHTML( "afterend", '
' ); dataLayer.push({ event: "form_submit", form_name: setFormName(), marketingConsent : getMarketingConsentCheckboxValue() }); event.target.submit(); } } // ****** Assign event listeners ****** document.addEventListener("DOMContentLoaded", function () { //Save to variables all the required inputs const form = document.querySelector("form"); // Add change event listener to the journalCheckbox const journalCheckbox = document.getElementById("nlform__journal") || document.getElementById("cform__journal"); //Journal checkbox status checker and requited attribute adder and remover if (journalCheckbox) { journalCheckbox.addEventListener("change", (event) => { const gaJournalInputs = document.querySelectorAll( "#ga-journal input, #ga-journal select" ); console.log(gaJournalInputs); const isChecked = event.target.checked; console.log("journal checkbox event fired, is checked is:", isChecked); gaJournalInputs.forEach((input) => { if (isChecked) { input.setAttribute("required", ""); } else { input.removeAttribute("required"); } }); }); } // Form submission event form.addEventListener("submit", submitHandler); }); })(window, document);