
/*--- IMPORTANT!  This javascript is tied by ID name to two page items.
**    These are idRawTwabble and idStatusTable.
*/

function initTwabbleSizing ()
{
    var zTwabbleBox     = document.getElementById ("idRawTwabble");

    zTwabbleBox.focus();        //-- user convenience.

    updateStatusDisplays ();    //-- Wipe the "need javascript" messages.

    /*--- Attach the event handlers that will update the count for the user.
        We have to use so many to cover keyboard, mouse click and right-click events
        on both decent and crappy browsers.

        Firefox STILL will not fire correctly on some right-mouse events, like spelling correction.
    */
    zTwabbleBox.onkeyup         = updateStatusDisplays;
    zTwabbleBox.onmousedown     = updateStatusDisplays;
    zTwabbleBox.onmouseup       = updateStatusDisplays;
    zTwabbleBox.oncontextmenu   = updateStatusDisplays;
    zTwabbleBox.onchange        = updateStatusDisplays;
    zTwabbleBox.ondragdrop      = updateStatusDisplays;
    zTwabbleBox.oninput         = updateStatusDisplays;
    //zTwabbleBox.onDOMCharacterDataModified = updateStatusDisplays;

    zTwabbleBox.onkeypress      = processHandyEditorCommands;
}


function updateStatusDisplays ()
{
    var iTargetLength           = 100;
    var zTwabbleBox             = document.getElementById ("idRawTwabble");
    var zStatusTable            = document.getElementById ("idStatusTable");

    var zPayloadCountCell       = zStatusTable.rows[0].cells[1];
    var zStatusDisplayCell      = zStatusTable.rows[1].cells[1];
    var zTotalCountCell         = zStatusTable.rows[2].cells[1];

    var iTabblewRawLength       = zTwabbleBox.value.length;
    var sTwabbleNoWhtSpace      = zTwabbleBox.value.replace (/\s/g,'');
    var iPayloadLength          = sTwabbleNoWhtSpace.length;

    zPayloadCountCell.innerHTML = iPayloadLength;
    zTotalCountCell.innerHTML   = iTabblewRawLength;

    if (iPayloadLength < iTargetLength)
    {
        zStatusDisplayCell.innerHTML             = "Too short";
        zStatusDisplayCell.style.backgroundColor = 'yellow';
    }
    else if (iPayloadLength == iTargetLength)
    {
        zStatusDisplayCell.innerHTML             = "Perfect Length";
        zStatusDisplayCell.style.backgroundColor = '#22ff22';
    }
    else
    {
        zStatusDisplayCell.innerHTML             = "Too long";
        zStatusDisplayCell.style.backgroundColor = 'red';
    }
}


function processHandyEditorCommands (zEvent)
{
    if (!zEvent)    return true;    //-- Not Firefox?  Screw em!

    var zKeyCode    = zEvent.keyCode ? zEvent.keyCode : zEvent.charCode;
    var sKeyChar    = String.fromCharCode (zKeyCode);
    sKeyChar        = sKeyChar.toLowerCase();


    if (zEvent.ctrlKey)
    {
        //-- We'll use ctrl-U and ctrl-L to change case. ---
        //alert ('Pressed: ]' + sKeyChar + '[');  //'

        if (sKeyChar=="l" || sKeyChar=="u")
        {
            //--- Change selected text or first char after curser.
            var zTwabbleBox             = document.getElementById ("idRawTwabble");
            var sTwabbleText            = zTwabbleBox.value;
            var sNewTwabbleText;
            var iTargetStart            = zTwabbleBox.selectionStart;
            var iTargetEnd              = zTwabbleBox.selectionEnd;

            if (iTargetStart == iTargetEnd)     iTargetEnd++;

            sNewTwabbleText             = sTwabbleText.slice (iTargetStart, iTargetEnd);

            if (sKeyChar=="l")
                sNewTwabbleText         = sNewTwabbleText.toLowerCase();
            else
                sNewTwabbleText         = sNewTwabbleText.toUpperCase();

            sNewTwabbleText             = sTwabbleText.slice (0, iTargetStart) + sNewTwabbleText + sTwabbleText.slice (iTargetEnd);

            //alert (sNewTwabbleText);
            //-- After using spelling corrector, this gets buggered, hence the multiple sets.
            zTwabbleBox.value           = sNewTwabbleText;
            zTwabbleBox.innerHTML       = sNewTwabbleText;

            zTwabbleBox.selectionStart  = iTargetStart;     //-- Have to reset since we repasted the text.
            zTwabbleBox.selectionEnd    = iTargetEnd;

            return false;   //-- Stop the global ctrl keys from messing with us.
        }
    }

    return true;
}
