// Javascript validation routines

var validated=true; // global

// call this from onChange
function valid(fld,errm) // varying number of arguments
	{
	var i;
	validated=true;
// scan regular expressions
	for (i=2;i<valid.arguments.length;i++)
	{
	var rx;
	rx=new RegExp(valid.arguments[i]);
	if (rx.exec(fld.value)!=null)
		return true; // OK
	}
// No matches...
	alert(errm);
	fld.focus(); // put focus back
	validate=false; // tell onSubmit if needed
	return false;
	}
// This scans all the onChanged routines
function onSubmit(frm)
	{
// Force validation of all fields
	var l;
	var i;
	validated=true;
	for (i=0;i<frm.elements.length && validated;i++)
		if (frm.elements[i].onChange!=null)
			frm.elements[i].onChange(); // Fake Change
	return validated;
	}
		
		
// Custom Validators
function validPhone(fld)
	{
	rv=valid(fld,"Please enter a valid phone number with area code.","^ *\\(? *([0-9][0-9][0-9]) *\\)? *([0-9][0-9][0-9]) *-? *([0-9][0-9][0-9][0-9]) *$");
	if (rv)
		fld.value="(" + RegExp.$1 + ") " + RegExp.$2 + "-" + RegExp.$3;
	return rv;
	}


