jQuery(document).ready(function() {

	/*
	 * $('#submit').on('click',function(e){ $.ajax({ type:'post', url
	 * :'/user/do_update', data:$('form').serializeArray(), success :
	 * function(data) { console.log(data);
	 * $("#grid-table").trigger("reloadGrid"); //重新载入 window.location.href = '/' }
	 * }); });
	 */
	 
	$("#userForm").validate({
		submitHandler:function(form){
			$(form).ajaxSubmit({
		    	success:function(e){
		    		if(e && e.success == true){
		    			$("#grid-table").trigger("reloadGrid"); // 重新载入
		    			sxUtil.msgbox.success('操作成功！');
			    		sxUtil.dialog.close($(form).data('index'));
		    		}else{
		    			sxUtil.msgbox.error(e,'修改失败');
					}
		    	}
		    });
		},
		rules : {
			'loginName': {
				required:true,
				rangelength:[2,20]
			},
			'name': {
				required:true,
				rangelength:[2,20]
			},
			'password': {
				required:true,
				rangelength:[5,32],
				checkPassword:true
			},
			'password2': {
				required:true,
				equalTo:'#password1'
			},
			'phone': {
				required:true,
				checkPhone:true
			},
			'userType.id': {
				required:true
			}
		},
		messages :{
			'loginName': {
				required:"登录名不能为空",
				rangelength:"登录名称必须是2~20个字符之间"
			},
			'name': {
				required:"用户名不能为空",
				rangelength:"用户名称必须是2~20个字符之间"
			},
			'password': {
				required:"密码不能为空",
				rangelength:"密码必须是5~32位数字和字母的组合"
			},
			'password2': {
				required:"确认密码不能为空",
				equalTo:"两次密码输入不一致"
			},
			'phone':{
				required:"手机号不能为空"
			},
			'userType.id':{
				required:"用户类型不能为空"
			}
		}
	 });
	
	// 自定义正则表达示验证方法
    $.validator.addMethod("checkPhone",function(value,element,params){  
            var checkPhone = /^1[34578]\d{9}$/;  
            return this.optional(element)||(checkPhone.test(value));  
        },"手机号格式不正确");
    
    $.validator.addMethod("checkPassword",function(value,element,params){  
        var checkPassword = /^[0-9A-Za-z]{5,32}$/;  
        return this.optional(element)||(checkPassword.test(value));  
    },"密码必须是5~32位数字和字母的组合");

});

$(".userRole_align span").find(".lbl").css({
	"width" : "280px",
	"whiteSpace" : "nowrap",
	"overflow" : "hidden",
	"textOverflow" : "ellipsis"
});
$("#userForm").delegate(
		"input[name$='admin']",
		"click",
		function() {
			if ($(this).is(':checked')) {
				$(this).prop("checked", true);
				$(this).parent().prev().find("input[name$='id']").prop(
						"checked", true);
			}
		})
$("#userForm").delegate(
		"input[name$='id']",
		"click",
		function() {
			if (!$(this).is(':checked')) {
				$(this).prop("checked", false);
				$(this).parent().next().find("input[name$='admin']").prop(
						"checked", false);
			}
		})