PokeYoke=Class.create();PokeYoke.prototype={initialize:function(F){this.pkykValidationRules=F;this.pkyk=new Object();if(pkykConf.checkOnBlur){for(var B in this.pkykValidationRules){var A=null;var G=document.getElementsByName(B);for(var E=0;E<G.length;E++){switch(G[E].tagName){case"INPUT":case"SELECT":case"TEXTAREA":A=G[E];break}}if(A){this.registerFieldBlur(A)}else{}}}if(pkykConf.checkOnSubmit){for(var B in this.pkykValidationRules){var D=document.getElementsByName(B);this.registerFormSubmit(D[0].form);return;}}},registerFieldBlur:function(A){Event.observe(A,"blur",this.checkFieldOnBlur.bind(this),false)},registerFormSubmit:function(A){Event.observe(A,"submit",this.checkFormOnSubmit.bind(this),false)},checkFieldOnBlur:function(A){window.setTimeout(function(){this.validateField(Event.element(A))}.bind(this),100);},checkFormOnSubmit:function(A){var C=Event.element(A);var D=Form.getElements(C);for(var B=0;B<D.length;B++){this.validateField(D[B])}this.renderGlobalErrors(C);if(Object.values(this.pkyk).length>0){Event.stop(A);return false}else{return true}},validateField:function(target){var required=false;var elementError=false;var errorMsg="";var targetName=target.name;var targetValue=this.trim($F(target));if(target.disabled){return }if(this.pkykValidationRules[target.name]){if(pkykConf.debug){alert(target.name+" is being validated")}if(this.pkykValidationRules[target.name]["required"]){required=this.pkykValidationRules[target.name]["required"]}if(required){if(!targetValue.match(/.+/)){errorMsg=this.pkykValidationRules[target.name]["required_msg"];elementError=true}else{errorMsg="";elementError=false}}var validators=this.pkykValidationRules[target.name]["validators"];if(validators!=undefined&&!elementError){for(var validatorName in validators){try{eval("validator = new "+validatorName+"()")}catch(e){continue}validator.initialize=function(parameters){try{for(var parameter in parameters){this[parameter]=parameters[parameter]}}catch(e){}};if(this.pkykValidationRules[target.name]["validators"][validatorName]["parameters"]){validator.initialize(this.pkykValidationRules[target.name]["validators"][validatorName]["parameters"])}errorMsg=validator.execute(targetValue);if(errorMsg){elementError=true;continue}}}if(elementError){this.pkyk[targetName]={target:target,errorMsg:errorMsg};this.renderInlineError(target,errorMsg)}else{this.removeErrors(target)}}},renderInlineError:function(A,C){if(pkykConf.displayBlurInlineErrors){var B=$(pkykConf.inlineErrorId+A.name);if(B){B.innerHTML=pkykConf.errorPrefix+C+pkykConf.errorSuffix;B.show()}else{new Insertion.After(A,'<div id="'+pkykConf.inlineErrorId+A.name+'" class="'+pkykConf.inlineErrorClass+'">'+C+"</div>")}}},renderGlobalErrors:function(E){if(pkykConf.displaySubmitGlobalErrors&&Object.values(this.pkyk).length>0){if($("pkykGlobalErrors")){$("pkykGlobalErrors").remove()}var G="";var F=(pkykConf.globalErrorTitle.length>0)?'<p class="'+pkykConf.globalErrorTitleClass+'">'+pkykConf.globalErrorTitle+"</p>":"";if(Object.values(this.pkyk).length>0){for(var D=0;D<Object.values(this.pkyk).length;D++){var B=Object.values(this.pkyk)[D].target.name;var A=pkykConf.errorPrefix+Object.values(this.pkyk)[D].errorMsg+pkykConf.errorSuffix;G+='<li class="'+pkykConf.globalErrorClass+'" id="pkykGlobal_'+B+'">'+A+"</li>"}new Insertion.Top(E,'<div id="pkykGlobalErrors">'+F+"<ul>"+G+"</ul></div>")}if(pkykConf.globalErrorFocus&&$("pkykGlobalErrors")){var C=$("pkykGlobalErrors").getElementsByTagName("li");for(var D=0;D<C.length;D++){Event.observe(C[D],"click",this.getFieldFocus.bind(this),false)}}}},removeErrors:function(B){if($(pkykConf.inlineErrorId+B.name)){$(pkykConf.inlineErrorId+B.name).innerHTML="";$(pkykConf.inlineErrorId+B.name).hide()}if($("pkykGlobal_"+B.name)){var A=$("pkykGlobal_"+B.name);Element.remove(A)}if(this.pkyk[B.name]){delete this.pkyk[B.name]}if(Object.values(this.pkyk).length==0&&$("pkykGlobalErrors")){$("pkykGlobalErrors").remove()}},getFieldFocus:function(A){var D=Event.element(A);var B=D.id;var C=$(B.replace(/pkykGlobal_/,""));if(C){C.scrollTo();Field.select(C)}},trim:function(A){cleanValue="";if(A){cleanValue=A.gsub(/^\s+/,"");cleanValue=cleanValue.gsub(/\s+$/,"")}return cleanValue}};var sfStringValidator=function(){this.insensitive=false;this.max=null;this.max_error="Input is too long";this.min=null;this.min_error="Input is too short";this.values=null;this.values_error="Invalid selection"};sfStringValidator.prototype.execute=function(A){if((this.min!=null)&&(A.length<this.min)){return this.min_error}if((this.max!=null)&&(A.length>this.max)){return this.max_error}if(this.values!=null){for(i=0;i<this.values.length;i++){if((this.insensitive?(this.values[i].toLowerCase()):(this.values[i]))==(this.insensitive?(A.toLowerCase()):(A))){return false}}return this.values_error}return false};var sfNumberValidator=function(){this.max=null;this.max_error="Input is too large";this.min=null;this.min_error="Input is too small";this.nan_error="Input is not a number";this.type="any";this.type_error="Wrong input"};sfNumberValidator.prototype.execute=function(A){if(isNaN(A)){return this.nan_error}iValue=parseInt(A,10);fValue=parseFloat(A);if((this.type=="int")||(this.type=="integer")){if(fValue!=iValue){return this.type_error}pValue=iValue}else{pValue=fValue}if((this.min!=null)&&(pValue<this.min)){return this.min_error}if((this.max!=null)&&(pValue>this.max)){return this.max_error}return false};var sfRegexValidator=function(){this.match=true;this.match_error="Invalid input";this.pattern=null};sfRegexValidator.prototype.execute=function(value){if(this.pattern!=null){match=(this.match=='true' ? true : false );pos=0;unescaped="";while(pos<this.pattern.length){c=this.pattern.charAt(pos);if(c=="\\"){pos++}try{unescaped+=this.pattern.charAt(pos)}catch(e){}pos++}try{eval("regExp = "+unescaped);value=String(value);if((value.match(regExp)&&match)||(!value.match(regExp)&&!match)){return false}else{return this.match_error}}catch(e){return false}}else{return false}};var sfEmailValidator=function(){this.email_error="Invalid input"};sfEmailValidator.prototype.execute=function(A){regExp=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$|^$/;if(regExp.test(A)){return false}else{return this.email_error}};var sfCompareValidator=function(){this.check=null;this.compare_error="The values you entered do not match. Please try again"};sfCompareValidator.prototype.execute=function(A){if(this.check!=null){check_field=document.getElementById(this.check);if(check_field!=null){check_value=check_field.value;if(check_value!=A){return this.compare_error}}}return false};var sfUrlValidator=function(){this.url_error="Please enter a correctly formatted url"};sfUrlValidator.prototype.execute=function(A){regExp=/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)|^$/i;if(regExp.test(A)){return false}else{return this.url_error}}
