$(document).ready(function()
{
    $('#begin, #start, #end, #finish').change(function()
    {
        var begin  = $('#begin').val().replace(/-/g, '/');
        var end    = $('#end').val().replace(/-/g, '/');
        var start  = $('#start').val();
        var finish = $('#finish').val();
        if(!begin || !end || !start || !finish) return false;

        var url = createLink('attend', 'ajaxGetHours', 'type=makeup&begin=' + begin + '&start=' + start + '&end=' + end + '&finish=' + finish);
        $.getJSON(url, function(data)
        {
            if(data.result == 'success') $('#hours').val(data.hours);
        });
    });
 
    $('#begin, #end').change(function()
    {
        var begin      = $('#begin').val();
        var end        = $('#end').val();
        var otherDomID = $(this).attr('id') == 'begin' ? 'end' : 'begin';
        if(begin)
        {
            var date  = begin.split('-');
            var year  = date[0];
            var month = date[1];
            var day   = date[2];
            date      = new Date(year, month, 0);
            var startDate = year + '-' + month + '-' + day;
            var endDate   = year + '-' + month + '-' + date.getDate();

            $('#end').datetimepicker('setStartDate', startDate);
            $('#end').datetimepicker('setEndDate', endDate);
        }
        if(end)
        {
            var date  = end.split('-');
            var year  = date[0];
            var month = date[1];
            var day   = date[2];
            var startDate = year + '-' + month + '-01';
            var endDate   = year + '-' + month + '-' + day;
            
            $('#begin').datetimepicker('setStartDate', startDate);
            $('#begin').datetimepicker('setEndDate', endDate);
        }
        if(!$(this).val())
        {
            $('#' + otherDomID).datetimepicker('setStartDate', '');
            $('#' + otherDomID).datetimepicker('setEndDate', '');
        }
    })

    $('#begin').change(function()
    {
        $.getJSON(createLink('makeup', 'ajaxGetToMakeup', 'date=' + $(this).val().replace(/-/g, '/')), function(data)
        {
            if(data.result == 'success') $('#timePrompt').text(data.tip);
        });
    });

    $('#begin, #end').change();
})
