﻿var webchat_companyID = "ukeas.com.cn";
var enterurl = "null";
var isOldSkin = false;
var webchat_Root = "http://www.ukeas.com.cn:887/";
var webchat_Url = "http://www.ukeas.com.cn:887/ChatBox.aspx";
var webchat_winName = "xsunt_webchat";
var globalWindowAttribute = 'toolbar=0,scrollbars=0,location=0,menubar=0,resizable=1,width=570,height=424,top=' + (screen.height - 424) / 2 + ',left=' + (screen.width - 570) / 2;
var webchat_AjaxHandler = "http://www.ukeas.com.cn:887/AjaxHandler.ashx?usw=ukeas.com.cn";
var webchat_configContent = "webchat_float=1&webchat_floatToRight=1&webchat_floatTop=180&webchat_floatSide=5&webchat_switch=0&webchat_closeIcon=0";
var webchat_opencommand = "OpenChatWindow('9999', 'UKEAS总机')";

function EncodeUrl(Str) {
    if (Str == null || Str == "") {
        return ""
    }
    var newStr = "";

    function toCase(sStr) {
        return sStr.toString(16).toUpperCase()
    }

    for (var i = 0, icode, len = Str.length; i < len; i++) {
        icode = Str.charCodeAt(i);
        if (icode < 0x10) {
            newStr += "%0" + icode.toString(16).toUpperCase()
        }
        else if (icode < 0x80) {
            if (icode == 0x20) {
                newStr += "+"
            }
            else if ((icode >= 0x30 && icode <= 0x39) ||
              (icode >= 0x41 && icode <= 0x5A) ||
              (icode >= 0x61 && icode <= 0x7A)) {
                newStr += Str.charAt(i)
            }
            else {
                newStr += "%" + toCase(icode)
            }
        }
        else if (icode < 0x800) {
            newStr += "%" + toCase(0xC0 + (icode >> 6));
            newStr += "%" + toCase(0x80 + icode % 0x40)
        }
        else {
            newStr += "%" + toCase(0xE0 + (icode >> 12));
            newStr += "%" + toCase(0x80 + (icode >> 6) % 0x40);
            newStr += "%" + toCase(0x80 + icode % 0x40)
        }
    }
    return newStr
}

function OpenChatWindow(cs, name) {
    var openUrl = webchat_Url;
    if (openUrl.indexOf("?") < 0)
        openUrl += "?dp=1"; //add a dummy param

    if (typeof cs != "undefined")
        openUrl += "&cs=" + cs;
    if (typeof name != "undefined")
        openUrl += "&name=" + name;
    openUrl += "&companyID=" + webchat_companyID;
    openUrl += "&enterurl=" + EncodeUrl(document.URL);
    openUrl += "&tm=" + (new Date()).getTime();
    var winName = typeof webchat_winName == "undefined" ? "XWebChatWindow" : webchat_winName;
    var winAttr = globalWindowAttribute;
    var chatWindow = window.open(openUrl, winName, winAttr);
    chatWindow.focus();
}

function CreateCSItems(json) {
    if (json.cslist) {
        var content = "<div class='chat' scrolling=no style='padding:0;width:88px;z-index:1000'>";
        var hasActiveCS = false;
        var listhtml = "";
        var offices = { list: [{ code: "ukeassh", name: "UKEAS上海" }, { code: "ukeasbj", name: "UKEAS北京" }, { code: "ukeasnj", name: "UKEAS南京" }, { code: "ukeasqd", name: "UKEAS青岛" }, { code: "ukeaswh", name: "UKEAS武汉" }, { code: "ukeashz", name: "UKEAS杭州" }, { code: "ukeasnb", name: "UKEAS宁波" }, { code: "ukeascd", name: "UKEAS成都" }, { code: "ukeascq", name: "UKEAS重庆" }, { code: "ukeasgz", name: "UKEAS广州" }, { code: "aileenzhao", name: "UKEAS台州" }, { code: "ukeasxa", name: "UKEAS西安"}] };
        for (var i = 0; i < json.cslist.length; i++) {
            var css = (i % 2 == 0) ? "csItemStyle" : "csAltStyle";
            var imgUrl = "http://www.ukeas.com.cn:887/images/";
            var operate;
            if (json.cslist[i].Status.toLowerCase() == "online") {
                imgUrl += "Online.gif";
                //operate = "onclick=\"OpenChatWindow('" + json.cslist[i].ID + "','" + json.cslist[i].Name + "')\"";
                hasActiveCS = true;
            }
            else {
                imgUrl += "Offline.gif";
                //operate = "disabled='disabled'";
            }
            var officeName = "";
            for (var k = 0; k < offices.list.length; k++) {
                if (offices.list[k].code == json.cslist[i].UserName) {
                    officeName = offices.list[k].name;
                    break;
                }
            }
            operate = "onclick=\"OpenChatWindow('" + json.cslist[i].ID + "','" + officeName + "')\"";
            listhtml += "<span " + operate + " class='" + css + "'><img src='" + imgUrl + "' style='border-width:0px;' />&nbsp;" + officeName + "</span><br/>";
        }
        if (hasActiveCS) {
            content += "<span onclick=\"OpenChatWindow('9999', 'UKEAS总机')\" class='csAltStyle'><img src='http://www.ukeas.com.cn:887/images/Online.gif' style='border-width:0px;' />&nbsp;UKEAS总机</span><br/>";
        }
        else {
            content += "<span disabled='disabled' class='csAltStyle'><img src='http://www.ukeas.com.cn:887/images/Offline.gif' style='border-width:0px;' />&nbsp;UKEAS总机</span><br/>";
        }
        content += listhtml;
        content += "</div>";
        $("#onlineList").append(content);
        $("#onlineList").append("<iframe style='width:88px;position:absolute;top:0;height:180px; z-index:-1;' src='about:blank' frameborder=\"0\" id='iframe_Online'></iframe>")
        $("#cs_list").append(content);
        $("#cs_list").append("<iframe style='width:88px;position:absolute;top:0;height:180px;z-index:-1;' src='about:blank' frameborder=\"0\" id='iframe_Online'></iframe>")
    }
}

$(function() {
    var jsonUrl = webchat_AjaxHandler;
    if (jsonUrl.indexOf("?") < 0)
        jsonUrl += "?dp=1"; //add a dummy param
    jsonUrl += "&companyID=" + webchat_companyID;
    jsonUrl += "&t=cslist";
    jsonUrl += "&jsoncallback=?";
    $.getJSON(jsonUrl, "", CreateCSItems);
});
