﻿var focused = false;
var lastNotify = new Date();
var nextMsgId = 0;
var MsgPrefix = "vMsg_";
var msgTxtBox = "vMESSAGE";
var typingStatus = "vTYPINGSTATUS";

if ('undefined' != typeof HTMLAnchorElement) {
    if (!HTMLAnchorElement.prototype.click) {
        HTMLAnchorElement.prototype.click = function() {
            var ev = document.createEvent('MouseEvents');
            ev.initEvent('click', true, true);
            if (this.dispatchEvent(ev) !== false) {
                //safari will have already done this, but I'm not sniffing safari
                //just in case they might in the future fix it; I figure it's better
                //to trigger the action twice than risk not triggering it at all
                document.location.href = this.href;
            }
        }
    }
}

function debug(obj) {
    if ('undefined' != typeof console)
        console.log("%o", obj);
}

function scrollDiv() {
    var d;
    if ((d = document.getElementById('vCONVERSATIONCONTAINER')) && ('undefined' != typeof d.scrollTop)) {
        d.scrollTop = 5000;
    }
    setMessageFocus();  
}

function checkEnter(e) { //e is event object passed from function invocation
    var characterCode;  // literal character code will be stored in this variable
    if (e && e.which) { //if which property of event object is supported (NN4)
        e = e;
        characterCode = e.which;  //character code is contained in NN4's which property
    }
    else {
        e = event;
        characterCode = e.keyCode;  //character code is contained in IE's keyCode property
    }

    if (characterCode == 13) { //if generated character code is equal to ascii 13 (if enter key)
        SendMsg();
        return false;
    } else {
        var now = new Date();
        var elapse = now.getSeconds() - lastNotify.getSeconds();
        var sameMinute = now.getMinutes() - lastNotify.getMinutes();
        if (sameMinute != 0 || elapse >= 10) {
            PageMethods.SetTypingNotification(OnNotificationComplete);
            lastNotify = new Date();
        }
        var messageBox = document.getElementById(msgTxtBox);
        if (messageBox) {
            setMessageFocus();
        }
        return true;
    }
}

function AgentTyping(isAgent) {
    var tStatus = document.getElementById(typingStatus);
    if (tStatus != null) {
        if (isAgent) {
            tStatus.style.display = 'inline';
        } else {
            tStatus.style.display = 'none';
        }
    }
}

function CheckMessage() {
    debug("CheckMessage()");
    PageMethods.GetMessages(OnMessageNotificationComplete);
    setMessageFocus();
}

function SendMsg() {
    var messageBox = document.getElementById(msgTxtBox);
    if (messageBox) {
        PageMethods.SendMessage(messageBox.value, OnMessageNotificationComplete);
        messageBox.value = "";
        setMessageFocus();
    }
}

function AddMessage(MessageID, UserName, Message, VisitorName) {
    debug("AddMessage:" + MessageID + " Visitor:" + UserName);    
    nextMsgId = parseInt(MessageID);
    if (document.getElementById(MsgPrefix + MessageID) == null) {
        if (UserName == "visitor")
            UserName = VisitorName;

        var msgClass = "vMsgAgentName";
        var txtClass = "vMsgAgentText";
        if (VisitorName == UserName) {
            msgClass = "vMsgVisitorName";
            txtClass = "vMsgVisitorText";
        }
            
        var newMessage = "<span class=\"" + msgClass + "\">" + UserName + ":</span> <span class=\"" + txtClass + "\">" + Message + "</span>";
        
        if (UserName == "System" || UserName == "Notice")
            newMessage = "<span class=\"vSystemMsg\">" + newMessage + "</span>";
                       
        newMessage = "<span id=\"" + MsgPrefix + MessageID + "\" name=\"" + MsgPrefix + MessageID + "\">" + newMessage + "</span><br />\n";

        var d;
        if ((d = document.getElementById('vCONVERSATIONCONTAINER')) && ('undefined' != typeof d.scrollTop)) {
            d.innerHTML += newMessage;
        }
        scrollDiv();
    }
}

function PushPage(pushurl, MessageID) {
    if (document.getElementById(MsgPrefix + MessageID) == null) {
        eval("window.open('" + pushurl + "', 'VelaroCoBrowsing');");
    }
}

var soundEnabled = true;
function ToggleSound() {
    soundEnabled = !soundEnabled;
    var soundOn = document.getElementById("vSoundOn");
    if (soundOn)
        soundOn.style.display = (soundEnabled ? "none" : "inline");

    var soundOff = document.getElementById("vSoundOff");
    if (soundOff)
        soundOff.style.display = (soundEnabled ? "inline" : "none");
}

function PlaySound() {
    if (!soundEnabled)
        return;
    
    var soundObj = document.getElementById("sound");
    if (soundObj) {
        if (soundUrl) {
            soundObj.src = soundUrl;    
        }
    }
}

function SendFocus() {
    if(!focused)    
        self.focus();
}

function hasFocus(bol) {
    focused = bol;
}

function OnMessageNotificationComplete(result, methodName) {
    if (result != '') {
        var message_array = result.split("%%");
        for (i=0;i<message_array.length;i++) {
            try {
                debug(message_array[i]);
                eval(message_array[i]);                
            }
            catch (ex) {
                debug("error: " + ex);
            }
        }
        
        setMessageFocus();
    }
}

function OnNotificationComplete(result, methodName) {
    if (result != '') {
    }
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function confirmExit() {
    document.location = document.location + '&closeme=yes';
}

function setMessageFocus() {
    var messageBox = document.getElementById(msgTxtBox);
    if (messageBox) {
        setCursor(messageBox, messageBox.value.length, messageBox.value.length);
    }
    debug("setMessageFocus()");
}

function setCursor(el, st, end) {
    if (el.setSelectionRange) {
        el.focus();
        el.setSelectionRange(st, end);
    }
    else {
        if (el.createTextRange) {
            range = el.createTextRange();
            range.collapse(false);
            range.select();
        }
    }
} 
