﻿// 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, anchorToCorner, insideX, insideY, xOffset, yOffset)
{
	var itsOn = capsLockOn(e);
	
	if (itsOn == -1)
		return;
	
	var warning = textBox.nextSibling;
	
	if (itsOn == 1)
	{
		warning.style.display = 'block';
		positionElement(warning, textBox, anchorToCorner, insideX, insideY, xOffset, yOffset, -99999, -99999);
	}
	else
		warning.style.display = 'none';
}

function hideCapsLockWarning(textBox)
{
	textBox.nextSibling.style.display = 'none';
}
