<%@ page language="C#" autoeventwireup="true" inherits="Msg_MsgShow, App_Web_oey1parl" %>

<!DOCTYPE html>

<html manifest="SisenMESS-Mobile-MsgShow.appcache" >
<head runat="server">
    <title>无标题页</title>
    <script src="../Scripts/MyDate.js"></script>
    <script src="../Scripts/UserMsgClass.js"></script>
    <script type="text/javascript">
        //禁止返回键
        javascript:window.history.forward(1);
        
        //window.onresize = ResetImageSize;
        
        //用户头像
        var userIcon = "../UserIcon/SisenMESS-User-None-Icon.gif";
        //当前聊天窗口的所属用户名
        var _UserNo;
        //还回上一页
        function BackPage()
        {
            //window.parent.ShowOrHideToolBar(true);
            //"MsgList.aspx?UserNo=" + window.parent._UserNo;
            //window.location.href = window.parent._PageTempName;
                        
            if(window.parent._PageTempName.indexOf("MsgList.aspx") !=-1)
            {
                window.location.href = "MsgList.aspx?UserNo=" + window.parent._UserNo;
            }
            else if(window.parent._PageTempName.indexOf("ContactList.aspx") !=-1)
            {
                window.location.href = "../Contact/ContactList.aspx?UserNo=" + window.parent._UserNo;
            }
            else if(window.parent._PageTempName.indexOf("WorkGroup.aspx") !=-1)
            {
                window.location.href = "../WorkGroup/WorkGroup.aspx?UserNo=" + window.parent._UserNo;
            }
        }
        //页面加载
        function PageLoadFun()
        {
            window.parent.ShowOrHideToolBar(false);
            
            MsgShowDiv.innerHTML = "<br>";//顶上有一块被挡住了。所以加两个换行。
            userIcon = window.parent._TempMsgUserIconUrl;
            LoadUserInfo();
            LoadLocalMsgList();
            LoadServerMsgList();
            LoadOutLineFileList();
            
            document.getElementById("FaceFrame").contentWindow.location.href = "../WaitingPage.aspx";
            document.getElementById("MyUserNo").value = window.parent._UserNo;
            ResetImageSize();
        }
        //读取用户信息
        function LoadUserInfo()
        {
            XMLHttpGet(document.getElementById("txt_UserNo").value);
            //document.getElementById("labUserName").innerText = "";
        }
        
        var xmlHttpRequest = null;
        //收到消息，发现没有用户信息，就到服务器端下载用户信息
        function XMLHttpGet(userno)
        { 
            xmlHttpRequest = new XMLHttpRequest();
            xmlHttpRequest.open("POST", "../SisenMESS-Mobile-WebService.asmx/GetUserInfo", true);
            // 当发生状态变化时就调用这个回调函数
            xmlHttpRequest.onreadystatechange = ajaxCallBack;
            // 使用post提交时必须加上下面这行代码
            xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            
            // 向服务器发出一个请求
            xmlHttpRequest.send("userNo=" + userno);
        }
        function ajaxCallBack()
        {
            if(xmlHttpRequest.readyState == 4)
            {
                if(xmlHttpRequest.status == 200)
                {
                    var content = xmlHttpRequest.responseText;
                    
                    var tempmsg = content.split('\n');
                    
                    var userno = document.getElementById("txt_UserNo").value;
                                                            
                    var username = tempmsg[1];
                    
                    var userdetail = tempmsg[2];
                    
                    var usericonpath = tempmsg[3];
                    
                    userIcon = usericonpath;
                    
                    localStorage.setItem("UserInfo-" + userno,username + "\n" + userdetail);
                                        
                    localStorage.setItem("userIcon-" + userno,usericonpath);
                    
                    document.getElementById("labUserName").innerText = username;//+ "<br><span class = 'UserDetail'>" + userdetail + "</span>";
                    
                    document.getElementById("txt_userTName").value = username;
                }
            }
        }
                
        var readmsgRequest = null;
        //读取服务器端聊天
        function LoadServerMsgList()
        {
            readmsgRequest = new XMLHttpRequest();
            readmsgRequest.open("POST", "../SisenMESS-Mobile-WebService.asmx/GetUserMsgDetail", true);
            // 当发生状态变化时就调用这个回调函数
            readmsgRequest.onreadystatechange = ShowMsgCallback;
            // 使用post提交时必须加上下面这行代码
            readmsgRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            
            // 向服务器发出一个请求
            readmsgRequest.send("userNo=" + window.parent._UserNo + "&sender=" + document.getElementById("txt_UserNo").value);
        }
        //显示聊天记录
        function ShowMsgCallback()
        {
            if(readmsgRequest.readyState == 4)
            {
                if(readmsgRequest.status == 200)
                {
                    var content = readmsgRequest.responseText;
                    
                    if(content=="")
                    {
                        return;
                    }
                    
                    var msghhtt = content.split('\n');
                    
                    var userno = document.getElementById("txt_UserNo").value;

                    for (var i=0;i<msghhtt.length;i++)
                    {
                        if(msghhtt[i]=="")
                        {
                            continue;
                        }
                        msghhtt[i] = msghhtt[i].replace(/SisenMESS_ReLine_Code/g,"<br>");
                        
                        var tempmmssgg = msghhtt[i].split('\t');
                        
                        var msgshowhtml = GetMsgRecvHtml(tempmmssgg[0],document.getElementById("txt_userTName").value,userIcon) + "<br>";
                        
                        MsgShowDiv.innerHTML += msgshowhtml;
                        
                        //保存用户最后通讯概要
                        SaveUserLastMsgSummary(window.parent._UserNo,userno,tempmmssgg[2],tempmmssgg[3]);
                        
                        //保存聊天记录
                        SaveLocalMsgList(window.parent._UserNo,userno,tempmmssgg[0], tempmmssgg[1] ,"0");
                    }
                    
                    
                    
                    //DoImageResize();
                    
                    MsgShowDiv.scrollTop = MsgShowDiv.scrollHeight;
                    
                    ResetImageSize();

//                    var tempmsg = content.split('\n');
//                    
//                    for (int i=0;i<tempmsg.length;i++)
//                    {
//                        
//                    }
                }
            }
        }
        
        
        var outFileRequest = null;
        //得到离线消息
        function LoadOutLineFileList()
        {
            outFileRequest = new XMLHttpRequest();
            outFileRequest.open("POST", "../SisenMESS-Mobile-WebService.asmx/GetUserSendFileGetList", true);
            // 当发生状态变化时就调用这个回调函数
            outFileRequest.onreadystatechange = ShowOutFileCallback;
            // 使用post提交时必须加上下面这行代码
            outFileRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            
            // 向服务器发出一个请求
            outFileRequest.send("userNo=" + window.parent._UserNo + "&sender=" + document.getElementById("txt_UserNo").value);
        }
        
        //显示聊天记录
        function ShowOutFileCallback()
        {
            if(outFileRequest.readyState == 4)
            {
                if(outFileRequest.status == 200)
                {
                    var content = outFileRequest.responseText;
                    
                    if(content=="")
                    {
                        return;
                    }
                    
                    var msghhtt = content.split('\n');
                    
                    var userno = document.getElementById("txt_UserNo").value;

                    for (var i=0;i<msghhtt.length;i++)
                    {
                        if(msghhtt[i]=="")
                        {
                            continue;
                        }
                        
                        var tempmmssgg = msghhtt[i].split('\t');
                        
                        var msgshowhtml = GetMsgRecvHtml(tempmmssgg[0],document.getElementById("txt_userTName").value,userIcon) + "<br>";
                        
                        MsgShowDiv.innerHTML += msgshowhtml;
                        
                        //保存聊天记录
                        SaveLocalMsgList(window.parent._UserNo,userno,tempmmssgg[0], tempmmssgg[1] ,"0");
                    }
                    
                    
                    
                    //DoImageResize();
                    
                    MsgShowDiv.scrollTop = MsgShowDiv.scrollHeight;
                    
                    ResetImageSize();

//                    var tempmsg = content.split('\n');
//                    
//                    for (int i=0;i<tempmsg.length;i++)
//                    {
//                        
//                    }
                }
            }
        }
        
        //接收消息
        function RecvMsg(senderr,msg,msgdate)
        {
            var userno = document.getElementById("txt_UserNo").value;

            if(userno != senderr)//说明不是当前用户的消息
            {
                return 0;
            }
            
            msg = msg.replace(/SisenMESS_ReLine_Code/g,"<br>");
            
            var msgshowhtml = GetMsgRecvHtml(msg,document.getElementById("labUserName").innerText,userIcon) + "<br>";
                        
            MsgShowDiv.innerHTML += msgshowhtml;
            
            MsgShowDiv.scrollTop = MsgShowDiv.scrollHeight;
            
            SetUserNoReadMsgCount(window.parent._UserNo,userno,0);
            
            ResetImageSize();
            
            return 1;
        }
        
        //发送消息
        function SendMsg()
        {
            if(!window.parent._IsLogin)
            {
                alert("与服务器断开连接，无法发送消息...");
                return;
            }
            var userno = document.getElementById("txt_UserNo").value;
            var msg = document.getElementById("txt_Msg").value;
            
            if(msg=="")
            {
                return;
            }
            
            msg = msg.replace(/\r/g,"").replace(/\n/g,"<br>");

            window.parent.SendMsgToServer("Msg\n" + userno + "\n" + msg);
            
            document.getElementById("txt_Msg").value = "";
            document.getElementById("txt_Msg").style.height='22px';
            
            var myDate = new Date();
            
            var mtim = myDate.toString("yyyy-MM-dd")

            msg +="<div style='width:100%; text-align:right; color:#66CCFF;font-size: 8px; '>" + myDate.toString("HH:mm:ss") + "</div>";
                        
            SaveLocalMsgList(window.parent._UserNo,userno,msg,mtim,"1");//保存聊天记录
            
            msg = GetMsgSendHtml(msg,window.parent._MyUserIconUrl);
            
            MsgShowDiv.innerHTML += msg + "<br>";
            
            MsgShowDiv.scrollTop = MsgShowDiv.scrollHeight;

            SaveLastUserList(window.parent._UserNo,userno);//保存最近联系人
            
            ResetImageSize();
        }
        //打开表情
        function OpenFace()
        {
            FaceDiv.style.height = "288px";
            FaceDiv.style.visibility="visible";
            
            if(document.getElementById("FaceFrame").contentWindow.location.href.indexOf("DefaultFace")==-1)
            {
                document.getElementById("FaceFrame").contentWindow.location.href = "../DefaltFace/DefaultFace.aspx";
            }
        }
        //发送表情
        function SendFace(faceName)
        {
            FaceDiv.style.height = "0px";
            FaceDiv.style.visibility = "hidden";
            
            if(faceName=="")
            {
                return;
            }
            if(!window.parent._IsLogin)
            {
                alert("与服务器断开连接，无法发送消息...");
                return;
            }
            var userno = document.getElementById("txt_UserNo").value;
            var DefaltFaceCode = "SisenStudioFaceImagesReplace2008";
            var msg = "<img src='" + DefaltFaceCode + faceName + "'>";

            window.parent.SendMsgToServer("Msg\n" + userno + "\n" + msg);
            
            var myDate = new Date();
            
            var mtim = myDate.toString("yyyy-MM-dd")
            msg = msg.replace(/SisenStudioFaceImagesReplace2008/g,"../DefaltFace/");
            msg +="<div style='width:100%; text-align:right; color:#66CCFF;font-size: 8px; '>" + myDate.toString("HH:mm:ss") + "</div>";
                        
            SaveLocalMsgList(window.parent._UserNo,userno,msg,mtim,"1");//保存聊天记录

            msg = GetMsgSendHtml(msg,window.parent._MyUserIconUrl);
            
            MsgShowDiv.innerHTML += msg + "<br>";
            
            MsgShowDiv.scrollTop = MsgShowDiv.scrollHeight;

            SaveLastUserList(window.parent._UserNo,userno);//保存最近联系人
            
            ResetImageSize();
        }
        
        function ShowOfflineFile(sender,id,isfolder)
        {
            window.location.href = "../Documents/ShowOfflineFile.aspx?UserNo=" + window.parent._UserNo+"&Sender="+sender+"&Id="+id+"&IsFolder="+isfolder+"&UserTName="+document.getElementById("labUserName").innerText.replace(/&/g,"＆").replace(/=/g,"＝");
        }
        //重置图片大小
        function ResetImageSize()
        {
            var screenwidth = document.documentElement.clientWidth * 0.74;
            
            var imgs = MsgShowDiv.getElementsByTagName("img");
            for(var i=0, len=imgs.length; i < len; i++)
            {
                if(imgs[i].width>screenwidth)
                {
                    var xcall1 = imgs[i].width/screenwidth;
                    
                    imgs[i].width = imgs[i].width/xcall1;
                    imgs[i].height = imgs[i].height/xcall1;
                    
                }
                //alert( imgs[i].src);
            }
        }
        //打开文件发送板
        function OpenSendFilePannel()
        {
            SendFilePanl.style.visibility="visible";
        }
        //关闭文件发送板
        function CloseSendFilePannel()
        {
            SendFilePanl.style.visibility="hidden";
        }
        
        //文件发送完成后，回调
        function SendFileFinish(id,filename)
        {
            window.parent.SendMsgToServer("File\n"+document.getElementById("txt_UserNo").value+"\n"+id+"\n"+filename);
        }
        
    </script>
    <link href="../Css/MsgList.css" rel="stylesheet" />
</head>
<body onload="PageLoadFun();" style="background-color:#f4f8f9;overflow:no;">
    <form id="form1" runat="server">
    <div id = "MainDiv" style ="position:fixed;overflow:no; left:0px; top:0px;width:100%;height:100%;" >
    <table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td style ="height:36px;background-color:#17b4eb;">
            <table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td style ="width:36px; text-align:center;"><img alt="" src="../Images/back.png" width="24" height="24" onclick="BackPage();" /></td>
                    <td style ="text-align:right;"><span id="labUserName" style="color:#FFFFFF; font-size:16px; font-weight:900;"></span><input id="txt_UserNo" runat="server" readonly="readonly" style="width: 0px; display:none" type="text" /><input id="txt_userTName" runat="server" readonly="readonly" style="width: 0px; display:none" type="text" /><input id="MyUserNo" runat="server" readonly="readonly" style="width: 0px; display:none" type="text" /></td>
                    <td style ="width:20px; text-align:center;"></td>
                </tr>
            </table>
        </td>
      </tr>
      <tr>
        <td><div id="MsgShowDiv" style="overflow:auto; width:100%; height:100%; background-color:#FFFFFF; z-index:0"></div></td>
      </tr>
      <tr>
        <td style="height:40px;background-color:#f3f3f3;">
        </td>
      </tr>
    </table> 
    </div>
    <div id="FootBar" style="position:absolute;left:0px;bottom:0;top:expression(document.documentElement.clientHeight + document.documentElement.scrollTop - this.offsetHeight);background-color:#f3f3f3;height:40px;width:100%;font-family:Tahoma; font-size:12px; border-top:1px solid #cccccc;z-index:2;">
            <table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td style ="width:10px;"></td>
                    <td style ="width:24px;"><img alt="表情" id ="btn_Face" src="../Images/face.gif" width="24" height="24" onclick="OpenFace();" /></td>
                    <td style ="width:10px;"></td>
                    <td style ="width:34px;"><img alt="文件" id ="btn_SendFile" src="../Images/jia.png" width="24" height="24" onclick="OpenSendFilePannel();" /></td>
                    
                    <td><textarea id="txt_Msg" style="cursor:text; font-size:13px; color:#003366;width:95%;line-height:20px;height:22px;font-family:Arial;cursor:text;*overflow-y:hidden; " onfocus="this.style.height='0px';this.style.height=(this.scrollHeight+'px');" oninput="this.style.height='0px';this.style.height=(this.scrollHeight+'px');"></textarea></td>
                    <td style="width:40px;"><img alt="发送" id ="btn_SendMsg" src="../Images/send.gif" width="56" height="30" onclick="SendMsg();" /></td>
                    <td style="width:10px;"></td>
                </tr>
            </table>
    </div>
    <div id="FaceDiv" style="position:fixed;left:0px;bottom:0;top:expression(document.documentElement.clientHeight + document.documentElement.scrollTop - this.offsetHeight);background-color:#f3f3f3;height:288px;width:100%;font-family:Tahoma; font-size:12px; border-top:1px solid #cccccc;z-index:2;visibility:hidden;">
        <iframe id="FaceFrame" border="0" marginwidth="0" marginheight="0"
                src="" frameborder="no" style="width:100%;height:100%;"
                scrolling="auto" name="FaceFrame"></iframe>
    </div>
    <div id="SendFilePanl" style="position:fixed;left:0px;bottom:0;top:expression(document.documentElement.clientHeight + document.documentElement.scrollTop - this.offsetHeight);background-color:#f3f3f3;height:58px;width:100%;font-family:Tahoma; font-size:12px; border-top:1px solid #cccccc;z-index:2;visibility:hidden;">
    <div style="text-align:center; height:20px;"><img alt="关闭文件发送" id ="Img1" src="../Images/CloseFace.gif" width="56" height="18" onclick="CloseSendFilePannel();" /></div>
    
    <asp:FileUpload ID="SendFileObj" runat="server" />
    <asp:Button ID="btn_UploadFilePro" runat="server" Width = "80" Height = "32" OnClick="btn_UploadFilePro_Click" Text="发送" />
    </div>
    </form>
</body>
</html>
