/**
 *  Copyright (C) 2021  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() {

  $("#emailforgotpass").focus();

  $('#emailforgotpass').on('input', function() {
    if ($(this).val().length != 0) {
        $("#emailaddressfgtLabel").css({'display' : 'block', 'opacity' : '1'});
    } else { $("#emailaddressfgtLabel").css('display','none'); }
  });

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

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

         if (emailforgotpreq == '') {
             var messageEmail = "Please enter your email address !";
             alert(messageEmail);
         } else if (/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/.test(emailforgotpreq)) {
             var messageEmail = '';
         } else {
             var messageEmail = "The email address is not valid !";
             alert(messageEmail);
           }

         if (messageEmail == '') {

               $.ajax({
                 type: "POST",
                 url: "src/regain-access.php",
                 dataType: "JSON",
                 data: {
                        emailforgot: emailforgotpreq,
                        messageToUser: messageEmail,
                        verifyToken: getVerifyToken
                       },
                 success: function(reqresponse) {
                     if (reqresponse.result == 'success') {
                         $("#submitmessage").empty();
                         $("#submitmessage").append("<span>"+reqresponse.messageonrequest+"</span>");
                         $("#submitmessage").css("color", "#10ba65");
                     } else {
                         $("#submitmessage").empty();
                         $("#submitmessage").append("<span>"+reqresponse.messageonrequest+"</span>");
                         $("#submitmessage").css("color", "#AC1F23");
                       }
                 },
                 error: function(reqresponse) {
                     $("#submitmessage").empty();
                     $("#submitmessage").append("<span>An error occurred while processing data !</span>");
                     $("#submitmessage").css("color", "#AC1F23");
                 }

               });
         }
  });
});
