---
---
// Do not remove `---` tags. These are the frontmatter tags for Jekyll variables.
/**
 * This script contains helper functions for the agreement checkboxes on `Getting Help` page.
 *
 * Please see `/getting-help/service-section.html` for usage.
 */
'use strict';

$(
    function () {
        const $confirmPersonalInformation = $('#confirm-personal-information');
        const $confirmDevelopmentAgreement = $('#confirm-development-agreement');
        const $orderButton = $('#order-now-btn');
        const $redirect = $('#redirect');
        const $loader = $('.redirect-loader', '#redirect');
        const $errorContainer = $('.redirect-error', '#redirect');
        const $tryAgain = $('#try-again');

        const orderUrl = "{{site.data.payment_config.orderUrl}}";
        const prodApiUrl = "{{site.data.payment_config.prodApiUrl}}";
        const devApiUrl = "{{site.data.payment_config.devApiUrl}}";
        // `window.mode` we setup in the `getting-help/service-section.html` file by Jekyll.
        const apiUrl = "{{jekyll.environment}}" === "development" ? devApiUrl : prodApiUrl;

        $confirmPersonalInformation.change(() => {
            confirmAgreement($orderButton);
        });

        $confirmDevelopmentAgreement.change(() => {
            confirmAgreement($orderButton);
        });

        $orderButton.click(() => {
            submitOrder();
        });

        $tryAgain.click(e => {
            e.preventDefault();
            hideRedirect();
            submitOrder();
        });

        /**
         * Disables and enables order button.
         *
         * @param {Object} disabledElement - disabled/enabled element
         */
        function confirmAgreement(disabledElement) {
            const isConfirmed = $confirmPersonalInformation.prop('checked') && $confirmDevelopmentAgreement.prop('checked');
            if (isConfirmed) {
                disabledElement.removeAttr('disabled');
                disabledElement.removeClass('disabled');
            } else {
                disabledElement.attr('disabled', true);
                disabledElement.addClass('disabled');
            }
        };

        /**
         * Submit order handler.
         */
        function submitOrder() {
            const dataProcessingConsent = $confirmPersonalInformation.prop("checked");
            const supportAgreementConsent = $confirmDevelopmentAgreement.prop("checked");
            const data = JSON.stringify({dataProcessingConsent, supportAgreementConsent});
            const transactionUrl = `${apiUrl}/transaction`;
            sendPaymentTransaction(transactionUrl, data);
        };

        /**
         * Sends the transaction data and returns the transaction ID. If the request is successful,
         * redirects to the Payment screen.
         *
         * @param {String} transactionUrl - the API URL
         * @param {Object} data - the transaction data
         */
        function sendPaymentTransaction(transactionUrl, data) {
            showRedirect(false);
            $.ajax(transactionUrl, {
                type: 'POST',
                data: data,
                contentType: 'application/json',
                success: (data) => {
                    const obj = JSON.parse(data);
                    const paymentUrl = `${orderUrl}&CUSTOMERID=${obj.id}`;
                    window.location = paymentUrl;
                    hideRedirect();
                },
                error: (jqXhr) => {
                    console.error(jqXhr.status + ': ' + jqXhr.statusText);
                    showRedirect(true);
                }
            });
        };

        /**
         * Shows the redirect screen.
         *
         * @param {Boolean} isError - if it is true, hides the loader and shows the error section
         */
        function showRedirect(isError) {
            $redirect.show();
            if (isError) {
                $errorContainer.show();
                $loader.hide();
            }
        };

        /**
         *  Hides the redirect screen.
         */
        function hideRedirect() {
            $redirect.hide();
        }
    }
);

@@ -521,16 +521,22 @@
 redirect
+Screen
  = $('#r
@@ -534,32 +534,38 @@
 n = $('#redirect
+Screen
 ');%0A        cons
@@ -600,32 +600,38 @@
 der', '#redirect
+Screen
 ');%0A        cons
@@ -681,16 +681,22 @@
 redirect
+Screen
 ');%0A    
@@ -1373,52 +1373,19 @@
 ick(
-() =%3E %7B%0A            submitOrder();%0A        %7D
+submitOrder
 );%0A%0A
@@ -1603,22 +1603,22 @@
 @param %7B
-Object
+jQuery
 %7D disabl
@@ -2134,33 +2134,32 @@
      %7D%0A        %7D
-;
 %0A%0A        /**%0A  
@@ -2621,33 +2621,392 @@
 data);%0A        %7D
-;
+%0A%0A        /**%0A         * The consent data.%0A         * @typedef %7Bobject%7D Consent%0A         * @property %7Bboolean%7D dataProcessingConsent - Indicates whether the user give a consent to processing of his%0A         * personal information.%0A         * @property %7Bboolean%7D supportAgreementConsent - Indicates whether the user agree to be bound by the terms of%0A         */
 %0A%0A        /**%0A  
@@ -3237,13 +3237,14 @@
 am %7B
-Objec
+Consen
 t%7D d
@@ -3976,33 +3976,32 @@
    %7D);%0A        %7D
-;
 %0A%0A        /**%0A  
@@ -4219,16 +4219,22 @@
 redirect
+Screen
 .show();
@@ -4356,17 +4356,16 @@
        %7D
-;
 %0A%0A      
@@ -4451,24 +4451,24 @@
 edirect() %7B%0A
-
             
@@ -4476,16 +4476,22 @@
 redirect
+Screen
 .hide();

Add JSDocs, cleanup code.