document.addEventListener("DOMContentLoaded",function(){const e=e=>null==e||""===e,r=(e,r)=>(" "+e.className+" ").indexOf(" "+r+" ")>-1,t=(e,r)=>{let t=[];for(;e.parentNode!==document.body;)e.matches(r)&&t.push(e),e=e.parentNode;return t},l=(t,l,o,s,i)=>{fetch(gutenaFormsBlock.ajax_url,{method:"POST",credentials:"same-origin",body:l}).then(e=>e.json()).then(l=>{if(o.disabled=!1,s.innerHTML=i,t.classList.remove("form-progress"),e(l)||"error"!==l.status){if(t.reset(),t.classList.add("display-success-message"),r(t,"hide-form-after-submit")&&t.classList.add("hide-form-now"),r(t,"after_submit_redirect_url")){let r=t.querySelector('input[name="redirect_url"]');(e(r)||0===r.length)&&console.log("redirect_url not found"),r=r.value,e(r)?console.log("redirect_url",r):setTimeout(()=>{location.href=r},2e3)}}else{t.classList.add("display-error-message");let r=t.querySelector(".wp-block-gutena-form-error-msg .gutena-forms-error-text");(e(r)||0===r.length)&&console.log("errorMsgElement not found"),r.innerHTML=l.message,console.log("Form Message",l)}})},o=l=>{if(e(l))return console.log("No input fields found"),!1;let o=t(l,".wp-block-gutena-forms");if(e(o))return console.log("Form not defined"),!1;o=o[0];let s=o.getAttribute("data-validation-messages");if(e(s))s={};else try{s=JSON.parse(s)}catch(e){console.log("Error parsing custom validation messages",e),s={}}let i="",n=r(l,"required-field"),a=t(l,".wp-block-gutena-field-group");if(e(a))return console.log("field_group not defined"),!1;a=a[0];let d=r(l,"checkbox-field")||r(l,"radio-field")||r(l,"optin-field");if(d){let r=l.querySelectorAll("input");if(e(r))return console.log("checkboxRadioHtml not defined"),!1;for(let e=0;e<r.length;e++)if(r[e].checked){i=r[e].value;break}}else i=l.value;let u=a.querySelector(".gutena-forms-field-error-msg");if(e(u))return console.log("errorHTML not defined"),!1;if(a.classList.remove("display-error"),n&&(e(i)||r(l,"select-field")&&"select"===i)){a.classList.add("display-error");let t=e(s.required_msg)?gutenaFormsBlock.required_msg:s.required_msg;return r(l,"select-field")&&(t=e(s.required_msg_select)?gutenaFormsBlock.required_msg_select:s.required_msg_select),r(l,"optin-field")?t=e(s.required_msg_optin)?gutenaFormsBlock.required_msg_optin:s.required_msg_optin:d&&(t=e(s.required_msg_check)?gutenaFormsBlock.required_msg_check:s.required_msg_check),u.innerHTML=t,!1}if(!e(i)&&r(l,"email-field")&&!i.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,}))$/))return a.classList.add("display-error"),u.innerHTML=e(s.invalid_email_msg)?gutenaFormsBlock.invalid_email_msg:s.invalid_email_msg,!1;if(!e(i)&&r(l,"number-field")){let r=l.getAttribute("min"),t=l.getAttribute("max");if(!e(r)&&i<r){a.classList.add("display-error");let t=e(s.min_value_msg)?gutenaFormsBlock.min_value_msg:s.min_value_msg;return u.innerHTML=t+" "+r,!1}if(!e(t)&&i>t){a.classList.add("display-error");let r=e(s.max_value_msg)?gutenaFormsBlock.max_value_msg:s.max_value_msg;return u.innerHTML=r+" "+t,!1}}return!0},s=(r,t)=>{if(!e(r)){let l=r.querySelector(".range-input-value");e(r)||(l.innerHTML=t)}};(()=>{let e=document.querySelectorAll(".wp-block-gutena-forms .range-field");if(0<e.length)for(let r=0;r<e.length;r++)s(e[r].parentNode,e[r].value),e[r].addEventListener("input",function(){s(this.parentNode,this.value)})})(),(()=>{let e=document.querySelectorAll(".wp-block-gutena-forms .gutena-forms-field");if(0<e.length)for(let r=0;r<e.length;r++)e[r].addEventListener("input",function(){o(e[r])})})(),(()=>{let e=document.querySelectorAll(".wp-block-gutena-forms .gutena-forms-submit-button");if(0<e.length)for(let r=0;r<e.length;r++)e[r].addEventListener("click",function(s){s.preventDefault();let i=t(this,".wp-block-gutena-forms");if(void 0===i)return void console.log("Form not defined");i=i[0];let n=i.querySelectorAll(".gutena-forms-field");if(0===n.length)return void console.log("No input fields found");let a=!0,d=n[0];for(let e=0;e<n.length;e++)!1===o(n[e])&&(!0===a&&(d=n[e]),a=!1);if(!1===a)return i.classList.add("display-error-message"),void d.scrollIntoView({behavior:"smooth"});i.classList.add("form-progress");let u=this.querySelector(".wp-block-button__link"),c=u.innerHTML;this.disabled=!0,u.innerHTML='<div class="gutena-forms-btn-progress"><div></div><div></div><div></div><div></div></div>';let m=new FormData(i);m.append("nonce",gutenaFormsBlock.nonce),m.append("action",gutenaFormsBlock.submit_action),i.classList.remove("display-error-message"),i.classList.remove("display-success-message"),l(i,m,e[r],u,c)})})(),setTimeout(()=>{},2e3)});