
//keypress.js 
//	Copyright (c) 2003 - 2004 HTC Global Services
//	This holds all the methods to validate the key pressed in a text field. These methods can be 
//  called on onKeyPress event in text fields.
//

var lastKeyCode = 0;
function keypressHandler(e)
{	
	if(!document.all)
		lastKeyCode = e.which;
	else
		lastKeyCode = window.event.keyCode;
}
	
if(!document.all)
	window.captureEvents(Event.KEYPRESS);

window.onkeypress = keypressHandler;

//asciiCode - PADD! 12-Oct-03
//	@c - the character e.g. "a" etc.
//	It returns the ascii code for passed in character 
//	if string is passed it returns code of the first character in string
//
function asciiCode(c)
{
	return c.charCodeAt(0);
}

function islower(c)
{
	return (c >= asciiCode("a") && c <= asciiCode("z"));
}
 
function isupper(c)
{
	return (c >= asciiCode("A") && c <= asciiCode("Z"));
}

function isalpha(c)
{
	 return (isupper(c) || islower(c));
}

function isnum(c)
{
	return (c >= asciiCode("0") && c <= asciiCode("9"));
}

function isalnum(c)
{
	return (isalpha(c) || isnum(c));
}

//acceptChars - PADD! 12-Oct-03
//  @from - characters in terms of string e.g. "a"
//  @to   - characters in terms of string e.g. "z"
//	this can be called for onkeypress in any field. it will accept characters in range [from, to]
//	THIS WORKS ONLY IN IE
function acceptChars(from, to)
{	
	var fromCode = from.charCodeAt(0);
	var toCode = to.charCodeAt(0);
	
	if (!((window.event.keyCode >= fromCode) && (window.event.keyCode <= toCode))) 
    {
		window.event.returnValue = "";
    }		
}//acceptChars

//accept1to9 - PADD! 12-Oct-03
//	this can be called for onkeypress in any field. it will accept only 1-9 numerals for those fields
//	THIS WORKS ONLY IN IE
function accept1to9()
{
	acceptChars("1", "9");
}//acceptNumbers

//accept0to9 - PADD! 12-Oct-03
//	this can be called for onkeypress in any field. it will accept only 0-9 numerals for those fields
//	THIS WORKS ONLY IN IE
function accept0to9()
{
	acceptChars("0", "9");
}//acceptNumbers

//acceptAlphabets - PADD! 12-Oct-03
//	this can be called for onkeypress in any field. it will accept only alphabets for those fields
//	THIS WORKS ONLY IN IE
function acceptAlphabets()
{
	var aCode = asciiCode("a");
	var zCode = asciiCode("z");
	var ACode = asciiCode("A");
	var ZCode = asciiCode("Z");
	
	var c = window.event.keyCode;
	if( (c >= aCode && c <= zCode) || (c >= ACode && c <= ZCode))
		;
	else
		window.event.returnValue = "";
}//acceptAlphabets

//acceptAlphaNums - PADD! 12-Oct-03
//	this can be called for onkeypress in any field. it will accept only alphanumerals for those fields
//	THIS WORKS ONLY IN IE
function acceptAlphaNums()
{
	var aCode = asciiCode("a");
	var zCode = asciiCode("z");
	var ACode = asciiCode("A");
	var ZCode = asciiCode("Z");
	var Code0 = asciiCode("0");
	var Code9 = asciiCode("9");
	
	var c = window.event.keyCode;
	if( (c >= Code0 && c <= Code9) || (c >= aCode && c <= zCode) || (c >= ACode && c <= ZCode))
		;
	else
		window.event.returnValue = "";		
}//acceptAlphaNums

//acceptSegCode - PADD! 12-Oct-03
//	@codetype - FND/CLS etc
//	following is called on keypress of the segment codes. It will accept 
//	only numerals or alphabets according to the allowed datatype for that 
//	segment code
//	THIS WORKS ONLY IN IE
function acceptSegCode(codetype)
{
	//NOTE: THIS IS NO MORE BEING CALLED...
	return;
	
	//initialize the arrays - it happens only once
	initSegCodes();
						
	if (typeof globalCodeDataType != "undefined" && (globalCodeDataType == "A" || globalCodeDataType == "N" || globalCodeDataType == "X"))
		datatype = globalCodeDataType;
	else
		datatype = arrDataTypes[codetype];

	switch(datatype)
	{
		case "A":
			acceptAlphabets();
			break;
		case "N":
			accept0to9();
			break;
		case "X":
			acceptAlphaNums();
			break;
	}//switch		
}//acceptSegCode


//acceptUserName - PADD! 13-Oct-03
//	following is called for onKeyPress of user name. it accepts only
//	alphabets for a valid user name
//  THIS WORKS ONLY IN IE
//
function acceptUserName()
{
	//NOTE: THIS IS NO MORE BEING CALLED...
	return;

	var c = window.event.keyCode;

	if (isalnum(c) || c == asciiCode("_"))
		;
	else	
		window.event.returnValue = "";
}//acceptUserName


//accept text not more than than maxlength
//used especially for textarea.
function textCounter(field, maxlimit )
{
	if (field.value.length >= maxlimit)
	{
		field.value = field.value.substring(0, maxlimit);
		field.blur();
	}
}
