text: // Copyright (c) 2018-2021 Alexander Kwaschny, kwaschny.net
  SimpleV=function(e,t){if(!(this instanceof SimpleV))return new SimpleV(e,t);var i,n,a,c=this;for(this._validationTypes=["email"],this._cache=[],this._cacheTracking=-1,this._groupNames={},this.validate=function(){for(var e=!0,t=null,i=c.form.getElementsByTagName("input"),n=i.length,a=0;a<n;a++)switch(i[a].type){case"text":case"password":case"search":if(!1===jQuery(i[a]).is(":visible")){!0===c.options.log&&console.warn("SimpleV: Element not visible. Element will not be validated.",i[a]);continue}!1===c._validateText(i[a],!0)&&(null===t&&(t=i[a]),e=!1);break;case"checkbox":case"radio":if(!1===jQuery(i[a]).is(":visible")){!0===c.options.log&&console.warn("SimpleV: Element not visible. Element will not be validated.",i[a]);continue}!1===c._validateDecision(i[a],!0)&&(null===t&&(t=i[a]),e=!1)}for(n=(i=c.form.getElementsByTagName("textarea")).length,a=0;a<n;a++)!1!==jQuery(i[a]).is(":visible")?!1===c._validateText(i[a],!0)&&(null===t&&(t=i[a]),e=!1):!0===c.options.log&&console.warn("SimpleV: Element not visible. Element will not be validated.",i[a]);for(n=(i=c.form.getElementsByTagName("select")).length,a=0;a<n;a++)!1!==jQuery(i[a]).is(":visible")?!1===c._validateDecision(i[a])&&(null===t&&(t=i[a]),e=!1):!0===c.options.log&&console.warn("SimpleV: Element not visible. Element will not be validated.",i[a]);return!1===e&&!0===c.options.scrollIntoView&&null!==t&&!1===c._isElementInViewport(t)&&(!0===c.options.log&&console.info("SimpleV: First element not passing validation is not visible in the current viewport. Scrolling into view...",t),c._scrollIntoView(t)),e},this.onValid=null,this.onInvalid=null,this._prepareElement=function(e){var t=e.getAttribute("data-v");if(null===t)return!1;if("string"==typeof e.name&&0<e.name.length){if("object"==typeof c._groupNames[e.name])return!0===c.options.log&&console.info('SimpleV: Element shares the "name" attribute with an already prepared element. Directives can only be declared once.',e),!1;c._groupNames[e.name]=e}if("string"!=typeof t||0===t.length)return!0===c.options.log&&console.warn("SimpleV: No directives found. Element will not be validated.",e),!1;var i,n=c._transformDirectives(t);if(null===n)return!0===c.options.log&&console.error("SimpleV: Invalid directives encountered. Element will not be validated.",e),!1;switch(n.max<1/0&&(e.maxLength=n.max),(n.element=e).type){case"checkbox":case"radio":0<e.name.length&&(i=e.tagName.toLowerCase(),n.groupedWith=c.form.querySelectorAll(i+'[name="'+e.name+'"]'))}return c._cacheDirectives(e,n),n},this._transformDirectives=function(e){if("string"!=typeof e)return null;for(var t={id:null,required:!1,type:null,min:-1/0,max:1/0,trim:!0,pattern:null,element:null,groupedWith:[],message:null,realtime:!1},i=e.split(","),n=i.length,a=0;a<n;a++){var s=i[a].trim();if("required"===s)t.required=!0;else if(/^(min|max)\s*[0-9]+$/.test(s)){var l=s.match(/^(min|max)\s*([0-9]+)$/);"min"===l[1]?t.min=parseInt(l[2],10):t.max=parseInt(l[2],10)}else if(/^\/.+\/$/.test(s)){var o=s.substring(1,s.length-1),o=new RegExp(o);t.pattern=o}else if("notrim"===s)t.trim=!1;else{if(-1===c._validationTypes.indexOf(s))return null;t.type=s}}return t},this._cacheDirectives=function(e,t){var i=++c._cacheTracking;t.id=i,e.setAttribute("data-v-cached",i),c._cache[i]=t},this._validateText=function(e,t){var i=1===e.nodeType?e:e.target,n=i.getAttribute("data-v-cached");if(null!==n){var a=c._cache[n];if(!0===t)a.realtime=!0;else if(!1===a.realtime)return;var s=i.value;!0===a.trim&&(s=s.trim());var l,o,r,d=s.length,m=[];switch(a.required&&0===d&&m.push("INPUT_REQUIRED"),a.type){case"email":(!0===a.required||0<d)&&(o=1<=(l=s.indexOf("@")),r=s.indexOf(".",l),o&&(2<=r-l&&r<d-1)||m.push("INPUT_EMAIL"))}return a.min>-1/0&&d<a.min&&m.push("INPUT_MIN"),a.max<1/0&&d>a.max&&m.push("INPUT_MAX"),null!==a.pattern&&(!0===a.required||0<d)&&!1===a.pattern.test(s)&&m.push("INPUT_PATTERN"),0<m.length?(c._showMessage(n,m),!1):(c._hideMessage(n),!0)}},this._registerTextValidation=function(e){var t=e.target;!0===t.autofocus&&(t.autofocus=!1,0===t.value.length)||c._validateText(e,!0)},this._validateDecision=function(e){var t=1===e.nodeType?e:e.target,i=t.getAttribute("data-v-cached");if(null!==i){var n,a,s=c._cache[i],l=[],o=s.groupedWith.length;if(s.required)if("SELECT"===t.tagName)""===t.value&&l.push("SELECTION_REQUIRED");else{var r=!1;if(0<o){for(n=0;n<o;n++)if(!0===s.groupedWith[n].checked){r=!0;break}}else r=t.checked;!1===r&&l.push("SELECTION_REQUIRED")}if(s.min>-1/0&&0<o){for(n=a=0;n<o;n++)!0===s.groupedWith[n].checked&&a++;a<s.min&&l.push("SELECTION_MIN")}if(s.max<1/0&&0<o){for(n=a=0;n<o;n++)!0===s.groupedWith[n].checked&&a++;a>s.max&&l.push("SELECTION_MAX")}return 0<l.length?(c._showMessage(i,l),!1):(c._hideMessage(i),!0)}},this._registerDecisionValidation=function(e){c._validateDecision(e)},this._createMessage=function(e){var t=document.createElement("div");if(t.className=c.options.className,"function"==typeof c.options.position)c.options.position(t,e);else switch(c.options.position){case"append":e.parentElement.appendChild(t);break;case"prepend":e.parentElement.insertBefore(t,e);break;default:throw new Error('SimpleV: Invalid option value encountered for "position". The value "'+c.options.position+'" is not supported.')}return t},this._showMessage=function(e,t){if(0!==t.length){var i=c._cache[e],n=i.groupedWith.length;if(0<n)for(var a=0<=t.indexOf("SELECTION_MIN"),s=0;s<n;s++)a&&!0===i.groupedWith[s].checked?(i.groupedWith[s].classList.remove(c.options.invalidClass),i.groupedWith[s].classList.add(c.options.validClass)):(i.groupedWith[s].classList.remove(c.options.validClass),i.groupedWith[s].classList.add(c.options.invalidClass));else i.element.classList.remove(c.options.validClass),i.element.classList.add(c.options.invalidClass);null===i.message&&(i.message=c._createMessage(i.element));var l=t[0];if("string"!=typeof SimpleV.l10n[l])throw new Error("SimpleV: Missing localization entry in SimpleV.l10n for reason: "+l);var o=i.element.getAttribute("data-v-msg"),r="string"==typeof o&&0<o.length?o:SimpleV.l10n[l];r=(r=r.replace(/\{min\}/g,i.min)).replace(/\{max\}/g,i.max),i.message.innerHTML=r,i.message.removeAttribute("hidden")}},this._hideMessage=function(e){var t=c._cache[e],i=t.groupedWith.length;if(0<i)for(var n=0;n<i;n++)t.groupedWith[n].classList.remove(c.options.invalidClass),t.groupedWith[n].classList.add(c.options.validClass);else t.element.classList.remove(c.options.invalidClass),t.element.classList.add(c.options.validClass);null!==t.message&&(t.message.setAttribute("hidden",""),t.message.innerHTML="")},this._isElementInViewport=function(e){var t=e.getBoundingClientRect();return 0<=t.top&&0<=t.left&&t.bottom<=window.innerHeight&&t.right<=window.innerWidth},this._scrollIntoView=function(e){jQuery("html, body").animate({scrollTop:jQuery(e).offset().top+c.options.scrollIntoViewOffset},c.options.scrollIntoViewDuration)},this.defaultOptions={delayValidation:!0,scrollIntoView:!0,scrollIntoViewDuration:600,scrollIntoViewOffset:-32,position:"append",className:"simplev-message",validClass:"simplev-valid",invalidClass:"simplev-invalid",log:!1},this.options=jQuery.extend({},this.defaultOptions,t),this.form=e,this.form.addEventListener("submit",function(e){var t=c.validate()?"function"!=typeof c.onValid||c.onValid(c):"function"==typeof c.onInvalid&&c.onInvalid(c);return!1===t&&e.preventDefault(),t}),n=(i=this.form.getElementsByTagName("input")).length,a=0;a<n;a++)switch(i[a].type){case"text":case"password":case"search":if(!1===this._prepareElement(i[a]))continue;i[a].addEventListener("input",c._validateText),i[a].addEventListener("blur",c._registerTextValidation);break;case"checkbox":case"radio":var s=this._prepareElement(i[a]);if(!1===s)continue;var l=s.groupedWith.length;if(0<l)for(var o=0;o<l;o++)s.groupedWith[o].setAttribute("data-v-cached",s.id),s.groupedWith[o].addEventListener("change",c._validateDecision);else i[a].addEventListener("change",c._validateDecision)}for(n=(i=this.form.getElementsByTagName("textarea")).length,a=0;a<n;a++)!1!==this._prepareElement(i[a])&&i[a].addEventListener("input",c._validateText);for(n=(i=this.form.getElementsByTagName("select")).length,a=0;a<n;a++)!1!==this._prepareElement(i[a])&&i[a].addEventListener("change",c._validateDecision)},SimpleV.l10n={INPUT_REQUIRED:"This field is required.",INPUT_MIN:"This field must contain at least {min} characters.",INPUT_MAX:"This field must not exceed a total number of {max} characters.",INPUT_PATTERN:"This field does not match the expected pattern.",INPUT_EMAIL:"This field is not a valid e-mail address.",SELECTION_REQUIRED:"Selection is required.",SELECTION_MIN:"Select at least {min} options.",SELECTION_MAX:"Select no more than {max} options."},SimpleV.forms=[],SimpleV.form=null,SimpleV.init=function(e,t){if("object"==typeof e)1!==e.nodeType&&(t=e,e=void 0);else if("string"==typeof e)e=document.querySelector(e);else if(void 0!==e)throw new Error("SimpleV: Invalid arguments encountered for function: init");var i;void 0===e&&(e=document.body);for(var n=(i="FORM"===e.tagName?[e]:e.querySelectorAll("form[data-v]")).length,a=0;a<n;a++){i[a].hasAttribute("data-v-cached")?void 0!==t&&!0===t.log&&console.warn("SimpleV: Form already initialized.",i[a]):"false"!==i[a].getAttribute("data-v")?(i[a].setAttribute("data-v-cached",SimpleV.forms.length),SimpleV.forms.push(new SimpleV(i[a],t)),1===SimpleV.forms.length&&(SimpleV.form=SimpleV.forms[0])):void 0!==t&&!0===t.log&&console.warn("SimpleV: Form explicitly skipped.",i[a])}},SimpleV.isValid=function(e){SimpleV.init(e);for(var t=0;t<SimpleV.forms.length;t++)if(SimpleV.forms[t].form===e)return SimpleV.forms[t].validate();return!1},SimpleV.version="0.2";