$(document).ready(function() {

	/* Initialize Datepicker */
    if ($('.calendar').length>0) {
        /* generate a variable for today's date in case the date picker has to be limited */
        limit = new Date();
        thisYear = limit.getYear();
        thisMonth = limit.getMonth()+1;
        if (thisYear < 999)
            thisYear += 1900;
        // limitStart = thisMonth+'/'+limit.getDate()+'/'+thisYear;

        limitStart = '01/01/2001';
        // initialise the "Select date" link
        $('.calendar')
            .datePicker(
                // associate the link with a date picker
                {
                    createButton:false,
                    startDate: limitStart
                }
            ).bind(
                    // when the link is clicked display the date picker
                    'click',
                    function(){
                    /*	updateInputs($(this).dpGetSelected()[0], this);  */
                        $(this).dpDisplay();
                        return false;
                    }
            ).bind(
                    /* when a date is selected update the SELECTs */
                    'dateSelected',
                    function(e, selectedDate, $td, state){
                        updateInputs(selectedDate, this);
                    }
            ).bind(
                    'dpClosed',
                    function(e, selected){
                        updateInputs(selected[0], this);
                    }
            );
	}
	
	var updateInputs = function (selectedDate, that)
	{
		var selectedDate = new Date(selectedDate);
		$('input.tag[rel='+ $(that).attr('rel') +']').attr('value', selectedDate.getDate()||'')
		$('input.monat[rel='+ $(that).attr('rel') +']').attr('value', selectedDate.getMonth()+1||'')
		$('input.jahr[rel='+ $(that).attr('rel') +']').attr('value', selectedDate.getFullYear()||'')
	}
});
