$(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_vertical").value==="")
       return false;
   
    if(document.getElementById("name_open_table").value==="")
        return false;
   
    if(document.getElementById("name_click_table").value==="")
        return false;
   
    return true;
}

function clear_data()
{
   document.getElementById("name_vertical").value="";
   document.getElementById("name_open_table").value="list_open";
   document.getElementById("name_click_table").value="list_click";
}

function add()
{
    if(check_data())
    {
        processing_show();
        $.ajax
        ({
            url     : 'scripts.php?action=add',
            type    : 'post',
            data    : 
            {
                name_vertical       : document.getElementById("name_vertical").value,
                name_open_table     : document.getElementById("name_open_table").value,
                name_click_table    : document.getElementById("name_click_table").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("Vertical 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);
            
            $('#show').dataTable().fnClearTable();
            for (var i = 0; i < list.length; i++) 
            {
                $('#show').dataTable().fnAddData
                ([ 
                    list[i].id_vertical,
                    list[i].name_vertical,
                    list[i].name_open_table,
                    list[i].name_click_table,
                    "<i class='icon-edit icon-white' title='Edit' style='cursor: pointer;' onclick="+"location.href='edit.php?id_vertical="+list[i].id_vertical+"'"+"></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_vertical         : id,
                name_open_table     : document.getElementById("name_open_table").value,
                name_vertical       : document.getElementById("name_vertical").value,
                name_click_table    : document.getElementById("name_click_table").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("ISP 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);
    }
}



