// validate registration form
// http://bassistance.de/jquery-plugins/jquery-plugin-validation/

jQuery(document).ready(function($){
	
	// custom validator method to verify that username is 5 characters or more, and is alphanumeric
	jQuery.validator.addMethod('checkUserName', function(value, element) { 
		var result = this.optional(element) || /^\w+$/i.test(value);
		if (!result) {
			$('#validateUsername').html('');
		}
		return result;
	}, 'Username may only contain numbers and digits.');
	
	// custom validator method to verify that password and password confirmation match
	jQuery.validator.addMethod('passwordsMatch', function(value, element, params) { 
		return jQuery(params[0]).val() == jQuery(params[1]).val();
	}, 'Passwords do not match.');

	// validate the form when it is submitted
	$('#registerForm').validate({
		errorClass: 'registrationError',
		errorElement: 'strong',
		rules: {
			username: {
				required: true,
				minlength: 5,
				checkUserName: true
			},
			email: {
				required: true,
				email: true
			},
			password: {
				required: true,
				minlength: 6
			},
			birth_year: {
				required: '#radio_user:checked'
			},
			band_name: {
				required: '#radio_band:checked'
			},
			user_agreement: {
				required: true
			}
		},
		messages: {
			username: {
				required: 'Please enter a username.',
				minlength: 'Username must be at least 5 characters.',
				checkUserName: 'Username may only contain numbers and digits.'
			},
			email: {
				required: 'Please enter an email.',
				email: 'Please enter a valid email address.'	
			},
			password: {
				required: 'Please enter a password.',
				minlength: 'Password must be at least 6 characters.'
			},
			birth_year: {
				required: 'Please select your birth year.'
			},
			band_name: {
				required: 'Please enter your band name.'
			},
			user_agreement: {
				required: 'Please read and agree to the terms &amp; conditions by checking the user agreement checkbox.'
			}
		}
	});
	
});