// Validate data in an input box.
// Author: Mark K Mueller
// Date:   11/10/2005
//
// The Validate function is intended to be called by the "onblur" event of an input box.
// The first argument is the input box object. The second argument is the expected data type.
// When the "onblur" event is triggered, the value of the text box will be matched against
// the appropriate regular expression.  If it fails the test, an error message will be
// immediately displayed in an alert dialog and the contents will be selected.  If the text 
// box has a title, it will be used as part of the error message.  If the datatype is omitted,
// the default format will be used.  When a value range datatype is used, the min and max
// arguments must be supplied. The Validate function will return true if the test passes.
// Leading and trailing white space will be trimmed.
//
// Functions:
//	Validate( object, datatype, min, max )
//
// Usage: <input onBlur="Validate(this)" title="Company name" name="co">
//	  <input onBlur="Validate(this,'phone')" title="Phone number" name="ph">
//	  <input onBlur="Validate(this,'irange',1,12)" title="Month" name="mo">
//
// Data types and formats:
// default	
// int		Integer			0-9, leading negative(-)
// uint		Unsigned integer	0-9
// float	Floating point		0-9, period(.), commas(,), leading negative(-)
// alpha	Alpha characters	a-z, space
// address 	Address			a-z, 0-9, hyphen(-), commas(,), period(.), hyphen(-), space
// phone	Phone number		nnn-nnnn
// ssn		Social Security No.	nnn-nn-nnnn
// email	Email address		address@address
// money	Money			0-9, period(.), commas(,)
// date		Date string		mm/dd/yyyy
// irange	Integer range		Any positive integer within specified range
// int_list	Integer list		A comma or space delimited list of integers
//
var regexp = new Object;
var regmsg = new Object;
regexp['default']= /^[a-z A-Z\d\.,-]*$/;
regmsg['default']= '"A-Z, 0-9, space, hyphen, period, comma"';
regexp['int']	= /^-{0,1}\d+$/;
regmsg['int']	= 'integer';
regexp['uint']	= /^\d+$/;
regmsg['uint']	= 'positive integer';
regexp['float']	= /^-{0,1}[\d,]*\.{0,1}\d+$/;
regmsg['float']	= 'number';
regexp['money']	= /^[\d,]*\.{0,1}\d{0,2}$/;
regmsg['money']	= 'monetary value';
regexp['alpha']	= /^[a-z A-Z]+$/;
regmsg['alpha']	= 'alpha characters only';
regexp['address']= /^[a-z A-Z\d\.,-]*$/;
regmsg['address']= '"A-Z, 0-9, space, hyphen, period, comma"';
regexp['zip']	= /^\d{5}$|^\d{5}\-\d{4}$/;
regmsg['zip']	= '"#####" or "#####-####"';
regexp['phone']	= /^\d{3}\-\d{4}$|^\d{3}\-\d{3}\-\d{4}$/;
regmsg['phone']	= '"###-####" or "###-###-####"';
regexp['ssn']	= /^\d{3}-\d{2}-\d{4}$/;
regmsg['ssn']	= '"###-##-####"';
regexp['email']	= /^[a-zA-Z\d\.-]{2,20}\@[a-zA-Z\d\.-]+\.[a-zA-Z\d]{1,4}$/;
regmsg['email']	= 'email address';
regexp['irange']= /^\d+$/;		// positive integer
regmsg['irange']= 'positive integer';
regexp['vrange']= /^[\d,]*\.{0,1}\d+$/;	// positive floating point
regmsg['vrange']= 'positive number';
regexp['int_list']= /^[\d, ]+$/;	// list of integers
regmsg['int_list']= '0-9, space, comma';
regexp['date']	= /^\d{1,2}\/\d{1,2}\/\d{4}$/;
regmsg['date']	= 'mm/dd/yyyy';
regexp['acctno']= /^[\d\-]+$/;
regmsg['acctno']= '"#########" or "#########-##"';
//
function Validate ( obj, type, min, max ) {
	if( obj.value == '' | obj.value == undefined ) return true;
	if( type == '' | type == undefined ) type = 'default';
	if( regexp[type] == undefined ) return false;
	obj.value = Trim(obj.value);
	if(obj.value == '') return true;
	var title = (obj.title)? 'at ' + obj.title : '';
	if ( !regexp[type].test(obj.value) ) {
		alert( 'Invalid data ' + title + '\n\nExpecting ' + regmsg[type] );
		obj.select();
		return false;
	}
	var range = /range$/;
	if( range.test(type) ){
		if( obj.value < min | obj.value > max ){
			obj.select();
			alert(  'Invalid data ' + title + '\n\nExpecting value range ' + min + ' - ' + max );
			return false;
		}
	}
	return true;
}
function Trim (str) {
	// Trim leading and trailing white space
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
// END OF FILE //
