jQuery(function() {

	//set focus on zip code entry field
	jQuery(window).load(function(){
		jQuery('#zipCodeText').focus();
	});

	// form submit
	jQuery('#zipEntryFormSubmit').click(function() {

		// validate zip code
		var zipCodeEntry = jQuery('#zipCodeText').val();
		var zipExp = /^\d{5}([\-]\d{4})?$/;

		// if this is not a proper 5-digit or 9-digit zip code...
		if (zipCodeEntry.search(zipExp) == -1) {
			jQuery('#invalidZipCodeFormat').show();
			return false;
		}
		else {
			jQuery('#zipEntryForm').submit();
			return true;
		}
	});

	jQuery('#zipEntryForm').submit(function() {

		// validate zip code
		var zipCodeEntry = jQuery('#zipCodeText').val();
		var zipExp = /^\d{5}([\-]\d{4})?$/;

		// if this is not a proper 5-digit or 9-digit zip code...
		if (zipCodeEntry.search(zipExp) == -1) {
			jQuery('#invalidZipCodeFormat').show();
			return false;
		}
		else {
			var location = document.zipEntryForm.action;
			location.indexOf("?") > 0 ? location += "&zipcode=" + zipCodeEntry : location += "?zipcode=" + zipCodeEntry;
			window.location = location;
			return false;
		}
	});
});

