$(function()
{
    $('#allip').change(function()
    {
        if($(this).prop('checked'))
        {
            $('#ip').attr('disabled', 'disabled');
        }
        else
        {
            $('#ip').removeAttr('disabled');
        }
    })

    $('.form-time').change(function()
    {
        const signIn       = Date.parse(new Date(v.date + ' ' + $('#signInLimit').val()));
        const signOut      = Date.parse(new Date(v.date + ' ' + $('#signOutLimit').val()));
        const lunchBegin   = Date.parse(new Date(v.date + ' ' + $('#lunchBegin').val()));
        const lunchEnd     = Date.parse(new Date(v.date + ' ' + $('#lunchEnd').val()));
        const dinnerBegin  = Date.parse(new Date(v.date + ' ' + $('#dinnerBegin').val()));
        const dinnerEnd    = Date.parse(new Date(v.date + ' ' + $('#dinnerEnd').val()));
        const lunchRest    = computeRestTime(signIn, signOut, lunchBegin, lunchEnd);
        const dinnerRest   = computeRestTime(signIn, signOut, dinnerBegin, dinnerEnd);
        const restTime     = signOut - signIn - lunchRest - dinnerRest;
        const workingHours = Math.round(restTime / (3600 * 1000) * 100) / 100;
        $('#workingHours').val(workingHours);
    })

    $('#signInLimit').change();
})

function computeRestTime(start, finish, restStart, restFinish)
{
    if(!restStart || !restFinish) return 0;
    if(start > restFinish || finish < restStart) return 0;

    realStart  = start  > restStart  ? start  : restStart;
    realFinish = finish < restFinish ? finish : restFinish;

    const time = realFinish - realStart;
    if(time < 0) time = 0;

    return time;
}
