
//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.
//

//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, CKK 04-27-07 ALL Browsers
function acceptChars(from, to, e)
{	
	//alert(e);
	// for NetScape following gives the event object as per someone on internet but 
	// it did not work -- on some later date
	// arguments.callee.caller.arguments[0];
	var fromCode = from.charCodeAt(0);
	var toCode = to.charCodeAt(0);

	var key = "";
	//valid for all browsers
	if (window.event)
        key = window.event.keyCode;
    else if(e)
        key = e.which;  
    //alert(key);
    //look at the key that came back and return true or false         
    //13=enter 0=(tab,delete,all system keys) 8=backspace
	if (!((key >= fromCode) && (key <= toCode)) && (key != 13) && (key != 0) && (key!= 8)) 
    {
		if(window.event)
		{
			window.event.returnValue = "";
			return false;
		}
		else if(e)
			return false;
    }
    return true;		
}//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, works on all browsers but is not used
function accept1to9(e)
{
	return acceptChars("1", "9", e);
}//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, CKK 05-01-2007 works in all browsers
function accept0to9(e)
{
	return acceptChars("0", "9", e);
}//acceptNumbers

//accept0to9 - SADD! 14-Dec-05
//this can be called for onkeypress in any field. it will accept only 0-9 numerals for those fields
//This will also accept decimals
//	THIS WORKS ONLY IN IE, CKK 04-06-2007 works in netscape now, if event parameter passed
function accept0to9WDec(e)
{
	var from = "0";
	var to = "9";
	
	var fromCode = from.charCodeAt(0);
	var toCode = to.charCodeAt(0);
	var key = "";
	//valid for all browsers
	if (window.event)
        key = window.event.keyCode;
    else if(e)
        key = e.which;  
    //alert(key);
    //look at the key that came back and return true or false         
    //46=. 13=enter 0=(tab,delete,all system keys) 8=backspace
	if (!((key >= fromCode) && (key <= toCode)) && (key != 13) && (key != 46) && (key != 0) && (key!= 8)) 
    {
		if(window.event)
		{
			window.event.returnValue = "";
			return false;
		}
		else if(e)
			return false;
    }
    return true;	
}//accept0to9WDec

function accept0to9WDecSigned()
{
	var from = "0";
	var to = "9";
	
	var fromCode = from.charCodeAt(0);
	var toCode = to.charCodeAt(0);

	if (!((window.event.keyCode >= fromCode) && (window.event.keyCode <= toCode)) && !(window.event.keyCode == 13) && !(window.event.keyCode == 46) && !(window.event.keyCode == 45)) 
    {
		window.event.returnValue = "";
    }		
}//accept0to9WDecSigned

//CKK 04-06-2007 works in netscape now, if event parameter passed
function accept0to9WDecWMinus(obj,e)
{
	var key = "";
	//valid for all browsers
	if (window.event)
        key = window.event.keyCode;
    else if(e)
        key = e.which;  
        
	if ((key == 45))
	{
		if (InStr(obj.value,"-") > -1)
		{
			if(window.event)
			{
				window.event.returnValue = "";
				return false;
			}
			else if(e)
				return false;
		}
	}
	
	var from = "0";
	var to = "9";
	
	var fromCode = from.charCodeAt(0);
	var toCode = to.charCodeAt(0);

	if (!((key >= fromCode) && (key <= toCode)) && (key != 13) && (key != 46) && (key != 45) && (key != 0) && (key!= 8)) 
    {
		if(window.event)
		{
			window.event.returnValue = "";
			return false;
		}
		else if(e)
			return false;
    }
    return true;	
}//accept0to9WDecWMinus

//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, NOTE: function is not used
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) || c == 13)
		;
	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, CKK 04-27-07 all browsers
function acceptAlphaNums(e)
{
	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 key = "";
	//valid for all browsers
	if (window.event)
        key = window.event.keyCode;
    else if(e)
        key = e.which;  
	if( (key >= Code0 && key <= Code9) || (key >= aCode && key <= zCode) || (key >= ACode && key <= ZCode) || key == 13 || key==0 || key==8)
		return true;
	else
	{
		if(window.event)
		{
			window.event.returnValue = "";
			return false;
		}
		else if(e)
			return false;
	}
	return true;
}//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();
	}
}

//CKK 05-17-2007 wrote this function to facilitate the need to only allow system events(tab, delete, enter) and
//the backspace key to work
function acceptSystemKeys(e)
{	
	var key = "";
	//valid for all browsers
	if (window.event)
        key = window.event.keyCode;
    else if(e)
        key = e.which;  
    //alert(key);
    //look at the key that came back and return true or false         
    //0=(tab,delete,all system keys) 8=backspace
	if ((key != 0) && (key!=8)) 
    {
		if(window.event)
			window.event.returnValue = "";
		return false;
    }
    return true;	
}

//AADD:9-9-08:To accept valid characters in contract no field
function acceptContractNo(e)
{	
	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 key = "";
	//valid for all browsers
	if (window.event)
        key = window.event.keyCode;
    else if(e)
        key = e.which;  
	if( (key >= Code0 && key <= Code9) || (key >= aCode && key <= zCode) || (key >= ACode && key <= ZCode) || key == 13 || key==0 || key==8 || key==45 || key==46 || key==95)
		return true;
	else
	{
		if(window.event)
		{
			window.event.returnValue = "";
			return false;
		}
		else if(e)
			return false;
	}
	return true;
}//acceptContractNo