ACMS.Dispatch._validate=function(elm){ACMS.Dispatch._validate.extract(elm);ACMS.Dispatch._validate.setValidator()};
ACMS.Dispatch._validate.setValidator=function(elm){var $field=ACMS.Dispatch._validate.input(elm);_.each($field,function(val,i){var name=$(val).attr("name");name=name.replace("[]","");$(val).data("oldvalue",$(val).val());setInterval(function(){var oldvalue=$(val).data("oldvalue");var value=$(val).val();if(oldvalue!=value)$(val).trigger("change");$(val).data("oldvalue",value)},100);$(val).bind("blur input change",function(){var validator=ACMS.Dispatch._validate.validators[name],success=true;_.each(validator,
function(obj,i){if(!ACMS.Library.Validator.isFunction(obj.validator)||"all_"===obj.validator.substring(0,4))return;var valid=ACMS.Library.Validator[obj.validator]($(val).val(),obj.arg);ACMS.Dispatch._validate.label(obj,valid);success&=valid});ACMS.Dispatch._validate.toggleClass(name,success);return true})})};
ACMS.Dispatch._validate.all=function(elm){var $field=ACMS.Dispatch._validate.input(elm),res=true;_.each(ACMS.Dispatch._validate.validators.checked,function(obj,i){var $input=$field.filter('[name="'+obj.field+'"], [name="'+obj.field+"[]"+'"]');var success=ACMS.Library.Validator[obj.validator]($input,obj.arg);ACMS.Dispatch._validate.label(obj,success);res&=success});_.each($field,function(val,i){var name=$(val).attr("name"),validator=ACMS.Dispatch._validate.validators[name],success=true;_.each(validator,
function(obj,i){if(!ACMS.Library.Validator.isFunction(obj.validator)||"all_"===obj.validator.substring(0,4))return;var valid=ACMS.Library.Validator[obj.validator]($(val).val(),obj.arg);ACMS.Dispatch._validate.label(obj,valid);success&=valid});ACMS.Dispatch._validate.toggleClass(name,success);res&=success});return!!res};
ACMS.Dispatch._validate.extract=function(elm){ACMS.Dispatch._validate.validators=[];ACMS.Dispatch._validate.validators.checked=[];ACMS.Dispatch._validate.input(elm).each(function(){if(this.name.match(/^(.*):(validator|v)#(.*)$/)){var field=RegExp.$1,validator=RegExp.$3,obj={field:field,validator:validator,arg:$(this).val(),id:$(this).attr("id")};if("all_"==obj.validator.substring(0,4))ACMS.Dispatch._validate.validators.checked.push(obj);if(!ACMS.Dispatch._validate.validators[field])ACMS.Dispatch._validate.validators[field]=
[];ACMS.Dispatch._validate.validators[field].push(obj)}})};ACMS.Dispatch._validate.input=function(elm){return $(":input:not(disabled):not(:radio:not(:checked)):not(:checkbox:not(:checked))",elm)};
ACMS.Dispatch._validate.label=function(obj,valid){var $label=$('label[for="'+obj.id+'"]');if($label.size()){$label.removeClass(ACMS.Config.validatorResultClass);$label.removeClass(ACMS.Config.validatorResultClass+"0");$label.removeClass(ACMS.Config.validatorResultClass+"1");$label.addClass("validator-result-"+(valid?"1":"0"))}};
ACMS.Dispatch._validate.toggleClass=function(name,success){var $custom=$('[data-validator="'+name+'"');if($custom.size()){var ok=ACMS.Config.validatorOkClass,ng=ACMS.Config.validatorNgClass;if(success)$custom.removeClass(ng).addClass(ok);else $custom.removeClass(ok).addClass(ng)}};
