$(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 check_data(){
   if(document.getElementById("name").value==="")return false;
   if(document.getElementById("provider").value==="")return false;
   if(document.getElementById("main_ip").value==="")return false;
   if(document.getElementById("support").value==="")return false;
   if(document.getElementById("login").value==="")return false;
   if(document.getElementById("password").value==="")return false;
   return true;
}

function clear_data(){
   document.getElementById("name").value="";
   document.getElementById("provider").value="";
   document.getElementById("main_ip").value="";
   document.getElementById("support").value="";
   document.getElementById("login").value="";
   document.getElementById("password").value="";
}

function add(){
    if(check_data()){
        processing_show();
        $.ajax({
            url: 'scripts.php?action=add',
            type: 'post',
            data: {
                name: document.getElementById("name").value,
                provider: document.getElementById("provider").value,
                main_ip: document.getElementById("main_ip").value,
                support: document.getElementById("support").value,
                login: document.getElementById("login").value,
                password: document.getElementById("password").value
            },
            success: function(data) {
                var str=data.replace(/(\r\n|\n|\r)/gm,"");
                if(str==="0"){ 
                    $("#message-success").slideDown("slow");
                    setTimeout(function(){close_message_success();},3000);
                    clear_data(); 
                }
                if(str==="1"){ 
                    $("#message-custom").text("Server Name already in use");
                    $("#message-warning-custom").slideDown("slow");
                    setTimeout(function(){close_message_warning_custom();},3000);
                }
                if(str==="2"){ 
                    $("#message-error").slideDown("slow");
                    setTimeout(function(){close_message_error();},3000);
                }
                processing_hide();
            },
            error: function() {
                $("#message-error").slideDown("slow");
                setTimeout(function(){close_message_error();},3000);
                processing_hide();
            }
            
        }); 
    }else{
        $("#message-warning").slideDown("slow");
        setTimeout(function(){close_message_warning();},3000);
    }
}

function refresh()
{
    processing_show();
    $.ajax({
        url: 'scripts.php?action=show',
        type: 'get',
        success: function(data) {
            var list = JSON.parse(data);
            var activeon='';
            var activeoff='';
            $('#show').dataTable().fnClearTable();
            for (var i = 0; i < list.length; i++) {
                if(list[i].active==1){
                    activeon="btn-success disabled";
                    activeoff="btn-default";
                }else{
                    activeon="btn-default";
                    activeoff="btn-warning disabled";
                }
                $('#show').dataTable().fnAddData([ 
                    list[i].id,
                    list[i].name,
                    list[i].provider,
                    "<a href='http://"+list[i].main_ip+"/' target='_blank'>"+list[i].main_ip+"</a>",
                    list[i].support,
                    "<div class='btn-group btn-toggle'><button id='active-on-"+list[i].id+"' class='btn btn-xs "+activeon+"' onclick='active_on("+list[i].id+");'>ON</button><button id='active-off-"+list[i].id+"' class='btn btn-xs "+activeoff+"'' onclick='active_off("+list[i].id+");'>OFF</button></div>",
                    "<i class='icon-edit icon-white' title='Edit' style='cursor: pointer;' onclick="+"location.href='edit.php?id_server="+list[i].id+"'"+"></i>&nbsp;&nbsp;<i class='icon-tasks icon-white' title='VMTAs' style='cursor: pointer;' onclick="+"location.href='vmta/show.php?id_server="+list[i].id+"'"+"></i>&nbsp;&nbsp;<i class='icon-cogs icon-white' title='Configuration' style='cursor: pointer;' onclick="+"location.href='config.php?id_server="+list[i].id+"&name_server="+list[i].name+"'"+"></i>" 
                ]);
            }
            processing_hide();
        },
        error: function() {
            $("#message-error").slideDown("slow");
            setTimeout(function(){close_message_error();},3000);
            processing_hide();
            
        }
    });
}


function refresh2()
{
    processing_show();
    $.ajax({
        url: 'scripts.php?action=show',
        type: 'get',
        success: function(data) {
            var list = JSON.parse(data);
            var activeon='';
            var activeoff='';
            $('#show').dataTable().fnClearTable();
            for (var i = 0; i < list.length; i++) {
                if(list[i].active==1){
                    activeon="btn-success disabled";
                    activeoff="btn-default";
                }else{
                    activeon="btn-default";
                    activeoff="btn-warning disabled";
                }
                $('#show').dataTable().fnAddData([ 
                    list[i].id,
                    list[i].name,
                    list[i].provider,
                    "<a href='http://"+list[i].main_ip+"/' target='_blank'>"+list[i].main_ip+"</a>",
                    list[i].support,
                    "<div class='btn-group btn-toggle'><button id='active-on-"+list[i].id+"' class='btn btn-xs "+activeon+"' onclick='active_on("+list[i].id+");'>ON</button><button id='active-off-"+list[i].id+"' class='btn btn-xs "+activeoff+"'' onclick='active_off("+list[i].id+");'>OFF</button></div>",
                    "<i class='icon-edit icon-white' title='Edit' style='cursor: pointer;' onclick="+"location.href='edit.php?id_server="+list[i].id+"'"+"></i>&nbsp;&nbsp;<i class='icon-tasks icon-white' title='VMTAs' style='cursor: pointer;' onclick="+"location.href='vmta/show.php?id_server="+list[i].id+"'"+"></i>&nbsp;&nbsp;<i class='icon-cogs icon-white' title='Configuration' style='cursor: pointer;' onclick="+"location.href='config.php?id_server="+list[i].id+"&name_server="+list[i].name+"'"+"></i>&nbsp;&nbsp;<i class='icon-remove-circle icon-white' title='Delete' style='cursor: pointer;' onclick="+"location.href='delete.php?id_server="+list[i].id+"'"+"></i>" 

                ]);
            }
            processing_hide();
        },
        error: function() {
            $("#message-error").slideDown("slow");
            setTimeout(function(){close_message_error();},3000);
            processing_hide();
            
        }
    });
}



function set(id){
    if(check_data() && id!=null){
        processing_show();
        $.ajax({
            url: 'scripts.php?action=set',
            type: 'post',
            data: {
                id: id,
                name: document.getElementById("name").value,
                provider: document.getElementById("provider").value,
                main_ip: document.getElementById("main_ip").value,
                support: document.getElementById("support").value,
                login: document.getElementById("login").value,
                password: document.getElementById("password").value
            },
            success: function(data) {
                var str=data.replace(/(\r\n|\n|\r)/gm,"");
                if(str==="0"){ 
                    $("#message-success").slideDown("slow");
                    setTimeout(function(){close_message_success();},3000);
                }
                if(str==="1"){ 
                    $("#message-custom").text("Server Name already in use");
                    $("#message-warning-custom").slideDown("slow");
                     setTimeout(function(){close_message_warning_custom();},3000);
                }
                if(str==="2"){ 
                    $("#message-error").slideDown("slow");
                    setTimeout(function(){close_message_error();},3000);
                }
                processing_hide();
            },
            error: function() {
                $("#message-error").slideDown("slow");
                setTimeout(function(){close_message_error();},3000);
                processing_show();
            }
            
        }); 
    }else{
        $("#message-warning").slideDown("slow");
        setTimeout(function(){close_message_warning();},3000);
    }
}

function active_on(id){
    processing_show();
    $.ajax({
        url: 'scripts.php?action=set_active_on',
        type: 'get',
        data: {
            id: id
        },
        success: function(data) {
            $("#active-off-"+id).removeClass("btn-warning disabled").addClass("btn-default");
            $("#active-on-"+id).addClass("btn-success disabled");
            $("#message-success").slideDown("slow");
            setTimeout(function(){close_message_success();},3000);
            processing_hide();
        },
        error: function() {
            $("#message-error").slideDown("slow");
            setTimeout(function(){close_message_error();},3000);
            processing_hide();
        }
    });
    
}

function active_off(id){
    processing_show();
    $.ajax({
        url: 'scripts.php?action=set_active_off',
        type: 'get',
        data: {
            id: id
        },
        success: function(data) {
            $("#active-on-"+id).removeClass("btn-success disabled").addClass("btn-default");
            $("#active-off-"+id).addClass("btn-warning disabled");
            $("#message-success").slideDown("slow");
            setTimeout(function(){close_message_success();},3000);
            processing_hide();
        },
        error: function() {
            $("#message-error").slideDown("slow");
            setTimeout(function(){close_message_error();},3000);
            processing_hide();
        }
    });
}