    $(document).ready(function() {
    	var now = new Date();
    
		$('form').validate({
			errorClass: 'validation-advice',
			errorElement: 'div',
			ignoreTitle: true,
			onsubmit : false,
			showErrors: function (errorMap, errorList) {
				if (errorList.length && errorList[0].element) {
					// Scroll first so popup doesn't get confused
					$(window).scrollToIfNeeded($(errorList[0].element).parent());
				}
				this.defaultShowErrors();
			}
		});
		
		var dateMappings = {
			
			'event_start_date' : {
				'year'  : 'start_year',
				'month' : 'start_month',
				'day'   : 'start_day'
			},
			'event_end_date' : {
				'year'  : 'end_year',
				'month' : 'end_month',
				'day'   : 'end_day'
			},
			'event_post_date' : {
				'year'  : 'post_year',
				'month' : 'post_month',
				'day'   : 'post_day'
			}

		};
	
		var timeMappings = {};
		/*
		{
			'startTime' : {
				'hour'   : 'startHour',
				'minute' : 'startMinute',
				'ampm'   : 'startAmPm'
			},
			'endTime' : {
				'hour'   : 'endHour',
				'minute' : 'endMinute',
				'ampm'   : 'endAmPm'
			}
		};
		*/
		
		var monthToName = {
			'1'  : 'January',
			'2'  : 'February',
			'3'  : 'March',
			'4'  : 'April',
			'5'  : 'May',
			'6'  : 'June',
			'7'  : 'July',
			'8'  : 'August',
			'9'  : 'September',
			'10' : 'October',
			'11' : 'November',
			'12' : 'December'
		};
	
		
		$('.dpDateHint').text(
			monthToName[now.getMonth()+1]+' '+now.getDate()+', '+now.getFullYear());

		$('.dateEntryField').each(function () {
			var options = {
				showDefault     : true,
				showOn          : 'both', 
				buttonImageOnly : true, 
				buttonImage     : '/images/calendar_16.png',
				dateFormat      : 'MM d, yy',
				autoSize        : true,
				constrainInput  : true,
				mandatory       : true,
				onSelect        : function (value, date, inst) {
					// datepick validator plugin is busted so do this ourselves
					if (document.getElementById("event_form")) {
						submitEventForm();
					}
				}
			};

			var year = $('input[name="'+dateMappings[this.id]['year' ]+'"]').val();
			var day  = $('input[name="'+dateMappings[this.id]['day'  ]+'"]').val();

			var monthName = $('input[name="'+dateMappings[this.id]['month']+'"]').val();
			var month = '1';
			for (var number in monthToName) {
				if (monthToName[number] == monthName) { month = number; break; }
			}
			if (year.length && month.length && day.length) {
				options.defaultDate = new Date(year, month-1, day);
			} else {
				options.defaultDate = now;
			}
			
			$(this).datepick(options);
		});
		
		$('.timeEntryField').each(function () {
			$(this).timeEntry({
				spinnerImage      : '/images/spinnerUpDown.png', 
				spinnerSize       : [15, 20, 0], 
				spinnerBigSize    : [30, 32, 0], 
				spinnerIncDecOnly : true,
				ampmPrefix        : ' '
			});

			var hour   = $('input[name="'+timeMappings[this.id]['hour'  ]+'"]').val();
			var minute = $('input[name="'+timeMappings[this.id]['minute']+'"]').val();
			var ampm   = $('input[name="'+timeMappings[this.id]['ampm'  ]+'"]').val();
			
			if (hour.length && minute.length && ampm.length) {
				if (ampm == 'AM' && hour == 12) {
					hour = 0; // midnight
				} else if (ampm == 'PM' && hour < 12) {
					hour = parseInt(hour) + 12; // noon - midnight
				}
				
				var time = new Date(0, 0, 0, hour, minute);
				$(this).timeEntry('setTime', time);
			}
		});
		    	
		// remember what kind of submit it is so we don't validate cancel
		$('input[type=submit]').click(function (e) {
			$('form').data('submitType', this.value);
		}).bind(($.browser.opera ? 'keypress' : 'keydown'), function (e) {
			if (e.keyCode == 32 || e.keyCode == 13) {
				$('form').data('submitType', this.value);
			}
		});

		$('form').submit(function() {
			if (document.pressed == 'nosubmit') { return false; }
			
        	//if (!checkSubmit()) { return false; }  // only submit once
        
			var submitType = $(this).data('submitType');
			
			if (!submitType || submitType != 'Cancel') {
				if (!$(this).validate().form()) { return false; }
			}

 			$('.dateEntryField').each(function () {
 				var date = $(this).datepick('getDate');
 				
 				if (!date) { return false; } // sanity check
				
				$('input[name="'+dateMappings[this.id]['year' ]+'"]').val(date.getFullYear());
				//$('input[name="'+dateMappings[this.id]['month']+'"]').val(monthToName[date.getMonth()+1]);
				$('input[name="'+dateMappings[this.id]['month']+'"]').val(date.getMonth()+1);
				$('input[name="'+dateMappings[this.id]['day'  ]+'"]').val(date.getDate());
 			});
 
			/*
 			$('.timeEntryField').each(function () {
				var time = $(this).timeEntry('getTime');
 				
 				if (!time) { return false; } // sanity check
				
				var hours = time.getHours();
				var ampm = 'AM';
				
				if (hours == 0) {
					hours = 12; // midnight
				} else if (hours > 12) {
					hours = parseInt(hours) - 12; // noon - midnight
					ampm = 'PM';
				} else if (hours == 12) {
					ampm = 'PM'
				}
				
				$('input[name="'+timeMappings[this.id]['hour'  ]+'"]').val(hours);
				$('input[name="'+timeMappings[this.id]['minute']+'"]').val(time.getMinutes());
				$('input[name="'+timeMappings[this.id]['ampm'  ]+'"]').val(ampm);
        	});
			*/
		});
    });

