// Which button was clicked?
var button;

var MAX_CHARS = 180;
var option1 = -1;

//-----------------------------------------------------------------------------
function myForm_OnSubmit()
//-----------------------------------------------------------------------------
{
	if ( button == "<< Back" ) return true;

	if ( f.pageName.value == undefined ||
	     f.pageName.value == 1 )
	{
    // Validate that a proper dollar amount was given
		if ( isNaN( Number( f.txtAmount.value ) ) || f.txtAmount.value == "" )
		{
			alert( "Please specify a dollar amount." );
			return false;
		}
		else if ( Number( f.txtAmount.value ) < 20 ||
		          Number( f.txtAmount.value ) > 250 )
		{
			alert( "Please specify an amount between $20 and $250." );
			return false;		
		}
		else if ( Number( f.txtAmount.value ) !=
		          Math.floor( Number( f.txtAmount.value ) ) )
		{
			alert( "Please specify a whole dollar amount without any pennies." );
			return false;
			
		} // end if
		
		// Ensure the date is not in the past
		var today = new Date();
		var sendDate = new Date( f.txtYear.options[f.txtYear.selectedIndex].text, f.txtMonth.options[f.txtMonth.selectedIndex].value - 1, f.txtDay.options[f.txtDay.selectedIndex].text );

		if ( sendDate < today && !( ( f.txtYear.options[f.txtYear.selectedIndex].text == today.getYear() || f.txtYear.options[f.txtYear.selectedIndex].text == today.getYear() + 1900 ) && f.txtMonth.options[f.txtMonth.selectedIndex].value == ( today.getMonth() + 1 ) && f.txtDay.options[f.txtDay.selectedIndex].text == today.getDate() ) )
		{
			alert( "The date you have selected is in the past according to your computer's clock." );
			return false;
		}
		
		errorStr = "";
		if ( f.txtRecipientName.value == "" ) errorStr = err( errorStr, "- The recipient's name" );
		if ( f.txtRecipientAddress.value == "" ) errorStr = err( errorStr, "- The recipient's address" );
		if ( f.txtRecipientCity.value == "" ) errorStr = err( errorStr, "- The recipient's city" );
		if ( f.txtRecipientProvince.value == "" ) errorStr = err( errorStr, "- The recipient's province" );

		recipientPostalCode = validatePostalCode( f.txtRecipientPostalCode.value );
		if ( recipientPostalCode == "(invalid)" && f.txtRecipientPostalCode.value != "" )
		{
			errorStr = err( errorStr, "- A proper postal code. e.g. N3J 7L5" );
		}
		else if ( f.txtRecipientPostalCode.value == "" )
		{
			errorStr = err( errorStr, "- The recipient's postal code" );
		}

		if ( f.txtPersonalizedMessage.value.length > MAX_CHARS ) errorStr = err( errorStr, "- A personalized message that is at most " + MAX_CHARS + " characters" );

		//senderName = validateName( f.txtSenderName.value );
		senderEmailAddress = validateEmailAddress( f.txtSenderEmailAddress.value );
		senderPhoneNumber = validatePhoneNumber( f.txtSenderPhoneNumber.value );

		if ( f.txtSenderName.value == "" ) errorStr = err( errorStr, "- The sender's name" );
		if ( f.txtSenderEmailAddress.value != "" && senderEmailAddress == "(invalid)" ) errorStr = err( errorStr, "- A proper sender's e-mail address. e.g. adele.twaithe@rogers.ca" );

		if ( senderPhoneNumber == "(invalid)" && f.txtSenderPhoneNumber.value != "" )
		{
			 errorStr = err( errorStr, "- A proper sender's phone number. e.g. 012-345-6789" );
		}
		else if ( f.txtSenderPhoneNumber.value == "" )
		{
			errorStr = err( errorStr, "- The sender's phone number" );
		}

		if ( errorStr != "" ) {
			errorStr = "Please enter:\n\n" + errorStr;
			alert( errorStr );
			return false;
		}

		//f.txtSenderName.value = senderName;
		//f.txtSenderEmailAddress.value = senderEmailAddress;
		f.txtSenderPhoneNumber.value = senderPhoneNumber;

		f.txtRecipientPostalCode.value = recipientPostalCode;

	}
	else if ( f.pageName.value == "2" )
	{
		//return true;

		errorStr = "";
		if ( f.txtNameOnCard.value == "" ) errorStr = err( errorStr, "- Your name as it appears on your credit card" );
		if ( f.txtCardNumber.value == "" ) errorStr = err( errorStr, "- Your credit card number" );
		if ( f.txtCreditCardSecurityCode.value == "" ) errorStr = err( errorStr, "- Your credit card security code" );
		if ( f.txtExpiryMonth.value == "" ) errorStr = err( errorStr, "- The expiry month of your credit card" );
		if ( f.txtExpiryYear.value == "" ) errorStr = err( errorStr, "- The expiry month of your credit card" );

		if ( errorStr != "" ) {
			errorStr = "Please enter:\n\n" + errorStr;
			alert( errorStr );
			return false;
		}

		//--------------------------------------------------------------
		// Simple card number validation
		//--------------------------------------------------------------
		str = f.txtCardNumber.value;
		str = str.replace( /-/g, "" );
		str = str.replace( / /g, "" );
		if ( str.length != 16 || isNaN( Number( str ) ) )
		{
			alert( "Please enter your credit card number exactly." );
			return false;
		} // end if: Invalid credit card number?

		//--------------------------------------------------------------
		// Expiry month/year validation
		//--------------------------------------------------------------

		if ( isNaN( Number( f.txtExpiryMonth.options[f.txtExpiryMonth.selectedIndex].value ) ) || Number( f.txtExpiryMonth.options[f.txtExpiryMonth.selectedIndex].value ) < 1 || Number( f.txtExpiryMonth.options[f.txtExpiryMonth.selectedIndex].value ) > 12 )
		{
			alert( "Please specify the expiry month of your credit card. (ex. January => 1)" );
			return false;
		}
		else if ( isNaN( Number( f.txtExpiryYear.options[f.txtExpiryYear.selectedIndex].value ) ) || Number( f.txtExpiryYear.options[f.txtExpiryYear.selectedIndex].value ) < 2002 )
		{
			alert( "Please specify the expiry year of your credit card." );
			return false;
		} // end if

		//---------------------------------------------------------------------
		// Ensure the date is not in the past
		//---------------------------------------------------------------------
		var today = new Date();
		var sendDate = new Date( f.txtExpiryYear.options[f.txtExpiryYear.selectedIndex].text, f.txtExpiryMonth.options[f.txtExpiryMonth.selectedIndex].value - 1, 1 );

		if ( sendDate < today && !( ( f.txtExpiryYear.options[f.txtExpiryYear.selectedIndex].text == today.getYear() || f.txtExpiryYear.options[f.txtExpiryYear.selectedIndex].text == today.getYear() + 1900 ) && f.txtExpiryMonth.options[f.txtExpiryMonth.selectedIndex].value == ( today.getMonth() + 1 ) ) )
		{
			alert( "The expiry date you have selected is in the past according to your computer's clock." );
			return false;
		}
		else
		{
			return true;
		} // end if

	} // end if

} // end routine: myForm_OnSubmit

//-----------------------------------------------------------------------------
function err( errorStr, newError )
//-----------------------------------------------------------------------------
{
	if ( errorStr != "" ) errorStr = errorStr + "\n";
	errorStr = errorStr + newError;
	return errorStr;
} // end routine: err

//-----------------------------------------------------------------------------
// Pad a number on the left with zeroes
//-----------------------------------------------------------------------------
function zero_pad( str, len )
//-----------------------------------------------------------------------------
{
	zeroes = len - str.length;
	if ( zeroes < 0 ) zeroes = 0;
	for ( i = 0 ; i < zeroes ; i++ )
	{
		str = "0" + str;
	} // end for: Zeroes
	return str;
} // end function: zero_pad

//-----------------------------------------------------------------------------
function validateEmailAddress( val )
//-----------------------------------------------------------------------------
// Parameters:
//   1. val - The email address
//-----------------------------------------------------------------------------
// Return value:
//   Default:    Returns the formatted email address
//   If invalid: Returns the string (invalid)
//-----------------------------------------------------------------------------
{

	// Description of the regular expression:
	// (Arbitrary whitespace)(One or more letters, numbers, underscores
	// ...or periods)(@)(One or more letters, numbers, underscores or
	// ...periods)(.)(One or more letters, numbers, underscores or
	// ...periods)(Arbitrary whitespace)

	regExp = /^\s*([a-z\d_\.]+@[a-z\d_\.]+\.[a-z\d_\.]+)\s*$/i;
	a = regExp.exec( val )

	if ( a == null )
	{
		// Invalid

		return "(invalid)";
	}
	else
	{
		// Valid

		return a[1];

	} // end if: Valid?

} // end routine: validateEmailAddress

//-----------------------------------------------------------------------------
function validateName( val )
//-----------------------------------------------------------------------------
// Parameters:
//   1. val - The individual's name
//-----------------------------------------------------------------------------
// Return value:
//   Default:    Returns the name
//   If invalid: Returns the string (invalid)
//-----------------------------------------------------------------------------
{

	// Description of the regular expression:
	//   1. (Arbitrary whitespace)(One or more letters)
	//      ...(Arbitrary whitespace)


	regExpA = /^\s*([a-z]+\s?[a-z]*)\s*$/i;
	a = regExpA.exec( val )
	
	if ( a != null )
	{
		// Valid

		return a[1];
		
	}
	else
	{
		// Invalid
		return "(invalid)";

	} // end if: Valid?

} // end routine: validateName

//-----------------------------------------------------------------------------
function validatePostalCode( val ) // (or ZIP)
//-----------------------------------------------------------------------------
// Parameters:
//   1. val - The postal code
//-----------------------------------------------------------------------------
// Return value:
//   Default:    Returns the formatted postal code
//   If invalid: Returns the string (invalid)
//-----------------------------------------------------------------------------
{
	// Description of the regular expression for Canadian format:
	// (Arbitrary whitespace)(Letter)(Number)(Letter)(Arbitrary whitespace)
	// ...(Number)(Letter)(Number)(Arbitrary whitespace)

	regExp = /^\s*([a-z]\d[a-z])\s*(\d[a-z]\d)\s*$/i;
	a = regExp.exec( val )

	if ( a == null )
	{
		// Invalid

			return "(invalid)";
	}
	else if ( a != null )
	{
		// Valid (Canadian format)

		return a[1] + " " + a[2];		
	} // end if: Valid?

} // end routine: validatePostalCode

//-----------------------------------------------------------------------------
function validatePhoneNumber( val )
//-----------------------------------------------------------------------------
// Parameters:
//   1. val - The phone number
//-----------------------------------------------------------------------------
// Return value:
//   Default:    Returns the formatted phone number
//   If invalid: Returns the string (invalid)
//-----------------------------------------------------------------------------
{

	// Description of the regular expression:
	// (Arbitrary Whitespace)(Optional left-bracket)(Three numbers)
	// ...(Optional right bracket)(Arbitrary whitespace or a dash)
	// ...(Three numbers)(Arbitrary whitespace or a dash)(Four numbers)
	// ...(Arbitrary whitespace)(Optional: Arbitrary non-digits followed by
	// ...an x followed by more arbitrary non-digits followed by
	// ...one or more digits followed by arbitrary whitespace)

	regExp = /^\s*(\(?)(\d\d\d)(\)?)(\s*|-)(\d\d\d)(\s*|-)(\d\d\d\d)\s*([^d]*x[^\d]*(\d+)\s*)?$/i;
	a = regExp.exec( val )
	regExp = /^\s*(\d\d\d)(\s*|-)(\d\d\d\d)\s*([^d]*x[^\d]*(\d+)\s*)?$/i;
	b = regExp.exec( val )


	if ( a == null )
	{
		if ( b == null )
		{
			// Invalid
			return "(invalid)";
		}
		else
		{
			// Valid
	
			tmp = b[1] + "-" + b[3];
			if ( b[5] != "" && b[5] != null ) tmp = tmp + " ext. " + b[5];
			return tmp;
	
		} // end if: Valid?
	}
	else
	{
		// Valid

		tmp = "(" + a[2] + ")" + " " + a[5] + "-" + a[7];
		if ( a[9] != "" && a[9] != null ) tmp = tmp + " ext. " + a[9];
		return tmp;

	} // end if: Valid?

} // end routine: validatePhoneNumber