(function(){var ruleName="jui-rules";JUI.Class("Form",{OPTS:{formId:"",method:"POST"},init:function(opts){this._super(opts);this.$form=$("#"+this.opts.formId);this.addExtMethod();this.parseForm(this.$form)},validate:function(){var ret=true;this.$els.each(function(){var r=$(this).validate();if(!r){ret=false}});return ret},check:function(){var ret=true;this.$els.each(function(){ret=$(this).validate();if(!ret){ret=false;return false}});return ret},submit:function(opts){var onSubmit=opts.onSubmit||this.opts.onSubmit;var data=opts.data||this.getData();if(onSubmit){var ret=onSubmit.call(this,data);if(ret===false){return}}this.fire("BeforeSubmit",data);this.doSubmit(opts,data)},doSubmit:function(opts,data){var that=this;var method=this.$form.attr("method")||this.opt("method");$.ajax({type:method,url:opts.url,traditional:true,dataType:"json",data:data,success:function(data,textStatus,jqXHR){var success=opts.success||that.opts.success;success&&success(data)},error:function(XMLHttpRequest,textStatus,errorThrown){that.fire("SubmitError",{xhr:XMLHttpRequest,status:textStatus,error:errorThrown})}})},load:function(data){if(typeof data==="string"){var url=data;this.loadRemoteData(url)}else{this.loadData(data)}},setData:function(data){this.load(data)},loadRemoteData:function(url){var that=this;var ret=this.fire("BeforeLoad");if(ret===false){return}$.ajax({type:"GET",url:url,traditional:true,dataType:"json",success:function(data,textStatus,jqXHR){that.fire("LoadSuccess",data);that.loadData(data)},error:function(XMLHttpRequest,textStatus,errorThrown){that.fire("LoadError",XMLHttpRequest,textStatus,errorThrown)}})},loadData:function(data){this.clear();for(var name in data){var val=data[name];var $el=this.$form.find('[name="'+name+'"]');$el.each(function(){var _$el=$(this);if(_$el.is(":radio")||_$el.is(":checkbox")){_$el.prop("checked",false);var elVal=_$el.val();if($.isArray(val)){for(var i=0,len=val.length;i<len;i++){if(elVal==val[i]){_$el.prop("checked",true)}}}else{_$el.prop("checked",elVal==val)}}else{_$el.val(val)}})}},clear:function(){this.$els.each(function(){var _$el=$(this);if(_$el.is(":radio")||_$el.is(":checkbox")){this.checked=false}else{this.value=""}var msg=_$el.data("msg");if(msg){msg.text("")}})},reset:function(){var form=this.form;if(form&&form.reset){form.reset()}else{this.clear()}},getData:function(fieldName){var that=this;var data={};this.$els.each(function(){var value=that._getInputVal($(this));if(value){var name=this.name;var dataValue=data[name];if(dataValue){if($.isArray(dataValue)){dataValue.push(value)}else{data[name]=[dataValue,value]}}else{data[name]=value}}});if(typeof fieldName==="string"){return data[fieldName]}return data},getFormData:function($form){var data={};var dataArr=$form.serializeArray();for(var i=0,len=dataArr.length;i<len;i++){var item=dataArr[i];var name=item.name;var itemValue=item.value;var dataValue=data[name];if(dataValue){if($.isArray(dataValue)){dataValue.push(itemValue)}else{data[name]=[dataValue,itemValue]}}else{data[name]=itemValue}}return data},_getInputVal:function($input){if($input.is(":radio")||$input.is(":checkbox")){if($input.is(":checked")){return $input.val()}}else{return $input.val()}},parseForm:function($form){var that=this;this.form=$form[0];this.$els=$form.find("input,select,textarea");this.$els.each(function(){that.doParse($(this))})},doParse:function($el){var rules=$el.attr("rules");rules=eval("("+rules+")")||[];$el.addRules(rules)},addExtMethod:function(){jQuery.fn.extend({addRules:function(rules){rules=rules||[];this.data(ruleName,rules)},buildRules:function(){var rules=this.attr("rules");rules=eval("("+rules+")")||[];this.addRules(rules)},validate:function(){var passValidate=this.attr("passValidate");if(passValidate=="true"){return true}var rules=this.data(ruleName)||[];var value=this.getValue();for(var i=0,len=rules.length;i<len;i++){var opt=rules[i];this._initMsgId(opt);var validate=new JUI.Validate(opt);var ret=validate.validate(value);if(!ret){return false}}return true},_initMsgId:function(opt){var msgId=opt.msgId||this.attr("msgId");if(!msgId){msgId="jui_form_msg_"+JUI.getId();this.parent().append('<span id="'+msgId+'"></span>')}this.data("msg",$("#"+msgId));opt.msgId=msgId},getValue:function(){return $.trim(this.val())}});this.addExtMethod=function(){}}},JUI.Common)})();