$(document).ready(function () {
	$('input, select, textarea').focus(function() {
		$(this).addClass('focus').parents('label').addClass('focus');
		$(this).addClass('focus').parents('tr').addClass('rowhover').addClass('activeRow');
	}).blur(function() {
		$(this).removeClass('focus').parents('label').removeClass('focus');
		$(this).removeClass('focus').parents('tr').removeClass('rowhover').removeClass('activeRow');
	});
	
	//Add support for row highlighting in pre-IE7 browsers
	if ($.browser.msie && $.browser.version < 7) {
		$('tr').hover(function() {
			$(this).addClass('rowhover');
		}, function() {
			/* The 'activeRow' class is only used to fixup IE6 behavior */
			if (!$(this).hasClass('activeRow')) {
				$(this).removeClass('rowhover');
			}
		});
	}
	
	//Override the default e-mail validation because it's lousy
	jQuery.validator.methods.email = function(value, element) {
		//https://www.intheround.net/wiki/Regular_Expressions#E-mail
		return this.optional(element) || /^([a-zA-Z0-9\_\.\-]){2,}\@(([a-zA-Z0-9\-]){2,}\.)+([a-zA-Z0-9]{2,4})$/i.test(element.value);
	};

	$('.show-chars-remaining').each(function () {
		//If we don't have a max length, we can't do anything useful
		if (!$(this).attr('maxlength')) {
			$('label.chars-remaining[for=' + $(this).attr('id') + ']').remove();
			return;
		}
		
		$(this).keyup(function (event) {
			var me = $(event.currentTarget),
				maxLength = me.attr('maxlength'),
				currVal  = me.val();
			
			if (!maxLength) {
				return;
			}
			
			if (currVal.length > maxLength) {
				me.val(currVal.substring(0, maxLength));
			} else {
				$('label.chars-remaining[for=' + $(this).attr('id') + '] span').html(maxLength - currVal.length);
			}
		}).keyup();
	});
});

