﻿// JScript 文件

var chatWindow;

function sendMessage(userId)
{
    //最后次检查该用户时候在线。
	var sReturn;
	var dataToSend = "userId=" + userId;
	
	//验证cookie。
	if (document.cookie.indexOf("__WEBIM_GUEST_FORCEOUT=True") > 0)
	{
	    alert(varMsg);
	    return;
	}
	
	var xmlhttp=createXMLHttpRequest();
	if(xmlhttp==null)
	{
		alert(varAlert2);
		return;
	}
	xmlhttp.open("POST", getuserisonlinePageUrl,true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{ 
			sReturn = xmlhttp.responseText;
			
            //-----------------------------------------------------------------------------
            //测试环境。
            //chatWindow = window.open(varIMGuestChat + "?AdminID=" + escape(userId),null, 'width=435,height=305,top='+(screen.availHeight-305)/2+',left='+(screen.availWidth-435)/2+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=1');
            //return;
            //-----------------------------------------------------------------------------
			
			if (sReturn == "true")
			{
							
				var cookieValue = document.cookie;
				
				if (cookieValue.indexOf("__IMGuestChatState") == -1)
				{
				    alert("本页面必须开启cookie，请开启cookie后刷新本页面重试！");
				    return;
				}
				
				if (cookieValue.indexOf("__IMGuestChatState=false") != -1)
				{
				    alert("请先关闭已经开启的会话窗口！");
				    return;
				}
				
				if (cookieValue.indexOf("__IMGuestChatState=true") != -1)
				{
				    //开始聊天。
				    chatWindow = window.open(varIMGuestChat + "?AdminID=" + escape(userId),null, 'width=435,height=305,top='+(screen.availHeight-305)/2+',left='+(screen.availWidth-435)/2+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=1');
	            }
			}
			else
			{
				if (confirm(varConfirm))
				{
					window.open(varIMUserLeaveWord + "?UserId=" + userId);
				}
				else
				{
					//window.location = "IMGuestStart.aspx";
				}
				
				ChangeServicerState(userId);
			}
		}
	}
	try
	{
		xmlhttp.send(dataToSend);
	}
	catch(e)
	{
		alert(varAlert5);
		return;
	}
}

//更新客服的状态。
function ChangeServicerState(userId)
{

    var objdiv = document.getElementById(userId);
    objdiv.childNodes[0].className = "UserIconStyle1";
    objdiv.childNodes[2].onclick = "";
    objdiv.childNodes[2].href = varIMUserLeaveWord + "?UserId=" + userId;
    objdiv.childNodes[2].target = "_blank";
    
    //更新数据库。
    var xmlhttp=createXMLHttpRequest();
    var dataToSend = "IsAsync=true";
    
	if(xmlhttp==null)
	{
		alert(varAlert2);
		return;
	}
	
	xmlhttp.open("POST", varupdatedatabase,false);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	try
	{
		xmlhttp.send(dataToSend);
	}
	catch(e)
	{
		alert(varAlert5);
		return;
	}
}

function Minimize()
{
    var tr = document.getElementById(servicerListTr);
    
    if ("none" == tr.style.display)
    {
        tr.style.display = "block";
    }
    else
    {
        tr.style.display = "none";
    }
}

function WindowClose()
{
    var control = document.getElementById(servicerListPlan);
    control.style.display = "none";
}
