﻿var _ere ="";
var ere = "";
var canSubmit = "0";
var maxtime = 0 ;
ChatRoom = new function()
{
    this._serviceUrl = "../handler.ashx";
    this._getMessagesInterval = 1000; // 每2秒从服务器获取一次消息
    this.bind = function(instance, method)
	{
		return function()
		{
			method.apply(instance, arguments);
		}
	}
	
	this.getMessages = function()//获取用户列表
	{
	    var UserID = document.getElementById('txtUserID').value;
        var UsID = document.getElementById('txtLawyerID').value;
        var ID = document.getElementById('txtnum').value;
        Request.sendPOST(this._serviceUrl, "action=get&ID="+ID+"&UserID="+UserID+"&Lawyer="+UsID,this.bind(this, this._getMessagesCallback), null, null); 
	}
	
	this._getMessagesCallback = function(req)//获取消息列表的回调函数 
	{
	    var ele = $("ChatContent");
		ele.scrollTop = ele.scrollHeight;
		document.getElementById("ChatContent").innerHTML= req.responseText;
		setTimeout(this.bind(this, this.getMessages),this._getMessagesInterval);
	}
	
	this.StateMessages = function()
	{
        Request.sendPOST(this._serviceUrl, "action=state",this.bind(this, this._StateMessagesCallback), null, null); 
	}
	
	this._StateMessagesCallback = function(req)//获取消息列表的回调函数
    {
        _ere=req.responseText;

        if(_ere!=0)
        {
            //dialog.opendlg( '新的在线咨询', '', 400, 300);
            window.open ('http://chat.960law.com/Chat.aspx?ID='+_ere, '_blank', 'width=690,height=490,toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no') ;
            setTimeout(this.bind(this, this.StateMessages),this._getMessagesInterval);
        }
        else
        {
		    setTimeout(this.bind(this, this.StateMessages),this._getMessagesInterval);
	    }
	}

    this.sendMessage = function()//发送聊天消息
	{
	    if(canSubmit == "0")
	    {
        var message = document.getElementById('UserInputTextBox').value;//消息
        var UserID = document.getElementById('txtUserID').value;
        var UsID = document.getElementById('txtLawyerID').value;
        var Type = document.getElementById('txtType').value;
        var num = document.getElementById('txtnum').value;
        var username = document.getElementById('txtusername').value;
         if (message=="")
	    {
	        alert("不能发空信息！");
	        return false;
	    }
		document.getElementById('UserInputTextBox').value = "";//清空输入框
		var Msg = encodeURI(message);
		var params="msg="+Msg+"&UserID="+UserID+"&Lawyer="+UsID+"&type="+Type+"&ID="+num+"&username="+username;
        Request.sendPOST(this._serviceUrl ,"action=send&"+params, null, null, null); //发送消息
        }
		else if(canSubmit == "1")
		{
		    alert("对方还没有接通，请等待！");
		}	
		else if(canSubmit == "3")
		{
		    alert("对方拒绝了你的再线咨询！\n请关闭页面重新咨询或者咨询其他在线律师！");
		}	
		else if(canSubmit == "4")
		{
		    alert("对方停止了你的咨询！\n请关闭页面重新咨询或者咨询其他在线律师！");
		}	
		else if(canSubmit == "5")
		{
		    alert("咨询人已经停止了对你的咨询！");
		}	
		else if(canSubmit == "01")
		{
		    alert("对方已经离线！");
		}	
		else if(canSubmit == "02")
		{
		    alert("对方离线或是对方已经离开，本次咨询失败！");
		}	
	}

    this.inputBox_KeyPress = function(e)//输入框响应回车操作
	{
		    if (e.ctrlKey &&  e.keyCode == 13)//回车
		    {
			    this.sendMessage();
			    try
			    {
				    e.preventDefault();
			    }
			    catch(e){}
			    return false;
		    }
		    return true;
	}
    
    this.Agree= function()
    {
        var ID = document.getElementById('txtnum').value;
        Request.sendPOST(this._serviceUrl ,"action=yes&ere="+ID, null, null, null);
    }

    this.NoAgree= function()
    {
        var ID = document.getElementById('txtnum').value;
        Request.sendPOST(this._serviceUrl ,"action=no&ere="+ID, null, null, null);
    }
    
    this.Application=function()
    {
        var UserID = document.getElementById('txtUserID').value;
        var LawyerID = document.getElementById('txtLawyerID').value;
        
        Request.sendPOST(this._serviceUrl ,"action=app&user="+UserID+"&lawyer="+LawyerID, this.bind(this, this._ApplicationCallback), null, null); 
    }
    
    this._ApplicationCallback = function(req)
    {
        
        if(maxtime == 10)
        {
               canSubmit = "02";
               document.getElementById("tip_content").innerHTML = "<font color=\"red\">&nbsp;&nbsp;<img src='images/chat/cuo.png' /> 对方离线或是对方已经离开，本次咨询失败！<font><br>";
        }
        else
        {
         
          var i=0;
          var app =req.responseText;
          if(app=="0")
          {
               maxtime+=1;
              canSubmit = "1";
              document.getElementById('tip_content').innerHTML = "<font color=\"#FFCC00\">&nbsp;&nbsp;<img src='images/chat/deng.png' /> 咨询申请中......请耐心等待！<font><br>";
              setTimeout(this.bind(this, this.Application),this._getMessagesInterval);
          }
          
          if(app=="1")
          {
               canSubmit = "1";
               document.getElementById("tip_content").innerHTML = "<font color=\"#FFCC00\">&nbsp;&nbsp;<img src='images/chat/deng.png' /> 咨询申请中......请耐心等待！<font><br>";
               
               setTimeout(this.bind(this, this.Application),this._getMessagesInterval);
          }
          
          if(app=="2")
          {
               canSubmit = "0";
               document.getElementById("tip_content").innerHTML = "<font color=\"#00CC00\">&nbsp;&nbsp;<img src='images/chat/dui.png' /> 申请通过你现在可以对律师进行咨询了！<font><br>";
               setTimeout(this.bind(this, this.Application),this._getMessagesInterval);
          }
          
          if(app=="3")
          {
               canSubmit = "3";
               document.getElementById("tip_content").innerHTML = "<font color=\"red\">&nbsp;&nbsp;<img src='images/chat/cuo.png' /> 对方拒绝了你的再线咨询！<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请关闭页面重新咨询或者咨询其他在线律师！<font><br>";
               
               setTimeout(this.bind(this, this.Application),this._getMessagesInterval);
          }
          
          if(app=="4")
          {
               canSubmit = "4";
               document.getElementById("tip_content").innerHTML = "<font color=\"red\">&nbsp;&nbsp;<img src='images/chat/cuo.png' /> 对方停止了你的咨询！<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请关闭页面重新咨询或者咨询其他在线律师！<font><br>";
          }
          
           if(app=="01")
          {
               canSubmit = "01";
               document.getElementById("tip_content").innerHTML = "<font color=\"red\">&nbsp;&nbsp;<img src='images/chat/cuo.png' /> 对方已经离线！请关闭页面重新咨询或者咨询其他在线律师！<font><br>";
          }
         }
    }
    
    
    this.Applicalawyer=function()
    {
        var UserID = document.getElementById('txtUserID').value;
        var LawyerID = document.getElementById('txtLawyerID').value;
        
        Request.sendPOST(this._serviceUrl ,"action=applica&user="+UserID+"&lawyer="+LawyerID, this.bind(this, this._ApplicaCallbacklawyer), null, null); 
    }
    
     this._ApplicaCallbacklawyer = function(req)
    {
          var i=0;
          var app =req.responseText;
          
          if(app=="0")
          {
               canSubmit = "0";
               setTimeout(this.bind(this, this.Applicalawyer),this._getMessagesInterval);
          }
          else
          {
               canSubmit = "5";
               document.getElementById("tip_content").innerHTML = "<font color=\"red\">&nbsp;&nbsp;<img src='images/chat/cuo.png' /> 咨询人已经停止了对你的咨询！<font><br>";
          }
    }
    
    this.Judge=function()
    {
        var Type = document.getElementById('txtType').value;
        if(Type=="1")
        {
            setTimeout(this.bind(this, this.getMessages),this._getMessagesInterval);
            setTimeout(this.bind(this, this.Applicalawyer),this._getMessagesInterval);
        }
        else
        {
            setTimeout(this.bind(this, this.getMessages),this._getMessagesInterval);
            setTimeout(this.bind(this, this.Application),this._getMessagesInterval);
        }
    }
    
    
    this.Deleted=function()
    {
        var num = document.getElementById('txtnum').value;
        Request.sendPOST(this._serviceUrl ,"action=delete&id="+num, null, null, null);
    }
}
    
