//得到接收的消息html(用于显示消息)
function GetMsgRecvHtml(msg,showname,iconpath)
{
    var htmltxt = "";
    htmltxt += "<table width='100%'  border='0' cellspacing='0' cellpadding='0'>";
    htmltxt += "<tr>";
    htmltxt += "<td style='width:10px;'></td>";
    htmltxt += "<td style='width:38px;vertical-align:top;text-align:center;font-size:8px;color:#666666;'><img style='border:1px #cacaca solid;background-color:#FFFFFF' src=\"" + iconpath + "\" width=\"32\" height=\"32\">"+showname+"</td>";
    htmltxt += "<td>";
    htmltxt += "  <table height='36' border='0' cellspacing='0' cellpadding='0'>";
    htmltxt += "  <tr>";
    htmltxt += "	<td style='width:10px; '>";
    htmltxt += "      <table style='width:100%; height:100%;' border='0' cellspacing='0' cellpadding='0'>";
    htmltxt += "        <tr>";
    htmltxt += "      	<td style='background-image:url(../Images/msg1.gif);height:23px; width:10px;background-size:10px 23px;' valign='top'></td>";
    htmltxt += "        </tr>";
    htmltxt += "        <tr>";
    htmltxt += "          <td style='background-image:url(../Images/msg2.gif);width:10px;background-size:10px 100%;' valign='top'></td>";
    htmltxt += "        </tr>";
    htmltxt += "        <tr>";
    htmltxt += "      	<td style='background-image:url(../Images/msg3.gif);height:4px; width:10px;background-size:10px 4px;'  valign='bottom'></td>";
    htmltxt += "        </tr>";
    htmltxt += "	  </table>";
    htmltxt += "  </td>";
    htmltxt += "  <td style='vertical-align:top;'>";
    htmltxt += "  <table style='height:100%; background-color:#f8f8f8;' border='0' cellspacing='0' cellpadding='0'>";
    htmltxt += "      <tr>";
    htmltxt += "  	    <td style='height:1px;background-image:url(../Images/msg-h1.gif);background-size:100% 2px;'></td>";
    htmltxt += "      </tr>";
    htmltxt += "      <tr>";
    htmltxt += "      	<td>";
    htmltxt += "      	    <div style='height:100%; padding-top: 10px;padding-bottom: 10px;padding-left: 10px;padding-right:10px;font-size:12px;'>" + msg + "</div>";
    htmltxt += "        </td>";
    htmltxt += "      </tr>";
    htmltxt += "      <tr>";
    htmltxt += "      	<td style='height:1px;background-image:url(../Images/msg-h2.gif);background-size:100% 1px;'></td>";
    htmltxt += "      </tr>";
    htmltxt += "  </table>";
    htmltxt += "  </td>";
    htmltxt += "  <td style='width:4px;'>";
    htmltxt += "      <table style='width:100%; height:100%;'  border='0' cellspacing='0' cellpadding='0'>";
    htmltxt += "          <tr>";
    htmltxt += "          	<td style='background-image:url(../Images/msg4.gif);height:4px; width:4px;background-size:4px 4px;'></td>";
    htmltxt += "          </tr>";
    htmltxt += "          <tr>";
    htmltxt += "          	<td style='background-image:url(../Images/msg5.gif);width:4px;background-size:4px 100%;'></td>";
    htmltxt += "          </tr>";
    htmltxt += "          <tr>";
    htmltxt += "          	<td  style='background-image:url(../Images/msg6.gif);height:4px; width:4px;background-size:4px 4px;'></td>";
    htmltxt += "          </tr>";
    htmltxt += "      </table>";
    htmltxt += "	</td>";
    htmltxt += "  </tr>";
    htmltxt += "</table>";
    htmltxt += "</td>";
    htmltxt += "<td style='width:10px;'></td>";
    htmltxt += "</tr>";
    htmltxt += "</table>";
    return htmltxt;
}
//得到发送消息的html(用于显示消息)
function GetMsgSendHtml(msg,userIconpath)
{
    var htmltxt = "";
    htmltxt += "<table width='100%' height='36' border='0' cellspacing='0' cellpadding='0'>";
    htmltxt += "  <tr>";
    htmltxt += "    <td style='width:10px;'></td>";
    htmltxt += "    <td><table height='100%' align='right' border='0' cellspacing='0' cellpadding='0'>";
    htmltxt += "      <tr>";
    htmltxt += "        <td style ='width:4px;'><table width='4' height='100%'  border='0' cellpadding='0' cellspacing='0'>";
    htmltxt += "            <tr>";
    htmltxt += "              <td style='background-image:url(../Images/msg1a.gif); height:4px; width:4px;background-size:4px 4px; '></td>";
    htmltxt += "            </tr>";
    htmltxt += "            <tr>";
    htmltxt += "              <td  style='background-image:url(../Images/msg2a.gif);background-size:4px 100%; '></td>";
    htmltxt += "            </tr>";
    htmltxt += "            <tr>";
    htmltxt += "              <td style='background-image:url(../Images/msg3a.gif); height:4px; background-size:4px 4px; '></td>";
    htmltxt += "            </tr>";
    htmltxt += "        </table></td>";
    htmltxt += "        <td><table width='100%' height='100%' border='0' cellspacing='0' cellpadding='0'>";
    htmltxt += "            <tr>";
    htmltxt += "              <td style='height:1px;background-image:url(../Images/msg-h3.gif); background-size:100% 2px;'></td>";
    htmltxt += "            </tr>";
    htmltxt += "            <tr>";
    htmltxt += "              <td style='background-color:#428fe6;'><div style='height:100%; padding-top: 10px;padding-bottom: 10px;padding-left: 10px;padding-right:10px;font-size:12px;color:#FFFFFF'>" + msg + "</div>";
    htmltxt += "            </tr>";
    htmltxt += "            <tr>";
    htmltxt += "              <td style='height:1px; background-image:url(../Images/msg-h4.gif); background-size:100% 2px;'></td>";
    htmltxt += "            </tr>";
    htmltxt += "        </table></td>";
    htmltxt += "        <td style='width:10px;'><table width='10' height='100%' border='0' cellspacing='0' cellpadding='0'>";
    htmltxt += "            <tr>";
    htmltxt += "              <td style='background-image:url(../Images/msg4a.gif);width:10px; height:22px;background-size:10px 22px;'></td>";
    htmltxt += "            </tr>";
    htmltxt += "            <tr>";
    htmltxt += "              <td style='background-image:url(../Images/msg5a.gif);width:10px;height:100%;background-size:10px 100%;'></td>";
    htmltxt += "            </tr>";
    htmltxt += "            <tr>";
    htmltxt += "              <td style='background-image:url(../Images/msg6a.gif);height:4px;height:4px;background-size:10px 4px;'></td>";
    htmltxt += "            </tr>";
    htmltxt += "        </table></td>";
    htmltxt += "      </tr>";
    htmltxt += "    </table></td>";
    htmltxt += "    <td style='width:38px;vertical-align:top;text-align:center;font-size:8px;color:#666666;'><img style='border:1px #9ac6f8 solid;background-color:#FFFFFF' src=\"../" + userIconpath + "\" width=\"32\" height=\"32\">"+window.parent._UserTName+"</td>";
    htmltxt += "    <td style='width:10px;'></td>";
    htmltxt += "  </tr>";
    htmltxt += "</table>";
    return htmltxt;

}

//读取本地聊天记录
function LoadLocalMsgList()
{
    var userno = document.getElementById("txt_UserNo").value;
    var usertname = document.getElementById("txt_userTName").value;
    
    var msgtemplist = "";
    //userno + "-MsgTempList-"+sender
    if(localStorage.getItem( window.parent._UserNo + "-MsgTempList-" + userno))
    {
        msgtemplist = localStorage.getItem(window.parent._UserNo + "-MsgTempList-" + userno);
    }
                
    var templlll = msgtemplist.split('\n');

    var hhhhtml = "";
    
    for (var i=templlll.length-1;i>=0;i--)
    {
        if(templlll[i]=="")
        {
            continue;
        }
        var mytime = templlll[i];
        var msglist = "";
        //userno + "-Msg-" + sender + "-" + tdate
        if(localStorage.getItem(window.parent._UserNo + "-Msg-" + userno + "-" + mytime))
        {
            msglist = localStorage.getItem(window.parent._UserNo + "-Msg-" + userno + "-" + mytime);
        }
        else
        {
            continue;
        }
        var tempmmmttt = "";
        var msggg = msglist.split('\n');
        for(var n=0;n<msggg.length;n++)
        {
            if(msggg[n]=="")
            {
                continue;
            }
            var te = msggg[n].split('\t');
            if(te[1] == "0")
            {
                tempmmmttt = tempmmmttt + GetMsgRecvHtml(te[0].replace(/SisenMESS_ReLine_Code/g,"<br>"),usertname,userIcon) + "<br>";
            }
            else
            {
                tempmmmttt = tempmmmttt + GetMsgSendHtml(te[0].replace(/SisenMESS_ReLine_Code/g,"<br>"),window.parent._MyUserIconUrl) + "<br>";
            }
        }
        
        hhhhtml = "<div style='text-align:center; color:#CCCCCC;font-size: 10px;'>---------" + mytime + "---------</div>" + tempmmmttt + "<br>" + hhhhtml;
    }
    
    MsgShowDiv.innerHTML += hhhhtml;
    
    MsgShowDiv.scrollTop = MsgShowDiv.scrollHeight;
    
    //            var dd = 2;
    //            for (var i=0;i < 3;i++)
    //            {
    //                var msglist ="";
    //                
    //                var today = new Date();
    //                today.setDate(today.getDate() - dd);
    //                var mytime = today.toLocaleDateString();    //获取当前时间
    //                
    //                if(localStorage.getItem(userno + "-Msg-" + mytime))
    //                {
    //                    msglist = localStorage.getItem(userno + "-Msg-" + mytime);
    //                    
    //                    MsgShowDiv.innerHTML += msglist + "<div style='text-align:center; color:#CCCCCC;'>---------" + mytime + "---------</div><br>";
    //                }
    //                
    //                dd = dd -1;
    //            }
    //            
    //            MsgShowDiv.scrollTop = MsgShowDiv.scrollHeight;
}

//保存聊天记录
function SaveLocalMsgList(userno,sender,msg ,tdate ,isSend)
{
    msg = msg.replace(/SisenMESS_ReLine_Code/g,"<br>");
    //var myDate = new Date();
    //var mytime = myDate.toLocaleDateString();    //获取当前时间
    var tempmsgdatelist = "";
    if(localStorage.getItem(userno + "-MsgTempList-"+sender))
    {
        tempmsgdatelist = localStorage.getItem(userno + "-MsgTempList-"+sender);
    }
    
    if(tempmsgdatelist.indexOf(tdate) == -1)
    {            
        tempmsgdatelist = tempmsgdatelist + "\n" + tdate;
        localStorage.setItem(userno + "-MsgTempList-"+sender, tempmsgdatelist);
    }
    
    var msglist ="";
    if(localStorage.getItem(userno + "-Msg-" + sender + "-" + tdate))
    {
        msglist = localStorage.getItem(userno + "-Msg-" + sender + "-" + tdate);
    }
                
    msglist += msg + "\t" + isSend + "\n";
    
    localStorage.setItem(userno + "-Msg-" + sender + "-" + tdate, msglist);
    
    if(isSend==1)//如果是自己，就不算是新来未读记录
    {
        return;
    }
    
    var msgcounttemp=0;
    
    if (localStorage.getItem(userno + "-LastMsgUserMsgCount-" + sender)) 
    {
        msgcounttemp = parseInt(localStorage.getItem(userno + "-LastMsgUserMsgCount-" + sender));
    }
    
    msgcounttemp+=1;
    
    localStorage.setItem(userno + "-LastMsgUserMsgCount-" + sender,msgcounttemp);
}

//保存最近联系人
function SaveLastUserList(userno,sender)
{
    var lastuserlist = sender + "\n";
    var tempuserlist = "";
    if (localStorage.getItem("LastMsgUserList-"+userno)) {
        tempuserlist = localStorage.getItem("LastMsgUserList-"+userno);
    }

    var useritem = tempuserlist.split('\n');

    for (var i = 0; i < useritem.length; i++) {
        if (useritem[i] == "") {
            continue;
        }
        if (useritem[i] == sender) {
            continue;
        }
        lastuserlist += useritem[i] + "\n";
    }

    localStorage.setItem("LastMsgUserList-"+userno, lastuserlist);
}
//保存用户最后通讯摘要
function SaveUserLastMsgSummary(userno,sender,sum,datetime)
{
    localStorage.setItem("UserLastMsgSummary-"+userno+sender,"<table width='100%'  border='0' cellspacing='0' cellpadding='0'><tr><td style='width:48px; '><span class = 'MsgDateTime'>"+datetime+"</span></td><td class = 'UserDetail'>"+sum+"</td></tr></table>");
}
//得到用户最后通讯摘要
function GetUserLastMsgSummary(userno,sender)
{
    if (localStorage.getItem("UserLastMsgSummary-"+userno+sender)) {
        return localStorage.getItem("UserLastMsgSummary-"+userno+sender);
    }
    return "";
}
//设置未读消息总数
function SetUserNoReadMsgCount(userno,sender,count)
{    
    localStorage.setItem(userno + "-LastMsgUserMsgCount-" + sender,count);
}

//得到用户未读消息
function GetUserNoReadMsgCount(userno,sender)
{
    var msgcounttemp=0;
    
    if (localStorage.getItem(userno + "-LastMsgUserMsgCount-" + sender)) 
    {
        msgcounttemp = parseInt(localStorage.getItem(userno + "-LastMsgUserMsgCount-" + sender));
    }
    
    return msgcounttemp;
}
//读取本地群组聊天记录
function LoadLocalMsgGroupMsgList(msgGroupId,userNo)
{
    var msgtemplist = "";
    if(localStorage.getItem(userNo + "-MsgGroupTempList-" + msgGroupId))
    {
        msgtemplist = localStorage.getItem(userNo + "-MsgGroupTempList-" + msgGroupId);
    }
    if(msgtemplist=="")
    {
        return;
    }
    
    var templlll = msgtemplist.split('\n');

    var hhhhtml = "";
    
    for (var i=templlll.length-1;i>=0;i--)
    {
        if(templlll[i]=="")
        {
            continue;
        }
        var mytime = templlll[i];
        var msglist = "";
        //userno + "-Msg-" + sender + "-" + tdate
        if(localStorage.getItem(userNo + "-MsgGroupMsg-" + msgGroupId + "-" + mytime))
        {
            msglist = localStorage.getItem(userNo + "-MsgGroupMsg-" + msgGroupId + "-" + mytime);
        }
        else
        {
            continue;
        }
        var tempmmmttt = "";
        var msggg = msglist.split('\n');
        
        for(var n=0;n<msggg.length;n++)
        {
            if(msggg[n]=="")
            {
                continue;
            }
            var te = msggg[n].split('\t');
            if(te[3] == "0")
            {
                tempmmmttt = tempmmmttt + GetMsgRecvHtml(te[0].replace(/SisenMESS_ReLine_Code/g,"<br>"),GetMsgGroupUserTName(te[1]),te[2]) + "<br>";
            }
            else
            {
                tempmmmttt = tempmmmttt + GetMsgSendHtml(te[0].replace(/SisenMESS_ReLine_Code/g,"<br>"),te[2]) + "<br>";
            }
        }
        
        hhhhtml = "<div style='text-align:center; color:#CCCCCC;font-size: 10px;'>---------" + mytime + "---------</div>" + tempmmmttt + "<br>" + hhhhtml;
        
    }
    
    MsgShowDiv.innerHTML += hhhhtml;
    
    MsgShowDiv.scrollTop = MsgShowDiv.scrollHeight;
}
//保存群聊天记录
function SaveLocalMsgGroupMsgList(msgGroupId,userNo,sender,senderimagepath,msg,tdate,isSend)
{
    var tempmsgdatelist = "";
    if(localStorage.getItem(userNo + "-MsgGroupTempList-" + msgGroupId))
    {
        tempmsgdatelist = localStorage.getItem(userNo + "-MsgGroupTempList-" + msgGroupId);
    }
    
    if(tempmsgdatelist.indexOf(tdate) == -1)
    {
        tempmsgdatelist = tempmsgdatelist + "\n" + tdate;
        localStorage.setItem(userNo + "-MsgGroupTempList-"+msgGroupId, tempmsgdatelist);
    }
    
    var msglist ="";
    if(localStorage.getItem(userNo + "-MsgGroupMsg-" + msgGroupId + "-" + tdate))
    {
        msglist = localStorage.getItem(userNo + "-MsgGroupMsg-" + msgGroupId + "-" + tdate);
    }
                
    msglist += msg + "\t" + sender + "\t" + senderimagepath + "\t" + isSend + "\n";
    
    localStorage.setItem(userNo + "-MsgGroupMsg-" + msgGroupId + "-" + tdate, msglist);
    
}