jQuery(document).ready(function() {
	$("input").keydown(function(event){
		var e = event || window.event || arguments.callee.caller.arguments[0];
		if(e && e.keyCode==13){ 
			return false;
        }
	});
	
	function getAsyncUrl(treeId, treeNode) {
		return !treeNode ? "/permission/findRootByParentId" : "/permission/findNodeByParentId";
	}

	function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
		var treeObj = $.fn.zTree.getZTreeObj("roleanizationTree");
		if (!treeNode) {
			var nodes = treeObj.getNodes();
			treeObj.selectNode(nodes[0]);
			treeObj.checkAllNodes(false);
		}
	}

	var roleTreeInit = function() {
		var setting = {
			callback : {
				onAsyncSuccess : zTreeOnAsyncSuccess
			},
			view : {
				dblClickExpand : false
			},
			async : {
				enable : true,
				dataType : "json",
				url : getAsyncUrl,
				autoParam : [ "id=parentId" ],
			},
			data : {
				simpleData : {
					enable : true
				}
			},
			check: {
				enable:true
			}
		};

		$.fn.zTree.init($("#roleanizationTree"), setting);
	}
	roleTreeInit();
	
	
	$("#roleForm").validate({
		onkeyup : false,
		submitHandler:function(form){
			var result = '';
			var treeObj = $.fn.zTree.getZTreeObj("roleanizationTree");
			var nodes = treeObj.getCheckedNodes(true);
			for(var i=0;i<nodes.length;i++){
				result += nodes[i].id+",";
			}
			//debugger;
			$(form).ajaxSubmit({
				data:{"result":result,"bo":true},
				dataType:"JSON",
		    	success:function(e){
		    		if(e && e.success == true){
		    			sxUtil.dialog.close($(form).data('index'));
			    		jQuery("#grid-table").trigger("reloadGrid");
		    			sxUtil.msgbox.success('操作成功！');
		    		}else{
		    			sxUtil.msgbox.error(e,'新增失败');
					}
		    	}
		    });
		},
		rules : {
			'name':{
				required : true,
				rangelength:[2,20],
				remote:{
					url:"/role/validateAdd",
					type:"get",
					dataType:'json',
					data:{
						roleName:function(){
							return $("input[name=name]").val();
						}
					}
				}
			}
			
		},
		messages :{
			'name':{
				required : "角色名称不能为空",
				rangelength:"角色名称必须是2~20个字符之间",
				remote:"角色名称已存在"
			}
		}
	 });
});