function validateEmail(email){return String(email).toLowerCase().match(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);} function callApi(url,method='GET',data=null){return new Promise((resolve,reject)=>{const xhr=new XMLHttpRequest();xhr.open(method,url);if(data){xhr.setRequestHeader('Content-Type','application/json');} xhr.onload=function(){if(xhr.status>=200&&xhr.status<300){resolve({status:xhr.status,response:xhr.responseText});}else{reject({status:xhr.status,error:xhr.responseText});}};xhr.onerror=function(){reject({status:0,error:'Network error occurred'});};xhr.send(data?JSON.stringify(data):null);});} function getMeta(metaName){const metas=document.getElementsByTagName('meta');for(let i=0;ihideAllChildren(child));}} function hideAllChildren(element){element.style.display='none';Array.from(element.children).forEach(child=>hideAllChildren(child));} function showDiv(divId){const el=document.getElementById(divId);if(el){el.style.removeProperty('display');Array.from(el.children).forEach(child=>showAllChildren(child));}} function showAllChildren(element){element.style.removeProperty('display');Array.from(element.children).forEach(child=>showAllChildren(child));} function postForm(){const body={name:document.getElementById('your-name').value,email:document.getElementById('email').value,phoneNumber:document.getElementById('phone-number').value,aboutYou:document.getElementById('about-you').value,formToken:getMeta('form-token')};if(!body.name||body.name.trim()===""||body.name.length>60||body.name.indexOf('"')!==-1){console.log("Invalid name:",body.name);document.getElementById('your-name').focus();return;} const validEmail=validateEmail(body.email);if(!validEmail){console.log("Invalid email address:",body.email);document.getElementById('email').focus();return;} hideDiv('contact-form');showDiv('confirmation-message-in-progress');callApi('/api/contact','POST',body).then(response=>{if(response.status===200){hideDiv('confirmation-message-in-progress');const result=JSON.parse(response.response);if(result.message.length>0){showDiv('confirmation-message-already-sent');}else{showDiv('confirmation-message-first-time');}}else{console.error('API call failed:',response);alert('Failed to send message: '+response.error);hideDiv('confirmation-message-in-progress');showDiv('contact-form');}}).catch(error=>{console.error('Error occurred:',error);alert('Error occurred: '+error.error);hideDiv('confirmation-message-in-progress');showDiv('contact-form');});}