!function(n,t){"use strict";var l=t.ea,e=t.console,r={settings:{debug:!1,optimize:!0,enumsAsNumbers:!0,registerAllMethods:!1,dependencyTriggers:"change keyup",apply:function(t){!function(n,t){for(var i in t)t.hasOwnProperty(i)&&(n[i]=t[i])}(r.settings,t),function(){if(!i.isBool(r.settings.debug))throw"EA settings error: debug value must be a boolean (true or false)";if(!i.isBool(r.settings.optimize))throw"EA settings error: optimize value must be a boolean (true or false)";if(!i.isBool(r.settings.enumsAsNumbers))throw"EA settings error: enumsAsNumbers value must be a boolean (true or false)";if(!i.isBool(r.settings.registerAllMethods))throw"EA settings error: registerAllMethods value must be a boolean (true or false)";if(!i.isString(r.settings.dependencyTriggers)&&null!==r.settings.dependencyTriggers&&void 0!==r.settings.dependencyTriggers)throw"EA settings error: dependencyTriggers value must be a string (multiple event types can be bound at once by including each one separated by a space), null or undefined";}();n("form").each(function(){n(this).find("input, select, textarea").off(".expressive.annotations");h.bindFields(this,!0)});u.info(i.string.format("EA settings applied:\n{0}",t))}},addMethod:function(n,t){s.addMethod(n,t)},addValueParser:function(n,t){i.addValueParser(n,t)},noConflict:function(){return t.ea===this&&(t.ea=l),this}},u={info:function(n){r.settings.debug&&e&&"function"==typeof e.log&&e.log("[info] "+u.prep(n,new Date))},warn:function(n){e&&"function"==typeof e.warn&&e.warn("[warn] "+u.prep(n,new Date))},fail:function(n){e&&"function"==typeof e.error&&e.error("[fail] "+u.prep(n,new Date))},prep:function(n,t){var r=(n=i.string.tryParse(n)).split("\n"),u=(void 0!==t&&null!==t?"("+i.datetime.stamp(t)+"): ":"")+r.shift();return r.length>0?u+"\n"+i.string.indent(r.join("\n"),19):u}},s={methods:{},addMethod:function(n,t){var i=this.methods[n];this.methods[n]=function(){return t.length===arguments.length?t.apply(this,arguments):"function"==typeof i?i.apply(this,arguments):t.apply(this,arguments)}},registerMethods:function(n,t,f){var o,e,s;if(this.initialize(),r.settings.registerAllMethods){for(e in this.methods)if(this.methods.hasOwnProperty(e)){if(n.hasOwnProperty(e)){u.warn(i.string.format("Field {0} - skipping {1}(...) method registration, naming conflict with the field identifier.",f,e));continue}s=this.methods[e];n[e]=s}}else for(o=0;o<t.length;o++)e=t[o],this.methods.hasOwnProperty(e)&&(s=this.methods[e],n[e]=s)},initialize:function(){var t=this;this.addMethod("Now",function(){return Date.now()});this.addMethod("Today",function(){return new Date((new Date).setHours(0,0,0,0)).getTime()});this.addMethod("ToDate",function(n){return Date.parse(n)});this.addMethod("Date",function(n,t,i){return new Date(new Date(n,t-1,i).setFullYear(n)).getTime()});this.addMethod("Date",function(n,t,i,r,u,f){return new Date(new Date(n,t-1,i,r,u,f).setFullYear(n)).getTime()});this.addMethod("TimeSpan",function(n,t,i,r){return 1e3*r+6e4*i+36e5*t+864e5*n});this.addMethod("Length",function(n){return null!==n&&void 0!==n?n.length:0});this.addMethod("Trim",function(t){return null!==t&&void 0!==t?n.trim(t):null});this.addMethod("Concat",function(n,t){return[n,t].join("")});this.addMethod("Concat",function(n,t,i){return[n,t,i].join("")});this.addMethod("CompareOrdinal",function(n,t){return n===t?0:null!==n&&null===t?1:null===n&&null!==t?-1:n>t?1:-1});this.addMethod("CompareOrdinalIgnoreCase",function(n,i){return n=null!==n&&void 0!==n?n.toLowerCase():null,i=null!==i&&void 0!==i?i.toLowerCase():null,t.methods.CompareOrdinal(n,i)});this.addMethod("StartsWith",function(n,t){return null!==n&&void 0!==n&&null!==t&&void 0!==t&&n.slice(0,t.length)===t});this.addMethod("StartsWithIgnoreCase",function(n,i){return n=null!==n&&void 0!==n?n.toLowerCase():null,i=null!==i&&void 0!==i?i.toLowerCase():null,t.methods.StartsWith(n,i)});this.addMethod("EndsWith",function(n,t){return null!==n&&void 0!==n&&null!==t&&void 0!==t&&n.slice(-t.length)===t});this.addMethod("EndsWithIgnoreCase",function(n,i){return n=null!==n&&void 0!==n?n.toLowerCase():null,i=null!==i&&void 0!==i?i.toLowerCase():null,t.methods.EndsWith(n,i)});this.addMethod("Contains",function(n,t){return null!==n&&void 0!==n&&null!==t&&void 0!==t&&n.indexOf(t)>-1});this.addMethod("ContainsIgnoreCase",function(n,i){return n=null!==n&&void 0!==n?n.toLowerCase():null,i=null!==i&&void 0!==i?i.toLowerCase():null,t.methods.Contains(n,i)});this.addMethod("IsNullOrWhiteSpace",function(n){return null===n||!/\S/.test(n)});this.addMethod("IsDigitChain",function(n){return/^[0-9]+$/.test(n)});this.addMethod("IsNumber",function(n){return/^[+-]?(?:(?:[0-9]+)|(?:[0-9]+[eE][+-]?[0-9]+)|(?:[0-9]*\.[0-9]+(?:[eE][+-]?[0-9]+)?))$/.test(n)});this.addMethod("IsEmail",function(n){return/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(n)});this.addMethod("IsPhone",function(n){return/^(\+\s?)?((?!\+.*)\(\+?\d+([\s\-\.]?\d+)?\)|\d+)([\s\-\.]?(\(\d+([\s\-\.]?\d+)?\)|\d+))*(\s?(x|ext\.?)\s?\d+)?$/.test(n)});this.addMethod("IsUrl",function(n){return/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/i.test(n)});this.addMethod("IsRegexMatch",function(n,t){return null!==n&&void 0!==n&&null!==t&&void 0!==t&&new RegExp(t).test(n)});this.addMethod("Guid",function(n){var t=i.guid.tryParse(n);if(t.error)throw t.msg;return t});this.addMethod("Min",function(n){if(0===arguments.length)throw"no arguments";if(1===arguments.length&&i.isArray(n)){if(0===n.length)throw"empty sequence";return Math.min.apply(null,n)}return Math.min.apply(null,arguments)});this.addMethod("Max",function(n){if(0===arguments.length)throw"no arguments";if(1===arguments.length&&i.isArray(n)){if(0===n.length)throw"empty sequence";return Math.max.apply(null,n)}return Math.max.apply(null,arguments)});this.addMethod("Sum",function(n){if(0===arguments.length)throw"no arguments";var t,r,u=0;if(1===arguments.length&&i.isArray(n)){if(0===n.length)throw"empty sequence";for(t=0,r=n.length;t<r;t++)u+=parseFloat(n[t]);return u}for(t=0,r=arguments.length;t<r;t++)u+=parseFloat(arguments[t]);return u});this.addMethod("Average",function(n){if(0===arguments.length)throw"no arguments";var r,u,f=[];if(1===arguments.length&&i.isArray(n)){if(0===n.length)throw"empty sequence";return t.methods.Sum(n)/n.length}for(r=0,u=arguments.length;r<u;r++)f.push(arguments[r]);return t.methods.Sum(f)/arguments.length})}},i={parsers:{},addValueParser:function(t,r){n.each(t.split(/\s+/),function(n,t){/\S/.test(t)&&(i.parsers[t]=r)})},array:{contains:function(n,t){for(var i=n.length;i--;)if(n[i]===t)return!0;return!1}},object:{keys:function(n){var t,i=[];for(t in n)n.hasOwnProperty(t)&&i.push(t);return i},tryParse:function(t){try{return n.parseJSON(t)}catch(n){return{error:!0,msg:"Given value was not recognized as a valid JSON. "+n}}}},string:{format:function(n,t){function f(n){var t=function(n,t){return"function"==typeof t?"function(...) {...}":t};return r.settings.registerAllMethods&&(t=null),n=i.isObject(n)?JSON.stringify(n,t,4):n,n=i.isString(n)?n.replace(/\$/g,"$$$$"):n}function e(n,t,i){return n.replace(new RegExp("\\{"+i+"\\}","gm"),t)}var u;if(t instanceof Array){for(u=0;u<t.length;u++)n=e(n,f(t[u]),u);return n}for(u=0;u<arguments.length-1;u++)n=e(n,f(arguments[u+1]),u);return n},indent:function(n,t){var i=Array((t||0)+1).join(" ");return n.replace(/^/gm,i)},tryParse:function(n){return i.isString(n)?n:void 0!==n&&null!==n?n.toString():{error:!0,msg:"Given value was not recognized as a valid string."}}},bool:{tryParse:function(t){return i.isBool(t)?t:!i.isString(t)||"true"!==(t=n.trim(t).toLowerCase())&&"false"!==t?{error:!0,msg:"Given value was not recognized as a valid boolean."}:"true"===t}},number:{tryParse:function(n){return function(n){return i.isNumeric(parseFloat(n))&&isFinite(n)}(n)?parseFloat(n):{error:!0,msg:"Given value was not recognized as a valid number."}}},timespan:{tryParse:function(n){if(i.isTimeSpan(n)){var t=/(\-)?(?:(\d*)\.)?(\d+)\:(\d+)(?:\:(\d+)\.?(\d{3})?)?/.exec(n),r="-"===t[1]?-1:1,u={days:i.number.tryParse(t[2]||0)*r,hours:i.number.tryParse(t[3]||0)*r,minutes:i.number.tryParse(t[4]||0)*r,seconds:i.number.tryParse(t[5]||0)*r,milliseconds:i.number.tryParse(t[6]||0)*r};return u.milliseconds+1e3*u.seconds+6e4*u.minutes+36e5*u.hours+864e5*u.days}return{error:!0,msg:"Given value was not recognized as a valid .NET style timespan string."}}},datetime:{stamp:function(n){function t(n){return("0"+n).slice(-2)}return t(n.getHours())+":"+t(n.getMinutes())+":"+t(n.getSeconds())},tryParse:function(n){if(i.isDate(n))return n.getTime();if(i.isString(n)){var t=Date.parse(n);if(i.isNumeric(t))return t}return{error:!0,msg:"Given value was not recognized as a valid RFC 2822 or ISO 8601 date."}}},guid:{tryParse:function(n){return i.isGuid(n)?n.toUpperCase():{error:!0,msg:"Given value was not recognized as a valid guid - guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)."}}},enumeration:{tryParse:function(n){return r.settings.enumsAsNumbers?i.number.tryParse(n):i.string.tryParse(n)}},isTimeSpan:function(n){return/(\-)?(?:(\d*)\.)?(\d+)\:(\d+)(?:\:(\d+)\.?(\d{3})?)?/.test(n)},isNumeric:function(n){return"number"==typeof n&&!isNaN(n)},isDate:function(n){return n instanceof Date},isObject:function(n){return"object"==typeof n||n instanceof Object},isString:function(n){return"string"==typeof n||n instanceof String},isBool:function(n){return"boolean"==typeof n||n instanceof Boolean},isGuid:function(n){return/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(n)},isArray:function(n){return"[object Array]"===Object.prototype.toString.call(n)},tryParse:function(n,t,r,f){var e;if(null!==f&&void 0!==f){if(!(e=i.findValueParser(r,f)).error)return e(n,r);u.warn(e.msg)}return e=i.findValueParser(r,t),e.error?i.tryAutoParse(n,t):(u.warn(i.string.format("Overridden {0} type parsing runs for {1} field. All fields of {0} type are going to be parsed using your value parser. If such a behavior is unintentional, change the name of your value parser to one, which does not indicate at {0} (or any other) type name.",t,r)),e(n,r))},tryAutoParse:function(n,t){return i.hasOwnProperty(t)?i[t].tryParse(n):i.object.tryParse(n)},findValueParser:function(n,t){var r=i.parsers[t];return"function"==typeof r?r:{error:!0,msg:i.string.format("Custom value parser {0} not found. Consider its registration with ea.addValueParser(), or remove redundant ValueParser attribute from {1} model field.",t,n)}}},f={getPrefix:function(n){return void 0!==n&&null!==n?n.substr(0,n.lastIndexOf(".")+1):""},extractValue:function(t,r,f,e,o){var l,s,c,h;if(s=f+r,0===(l=n(t).find(i.string.format(':input[name="{0}"]',s))).length)throw i.string.format("DOM field {0} not found.",s);if(null===(c=function(n){switch(n.attr("type")){case"checkbox":return n.length>2&&u.warn(i.string.format("DOM field {0} is ambiguous (unless custom value parser is provided).",n.attr("name"))),n.is(":checked");case"radio":return n.filter(":checked").val();default:return n.length>1&&u.warn(i.string.format("DOM field {0} is ambiguous (unless custom value parser is provided).",n.attr("name"))),n.val()}}(l))||void 0===c||""===c)return null;if(null!==(h=i.tryParse(c,e,s,o))&&void 0!==h&&h.error)throw i.string.format("DOM field {0} value conversion to {1} failed. {2}",s,e,h.msg);return h},deserializeObject:function(n,t,i,u,f,e){function h(n,t,i){var f,r,e,h,u,o,c,s;for(c=/^([a-z_0-9]+)\[([0-9]+)\]$/i,f=n.split("."),r=i,e=0;e<f.length-1;e++)n=f[e],(h=c.exec(n))?(n=h[1],u=h[2],r.hasOwnProperty(n)||(r[n]={}),r[n][u]=r[n][u]||{},r=r[n][u]):(r.hasOwnProperty(n)||(r[n]={}),r=r[n]);n=f[f.length-1];s=c.exec(n);s?(o=s[1],u=s[2],r[o]=r[o]||[],r[o][u]=t):r[n]=t}var o,c,l,s={};for(o in t)t.hasOwnProperty(o)&&(c=t[o],l=f[o],h(o,this.extractValue(n,o,e,c,l),s));for(o in i)i.hasOwnProperty(o)&&h(o,i[o],s);for(o in u)u.hasOwnProperty(o)&&h(o,r.settings.enumsAsNumbers?u[o]:o.split(".").pop(),s);return s},adjustGivenValue:function(n,t,r){var o,f,e;if(n="checkbox"===t.type?t.checked:n,o=t.name.replace(r.prefix,""),f=r.parsersMap[o],null!==f&&void 0!==f){if(e=i.findValueParser(t.name,f),!e.error)return e(n,t.name);u.warn(e.msg)}return n},ctxEval:function(n,t){return new Function("expression","context","with(context){return eval(expression)}")(n,t)}},h={referencesMap:[],collectReferences:function(n,t,r){for(var u,f=0;f<n.length;f++)(u=r+n[f])!==t&&(this.referencesMap[u]=this.referencesMap[u]||[],i.array.contains(this.referencesMap[u],t)||this.referencesMap[u].push(t))},validateReferences:function(t,r){var e,o,f,s;if(s=n(r).validate(),void 0!==(f=this.referencesMap[t])&&null!==f)for(u.info(i.string.format("Validation triggered for the following dependencies of {0} field:\n{1}.",t,f.join(", "))),e=f.length;e--;)0!==(o=n(r).find(i.string.format(':input[data-val][name="{0}"]',f[e])).not(s.settings.ignore)).length&&o.valid();else u.info(i.string.format("No fields dependent on {0} detected.",t))},bindFields:function(t,f){if(null!==r.settings.dependencyTriggers&&void 0!==r.settings.dependencyTriggers&&""!==r.settings.dependencyTriggers){var e=[];n.each(r.settings.dependencyTriggers.split(/\s+/),function(n,t){/\S/.test(t)&&e.push(i.string.format("{0}.expressive.annotations",t))});n(t).find("input, select, textarea").not(function(t,i){var r=n(i).hasClass("ea-triggers-bound");return n(i).addClass("ea-triggers-bound"),!f&&r}).on(e.join(" "),function(r){var f=n(this).attr("name");u.info(i.string.format("Dependency validation trigger - {0} event, handled.",r.type));h.validateReferences(f,t)})}}},c=function(t,r){var u={prefix:f.getPrefix(r.element.name),form:r.form};for(var e in r.params)r.params.hasOwnProperty(e)&&(u[e]=void 0!==r.params[e]?n.parseJSON(r.params[e]):{});r.message&&(r.messages[t]=function(n){var t,i,u,e,o;t=r.message;for(i in n.errFieldsMap)n.errFieldsMap.hasOwnProperty(i)&&(u=n.errFieldsMap[i],e=f.extractValue(n.form,i,n.prefix,"string",null),o=new RegExp(u,"g"),t=t.replace(o,e));return t});h.bindFields(r.form);h.collectReferences(i.object.keys(u.fieldsMap),r.element.name,u.prefix);r.rules[t]=u},a=function(n,t,e,o){var h,c;return void 0!==(t=f.adjustGivenValue(t,e,o))&&null!==t&&""!==t?(h=f.deserializeObject(o.form,o.fieldsMap,o.constsMap,o.enumsMap,o.parsersMap,o.prefix),s.registerMethods(h,o.methodsList,e.name),u.info(i.string.format("Field {0} - {1} expression:\n[{2}]\nto be executed within the following context{3}:\n{4}",e.name,n,o.expression,r.settings.registerAllMethods?" (methods not shown)":"",h)),c=f.ctxEval(o.expression,h),{valid:c,condition:c}):{valid:!0,condition:void 0}},v=function(n,t,e,o){t=f.adjustGivenValue(t,e,o);var h,c=void 0,l="Field {0} - {1} expression:\n[{2}]\nto be executed within the following context{3}:\n{4}";return r.settings.optimize||(h=f.deserializeObject(o.form,o.fieldsMap,o.constsMap,o.enumsMap,o.parsersMap,o.prefix),s.registerMethods(h,o.methodsList,e.name),u.info(i.string.format(l,e.name,n,o.expression,r.settings.registerAllMethods?" (methods not shown)":"",h)),c=f.ctxEval(o.expression,h)),void 0===t||null===t||""===t||!/\S/.test(t)&&!o.allowEmpty?void 0!==c?{valid:!c,condition:c}:(h=f.deserializeObject(o.form,o.fieldsMap,o.constsMap,o.enumsMap,o.parsersMap,o.prefix),s.registerMethods(h,o.methodsList,e.name),u.info(i.string.format(l,e.name,n,o.expression,r.settings.registerAllMethods?" (methods not shown)":"",h)),c=f.ctxEval(o.expression,h),{valid:!c,condition:c}):{valid:!0,condition:c}},o=" abcdefghijklmnopqrstuvwxyz";n.each(o.split(""),function(){var t=i.string.format("requiredif{0}",n.trim(this));n.validator.unobtrusive.adapters.add(t,["expression","fieldsMap","constsMap","enumsMap","methodsList","parsersMap","errFieldsMap","allowEmpty"],function(n){c(t,n)})});n.each(o.split(""),function(){var t=i.string.format("assertthat{0}",n.trim(this));n.validator.unobtrusive.adapters.add(t,["expression","fieldsMap","constsMap","enumsMap","methodsList","parsersMap","errFieldsMap"],function(n){c(t,n)})});n.each(o.split(""),function(){var r=n.trim(this),t=i.string.format("assertthat{0}",r);n.validator.addMethod(t,function(r,f,e){try{var o=a(t,r,f,e);return u.info(i.string.format("Field {0} - {1} outcome: {2}, assertion {3}.",f.name,t,void 0===o.condition?"assertion expression computation redundant":o.condition?"expression true":"expression false",o.valid?"satisfied":"not satisfied")),n(f).trigger("eavalid",["assertthat",o.valid,e.expression]),o.valid}catch(n){u.fail(n)}},"")});n.each(o.split(""),function(){var r=n.trim(this),t=i.string.format("requiredif{0}",r);n.validator.addMethod(t,function(f,e,s){try{var h=v(t,f,e,s);return u.info(i.string.format("Field {0} - {1} outcome: {2}, requirement {3}.",e.name,t,void 0===h.condition?"requirement expression computation redundant":h.condition?"required":"not required",h.valid?"satisfied":"not satisfied")),n(e).trigger("eavalid",["requiredif",h.valid,s.expression,h.condition,o.indexOf(r)]),h.valid}catch(n){u.fail(n)}},"")});t.ea=r}(jQuery,window)