// filename:    keypress.js
// author:      Collin Lourie
// credit:		Jeremy Wollard (wollard@flash.net)
// date:        May 21,  2004
// description: 
//
//
//		Provides methods to block or accept key presses (for use in forms)
//		1. include the script
//			<script language="javascript" src="keypress.js"></script>
//		2. add method to event handler
//			onKeypress="acceptNumbers()"
//		
//		inside onKeypress you can use of the shortcut methods, acceptNumbers() or
//		blockSpecialChars(), or you can use of the methods that take arguments:
// 
//		function Keys(rule, key_code1, key_code2, key_code3...)
//			rule is either "block" or "accept" (note: think of accept as "accept only")
//			then you can enter as many key codes as you like.
//
//		function KeyRange(rule, startKey, endKey)
//			rule is either "block" or "accept" (note: think of accept as "accept only")
//			then you can enter as many key codes as you like, where the first number is
//			the inclusive start of the range and the second number in the pair is the
//			inclusive end.
//
//
// tip: visit http://www.parkenet.com/apl/KeyCodes.htm for an easy key code generator
//

// get if the browser is Netscape
var isNS4 = (navigator.appName=="Netscape")?1:0;

// block or accept certain key presses
function Keys()
{
	// get the array of arguments and its length
	var args = Keys.arguments;
	var numArgs = args.length;
	
	// check if the keys should be blocked or accepted
	var block = (args[0] == "block");
	
	// check the keyCode of each argument
	for (var i=1; i < numArgs; i++)
	{
		if(!isNS4)
		{
			if(event.keyCode == args[i])
			{
				if(block)
				{
					event.returnValue = false;
					return;
				}
				else
					return;
			}
			else
			{
				if(!block)
					event.returnValue = false;
			}
		}
		else
		{
			if(event.which == args[i])
			{
				if(block)
					return false;
				else
					return true;
			}
			else
			{
				if(!block)
					return false;
			}
		}
	}
}

// block or accept certain key presses from a range of keys
function KeyRange()
{
	// get the array of arguments and its length
	var args = KeyRange.arguments;
	var numArgs = args.length;
	
	// check if the keys should be blocked or accepted
	var block = (args[0] == "block");
	
	// check the keyCode of each argument
	for (var i=1; i < numArgs; i+=2)
	{
		if(!isNS4)
		{
			if(event.keyCode >= args[i] && event.keyCode <= args[i+1])
			{
				if(block)
				{
					event.returnValue = false;
					return;
				}
				else
					return;
			}
			else
			{
				if(!block)
					event.returnValue = false;
			}
		}
		else
		{
			if(event.which >= args[i] && event.which <= args[i+1])
			{
				if(block)
					return false;
				else
					return true;
			}
			else
			{
				if(!block)
					return false;
			}
		}
	}
}

// shortcut functions for some commonly used key press rules
function acceptNumbers()
{
	KeyRange("accept",48,57);
}

function blockSpecialChars()
{
	KeyRange("block",
			 33,47,
			 58,64,
			 91,96,
			 123,126);
}

//block spaces and special chars
function blockSpecialCharsPlusSpaces()
{
	KeyRange("block",
			 32,47,
			 58,64,
			 91,96,
			 123,126);
}