// regFromValidate.js
/* check that a given field is a numeric string which can contain spaces as well as digits */
formUtils = new Object;

formUtils.validatePhone = function(phoneId, e)
{
	var phone;
	
	if ( typeof(phoneId) == 'string' )
	{
		phone = document.getElementById(phoneId);
		if ( phone == null )
			throw new Error('phoneId tag not found!');
	}
	else
		phone = phoneId;
		
	if ( !/^ *\d+[\d ]*$/.test(phone.value) )
	{
		alert('Phone field may contain number and spaces only!');
		e.cancelable ? e.preventDefault() : e.returnValue = false;
	}
}

/*
*	this function check the email address for correcness 
*	 return flase if fails else true
*/
formUtils.emailCheck = function(emailId, e)
{
	var email;
	
	if ( typeof(emailId) == 'string' )
	{
		email = document.getElementById(emailId);
		if ( email == null )
			throw new Error('emailId tag not found!');
	}
	else
		email = emailId;
		
	if  ( !/^.+@.+[\.].+$/.test(email.value) )
	{
		alert('Invalid Email address!');
		e.cancelable ? e.preventDefault() : e.returnValue = false;
	}
}

formUtils.matchFields = function(fld1, fld2, e)
{
	var field1, field2;
	
	if ( typeof(fld1) == 'string' )
	{
		field1 = document.getElementById(fld1);
		if ( field1 == null )
			throw new Error('field1 tag not found!');
	}
	else
		field1 = fld1;
			
	if ( typeof(fld2) == 'string' )
	{
		field2 = document.getElementById(fld2);
		if ( field2 == null )
			throw new Error('field2 tag not found!');
	}
	else
		field2 = fld2;
		
	if  ( field1.value != field2.value )
	{
		alert('Fields ' + field1.id + ' and ' + field2.id + ' do not Match!');
		e.cancelable ? e.preventDefault() : e.returnValue = false;
	}
}

formUtils.toLower = function(fld)
{
	var field;
	
	if ( typeof(fld) == 'string' )
	{
		field = document.getElementById(fld);
		if ( field1 == null )
			throw new Error('field tag not found!');
	}
	else
		field = fld;
		
	var fldStr = field.value.toLowerCase();
	alert(fldStr);
	field.value = fldStr;
	alert(field.value);
}
