var MH = {};

MH.CheckLogin = function()
{
    var isLogin = MH.IsLogin();
    if (!isLogin)
    {
        alert("您还没有登录，请先登录。");
    }
    return isLogin;
}
MH.IsLogin = function()
{
    var ma = MH.GetMemberAuction();
    if (MH.currMem != null) return true;
    MH.currMem = ma.GetCurrentMemberInfo("tmp");    
    return MH.currMem != null;
}

MH.Login = function(u,p)
{
    if (u == ""){MH.LoginMsg("*请输入用户名");return;}
    if (p == ""){MH.LoginMsg("*请输入密码");return;}
       
    MH.LoginMsg("正在登录，请稍候...");
    var ma = MH.GetMemberAuction();
    ma.Login(u,p,function(obj){
        if (obj == null || obj == false)
        {
            MH.LoginMsg("登录失败，请重试。");
            return ;
        }        
        MH.LoginMsg("登录成功，正在跳转页面...");
        window.location = '/MemberCenter.html';
    })
}

MH.GetMemberAuction = function()
{  
    if (this.ma == null){
        this.ma = new MemberAcution();    
    }
    return this.ma;
}

MH.LoginMsg = function(msg)
{
    var txt = $('#MemberLogonTxt')[0]; 
    txt.innerHTML = "<span style='color:red;'>" + msg + "</span>";
}

MH.LogonOut = function()
{
     var ma = new MemberAcution(); 
     ma.LogonOut("",function(){
        window.location = "/";
     });
}

MH.ChgPwd = function()
{
   var ma = MH.GetMemberAuction();
   var o = $("#OldPwd")[0].value;
   var n = $("#NewPwd")[0].value;
   if (o == "" || n == ""){
        alert("请输入密码");
        return;
   }
   ma.ChgPwd(o,n,function(r){
        if (r.Succeed)
        {
            alert("修改成功");
        }else
        {
            alert("修改失败,原因是"+r.Message);
        }
   })
}

MH.AccountIsExist = function(account,callback)
{
   var ma = MH.GetMemberAuction();
   ma.AccountIsExist(account,callback);
}

MH.AddToFavorite = function(userId)
{
    var isLogon = MH.CheckLogin();
    if (!isLogon) return;
    var ma = MH.GetMemberAuction();      
    ma.AddToFavorite(userId,function(v){
        if (!v)
        {
            alert("收藏失败!");
        }else
        {
            alert("收藏成功!");    
        }
    });  
}

MH.SendMsg = function(userId)
{
    var isLogon = MH.CheckLogin();
    if (!isLogon) return;
    window.location =  '/Message/Send.php?toId=' + userId;
}

MH.Init = function()
{
    try
    {
        if ($("#buttonSave").length == 0) return ;
        $("#buttonSave")[0].onclick = MH.ChgPwd;
    }catch(e){
    }
}
$(document).ready(function(){MH.Init()}) 