/****************************************************************************************
     Script to detect if Caps Lock is engaged when a key is pressed in a text input
          This script based on v2.0.0 written by Mark Wilton-Jones, 1/11/2003
*****************************************************************************************
Please see http://www.howtocreate.co.uk/jslibs/ for details and a demo of this script
Please see http://www.howtocreate.co.uk/jslibs/termsOfUse.html for terms of use

JavaScript offers no way to check the state of the Caps Lock, so this script checks the
letters pressed and compares them with the SHIFT key state
*/

var fieldObj = null;

function capsDetect( e ) {  
	if( !e ) { e = window.event; } if( !e ) { MWJ_say_Caps( false ); return; }
	//what (case sensitive in good browsers) key was pressed
	var theKey = e.which ? e.which : ( e.keyCode ? e.keyCode : ( e.charCode ? e.charCode : 0 ) );
	//was the shift key was pressed
	var theShift = e.shiftKey || ( e.modifiers && ( e.modifiers & 4 ) ); //bitWise AND
	//if upper case, check if shift is not pressed. if lower case, check if shift is pressed
	MWJ_say_Caps( ( theKey > 64 && theKey < 91 && !theShift ) || ( theKey > 96 && theKey < 123 && theShift ) );
}

var hasSeenError = false;

function MWJ_say_Caps( oC ) {
  //alert("MWJ_say_Caps(): " + hasSeenError);
  if( oC ) { 
    showCapsError();
  } else {
    hideCapsError();
  }
}

function positionCapsError(fieldObject){
  fieldObj = fieldObject;
  document.getElementById("capsError").style.left = getPageX(fieldObject);
  document.getElementById("capsError").style.top = (getPageY(fieldObject) + 25) ;
}

function showCapsError() {
  if(!hasSeenError) {
    document.getElementById("capsError").style.visibility = "visible";
  }
}

function hideCapsError() {
  document.getElementById("capsError").style.visibility = "hidden";
}

function seenError() {
  fieldObj.focus();
  hasSeenError = true;
  hideCapsError();
}

function getPageX(o) { 
  var x=0; 
  while(eval(o)) { 
    x+=o.offsetLeft;
    o=o.offsetParent;
  } 
  return x; 
}

function getPageY(o) { 
  var y=0; 
  while(eval(o)) { 
    y+=o.offsetTop; 
    o=o.offsetParent; 
  } 
  return y; 
}
