var ChatPageHandler = new CChatPageHandler();
function CChatPageHandler()
{
    // =============================================
    // PREPARE INTERNAL VALUES
    // =============================================
    var strLblConnId = "sectChatConnectInfo";

    var strTxtEntryId = "iptChatEntry";
    var strTxtOutputId = "sectChatDisplay";

    var strBtnConnectId = "btnChatConnect";
    var strBtnTerminateId = "btnChatTerminate";
    var strBtnPostId = "btnChatSend";

    var Chat_Interface = new CChat_Interface(UpdateConnectionState, AddToOutput);

    // =============================================
    // INITIALISE PAGE
    // =============================================
    this.Init = Init;	
    function Init()
    {
        // If press [Enter] in text entry, send entered content
        var objEle = document.getElementById(strTxtEntryId);
        if (!objEle)
            return;
        objEle.onkeypress = PostOnEnter;
    
        // When user leaves / refreshes page, terminate any connection
        var _fncOld = window.onbeforeunload;
        window.onbeforeunload = function()
        {
            HardTerminate();
            try
            {
                if (typeof(_fncOld) != "undefined")
                    _fncOld.apply(this, arguments);
            } catch(e) {}
        }
    }

    function PostOnEnter(event)
    {
        // Ensure reference to event set up (Internet Explorer)
        if (typeof(event) == "undefined")
            event = window.event;
        
        // If pressed [Enter], send content and swallow event
        if (event.keyCode == 13)
        {
            PushText();
            event.cancelBubble = true;
            if (event.stopPropagation)
    	        event.stopPropagation();
            return false;
        }
    }

    // =============================================
    // INITIATE / CANCEL CONNECTION
    // =============================================
    this.Connect = Connect;
    function Connect()
    {
        ClearOutput();
        AddToOutput("<b>Connecting to Comtek Server..</b><br>(A representative will be with you soon).");
        Chat_Interface.GetConnection();
    }

    this.Terminate = Terminate;
    function Terminate(bSilent)
    {
        if (!ObjectDetection.IsBoolean(bSilent))
            bSilent = false;    
        if (!bSilent)
            AddToOutput("<b>Terminating Connection..</b>");
        Chat_Interface.Terminate();
    }

    this.HardTerminate = HardTerminate;
    function HardTerminate()
    {
        Chat_Interface.HardTerminate();
    }

    var bConnected = false;
    function UpdateConnectionState(strId, bValid, bAuthorised)
    {
        // Update connection summary display
        var strConnDisplay;
        if ((bValid) && (!bAuthorised))
            strConnDisplay = "Connecting to Server.. " + strId;
        else if ((bValid) && (bAuthorised))
            strConnDisplay = "Connected..";
        else
            strConnDisplay = "Not connected.";
        var objEleConnSummary = document.getElementById(strLblConnId);
        var strCurrentDisplay = objEleConnSummary.innerHTML;
        if (strCurrentDisplay != strConnDisplay)
            objEleConnSummary.innerHTML = strConnDisplay;
        
        // Enable / disable connection controls
        var objEleBtnConnect = document.getElementById(strBtnConnectId);
        objEleBtnConnect.disabled = (bValid || bAuthorised);
        var objEleBtnTerminate = document.getElementById(strBtnTerminateId);
        objEleBtnTerminate.disabled = (strId == null);
        
        // Enable / disable entry controls
        var objEleTxtEntry = document.getElementById(strTxtEntryId);
        objEleTxtEntry.disabled = !bAuthorised;
        var objEleBtnEntry = document.getElementById(strBtnPostId);
        objEleBtnEntry.disabled = !bAuthorised;
    
        // If was previously connected, but now not, add an entry
        // in the connection output box to indicate this to user
        if ((bConnected)  // If was connected before this..
         && (!bValid))    // ..but aren't any more
            AddToOutput("<b>Connection Terminated.</b>");
        
        // Update internal currently-connected status
        bConnected = bValid;
    }

    // =============================================
    // DISPLAY CONTENT
    // =============================================
    function GetOutputElement()
    {
        return document.getElementById(strTxtOutputId);
    }

    function ClearOutput()
    {
        var objEle = GetOutputElement();
        objEle.innerHTML = "";
    }

    function AddToOutput(strContent)
    {
        // First segment of content should be the poster's name,
        // which we want to make bold (and no HTML tags are allowed
        // in content, so we have to do it here)
        var intDelim = strContent.indexOf(":");
        if (intDelim != -1)
            strContent = "<b>" + strContent.substring(0, intDelim) + "</b>"
                       + strContent.substring(intDelim, strContent.length);

        // Display content in output element
        var objEle = GetOutputElement();
        objEle.innerHTML += strContent + "<br /><br />";
    
        // Scroll to bottom of content
        objEle.scrollTop = objEle.scrollHeight;    
    }

    // =============================================
    // POST CONTENT
    // =============================================
    this.PushText = PushText;
    function PushText()
    {
        var objEle = GetInputElement();
        var strText = objEle.value;
        Chat_Interface.PushText(strText);
        objEle.value = "";
    }

    function GetInputElement()
    {
        return document.getElementById(strTxtEntryId);
    }
}
