//打印客服人员列表
var xmlHttp;
var date;
var count = 100;
var caller;
var divstr = "<div id=invitediv style='POSITION:absolute;width:400px; height:149px; top: 50%; left: 50%; margin-left: -200px; margin-top: -75px; display:none'>" +
    "<table width=400 height=149 border=0 cellpadding=0 cellspacing=0 BACKGROUND=onlinechat/invitebg.jpg>" +
    "	<tr>" +
    "		<td width=150 height=30>&nbsp;</td>" +
    "		<td width=210>&nbsp;</td>" +
    "		<td width=40><img src=onlinechat/c1.gif onclick='donotchat()' style='cursor: pointer'></td>" +
    "	</tr>" +
    "	<tr>" +
    "		<td height=80>&nbsp;</td>" +
    "		<td>金流客服邀请与您通话<br><br>现在通话吗？</td>" +
    "		<td>&nbsp;</td>" +
    "	</tr>" +
    "	<tr>" +
    "		<td height=39>&nbsp;</td>" +
    "		<td align=right><img src=onlinechat/i1.gif style='cursor: pointer' onclick='calltoUser()'>&nbsp;&nbsp;&nbsp;&nbsp;<img src=onlinechat/i2.gif onclick='donotchat()' style='cursor: pointer'></td>" +
    "		<td>&nbsp;</td>" +
    "	</tr>" +
"</table>" +
"</div>";
document.write(divstr);

var Browser = {};
Browser.urlInfo = function(url)
{
    if(/^(https?|ftp|file):\/\/([^:\/]+)(?::(\d*))?\/?(?:(.*)\/)?([^#?]*)\#?([^?]*)(?:\?(.*))?$/.test(url))
    {
        this.protocol = RegExp.$1;
        this.hostName = RegExp.$2;
        this.port = RegExp.$3;
        this.path = RegExp.$4;
        this.fileName = RegExp.$5;
        this.hash = RegExp.$6;
        this.param = RegExp.$7;
        this.rootUrl = [this.protocol,"://",this.hostName,(!/^\s*$/.test(this.port))?":"+this.port:""].join('');
        this.baseUrl = [this.rootUrl,this.path].join('/');
        this._data = null;
    }
    else
        return null;
}

/**
 * 客服图标
 */
 divstr = "<div id=servicediv style='left:5px;TOP:100px;POSITION:absolute; width: 100px; height: 179px'><img src='onlinechat/service-on.gif' onclick='calltoUser()' style='cursor:pointer'></div>";
 document.write(divstr);

function createXmlHttp()
{
    date = new Date();
    if(xmlHttp == null)
    {
        //XMLHTTP
        try{xmlHttp = new XMLHttpRequest();} catch(e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
}

function getServiceUser()
{
   createXmlHttp();
   //请求数据
   xmlHttp.open("post", "updateUserAction.do?act=GET_SERVICE_USER&d="+date.getTime(), false);
   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.send();
   var xml = xmlHttp.responseText;
   xmlHttp.abort();
   //
}
//求出访问来源
function getReferer()
{
    var aa = new Browser.urlInfo(document.referrer);
    return aa.baseUrl ;
}
//刷新在线时间
function refreshOnlineTime()
{
    createXmlHttp();

    //请求数据
    xmlHttp.open("get", "updateUserAction.do?act=REFRESHONLINETIME&callPage="+encodeURI(document.title)+
      "&comeFrom="+getReferer()+"&d="+date.getTime(), false);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send();
    xmlHttp.abort();
}
//刷新是否有新消息
function refreshMessage()
{
    createXmlHttp();
    xmlHttp.open("get", "chatAction.do?act=REFRESH_MESSAGE&d="+date.getTime(), false);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send();
    var xml = xmlHttp.responseText;
    xmlHttp.abort();
    senders = xml.split(",");
    var b = false;
    for(var i=0;i<senders.length;++i)
    {
        temp = Jtrim(senders[i]);
        try{
            if(temp!=null && temp != ""){                //popChat(senders[i]);
                b = true;
                if(isDigit(temp) && parseFloat(temp)>0){
                    caller = temp;
                    showInvite(true, temp);
                }
                else{
                    popChat(temp)
                }
            }
        }catch(e){}
    }
    if(!b){showInvite(false);}
    if(++count >= 18)
    {
        refreshOnlineTime();
        count = 0;
    }
    window.setTimeout("refreshMessage()", 10000);
}
//求出在线访客
function getOnlineCaller()
{
    createXmlHttp();
    xmlHttp.open("get", "chatAction.do?act=GET_ONLINE_CALLER&d="+date.getTime(), false);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send();
    var xml = xmlHttp.responseText;
    xmlHttp.abort();

    document.getElementById("div_caller").innerHTML = xml;
}
function popChat(cip, cid)
{
    var top = (screen.availHeight - 468)/2;
    var left = (screen.availWidth - 692)/2;
    url = "onlinechat/chatmessage.jsp?callerId="+cip+"&cid="+cid;
    window.open(url, "", "top="+top+",left="+left+",height=468,width=692,status=no,toolbar=no,menubar=no,location=no");
}
refreshMessage();

/**
 * 邀请对话
 */
function showInvite(b)
{
    if(b)
    {
        document.getElementById("invitediv").style.display='block';
        window.focus();
    }
    else
    {
        document.getElementById("invitediv").style.display='none';
    }
}

/**
 * 关闭邀请对话
 */
function closeInvite()
{
    showInvite(false);
}

/**
 * 暂不通话
 */
function donotchat()
{
    showInvite(false);
    createXmlHttp();
    xmlHttp.open("get", "chatAction.do?act=GET_CHAT_MESSAGE&callerId="+caller+"&"+date.getTime(), false);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send();
    var xml = xmlHttp.responseText;
    xmlHttp.abort();
    caller = null;
}
/**
 * 访客请求咨询
 */
function calltoUser()
{
    if(caller == null || parseFloat(caller)<=0){
        //求出最空闲的客服
        createXmlHttp();
        xmlHttp.open("get", "chatAction.do?act=GET_FREE_SERVICE&d="+date.getTime(), false);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.send();
        var xml = Jtrim(xmlHttp.responseText);
        xmlHttp.abort();
        caller = parseFloat(xml);
    }
    //如果有客服在线则弹出聊天窗口
    if(parseFloat(caller)>0){
        popChat(parseFloat(caller));
    }
    //否则转到留言页面
    else{
        alert("客服不在线，自动转到留言页面。\n\n请您留言！");
        location.href="contact us.html";
    }
}

function moveServiceUser()
{
    var L1 = document.getElementById("invitediv");
    var L2 = document.getElementById("servicediv");
    //L1.style.left=((document.body.offsetWidth-parseFloat(L1.style.width))/2)+document.body.scrollLeft;
    L1.style.top=((document.body.offsetHeight-parseFloat(L1.style.height))/2)+document.body.scrollTop;

    //L2.style.left=((document.body.offsetWidth-parseFloat(L2.style.width))/2)+document.body.scrollLeft;
    if(L2 != null)
    L2.style.top=((document.body.offsetHeight-parseFloat(L2.style.height))/2)+document.body.scrollTop;
}
window.setInterval("moveServiceUser()", 200);

window.onbeforeunload = function(){
    location.href = "updateUserAction.do?act=USER_OUTLINE";
}
/*
 *去掉字符串两边的空格
 */
function Jtrim(str)
{
	str=new String(""+str);
        var i = 0;
        var len = str.length;
        if ( str == "" ) return( str );
        j = len -1;
        flagbegin = true;
        flagend = true;
        while (( flagbegin == true) && (i< len))
        {
           iCode = str.charCodeAt(i);
           if (iCode==32 || iCode==13 || iCode==10)
           {
              i=i+1;
              flagbegin=true;
            }
            else
            {
                flagbegin=false;
             }
        }

        while  ((flagend== true) && (j>=0))
        {
            iCode = str.charCodeAt(j);
           if (iCode==32 || iCode==13 || iCode==10)
           {
                j=j-1;
                flagend=true;
            }
            else
            {
                 flagend=false;
             }
        }

        if ( i > j ) return ("");
        trimstr = str.substring(i, j+1);
        return trimstr;
}
function isDigit(s)
{
    return (/^[0-9]{1,20}$/.test(s)) ;
}

