$(document).ready(function(){
  $('[data-toggle="tooltip"]').tooltip();

    Offline.on('confirmed-down', () => {$('.online').find('button').prop('disabled', true)})

    Offline.on('confirmed-up', () => {$('.online').find('button').prop('disabled', false)});
});
var l = window.location;
var base_url = l.protocol + "//" + l.host + "/" + l.pathname.split('/')[1];
base_url += '/';

/************************************** Display Error Function **************************************/

    function display_alert(type,message)
    {
    	var alert_msg = '';

        if(type == 'err')
        {
        	alert_msg += '<div class="alert alert-danger" role="alert">';
            alert_msg += '<span><strong>'+message+'</strong></span></div>';
        }
        else if(type == 'warn')
        {
            alert_msg += '<div class="alert alert-warning" role="alert">';
            alert_msg += '<span ><strong>'+message+'</strong></span></div>';
        }
        else if(type == 'succ')
        {
            alert_msg += '<div class="alert alert-success" role="alert">';
            alert_msg += '<span><strong>'+message+'</strong></span></div>';
        }

        $(".alert_msg").html(alert_msg);

        setTimeout(function(){
    		$(".alert_msg").html("");
        },5000);
    }

        function login_btn()
    {
        var check = 1;

        if($("#user_name").val() == '')
        {
            $("#user_name").css({'border-color':'red'});
            check = 0;
        }
        else
        {
            $("#user_name").css({'border-color':'#CCC'});
        }

        if($("#user_password").val() == '')
        {
            $("#user_password").css({'border-color':'red'});
            check = 0;
        }
        else
        {
            $("#user_password").css({'border-color':'#CCC'});
        }

        if(check == 0)
        {
            var msg = 'Oh snap ! You forgot to enter some information';
            display_alert('err',msg);
            $("body, html").animate({'scrollTop':0},2000);
        }
        else
        {
            var str = $("#login_form").serialize();

            $("#loading").show();

            $.ajax({
                type:"POST",
                data:str,
                url:base_url+"loginctrl/login_action",
                success: result =>
                {
                    console.log(result);

                    if(result == 1)
                    {
                        window.location.href=base_url+"master/masterctrl";
                    }
                    else if (result == 2)
                    {
                        var msg = 'Wrong Password !';
                        display_alert('err',msg);
                        $("body, html").animate({'scrollTop':0},2000);
                    }
                    else if (result == 3)
                    {
                        var msg = 'Invalid User Name !';
                        display_alert('err',msg);
                        $("body, html").animate({'scrollTop':0},2000);
                    }
                    else
                    {
                        console.log('no data');
                    }

                    $("#loading").hide();
                }
            });

        }

    }
