﻿jQuery.validator.addMethod("zipcode", function(zip) {
	// matches US ZIP code
	// allow either five digits or nine digits with an optional '-' between
	zip = zip.replace(/^\s+/, "");
	zip = zip.replace(/\s+$/, "");

	if(zip.length == 0) {
		return true;
	}

	if(zip.match(/^\d{5}([- ]?\d{4})?$/)) {
		return true;
	}
	return false;
}, "Please enter a valid US zip code");

$(function() {		
	$("#contact_form").validate({
		errorPlacement: function(error, element) {
			error.insertAfter(element.parent());
//			error.insertBefore(element.parent().next());
//     		error.appendTo(element.parent().next());
//     		error.appendTo(element.parent());
   		},
		rules: {
			name: {
				required: true,
				minlength: 2
			},
			address1: {
				required: true,
				minlength: 2
			},
			city: {
				required: true,
				minlength: 2
			},
			state: {
				required: true,
				minlength: 2
			},
			zip: {
				required: true,
				zipcode: true
			},
			email: {
				required: true,
				email: true
			},
			how: "required"
		},
		messages: {
			name: "* Please enter your name",
			address1: "* Please enter your address",
			city: "* Please enter your city",
			state: "* Please enter your state",
			zip: "* Please enter a valid US zip code",
			email: "* Please enter a valid e-mail address",
			how: "* Please select how you found out about this site",
		}
	});
});
