// JavaScript Document

function clearThis ( formInput ) { 
	thisElement = document.getElementById(formInput);
	thisElement.value = "";
}

//--------------------------------------------------------------------------------------------------
// checkChar
//
// Tests if a supplied string contains any illegal characters.

function checkChar( thisString ) {
	validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
	isValid = true;
	
	for( i=0; i <= thisString.length && isValid; i++ ) {
		thisChar = thisString.charAt(i);
		if(  validChars.indexOf(thisChar) == -1 ) {
			isValid = false;
		}
	}
	return isValid;
}

//--------------------------------------------------------------------------------------------------
// validateAndSubmitRegistration
//
// Performs a client-side validation of input for the registration form

function validateAndSubmitRegistration() {
	validationMsg = "";
	return true;			// Use this to deactivate function (e.g. to test server-side validation)

	// Get relevant form values
	Username			= document.frmRegistration.Username.value;
	SignupEmail		= document.frmRegistration.SignupEmail.value;
	ConfirmEmail	= document.frmRegistration.ConfirmEmail.value;
	FirstName		= document.frmRegistration.FirstName.value;
	Surname			= document.frmRegistration.Surname.value;
	PostalAddress	= document.frmRegistration.PostalAddress.value;
	PhoneBH			= document.frmRegistration.PhoneBH.value;
	PhoneAH			= document.frmRegistration.PhoneAH.value;
	Mobile			= document.frmRegistration.Mobile.value;
	validateCode	= document.frmRegistration.validateCode.value;
	
	// Check for valid characters in username: alphanumeric plus underscores
	if( !checkChar( Username ) ) {
		validationMsg = validationMsg + "- Username can only contain letters, numbers and underscores (_).\n\n";
	}
	
	// Check other required fields
	if( SignupEmail == "" || ConfirmEmail == "" || FirstName == "" || Surname == "" || PostalAddress == "" || validateCode == "" ) {
		validationMsg = validationMsg + "- Please complete all required fields (*).\n\n";
	}
	
	// Check user has supplied at least one phone number
	if( PhoneBH == "" && PhoneAH == "" && Mobile == "" ) {
		validationMsg = validationMsg + "- Please supply at least one phone number.\n\n";
	}

	// Check email and confirmation are the same
	if( SignupEmail != ConfirmEmail ) {
		validationMsg = validationMsg + "- Email addresses do not match.\n\n";
		document.frmRegistration.SignupEmail.value = "";
		document.frmRegistration.ConfirmEmail.value = "";
	}
	
	// Submit or reject form?
	if ( validationMsg == "" ) {
		// There were no errors. Submit the form.			
		return true;
	}
	else {
		// There were errors. Display alert box and cancel submission.
		validationMsg = "Please correct the following:\n\n" + validationMsg;
		alert(validationMsg);
		return false;
	}
}

//--------------------------------------------------------------------------------------------------
// validateAndSubmitPasswordReset
//
// Performs a client-side validation of input for the password reset form

function validateAndSubmitPasswordReset() {
	validationMsg = "";
	return true;			// Use this to deactivate function (e.g. to test server-side validation)

	// Get relevant form values
	SpecifiedEmail	= document.frmPasswordReset.SpecifiedEmail.value;
	validateCode	= document.frmPasswordReset.validateCode.value;

	// Check email entered
	if( SpecifiedEmail == "" ) {
		validationMsg = validationMsg + "- Email address not specified.\n\n";
	}	
	
	// Check email entered
	if( SpecifiedEmail == "" ) {
		validationMsg = validationMsg + "- You must enter the form verification code.\n\n";
	}
	
	// Submit or reject form?
	if ( validationMsg == "" ) {
		// There were no errors. Submit the form.			
		return true;
	}
	else {
		// There were errors. Display alert box and cancel submission.
		validationMsg = "Please correct the following:\n\n" + validationMsg;
		alert(validationMsg);
		return false;
	}
}

//--------------------------------------------------------------------------------------------------
// validateAndSubmitAccountUpdate
//
// Performs a client-side validation of input for the Edit Account form

function validateAndSubmitAccountUpdate() {
	validationMsg = "";
	return true;			// Use this to deactivate function (e.g. to test server-side validation)

	// Get relevant form values
	//Username			= document.frmEditAccount.Username.value;
	SuppliedEmail		= document.frmEditAccount.SuppliedEmail.value;
	ConfirmedEmail		= document.frmEditAccount.ConfirmedEmail.value;
	FirstName			= document.frmEditAccount.FirstName.value;
	Surname				= document.frmEditAccount.Surname.value;
	PostalAddress		= document.frmEditAccount.PostalAddress.value;
	CurrentPassword	= document.frmEditAccount.CurrentPassword.value;
	NewPassword			= document.frmEditAccount.NewPassword.value;
	ConfirmedPassword	= document.frmEditAccount.ConfirmedPassword.value;
	PhoneBH				= document.frmEditAccount.PhoneBH.value;
	PhoneAH				= document.frmEditAccount.PhoneAH.value;
	Mobile				= document.frmEditAccount.Mobile.value;

	//alert("|" + SuppliedEmail + "|" + ConfirmedEmail + "|" + FirstName  + "|" + Surname  + "|" + PostalAddress  + "|" + CurrentPassword  + "|" + NewPassword  + "|" + ConfirmedPassword  + "|" + PhoneBH  + "|" + PhoneAH  + "|" + Mobile  + "|" );

	// Check required fields
	if( FirstName == "" || Surname == "" || PostalAddress == "" ) {
		validationMsg = validationMsg + "- Please complete all required fields (*).\n\n";
	}
	
	// Check user has supplied at least one phone number
	if( PhoneBH == "" && PhoneAH == "" && Mobile == "" ) {
		validationMsg = validationMsg + "- Please supply at least one phone number.\n\n";
	}

	// Check email and confirmation are the same
	if( SuppliedEmail != ConfirmedEmail ) {
		validationMsg = validationMsg + "- Email addresses do not match.\n\n";
		document.frmEditAccount.SuppliedEmail.value = "";
		document.frmEditAccount.ConfirmedEmail.value = "";
	}
	
	// If user wants to change password, they must also enter current password for the account
	if( NewPassword != '' && CurrentPassword == '' ) {
		validationMsg = validationMsg + "- To set a new password you must also specify the current (old) password.\n\n";
	}
	
	// Check for valid characters in password: alphanumeric plus underscores
	if( !checkChar( NewPassword ) ) {
		validationMsg = validationMsg + "- Password can only contain letters, numbers and underscores (_).\n\n";
		document.frmEditAccount.NewPassword.value = "";
		document.frmEditAccount.ConfirmedPassword.value = "";
	}
		
	// Check password and confirmation are the same
	if( NewPassword != ConfirmedPassword ) {
		validationMsg = validationMsg + "- Passwords do not match.\n\n";
		document.frmEditAccount.NewPassword.value = "";
		document.frmEditAccount.ConfirmedPassword.value = "";
	}
	
	// Submit or reject form?
	if ( validationMsg == "" ) {
		// There were no errors. Submit the form.			
		return true;
	}
	else {
		// There were errors. Display alert box and cancel submission.
		validationMsg = "Please correct the following:\n\n" + validationMsg;
		alert(validationMsg);
		return false;
	}
}