(function () {

  // body
   	$("#"+link).addClass("active");
    $("#"+sub_link).addClass("active");
    // $(".master_table").dataTable();
    $(".flexselect").flexselect();


    $('.date-picker').datepicker( {
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        todayHighlight: true,
        format: 'M-yyyy',
        onClose: function(dateText, inst) {
            var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
            var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
            $(this).datepicker('setDate', new Date(year, month, 1));
        }
    });



    Offline.options={
                        checkOnLoad: false, // to check the connection status immediatly on page load.
                        interceptRequests: true, // to monitor AJAX requests to check connection.
                        reconnect: { // to automatically retest periodically when the connection is down (set to false to disable).
                                        initialDelay: 3, // delay time in seconds to wait before rechecking.
                                        delay: 10 // wait time in seconds between retries.
                                    },
                        requests: true // to store and attempt to remake requests which failed while the connection was down.
                    };

    Offline.on('confirmed-down', () => {disable_enable_background(true)})

    Offline.on('confirmed-up', () => {disable_enable_background(false)});



}());


var l = window.location;
var base_url = l.protocol + "//" + l.host + "/" + l.pathname.split('/')[1];
/*var base_url = l.protocol + "//" + l.host + "/" + l.pathname.split('/')[1]+ "/" + l.pathname.split('/')[2];*/
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 display_alert_popup(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_popup").html(alert_msg);

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

        $("body, html").animate({'scrollTop':0},1000);
    }

    function display_trans_err(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>';
        }

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

    function reload_page()
    {
        window.location.reload();
    }


/*commomn ajax call function*/
function ajaxCall(callType,path,form_data,datatype,res_callback,err_callback)
{
    disable_enable_background(true)
    $.ajax({
        type: ''+callType+'',
        url:path,
        data:form_data,
        dataType:''+datatype+'',
        success:function(response)
        {
            console.log(response)
            res_callback(response);
            disable_enable_background(false)
        },
        error:function(error)
        {
            alert('Something went wrong!')
            console.log(error)
            err_callback(error);
            disable_enable_background(false)
        }
    });
}

function fileUpAjaxCall(callType,path,form_data,datatype,res_callback,err_callback)
{
    disable_enable_background(true)
    $.ajax({
        type: ''+callType+'',
        url:path,
        data:form_data,
        dataType:''+datatype+'',
        contentType:false,
        processData:false,
        success:function(response)
        {
            console.log(response)
            res_callback(response);
            disable_enable_background(false)
        },
        error:function(error)
        {
            alert('Something went wrong!')
            console.log(error)
            err_callback(error);
            disable_enable_background(false)
        }
    });
}

function disable_enable_background(flag)
{
    if(flag) //disable background
    {
        $("#loading").show();
        $(".main-header, .content-wrapper, .modal").css({'pointer-events':'none', 'opacity' : '0.4'});
    }
    else //enable background
    {
        $("#loading").hide();
        $(".main-header, .content-wrapper, .modal").css({'pointer-events':'auto', 'opacity' : '1'});
    }

}