function checkSignupForm(form) {

    if (! $('agreement').checked) {
        alert('You must agree to the terms of service and privacy policy.');
        return false;
    }
    
    return checkProfileForm(form);
}

function checkProfileForm(form) {

    form = form.id;    
    if (! checkForm(form)) return false;

    // Check state/other
    if (checkField('state') && checkField('stateother')) {
        highlightField('state', 1);
        displayError(form, 'Please fill in a value for all required fields');
        return false;
    }

    // Make sure the email address looks valid
    if (! $F('email').match(/.+\@.+\..+/)) {
        highlightField('email', 1);
        alert('The email address you entered does not appear to be valid.  Please enter your full email address. e.g. username@example.com');
        return false;
    }
    if ($('referralsource') && $F('referralsource') != '' && !$F('referralsource').match(/.+\@.+\..+/)) {
        highlightField('referralsource', 1);
        alert('The referral address you entered does not appear to be valid.  Please enter a full email address. e.g. username@example.com');
        return false;
    }
            
    // Check passwords
    if ($F('password1') != $F('password2')) {
        alert('The passwords do not match.');
        highlightField('password1', 1);
        highlightField('password2', 1);
        return false;
    }
}

function checkBillingForm(form) {
    
    form = form.id;    
    if (! checkForm(form)) return false;
    
    // Check state/other
    if (checkField('state') && checkField('stateother')) {
        highlightField('state', 1);
        displayError(form, 'Please fill in a value for all required fields');
        return false;
    }
    
    // Make sure credit card number looks somewhat valid
    if ($F('cardnumber').length < 13 || !checkChars($F('cardnumber'), '0123456789- ')) {
        highlightField('cardnumber', 1);
        displayError(form, 'Please enter a valid credit card number');
        return false;
    }
    
    // Make sure CVV looks somewhat valid
    if ($F('cvmvalue').length < 3 || !checkChars($F('cvmvalue'), '0123456789')) {
        highlightField('cvmvalue', 1);
        displayError(form, 'Please enter a valid verification code');
        return false;
    }
    
    // Make sure we find some digits in the address
    if ($F('country') == 'US' && !$F('address1').match(/\d+/)) {
        highlightField('address1', 1);
        displayError(form, 'Please enter a valid address');
        return false;
    }
    
    return true;
}

function selectUS() {
    var options = $('country').options;
    for (var i = 0; i < options.length; i++) {
        if (options[i].value == 'US') {
            $('country').selectedIndex = i;
        }
    }
}
