/********************************************************/
/*公共的js文件，包括一些公用的方法和工具方法
/*
/********************************************************/



/**
 * 判断字符串是否为空
 */
function isEmpty(str){
    str = $.trim(str);
    if(str!=null&&str.length==0)
        return true;
    else if(str=="")
        return true
    else
        return false;
}

$(function(){
    $("#submit").click(login);
    addValidatorMethod();
    highLightMenu();
//    initUser();
});

/**
 * 初始化验证规则
 */
function addValidatorMethod(){
    //电话号码
    jQuery.validator.addMethod("tel",function(value,element){
        var tel = /(^\d{3,4}-?\d{7,9}-?\d{4})|(^\d{3,4}-?\d{7,9})$/;
        return this.optional(element) || (tel.test(value));
    },"请填写正确的电话号码");

     //手机
    jQuery.validator.addMethod("mobile",function(value,element){
        var mobile = /^(((13[0-9]{1})|(15[0-9]{1})|(14[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        return this.optional(element) || (mobile.test(value));
    },"请填写正确的手机号码");

     //邮编
    jQuery.validator.addMethod("postCode",function(value,element){
        var postcode = /^[0-9]{6}$/;
        return this.optional(element) || (postcode.test(value));
    },"请填写正确的邮编");
    
     //IP
    jQuery.validator.addMethod("ip",function(value,element){
        var ip = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
        return this.optional(element) || (ip.test(value)) && (RegExp.$1<256 &&  RegExp.$2<256  && RegExp.$3<256 &&  RegExp.$4<256  );
    },"请填写正确的IP");

    //money
    jQuery.validator.addMethod("money",function(value,element){
        var money = /^\d{1,12}(?:\.\d{1,2})?$/;
        return this.optional(element) || (money.test(value));
    },"请填写正确的金额");

    //username
    jQuery.validator.addMethod("username",function(value,element){
        var username = /^[A-Za-z0-9]+$/;
        return this.optional(element) || (username.test(value));
    },"用户名不符合规则");

    //password
    jQuery.validator.addMethod("password",function(value,element){
        var password = /^[A-Za-z0-9]+$/;
        return this.optional(element) || (password.test(value));
    },"密码不符合规则");

    

}


/**
 * 初始化用户函数
 */
function initUser(){
    $.ajax({
        url:"user/initUser.shtml",
        data:{},
        type:"POST",
        contentType:"application/x-www-form-urlencoded;charset=utf-8",
        dataType:"json",
        //是否同步
        async:true,
        error:function(x,e){
            alert("ajax调用错误"+e);
        },
        success:function(data){
            //session存在用户
            if(data){
                $("#topLogin1").hide();
                $("#topLogin2").show();
                $("#username_dis").html(data.userName);
                $("#lastLoginTime_dis").html(data.lastLoginTime);
                $("#lastLoginIP_dis").html(data.lastLoginIP);
            }
            else{
                $("#topLogin1").show();
                $("#topLogin2").hide();
                $("#userName").val("")
                $("#userPass").val("")
            }
           
        }
    });
}
//注销函数
function logout(){
        $.ajax({
            url:"user/logout.shtml",
            data:{},
            type:"POST",
            contentType:"application/x-www-form-urlencoded;charset=utf-8",
            dataType:"json",
            //是否同步
            async:false,
            error:function(x,e){
                alert("ajax调用错误"+e);
            },
            success:function(data){
                window.location.reload();

            }
        });
}

//登陆函数
function login(){
    var userNameValue = $.trim($("#userName").val());
    var userPassValue = $.trim($("#userPass").val());
    if(isEmpty(userNameValue)||isEmpty(userPassValue))
        return;
    $.ajax({
            url:"user/login.shtml",
            data:{userName:userNameValue,userPass:userPassValue},
            type:"POST",
            contentType:"application/x-www-form-urlencoded;charset=utf-8",
            dataType:"json",
            //是否同步
            async:false,
            error:function(x,e){
                alert("ajax调用错误"+e);
            },
            success:function(data){
                //登陆成功
                if(data.success){
                         window.location.reload();
                }
                else{
                	if(data.msg=="userinfoError")
                		alert("用户名或密码不正确");
                	else if(data.msg="notActive")
                		alert("用户正在认证中。。。");
                }
            }
        });
}

//菜单高亮显示
function highLightMenu(){
    $(".menu>ul>li>a").click(function(){
        $(this).attr("id","menuS");
    });
}

/**
 *打开遮罩
 */
function openMask(){
    /**
     *覆盖层
     */
    var mask = "<div id=\"coverMask\" style=\"filter:Alpha(opacity=50);text-align: center;\">"+"<img src=\""+basePath+"resources/images/submit-bar.gif\"/>"+"</div>";
    $("body").append(mask);
    $("#coverMask").css({left:"0px",top:"0px",height:document.body.clientHeight+"px",width:document.body.clientWidth+"px","z-index":"1000",position:"absolute","background-color":"#ABABAB"});
    $("#coverMask>img").css({"margin-top":document.body.clientHeight/2+"px"});
}
//关闭遮罩
function closeMask(){
    $("#coverMask").remove();
}




