(function(){
function esc(s){
return String(s ?? '').replace(/[&<>"']/g, m=> ({
'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#039;'
}[m]));
}
function showStatus(html, type){
const el=document.getElementById('cnctcinq-contact-quote-status');
if(!el) return;
const cls=type==='success' ? 'alert alert-success':'alert alert-danger';
el.innerHTML='<div class="'+cls+'">'+ html +'</div>';
}
function delay(ms){
return new Promise(resolve=> setTimeout(resolve, ms));
}
async function getFreshNonce(){
const fd=new FormData();
fd.set('action', 'cnctcinq_get_contact_nonce');
const res=await fetch(CNCTCINQ_QUOTE.ajax_url, {
method: 'POST',
body: fd,
credentials: 'same-origin'
});
const raw=await res.text();
let json=null;
try {
json=JSON.parse(raw);
} catch (e){
throw new Error('Could not prepare secure request.');
}
if(!json||!json.success||!json.data||!json.data.nonce){
throw new Error('Could not prepare secure request.');
}
return json.data.nonce;
}
async function getRecaptchaToken(){
if(!window.grecaptcha||!window.CNCTCINQ_QUOTE||!window.CNCTCINQ_QUOTE.recaptcha_site_key){
throw new Error('reCAPTCHA not ready.');
}
return await new Promise((resolve, reject)=> {
window.grecaptcha.ready(function(){
window.grecaptcha.execute(window.CNCTCINQ_QUOTE.recaptcha_site_key, {
action: 'cnctcinq_contact_quote'
}).then(resolve).catch(reject);
});
});
}
async function submitForm(form){
const btn=form.querySelector('button[type="submit"]');
const originalText=btn ? btn.textContent:'';
try {
if(btn){
btn.disabled=true;
btn.textContent='Submitting...';
}
showStatus('Preparing your request...', 'success');
let token='';
try {
token=await getRecaptchaToken();
} catch(firstErr){
console.warn('EG CONTACT first reCAPTCHA attempt failed, retrying...', firstErr);
showStatus('Security check is still loading. Retrying...', 'success');
await delay(1000);
token=await getRecaptchaToken();
}
if(!token){
throw new Error('Security verification failed. Please try again.');
}
const tokenInput=form.querySelector('[name="recaptcha_token"]');
if(tokenInput) tokenInput.value=token;
const fd=new FormData(form);
fd.set('action', 'cnctcinq_submit_contact_quote');
const freshNonce=await getFreshNonce();
fd.set('cnctcinq_contact_quote_nonce', freshNonce);
fd.set('nonce', freshNonce);
console.log('EG CONTACT NONCE SENT:', freshNonce.substring(0, 10));
const res=await fetch(CNCTCINQ_QUOTE.ajax_url, {
method: 'POST',
body: fd,
credentials: 'same-origin'
});
const raw=await res.text();
console.log('EG CONTACT STATUS:', res.status);
console.log('EG CONTACT RAW RESPONSE:', raw);
let json=null;
try {
json=JSON.parse(raw);
} catch(parseErr){
throw new Error('Server returned an invalid response. Please try again.');
}
if(!json||!json.success){
const msg=(json&&json.data&&json.data.message) ? json.data.message:'Submission failed.';
showStatus(
esc(msg) +
'<br><small>If the form fails, please email us directly at <strong>executivegourmet@yahoo.com</strong>.</small>',
'error'
);
if(btn){
btn.disabled=false;
btn.textContent=originalText;
}
return;
}
const id=json.data&&json.data.inquiry_id ? parseInt(json.data.inquiry_id, 10):0;
showStatus(
'✅ Successfully submitted your request.' + (id ? ' Reference: <strong>#'+id+'</strong>':''),
'success'
);
form.reset();
if(tokenInput) tokenInput.value='';
if(btn){
btn.disabled=false;
btn.textContent=originalText;
}} catch(err){
console.error('EG CONTACT SUBMIT ERROR:', err);
showStatus(
esc(err&&err.message ? err.message:'Unexpected error.') +
'<br><small>If the form fails, please email us directly at <strong>executivegourmet@yahoo.com</strong>.</small>',
'error'
);
if(btn){
btn.disabled=false;
btn.textContent=originalText;
}}
}
document.addEventListener('submit', function(ev){
const form=ev.target;
if(!form||form.id!=='cnctcinq-contact-quote-form') return;
ev.preventDefault();
submitForm(form);
});
})();