function cf7smtGetUrlParam(name){const urlParams=new URLSearchParams(window.location.search);return urlParams.get(name)||'';} function cf7smtSet(form,name,value){const input=form.querySelector(`input[name="${name}"]`);if(input&&value!==undefined&&value!==null)input.value=String(value);} function cf7smtSafeSet(form,name,value,fallback=''){try{cf7smtSet(form,name,value??fallback);}catch(_){}} function cf7smtDebounce(fn,wait){let t;return(...a)=>{clearTimeout(t);t=setTimeout(()=>fn(...a),wait);};} function cf7smtUuid(){return'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,c=>{const r=Math.random()*16|0,v=c==='x'?r:(r&0x3|0x8);return v.toString(16);});} function cf7smtReadCookie(name){const m=document.cookie.match(new RegExp('(?:^|;\\s*)'+name.replace(/[-[\]{}()*+?.\\^$|#\\s]/g,'\\$&')+'=([^;]*)'));return m?decodeURIComponent(m[1]):'';} function cf7smtTimeout(promise,ms,fallback){return new Promise(resolve=>{let done=false;const t=setTimeout(()=>{if(!done){done=true;resolve(fallback);}},ms);promise.then(v=>{if(!done){done=true;clearTimeout(t);resolve(v);}}).catch(()=>{if(!done){done=true;clearTimeout(t);resolve(fallback);}});});} function cf7smtGetGAClientId(){const fromGtag=new Promise(resolve=>{try{if(window.gtag&&window.CF7SMT&&CF7SMT.ga4_id){gtag('get',CF7SMT.ga4_id,'client_id',cid=>resolve(cid||''));return;}}catch(_){} resolve('');});const fromCookie=new Promise(resolve=>{try{const ga=cf7smtReadCookie('_ga');if(ga){const p=ga.split('.');return resolve(p.length>=4?p.slice(-2).join('.'):'');}}catch(_){} resolve('');});return cf7smtTimeout(Promise.race([fromGtag,fromCookie]),900,'');} async function cf7smtFillHidden(form){try{cf7smtSafeSet(form,'language_page',document.documentElement.lang||'');}catch(_){} try{cf7smtSafeSet(form,'language_browser',navigator.language||'');}catch(_){} try{const now=new Date();const y=now.getFullYear();const m=String(now.getMonth()+1).padStart(2,'0');const d=String(now.getDate()).padStart(2,'0');const hh=String(now.getHours()).padStart(2,'0');const mm=String(now.getMinutes()).padStart(2,'0');const ss=String(now.getSeconds()).padStart(2,'0');cf7smtSafeSet(form,'date',`${y}-${m}-${d} ${hh}:${mm}:${ss}`);}catch(_){} try{cf7smtSafeSet(form,'url',window.location.href);}catch(_){} try{cf7smtSafeSet(form,'timezone',Intl.DateTimeFormat().resolvedOptions().timeZone||'');}catch(_){} try{const cid=await cf7smtGetGAClientId();if(cid)cf7smtSafeSet(form,'client_id',cid);}catch(_){} try{const geo=await cf7smtTimeout((async()=>{const ctrl=new AbortController();const timer=setTimeout(()=>ctrl.abort(),1100);const res=await fetch('https://ipwho.is?lang=en',{signal:ctrl.signal,cache:'no-store'});clearTimeout(timer);if(!res.ok)throw new Error('geo http');const j=await res.json();if(!j||!j.success)throw new Error('geo fail');return{combo:`${j.country || ''} ${j.city || ''}`.trim(),code:j.country_code||'',iso:(j.country_code||'').toLowerCase(),name:j.country||''};})(),1200,null);if(geo){cf7smtSafeSet(form,'location_ip',geo.combo,'Unknown');cf7smtSafeSet(form,'country_code',geo.code,'');cf7smtSafeSet(form,'country_iso',geo.iso,'');cf7smtSafeSet(form,'country_name',geo.name,'');}else{cf7smtSafeSet(form,'location_ip','Unknown');}}catch(_){cf7smtSafeSet(form,'location_ip','Unknown');} const params=['utm_source','utm_medium','utm_campaign','utm_term','utm_content','utm_referrer','gclid','gbraid','gad_source','gad_campaignid','matchtype','position','device','targetid','location','fbclid','fb_campaign_id','fb_adset_id','fb_ad_id','fb_campaign_name','fb_adset_name','fb_ad_name','yclid','ym_uid'];params.forEach(p=>{const v=cf7smtGetUrlParam(p);if(v)cf7smtSafeSet(form,p,v);});if(!cf7smtGetUrlParam('utm_referrer')&&document.referrer){cf7smtSafeSet(form,'utm_referrer',document.referrer);} const pageTitle=document.querySelector('h1, .first__title, .page-title')?.textContent?.trim();if(pageTitle)cf7smtSafeSet(form,'project_name',pageTitle);const budgetElement=document.querySelector('.project__budget');if(budgetElement)cf7smtSafeSet(form,'project_budget',budgetElement.textContent.trim());const modalTitle=form.closest('.modal')?.querySelector('.modal-title, .application__title')?.textContent?.trim();const formTitle=form.querySelector('.form-title')?.textContent?.trim();cf7smtSafeSet(form,'form_type',modalTitle||formTitle||'Contact Form');const sidInput=form.querySelector('input[name="session_id"]');if(sidInput&&!sidInput.value)cf7smtSafeSet(form,'session_id',cf7smtUuid());const phoneRaw=form.querySelector('input[name="your-phone"]')?.value||'';const phoneSanitized=phoneRaw.replace(/\D/g,'');if(phoneSanitized.length>6)cf7smtSafeSet(form,'whatsapp','https://api.whatsapp.com/send?phone='+phoneSanitized);} (function initCF7SMT(){document.addEventListener('DOMContentLoaded',()=>{document.querySelectorAll('input[name="extra_field"]').forEach(i=>{i.value='';});});const attached=new WeakSet();function attach(form){if(!form||attached.has(form))return;attached.add(form);let filledOnce=false;form.addEventListener('focusin',()=>{if(filledOnce)return;filledOnce=true;cf7smtFillHidden(form).catch(()=>{});});const phoneInput=form.querySelector('input[name="your-phone"]');if(phoneInput)phoneInput.addEventListener('input',cf7smtDebounce(()=>cf7smtFillHidden(form),300));} document.querySelectorAll('form.wpcf7-form').forEach(attach);const mo=new MutationObserver(()=>{document.querySelectorAll('form.wpcf7-form').forEach(attach);});mo.observe(document.documentElement,{childList:true,subtree:true});document.addEventListener('wpcf7beforesubmit',(e)=>{const form=e.target;cf7smtFillHidden(form).catch(()=>{});const submit=form.querySelector('input[type="submit"], button[type="submit"]');if(submit){submit.disabled=true;if('value'in submit)submit.value='Sending...';else{submit.dataset.prevText=submit.textContent||'';submit.textContent='Sending...';}} form.dataset.cf7smtTimer&&clearTimeout(form.dataset.cf7smtTimer);form.dataset.cf7smtTimer=setTimeout(()=>{if(!submit)return;submit.disabled=false;if('value'in submit)submit.value='Send';else submit.textContent=submit.dataset.prevText||'Send';},12000);});document.addEventListener('wpcf7submit',(e)=>{const form=e.target;if(form.dataset.cf7smtTimer){clearTimeout(form.dataset.cf7smtTimer);delete form.dataset.cf7smtTimer;} const submit=form.querySelector('input[type="submit"], button[type="submit"]');if(submit){submit.disabled=false;if('value'in submit)submit.value='Send';else submit.textContent=submit.dataset.prevText||'Send';} if(e.detail?.status==='mail_sent'){const wrapper=form.closest('[data-pdf]');const pdfUrl=wrapper?.dataset?.pdf;if(pdfUrl){const a=document.createElement('a');a.href=pdfUrl;a.download='';document.body.appendChild(a);a.click();a.remove();} const lang=document.documentElement.lang||'en';const redirectUrl=lang.startsWith('ru')?'/ru/spasibo/':'/thanks/';setTimeout(()=>{window.location.href=redirectUrl;},700);}});})();