﻿// JScript 文件
//检查用户名
var VarW_UNameBak = "";
var IsUNameExists = "False";
var VarW_EppooMsgTypeId = 10;
var ReplyPower = false;
var VarW_Action = "N";
var VarW_CurMsgId = 0;
var VarW_IsAnonMsg = "";
var VarW_CurPage = 1;
function fGoto()
{
    ;
}

function CallBack_ChkUName(res)
{
    //alert(res.value);    
    IsUNameExists = res.value;
    //alert(IsUNameExists);
    SaveMsg();
    /*if(r=="True")
    {
        //document.getElementById("span_UNameHint").innerHTML = "";        
        IsUNameExists = "T";
        SaveMsg();
    }
    else if(r=="False")
    {
        //document.getElementById("span_UNameHint").innerHTML = "--用户名不存在";
        IsUNameExists = "F";
        if
    }
    else
    {
        //document.getElementById("span_UNameHint").innerHTML = "--输入次数过多";
        IsUNameExists = "M";
        SaveMsg();
    }*/
    
    //document.getElementById("SubBtn").disabled = false;
}

function ChkUName()
{   
    var UName = document.getElementById("UName").value;
    //本次输入的用户名与上一次输入的不同,检查
    if(UName!=VarW_UNameBak)
    {
        document.getElementById("span_UNameHint").innerHTML = "--正在检查用户名...";
        VarW_UNameBak = UName;
        eppoo_msg.ChkUName(UName,CallBack_ChkUName);
    }
    else
    {
        document.getElementById("SubBtn").disabled = false;
    }
}


//Ctr the button
function CtrSubBtn(par)
{
    if(par=="C")
    {
        document.getElementById("SubBtn").value = "处理中...";
        document.getElementById("SubBtn").disabled = true;
    }
    else
    {
        document.getElementById("SubBtn").value = "提交";
        document.getElementById("SubBtn").disabled = false;
    }
}


//Save
function CallBack_SaveMsg(res)
{    
    var res = res.value;
    if(res=="S")
    {
        document.getElementById("Cont").value="";
        CtrSubBtn("O");
        QuerData(VarW_EppooMsgTypeId);
    }
    else
    {
        CtrSubBtn("O");
        alert(res);
    }
}

function PreSaveMsg()
{   
    var UName = document.getElementById("UName").value;
    //alert(UName);
    eppoo_msg.ChkUName(UName,CallBack_ChkUName);
}
function SaveMsg()
{
    var Cont = document.getElementById("Cont").value.replace(/^\s+|\s+$/g,"");
    
    if(Cont.length==0)
    {
        CtrSubBtn("O");
        alert("请输入内容.");
        document.getElementById("Cont").focus();
        return;
    } 
    Cont = document.getElementById("Cont").value;
    
    var UName = document.getElementById("UName").value.replace(/^\s+|\s+$/g,"");
    
    var IsMSMHint = "N";
    if(document.getElementById("IsMSMHint").checked)
        IsMSMHint = "Y";
        
    if(IsMSMHint=="Y" && UName.length==0)
    {
        CtrSubBtn("O");
        alert("您选择了站内信通知，请输入您的用户名！");
        document.getElementById("UName").focus();
        return;
    }
    
    if(UName.length>0 && UName.length<2)
    {
        CtrSubBtn("O");
        alert("请输入正确的用户名！");
        document.getElementById("UName").focus();
        return;
    }
    /*if(IsUNameExists=="False" && UName!="")
    {
        if(!confirm("输入的用户名不存在,是否继续提交?"))
        {
            CtrSubBtn("O");
            return;
        }
        else
            UName = "";
    }
    else if(IsUNameExists=="Max")
    {
        UName = "";
    }*/
    
    VarW_EppooMsgTypeId = document.getElementById("TypeId").value;    
    

        
  
    //alert("Que Id:" + VarW_EppooMsgTypeId + "\nUserName:" + UName + "\nContent:" + Cont + "\nMSM Hint:" + IsMSMHint);
    eppoo_msg.SaveMsg(VarW_EppooMsgTypeId,UName,Cont,IsMSMHint,CallBack_SaveMsg);
}





//Read data
function QuerData(type_id)
{
    VarW_EppooMsgTypeId = type_id;
    getDataByPager(1);
    
}

function getDataByPager(cp)
{
    document.getElementById("loadingHint").innerHTML = "<img src='http://eppoo.com/eppoo/img/loading_32x32.gif' style='vertical-align:middle; width:16px; height:16px;' /> 正在加载数据…";
    var count = document.getElementById("count").value;
    if(cp=="1" || count=="-1" || isNaN(count) || count=="")
        count = -1;
    var PageSize = 5;
    eppoo_msg.GetEppooMsgFromDB(VarW_EppooMsgTypeId,cp,count,PageSize,
    
    function(res)
    {
        var r = res.value;
        if(r!="")
        {
            var starpos = r.indexOf("*");
            var rc = r.substr(0,starpos);
            r = r.substr(starpos+1);
            if(r!="")
            {
                r = eval("("+r+")");
                var oLen = r.length;
                var PageStr = getPager(rc,PageSize,"getDataByPager",cp,"1");
                document.getElementById("count").value = rc;       
                var MsgList = "";
                for(var i=0;i<oLen;i++)
                {
                    var o = r[i];                    
                    var Cont = o.cont;
                    Cont = Cont.replace(/\r\n/g,"<br>");
                    if(o.uInf!="")
                    {
                        var uinf = o.uInf.split("*");
                        var blog = "www";
                        var unam = "亿泊网友";
                        if(uinf.length==2)
                        {
                            blog = uinf[0];
                            unam = uinf[1];
                        }
                        MsgList += "<a href='http://"+uinf[0]+".eppoo.com/' target='_blank' title='访问"+unam+"的个性空间'><span class='user'>"+unam+"</span></a> &nbsp;&nbsp;"+o.ct;
                    }
                    else
                        MsgList += "<span class='user' title='匿名用户'>亿泊网友</span></a> &nbsp;&nbsp;"+o.ct;

                    if(ReplyPower)
                        MsgList += "&nbsp;<a href='javascript:fGoto()' onclick=\"javascript:PreReplyMsg("+o.id+");\">回复</a>";
                    MsgList += "<br />友好建议&nbsp;：<strong>"+ Cont +"</strong>";
                    MsgList += "<br />";
                    
                    Cont = o.rcont;
                    
                    if(Cont!="")
                        MsgList += "<span class='red'>Eppoo回复：</span>"+Cont.replace(/\r\n/g,"<br>")+"["+o.rt+"]";
                        
                    MsgList += "<hr />"
                }
                
                document.getElementById("MsgList").innerHTML = MsgList + "<div>" + PageStr + "</div>";
                document.getElementById("loadingHint").innerHTML = "";
            }
        }
    }
    
    );
}


function CallBack_GetReplyPower(res)
{
    var result = res.value;
    if(result.length>1)//已登录
    {
        if(result.substr(0,1)=="T")//有权限回复
            ReplyPower = true;
            
        document.getElementById("UName").value = result.substr(1,result.length-1);
        //document.getElementById("span_UName").style.display = "none";
        //document.getElementById("span_AUName").style.display = "block";
        //document.getElementById("span_AUName").innerText = "ABCD"//result.substr(1,result.length-1);
        
        //document.getElementById("UName").readonly = true;
    }
        
    if(getMsgId()=="")
        QuerData(10);
    else
        GetMsgByMsgId();
}

function GetMsgByMsgId()
{
    eppoo_msg.GetMsgById(getMsgId(),
    
    function(res)
    {
        var r = res.value;
        //alert(r);
        if(r!="")
        {
            r = eval("("+r+")");    
            var MsgList = "";

            var o = r[0];                    
            var Cont = o.cont;
            Cont = Cont.replace(/\r\n/g,"<br>");
            if(o.uInf!="")
            {
                var uinf = o.uInf.split("*");
                var blog = "www";
                var unam = "亿泊网友";
                if(uinf.length==2)
                {
                    blog = uinf[0];
                    unam = uinf[1];
                }
                MsgList += "<a href='http://"+uinf[0]+".eppoo.com/' target='_blank' title='访问"+unam+"的个性空间'><span class='user'>"+unam+"</span></a> &nbsp;&nbsp;"+o.ct;
            }
            else
                MsgList += "<span class='user' title='匿名用户'>亿泊网友</span></a> &nbsp;&nbsp;"+o.ct;
            //alert(ReplyPower);
            if(ReplyPower)
                MsgList += "&nbsp;<a href='javascript:fGoto()' onclick=\"javascript:PreReplyMsg("+o.id+");\">回复</a>";
            MsgList += "<br />友好建议&nbsp;：<strong>"+ Cont +"</strong>";
            MsgList += "<br />";
            
            Cont = o.rcont;
            
            if(Cont!="")
                MsgList += "<span class='red'>Eppoo回复：</span>"+Cont.replace(/\r\n/g,"<br>")+"["+o.rt+"]";
                
            MsgList += "<hr />"
            
            document.getElementById("MsgList").innerHTML = MsgList;
        }
        
    }
    
    );
}

function EppooMsgLoad()
{
    eppoo_msg.GetReplyPower(CallBack_GetReplyPower);
    
}

function PreReplyMsg(_id,_isAnon)
{
    VarW_Action = "R";
    VarW_CurMsgId = _id;
    //VarW_IsAnonMsg = _isAnon;
    document.getElementById("TypeId").disabled=true;
    document.getElementById("UName").disabled=true;
    document.getElementById("Cont").value="在这里输入回复内容";
}

function SubBtn_Onclick()
{
    CtrSubBtn("C");
    if(VarW_Action=="N")
        SaveMsg();
        //PreSaveMsg();
    else if(VarW_Action=="R")
        ReplyMsg();
}

function CallBack_ReplyMsg(res)
{
    
    CtrSubBtn("O");
    switch(res.value)
    {
        case "S":
            QuerData(VarW_EppooMsgTypeId);
            document.getElementById("TypeId").disabled=false;
            document.getElementById("UName").disabled=false;
            document.getElementById("Cont").value="";
            alert("操作成功！");
            break;
        case "N":
            alert("没有权限！");
            break;
        default:
            alert("系统异常！");
            break;
    }
}
function ReplyMsg()
{
    var Cont = document.getElementById("Cont").value.replace(/^\s+|\s+$/g,"");
        
    if(Cont.length==0)
    {
        alert("请输入回复内容.");
        document.getElementById("Cont").focus();
        CtrSubBtn("O");
        return;
    } 
    /*if(VarW_IsAnonMsg!="Y" && VarW_IsAnonMsg!="N")
    {
        CtrSubBtn("O");
        alert("参数错误！\n\n可能需要你刷新本页面再试。")
    } */ 
    //alert("Que Id:" + TypeId + "\nUserName:" + UName + "\nContent:" + Cont + "\nMSM Hint:" + IsMSMHint);
    //alert(VarW_IsAnonMsg);
    eppoo_msg.ReplyMsg(VarW_CurMsgId,VarW_EppooMsgTypeId,Cont,VarW_IsAnonMsg,CallBack_ReplyMsg);
}