$(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("username").value==="")return false;
   if(document.getElementById("password").value==="")return false;
   if(document.getElementById("confirm_password").value==="")return false;
   if(document.getElementById("username").value.length<6){
      $("#message-custom").text("Username Too Short");
      $("#message-warning-custom").slideDown("slow");
      setTimeout(function(){close_message_warning_custom();},3000);
      return false;
   }
   if(document.getElementById("password").value.length<6){
      $("#message-custom").text("Password Too Short");
      $("#message-warning-custom").slideDown("slow");
      setTimeout(function(){close_message_warning_custom();},3000);
      return false;
   }
   if((document.getElementById("password").value) != (document.getElementById("confirm_password").value)){
      $("#message-custom").text("Password Not Match");
      $("#message-warning-custom").slideDown("slow");
      setTimeout(function(){close_message_warning_custom();},3000);
      return false;
   }
   return true;
}

function clear_data(){
   document.getElementById("username").value="";
   document.getElementById("password").value="";
   document.getElementById("confirm_password").value="";
}

function add(){
    if(check_data()){
        processing_show();
        $.ajax({
            url: 'scripts.php?action=add',
            type: 'post',
            data: {
                username: document.getElementById("username").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("Username 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 color="";
            $('#show').dataTable().fnClearTable();
            for (var i = 0; i < list.length; i++) {
                switch(list[i].status){
                    case 'Active': color='success';break;
                    case 'Under Review': color='info';break;
                    case 'Stopped Temporarily': color='warning';break;
                    case 'Suspended': color='important';break;
                    case 'Cancelled': color='inverse';break;
                    default : color='';
                }
                $('#show').dataTable().fnAddData([ 
                    list[i].id,
                    list[i].username,
                    "<div class='center' id='idc-status"+list[i].id+"'><span class='badge badge-"+color+"'>"+list[i].status+"</span></div>",
                    "<i class='icon-edit icon-white' title='Edit' style='cursor: pointer;' onclick="+"location.href='edit.php?id_it="+list[i].id+"'"+"></i>&nbsp;&nbsp;<i class='icon-remove-circle icon-white' title='Delete' style='cursor: pointer;' onclick="+"location.href='scripts.php?action=delete&id_it="+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,
                username: document.getElementById("username").value,
                password : document.getElementById("password").value,
                status : document.getElementById("status").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("Username 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 refresh_log(){
    processing_show();
    $.ajax({
        url: 'scripts.php?action=show_log',
        type: 'get',
        success: function(data) {
            var list = JSON.parse(data);
            var color="";
            $('#show').dataTable().fnClearTable();
            for (var i = 0; i < list.length; i++) {
                switch(list[i].status){
                    case 'Active': color='success';break;
                    case 'Under Review': color='info';break;
                    case 'Stopped Temporarily': color='warning';break;
                    case 'Suspended': color='important';break;
                    case 'Cancelled': color='inverse';break;
                    default : color='';
                }
                $('#show').dataTable().fnAddData([ 
                    list[i].id,
                    list[i].first_name,
                    list[i].last_name,
                    list[i].username,
                    "<div class='center' id='idc-status"+list[i].id+"'><span class='badge badge-"+color+"'>"+list[i].status+"</span></div>",
                    "<i class='icon-edit icon-white' title='Edit' style='cursor: pointer;' onclick="+"location.href='edit.php?id_mailer="+list[i].id+"'"+"></i>&nbsp;&nbsp;<i class='icon-file icon-white' title='Log' style='cursor: pointer;' onclick="+"location.href='log.php?id_mailer="+list[i].id+"'"+"></i>" 
                ]);
            }
            processing_hide();
        },
        error: function() {
            $("#message-error").slideDown("slow");
            setTimeout(function(){close_message_error();},3000);
            processing_hide();
        }
    });
}