var NetRDatePicker = function () {
	// Prepare to show a date picker linked to three select controls 
	function readFromLinked() {
		$('#from-linked-dates').val($('#from-year').val() + '-' + $('#from-month').val() + '-' + $('#from-day').val());
		return {};
	}
	// Update three select controls to match a date picker selection 
	function updateFromLinked(date) {
		$('#from-year').val(date.substring(0, 4));
		$('#from-month').val(date.substring(5, 7));
		$('#from-day').val(date.substring(8, 10));
	}
	// Prevent selection of invalid dates through the select controls 
	function checkFromLinkedDays() { 
		var daysInMonth = 32 - new Date($('#from-year').val(), $('#from-month').val() - 1, 32).getDate(); 
		$('#from-day option').attr('disabled', ''); 
		$('#from-day option:gt(' + (daysInMonth - 1) +')').attr('disabled', 'disabled'); 
		if ($('#from-day').val() > daysInMonth) { 
			$('#from-day').val(daysInMonth); 
		}
	}
	function readToLinked() {
		$('#to-linked-dates').val($('#to-year').val() + '-' + $('#to-month').val() + '-' + $('#to-day').val());
		return {};
	}
	// Update three select controls to match a date picker selection 
	function updateToLinked(date) {
		$('#to-year').val(date.substring(0, 4));
		$('#to-month').val(date.substring(5, 7));
		$('#to-day').val(date.substring(8, 10));
	}
	// Prevent selection of invalid dates through the select controls 
	function checkToLinkedDays() { 
		var daysInMonth = 32 - new Date($('#to-year').val(), $('#to-month').val() - 1, 32).getDate(); 
		$('#to-day option').attr('disabled', ''); 
		$('#to-day option:gt(' + (daysInMonth - 1) +')').attr('disabled', 'disabled'); 
		if ($('#to-day').val() > daysInMonth) { 
			$('#to-day').val(daysInMonth); 
		}
	}
	return {
		readFromLinked: readFromLinked,
		updateFromLinked: updateFromLinked,
		checkFromLinkedDays: checkFromLinkedDays,
		readToLinked: readToLinked,
		updateToLinked: updateToLinked,
		checkToLinkedDays: checkToLinkedDays
	};
}();

// Init on document ready
$(document).ready(function() {
// Set up datepickers after checking that the necessary elements exist
	if (document.getElementById('from-date') && document.getElementById('to-date')) {
		$("#from-date", "#to-date").datepicker($.extend({}, $.datepicker.regional["sv"],{
			minDate: new Date(),
			maxDate: new Date(2010, 12 - 1, 31),
			beforeShow: NetRDatePicker.readFromLinked,
			onSelect: NetRDatePicker.updateFromLinked,
			showOn: "both",
			buttonText: "Välj från almanacka",
			buttonImage: "/i/icons/calendar.gif",
			buttonImageOnly: true
		}));
//		$("#from-month, #from-year").change(NetRDatePicker.checkFromLinkedDays);
	}
	$('input.has-datepicker').datepicker();
});
