!function(){"use strict";var l={};function r(e){var r,a,t=e.target.closest(".cf-control-group");return t&&(e=e.target.closest("form"),a=e.closest(".cf").dataset.id,(l.hasOwnProperty(a)?l[a]:(e=e.querySelectorAll("*[data-calc]"),r=[],e.forEach(function(e){for(var a,t=e.dataset.calc,l=/{field.(.*?)}/g;null!==(a=l.exec(t));)a.index===l.lastIndex&&l.lastIndex++,r.includes(a[1])||r.push(a[1])}),l[a]=r)).includes(t.dataset.name))}function n(e,a){for(var t=new exprEval.Parser,l=0;l<a.length;l++){var r=function(e){for(var a={},t=0;t<e.elements.length;t++){var l,r,n,c=e.elements[t];c.name&&!c.disabled&&"button"!=c.type&&"file"!=c.type&&"reset"!=c.type&&"submit"!=c.type&&-1!=c.name.indexOf("cf[")&&(l=c.closest(".cf-control-group"))&&(r=c.dataset.calcValue||c.value,"checkbox"!=c.type&&"radio"!=c.type||(r=c.checked?r:0),"select-one"==c.type&&(r=c.options[c.selectedIndex].dataset.calcValue),c=l.dataset.key,"string"==typeof r&&(r=r.replace(",",".")),r=parseFloat(r),r=isNaN(r)?0:r,a.hasOwnProperty(c)&&(n=a[c].field_value,r+=n),a[c]={field_value:r,field_name:l.dataset.name})}return a}(e),n=a[l],c={};if(f=n.dataset.calc){for(var o=0,s=Object.entries(r);o<s.length;o++){var d=s[o],i=d[0],d=d[1],u=new RegExp("{field."+d.field_name+"}","gi"),f=f.replace(u,"{field"+i+"}");c["field"+i]=d.field_value}f=(f=f.replace(/{/g,"")).replace(/}/g,"");var p,v,m,y,g,r=void 0;try{r=t.evaluate(f,c)}catch(e){console.error("Convert Forms failed to evalute calculation formula:",f,"- Make sure you're usign the correct syntax: {field.FIELDNAME}")}isNaN(r)||(p=n.dataset.thousand_separator||null,v=n.dataset.decimal_separator||null,g=n.dataset.precision||2,m=n.dataset.prefix||null,y=n.dataset.suffix||null,r=parseFloat(r).toFixed(g),g=r=v&&"."!=v?r.replace(".",v):r,p&&(g=g.replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1"+p)),m&&(g=m+g),y&&(g+=y),"INPUT"===n.nodeName?(ConvertForms.Helper.setValue(n,g),n.dataset.calcValue=r):n.innerHTML=g)}}}ConvertForms.Helper.onReady(function(e){e.forEach(function(e){var a=e.querySelector("form"),t=a.querySelectorAll("*[data-calc]");0<t.length&&(a.addEventListener("input",function(e){r(e)&&n(a,t)}),a.addEventListener("change",function(e){r(e)&&n(a,t)}),n(a,t))})})}(document);

