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

 $("#emailaddress").focus();
 $("#submitmessage").empty();

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

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

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

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

 if (signupCheck == 'true') {

   $("#signuptab").on("click", function() {
      $("#signintab").removeClass("active");
      $("#signuptab").addClass("active");
   });

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

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

      if (getemailaddress == '') {
          var messageEmailad = "The email address cannot be empty ! ";
      } else if (/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/.test(getemailaddress)) {
          var messageEmailad = "";
      } else {
          var messageEmailad = "The email address is not valid ! ";
        }

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

      if (/^((?=.*\d)(?=.*[a-z])(?=.*\W).{10,})$/.test(typedPass)) {
          var messagePass = "";
      } else {
          var messagePass = "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 ! ";
        }

      // Verify if password is identical in the two password fields
      var typedPassRepeat = $("#passwordrepeat").val();

      if (typedPassRepeat == typedPass) {
          var messagePass = "";
      } else {
          var messagePass = "Passwords don't match. Please enter your password again ! ";
        }

      var messageToSend = messageEmailad + messagePass;

      var getusername = $("#login").val();
      var getuserrole = $("#selectrole option:selected").val();

      if (messageToSend != '' && (getemailaddress == '' || getusername == '' || typedPass == '' || getuserrole == '')) {
          $("#submitmessage").empty();
          $("#submitmessage").append("<span>"+messageToSend+" All the fields are required !</span>");
          $("#submitmessage").css("color", "#AC1F23");
      } else if (messageToSend != '' && getemailaddress != '' && getusername != '' && typedPass != '' && getuserrole != '') {
          $("#submitmessage").empty();
          $("#submitmessage").append("<span>"+messageToSend+"</span>");
          $("#submitmessage").css("color", "#AC1F23");
      } else if (messageToSend == '' && (getemailaddress == '' || getusername == '' || typedPass == '' || getuserrole == '')) {
          $("#submitmessage").empty();
          $("#submitmessage").append("<span>All the fields are required !</span>");
          $("#submitmessage").css("color", "#AC1F23");
      } else if (messageToSend == '' && getemailaddress != '' && getusername != '' && typedPass != '' && getuserrole != '') {

          $.ajax({
             type: "POST",
             url: "register-user.php",
             dataType: "JSON",
             data: {
                    emailaddress: getemailaddress,
                    login: getusername,
                    password: typedPass,
                    currentmessage: messageToSend,
                    vd_ajax_call: validateToken
                   },
             success: function(response) {
                     if (response.result == 'success') {
                         $("#submitmessage").empty();
                         $("#submitmessage").append("<span>"+response.messageoninsert+"</span>");
                         $("#submitmessage").css("color", "#10ba65");
                     } else {
                         $("#submitmessage").empty();
                         $("#submitmessage").append("<span>"+response.messageoninsert+"</span>");
                         $("#submitmessage").css("color", "#AC1F23");
                       }
             },
             error: function(response) {
                         $("#submitmessage").empty();
                         $("#submitmessage").append("<span>An error occurred while saving the data !</span>");
                         $("#submitmessage").css("color", "#AC1F23");
             }
          });
      }
    });

 } else {
          $("#signupsec").css("cursor", "default");
          $("#signupsec").attr("href", "");
 }
});

