		$(function()
		{
			
			// initialise the "Select date" link
			$('#date_pick')
				.datePicker(
					// associate the link with a date picker
					{
						createButton:false
					}
				).bind(
					// when the link is clicked display the date picker
					'click',
					function()
					{
						updateSelects($(this).dpGetSelected()[0]);
						$(this).dpDisplay();
						return false;
					}
				).bind(
					// when a date is selected update the SELECTs
					'dateSelected',
					function(e, selectedDate, $td, state)
					{
						updateSelects(selectedDate);
					}
				).bind(
					'dpClosed',
					function(e, selected)
					{
						updateSelects(selected[0]);
					}
				);

			$('#dd_pick')
				.datePicker(
					// associate the link with a date picker
					{
						createButton:false
					}
				).bind(
					// when the link is clicked display the date picker
					'click',
					function()
					{
						updateDeparture($(this).dpGetSelected()[0]);
						$(this).dpDisplay();
						return false;
					}
				).bind(
					// when a date is selected update the SELECTs
					'dateSelected',
					function(e, selectedDate, $td, state)
					{
						updateDeparture(selectedDate);
					}
				).bind(
					'dpClosed',
					function(e, selected)
					{
						updateDeparture(selected[0]);
					}
				);
				
			var updateSelects = function (selectedDate)
			{
				selectedDate = new Date(selectedDate);
				var d = selectedDate.getDate();
				var m = selectedDate.getMonth();
				var y = selectedDate.getFullYear();
				($('#ad')[0]).selectedIndex = d-1;
				($('#am')[0]).selectedIndex = m;
				($('#dd')[0]).selectedIndex = d;
				($('#dm')[0]).selectedIndex = m;
			}

			var updateDeparture = function (selectedDate)
			{
				selectedDate = new Date(selectedDate);
				var d = selectedDate.getDate();
				var m = selectedDate.getMonth();
				var y = selectedDate.getFullYear();
				($('#dd')[0]).selectedIndex = d-1;
				($('#dm')[0]).selectedIndex = m;
			}
			// listen for when the selects are changed and update the picker
			$('#ad, #am')
				.bind(
					'change',
					function()
					{
						var d = new Date(
									$('#am').val(),
									$('#ad').val()
								);
						$('#date_pick').dpSetSelected(d.asString());
					}
				);
			
			// default the position of the selects to today
			var today = new Date();
			var tomorrow = new Date(); 
			tomorrow.setDate(today.getDate() + 1); 
			($('#ad')[0]).selectedIndex = today.getDate()-1;
			($('#am')[0]).selectedIndex = today.getMonth();
			($('#dd')[0]).selectedIndex = tomorrow.getDate() - 1;
			($('#dm')[0]).selectedIndex = tomorrow.getMonth();
			
			// and update the datePicker to reflect it...
			$('#ad').trigger('change');

		});

		function change() {

			window.location='?d=' + document.getElementById('ad').value + '&m=' + document.getElementById('am').value;
		
		}