//Name:  VerifyScripts.js
//Purpose: Routines to verify content of certain form fields
//Uses Tables: None
//Dependendant Pages: User Reg Forms, Event Reg Forms, Event Admin Forms
//Date Created: 12.11.02
//Author: Brady P. Merkel
// 
// Use these parameters to verify the data of a form field:
// Email addresses:  onBlur="return VerifyEmailFormat(this)"
// Phone Numbers:  onBlur="return VerifyPhoneFormat(this)"
// Dates:  onBlur="return VerifyDateFormat(this)"
//
//Changes made to page:
//Initials	Date		Description
//bpm		12.11.02	Initial implementation

// Verify a form field has content that matches mailbox@domain.org format
function VerifyEmailFormat(txtfld)
{
	// allow blanks (blank fields verified later)
	if ( txtfld.value.match(/^\s*$/) )
		return true;

	// verify the email address format
	if ( !txtfld.value.match(/\S+\@\S+\.\S+/) )
	{
		alert("Please enter a valid email address (mailbox@domain.org)");
		txtfld.focus();
		return false;
	}
	return true;
}

// Verify a form field has content that matches phone number format of (xxx)xxx-xxxx
function VerifyPhoneFormat(txtfld)
{
	// allow blanks (blank fields verified later)
	if ( txtfld.value.match(/^\s*$/) )
		return true;
	
	// collapse into only digits
	var match = txtfld.value.match(/(\d+)*/g);
	// check if there's only digits
	if ( match )
	{
		var newnum = "";
		for ( var i = 0; i < match.length; ++i)
			newnum = newnum + match[i];
	}

	// remove leading 1, if any
	if ( newnum.substr(0, 1) == '1' )
		newnum = newnum.substr(1);
	
	// Re-format the string
	match = newnum.match(/(\d{3})(\d{3})(\d+)/)
	// make sure we matched the numbers correctly
	if ( match )
		newnum = "(" + match[1] + ")" + match[2] + "-" + match[3];
	// if the new string length is 13, then set the text field's value to it
	if ( newnum.length == 13 )
		txtfld.value = newnum;
	
	// verify the phone format
	if ( !txtfld.value.match(/\(\d{3}\)\d{3}-\d{4}/) )
	{
		alert("Please enter a valid phone number (xxx)xxx-xxxx");
		txtfld.focus();
		return false;
	}
	return true;
}

// Verify a form field has content that matches date format and is a valid date
function VerifyDateFormat(txtfld)
{
	// allow blanks (blank fields verified later)
	if ( txtfld.value.match(/^\s*$/) )
		return true;

	// verify the date format
	if ( !txtfld.value.match(/\d{1,2}\/\d{1,2}\/\d{2,4}/) )
	{
		alert("Please enter the date in the format mm/dd/yy");
		txtfld.focus();
		return false;
	}
	
	// verify the date
	if ( !checkDate(txtfld.value) )
	{
		alert("Please enter a valid date in the format mm/dd/yy");
		txtfld.focus();
		return false;
	}
	return true;
}

// Verify that a date exists
function checkDate( value )
{
	var match = value.match(/^[ ]*[0]?(\d{1,2})\/(\d{1,2})\/(\d{2,4})[ ]*$/);
	// make sure we matched the date format
	if ( match )
	{
		var tmpdate = new Date( match[3], parseInt( match[1]) - 1, match[2] );
		// test result to make sure it matches passed-in values
		if ( tmpdate.getDate() == parseInt(match[2]) && ((tmpdate.getFullYear() % 100) == parseInt(match[3]) || tmpdate.getFullYear() == parseInt(match[3])) && (tmpdate.getMonth()+1) == parseInt(match[1]) )
			return true;
	}
	return false;
}
