$(document).ready(function(){
    razmessage();
});

function razmessage(){
  $("#message-warning").hide();
  $("#message-warning-custom").hide();
  $("#message-error").hide();
  $("#message-success").hide();
}

function close_message_warning(){
    $("#message-warning").slideUp("slow");
}

function close_message_warning_custom(){
    $("#message-warning-custom").slideUp("slow");
}

function close_message_error(){
    $("#message-error").slideUp("slow");
}

function close_message_success(){
    $("#message-success").slideUp("slow");
}

function processing_show(){
    $("#processing").html("<div class='progress progress-info progress-striped active'><div class='bar' style='width: 100%;'>&nbsp;&nbsp;Processing...&nbsp;&nbsp;</div></div>");
    $("#processing").show();
}

function processing_hide(){
    $("#processing").hide();
}

function refresh(){
    processing_show();
    $.ajax({
        url: 'scripts.php?action=show',
        type: 'get',
        success: function(data) {
            var list = JSON.parse(data);
            $('#show').dataTable().fnClearTable();
            for (var i = 0; i < list.length; i++) {
                $('#show').dataTable().fnAddData([ 
                    list[i].id,
                    list[i].date_activity,
                    "<span class='badge' style='cursor: pointer;' onclick="+"location.href='mailer.php?id_mailer="+list[i].id_mailer+"'"+">"+list[i].username+"</span>"+" has "+list[i].activity+" on "+"<span class='badge' style='cursor: pointer;' onclick="+"location.href='../../server/edit.php?id_server="+list[i].id_server+"'"+">"+list[i].name_server+"</span>",
                    list[i].connect_ip
                ]);
            }
            processing_hide();
        },
        error: function() {
            $("#message-error").slideDown("slow");
            setTimeout(function(){close_message_error();},3000);
            processing_hide();
        }
    });
}

function refresh_mailer(id){
    processing_show();
    $.ajax({
        url: 'scripts.php?action=show_mailer',
        type: 'get',
        data: {
            id : id
        },
        success: function(data) {
            var list = JSON.parse(data);
            $('#show').dataTable().fnClearTable();
            for (var i = 0; i < list.length; i++) {
                $('#show').dataTable().fnAddData([ 
                    list[i].id,
                    list[i].date_activity,
                    "<span class='badge' style='cursor: pointer;' onclick="+"location.href='../../server/edit.php?id_server="+list[i].id_server+"'"+">"+list[i].name_server+"</span>",
                    list[i].activity,
                    list[i].connect_ip
                ]);
            }
            processing_hide();
        },
        error: function() {
            $("#message-error").slideDown("slow");
            setTimeout(function(){close_message_error();},3000);
            processing_hide();
        }
    });
}