$(document).ready(function() {
	
	$("#mailingform form").validate({

		errorElement: "em",
		errorPlacement: function(error, element) {
			error.appendTo(element.parent());
		},
		rules: {
			name: "required",
		    email: {
				required: function(element) {
					return ($("#mailinglist:checked").length==1);
				},
				email: true
		    },		
		    phone: {
				minlength: 7
		    },
			address: {
				required: function(element) {
					return ($("#package:checked").length==1);
				}
		    },			
		    city: {
				required: function(element) {
					return ($("#package:checked").length==1);
				}
		    },			
		    zipcode: {
				required: function(element) {
					return ($("#package:checked").length==1);
				}
		    },			
		    country: {
				required: function(element) {
					return ($("#package:checked").length==1);
				}
		    }			
		},	
	
		messages: {
			name: "Don\u2019t forget your first and last name",
			email: {
				required: "To get in touch with you, we\u2019ll need your email address",
				email: "Check to see if your email address is correct"
			},
			phone: {
				minlength: "Check your phone number to ensure it\u2019s correct"
			},
			address: {
				required: "We need your full street address"
			},	
			city: {
				required: "Include your city and, if applicable, state or province"
			},
			zipcode: {
				required: "Check that your correct postal code is included"
			},
			country: {
				required: "Including the country lets us deliver the package faster"
			}
		},

	
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				beforeSubmit: function(formData, jqForm, options) { 
					var queryString = $.param(formData); 
				    // console.log('About to submit: \n\n' + queryString);
					$("#contact input, #contact, textarea").attr("readonly","true");
					$("input[type=submit]").attr("disabled","true");
					$("input[type=submit]").attr("value","Sending\u2026");
				},
				success: function(responseText, statusText)  { 
					// console.log('status: ' + statusText + '\n\nresponseText: \n' + responseText +  '\n\nThe output div should have already been updated with the responseText.');
					$("#contact input, #contact, textarea").attr("disabled","true");
					$("input[type=submit]").remove();
					$("fieldset:last").append("<p class=\"success submit\">Your information has been sent. Thank you for your interest in Lee\u2019s art \u2014 she\u2019ll be in touch soon.</p>");
				},
			});	
			return false;
		}

	
	
	});

	
});
