function date_time_validate(id_data_start,id_data_stop,id_h_start,id_h_end,id_m_start,id_m_end)
{

    var data        = $("#"+id_data_start).val();
    var data_stop   = $("#"+id_data_stop).val();
    var h_start     = $("#"+id_h_start).val();
    var h_end       = $("#"+id_h_end).val();
    var m_start     = $("#"+id_m_start).val();
    var m_end       = $("#"+id_m_end).val();

    if(!data || !data_stop || data > data_stop)
    {
        return 1;
    }

    var nowDate = new Date();

    var y = nowDate.getFullYear().toString();      
    var m = (nowDate.getMonth() + 1).toString();   
    var d = nowDate.getDate().toString();          

    if(m.length === 1){ m = '0' + m; }     
    if(d.length === 1){ d = '0' + d; }

    var currDate = y+'-'+m+'-'+d;

    if( data < currDate )
    {
        return 2;
    }

    if(
            !h_start || !h_end || m_start.length === 0 || m_end.length === 0 ||
            h_start > 23 || h_end > 23 || m_start > 59 || m_end > 59 ||
            h_start < 5 || h_end < 5 || m_start < 0 || m_end < 0
        )
    {
        return 3;
    }


    if(data == data_stop)
        {
            if(h_start > h_end)
                return 3;
            else if(h_start === h_end)
                if(m_start >= m_end)
                {
                    return 3;
                }
        }
        
    if(data == currDate)
    {
        var currHour = nowDate.getHours();    

        if( parseInt(h_start) < parseInt(currHour) )
        {
            return 2;
        }

        var currMinute = nowDate.getMinutes();

        if(parseInt(h_start) == parseInt(currHour))
            if( m_start < currMinute )
            {
                return 2;
            }
    }
    return 0;
}
function refreshDate(data_start, data_stop)
{
        $("#"+data_start).change(function(){ 
            
            var d_start = $("#"+data_start).val();
            var d_stop  = $("#"+data_stop).val();
            
            if(d_start > d_stop)
            {
                $("#"+data_stop).val(d_start);
            }
        });
}
function date_validate(id_data_start,id_data_stop)
{
    var data        = $("#"+id_data_start).val();
    var data_stop   = $("#"+id_data_stop).val();

    if(!data || !data_stop || data > data_stop)
    {
        return 1;
    }

    var nowDate = new Date();

    var y = nowDate.getFullYear().toString();      
    var m = (nowDate.getMonth() + 1).toString();   
    var d = nowDate.getDate().toString();          

    if(m.length === 1){ m = '0' + m; }     
    if(d.length === 1){ d = '0' + d; }

    var currDate = y+'-'+m+'-'+d;

    if( data < currDate )
    {
        return 2;
    }
}

// 1811

var g_edit_h    =   '';
var g_edit_m    =   '';
var limit       =   15;

function refreshHour(id_h_start, id_m_start, id_h_end, id_m_end)
{
    $("#"+id_h_start).blur(function(){

        if(!g_edit_h)
            return false;

        var start_h = $("#"+id_h_start).val();
        var end_h   = $("#"+id_h_end).val();

        var start_m = $("#"+id_m_start).val();

        if(!start_h)
            $("#"+id_h_start).val("05");

        if(start_h < 5)
            $("#"+id_h_start).val("05");

        if(start_h > 23)
            $("#"+id_h_start).val("23");

        try
        {
            var h = parseInt(start_h);
            if(h >= 24)
                return false;
        }
        catch(s)
        {
            return false;
        }


        start_h = $("#"+id_h_start).val();
        if(start_h.length == 1)
        {
            start_h = "0"+start_h;
            $("#"+id_h_start).val(start_h);
        }

        if(start_h > end_h)
        {
            var new_end_h = "24";
            if(parseInt(start_m) < 30)
            {
                new_end_h = parseInt(start_h) + "";
                $("#"+id_m_end).val("30");
            }
            else
                new_end_h = parseInt(start_h) + 1 + "";
            
            new_end_h = new_end_h.length==1?"0"+new_end_h:new_end_h;
            
            $("#"+id_h_end).val(new_end_h);
            if(new_end_h == 24)
                $("#"+id_m_end).val("00");
        }
        
        g_edit_m = true;
        $("#"+id_m_start).blur();
        
        g_edit_h = false;
    });

    $("#"+id_m_start).blur(function(){

        if(!g_edit_m)
            return false;
        
        var start_h = $("#"+id_h_start).val();
        var end_h   = $("#"+id_h_end).val();
        var start_m = $("#"+id_m_start).val();

        if(!start_m)
            return false;

            if(start_m < 0)
            {
                $("#"+id_m_start).val("00");
            }
            else if(start_m > 59)
            {
                $("#"+id_m_start).val("30");
            }

        start_m = $("#"+id_m_start).val();

        if(intervalOk(id_h_start, id_m_start, id_h_end, id_m_end, limit))
        {
            
        }
        else
        {
            var withLimit   =   (parseInt(start_m)+limit) % 60 + '';
            withLimit       =   withLimit.length === 1?'0'+withLimit:withLimit;
            $("#"+id_m_end).val(withLimit);
        }
        
        var new_h_end = $("#"+id_h_start).val();
        new_h_end = new_h_end.length==1?"0"+new_h_end:new_h_end;
        $("#"+id_h_end).val(new_h_end);

        if(start_h > end_h)
        {
            var new_end_h = "24";
            if(parseInt(start_m) < 30)
            {
                new_end_h = parseInt(start_h) + "";
                $("#"+id_m_end).val(parseInt(start_m)+30);
            }
            else
                new_end_h = parseInt(start_h) + 1 + "";
            
            new_end_h = new_end_h.length==1?"0"+new_end_h:new_end_h;
            $("#"+id_h_end).val(new_end_h);
            if(new_end_h == 24)
                $("#"+id_m_end).val("00");
        }

        start_h = $("#"+id_h_start).val();
        end_h   = $("#"+id_h_end).val();

        if(start_h == end_h)
        {

            var end_m = $("#"+id_m_end).val();
            var new_h = parseInt(start_h) + 1 + '';
            new_h = new_h.length==1?"0"+new_h:new_h;

            if(start_m > end_m)
            {
                $("#"+id_h_end).val(new_h);
            }
            else if(start_m == end_m)
            {
                if(start_m < 30)
                {
                    $("#"+id_m_end).val("30");
                }
                else
                {
                    $("#"+id_m_end).val("00");
                    $("#"+id_h_end).val(new_h);
                }
            }
        }
         
        g_edit_m = false;
        
    });
    
    $("#"+id_h_end).blur(function(){
        
        var start_h = $("#"+id_h_start).val();
        var end_h   = $("#"+id_h_end).val();

        var start_m = $("#"+id_m_start).val();
        var end_m   = $("#"+id_m_end).val();

        if(!start_h)
        {
            start_h = "05";
            $("#"+id_h_start).val(start_h);
        }
        
        if(end_h)
        {
            end_h = end_h.length==1?"0"+end_h:end_h;
            $("#"+id_h_end).val(end_h);
            if(end_h < start_h)
            {
                g_edit_h = true;
                $("#"+id_h_start).blur();
            }
            else if(end_h == start_h)
            {
                if(start_m && end_m)
                    if(end_m <= start_m)
                    {
                        g_edit_h = true;
                        $("#"+id_h_start).blur();
                    }
            }
        }
            
        $("#"+id_m_end).blur();
        
    });
    
    $("#"+id_m_end).blur(function(){
        var start_h = $("#"+id_h_start).val();
        var end_h   = $("#"+id_h_end).val();

        var start_m = $("#"+id_m_start).val();
        var end_m   = $("#"+id_m_end).val();
        
        if(start_h && end_h && start_m && end_m)
            if(parseInt(start_h) == parseInt(end_h))
            {
                g_edit_m = true;
                $("#"+id_m_start).blur();
            }
        
    });
    
    $("#"+id_h_start).keypress(function(){
        g_edit_h = true;
    });
    
    $("#"+id_m_start).keypress(function(){
        g_edit_m = true;
    });
    
}

function limitOfInterval(id_h_start, id_m_start, id_h_end, id_m_end)
{
    var start_h = $("#"+id_h_start).val();
    var end_h   = $("#"+id_h_end).val();
    var start_m = $("#"+id_m_start).val();
    var end_m   = $("#"+id_m_end).val();

    try
    {

        if(start_h && end_h && start_m && end_m)
        {
            
            start_h = parseInt(start_h);
            end_h   = parseInt(end_h);
            start_m = parseInt(start_m);
            end_m   = parseInt(end_m);
            

            var h_diff = Math.abs(start_h-end_h);
            
            var diff = 60;
            
            if(h_diff == 0)
            {
                diff = Math.abs(start_m - end_m);
            }
            else if(h_diff == 1)
            {
                diff = 60 - start_m + end_m;
            }
            
            return diff;
        }  
        
    }catch(s)
    {
        return false;
    }

}

function intervalOk(id_h_start, id_m_start, id_h_end, id_m_end, L)
{
    
    var lim = limitOfInterval(id_h_start, id_m_start, id_h_end, id_m_end);

    if(lim && L)
        return lim >= L;
    
    return false;
}

function edit_date_time_validate(id_data_start,id_data_stop,id_h_start,id_h_end,id_m_start,id_m_end)
{

    var data        = $("#"+id_data_start).val();
    var data_stop   = $("#"+id_data_stop).val();
    var h_start     = $("#"+id_h_start).val();
    var h_end       = $("#"+id_h_end).val();
    var m_start     = $("#"+id_m_start).val();
    var m_end       = $("#"+id_m_end).val();

    if(!data || !data_stop || data > data_stop)
    {
        return 1;
    }

    var nowDate = new Date();

    var y = nowDate.getFullYear().toString();      
    var m = (nowDate.getMonth() + 1).toString();   
    var d = nowDate.getDate().toString();          

    if(m.length === 1){ m = '0' + m; }     
    if(d.length === 1){ d = '0' + d; }

    var currDate = y+'-'+m+'-'+d;

    if( data_stop < currDate )
    {
        return 2;
    }

    if(
            !h_start || !h_end || m_start.length === 0 || m_end.length === 0 ||
            h_start > 23 || h_end > 23 || m_start > 59 || m_end > 59 ||
            h_start < 5 || h_end < 5 || m_start < 0 || m_end < 0
        )
    {
        return 3;
    }


    if(data == data_stop)
        {
            if(h_start > h_end)
                return 3;
            else if(h_start === h_end)
                if(m_start >= m_end)
                {
                    return 3;
                }
        }
        
    if(data_stop == currDate)
    {
        var currHour = nowDate.getHours();    

        if( parseInt(h_start) < parseInt(currHour) )
        {
            return 2;
        }

        var currMinute = nowDate.getMinutes();

        if(parseInt(h_start) == parseInt(currHour))
            if( m_start < currMinute )
            {
                return 2;
            }
    }
    return 0;
}

function edit_date_validate(id_data_start,id_data_stop)
{
    var data        = $("#"+id_data_start).val();
    var data_stop   = $("#"+id_data_stop).val();

    if(!data || !data_stop || data > data_stop)
    {
        return 1;
    }

    var nowDate = new Date();

    var y = nowDate.getFullYear().toString();      
    var m = (nowDate.getMonth() + 1).toString();   
    var d = nowDate.getDate().toString();          

    if(m.length === 1){ m = '0' + m; }     
    if(d.length === 1){ d = '0' + d; }

    var currDate = y+'-'+m+'-'+d;

    if(data < currDate )
    {
        return 2;
    }
}

function refreshRange(start_date,stop_date)
{
    $('#'+start_date).on('change',function(){
                
        var start = Date.parse($(this).val());
        var final = Date.parse($('#'+stop_date).val());

        if(start == "Invalid date" || isNaN(start) || final == "Invalid date" || isNaN(final))
        {
            return false;
        }

        if(start > final)
            $("#"+stop_date).val($("#"+start_date).val());

    });

    $('#'+stop_date).on('change',function(){

        var start = Date.parse($('#'+start_date).val());
        var final = Date.parse($(this).val());

        if(start == "Invalid date" || isNaN(start) || final == "Invalid date" || isNaN(final))
        {
            return false;
        }

        if(start > final)
            $("#"+start_date).val($("#"+stop_date).val());

    });
}