
var bindOnce = false; 
var d=new Date()
var day=d.getDate()
var month=d.getMonth() + 1
var year=d.getFullYear()

var endyear=d.getFullYear() +1

var todayDate = day+ "/" + month+ "/" + year 

$(function()
{
	
	// initialise the "Select date" link
	$('#date-pick-start')
		.datePicker(
			// associate the link with a date picker
			{
				createButton:false,
				startDate:todayDate,
				endDate:'31/12/' + endyear
			}
		).bind(
			// when the link is clicked display the date picker
			'click',
			function()
			{
				
				
				if (bindOnce == true) {
					updateSelectsStarts($(this).dpGetSelected()[0]);
					$(this).dpDisplay();												
					return false;							
				}else{
					updateSelectsStarts($(this).dpGetSelected()[0]);
					$(this).dpDisplay();												
					updateSelectsEnds($(this).dpGetSelected()[0]);
					$('#EndDateDay').trigger('change');
					return false;	
				}
			}
		).bind(
			// when a date is selected update the SELECTs
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				if (bindOnce == true) {
					updateSelectsStarts(selectedDate);						
				}else{
					updateSelectsStarts(selectedDate);						
					updateSelectsEnds(selectedDate);							
					$('#EndDateDay').trigger('change');	
				}					
			}
		).bind(
			'dpClosed',
			function(e, selected)
			{
				if (bindOnce == true) {						
					updateSelectsStarts(selected[0]);
				}else{
					updateSelectsStarts(selected[0]);
					updateSelectsEnds(selected[0]);
					$('#EndDateDay').trigger('change');
				}
			}
		);
		
	var updateSelectsStarts = function (selectedDate)
	{
		var d = selectedDate.getDate();
		var m = selectedDate.getMonth();
		var y = selectedDate.getFullYear();
		($('#StartDateDay')[0]).selectedIndex = d - 1;
		($('#StartDateMonth')[0]).selectedIndex = m;
		($('#StartDateYear')[0]).selectedIndex = y - year;
		
	}
	// listen for when the selects are changed and update the picker
	$('#StartDateDay, #StartDateMonth, #StartDateYear')
		.bind(
			'change',
			function()
			{
				var d = new Date(
							$('#StartDateYear').val(),
							$('#StartDateMonth').val()-1,
							$('#StartDateDay').val()
						);
				$('#date-pick-start').dpSetSelected(d.asString());
			}
		);
	
	// default the position of the selects to today
	var today = new Date();
	//($('#StartDateDay')[0]).selectedIndex = today.getDate() - 1;
	//($('#StartDateMonth')[0]).selectedIndex = today.getMonth();
	//($('#StartDateYear')[0]).selectedIndex = today.getFullYear() - 2007;
	
	// and update the datePicker to reflect it...
	$('#StartDateDay').trigger('change');
	


	// initialise the "Select date" link
	$('#date-pick-end')
		.datePicker(
			// associate the link with a date picker
			{
				createButton:false,
				startDate:todayDate,
				endDate:'31/12/' + endyear
			}
		).bind(
			// when the link is clicked display the date picker
			'click',
			function()
			{
				updateSelectsEnds($(this).dpGetSelected()[0]);
				$(this).dpDisplay();
				bindOnce = true;
				return false;
			}
		).bind(
			// when a date is selected update the SELECTs
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				updateSelectsEnds(selectedDate);
			}
		).bind(
			'dpClosed',
			function(e, selected)
			{
				updateSelectsEnds(selected[0]);
			}
		);
		
	var updateSelectsEnds = function (selectedDate)
	{
		var d = selectedDate.getDate();
		var m = selectedDate.getMonth();
		var y = selectedDate.getFullYear();
		($('#EndDateDay')[0]).selectedIndex = d - 1;
		($('#EndDateMonth')[0]).selectedIndex = m;
		($('#EndDateYear')[0]).selectedIndex = y - year;
	}
	// listen for when the selects are changed and update the picker
	$('#EndDateDay, #EndDateMonth, #EndDateYear')
		.bind(
			'change',
			function()
			{
				var d = new Date(
							$('#EndDateYear').val(),
							$('#EndDateMonth').val()-1,
							$('#EndDateDay').val()
						);
				$('#date-pick-end').dpSetSelected(d.asString());
			}
		);
	
	// default the position of the selects to today
	var today = new Date();
	//($('#EndDateDay')[0]).selectedIndex = today.getDate() - 1;
	//($('#EndDateMonth')[0]).selectedIndex = today.getMonth();
	//($('#EndDateYear')[0]).selectedIndex = today.getFullYear() - 2007;
	
	// and update the datePicker to reflect it...
	$('#EndDateDay').trigger('change');
});
