/*! ========= INFORMATION ============================
	- author:    CalcHub
	- url:       https://calchub.xyz
	- plugin:    https://wordpress.org/plugins/calculator-builder/
==================================================== */
"use strict";const CalculatorBuilder=function(){function e(e){let t=[];const o=e.querySelectorAll("fieldset");for(let e=0;e<o.length;e++){t[e+1]=o[e]}return t}function t(e){let t=[];const o=e.querySelectorAll(".formbox__title");for(let e=0;e<o.length;e++){t[e+1]=o[e]}return t}function o(e){const t=e.querySelectorAll('[name^="formbox-field-"], button');let o=[];for(let e=0;e<t.length;e++){o[e+1]=t[e]}return o}function r(e,t){let o=[];const r=e.querySelectorAll('[name^="formbox-field-"]');for(let e of t.entries()){o[e[0].split("-")[2]]=parseFloat(e[1])}for(let e=0;e<r.length;e++){if("textarea"===r[e].tagName.toLowerCase()){o[r[e].getAttribute("name").split("-")[2]]=r[e].value}if("input"===r[e].tagName.toLowerCase()&&"number"!==r[e].getAttribute("type")&&"radio"!==r[e].getAttribute("type")){o[r[e].getAttribute("name").split("-")[2]]=r[e].value}if("input"===r[e].tagName.toLowerCase()&&"checkbox"===r[e].getAttribute("type")){let t=r[e].getAttribute("name").split("-");r[e].checked?o[t[2]]=parseFloat(r[e].value):o[t[2]]=0}}return o}function n(e){"Enter"!==e.key&&a(this.form,"add")}function i(n){n.preventDefault();const i=this.form,l=t(this.form),s=e(this.form),d=o(this.form),u=new FormData(this.form);let f=r(this.form,u),m=window[this.form.id](f,s,d,l,i);if("string"==typeof m)return c(i,"show",m),a(i,"add"),!1;c(i,"hide");const h=this.form.querySelectorAll('[name^="formbox-field-"]');for(let e=0;e<h.length;e++)if(h[e].hasAttribute("required")&&""===h[e].value)return h[e].focus({preventScroll:!1}),!1;const p=this.form.querySelectorAll(".formbox__field-result");for(let e=1;e<m.length;e++){let t=p[e-1];"input"!==t.tagName.toLowerCase()&&"textarea"!==t.tagName.toLowerCase()||(t.value=m[e]),"textarea"===t.tagName.toLowerCase()&&t.hasAttribute("hidden")&&(t.nextElementSibling.innerHTML=m[e]),a(this.form,"remove")}}function l(n){n.preventDefault();const i=t(this.form),l=e(this.form),s=o(this.form),a=new FormData(this.form);let c=r(this.form,a);window[this.form.id](c,l,s,i)}function s(){a(this.form,"add");const n=t(this.form),i=e(this.form),l=o(this.form);let s=r(this.form,this.data);window[this.form.id](s,i,l,n),c(this.form,"hide")}function a(e,t){let o=e.querySelectorAll(".formbox__container.has-result");o&&o.forEach((e=>{"remove"===t?e.classList.remove("is-hidden"):e.classList.add("is-hidden")}))}function c(e,t,o=""){const r=e.querySelector(".has-alert");if("hide"===t&&r){r.classList.add("is-hidden");r.querySelector(".formbox__field-alert").innerHTML=""}else if("show"===t&&r){const e=r.querySelector(".formbox__field-alert");r.classList.remove("is-hidden"),e.innerHTML=o}}document.querySelectorAll(".formbox").forEach((e=>{const t=e.querySelectorAll('input[type="range"]');t.length>0&&t.forEach((e=>{const t=e.closest(".formbox__field").querySelector(".formbox__field-range-value");t&&(e.oninput=function(){t.innerHTML=this.value})}));const o={handleEvent:i,form:e};let r=e.querySelector(".formbox__btn-calc");e.querySelector(".calc-only-click")?(e.querySelectorAll("input, select, button.formbox__btn-calc").forEach((e=>{e.addEventListener("keydown",(function(e){"Enter"!==e.key&&13!==e.keyCode||e.preventDefault()}))})),r&&r.addEventListener("click",o)):e.addEventListener("submit",o);const a=new FormData(e);e.addEventListener("reset",{handleEvent:s,form:e,data:a}),e.querySelector(".calc-reset")&&(e.addEventListener("change",{handleEvent:n,form:e}),e.addEventListener("keyup",{handleEvent:n,form:e})),e.querySelector(".calc-not-change")||(e.addEventListener("change",o),e.addEventListener("keyup",o)),e.querySelector(".calc-load")&&window.addEventListener("load",o),window.addEventListener("load",{handleEvent:l,form:e})})),Element.prototype.hide=function(){this.classList.add("is-hidden")},Element.prototype.show=function(){this.classList.remove("is-hidden")},Element.prototype.addClass=function(e){this.classList.add(e)},Element.prototype.removeClass=function(e){this.classList.remove(e)},Element.prototype.addAttr=function(e,t){e=e||"",t=t||"",this.setAttribute(e,t)},Element.prototype.removeAttr=function(e){e=e||"",this.removeAttribute(e)},Element.prototype.text=function(e){e=e||"",this.innerText=e},Number.prototype.round=function(e="2"){const t=Math.pow(10,parseInt(e));return Math.round(this*t)/t}};document.addEventListener("DOMContentLoaded",(function(){new CalculatorBuilder}));const roundVal=(e,t="2")=>{const o=Math.pow(10,parseInt(t));return Math.round(e*o)/o};function checkCalcVariable(e,t){return""!==t?t:e}