Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
document.addEventListener('DOMContentLoaded', function () {
// Input field focus and blur events
const formElements = document.querySelectorAll('form input, form textarea');
formElements.forEach(function (element) {
element.addEventListener('focus', function () {
highlightField(element, true);
});
element.addEventListener('blur', function () {
highlightField(element, false);
});
});
// Form submission event
const form = document.getElementById('form_contact');
form.addEventListener('submit', function (event) {
if (!validateForm()) {
event.preventDefault();
}
});
// Function to highlight/unhighlight input fields and labels
function highlightField(element, isFocus) {
const label = document.querySelector(`label[for=${element.id}]`);
if (isFocus) {
element.style.backgroundColor = 'yellow';
label.style.fontWeight = 'bold';
} else {
element.style.backgroundColor = 'white';
label.style.fontWeight = 'normal';
}
}
// Function to validate the form
function validateForm() {
const requiredFields = ['firstname', 'lastname', 'email', 'phone'];
let formIsValid = true;
requiredFields.forEach(function (fieldName) {
const field = document.getElementById(fieldName);
if (field.value.trim() === '') {
field.style.backgroundColor = 'red';
formIsValid = false;
} else {
field.style.backgroundColor = 'white';
}
});
const termsCheckbox = document.getElementById('terms');
if (!termsCheckbox.checked) {
formIsValid = false;
}
// Check if the form is valid and set the cookie if needed
if (formIsValid) {
const formSubmittedCookie = getCookie('formSubmitted');
if (!formSubmittedCookie) {
setCookie('formSubmitted', 'true', 1);
} else {
document.getElementById('btnSubmit').disabled = true;
}
}
return formIsValid;
}
// Function to get the value of a cookie
function getCookie(name) {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
if (cookie.startsWith(name + '=')) {
return cookie.substring(name.length + 1);
}
}
return null;
}
// Function to set a cookie
function setCookie(name, value, days) {
const expires = new Date();
expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000);
document.cookie = `${name}=${value};expires=${expires.toUTCString()};path=/`;
}
});