﻿function getInfoForLiveHelp(timeOutInMilliseconds) {
    if (currSessionID) {
        scrptSrvc.ChatService.RefreshUsersActiveStatus(currSessionID, null, failure);
        scrptSrvc.ChatService.CheckIfLiveHelpIsAvailable(onDisplayLiveHelpAvailability, failure);
        scrptSrvc.ChatService.CheckIfSomeoneCommunicates(currSessionID, onCheckIfSomeoneCommunicates, failure);
    }
    if (currChatRoomID != "") {
        scrptSrvc.ChatService.PopulateRoomMessages(currChatRoomID, currSessionID, OnDisplayMessagesOfRoom, failure);
        scrptSrvc.ChatService.GetUsersWritingStatus(currChatRoomID, currSessionID, OnDisplayUsersWritingStatus, failure);
    }   
    window.setTimeout('getInfoForLiveHelp(' + timeOutInMilliseconds + ')', timeOutInMilliseconds);
}

function beginChat(invitedSessionID) {
    scrptSrvc.ChatService.BeginChat(currSessionID, invitedSessionID, true, onBeginChat);
}

function joinToChatRoom(sessionID, chatRoomKey) {
    scrptSrvc.ChatService.AddUserToRoom(sessionID, chatRoomKey);
    hideAddUserWindow();
    hideUploadFileWindow();
}

function onDisplayLiveHelpAvailability(invitedSessionID) {
    var imgLiveHelp = document.getElementById('imgLiveHelp');
    var liveHelp = document.getElementById('liveHelp');
    if (imgLiveHelp && liveHelp) {
        if (invitedSessionID != "") {
            //admin is available for live help
            imgLiveHelp.src = "../DataConceptNew/SiteImages/liveHelp_flash.png";
            imgLiveHelp.alt = "Online";
            liveHelp.onclick = function() { beginChat(invitedSessionID); }
            liveHelp.style.cursor = "pointer";
        }
        else {
            imgLiveHelp.src = "../DataConceptNew/SiteImages/liveHelp_redflash.png";
            imgLiveHelp.alt = "Offline";
            //liveHelp.onclick = "javascript:void(0);";
            liveHelp.onclick = function() { move(); };
            liveHelp.style.cursor = "pointer";
        }
    }
}

function move() {
    window.location = '../DataConceptNew/Form.aspx?pagenb=20918';
}


function onCheckIfSomeoneCommunicates(result) {
try {
        if (result != "") {
            currChatRoomID = result;
            openChatRoomById(currChatRoomID);
            scrptSrvc.ChatService.PopulateRoomMessages(currChatRoomID, currSessionID, OnDisplayMessagesOfRoom);
        }
    }
catch (err) { }
}

function failure(errors, userContext, methodName) {
    //do nothing
    //swallow exceptions
}

function OnShowParticipantNames(result) {
    try {
    var participantNames = document.getElementById('participantNames');
    if (participantNames) {
        participantNames.innerHTML = "with " + result;
    }
    }
catch (err) { }
}

function onBeginChat(result) {
    openChatRoomById(result);
}

function openChatRoomById(id) {
try {
    openMessageWindow();
    scrptSrvc.ChatService.GetParticipatedUsers(id, currSessionID, OnShowParticipantNames);
    scrptSrvc.ChatService.PopulateRoomMessages(currChatRoomID, currSessionID, OnDisplayMessagesOfRoom);
    if (currChatRoomID != "") {
        scrptSrvc.ChatService.InformThatMessagesAreRead(currChatRoomID, currSessionID);
    }
}
catch (err) { }
}

function leaveChatRoom(roomKey) {
    if (confirm("Are you sure that you want to exit the chat room;")) {
        scrptSrvc.ChatService.ChangeWritingUsersStatus(currChatRoomID, currSessionID, false);
        scrptSrvc.ChatService.LeaveChatRoom(currSessionID, roomKey);
        if (roomKey == currChatRoomID) {
            currChatRoomID = "";
        }
        closeMessageWindow();
    }
}

function closeMessageWindow() {
    var dvMessage = document.getElementById('dvMessage');
    if (dvMessage) {
        dvMessage.style.visibility = "hidden";
    }
}

function openMessageWindow() {
    var dvMessage = document.getElementById('dvMessage');
    if (dvMessage) {
        dvMessage.style.visibility = "visible";
    }
}

// This function calls the Web Service method.
function SendMessage() {
    scrptSrvc.ChatService.ChangeWritingUsersStatus(currChatRoomID, currSessionID, false);
    var message = document.getElementById("Message");
    scrptSrvc.ChatService.SendMessage(currChatRoomID, message.value, currSessionID, false, OnDisplayMessagesOfRoom);
    message.value = "";
    message.focus();
}
// This is the callback function that
// processes the Web Service return value.
function OnDisplayMessagesOfRoom(result) {
    var RsltElem = document.getElementById("Results");
    if (RsltElem) {
        RsltElem.innerHTML = result;
    }
}

function OnDisplayUsersWritingStatus(result) {
    var status = document.getElementById("status");
    if (status) {
        status.innerHTML = result;
    }
}
function catchEnterBtn() {
    // Trap Enter(13) except of the combination shift+Enter
    if (typeof window.event != 'undefined') // IE
        document.onkeydown = function() // IE
        {
            retVal = true;
            var t = event.srcElement.type;
            var kc = event.keyCode;
            if (kc == 13 && event.shiftKey != 1) {
                SendMessage();
                retVal = false;
            }
            else if (event.charCode != 0) {//e.charcode = 0 for non letter buttons
                scrptSrvc.ChatService.ChangeWritingUsersStatus(currChatRoomID, currSessionID, true);
            }
            return retVal;
        }
    else
        document.onkeypress = function(e)  // FireFox/Others
        {
            retVal = true;
            var t = e.target.type;
            var kc = e.keyCode;
            if (kc == 13 && e.shiftKey != 1) {
                SendMessage();
                retVal = false;
            }
            else if (e.charCode != 0) {//e.charcode = 0 for non letter buttons
                scrptSrvc.ChatService.ChangeWritingUsersStatus(currChatRoomID, currSessionID, true);
            }
            return retVal;
        }
        
}

function catchEmptyEntry() {
    var message = document.getElementById("Message");
    if (message && message.value == "") {
        scrptSrvc.ChatService.ChangeWritingUsersStatus(currChatRoomID, currSessionID, false);
    }
}


