/**
 *  Copyright (C) 2022, 2024  Double Bastion LLC
 *
 *  This file is part of Roundpin, which is licensed under the
 *  GNU Affero General Public License Version 3.0. The license terms
 *  are detailed in the "LICENSE.txt" file located in the root directory.
 */

$(document).ready(function() {

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

      var dbName = $("#databasename").val();
      var dbUser = $("#databaseuser").val();
      var dbPass = $("#dbuserpassword").val();

      if ((dbName == '') || (dbUser == '') || (dbPass == '')) { alert("All fields must be filled !"); }

      if (dbName.includes("'")) {
          $("#databasename").val('');
          alert("The database name shouldn't contain single quotes ('). Change the database name and try again !");
      }

      if (dbUser.includes("'")) {
          $("#databaseuser").val('');
          alert("The database user shouldn't contain single quotes ('). Change the database user and try again !");
      }

      if (dbPass.includes("'")) {
          $("#dbuserpassword").val('');
          alert("The database user password shouldn't contain single quotes ('). Change the database user password and try again !");
      }
   });


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

      var adminUser = $('#adminuser').val();

      if (adminUser.includes("'")) {
          $("#adminuser").val('');
          alert("The administrator's username shouldn't contain single quotes ('). Change the administrator's username and try again !");
      }

      // Verify if password meets constraints
      var adminPass = $("#adminuserpassword").val();

      if (adminPass.includes("'")) {
          $("#adminuserpassword").val('');
          alert("The administrator's password shouldn't contain single quotes ('). Change the administrator's password and try again !");
      }

      if (!(/^((?=.*\d)(?=.*[a-z])(?=.*\W).{10,})$/.test(adminPass))) {
          $("#adminuserpassword").val('');
          alert("The password does not meet the requirements (to be at least 10 characters long, to contain at least one letter, at least one digit and at least one special character). Please choose a different password !");
      }

      if ((adminUser == '') || (adminPass == '')) { alert("All fields must be filled !"); }

      // Verify if the email address is correctly formatted
      var adminEmailAddress = $("#adminuseremail").val();

      if (adminEmailAddress == '') {
          alert("The email address cannot be empty !");
      }

      var reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; 

      if (!(reg.test(adminEmailAddress))) {
          $("#adminuseremail").val('');
          alert('Please enter a valid email address');
      }
   });

});
