/*
*	Data validation scripts
*
*	Copyright(C) 2005, Eddie Teo (eddieteo@online.com.sg). All rights reserved.
*
*	You are grant a non-exclusive, royalty free, license to use, modify or
*	redistribute this code in any form, provided that this copyright notice
*	and license appear on all copies of the code.
*/

//////////////////////////////////////////////////////////////////////
//	Is string empty?                                            //
//	Return:                                                     //
//		true if empty, false otherwise                      //
//////////////////////////////////////////////////////////////////////
function IsEmpty( s )
{
	return ( s.length == 0 );
}

//
//	Is string not empty?
//	Return:
//		true if not empty, false otherwise
//
function IsNotEmpty( s )
{
	return ( s.length > 0 );
}

//////////////////////////////////////////////////////////////////////
//	Is string of digits?                                        //
//	Return:                                                     //
//		true if valid, false otherwise                      //
//////////////////////////////////////////////////////////////////////
function IsDigit( s )
{
	var c, i, len = s.length;
	for( i = 0; i < len; i++ )
	{
		c = s.charAt(i);
		if( (c < "0") || (c > "9") ) return false;
	}
	return true;
}

///////////////////////////////////////////////////////////////////////////
//	Is check box checked?
//	Return:
//		true if checked, false otherwise
//
function IsChecked( s )
{
	return ( s.checked );
}

//
//	Is string an integer?
//	Return:
//		true if valid, false otherwise
//
function IsInteger( s )
{
	var c, i, len = s.length;

	c = s.charAt(i);							// Skip number sign if present
	i = ( (c != "-") && (c != "+") ) ? 0 : 1;

	while( i < len )
	{
		c = s.charAt(i);
		if( (c < "0") || (c > "9") ) return false;
		i++;
	}
	return true;
}

//
//	Is string a number?
//	Return:
//		true if valid, false otherwise
//	Remarks:
//		Scientific notation number are not accepted yet
//
function IsNumber( s )
{
	var c, i, len = s.length;

	c = s.charAt(i);							// Skip number sign if present
	i = ( (c != "-") && (c != "+") ) ? 0 : 1;

	while( i < len )
	{
		c = s.charAt(i);
		if( (( c < "0" ) || ( c > "9" )) && (( c != "." ) || ( s.indexOf( ".", i+1 ) != -1 )) ) return false;
		i++;
	}
	return true;
}

//
//	Is string a boolean?
//	Return:
//		true if valid, false otherwise
//
function IsBoolean( s )
{
	return ( (s == "true") || (s == "false") ) ? true : false;
}

//
//	Is string an e-mail address?
//	Return:
//		true if valid, false otherwise
//
function IsEMailAddr( s )
{
	var i = s.indexOf( "@" );
	return ( (i > 0) && (s.indexOf( ".", i+1 ) > 0) ) ? true : false;
}

//
//	Is string a phone number?
//	Return:
//		true if valid, false otherwise
//	Remarks:
//		Brackets is accepted, but no test for pair
//
function IsPhoneNumber( s )
{
	var c, i, len = s.length;

	i = (s.charAt(i) != "+") ? 0 : 1;
	while( i < len )
	{
		c = s.charAt(i);
		if( (( c < "0" ) || ( c > "9" )) && (" -()\t\n\r".indexOf( c ) < 0) ) return false;
		i++;
	}
	return true;
}

//
//	Is string a credit card number?
//	Return:
//		true if valid, false otherwise
//	Remarks:
//		Number is verify with the Luhn Mod-10 test
//
function IsCreditCard( s )
{
	var i = s.length;
	if( i > 19 ) return false;				// Skip number with more than 19 digits

	var c, v, m = 1, sum = 0;
	while( i > 0 )
	{
		i--;
		c = s.charAt(i);
		if( (c < "0") || (c > "9") )
		{
			if( " \t".indexOf( c ) < 0 ) return false;
		}
		else
		{
			v = parseInt( c ) * m;
			sum += (v < 10) ? v : (v % 10) + 1;
			m = (m == 1) ? 2 : 1;
		}
	}
	return (( sum % 10 ) == 0) ? true : false;
}
