/* 
	function 			: 	to validate if a field entry is an (signed/non-negative) integer
	created by 		:		ykchoong (ykchoong@hotmail.com)
	modified by		:		jason wong (jasonwong02@hotmail.com)
	date created	:		June 1999
	date modified	:		24 February 2004
	version				:		0.1
	
	syntax				: 	isInt ( str )
										isSignedInt( str )
										isNonNegInt( str )
										- str				(required) indicates the string value 
										
										Example:
											isInt( frm.txtTest.value )
											isSignedInt( frm.txtTest.value )
											isNonNegInt( frm.txtTest.value )
										
										isIntInRange  ( str, startInt, endInt )
										- str				(required) indicates the string value 
										- startInt	(required) indicates the starting number
										- endInt 		(required) indicates the ending number
										
										Example:
											isIntInRange(frm.txtTest.value, 0, 5)
											
										** noNaN ( )
										
										Example:
											onKeyPress="return noNaN()"
											
										** Only works on IE

	prerequisite	:		fn_trim.js
										val_empty.js
*/

var def = false;

function isIntInRange( str, a, b )
{
	if ( isEmpty( str ) )
      { return ( isIntInRange.arguments.length == 3 ? def : (isIntInRange.arguments[3] == true) ); }

   if ( !isInt( str ) ) 
	 	{ return false; }

   var num = parseInt( LTrimCh( str ) );
   return ( num >= a && num <= b );
}

function isInt( str )
{
	str = trim( str );

	if ( isEmpty( str ) )
  	{ return ( isInt.arguments.length == 1 ? def : (isInt.arguments[1] == true) ); }

  for ( var i = 0; i < str.length; i++)
  {
   	var c = str.charAt(i);
    if ( !isChrInt(c) ) return false;
  }
  return true;
}

function isChrInt( c )
{ 
	return ( c >= "0" && c <= "9"); 
}

function noNaN ( )
{
	if (document.all)
		if ( (event.keyCode < 48) || (event.keyCode > 57) )
	  	return false;
}

function isSignedInt( str )
{
	str = trim(str);
  if ( isEmpty(str) )
  {
  	if ( isSignedInt.arguments.length > 1 )
			{ return (isSignedInt.arguments[1] == true); }
    else
			{ return def; }
 	}

  var startPos = 0;
  var secArg = def;
	
  if ( isSignedInt.arguments.length > 1 )
		{ secArg = isSignedInt.arguments[1]; }

  // skip leading + or -
  if ( (str.charAt(0) == "-") || (str.charAt(0) == "+") )
  	{ startPos = 1; }
  
  str = str.substring(startPos, str.length)
  str = trimCh(str);
  return ( isInt(str,secArg) );
}

function isNonNegInt( str )
{
	str = trim(str);
  var secArg = def;
	
 	if (isNonNegInt.arguments.length > 1)
  	{ secArg = isNonNegInt.arguments[1]; }
		
  if ( isEmpty(str) )
		{ return secArg;	}

 	// The next line is a bit byzantine.  What it means is:
  // a) s must be a signed integer, AND
  // b) one of the following must be true:
  // 		i)  s is empty and we are supposed to return true for empty strings
  //    ii) this is a number >= 0
  //	return ( isSignedInteger(s,secondArg) && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
  str = trimCh(str);
  return ( isSignedInt(str,secArg) && parseInt(str)>= 0 );
}