//euc-jp
$(function()
{
    var n = 14;  //  期限 ○日後まで
    var sDate, eDate;

    dd = new Date();
    dd.addDays(5);
    sDate = dd.asString();

    dd.addDays(n-1);
    eDate = dd.asString();

    // 「カレンダーの表示」リンククリック時
    $('#date-pick')
        .datePicker(
            // associate the link with a date picker
            {
                createButton:false,
                startDate: sDate, //'2008/03/03',
                endDate: eDate //'2010/12/31'
            }

        ).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]);
            }
        );

    var updateSelects = function (selectedDate)
    {

        selectedDate = new Date(selectedDate);
        var today = new Date();
        var d = selectedDate.getDate();
        var m = selectedDate.getMonth();
        var y = selectedDate.getFullYear();
        ($('#Date_Day')[0]).selectedIndex = d - 1;
        ($('#Date_Month')[0]).selectedIndex = m;
        ($('#Date_Years')[0]).selectedIndex = y - today.getFullYear();
    }
    // listen for when the selects are changed and update the picker
    $('#Date_Day, #Date_Month, #Date_Years')
        .bind(
            'change',
            function()
            {
                var d = new Date(
                            $('#Date_Years').val(),
                            $('#Date_Month').val()-1,
                            $('#Date_Day').val()
                        );
                $('#date-pick').dpSetSelected(d.asString());
            }
        );

    // default the position of the selects to today
    var today = new Date();
    ($('#Date_Day')[0]).selectedIndex = today.getDate() - 1;
    ($('#Date_Month')[0]).selectedIndex = today.getMonth();
    ($('#Date_Years')[0]).selectedIndex = 0;
    // and update the datePicker to reflect it...
    $('#Date_Day').trigger('change');
});
