if (xhr_enabled()) {

	jQuery(function(){
		jQuery('div.form-error').wrap('<div class="error-slide"></div>');
					
		jQuery('input[name="username"]').focus(function(){
			jQuery(this).siblings('div.error-slide').slideUp(function(){
				jQuery(this).remove();
			});
		}).blur(function(){
			var input = jQuery(this), username = input.val();
			
			if (!username) 
				return;

			if (username.length < 3 || username.length >25) {
				msg = "Please keep your username between 3 and 25 characters long.";
				addError(input, msg);
				return;
			}
			
			jQuery.get('/_assets/inc/register_ajax.php', {
				method: 'usernameUsed',
				username: username
			}, function(result){
				if (result == 1) {
					msg = "This username is already taken, please choose another.";
					addError(input, msg);
				}
			});
		});
		
		jQuery('input[name="email"]').focus(function(){
			jQuery(this).siblings('div.error-slide').slideUp(function(){
				jQuery(this).remove();
			});
		}).blur(function(){
			var input = jQuery(this), email = input.val();
			
			if (!email) 
				return;
				
			if (!email.match(/^[-~`!#\$%\^&\*_=+\{\}\|\'\/\.a-z0-9]*@[a-z0-9.-]+\.[a-z]{2,4}$/i)) {
				msg = "Please enter a valid email address";
				addError(input, msg);
				return;
			}
			
			jQuery.get('/_assets/inc/register_ajax.php', {
				method: 'emailUsed',
				email: email
			}, function(result){
				if (result == 1) {
					msg = email+" is being used by another BabyNames account. Please use another email address.";
					addError(input, msg);
				}
			});
		});
		
		jQuery('input[name="password"]').focus(function(){
			jQuery(this).siblings('div.error-slide').slideUp(function(){
				jQuery(this).remove();
			});
		}).blur(function(){
			var input = jQuery(this), password = input.val();
			
			if (!password) 
				return;
				
			if (password.length < 5) {
				msg = "Please choose a password with at least 5 characters.";
				addError(input, msg);
				return;
			}
		});

		jQuery('#commjoinform').submit(function () { 
			jQuery('#birth_day').siblings('div.error-slide').slideUp(function(){
				jQuery(this).remove();
			});
			return checkBirthdate();
		} );

		function checkBirthdate () {
			y = jQuery('#birth_year').val();
			m = jQuery('#birth_month').val();
			d = jQuery('#birth_day').val();
			bd = new Date(y, m-1, d);
			if ((bd.getMonth()+1!=m)||(bd.getDate()!=d)||(bd.getFullYear()!=y)) {
				input = jQuery('#birth_year');
				msg = "Please choose a valid birth date.";
				addError(input, msg);
				return false;
			}
			else return true;
		}

		function addError(input, msg) {
			jQuery('<div class="error-slide"><div class="form-error">'+msg+'</div></div>').hide().insertAfter(input).slideDown();
		}
	});
	
} // xhr_enabled