$(document).ready(function() {

  // Change password
  $("#savenewpassword").on("click", function() {

     var currentPassword = $("#currentpassword").val();
     var newPassword = $("#newpassword").val();
     var confirmNewPassword = $("#confirmpassword").val();

         if ((currentPassword != '') && (newPassword != '') && (confirmNewPassword != '')) {

            if (newPassword == confirmNewPassword) {

                if (/^((?=.*\d)(?=.*[a-z])(?=.*\W).{10,})$/.test(newPassword)) {

                   $.ajax({
                     type: "POST",
                     url: "save-password.php",
                     dataType: "JSON",
                     data: {
                            currentPass: currentPassword,
                            newPass: newPassword,
                            verifyKey: getVerifyKey
                           },
                     success: function(resp) {
                         if (resp.result == 'success') {
                             $("#savenewpasstext").empty();
                             $("#savenewpasstext").append("<span>"+resp.messagetosend+"</span>");
                             $("#savenewpasstext").css("color", "#10ba65");
                         } else {
                             $("#savenewpasstext").empty();
                             $("#savenewpasstext").append("<span>"+resp.messagetosend+"</span>");
                             $("#savenewpasstext").css("color", "#AC1F23");
                           }
                     },
                     error: function(resp) {
                         $("#savenewpasstext").empty();
                         $("#savenewpasstext").append("<span>An error occurred while processing data !</span>");
                         $("#savenewpasstext").css("color", "#AC1F23");
                     }

                   });

                } else {
                    $("#savenewpasstext").empty();
                    $("#savenewpasstext").append("<span>The new 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 !</span>");
                    $("#savenewpasstext").css("color", "#AC1F23");
              }

            } else {
                    $("#savenewpasstext").empty();
                    $("#savenewpasstext").append("<span>The new password fields do not match !</span>");
                    $("#savenewpasstext").css("color", "#AC1F23");
              }

         } else {
                  $("#savenewpasstext").empty();
                  $("#savenewpasstext").append("<span>All fileds are required !</span>");
                  $("#savenewpasstext").css("color", "#AC1F23");
           }

  });

});
