`;
const modalElement = document.createElement('div');
modalElement.id = 'trial-modal';
modalElement.className = 'modal-overlay';
modalElement.innerHTML = modalHtml;
document.body.appendChild(modalElement);
const modalList = modalElement.querySelector('#modal-locations-list');
const closeBtn = modalElement.querySelector('.modal-close');
// 3. Logic Functions
function populateModal(message) {
modalList.innerHTML = '';
clinicData.locations.forEach(loc => {
const waLink = `https://wa.me/${loc.phone}?text=${encodeURIComponent(message)}`;
const linkDiv = document.createElement('a');
linkDiv.href = waLink;
linkDiv.className = 'modal-location-link';
linkDiv.target = '_blank';
linkDiv.innerText = loc.name;
linkDiv.onclick = () => closeModal();
modalList.appendChild(linkDiv);
});
}
function openModal(msg) {
populateModal(msg);
modalElement.style.display = 'flex';
modalElement.classList.add('is-visible');
document.body.style.overflow = 'hidden';
}
function closeModal() {
modalElement.classList.remove('is-visible');
modalElement.style.display = 'none';
document.body.style.overflow = '';
}
// 4. Event Listeners
document.addEventListener('click', function(e) {
const btn = e.target.closest('.trial-btn');
if (btn) {
e.preventDefault();
const prefillMsg = btn.getAttribute('data-note') || "Hi, I'd like to book an appointment.";
openModal(prefillMsg);
}
if (e.target === modalElement || e.target === closeBtn) {
closeModal();
}
});
document.addEventListener('keydown', (e) => {
if (e.key === "Escape" && modalElement.style.display === 'flex') {
closeModal();
}
});
});
jQuery(document).ready(function ($) {
const $button = $('.trial-btn');
const $targetInput = $('input[name="event"]');
const $messageTextarea = $('textarea[name="your-message"]');
if ($button.length) {
const eventValue = $button.data('event');
const noteValue = $button.data('note');
const selectIssues = $button.attr('select-issues');
const selectProgrammes = $button.attr('select-programmes');
$button.on('click', function (event) {
if ($targetInput.length) {
$targetInput.val(eventValue);
}
if ($messageTextarea.length && noteValue && noteValue.trim().length) {
let currentValue = $messageTextarea.val().trim();
if (currentValue.indexOf(noteValue.trim()) === -1) {
const newValue = noteValue + ' ' + currentValue;
$messageTextarea.val(newValue);
}
}
if (selectIssues && selectIssues.length) {
const issueSelector = $('[data-name^="issues-"] label [data-value="' + selectIssues + '"]');
if (issueSelector.length && issueSelector.prop('checked') === false) {
issueSelector.click();
}
}
if (selectProgrammes && selectProgrammes.length) {
const programmeSelector = $('[data-name^="programmes-"] label [data-value="' + selectProgrammes + '"]');
if (programmeSelector.length && programmeSelector.prop('checked') === false) {
programmeSelector.click();
}
}
});
}
});