﻿
//Load jQuery dynamically
if (!(window.jQuery)) {
    var s = document.createElement('script');
    s.setAttribute('src', '/js/jquery.min.js');
    s.setAttribute('type', 'text/javascript');
    document.getElementsByTagName('head')[0].appendChild(s);
}

var ChatPolling = false;
var LastChatCaller = null;
var ChatTimerIntervalID = null;


// global request and XML document objects
var reqChat;

function loadChatXMLDoc(url, func) {
    if (window.XMLHttpRequest) {
        reqChat = new XMLHttpRequest();
        reqChat.onreadystatechange = func;
        reqChat.open("GET", url, true);
        reqChat.send();
    } else if (window.ActiveXObject) {
        isIE = true;
        reqChat = new ActiveXObject("Microsoft.XMLHTTP");
        if (reqChat) {
            reqChat.onreadystatechange = func;
            reqChat.open("GET", url, true);
            reqChat.send();
        }
    }
}

function ChatAjaxUpdate(Caller, Message) {
    if (Caller == null) return;
    if (Caller == "") return;

    var url = "/PageCode/Chat/Ajax.aspx?CallingControlName=" + Caller + "&Message=" + Message;

    loadChatXMLDoc(url, ChatAjaxCallBack);
}

function ChatAjaxCallBack() {
    if (reqChat.readyState == 4) {
        // only if "OK"
        if (reqChat.status == 200) {
            ChatAjaxApply();
        } else {
            //alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }

    ChatPolling = false;
}

function ChatPoller() {
    if (ChatPolling != false) return; // Don't overrun

    ChatPolling = true; // We're polling now

    ChatAjaxUpdate("poller", "");
}

function ChatStartPoller() {
    if (ChatTimerIntervalID != null) clearInterval(ChatTimerIntervalID);

    ChatTimerIntervalID = setInterval(ChatPoller, 10 * 1000); // Every 10 seconds 
}

function ChatStopPoller() {
    if (ChatTimerIntervalID != null) clearInterval(ChatTimerIntervalID);
    ChatTimerIntervalID = null;
}

var _PolicyGUID = "";

function ChatAjaxApply() {
    if (ChatTimerIntervalID == null) return;
    if (reqChat == null) return;
    if (reqChat.responseXML == null) return;

    var DocumentElement = reqChat.responseXML.documentElement;
    if (DocumentElement == null) return;
    if (DocumentElement.childNodes == null) return;

    for (var i = 0; i < DocumentElement.childNodes.length; i++) {
        var CurrentNode = DocumentElement.childNodes[i];

        if (CurrentNode.nodeName == "pg") {
            _PolicyGUID = $(CurrentNode).text();
        }
        else if (CurrentNode.nodeName == "action") {
            if ($(CurrentNode).text() == "start_chat") {
                if (_PolicyGUID != "") {
                    //Does the user want to chat?
                    ShowChatConfirm(_PolicyGUID);
                    ChatStopPoller();
                }
            }
        }
        else if (CurrentNode.nodeName == "TargetControl") {

        }
    }
}

// Called when a chat invite is sent.  
function ShowChatConfirm(PolicyGUID) {
    if (document.getElementById('chatConfirmationDiv')) {
        document.getElementById('chatConfirmationDiv').style.display = 'block';
    }
    else {
        var winparams = 'width=600,height=405,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes';
        var s = '';
        s += '<p>Would you like to <b>chat online</b> with a<br />customer service representative?</p>';
        s += '<p><input type="button" class="nicebutton nicesimple nicegreen" onclick="ChatInvitationResponse(true); ';
        s += ' window.open(ChatWindowUrl(\'' + PolicyGUID + '\',false),ChatWindowTitle(\'' + PolicyGUID + '\',false),\'' + winparams + '\'); ';
        s += ' HideChatConfirm(); return false;" value="Yes" />';

        s += ' <input type="button" class="nicebutton nicesimple nicered" value="No" ';
        s += ' onclick="ChatInvitationResponse(false); HideChatConfirm(); return false;" /></p></div>';
        s += '<div style="cursor:pointer;font-size:16px;font-weight:bold;position:absolute;top:0;right:0;padding:4px;" onclick="ChatInvitationResponse(false); HideChatConfirm();">&times;</div>';

        var ChatDiv = document.createElement('div');
        ChatDiv.setAttribute('id', 'chatConfirmationDiv');
        //ChatDiv.setAttribute('style', 'background-color:white;border:solid 2px grey;padding:10px;position:fixed;top:200px;left:200px;width:350px;text-align:center;display:block;color:#000000;font-size:14px;');
        ChatDiv.innerHTML = s;

        document.body.appendChild(ChatDiv);
    }
}

function HideChatConfirm() {
    document.getElementById('chatConfirmationDiv').style.display = 'none';
}

function ChatInvitationResponse(WantsToChat) {
    if (WantsToChat)
        ChatAjaxUpdate("prompt", "true");
    else
        ChatAjaxUpdate("prompt", "false");
}

function ChatWindow(PolicyGUID, FromConsole) {
    window.open(ChatWindowUrl(PolicyGUID, FromConsole), ChatWindowTitle(PolicyGUID, FromConsole), 'width=600,height=405,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes');
}

function ChatWindowUrl(PolicyGUID, FromConsole) {
    var windowurl = '/PageCode/Chat/Window.aspx?GUID=' + PolicyGUID;
    if (FromConsole)
        windowurl += '&console=1';
    return windowurl;
}

function ChatWindowTitle(PolicyGUID, FromConsole) {
    return 'Policy_' + PolicyGUID.replace(/-/g, '_');
}
