function isValidEmailAddress(emailAddress) 
{
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
};

var justifications = new Array();
justifications[0] = 'If you\'ve got a huge porn bill, <a href="/plans">Sign up now</a>.';
justifications[1] = 'If you just bought a sports car, <a href="/plans">Sign up now</a>.';
justifications[2] = 'If you\'re tired of dishes, <a href="/plans">Sign up now</a>.';
justifications[3] = 'If you\'re cheating on your wife, <a href="/plans">Sign up now</a>.';
justifications[4] = 'If you\'re tired of your husband, <a href="/plans">Sign up now</a>.';

justificationRotate = function(){
	$('#justification').fadeOut('normal', function(){
		var arrIndex = parseInt($(this).attr('rel'));
		
		arrIndex = arrIndex + 1;
		
		if ( arrIndex >= justifications.length )
		{
			arrIndex = 0;
		};
		
		$('#justification')
		.html( justifications[arrIndex] )
		.attr('rel', arrIndex )
		.fadeIn();
	});
};

$(document).ready(function(){
	setInterval(justificationRotate, 5000);
	
	$('area').colorbox();

	$('#contact form').submit(function(){
		$('.error', this).removeClass('error');
		$('.error-message', this).remove();

		var email = $('#email').val();
		if ( !isValidEmailAddress(email) )
		{
			$('#email')
			.addClass('error')
			.after('<span class="error-message">Please enter a valid email address</span>');
		};
		
		if ( $('#message').val().length == 0 )
		{
			$('#message')
			.addClass('error')
			.after('<span class="error-message">Please enter a valid email address</span>');
		};
		
		if ( $('.error', this).length > 0 )
		{
			return false;
		};
	});

});
