$(document).ready(function() {

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

 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 ! ";
        }

      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,
                    selectrole: getuserrole,
                    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", "");
 }

});

