/*
 * jQuery bValidator plugin
 *
 * http://code.google.com/p/bvalidator/
 *
 * Copyright (c) 2012 Bojan Mauser
 *
 * $Id: jquery.bvalidator.js 99 2012-08-18 13:11:54Z bmauser $
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */
(function(a){a.fn.bValidator=function(c,b){return this.each(function(){new bValidator(a(this),c,b)})};bValidator=function(q,r,x){var e={singleError:false,offset:{x:-23,y:-4},position:{x:"right",y:"top"},template:'<div class="{errMsgClass}"><em/>{message}</div>',templateCloseIcon:'<div style="display:table"><div style="display:table-cell">{message}</div><div style="display:table-cell"><div class="{closeIconClass}">x</div></div></div>',showCloseIcon:true,showErrMsgSpeed:"normal",scrollToError:true,classNamePrefix:"bvalidator_",closeIconClass:"close_icon",errMsgClass:"errmsg",errorClass:"invalid",validClass:"",lang:"en",errorMessageAttr:"data-bvalidator-msg",validateActionsAttr:"data-bvalidator",forceValidAttr:"data-bvalidator-forcevalid",modifyActionsAttr:"data-bvalidator-modifier",paramsDelimiter:":",actionsDelimiter:",",validateOn:null,errorValidateOn:"keyup",onBeforeValidate:null,onAfterValidate:null,onValidateFail:null,onValidateSuccess:null,onBeforeElementValidation:null,onAfterElementValidation:null,onBeforeAllValidations:null,onAfterAllValidations:null,validateOnSubmit:true,stopSubmitPropagation:true,noMsgIfExistsForInstance:[],autoModifiers:{digit:["trim"],number:["trim"],email:["trim"],url:["trim"],date:["trim"],ip4:["trim"],ip6:["trim"]},ajaxAnswerValid:"ok",ajaxDelay:300,ajaxOptions:{cache:false},ajaxParamName:"bValue",errorMessages:{en:{"default":"Please correct this value.",equalto:"Please enter the same value again.",differs:"Please enter a different value.",minlength:"The length must be at least {0} characters",maxlength:"The length must be at max {0} characters",rangelength:"The length must be between {0} and {1}",min:"Please enter a number greater than or equal to {0}.",max:"Please enter a number less than or equal to {0}.",between:"Please enter a number between {0} and {1}.",required:"This field is required.",alpha:"Please enter alphabetic characters only.",alphanum:"Please enter alphanumeric characters only.",digit:"Please enter only digits.",number:"Please enter a valid number.",email:"Please enter a valid email address.",image:"This field should only contain image types",url:"Please enter a valid URL.",ip4:"Please enter a valid IPv4 address.",ip6:"Please enter a valid IPv6 address.",date:"Please enter a valid date in format {0}."}}},c=function(D,A,F,E){var z=D.data("ajaxData.bV"+A);
if(!z){z={};D.data("ajaxData.bV"+A,z)}else{clearTimeout(z.timeOut)}z.val=D.val();if(z.lastValidated===z.val){return f.ajax(z.lastResponse)}var B=a.extend({},e.ajaxOptions);if(typeof B.data!="object"){B.data={}}B.url=F;if(E){var C=false;B.async=false;B.data[e.ajaxParamName]=z.val;a.ajax(B).done(function(G){z.lastValidated=z.val;z.lastResponse=G;C=f.ajax(G)});return C}else{z.timeOut=setTimeout(function(){var G=D.val();if(z.val==G){B.async=true;B.data[e.ajaxParamName]=G;a.ajax(B).done(function(H){z.lastValidated=G;
z.lastResponse=H;t.validate(false,D,undefined,H)})}},e.ajaxDelay)}return},n=function(z){return z.is(":input")?z:z.find(":input["+e.validateActionsAttr+"], :input["+e.modifyActionsAttr+"]").not(":button, :image, :reset, :submit, :hidden, :disabled")},s=function(z){z.bind(e.validateOn+".bV"+x,{bVInstance:t},function(A){A.data.bVInstance.validate(false,a(this))})},k=function(B,z){for(var A=0;A<z.length;A++){if(B.data("errMsg.bV"+z[A])){return true}}return false},j=function(B,C){l(B);msg_container=a('<div class="bVErrMsgContainer"></div>').css("position","absolute");
B.data("errMsg.bV"+x,msg_container);msg_container.insertAfter(B);var E="";for(var A=0;A<C.length;A++){E+="<div>"+C[A]+"</div>\n"}if(e.showCloseIcon){E=e.templateCloseIcon.replace("{message}",E).replace("{closeIconClass}",e.classNamePrefix+e.closeIconClass)}var D=a(e.template.replace("{errMsgClass}",e.classNamePrefix+e.errMsgClass).replace("{message}",E));D.appendTo(msg_container);D.find("."+e.classNamePrefix+e.closeIconClass).click(function(G){G.preventDefault();a(this).closest("."+e.classNamePrefix+e.errMsgClass).css("visibility","hidden")
});var F=i(B,D);D.css({visibility:"visible",position:"absolute",top:F.top,left:F.left}).fadeIn(e.showErrMsgSpeed);if(e.scrollToError){var z=D.offset().top;if(u===null||z<u){u=z}}},l=function(z){var A=z.data("errMsg.bV"+x);if(A){A.remove();z.data("errMsg.bV"+x,null)}},i=function(E,H){var z=E.data("errMsg.bV"+x),D=-((z.offset().top-E.offset().top)+H.outerHeight()-e.offset.y),B=(E.offset().left+E.outerWidth())-z.offset().left+e.offset.x,F=e.position.x,C=e.position.y;if(C=="center"||C=="bottom"){var G=H.outerHeight()+E.outerHeight();
if(C=="center"){D+=G/2}if(C=="bottom"){D+=G}}if(F=="center"||F=="left"){var A=E.outerWidth();if(F=="center"){B-=A/2}if(F=="left"){B-=A}}return{top:D,left:B}},o=function(z,C,B,A){if(a.isFunction(e[z])){return e[z](C,B,A)}},m=function(A){var z=A.attr("name");if(z&&/^[^\[\]]+\[.*\]$/.test(z)){return a("input:checkbox").filter(function(){var B=new RegExp(z.match(/^[^\[\]]+/)[0]+"\\[.*\\]$");return this.name.match(B)})}return A},h=function(A){var z={};if(A.is("input:checkbox")){z.value=A.attr("name")?z.selectedInGroup=m(A).filter(":checked").length:A.attr("checked")
}else{if(A.is("input:radio")){z.value=A.attr("name")?z.value=a('input:radio[name="'+A.attr("name")+'"]:checked').length:A.val()}else{if(A.is("select")){z.selectedInGroup=a("option:selected",A).length;z.value=A.val()}else{if(A.is(":input")){z.value=A.val()}}}}return z},d=function(A){var z=a.trim(A).replace(new RegExp("\\s*\\"+e.actionsDelimiter+"\\s*","g"),e.actionsDelimiter);if(!z){return null}return z.split(e.actionsDelimiter)},v=function(z){var A=a.trim(z).match(/^(.*?)\[(.*?)\]/);if(A&&A.length==3){return{name:A[1],params:A[2].split(e.paramsDelimiter)}
}else{return{name:z,params:[]}}},y=function(C,B){var z,A=g(C,B);if(typeof A!=="undefined"){z=a(B).val();if(z!=A){a(B).val(A)}}},g=function(A,z){A.params.unshift(a(z).val());if(typeof b[A.name]=="function"){return b[A.name].apply(z,A.params)}else{if(typeof window[A.name]=="function"){return window[A.name].apply(z,A.params)}else{if(window.console.warn){window.console.warn("[bValidator] unknown modifier: "+A.name)}}}},p=function(B,z,A){if(typeof f[B.name]=="function"){B.params.unshift(A);return f[B.name].apply(z,B.params)
}if(typeof window[B.name]=="function"){B.params.unshift(A.value);return window[B.name].apply(z,B.params)}if(window.console.warn){window.console.warn("[bValidator] unknown validator: "+B.name)}},f={equalto:function(A,z){return A.value==a("#"+z).val()},differs:function(A,z){return A.value!=a("#"+z).val()},minlength:function(z,A){return(z.value.length>=parseInt(A))},maxlength:function(z,A){return(z.value.length<=parseInt(A))},rangelength:function(z,B,A){return(z.value.length>=parseInt(B)&&z.value.length<=parseInt(A))
},min:function(z,A){if(z.selectedInGroup){return z.selectedInGroup>=parseFloat(A)}else{if(!f.number(z)){return false}return(parseFloat(z.value)>=parseFloat(A))}},max:function(A,z){if(A.selectedInGroup){return A.selectedInGroup<=parseFloat(z)}else{if(!f.number(A)){return false}return(parseFloat(A.value)<=parseFloat(z))}},between:function(A,B,z){if(A.selectedInGroup){return(A.selectedInGroup>=parseFloat(B)&&A.selectedInGroup<=parseFloat(z))}if(!f.number(A)){return false}var C=parseFloat(A.value);return(C>=parseFloat(B)&&C<=parseFloat(z))
},required:function(z){if(!z.value||!a.trim(z.value)){return false}return true},alpha:function(z){return f.regex(z,/^[a-z ._\-]+$/i)},alphanum:function(z){return f.regex(z,/^[a-z\d ._\-]+$/i)},digit:function(z){return f.regex(z,/^\d+$/)},number:function(z){return f.regex(z,/^[-+]?\d+(\.\d+)?$/)},email:function(z){return f.regex(z,/^([a-zA-Z\d_\.\-\+%])+\@(([a-zA-Z\d\-])+\.)+([a-zA-Z\d]{2,4})+$/)},image:function(z){return f.regex(z,/\.(jpg|jpeg|png|gif|bmp)$/i)},url:function(z){return f.regex(z,/^\b(https?|ftp):\/\/([-A-Z0-9.]+)(\/[-A-Z0-9+&@#\/%=~_|!:,.;]*)?(\?[A-Z0-9+&@#\/%=~_|!:,.;]*)?$/i)
},regex:function(z,B,A){if(typeof B==="string"){B=new RegExp(B,A)}return B.test(z.value)},ip4:function(z){return f.regex(z,/^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/)},ip6:function(z){return f.regex(z,/^(?:(?:(?:[A-F\d]{1,4}:){5}[A-F\d]{1,4}|(?:[A-F\d]{1,4}:){4}:[A-F\d]{1,4}|(?:[A-F\d]{1,4}:){3}(?::[A-F\d]{1,4}){1,2}|(?:[A-F\d]{1,4}:){2}(?::[A-F\d]{1,4}){1,3}|[A-F\d]{1,4}:(?::[A-F\d]{1,4}){1,4}|(?:[A-F\d]{1,4}:){1,5}|:(?::[A-F\d]{1,4}){1,5}|:):(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)|(?:[A-F\d]{1,4}:){7}[A-F\d]{1,4}|(?:[A-F\d]{1,4}:){6}:[A-F\d]{1,4}|(?:[A-F\d]{1,4}:){5}(?::[A-F\d]{1,4}){1,2}|(?:[A-F\d]{1,4}:){4}(?::[A-F\d]{1,4}){1,3}|(?:[A-F\d]{1,4}:){3}(?::[A-F\d]{1,4}){1,4}|(?:[A-F\d]{1,4}:){2}(?::[A-F\d]{1,4}){1,5}|[A-F\d]{1,4}:(?::[A-F\d]{1,4}){1,6}|(?:[A-F\d]{1,4}:){1,7}:|:(?::[A-F\d]{1,4}){1,7})$/i)
},date:function(H,G){if(H.value.length==10&&G.length==10){var I=G.match(/[^mdy]+/g);if(I.length==2&&I[0].length==1&&I[0]==I[1]){var D=H.value.split(I[0]),B=G.split(I[0]);for(var A=0;A<3;A++){if(B[A]=="dd"){var F=D[A]}else{if(B[A]=="mm"){var C=D[A]}else{if(B[A]=="yyyy"){var E=D[A]}}}}var z=new Date(E,C-1,F);if((z.getMonth()+1!=C)||(z.getDate()!=F)||(z.getFullYear()!=E)){return false}return true}}return false},extension:function(){var z=arguments[0],B="";if(!arguments[1]){return false}for(var A=1;A<arguments.length;
A++){B+=arguments[A];if(A!=arguments.length-1){B+="|"}}return f.regex(z,"\\.("+B+")$","i")},ajax:function(z){if(z==e.ajaxAnswerValid){return true}return false}},b={trim:function(z){return a.trim(z)}},t=this,u;if(window.bValidatorOptions){a.extend(true,e,window.bValidatorOptions)}if(r){a.extend(true,e,r)}var w=q.data("bValidators");if(!w){w={};q.data("bValidators",w)}if(q.data("bValidator")){if(!x){return q.data("bValidator")}if(w[x]){return w[x]}}else{if(!x){x="first"}q.data("bValidator",this)}w[x]=this;
if(q.is("form")){if(e.validateOnSubmit){q.bind("submit.bV"+x,function(z){if(t.validate(false,undefined,1)){return true}else{if(e.stopSubmitPropagation){z.stopImmediatePropagation();return false}}})}q.bind("reset.bV"+x,function(){t.reset()})}if(e.validateOn){s(n(q))}this.validate=function(z,E,F,D){var B=true,A=E?E:n(q);u=null;if(typeof D!=="undefined"||o("onBeforeAllValidations",A)!==false){A.each(function(){var U=d(a(this).attr(e.validateActionsAttr)),H=d(a(this).attr(e.modifyActionsAttr)),R=-1,Z=[],S,J=0;
if(H){for(var V=0;V<H.length;V++){S=v(H[V]);if(!S.name){continue}y(S,this)}}if(U){for(var V=0;V<U.length;V++){S=v(U[V]);if(!S.name){continue}if(e.autoModifiers&&e.autoModifiers[S.name]){for(var W=0;W<e.autoModifiers[S.name].length;W++){y(v(e.autoModifiers[S.name][W]),this)}}if(S.name=="required"){var N=1}else{if(S.name=="ajax"){var P=1}}if(S.name=="valempty"){var Y=1}else{Z[++R]=S}}}else{return true}var ab=h(a(this)),aa=[],X;if(!F&&P&&typeof D==="undefined"){var Q=0;for(var V=0;V<Z.length;V++){if(Z[V].name=="ajax"){break
}if(!p(Z[V],this,ab)){Q=1;break}}if(!Q){D=c(a(this),x,S.params[0]);if(typeof D==="undefined"){return true}}}if(e.noMsgIfExistsForInstance.length&&k(a(this),e.noMsgIfExistsForInstance)){z=1}if((!N&&!Y&&!f.required(ab))||a(this).attr(e.forceValidAttr)=="true"){J=1}if(!J){var K=a(this).attr(e.errorMessageAttr),L=0;a(this).data("checked.bV"+x,1);if(o("onBeforeElementValidation",a(this))!==false){for(var V=0;V<Z.length;V++){if(Z[V].name=="valempty"){continue}if(o("onBeforeValidate",a(this),Z[V].name)===false){continue
}if(Z[V].name=="ajax"){if(Q){continue}if(F||typeof D==="undefined"){if(!aa.length){X=c(a(this),x,Z[V].params[0],1)}else{X=true}}else{X=f.ajax.apply(this,[D])}}else{X=p(Z[V],this,ab)}if(o("onAfterValidate",a(this),Z[V].name,X)===false){continue}if(!X){if(!z){if(!L){if(!K){if(e.singleError&&aa.length){L=1;K=""}else{if(e.errorMessages[e.lang]&&e.errorMessages[e.lang][Z[V].name]){K=e.errorMessages[e.lang][Z[V].name]}else{if(e.errorMessages.en[Z[V].name]){K=e.errorMessages.en[Z[V].name]}else{var G=a(this).attr(e.errorMessageAttr+"-"+Z[V].name);
if(G){K=G}else{if(e.errorMessages[e.lang]&&e.errorMessages[e.lang]["default"]){K=e.errorMessages[e.lang]["default"]}else{K=e.errorMessages.en["default"]}}}}}}else{L=1}if(K.indexOf("{")){for(var T=0;T<Z[V].params.length-1;T++){K=K.replace(new RegExp("\\{"+T+"\\}","g"),Z[V].params[T+1])}}if(!(aa.length&&Z[V].name=="required")){aa[aa.length]=K}K=null}}else{aa[aa.length]=""}B=false;o("onValidateFail",a(this),Z[V].name,aa)}else{o("onValidateSuccess",a(this),Z[V].name)}}}var I=o("onAfterElementValidation",a(this),aa)
}if(!z&&I!==false&&a(this).data("checked.bV"+x)){var ac=a(this).is("input:checkbox,input:radio")?1:0;if(aa.length){if(I!==0){j(a(this),aa)}if(!ac){a(this).removeClass(e.classNamePrefix+e.validClass);if(e.errorClass){a(this).addClass(e.classNamePrefix+e.errorClass)}}if(e.errorValidateOn){if(e.validateOn){a(this).unbind(e.validateOn+".bV"+x)}var O=ac||a(this).is("select,input:file")?"change":e.errorValidateOn;if(ac){var M=a(this).is("input:checkbox")?m(a(this)):a('input:radio[name="'+a(this).attr("name")+'"]');
a(M).unbind(".bVerror"+x);a(M).bind("change.bVerror"+x,{bVInstance:t,groupLeader:a(this)},function(ad){ad.data.bVInstance.validate(false,ad.data.groupLeader)})}else{a(this).unbind(".bVerror"+x);a(this).bind(O+".bVerror"+x,{bVInstance:t},function(ad){ad.data.bVInstance.validate(false,a(this))})}}}else{if(I!==0){l(a(this))}if(!ac){a(this).removeClass(e.classNamePrefix+e.errorClass);if(e.validClass){a(this).addClass(e.classNamePrefix+e.validClass)}}if(e.validateOn){a(this).unbind(e.validateOn+".bV"+x);
s(a(this))}a(this).data("checked.bV"+x,0)}}})}o("onAfterAllValidations",A,B);if(u&&!E&&(a(window).scrollTop()>u||a(window).scrollTop()+a(window).height()<u)){var C=navigator.userAgent.toLowerCase();a(C.indexOf("chrome")>-1||C.indexOf("safari")>-1?"body":"html").animate({scrollTop:u-10},{duration:"slow"})}return B};this.getOptions=function(){return e};this.getValidators=this.getActions=function(){return f};this.getModifiers=function(){return b};this.isValid=function(z){return this.validate(true,z,1)
};this.removeMsg=this.removeErrMsg=function(z){l(z)};this.showMsg=function(z,A){if(z.length){if(typeof(A)=="string"){A=[A]}j(z,A)}};this.getInputs=function(){return n(q)};this.bindValidateOn=function(z){s(z)};this.reset=function(){elements=n(q);if(e.validateOn){s(elements)}elements.each(function(){l(a(this));a(this).unbind(".bVerror"+x);a(this).removeClass(e.classNamePrefix+e.errorClass);a(this).removeClass(e.classNamePrefix+e.validClass);a(this).removeData("ajaxData.bV"+x);a(this).removeData("errMsg.bV"+x);
a(this).removeData("checked.bV"+x)})};this.destroy=function(){if(q.is("form")){q.unbind(".bV"+x)}this.reset();q.removeData("bValidator");q.removeData("bValidators")}}})(jQuery);