﻿var comm100_EnumChatAction = {
    HeartBeatForSite: 80,
    AutoInvitationShowed: 81,
    RefuseInvitation: 82
};
var comm100_EnumButton = {
    Button: 0,
    Text: 1   
};
var comm100_g_lastError = "";
var comm100_g_secureCookies = false;

if (window.comm100_g_Info == null) {
    var serverUri = comm100_GetServerUriFromCodePage();
    
    window.comm100_g_Info =
    {
        visitorId: -1,
        siteId: 27870,
        planId: 427,
        serverUri: serverUri,
        urlService: serverUri + "/LiveChatVisitorHandler.ashx",
        urlChatButton: serverUri + "/ChatButton.aspx",
        chatWindowUrl: serverUri + "/ChatWindow.aspx",
        /* Configs */
        intervalForSite: 20, //in second
        intervalForGarbage: 60, //in second
        invitationMoveDirection: 1, //invitation move default to right
        invitationLastScrollY: 0,
        invitationIfStopMoving: false,
        ifIsBeingInvited: false,
        ifAutoInvited: false,        
        callId: 0,
        callBackIds: [],
        /* Invatition Setting */
        invitationAutoPopupDelay: 0,
        invitationIfAutoPopup: false,
        invitationIfDefaultImages: false,
        invitationIfSoundOn: false,
        invitationNoImageURL: "",
        invitationPositionLeft: 0,
        invitationPositionStyleEnum: 0,
        invitationPositionTop: 0,
        invitationSoundURL: "",
        invitationYesImageURL: "",

        invitationDivName: "comm100_invitationDiv",
        chatButtonDivId2_0: "LiveChatDiv",
        LiveChatDivId: "comm100_LiveChatDiv",
        invitationSoundObjectId: "comm100_invitationSound",

        enumButton: comm100_EnumButton.Button,
        cookiePrefix: "comm100_",
        scriptPrefix: "lcScript_", 
        chatButtonTextContent: "",
        chatButtonOfflineURL: "/App_Themes/Default/images/livechat_offline.png",
        chatButtonOnlineURL: "/App_Themes/Default/images/livechat_online.png",
        ifOnline: null,
        ifVersion2_0: false,
        chatButtonLinkId: "comm100_TextLink",
        chatButtonImageId: "comm100_ButtonImage",        
        ifChatButtonLoaded: null,
        heartBeatTimer: null
    };
};

/* Start to Run */
if (comm100_g_Info.ifChatButtonLoaded == null) {
    comm100_g_Info.ifChatButtonLoaded = false;
    comm100_PageVisited();
}

/* Page visited */
function comm100_PageVisited() {
    try {
        comm100_InitGlobalVariables();
        comm100_LoadChatButton();
    }
    catch (e) {
        comm100_HandleException(e, "PageVisited");
    }
}

/* Init global variables */
function comm100_InitGlobalVariables() {
    /*var parameterMap = comm100_GetJavaScriptSrcParameterMap();
    comm100_g_Info.siteId = comm100_RequestJSParameter(parameterMap, "siteId", comm100_g_Info.siteId);
    comm100_g_Info.planId = comm100_RequestJSParameter(parameterMap, "planId", comm100_g_Info.planId);

    var visitorIdFromCookieOld = comm100_FindCookie(comm100_g_Info.siteId);
    var visitorIdFromCookie = comm100_FindCookie(comm100_g_Info.cookiePrefix + comm100_g_Info.siteId);
    if (visitorIdFromCookie != null && visitorIdFromCookie != "") {
        var visitorId = parseInt(visitorIdFromCookie, 10);
        if (isNaN(visitorId) == false) {
            comm100_g_Info.visitorId = visitorId;
        }
    }
    else if (visitorIdFromCookieOld != null && visitorIdFromCookieOld != "") {
        var visitorId = parseInt(visitorIdFromCookieOld, 10);
        if (isNaN(visitorId) == false) {
            comm100_g_Info.visitorId = visitorId;
        }
    }*/
}

/* Load Chat Button */
function comm100_LoadChatButton() {
    if (document.getElementsByTagName("head").length == 0) {
        var htmlNode = document.documentElement;
        var headNode = document.createElement("head");
        htmlNode.insertBefore(headNode, htmlNode.firstChild);
    }

    comm100_CreateDivForInviation();
    comm100_LoadChatButtonByScript();
}

/* Load Chat Button */
function comm100_LoadChatButtonByScript() {
    var fullurl = comm100_g_Info.urlChatButton + "?siteId=" + comm100_g_Info.siteId + "&visitorId=" + comm100_g_Info.visitorId + "&planId=" + comm100_g_Info.planId;
    var visitorClientInfo = new comm100_VisitorClientInfo();
    fullurl += "&flash=" + visitorClientInfo.flashVersion;
    //fullurl += "&title=" + visitorClientInfo.pageTitle;
    fullurl += "&url=" + visitorClientInfo.pageUrl;
    fullurl += "&referer=" + visitorClientInfo.refererUrl;
    fullurl += "&res=" + visitorClientInfo.screenResolution;
    fullurl += "&timezone=" + visitorClientInfo.timeZone;

    comm100_SendWebRequestByScript(fullurl);
    comm100_g_Info.callId++;
}

function comm100_LoadChatButtonCallback(visitorId, onlineOperatorNumber, buttonType) {
    try {
        comm100_g_Info.visitorId = visitorId;
        comm100_g_Info.enumButton = buttonType;

        comm100_SetLiveChatCookieExpired(comm100_g_Info.siteId, visitorId);
        comm100_SetLiveChatCookie(comm100_g_Info.cookiePrefix + comm100_g_Info.siteId, visitorId);

        if (document.getElementById(comm100_g_Info.chatButtonDivId2_0) != null) {
            comm100_g_Info.ifVersion2_0 = true;
            comm100_SetChatButton2_0();
        }
        else {
            if (comm100_g_Info.enumButton == comm100_EnumButton.Text
                && document.getElementById(comm100_g_Info.chatButtonLinkId) != null) {
                document.getElementById(comm100_g_Info.chatButtonLinkId).innerHTML = comm100_g_Info.chatButtonTextContent;
            }
        }

        if (comm100_g_Info.enumButton == comm100_EnumButton.Button)
        {
            comm100_ShowOperatorOnlineOrOffline(onlineOperatorNumber > 0);
        }

        setTimeout("comm100_HeartBeatForSite()", 1000);

        comm100_g_Info.ifChatButtonLoaded = true;
    } catch (e) { comm100_HandleException(e, "LoadChatButtonCallback"); }
}

function comm100_ShowOperatorOnlineOrOffline(ifOnline) {
    var imgChatButton = document.getElementById(comm100_g_Info.chatButtonImageId);
    if (comm100_g_Info.ifOnline != ifOnline) {
        if (ifOnline) {
            imgChatButton.src = comm100_g_Info.chatButtonOnlineURL;
        }
        else
            imgChatButton.src = comm100_g_Info.chatButtonOfflineURL;

        comm100_g_Info.ifOnline = ifOnline;
    }
}

/* Find Cookie */
function comm100_FindCookie(key) {
    var cookie = document.cookie;
    var allCookies = cookie.split("; ");
    for (i = 0; i < allCookies.length; i++) {
        var keyval = allCookies[i].split("=");
        if (keyval != null && keyval.length == 2 && key == keyval[0])
            return keyval[1];
    }
    return "";
}

/* Set LiveChat Cookie */
function comm100_SetLiveChatCookie(name, value) {
    var date = new Date();
    var dateAfterOneYear = new Date(date.getTime() + (1000 * 60 * 60 * 24 * 365 * 100));
    var expirydate = dateAfterOneYear.toGMTString();
    if (name != "-")
        comm100_SetCookie(name, value, expirydate);
}

/* Set LiveChat Cookie */
function comm100_SetLiveChatCookieExpired(name, value) {
    var date = new Date();
    var dateExpired = new Date(date.getTime() - 1000);
    var expirydate = dateExpired.toGMTString();
    if (name != "-")
        comm100_SetCookie(name, value, expirydate);
}

/* Set Cookie */
function comm100_SetCookie(name, value, expiry) {
    var mainDomain = null;
    var cookieSet = name + "=" + value + ";path=/" + (comm100_g_secureCookies ? ";secure" : "") + (expiry == null ? "" : (";expires=" + expiry)) + (mainDomain == null ? "" : (";domain=" + mainDomain));
    document.cookie = cookieSet;
}

/* Get Js parameters from js url */
function comm100_RequestJSParameter(parasMap, param, defaultValue) {

    if (!parasMap)
        return defaultValue;

    var paraValue = parasMap[param];
    var returnValue = parseInt(paraValue, 10);
    if (!returnValue || isNaN(returnValue))
        return defaultValue;
    else
        return returnValue;
}

/* Get Js parameters from js url */
function comm100_GetJavaScriptSrcParameterMap() {
    var map = new Array();
    var tgs = document.getElementsByTagName("script");
    if (tgs.length <= 0) { return null; }
    var src = tgs.item(tgs.length - 1).src;
    var pos = src.indexOf('?');
    if (-1 == pos) { return null; }
    var paras = src.substring(pos + 1);
    paras = paras.split('&');
    for (var n = 0; n < paras.length; n++) {
        comm100_ParseParameter(map, paras[n]);
    }

    return map;
}

/* Get Js parameters from js url */
function comm100_GetServerUriFromCodePage() {
    /*try {
        var JSProtocol = "";

        var tgs = document.getElementsByTagName("script");
        if (tgs.length <= 0) { return null; }
        var src = tgs.item(tgs.length - 1).src;
        var pos = src.indexOf('//');
        if (-1 == pos) { return null; }
        JSProtocol = src.substring(0, pos + 2);
        var srcWithoutProtocol = src.substring(pos + 2);
        pos = srcWithoutProtocol.indexOf('/');
        if (-1 == pos) return JSProtocol + srcWithoutProtocol;
        return JSProtocol + srcWithoutProtocol.substring(0, pos);
    }
    catch (e) {
        comm100_HandleException(e, "GetServerUriFromCodePage");
    }
    return "";*/
    return "http://chatserver.comm100.com/";
}

/* Parse Js parameters */
function comm100_ParseParameter(map, para) {
    var pos = para.indexOf('=');
    var key = para.substring(0, pos);
    var value = para.substring(pos + 1);
    map[key] = value;
}

/* Heart Beat For Site Callback */
function comm100_HeartBeatForSiteCallback(ifOpratorOnline, ifInvited, errorMessage) {
    if (comm100_g_Info.enumButton == comm100_EnumButton.Button) {
        comm100_ShowOperatorOnlineOrOffline(ifOpratorOnline);
    }

    if (ifInvited == true) {
        comm100_g_Info.ifAutoInvited = false;
        comm100_Invite();
    }
    else if (comm100_g_Info.ifAutoInvited == false) {
        comm100_g_Info.ifIsBeingInvited = false;
        comm100_g_Info.invitationIfStopMoving = false;
        comm100_RemoveInvitationDiv();
    }
}

/* Handle Response */
function comm100_HR(responseData) {
    try {
        var errorCode = responseData["c"];
        var errorMessage = responseData["e"];
        var status = responseData["s"];
        var action = responseData["a"];
        var callId = responseData["i"];
        comm100_g_Info.callBackIds.push(callId);

        if (errorCode == 0) {
            if (action == comm100_EnumChatAction.HeartBeatForSite) {
                var ifOperatorOnline = status["o"];
                var ifInvited = status["i"];                
                comm100_HeartBeatForSiteCallback(ifOperatorOnline, ifInvited, errorMessage);
            }
        }
        else {
            if (errorCode == 1004) {
                comm100_LoadChatButtonByScript();
            }

            //comm100_HandleErrorFromServer(errorCode, errorMessage);
        }
    }
    catch (e) {
        comm100_HandleException(e, "HandleResponse");
    }
}

function comm100_HeartBeatForSite() {
    comm100_SendActionCallByScript(comm100_EnumChatAction.HeartBeatForSite);
    var multiple = 1;
    if (comm100_g_Info.callId > 120) {// 120s / 60min
        multiple = 6;
    }
    else if (comm100_g_Info.callId > 90) {// 60s / 30min
        multiple = 3;
    }
    setTimeout("comm100_HeartBeatForSite()", comm100_g_Info.intervalForSite * multiple * 1000);
}

function comm100_AutoInvitationShowed() {
    comm100_SendActionCallByScript(comm100_EnumChatAction.AutoInvitationShowed);
}

function comm100_RefuseInvitation() {
    comm100_SendActionCallByScript(comm100_EnumChatAction.RefuseInvitation);
}

/* Send Live Chat Action By Script */
function comm100_SendActionCallByScript(action) {
    var enumAction = action;
    var radomString = Math.round(10000 * Math.random());
    var handleUrl = comm100_g_Info.urlService + "?siteId=" + comm100_g_Info.siteId + "&action=" + enumAction + "&visitorId=" + comm100_g_Info.visitorId + "&callId=" + comm100_g_Info.callId + "&r=" + radomString;
    comm100_SendWebRequestByScript(handleUrl);
    comm100_g_Info.callId++;
}

/* Send Web Request By Script */
function comm100_SendWebRequestByScript(url) {
    comm100_LoadScript(url, "lcScript_" + comm100_g_Info.callId);
}

/* Get Flash Player Version */
function comm100_GetPlayerVersion() {
    var _25 = new comm100_PlayerVersion([0, 0, 0]);
    if (navigator.plugins && navigator.mimeTypes.length) {
        var x = navigator.plugins["Shockwave Flash"];
        if (x && x.description) { _25 = new comm100_PlayerVersion(x.description.replace(/([a-z]|[A-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split(".")); }
    } else {
        try {
            var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
            for (var i = 3; axo != null; i++) { axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i); _25 = new comm100_PlayerVersion([i, 0, 0]); }
        }
        catch (e) { }

        try { _25 = new comm100_PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(",")); }
        catch (e) { }
    }

    return _25;
};

function comm100_PlayerVersion(_29) {
    this.major = parseInt(_29[0]) != null ? parseInt(_29[0]) : 0;
    this.minor = parseInt(_29[1]) || 0;
    this.rev = parseInt(_29[2]) || 0;
    this.version = _29.join(".");
};

/* Get Visitor Client Information */
function comm100_VisitorClientInfo() {
    this.screenResolution = encodeURIComponent(screen.width + "x" + screen.height);
    this.pageUrl = encodeURIComponent(document.location);
    this.refererUrl = encodeURIComponent(document.referrer);
    this.pageTitle = encodeURIComponent(document.title);
    this.flashVersion = comm100_GetPlayerVersion().version;
    var timeZoneOffset = new Date().getTimezoneOffset() / 60 * (-1);

    if (timeZoneOffset > 0) {
        this.timeZone = encodeURIComponent("GMT+" + timeZoneOffset);
    }
    else if (timeZoneOffset < 0) {
        this.timeZone = encodeURIComponent("GMT" + timeZoneOffset);
    }
    else {
        this.timeZone = encodeURIComponent("GMT");
    }
}


/* Let the invitation div move */
function comm100_MoveInvitationdiv() {
    if (comm100_g_Info.ifIsBeingInvited == true && comm100_g_Info.invitationIfStopMoving == false) {
        var div = document.getElementById(comm100_g_Info.invitationDivName);
        if (!div)
            return;
        if (comm100_g_Info.invitationPositionStyleEnum == 0) {
            var xx = new Array();
            var speed = 1;  //distance
            var speed2 = 20; //interval time, 10ms
            xx.push(0);
            xx.push((window.innerWidth || document.body.clientWidth) - div.offsetWidth - 30);
            var x = parseInt(div.style.left.replace(/^(\d+).*?$/, "$1"));
            div.style.left = x + speed * comm100_g_Info.invitationMoveDirection + "px";
            if (x <= xx[0]) { comm100_g_Info.invitationMoveDirection = 1; }
            else if (x >= xx[1]) { comm100_g_Info.invitationMoveDirection = -1; }
        }

        var currentTop = document.body.scrollTop || document.documentElement.scrollTop;
        var percent = 0.2 * (currentTop - comm100_g_Info.invitationLastScrollY);
        if (percent > 0) percent = Math.ceil(percent);
        else percent = Math.floor(percent);
        var y = parseInt(div.style.top.replace(/^(\d+).*?$/, "$1"));
        var newTop = y + percent;
        comm100_g_Info.invitationLastScrollY = comm100_g_Info.invitationLastScrollY + percent;
        div.style.top = newTop + "px";
        setTimeout("comm100_MoveInvitationdiv()", speed2);
    }
}

/* Stop the invitation div moving*/
function comm100_StopMovingInvitationdiv() {
    comm100_g_Info.invitationIfStopMoving = true;
}

/* Stop the invitation div moving*/
function comm100_ContinueMovingInvitationdiv() {
    comm100_g_Info.invitationIfStopMoving = false;
    comm100_MoveInvitationdiv();
}

/* the href about chat*/
function comm100_Chat() {
    //if (!comm100_g_Info.ifChatButtonLoaded)
        //return;

    comm100_g_Info.ifAutoInvited = false;
    var radomString = Math.round(10000 * Math.random());
    var chatWindowUrl = comm100_g_Info.chatWindowUrl + "?siteId=" + comm100_g_Info.siteId +
                "&visitorId=" + comm100_g_Info.visitorId +
                "&planId=" + comm100_g_Info.planId + "&r=" + radomString;
    /*remove scrollbars=yes, */
    window.open(chatWindowUrl, 'livechat' + comm100_g_Info.siteId, 'left = 150, top = 150, height = 550, width = 551, resizable=yes, toolbar = no, location = no, menubar = no, status = no');
    comm100_StopInvatition();
}

function comm100_StopInvatition() {
    comm100_g_Info.ifIsBeingInvited = false;
    comm100_g_Info.invitationIfStopMoving = false;
    comm100_RemoveInvitationDiv();
}

/* Cancel Invatition*/
function comm100_CancelInvatition() {
    comm100_RefuseInvitation();
    comm100_StopInvatition();
}

/* Accept Invatition*/
function comm100_AcceptInvatition() {
    comm100_Chat();
}

function comm100_GetChatHTML2_0() {
    var html = '<a href="#" onclick="comm100_Chat();return false;" target="_blank">';
    if (comm100_g_Info.enumButton == comm100_EnumButton.Button) {
        html += '<img id="' + comm100_g_Info.chatButtonImageId + '" border="0px" alt="Live Chat by comm100" /></a>';
    }
    else if (comm100_g_Info.enumButton == comm100_EnumButton.Text) {
        html += comm100_g_Info.chatButtonTextContent + '</a>';
    }
    return html;
}

function comm100_CreateDivForInviation() {
    if (document.getElementById(comm100_g_Info.LiveChatDivId) == null) {
        document.write("<div id='" + comm100_g_Info.LiveChatDivId + "'></div>");
    }
}

//For version 2.0
function comm100_SetChatButton2_0() {
    if (comm100_g_Info.ifVersion2_0 == true) {
        var divLiveChat = document.getElementById(comm100_g_Info.chatButtonDivId2_0);
        if (divLiveChat != null) divLiveChat.innerHTML = comm100_GetChatHTML2_0();
    }
}

function comm100_GetLiveChatDiv() {
    return document.getElementById(comm100_g_Info.LiveChatDivId);
}

function comm100_SetChatLinkText(linkText) {
    comm100_g_Info.chatButtonTextContent = linkText;
}

function comm100_SetChatButtonImage(chatButtonOnlineURL, chatButtonOfflineURL) {
//    comm100_g_Info.chatButtonOnlineURL = chatButtonOnlineURL;
//    comm100_g_Info.chatButtonOfflineURL = chatButtonOfflineURL;
	comm100_g_Info.chatButtonOnlineURL = "/App_Themes/Default/images/livechat_online.png";
    comm100_g_Info.chatButtonOfflineURL = "/App_Themes/Default/images/livechat_offline.png";
}

/* Set invatition div configuration*/
function comm100_SetInvatition(autoPopupDelay, ifAutoPopup, ifSoundOn, positionEnum, positionTop, positionLeft, yesImageURL, noImageURL, soundURL) {
    comm100_g_Info.invitationAutoPopupDelay = autoPopupDelay;
    comm100_g_Info.invitationIfAutoPopup = ifAutoPopup;
    comm100_g_Info.invitationIfSoundOn = ifSoundOn;
    comm100_g_Info.invitationNoImageURL = noImageURL;
    comm100_g_Info.invitationPositionLeft = positionLeft;
    comm100_g_Info.invitationPositionStyleEnum = positionEnum;
    comm100_g_Info.invitationPositionTop = positionTop;
    comm100_g_Info.invitationSoundURL = soundURL;
    comm100_g_Info.invitationYesImageURL = yesImageURL;
}

/* Show Invation Div*/
function comm100_Invite() {
    try {
        if (comm100_g_Info.ifIsBeingInvited == false) {
            comm100_g_Info.ifIsBeingInvited = true;
            comm100_RemoveInvitationDiv();
            comm100_CreateInvitationDiv();
            comm100_MoveInvitationdiv();
            window.focus();
        }
    }
    catch (e) {
        comm100_HandleException(e, "Invite");
    }
}

function comm100_AutoInvite() {
    try {
        comm100_g_Info.ifAutoInvited = true;
        comm100_AutoInvitationShowed();
        setTimeout("comm100_Invite()", comm100_g_Info.invitationAutoPopupDelay * 1000);
    }
    catch (e) {
        comm100_HandleException(e, "AutoInvite");
    }
}

function comm100_CreateInvitationDiv() {
    var creatdiv = document.createElement('div');
    creatdiv.setAttribute('id', comm100_g_Info.invitationDivName);
    comm100_g_Info.invitationLastScrollY = 0;
    creatdiv.style.position = "absolute";
    creatdiv.style.top = "" + comm100_g_Info.invitationPositionTop + "px";
    creatdiv.style.left = "" + comm100_g_Info.invitationPositionLeft + "px";
    creatdiv.style.zIndex = 65536000;
    creatdiv.onmouseover = function() { comm100_StopMovingInvitationdiv(); };
    creatdiv.onmouseout = function() { comm100_ContinueMovingInvitationdiv(); };
    var innerHTML = "<iframe style='position:absolute; z-index:-1;width:100%;height:100%;filter:alpha(opacity=0,style=0);' frameborder='0' src='javascript:\"\"'></iframe><div><a href='#' onclick=\"comm100_AcceptInvatition();return false;\" target='_blank'>";
    innerHTML += "<img src='" + comm100_g_Info.invitationYesImageURL + "' style='border:none' /></a></div><div style='position:absolute; top:0px;right:0px;z-index:3;'>";
    innerHTML += "<a href='#' onclick=\"comm100_CancelInvatition();return false;\" target='_blank'><img src='" + comm100_g_Info.invitationNoImageURL + "' style='border:none' /></a>";
    innerHTML += "</div>";
    creatdiv.innerHTML = innerHTML;
    //comm100_GetLiveChatDiv().appendChild(creatdiv);
    document.body.appendChild(creatdiv);
    if (comm100_g_Info.invitationIfSoundOn == true) {
        comm100_PlaySound();
    }
}

/**
* Remove Invitation Div
*/
function comm100_RemoveInvitationDiv() {
    var invitationDiv = document.getElementById(comm100_g_Info.invitationDivName);
    if (invitationDiv != null) {
        invitationDiv.style.display = "none";
        document.body.removeChild(invitationDiv);
        comm100_RemoveSoundObject();
    }
}

function comm100_PlaySound() {
    var browser = comm100_GetBrowserType();
    var soundFileUrl = comm100_g_Info.invitationSoundURL;
    try {
        var body = document.getElementsByTagName('body');
        var sndObj;
        if (browser == 'IE' || browser == "OPR") {
            sndObj = document.createElement('bgsound'); sndObj.autostart = 'true';
            sndObj.loop = '0'; sndObj.src = soundFileUrl;
        }
        else {
            sndObj = document.createElement('object'); sndObj.setAttribute('data', soundFileUrl);
            //sndObj.setAttribute('type','audio/midi');
            sndObj.setAttribute('controller', 'true');
        }
        sndObj.id = comm100_g_Info.invitationSoundObjectId;
        sndObj.style.position = 'absolute';
        sndObj.style.left = '0px';
        sndObj.style.top = '0px';
        sndObj.hidden = 'true';
        body[0].appendChild(sndObj);
    }
    catch (e) { }
};

function comm100_RemoveSoundObject() {
    var sndObj = document.getElementById(comm100_g_Info.invitationSoundObjectId);
    if (sndObj != null) {
        try { sndObj.parentNode.removeChild(sndObj); } catch (e) { }
    }
};

/* Get Browser Type*/
function comm100_GetBrowserType() {
    var agt = navigator.userAgent.toLowerCase();
    if (agt.indexOf("safari") != -1)
    { return 'SAFARI'; }
    if (document.layers) { return "NS"; }
    if (document.all) {
        var is_opera = (agt.indexOf("opera") != -1);
        var is_konq = (agt.indexOf("konqueror") != -1);
        if (is_opera) { return "OPR"; }
        else {
            if (is_konq) { return "KONQ"; }
            else { hcIE = true; return "IE"; }
        }
    }
    if (document.getElementById) {
        var is_ff = (agt.indexOf("firefox") != -1);
        if (is_ff) { return "FF"; }
        return "MOZ";
    }
    return "MOZ";
}

function comm100_GarbageCollection() {
    var headLoc = document.getElementsByTagName("head").item(0);
    for (var i = 0, iMax = comm100_g_Info.callBackIds.length; i < iMax; i++) {
        if (comm100_g_Info.callBackIds[i] != null && document.getElementById("lcScript_" + comm100_g_Info.callBackIds[i]) != undefined)
            headLoc.removeChild(document.getElementById("lcScript_" + comm100_g_Info.callBackIds[i]));
    }
    var removedItems = comm100_g_Info.callBackIds.splice(0, comm100_g_Info.callBackIds.length);
}

/* For Debug: handle exception or error*/
function comm100_HandleException(e, method) {
    if (e && e.message && e.message != comm100_g_lastError) {
        alert(e.message + " [" + method + "]");
        comm100_g_lastError = e.message;       
    }
}

/* Load Script */
function comm100_LoadScript(url, id) {
    var headLoc = document.getElementsByTagName("head").item(0);
    var scriptObj = document.createElement("script");
    scriptObj.setAttribute("type", "text/javascript");
    scriptObj.setAttribute("src", url);
    scriptObj.setAttribute("id", id);
    headLoc.appendChild(scriptObj);
    return scriptObj;
}
if (typeof comm100_g_garbageStarted == "undefined") {
    var garbageTimer = setInterval("comm100_GarbageCollection()", comm100_g_Info.intervalForGarbage * 1000);
}
var comm100_g_garbageStarted = true;
