
// Validate users' inputs
function validateForm(thisForm) {
    with (thisForm){
//SignupButton.disabled = true;
//SignupButton.value = "Please wait...";
//return true;    
        // Check for madatory fields.
        
        if (FirstName.value == ""){
            alert ("Please enter your first name.");
            FirstName.focus();
            return false;
        }
        if (LastName.value == ""){
            alert ("Please enter your last name.");
            LastName.focus();
            return false;
        }
        
        if (Gender.value == ""){
            alert ("Please select your gender.");
            Gender.focus();
            return false;
        }
        
        if (ContactNumber.value == ""){
            alert ("Please enter your daytime contact number.");
            ContactNumber.focus();
            return false;
        }
        
        if (EmailAddress.value == ""){
            alert ("Please enter your email address.");
            EmailAddress.focus();
            return false;
        }
        
        if (ConfirmEmailAddress.value == ""){
            alert ("Please confirm your email address.");
            ConfirmEmailAddress.focus();
            return false;
        } else if (EmailAddress.value != ConfirmEmailAddress.value){
            alert ("Your email address and the confirmation do not match.");
            ConfirmEmailAddress.focus();
            return false;
        }
        
        if (BlueCardHolder.value == "Yes"){
            if (BlueCardNumber.value == ""){
                alert ("Please enter your Blue Card number.");
                BlueCardNumber.focus();
                return false;
            }
            if (BCEDay.value == 0){
                alert ("Please enter your Blue Card expiry date.");
                BCEDay.focus();
                return false;
            }
            if (BCEMonth.value == 0){
                alert ("Please enter your Blue Card expiry date.");
                BCEMonth.focus();
                return false;
            }
            if (BCEYear.value == 0){
                alert ("Please enter your Blue Card expiry date.");
                BCEYear.focus();
                return false;
            }
        }
        
        if (Pref1.value == 0){
            alert ("Please select your first preference.");
            Pref1.focus();
            return false;
        }
        if (Pref2.value == 0){
            alert ("Please select your second preference.");
            Pref2.focus();
            return false;
        }
        if (Pref3.value == 0){
            alert ("Please select your third preference.");
            Pref3.focus();
            return false;
        }
        
        if (VolunteeredBefore.value == 1){
            if (PastC4KExperience.value == ""){
                alert ("Please enter the area you have volunteered before.");
                PastC4KExperience.focus();
                return false;
            }
        }
        
        if (ReferredBy.value == ""){
            alert ("Please tell us how did you find out about volunteering for Christmas 4 Kids.");
            ReferredBy.focus();
            return false;
        }
        if (ReferredBy.value == "Other" && OtherReferrer.value == ""){
            alert ("Please enter how did you find out about volunteering for C4K.");
            OtherReferrer.focus();
            return false;
        }
        
        if (FromHBCC.value == 1 && PastoralGroup.value == 0){
            alert ("Please select the unit/group you attend.");
            PastoralGroup.focus();
            return false;
        }

        // Validate the Blue Card expiry date
        if (BlueCardHolder.value == "Yes"){
            if (BCEMonth.value == 4 || BCEMonth.value == 6 || BCEMonth.value == 9 || BCEMonth.value == 11){
                if (BCEDay.value > 30){
                    alert ("The Blue Card expiry date is not valid.");
                    BCEDay.focus();
                    return false;
                }
            } else if (BCEMonth.value == 2){
                if (BCEYear.value % 4 == 0){
                    if (BCEDay.value > 29){
                        alert ("The Blue Card expiry date is not valid.");
                        BCEDay.focus();
                        return false;
                    }
                } else {
                    if (BCEDay.value > 28){
                        alert ("The Blue Card expiry date is not valid.");
                        BCEDay.focus();
                        return false;
                    }
                }
            }
            // Checks if the Blue Card is expired
            var today = Date.parse(Date());
            var expiryDate = Date.parse(BCEMonth.value + "-" + BCEDay.value + "-" + BCEYear.value);
            if (today > expiryDate){
                alert ("Your Blue Card has already expired. Please make sure you have renewed your Blue Card.");
                BlueCardHolder.focus();
                return false;
            }
        }

        // Validate the format of the email address

        // The following pattern checks whether the input string is a valid email 
        // address in the form "name@domain.com". Actually, it does not have to be a
        // ".com" address. Any combination of letters following the last period are 
        // fine. Also, the email name can have a dash or be separated by one or more 
        // periods. The Domain name can also have multiple words separated by periods. 

        // [\w-]+    
        // One or more matches of any character (a-z, A-Z, 0-9, and underscore) or dash. On either side of the @ character this ensures the address is in the form name@domainname.
        // \.              
        // An escaped period. (Without the backslash, a period matches any single character except the newline character.) Using this ensures there is at least one period in the domain name.
        // *?            
        // A non-greedy, or minimal, match of zero or more matches of the preceding expression.
        // ([\w-]+\.)*?    
        // Combination of the three preceding expressions:
        // Zero or more non-greedy matches of the expression one or more matches of any character (a-z, A-Z, 0-9, and underscore) or dash, followed by only one period.

        re = /^([\w-]+\.)*?[\w-]+@[\w-]+\.([\w-]+\.)*?[\w]+$/;
        if (!re.test(EmailAddress.value)){
            alert ("The format of your email address is not correct.");
            EmailAddress.focus();
            return false;
        }

        // Validate the format of PostCode

        // The following pattern checks whether the input string is a valid post code
        // in the format dddd, where d is any digit 0-9.
        // The ^ character matches the position at the beginning of the input string. 
        // The $ character matches the position at the end of the input string.
        // \d{4}: Exactly four digits (0-9).

        /*
        re = /^\d{4}$/;

        if (!re.test(PostCode.value)){
            alert ("The format of your post code is not correct.");
            PostCode.focus();
            return false;
        }
        */
        
        // Check preferences are not the same
        if (Pref1.value == Pref2.value && Pref2.value == Pref3.value){
            alert ("Your preferences 1, 2 and 3 are the same.");
            Pref1.focus();
            return false;
        }
        if (Pref1.value == Pref2.value){
            alert ("Your preferences 1 and 2 are the same.");
            Pref1.focus();
            return false;
        }
        if (Pref1.value == Pref3.value){
            alert ("Your preferences 1 and 3 are the same.");
            Pref1.focus();
            return false;
        }
        if (Pref2.value == Pref3.value){
            alert ("Your preferences 2 and 3 are the same.");
            Pref2.focus();
            return false;
        }
        
        SignupButton.disabled = true;
        SignupButton.value = "Please wait...";

    }
    return true;
        
}

function ToggleBlueCard(BlueCardHolder)
{
    // If user selected 'Yes' in BlueCardHolder
    // then enable BlueCardNumber and BlueCardExpiry
    if(BlueCardHolder.selectedIndex == 0){
        document.VolunteerForm.BlueCardNumber.disabled = false;
        document.VolunteerForm.BCEDay.disabled = false;
        document.VolunteerForm.BCEMonth.disabled = false;
        document.VolunteerForm.BCEYear.disabled = false;
    } else {
        document.VolunteerForm.BlueCardNumber.disabled = true;
        document.VolunteerForm.BCEDay.disabled = true;
        document.VolunteerForm.BCEMonth.disabled = true;
        document.VolunteerForm.BCEYear.disabled = true;
    }
}

function TogglePastExperience(VolunteeredBefore)
{
    // If user selected 'Yes' in VolunteeredBefore
    // then enable PastC4KExperience
    if(VolunteeredBefore.selectedIndex == 0){
        document.VolunteerForm.PastC4KExperience.disabled = false;
    } else {
        document.VolunteerForm.PastC4KExperience.disabled = true;
    }
}

function ToggleOtherReferrer(ReferredBy)
{
    // If user selected 'Other' in ReferredBy
    // then enable OtherReferrer
    if(ReferredBy.value == "Other"){
        document.VolunteerForm.OtherReferrer.disabled = false;
    } else {
        document.VolunteerForm.OtherReferrer.disabled = true;
    }
}

function TogglePastoralGroups(FromHBCC)
{
    // If user selected 'Yes' in FromHBCC
    // then enable PastoralGroup
    if(FromHBCC.selectedIndex == 0){
        document.VolunteerForm.PastoralGroup.disabled = false;
    } else {
        document.VolunteerForm.PastoralGroup.disabled = true;
    }
}
