$(document).ready(function() {

 $("#savenewdetdir").on("click", function() {

   var newdir = $("#currentdetdir").val();

   if (newdir != '') {

       // Validate folder path
       if (/^(\/[\w^]+)+?$/.test(newdir)) {

           $.ajax({
               type: "POST",
               url: "save-new-detections-dir.php",
               dataType: "JSON",
               data: {newdir: newdir},
               success: function(resp) {
                     if (resp.result == 'success') {
                             $("#savenewdettext").empty();
                             $("#savenewdettext").append("<span>"+resp.messagetosend+"</span>");
                             $("#savenewdettext").css("color", "#10ba65");
                     } else {
                             $("#savenewdettext").empty();
                             $("#savenewdettext").append("<span>"+resp.messagetosend+"</span>");
                             $("#savenewdettext").css("color", "#AC1F23");
                       }
               },
               error: function(resp) {
                         $("#savenewdettext").empty();
                         $("#savenewdettext").append("<span>An error occurred while processing data !</span>");
                         $("#savenewdettext").css("color", "#AC1F23");
               }
           });

       } else {
           $("#savenewdettext").empty();
           $("#savenewdettext").append("<span>The input is not a valid directory path. Please enter a valid directory path without a slash at the end !</span>");
           $("#savenewdettext").css("color", "#AC1F23");
       }

  } else {
           $("#savenewdettext").empty();
           $("#savenewdettext").append("<span>Please enter the directory for the ClamAV periodic scan detections !</span>");
           $("#savenewdettext").css("color", "#AC1F23");
  }

 });

});

