$(function() {
	$(".txtClubPhone").mask("(999) 999-9999");
	$('#frontJoinForm input[title!=""]').hint();
    $('#frontJoinForm').validate({
        rules: {
            name: "required",
            email: {
				required: function() {
					if($("#contactByEmail").attr('checked') || $("#contactByBoth").attr('checked')) {
						return true;
					} else {
						return false;
					}
				},
            },
            phoneNumber: {
				required: function() {
					if($("#contactByPhone").attr('checked') || $("#contactByBoth").attr('checked')) {
						return true;
					} else {
						return false;
					}
				},
            }
        },
        messages: {
           name:  "<span class='error'>*</span>",
           email: "<span class='error'>*</span>",
           phoneNumber: "<span class='error'>*</span>",
        }
    });
    $("#frontJoinForm").submit(function() {
		if($("#frontJoinForm").valid() != true) {
			return;
		}
		$("#frontJoinForm .activity").animate({ opacity: 1 }, 500, 'linear', function() {
			$(this).css({'visibility' : 'visible'});
		});
    	var formValues = $(this).serialize();
    	formValues += "&format=json";
		$.post('/newsletter/preferences/join', formValues, function(response) {
			$("#frontJoinForm .activity").animate({ opacity: 0 }, 500, 'linear', function() {
				$(this).css({'visibility' : 'hidden'});
			});
			if(response.error > 0) {
				message = "<span class='error'>Error: Try again</span>";
			} else if (response.success > 0) {
				message = "<span class='success'>Success!</span>";
			}
			$("#frontJoinForm .message").html(message)
										.fadeIn("slow");
			setTimeout(function() {
				$("#frontJoinForm .message").fadeOut("4000", function() {
					$("#frontJoinForm .message").html("");
				});
			}, 5000);
		});
		return false;
    });
});
