| 1 |
var submit_form=function(){"use strict";function e(e,t){const a=new URL(e?.ajaxURL),o=e?.entryId?"bitforms_update_form_entry":"bitforms_submit_form";return a.searchParams.append("action",o),e?.entryId&&(a.searchParams.append("_ajax_nonce",e.nonce||""),a.searchParams.append("entryID",e.entryId),a.searchParams.append("formID",e.formId),a.searchParams.append("entryToken",e.entryToken||"")),fetch(a,{method:"POST",body:t})}const t=[403,406];function a(e){const t=window.bf_globals?.[e],a={contentId:e,message:t?.wafBlockMsg||"Your submission was blocked by the site's security firewall."};return bfSelect(`#form-${e}`)?.dispatchEvent(new CustomEvent("bf-form-waf-blocked",{bubbles:!0,detail:a})),a.message}function o(e){const t=window.bf_globals?.[e];if(!t)return;const a=bfSelect(`#form-${e}`);try{if(t.turnstileSiteKey&&window.turnstile){const e=bfSelect(".cf-turnstile",a);e?window.turnstile.reset(e):window.turnstile.reset()}t.hCaptchaSiteKey&&window.hcaptcha&&window.hcaptcha.reset(),t.gRecaptchaSiteKey&&"v2"===t.gRecaptchaVersion&&window.grecaptcha&&window.grecaptcha.reset()}catch(e){}}function n(e,n,s){e.then(e=>e.text().then(o=>{let r=null;try{r=JSON.parse(o)}catch(e){r=null}if(null===r){const o=t.includes(e.status),r=new Error(o?"waf-blocked":"Invalid response");throw r.wafBlocked=o,r.data={data:o?a(n):"Invalid server response. Check the browser console for more details."},r}if(e.status>=400){const e=new Error(r?.data||"Request failed");throw e.data=r,e}return r})).then(e=>{let t=null,a=null,c="",d=1e3;const i=window.bf_globals[n];if(void 0!==e&&e.success){const o=new CustomEvent("bf-form-submit-success",{detail:{formId:n,entryId:e.data?.entry_id,formData:s}});bfSelect(`#form-${n}`).dispatchEvent(o);const r=bfSelect(`#form-${n}`),l="object"==typeof e.data&&e.data.afterSubmit?e.data.afterSubmit:"reset";if("keep"!==l&&bfReset(n),"hide"===l&&r&&r.remove(),"object"==typeof e.data){r&&"hide"!==l&&e?.data?.hidden_fields?.map(e=>{setHiddenFld(e,r)}),t=e.data.redirectPage,e.data.cron&&(a=e.data.cron),e.data.cronNotOk&&(a=e.data.cronNotOk),e.data.new_nonce&&(c=e.data.new_nonce,i.nonce=c);let o=!1;!e.data.suppressConfirmation&&e.data.message&&(o=!0,setBFMsg({contentId:n,msgId:e.data.msg_id,msg:e.data.message,duration:e.data.msg_duration,show:!0,type:"success",error:!1})),d=o?Number(e.data.msg_duration||1e3):0}else setBFMsg({contentId:n,msg:e.data,type:"success",show:!0,error:!1});if(localStorage.removeItem(`bitform-partial-form-${i.formId}`),"hide"!==l){const e=bfSelect('input[name="entryID"]',r);e&&e.remove()}delete i.entryId}else{const t=new CustomEvent("bf-form-submit-error",{detail:{formId:n,errors:e.data}});bfSelect(`#form-${n}`).dispatchEvent(t),bfValidationErrMsg(e,n),o(n)}if(function(e,t,a){const o=window.bf_globals[a];if(e)if("string"==typeof e){const t=new URL(e);t.protocol!==window.location.protocol&&(t.protocol=window.location.protocol),fetch(t,{keepalive:!0}).catch(()=>{})}else{const a=new URL(o.ajaxURL);a.searchParams.append("action","bitforms_trigger_workflow");const n={cronNotOk:e,token:t||o.nonce,id:o.appID},r=()=>fetch(a,{method:"POST",body:JSON.stringify(n),headers:{"Content-Type":"application/json"},keepalive:!0});r().catch(()=>{setTimeout(()=>{r().catch(()=>{})},2e3)})}}(a,c,n),t){const e=setTimeout(()=>{window.location=decodeURI(t),e&&clearTimeout(e)},d)}r(n,!1)}).catch(e=>{if(e&&"object"==typeof e&&"data"in e){const t=e.data,a=new CustomEvent("bf-form-submit-error",{detail:{formId:n,errors:t?.data??t}});if(bfSelect(`#form-${n}`)?.dispatchEvent(a),"undefined"!=typeof bfValidationErrMsg)bfValidationErrMsg(t??{data:e.message},n);else{const a=t&&"string"==typeof t.data?t.data:e?.message||"Validation failed";setBFMsg({contentId:n,msg:a,show:!0,type:"error",error:!0})}}else{const t=e?.message?e.message:"Unknown Error";setBFMsg({contentId:n,msg:t,show:!0,type:"error",error:!0})}o(n),r(n,!1)})}function r(e,t){const a=bfSelect('button[type="submit"]',bfSelect(`#form-${e}`));a&&(a.disabled=t);const o=bfSelect('button[type="submit"] span',bfSelect(`#form-${e}`));o&&o.classList.toggle("d-none")}return function(t=null){(t?[t]:Object.keys(window.bf_globals||{})).forEach(t=>{const a=bfSelect(`#form-${t}`);a&&(a.addEventListener("submit",t=>{!function(t){t.preventDefault();const a=t.target.id.slice(t.target.id.indexOf("-")+1);if("undefined"!=typeof validateForm&&!validateForm({form:a})){const e=new CustomEvent("bf-form-validation-error",{detail:{formId:a,fieldId:"",error:""}});return void t.target.dispatchEvent(e)}r(a,!0);let o=new FormData(t.target);const s=window.bf_globals[a];"undefined"!=typeof advancedFileHandle&&(o=advancedFileHandle(s,o)),"undefined"!=typeof decisionFldHandle&&(o=decisionFldHandle(s,o)),"undefined"!=typeof hideChildFldHandle&&(o=hideChildFldHandle(s,o)),s.GCLID&&o.set("GCLID",s.GCLID);const c=[];Object.entries(s?.fields||{}).forEach(e=>{e[1]?.valid?.hide&&c.push(e[0])}),c.length&&o.append("hidden_fields",c),"v3"===s?.gRecaptchaVersion&&s?.gRecaptchaSiteKey?grecaptcha.ready(()=>{grecaptcha.execute(s.gRecaptchaSiteKey,{action:"submit"}).then(t=>{o.append("g-recaptcha-response",t),n(e(s,o),a,o)}).catch(()=>{r(a,!1),setBFMsg({contentId:a,msg:"reCAPTCHA verification failed. Please try again.",show:!0,type:"error",error:!0})})}):n(e(s,o),a,o)}(t)}),bfSelect('button[type="reset"]',a)?.addEventListener("click",()=>bfReset(a.id.replace("form-",""),!0)))}),document.querySelectorAll(".msg-backdrop,.bf-msg-close").forEach(e=>{e.addEventListener("click",t=>{t.target===e&&(t.stopPropagation(),bfSelect(`#${e.dataset.contentid} .msg-container-${e.dataset.msgid}`).classList.replace("active","deactive"))})}),localStorage.setItem("bf-entry-id","")}}(); |