
		(function() {
			let storage = window.localStorage;
			let currentDate = new Date();
			let formattedDate = [
				currentDate.getFullYear(),
				String(currentDate.getMonth() + 1).padStart(2, '0'),
				String(currentDate.getDate()).padStart(2, '0')
			].join("-");

			function initialize() {
				if (storage.getItem("aif-us") === null) {
					storage.setItem("aif-us", "1");
					storage.setItem("aif-us-date-1", formattedDate);
				} else if (storage.getItem("aif-us") === "1" && storage.getItem("aif-us-date-1") !== formattedDate) {
					storage.setItem("aif-us", "2");
					storage.setItem("aif-us-date-2", formattedDate);
					setTimeout(loadSubscriptionPopup(), 3000);
				} else if (storage.getItem("aif-us") === "2" && storage.getItem("aif-us-date-1") !== storage.getItem("aif-us-date-2")) {
				  	setTimeout(loadSubscriptionPopup(), 3000);
				} else if (storage.getItem("aif-us") === "3") {
					$('#AiFollowTY').fadeIn();
					storage.removeItem("aif-us");
				}

				const tncCheckbox = document.getElementById('tncCheckbox');
				const submitBtn = document.getElementById('aifSubmitBtn');
				const errorSpan = document.getElementById('aifError');

				function toggleSubmit() {
					if (tncCheckbox.checked) {
						submitBtn.classList.remove('btn-disable');
					} else {
						submitBtn.classList.add('btn-disable');
					}
				}
				tncCheckbox.addEventListener('change', toggleSubmit);
				toggleSubmit();

				submitBtn.addEventListener('click', function(e){
					if(submitBtn.classList.contains('btn-disable')){
						e.preventDefault();
      					e.stopPropagation();
						return false;
					}
				});

				// Auto-hide error message when typing
				['input','change'].forEach(event => {
					document.getElementById('firstname').addEventListener(event, () => errorSpan.classList.add('d-none'));
					document.getElementById('lastname').addEventListener(event, () => errorSpan.classList.add('d-none'));
					document.getElementById('mail').addEventListener(event, () => errorSpan.classList.add('d-none'));
					tncCheckbox.addEventListener(event, () => errorSpan.classList.add('d-none'));
				});
			}

			window.onload = initialize;
		})();

		function loadSubscriptionPopup() { 
			FWEvent('AIF','popup_open','10_off_new_order');  
			$('#AiFollow').fadeIn();
		}

		function aif_subscribe() { 
			const errorSpan = document.getElementById('aifError');
			const fn = document.getElementById("firstname").value.trim();
			const ln = document.getElementById("lastname").value.trim();
			const em = document.getElementById("mail").value.trim();
			const tncCheckbox = document.getElementById('tncCheckbox');
			
			const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

			errorSpan.classList.add('d-none');
			errorSpan.textContent = '';

			if (!fn || !ln || !em) {
				errorSpan.textContent = "Please fill out all fields.";
				errorSpan.classList.remove('d-none');
				return;
			}

			if (!emailRegex.test(em)) {
				errorSpan.textContent = "Please enter a valid email address.";
				errorSpan.classList.remove('d-none');
				return;
			}

			if (!tncCheckbox.checked) {
				errorSpan.textContent = "You must agree to the terms to proceed.";
				errorSpan.classList.remove('d-none');
				return;
			}

			FWEvent('AIF','email_signup','10_off_new_order');
			localStorage.removeItem("aif-us");
			localStorage.removeItem("aif-us-date-1");
			localStorage.removeItem("aif-us-date-2");
			localStorage.setItem("aif-us", "3");
			document.forms[0].submit();
		}

		function aif_close() {
			FWEvent('AIF','popup_close','10_off_new_order');
			$('#AiFollow').fadeOut();
			$('#aif-badge').removeClass('d-none').fadeIn();
		}

		function aif_open() {
			FWEvent('AIF','popup_open','10_off_new_order');
			$('#AiFollow').fadeIn();
			$('#aif-badge').fadeOut();
		}