﻿// JScript 文件
function StatusManager(id,gpid,queueid,queuePos,Version){
    this.staffId = id;
    this.gpId    = gpid;
    this.showQueueDiv = true;
    this.queueId = queueid;
    this.queueNO = queuePos;
    this.queueLength = 0;
    this.ClientType=null;
    this.version = Version;
    this.ud = ""; 
    var STATUS_OUT_TEXT			= "不在线.." ;
	var STATUS_DOWN_TEXT		= "正在登录.." ;
	var STATUS_READY_TEXT		= "请求服务";
	var STATUS_READY_QUEUENOTEMPTY_TEXT		= "响应排队";
	var STATUS_PENDING_TEXT		= "等待服务";
	var STATUS_BUSY_TEXT		= "忙碌...";
	var STATUS_STOP_TEXT		= "退出登录..";
	var STATUS_REBOOT_TEXT		= "客户重启";
	var timerId1 = null;
	var timerId2_QueueInfo = null;
	var self = this;
	var divOuterId = "divOuter";
	var divQueueId = "divQueue";
	this.spanQueueLengthId1 = "spanLength1";
	this.spanQueueLengthId2 = "spanLength2";
	this.divQueueInfo_Offline = "divOffline";	
	this.divQueueInfo1 = "divBeforeQueue";  //TODO:
	this.divQueueInfo2 = "divEnterQueue";
	this.WaitInfo = "WaitNumber";
	var linkJoinQueueid = "linkJoinQueue";
	var loginResult = null;
	var spId = null;
	var divError = "divError";
	var serviceid= null;
	this.start = function()
	{
	    if (window.attachEvent)
	    {
	        window.attachEvent("onload",this.onloadHandler);
	    }
	    else if (window.addEventListener)
	    {       
            window.addEventListener('load', this.onloadHandler, false); 
         }  
         else
         {
            window.onload = this.onloadHandler;
         }
	    //window.onbeforeunload = this.beforeUnloadHandler;
	    //window.attachEvent("onunload",this.unloadHandler);
	}
	
    String.prototype.Trim = function()
    {
        return this.replace(/(^s*)|(s*$)/g, "");
    }

	this.onloadHandler = function()
	{
		var interval = self.getRefreshInterval();
		self.CheckSupporterStatus();		
		timerId1 = window.setInterval(self.CheckSupporterStatus,interval * 1000);
		if(self.queueId != null && self.queueId != "")
		{
		    timerId2_QueueInfo = window.setInterval(self.checkQueueStatus,2000);
		    if($(linkJoinQueueid) != null)
		        $(linkJoinQueueid).style.display = "none";
		}
		else
		{
		    if($(linkJoinQueueid) != null)
		        $(linkJoinQueueid).style.display = "inline";
		}
	}
	
	this.beforeUnloadHandler = function()
	{
	    if(self.queueId != null && self.queueId != "")
	    {
	        return GlobalString.RefreshStatus_CloseWindow;
	        
	    }
	}
	
	this.unloadHandler = function()
	{
	    if(self.queueId != null && self.queueId != "")
	    {
	        Service_Index.ExitQueue(self.queueId);
	    }
	}
	
	this.getRefreshInterval = function()
    {	
	    var interval = 10;
	    var frm = document.getElementById("aspnetForm");	   
	    if(frm.attributes["RefreshInterval"])
	    {	    
		    var attr = frm.attributes["RefreshInterval"];		    
		    if(!isNaN(attr.value))
		    {
		         if(attr.value!=0)
		         {
		         interval = parseInt(attr.value);
		         }
		    }
	    }	  
	    return interval;
    }
    
    this.checkQueueStatus = function()
	{
	    Service_Index.GetQueueInfo(self.queueId, self.checkQueueStatus_callback);
	}
	
	this.checkQueueStatus_callback = function(res) 
	{
		if(res.error!=null || res.value == null)
            return;

        var qu = res.value;
        if(qu.State == 1) return;   //等候服务

        var tempQueueId = self.queueId; 
        self.queueId = "";
        window.clearInterval(timerId2_QueueInfo);
        if(qu.State == 2)
        {
            AjaxMethods.QueuedUserStartService(tempQueueId, self.requestService_callback);
        }
        else if (qu.State == 4)
        {
            // Rejected
            if($(linkJoinQueueid) != null)
                $(linkJoinQueueid).style.display = "inline";
	        AjaxMethods.GetSpCurrentStatus(self.staffId, self.gpId, self.queueNO, self.send_callback);
            alert(GlobalString.RefreshStatus_NotSupporterAccept);            
        }
 	} 
 	
    this.GetUserInfo=function(euId)
    {
        Service_Index.ShowEuInfo(euid, self.GetUserInfo_callback);
    }
    
    this.GetUserInfo_callback=function(res)
    {
    	if(res.error!=null)
        {
            //alert(res.error);
            if($(divError) != null)
                $(divError).style.display="block";
            return;
        }
        if($(divError) != null)
            $(divError).style.display="none";
        
        if(res.value == null) return;
        alert(res.value.name);
    }
    
    this.LocalSendMail=function(mailid)
    {
   
        if($("txtBody").value.Trim()=='')
        {
            $('lblSending').style.display="none";
            alert(GlobalString.RefreshStatus_NotText);
            return;
        }

        AjaxMethods.SendMailEx (mailid,$("txtBody").value    
            , $("txtContact").value, $("txtTelephone").value
            , $("txtEMail").value, $("txtCompany").value
            , self.SendMail_callback);
    }
    
    this.SendMail_callback=function(res)
    {
    
        $('lblSending').style.display="none";
    	if(res.error!=null)
        {
            //alert(res.error);
            alert(res.error);
            return;
        }
             
   
        if(res.value =="")
        {
           alert(GlobalString.RefreshStatus_SendSuccess);
           $("divInfoLayer").style.display="none";
           return;
        }
       
        alert(res.value);
    }
   
    this.SendMessage=function()
    {
        Service_Index.SendMessage($("UserName").value+"|"+
        $("CompanyName").value+"|"+
        $("MailAddress").value+"|"+
        $("ContactPerson").value+"|"+
        $("ContactPhone").value+"|"+
        $("CompanyPage").value+"|"+
        $("MessageText").value
       
        , self.SendMessage_callback);
    }
    
    this.SendMessage_callback=function(res)
    {
    	if(res.error!=null)
        {
            //alert(res.error);
            $(divError).style.display="block";
            return;
        }
        $(divError).style.display="none";
       
        if(res.value =="")
        {
           alert(GlobalString.RefreshStatus_SendSuccess);
           $("divInfoLayer").style.display="none";
           return;
        }
        alert(res.value);
    }
    this.CheckSupporterStatus = function()
	{
	    self.showQueueDiv = true;	    
	    AjaxMethods.GetSpCurrentStatus(self.staffId, self.gpId, self.queueNO, self.send_callback);
	}
	
	this.send_callback = function(res) 
	{
		 if(res.error!=null)
        {
            //alert(res.error);
            $(divError).style.display="block";
            return;
        }
        $(divError).style.display="none";
        var response = res.value;
        if(response == null) return;
        if(response.Supporters == null) return;
        
        var ServiceMode = response.ServiceMode;
	    if(ServiceMode==2)
	    {
	       window.location="CustomerLogin.aspx?GpId=&ClientType=" + self.ClientType + "&Version=" + self.version + "&id="+id;
	       return;
	    }
	    else if(ServiceMode==3)
	    {
	        window.location="AutoLogin.aspx?StaffId=" + id + "&ClientType=" + self.ClientType + "&Version=" + self.version; 
	        return;
	    }
	    
        var nodes = $A(response.Supporters);        
        self.queueLength = response.QueueLength;
        nodes.each
        (
            function(node)
            {
                var spTable = document.getElementById("T" + node.Id);
                if(spTable != null)
                    self.SetSpInfo(node.WebStatus, spTable, node.Id, response, node.AssignNum, node.ServiceNumber, node.SpServiceNumber, node.Version);
                //else
				    //window.location = "Index.aspx?GroupID=" + self.gpId + "&Version=" + self.version; 
				    
            }
        );
        
        //alert($(divQueueId).outerHTML);
        if($(divError) != null)
            $(divQueueId).style.display = (self.showQueueDiv?"block":"none");
        var inQueue = (self.queueId != null && self.queueId != "");
        if($(self.spanQueueLengthId2) != null)
            $(self.spanQueueLengthId2).innerHTML=res.value.NoofBeforeMe;
        if($(self.spanQueueLengthId1) != null)
            $(self.spanQueueLengthId1).innerHTML=res.value.QueueLength;
        if($(self.divQueueInfo2) != null)
            $(self.divQueueInfo2).style.display = (inQueue?"block":"none");
        if($(self.divQueueInfo_Offline) != null)
            $(self.divQueueInfo_Offline).style.display = (!inQueue&&res.value.AllOffline?"block":"none");
        
        if($(self.WaitInfo)!=null)
        {
            var varClientType = self.ClientType;
            if(self.ClientType.toLowerCase() != "installableclient")
            {
                self.ClientType = "";
                varClientType = "iscompact";
            }
            var strClick = "'AutoQueuing.aspx?Action=JoinQueue&StaffId=" + self.staffId
                    + "&GpID=" + self.gpId + "&ClientType="+varClientType+"&EuId=&Version=" + self.version + "'";
                if(self.ClientType.toLowerCase() == "installableclient")
                    strClick = "window.location= " + strClick + ";return false;";
                else {
                    strClick = "window.open(" + strClick + 
                        ",'','width=593,height=423,toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');return false";
                }
                tdJoinQueue.innerHTML="<A href='Javascript:void(0);' onclick=\"" + strClick 
                    + "\" onMouseOver=\"MM_swapImage('queue','','"+GlobalString.Index_supportcenterqueueicon1+"',1)\" onMouseOut=\"MM_swapImgRestore()\">"
                    + "<img src='"+GlobalString.Index_supportcenterqueueicon1+"' name='queue' height='18' border='0' id='queue'></A>";
        }
        
        // before queue
        if($(self.divQueueInfo1) != null)
            $(self.divQueueInfo1).style.display = (!inQueue&&!res.value.AllOffline&&!res.value.ReadySupporterCount?"block":"none");
        if(res.value.ReadySupporterCount == 0 && res.value.AllOffline)
        {
            $(self.divQueueInfo1).style.display="none";
            $(self.divQueueInfo_Offline).style.display="block";
        }
        if($(divOuterId) != null && $(divOuterId).offsetTop>0) $(divOuterId).style.marginTop = $(divOuterId).offsetTop*(-1); //--to correct error 0286
	} 
	
	this.SetSpInfo = function(newState, spTable, spId, value, AssignNum, ServiceNumber, SpServiceNumber, Version)
	{
	    var id = value.ServiceId;
		var tdStatus = spTable.rows[1].cells[0];
		switch(newState)
		{
			case 'Z':
				//btnStatus.value = STATUS_DOWN_TEXT
				tdStatus.innerHTML="<img src='"+GlobalString.Index_supportcenterblogin+"'>";
				break;
		    case 'B':	//'B'
				//btnStatus.value = STATUS_BUSY_TEXT
				tdStatus.innerHTML="<img src='"+GlobalString.Index_supportcenterbbusy+"' border='0'>";
				if ((SpServiceNumber != 0 && ServiceNumber >= SpServiceNumber) || (Version == 0)|| (Version == 4)) //同时接起的客户达到阈值才显示为忙碌，否则继续执行'A'段。
				    break;
			case 'A':	//'A'
				//btnStatus.value = STATUS_READY_TEXT
				if(AssignNum>0)
				    tdStatus.innerHTML="<img src='"+GlobalString.Index_supportcenterbqueue+"' border='0'>";
				else
				{   
                    var visitorId = new Date().getTime()*10+Math.round(Math.random()*9)+1;
			        var strClick = "'PrepareWebChat.aspx?StaffId=" + self.staffId + "&GroupID="+self.gpId+ "&QueueId=" + self.queueId + "&QueueNo=" + self.queueNO +"&ClientType=" + self.ClientType + "&spanQueueLengthId1=" 
			        + self.spanQueueLengthId1 + "&spanQueueLengthId2=" + self.spanQueueLengthId2 + "&divQueueInfo_Offline=" +  self.divQueueInfo_Offline + "&divQueueInfo1=" + self.divQueueInfo1 + "&divQueueInfo2=" +  self.divQueueInfo2 
			        + "&Version=" + self.version + "&EuId=&SpId=" + spId + "'";
			        if(self.ClientType.toLowerCase() == "installableclient")
			            strClick = "window.location= " + strClick + ";return false;";
			        else
			            strClick = "window.open(" + strClick + ",'" + visitorId + "','width=593,height=423,toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');";
			        if (spId == "Robot")
			            tdStatus.innerHTML="<A href='Javascript:void(0);' onclick=\"" + strClick + ";\" onMouseOver=\"MM_swapImage('St_"+ spId +"','','"+GlobalString.Index_supportcenterbrequest2+"',1)\" onMouseOut=\"MM_swapImgRestore()\"><img src='"+GlobalString.Index_supportcenterbrequest1+"' border='0' id=\"St_" + spId + "\"></A>";//"<A href='Javascript:void(0);' onclick=\"" + strClick + "\"><img src='../images/fw.gif' width='61' height='23' border='0'></A>";
			        else
			            tdStatus.innerHTML="<A href='Javascript:void(0);' onclick=\"if(this.disabled != true){" + strClick + ";}else{return false;};this.disabled = true;\" onMouseOver=\"MM_swapImage('St_"+ spId +"','','"+GlobalString.Index_supportcenterbrequest2+"',1)\" onMouseOut=\"MM_swapImgRestore()\"><img src='"+GlobalString.Index_supportcenterbrequest1+"' border='0' id=\"St_" + spId + "\"></A>";//"<A href='Javascript:void(0);' onclick=\"" + strClick + "\"><img src='../images/fw.gif' width='61' height='23' border='0'></A>";
				}
				self.showQueueDiv = (self.queueLength>0);		
				break;
			case 'C':
				tdStatus.innerHTML="<img src='"+GlobalString.Index_supportcenteraway+"'>";
				break;
			case "D":	//'D'
			default:
				//btnStatus.value = STATUS_OUT_TEXT	
				tdStatus.innerHTML = "<img src='"+GlobalString.Index_supportcenterboffline1+"'>";
				//if(self.ClientType=="installableclient")
                //    tdStatus.innerHTML = "<img src='../images/supportcenterboffline1.jpg' width='68' height='19' border='0'>";
                //else
                //    tdStatus.innerHTML="<A href='javascript:spLogin(\"" + spId + "\")' onMouseOver=\"MM_swapImage('St_" + spId + "','','../Images/supportcenterboffline2.jpg',1)\" onMouseOut=\"MM_swapImgRestore()\"><img src='../images/supportcenterboffline1.jpg' width='68' height='19' border='0' id=\"St_" + spId + "\"></A>";
				//tdStatus.innerHTML="<A href='SpLogin.aspx?SpId=" + spId + "'><img src='../images/zx.gif' width='61' height='23' border='0'></A>";
				break;
		}
		
	}
	
	this.requestService = function(spId)
	{
	    AjaxMethods.RequestService(spId, "", "",self.version,self.queueId, self.requestService_callback);
	} 
	
	this.requestService_callback = function(res)
	{
	    if(res.error!=null || res.value == null)
        {
            alert(GlobalString.Index_Send_Raise_Error);
            return;
        }
        
        var response = res.value;
        if (response.ResponseType == 1)
        {
            window.location = response.LoginUrl 
                + "&GpId=" + self.gpId
                + "&ClientType=" + self.ClientType;
        }
        else if (response.ResponseType == 2)
        {
             window.location = "WebChat.aspx?SpId=" + response.SpId + "&ECharacterId=" + response.ECharacterId + "&Version=" + self.version + "&ClientType=" + self.ClientType,"",""; 
        }
        else if (response.ResponseType == 3)
        {
            window.location = "FlyForceDown.aspx?SpId=" + response.SpId +"&EuId=0&ECharacterId=" + response.ECharacterId + "&Version=" + self.version + "&ClientType=" + self.ClientType;
        }
        else if (response.ResponseType == 4)
        {
            alert(GlobalString.Index_NO_Authorise);        
        }
        else 
        {
            alert(GlobalString.Index_Send_Fail);        
        }
	}
	
    this.ShowLayer=function(divShow,isShow,W,H,offsetX,offsetY)
        {
            
	        var divLayer = document.getElementById(divShow);
	        if(divLayer == null) return;
	        if(!isShow)
	        {
		        divLayer.style.display="none"
		        return;
	        }

        	
	        if(W==""||H=="")
	        {
	            if(ie)
	            {
		            W = document.body.offsetWidth-offsetX*2;
		            H = document.body.offsetHeight-offsetY*2;
	            }
	            else
	            {
		            W = window.innerWidth-offsetX*2;
		            H = window.innerHeigth-offsetY*2;
	            }
	        }
	        
	        divLayer.width=W;
	        divLayer.height=H;
        	
	        divLayer.style.display = "block";
	        divLayer.style.zIndex = 100;
	        var  iTop=0;
            var  iLeft=(document.body.offsetWidth-W)/2;
            var  iTop=(document.body.offsetHeight-H)/2;
	        
	        divLayer.style.top = iTop;
	        divLayer.style.left =iLeft;

	        //evt.cancelBubble = true;
        }
}	

