var sxUtil = sxUtil || {};

sxUtil['form']={
		save:function(o){
			var op = $.extend({errorMsg:'新增失败', successMsg:'新增成功'},o);
			$("#"+op.formId).find('#orgId').val($('#_orgId').val());
			this.submit($.extend(op,o));
		},
		edit:function(o){
			var op = { errorMsg:'编辑失败', successMsg:'编辑成功'};
			this.submit($.extend(op,o));
		},
		del:function(o){
			var op = { errorMsg:'删除失败', successMsg:'删除成功'};
			op = $.extend(op,o);
			var index = layer.load(1, {time: 30*1000,shade: [0.5, '#393D49']});
			$.ajax({
				url: op.url,
				data: op.data,
				type: 'POST',
				success:function(e){
					layer.close(index);
		    		if(e && e.success == true){
		    			o.callback(e);  sxUtil.dialog.close(op.index); layer.msg(op.successMsg, {icon: 1});
		    		}else{
						layer.msg( (e && e.msg) || op.errorMsg, {icon: 0});
					}
			    }
		    });
		},
		submit:function(o){
			var op = {
				formId:'', 
				index:'', 
				errorMsg:'失败',
				successMsg:'成功',
				callback:function(e){} 
			}
			
			op = $.extend(op,o);
			
			$("#"+op.formId).validate({
				submitHandler:function(form){
					var index = layer.load(1, {time: 30*1000,shade: [0.5, '#393D49']});
					$(form).ajaxSubmit({
				    	success:function(e){
				    		layer.close(index);
				    		if(e && e.success == true){
				    			o.callback(e); sxUtil.dialog.close(op.index); layer.msg(op.successMsg, {icon: 1});
				    		}else{
				    			layer.msg( (e && e.msg) || op.errorMsg, {icon: 0});
							}
				    	}
				    });
				}
			 });
		},
		search:function(o){
			var op = { formId:'', index:'', callback:function(){} }
			
			op = $.extend(op,o);
			
			$("#"+op.formId).validate({
				submitHandler:function(form){
					var names = $(form).serializeArray(),jsonData={};
					for(var i in names){
						jsonData[names[i].name] = names[i].value;
					}
					op.callback(jsonData); sxUtil.dialog.close(op.index);
				}
			 });
		}
}