﻿// Thanks to http://aspnet.4guysfromrolla.com/articles/051408-1.aspx for the brains behind this function!
// Returns 1 if Caps Lock is on; 0 if it's off; -1 if we can't tell
function capsLockOn(e)
{
    var myKeyCode = 0;
    var myShiftKey = e.shiftKey;

    if (document.all)
    {
        // Internet Explorer 4+
        myKeyCode = e.keyCode;
    }
    else if (document.getElementById)
    {
        // Mozilla / Opera / etc.
        myKeyCode = e.which;
    }

    if ((myKeyCode >= 65 && myKeyCode <= 90) || (myKeyCode >= 97 && myKeyCode <= 122))
        if (
    // Upper case letters are seen without depressing the Shift key, therefore Caps Lock is on
			((myKeyCode >= 65 && myKeyCode <= 90) && !myShiftKey)

			||

    // Lower case letters are seen while depressing the Shift key, therefore Caps Lock is on
			((myKeyCode >= 97 && myKeyCode <= 122) && myShiftKey)
		)
        return 1;
    else
        return 0;
    else
        return -1;
}

// We assume the TextBox is always immediately followed by the warning div
function showCapsLockWarning(textBox, e)
{
    var itsOn = capsLockOn(e);

    if (itsOn == -1)
        return;

    var warning = textBox.nextSibling;

    if (itsOn == 1)
    {
        if ($(warning).css('display') != 'block')
            $(warning).repositionAndFadeIn('fast');
    }
    else
        $(warning).fadeOut('fast');
}

function hideCapsLockWarning(textBox)
{
    $(textBox.nextSibling).fadeOut('fast');
}
