(function () { var root = document.querySelector('[data-order]'); if (!root || !window.BX || !BX.ajax || !BX.ajax.runAction) return; var form = root.querySelector('[data-order-form]'); var err = root.querySelector('[data-order-error]'); var guard = form.querySelector('altcha-widget'); var base = root.getAttribute('data-base') || '/'; function text(name) { return root.getAttribute('data-text-' + name) || ''; } function clear(el) { el.classList.remove('vino-inp--invalid'); el.removeAttribute('aria-invalid'); } function say(message) { err.textContent = message; err.hidden = !message; } function check() { var f = form.elements; var focus = null; var message = ''; var missing = 0; function bad(el, note) { el.classList.add('vino-inp--invalid'); el.setAttribute('aria-invalid', 'true'); if (!focus) focus = el; if (note && !message) message = note; } [f.name, f.phone, f.email, f.consent].forEach(clear); var phone = f.phone.vinoPhone; var email = f.email.value.trim(); if (!f.name.value.trim()) { bad(f.name, ''); missing++; } if (!phone || !phone.filled()) { bad(f.phone, ''); missing++; } if (!email) { bad(f.email, ''); missing++; } if (missing > 1) message = text('many'); else if (missing === 1) message = !f.name.value.trim() ? text('name') : (phone && !phone.filled() ? text('phone') : text('email')); if (!missing) { if (phone && !phone.complete()) bad(f.phone, text('phone')); if (!/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(email)) bad(f.email, text('email')); } if (!f.consent.checked) bad(f.consent, text('consent')); say(message); if (focus) focus.focus(); return !focus; } function serverMessage(res) { var code = res && res.errors && res.errors[0] && res.errors[0].code; if (code === 'rate') return text('rate'); if (code === 'empty' || code === 'cart') return text('empty'); return text('fail'); } form.addEventListener('input', function (e) { clear(e.target); }); form.addEventListener('change', function (e) { clear(e.target); }); if (window.VinoAltcha) VinoAltcha.arm(guard); window.addEventListener('pageshow', function (e) { if (!e.persisted) return; var submit = form.querySelector('[type="submit"]'); submit.disabled = false; submit.classList.remove('vino-btn--loading'); }); form.addEventListener('submit', function (e) { e.preventDefault(); if (!check()) return; var button = form.querySelector('[type="submit"]'); var payInput = form.querySelector('[name="pay"]:checked'); var online = !!payInput && payInput.getAttribute('data-online') === 'Y'; button.disabled = true; if (online && window.VinoBusy) VinoBusy.show(text('busy')); else button.classList.add('vino-btn--loading'); say(''); (window.VinoAltcha ? VinoAltcha.armed(guard) : Promise.resolve(false)) .then(function (ok) { if (!ok) throw new Error('guard'); var f = form.elements; var pay = form.querySelector('[name="pay"]:checked'); return BX.ajax.runAction('jinove:vinoteca.order.create', { data: { name: f.name.value, phone: f.phone.vinoPhone.value(), email: f.email.value, comment: f.comment.value, pay: pay ? pay.value : '', consent: f.consent.checked ? 'Y' : '', website: f.website.value, altcha: f.altcha ? f.altcha.value : '', page: window.location.pathname, base: base } }); }) .then(function (res) { if (res && res.data && res.data.ok && res.data.redirect) { window.location.href = res.data.redirect; return; } button.disabled = false; button.classList.remove('vino-btn--loading'); if (window.VinoBusy) VinoBusy.hide(); say(text('fail')); }) .catch(function (res) { button.disabled = false; button.classList.remove('vino-btn--loading'); if (window.VinoBusy) VinoBusy.hide(); if (window.VinoAltcha) VinoAltcha.reset(guard); say(serverMessage(res)); }); }); })();