PageSourceSearch

https://www.virt-host.com/wp-content/plugins/whmpress_whmcs_cart/assets/js/scripts.js?ver=1791291177

js virt-host.com collected 2026-10-06 12:53:23 UTC 142,252 bytes, 2,781 lines download raw bytes

1/**
2 * Main Script files for wcop plugin
3 */
4(
5    function ($) {
6
7        // Single Page order processing starts below
8        $(document).on('ready', function () {
9            $(document).find('[name="promocode"]').each(function () {
10                let input = $(this);
11                if (input.val()) {
12                    input.closest('.whcom_checkbox_container').find('.whcom_button').trigger('click');
13                }
14            });
15            if (document.location.href.indexOf('pid') > -1) {
16                let urlParams = new URLSearchParams(window.location.search);
17                let pidFromUrl = parseInt(urlParams.get('pid') || '0', 10);
18                let mainForm = $('form.wcop_sp_add_product').first();
19                if (pidFromUrl > 0 && mainForm.length) {
20                    let pidValue = String(pidFromUrl);
21                    let hiddenProductId = mainForm.find('input[name="get_product_id"]');
22                    if (hiddenProductId.length && (!hiddenProductId.val() || parseInt(hiddenProductId.val(), 10) <= 0)) {
23                        hiddenProductId.val(pidValue);
24                    }
25                    let collapsiblePid = mainForm.find('input[name="wcop_collapsible_selected_pid"]');
26                    if (collapsiblePid.length) {
27                        collapsiblePid.val(pidValue);
28                    }
29
30                    let pidRadio = mainForm.find('input[type="radio"][name="pid"][value="' + pidValue + '"]').first();
31                    let pidSelect = mainForm.find('select[id^="wcop_sp_product_select"]').first();
32
33                    if (pidRadio.length) {
34                        pidRadio.prop('checked', true);
35                        pidRadio.closest('label.whcom_label_product.whcom_radio').addClass('whcom_checked');
36                        pidRadio.trigger('change');
37                    } else if (pidSelect.length) {
38                        pidSelect.find('option[value="' + pidValue + '"]').prop('selected', true);
39                        pidSelect.trigger('change');
40                    } else {
41                        let hiddenPid = mainForm.find('input[name="pid"]').first();
42                        if (hiddenPid.length) {
43                            hiddenPid.val(pidValue);
44                        }
45                        if (typeof wcop_sp_update_product_summary === 'function') {
46                            wcop_sp_update_product_summary();
47                        }
48                    }
49                } else {
50                    let productSelect = $('select[id^="wcop_sp_product_select"]').first();
51                    if (productSelect.length) {
52                        productSelect.trigger('change');
53                    } else {
54                        $('input[name="pid"]:checked').first().trigger('change');
55                    }
56                }
57            }
58            $('.wcop_submit_on_load').submit().removeClass('wcop_submit_on_load');
59        });
60        // Change Section Active CSS class
61        $(document).on('click', '.wcop_sp_section', function () {
62            let this_section = $(this);
63            let parent = this_section.closest('.wcop_main');
64            parent.find('.wcop_sp_section').removeClass('wcop_sp_section_active');
65            this_section.addClass('wcop_sp_section_active');
66        });
67
68
69        $(document).on('change', '.wcop_sp_main input, .wcop_sp_main select', function () {
70            let input = $(this);
71            if (input.hasClass('wcop_input')) {
72                wcop_sp_update_product_summary();
73            }
74        });
75        $(document).on('click', '.wcop_sp_nav > ul > li', function () {
76            $(this).addClass('active').siblings('li').removeClass('active');
77        });
78        $(window).on('scroll', function () {
79            let scrollPos = $(document).scrollTop();
80            $('.wcop_sp_nav a.whcom_smooth_scroll').each(function () {
81                let currLink = $(this);
82                let refElement = $(currLink.attr("href"));
83                let topGap = $('.wcop_sp_nav').data('scroll-top-gap') || 20;
84                let elemPositionTop = refElement.position().top;
85                let elemPositionBottom = refElement.position().top + parseInt(refElement.outerHeight(true)) - topGap;
86
87                if (elemPositionTop <= scrollPos && elemPositionBottom > scrollPos) {
88                    currLink.closest('li').trigger("click");
89                }
90            });
91        });
92        // Check Product Domain
93        $(document).on('click', '.whcom_domain_form_item_toggle', function () {
94            let toggle = $(this);
95            let this_option = toggle.closest('.whcom_domain_form_item_container');
96            let parent = toggle.closest('.whcom_domain_form_container');
97            parent.find('.whcom_domain_form_item_container').each(function () {
98                let curr_option = $(this);
99                if (curr_option[0] !== this_option[0]) {
100                    curr_option.children('.whcom_domain_form_item_content').slideUp('fast', function () {
101                        curr_option.removeClass('active');
102                    });
103                }
104            });
105            if (!this_option.hasClass('active')) {
106                this_option.children('.whcom_domain_form_item_content').slideDown('fast', function () {
107                    this_option.addClass('active');
108                });
109            }
110        });
111        $(document).on('submit', '.wcop_sp_check_product_domain', function (e) {
112            e.preventDefault();
113            let form = $(this);
114            let product_selector = $('#wcop_sp_product_select');
115            let selected_option = product_selector.find(':selected');
116            let main_form = product_selector.closest('form.wcop_sp_add_product');
117            if (!main_form.length) {
118                main_form = form.closest('form.wcop_sp_add_product');
119            }
120            if (!main_form.length) {
121                main_form = $('form.wcop_sp_add_product').first();
122            }
123            let response_container = $('.wcop_sp_domain_response');
124            let submit = form.find('button[type="submit"]');
125            let submit_val = submit.html();
126            let is_free_domain_attached = selected_option.data('is-free-domain-attached');
127            let suggestion_domain = $.trim(form.find('input[name="domain"]').val() || '');
128            let suggestion_ext = $.trim(form.find('select[name="ext"]').val() || form.find('input[name="ext"]').val() || '');
129            let suggestion_domaintype = $.trim(form.find('input[name="domaintype"]').val() || '');
130
131
132            let data = $(this).serializeArray();
133            pid = main_form.find('[name="get_product_id"]').val();
134            default_billingcycle = main_form.find('[name="default_billingcycle"]').val();
135            data.push({'name': 'pid', 'value': pid});
136            data.push({'name': 'default_billingcycle', 'value': default_billingcycle});
137            data.push({'name': 'is_free_domain_attached', 'value': is_free_domain_attached});
138            let cart_index = -1;
139            if (form.hasClass('domain_already_in_cart')) {
140                let tld = form.find('select[name="domain"] :selected').data('domain-tld');
141                let domain_type = form.find('select[name="domain"] :selected').data('domain-type');
142                cart_index = form.find('select[name="domain"] :selected').data('cart-index');
143                data.push({'name': 'ext', 'value': tld});
144                data.push({'name': 'domaintype', 'value': domain_type});
145                suggestion_ext = $.trim(tld || '');
146                suggestion_domaintype = $.trim(domain_type || '');
147            }
148            data.push({'name': 'currency', 'value': getUrlParameter('currency')});
149            submit.html(whcom_spinner_icon_only);
150            response_container.html(whcom_spinner_block);
151            
152            jQuery.ajax({
153                url: wcop_ajax.ajax_url,
154                type: 'post',
155                data: data,
156                success: function (response) {
157                    let res = JSON.parse(response);
158                    console.log(res);
159                    submit.html(submit_val);
160                    if (res.status === "OK") {
161                        response_container.html(res.domain_attachment_form);
162                        $('input[name="cart_index"]').val(cart_index);
163                        if (res.type === 'existing') {
164                            $('.wcop_sp_attach_product_domain').submit();
165                        }
166                        if (form.hasClass('domain_already_in_cart')) {
167                            $('.wcop_sp_attach_product_domain').submit();
168                        }
169                    } else {
170                        response_container.html(res.message);
171                    }
172
173                    if (window.wcop_should_load_domain_suggestions(form, suggestion_domaintype, suggestion_domain, suggestion_ext)) {
174                        window.wcop_request_domain_suggestions(main_form, response_container, suggestion_domain, suggestion_ext, suggestion_domaintype, 0, false);
175                    } else {
176                        window.wcop_clear_domain_suggestions(response_container);
177                    }
178                }
179            });
180        });
181        // Reset Domain Form
182        $(document).on('click', '.wcop_sp_reset_domain_form', function (e) {
183            e.preventDefault();
184            let response_container = $('.wcop_sp_domain_response');
185            let suggestions_container = response_container.find('.wcop_sp_domain_suggestions').detach();
186            response_container.empty();
187            if (suggestions_container.length) {
188                response_container.append(suggestions_container);
189            }
190            $('#domain').focus();
191            $('form.wcop_sp_check_product_domain').each(function () {
192                $(this)['0'].reset()
193            });
194        });
195        // Attach Product Domain
196        $(document).on('submit', '.wcop_sp_attach_product_domain', function (e) {
197            e.preventDefault();
198            let form = $(this);
199            let product_selector = $('#wcop_sp_product_select');
200            let domain_selector = $('form.wcop_sp_add_product');
201            let main_form = product_selector.closest('form.wcop_sp_add_product');
202            let domain_options_container = $('#wcop_sp_domain_config');
203            let show_domain_nameservers = domain_selector.find('[name="show_domain_nameservers"]').val();
204            let submit = form.find('button[type="submit"]');
205            let submit_val = submit.html();
206            let selected_product = product_selector.find(':selected').val();
207            let products_select = $('#wcop_sp_product_select_domains').html();
208            let domain_notice = main_form.find('#wcop_sp_product_domain_notice');
209            let payment_options_container = main_form.find('.whcom_payment_options');
210            let payment_options_heading = main_form.find('.wcop_payment_heading');
211
212            if (domain_notice.show()) {
213                domain_notice.hide();
214            }
215
216
217            let data = $(this).serializeArray();
218            pid = main_form.find('[name="get_product_id"]').val();
219            default_billingcycle = main_form.find('[name="default_billingcycle"]').val();
220            wcop_sp_template = main_form.find('[name="wcop_sp_template"]').val();
221            data.push({'name': 'pid', 'value': pid});
222            data.push({'name': 'default_billingcycle', 'value': default_billingcycle});
223            data.push({'name': 'wcop_sp_template', 'value': wcop_sp_template});
224            data.push({'name': 'show_domain_nameservers', 'value': show_domain_nameservers});
225            if (data[3].value === "transfer") {
226                window.eppcode = data[5].value;
227            }
228            submit.html(whcom_spinner_icon).prop('disabled', true);
229
230            //== Change Summary section heading either it is empty or populated with content
231            jQuery('.whcom_empty_summary_section_heading').css('display', 'none');
232            jQuery('.whcom_filled_summary_section_heading').css("display", "block");
233
234            domain_options_container.hide();
235            data.push({'name': 'currency', 'value': getUrlParameter('currency')});
236            jQuery.ajax({
237                url: wcop_ajax.ajax_url,
238                type: 'post',
239                data: data,
240                success: function (response) {
241                    let res = JSON.parse(response);
242                    if (res.domain_config.status === "OK") {
243                        $('#wcop_sp_choose_a_domain').slideUp('400', function () {
244                            $(this).hide();
245                        });
246
247                        /*
248                        * Rendering the payment gateway options in respective container.
249                        * We Render payment gateways on this click due to speed issues.
250                         */
251
252                        payment_options_container.html(res.payment_options);
253                        payment_options_heading.show();
254
255                        $("#wcop_sp_domain_config").show();
256                        $('[href="#wcop_sp_choose_a_domain"]').prop('href', '#wcop_sp_domain_config');
257                        submit.html(submit_val).prop('disabled', false);
258                        if (res.domain_config.free_domain === true) {
259                            //domain_options_container.show().find( '.wcop_sp_section_content' ).html( res.domain_config_form ).prop('disabled',true);
260                        } else {
261                            domain_options_container.show().find('.wcop_sp_section_content').html(res.domain_config.domain_config_form);
262                        }
263                        $('.wcop_input[name="regperiod"]').trigger('change');
264                    } else {
265                        if (res.domain_config && res.domain_config.scroll_to_product) {
266                            window.wcop_scroll_to_product_section();
267                        }
268                        $('.wcop_sp_domain_response').html(res.domain_config.message, function () {
269                            wcop_sp_update_product_summary();
270                        });
271                    }
272                }
273            });
274        });
275        // Select/Change Product
276        $(document).on('change', 'select[id^="wcop_sp_product_select"], input[name="pid"]', function () {
277            let product_select = $(this);
278            let isRadio = product_select.is('input[name="pid"]');
279            let selected_option = isRadio ? product_select : product_select.find(':selected');
280            let main_form = product_select.closest('form.wcop_sp_add_product');
281            let options_spinner = main_form.find('.wcop_sp_product_options_spinner');
282            let options_container = main_form.find('#wcop_additional_services');
283            let billingcycle_container = main_form.find('.wcop_sp_prod_billingcycle');
284            let free_domain_popup_container = main_form.find('.wcop_sp_free_domain_popup');
285            let payment_options_heading = main_form.find('.wcop_payment_heading');
286
287            //~~ Defining Payment gateway container so that, if product selects then this 
287section populater
288            let payment_options_container = main_form.find('.whcom_payment_options');
289            let default_payment_options = main_form.find('.whcom_default_payment_options');
290            let domain = main_form.find('input[name=domain]').val();
291            let domain_attached = main_form.find('input[name=attached_domain]').val();
292            let domain_required = selected_option.data('domain-required');
293            let domain_notice = main_form.find('#wcop_sp_product_domain_notice');
294            let wcop_sp_template = main_form.find('[name="wcop_sp_template"]').val();
295            let wcop_sp_promocode = main_form.find('[name="promocode"]').val();
296            let get_sp_selected_addon = main_form.find('[name="get_selected_addon"]').val();
297            let hide_server_fields = main_form.find('[name="hide_server_fields"]').val();
298            //== picking free domain notice div to show or hide
299            let is_free_domain_attached = selected_option.data('is-free-domain-attached');
300            let free_domain_notice = main_form.find('#wcop_sp_product_free_domain_notice');
301
302            if (isRadio) {
303                main_form.find('label.whcom_label_product.whcom_radio').removeClass('whcom_checked');
304                product_select.closest('label.whcom_label_product.whcom_radio').addClass('whcom_checked');
305            }
306
307            if (domain === undefined && domain_attached === undefined && domain_required === 'yes') {
308                domain_notice.show();
309            } else {
310                domain_notice.hide();
311            }
312
313            //== show or hide free domain notice
314            if (wcop_sp_template === '01_default') {
315                if (is_free_domain_attached === 'yes') {
316                    free_domain_notice.show();
317                    domain_notice.hide();
318                } else {
319                    free_domain_notice.hide();
320                }
321            }
322
323            let checkedValue = main_form.find('.whcom_product_content input[name="pid"]:checked').val();
324
325            let urlParams = new URLSearchParams(window.location.search);
326            let billingcycle = urlParams.get('billingcycle');
327
328            let data = {};
329            data.action = "wcop_sp_process";
330            data.wcop_sp_what = "change_product";
331            data.default_billingcycle = main_form.find('[name="default_billingcycle"]').val();
332            data.pid = checkedValue ?? product_select.val();
333            // data.billingcycle = billingcycle;
334            //== Add wcop template options
335            data.wcop_sp_template = wcop_sp_template;
336            data.wcop_sp_promocode = wcop_sp_promocode;
337            data.get_sp_selected_addon = get_sp_selected_addon;
338            data.hide_server_fields = hide_server_fields;
339            console.log("I am in product select new");
340            console.log(data);
341            if (domain_attached === 'yes') {
342                data.tld = main_form.find('[name=tld]').val();
343                data.sld = main_form.find('[name=sld]').val();
344                data.domain = main_form.find('[name=domain]').val();
345                data.domaintype = main_form.find('[name=domaintype]').val();
346            }
347            options_spinner.show();
348            billingcycle_container.html(whcom_spinner_block);
349            options_container.hide();
350            data.currency = getUrlParameter('currency');
351            $.ajax({
352                url: wcop_ajax.ajax_url,
353                type: 'post',
354                data: data,
355                success: function (response) {
356                    let res = JSON.parse(response);
357                    console.log('I am in product selectttt');
358                    console.log(res);
359                    if (res.status === "OK") {
360                        options_spinner.hide();
361                        billingcycle_container.html(res.response_html.billingcycles);
362                        payment_options_container.html(res.response_html.payment_options);
363                        free_domain_popup_container.html(res.response_html.free_domains_popup);
364                        $('.whcom_subdomain_form_item_container').remove();
365                        if (res.response_html.free_subdomains) {
366                            $('div#wcop_sp_choose_a_domain .whcom_domain_form_container').append(res.response_html.free_subdomains);
367                        }
368
369                        payment_options_heading.show();
370                        default_payment_options.hide();
371                        if (res.options_available) {
372                            if (res.template_name === '04_sleek') {
373                                options_container.hide();
374                            } else {
375                                options_container.show();
376                            }
377                            options_container.find('.wcop_sp_section_content').html(res.response_html.configurations);
378                        } else {
379                            options_container.find('.wcop_sp_section_content').html('This product has no configureable options');
380                        }
381                        if (res.domain_options && res.domain_options.status === 'OK' && main_form.find('[name=free_domain]').val() !== res.domain_options.free_domain) {
382                            main_form.find('#wcop_sp_domain_config').html(res.domain_options.domain_config_form);
383                        }
384                        window.renderWhcomCollapse();
385                        main_form.find('#wcop_sp_product_billingcycle').trigger('change');
386                    } else {
387                        billingcycle_container.html(res.message);
388                    }
389                }
390            });
391        });
392        // Select/Change Product Billingcycle
393        $(document).on('change', '#wcop_sp_product_billingcycle', function () {
394            let billingcycle_select = $(this);
395            let main_form = billingcycle_select.closest('form.wcop_sp_add_product');
396            let options_spinner = main_form.find('.wcop_sp_product_options_spinner');
397            let options_container = main_form.find('.wcop_sp_config_options_container');
398            let domain_attached = main_form.find('input[name=attached_domain]').val();
399
400            let data = {};
401            data.action = "wcop_sp_process";
402            data.wcop_sp_what = "change_billingcycle";
403            if (main_form.find('[name=wcop_sp_template]').val() === '05_modern' || main_form.find('[name=wcop_sp_template]').val() === '04_sleek') {
404                data.pid = jQuery('label.whcom_label_product.whcom_radio.wcap_li_content_item.whcom_checked').data('pid')
405            } else {
406                data.pid = main_form.find('[name="pid"]').val();
407            }
408            data.billingcycle = billingcycle_select.val();
409            if (domain_attached === 'yes') {
410                data.tld = main_form.find('[name=tld]').val();
411                data.sld = main_form.find('[name=sld]').val();
412                data.domain = main_form.find('[name=domain]').val();
413                data.domaintype = main_form.find('[name=domaintype]').val();
414            }
415
416            options_spinner.show();
417            //== Commented because configurable options section was not showing
418            /*options_container.hide();*/
419            data.currency = getUrlParameter('currency');
420            $.ajax({
421                url: wcop_ajax.ajax_url,
422                type: 'post',
423                data: data,
424                success: function (response) {
425                    let res = JSON.parse(response);
426                    options_spinner.hide();
427                    if (res.status === "OK") {
428                        if (res.options_available) {
429                            options_container.show().html(res.options_html);
430                        }
431                        if (res.domain_options && res.domain_options.status === 'OK' && main_form.find('[name=free_domain]').val() !== res.domain_options.free_domain) {
432                            main_form.find('#wcop_sp_domain_config').html(res.domain_options.domain_config_form);
433                        }
434                    }
435                }
436            });
437        });
438        // Add Remove discount coupon
439        $(document).on('click', '.wcop_sp_apply_remove_coupon', function (e) {
440            e.preventDefault();
441            let button = $(this);
442            let button_text = button.html();
443            let coupon_container = button.closest('.wcop_sp_promo_container');
444            let coupon_response = coupon_container.find('.wcop_sp_coupon_response');
445            let promo_action = button.data('promo-action');
446            let promocode = coupon_container.find('input[name="promocode"]').val();
447
448            let data = {
449                action: "wcop_sp_process",
450                wcop_sp_what: "add_remove_coupon",
451                promo_action: promo_action,
452                promocode: promocode
453            };
454            button.html(whcom_spinner_icon_only);
455            data.currency = getUrlParameter('currency');
456            $.ajax({
457                url: wcop_ajax.ajax_url,
458                type: 'post',
459                data: data,
460                success: function (response) {
461                    let res = JSON.parse(response);
462                    console.log('I am here in coupon section');
463                    console.log(res);
464                    button.html(button_text);
465                    if (res.status === "OK") {
466                        coupon_container.html(res.response_html);
467                        wcop_sp_update_product_summary();
468                    } else {
469                        coupon_response.html(res.message);
470                        wcop_sp_update_product_summary();
471                    }
472                }
473            });
474
475        });
476        // Add Remove discount coupon
477        $(document).on('click', '.wcop_sp_summary_apply_remove_coupon', function (e) {
478            e.preventDefault();
479            let button = $(this);
480            let button_text = button.html();
481            let coupon_container = button.closest('.wcop_sp_promo_container');
482            let coupon_response = coupon_container.find('.wcop_sp_coupon_response');
483            let promo_action = button.data('promo-action');
484            let promo_input = coupon_container.find('input[name="promocode"]');
485            let promocode = promo_input.val();
486            button.html(whcom_spinner_icon_only);
487            if (promo_action === 'add_coupon') {
488                console.log("I am here in add coupon");
489                $('form.wcop_sp_add_product [name=promocode]').prop('value', promocode);
490                wcop_sp_update_product_summary();
491            } else if (promo_action === 'remove_coupon') {
492                $('form.wcop_sp_add_product [name=promocode]').prop('value', '');
493                console.log('Removing Coupon');
494                wcop_sp_update_product_summary();
495            }
496        });
497        // Estimate Taxes
498        $(document).on('click', '.wcop_sp_estimate_taxes_button', function (e) {
499            e.preventDefault();
500            let button = $(this);
501            let button_text = button.html();
502            let container = button.closest('.wcop_sp_estimate_taxes_container');
503            let country = container.find('[name="estimate_tax_country"]').val();
504            let state = container.find('[name="estimate_tax_state"]').val();
505
506            let data = {
507                action: "wcop_sp_process",
508                wcop_sp_what: "estimate_taxes",
509                country: country,
510                state: state
511            };
512            button.html(whcom_spinner_icon);
513            data.currency = getUrlParameter('currency');
514            $.ajax({
515                url: wcop_ajax.ajax_url,
516                type: 'post',
517                data: data,
518                success: function (response) {
519                    let res = JSON.parse(response);
520                    button.html(button_text);
521                    if (res.status === "OK") {
522                        wcop_sp_update_product_summary();
523                    }
524                }
525            });
526
527        });
528        // Add Product to local cart Form
529        $(document).on('submit', '.wcop_sp_add_product', function (e) {
530            e.preventDefault();
531            let $form = $(this);
532            let response_field = $form.find('.whcom_sp_order_response');
533            let show_direct_invoice = $form.find('[name="show_direct_invoice"]').val();
534            let redirect_to_custom_field = $form.find('[name="redirect_to_custom_field"]').val();
535            let redirect_to_custom_url = $form.find('[name="redirect_to_custom_field"]').attr('data-link');
536            let response_div = $('#wcop_sp_main');
537            let spepp = window.eppcode;
538            let data = $(this).serializeArray();
539            /*var elmnt = document.getElementById("wcop_sp_login_register_container");
540            elmnt.scrollIntoView();*/
541            data.push({'name': 'eppcode', 'value': spepp});
542            response_field.show();
543            response_field.html(whcom_spinner_icon);
544            console.log("show direct invoice");
545            console.log(show_direct_invoice);
546            data.push({'name': 'currency', 'value': getUrlParameter('currency')});
547            jQuery('div#wcop_review_checkout button.whcom_button.whcom_button_big').prop('disabled', true);
548            jQuery.ajax({
549                url: wcop_ajax.ajax_url,
550                type: 'post',
551                data: data,
552                success: function (response) {
553                    let res;
554                    try {
555                        res = JSON.parse(response);
556                    } catch (e) {
557                        res = { status: 'ERROR' };
558                    }
559                    if (!res || typeof res !== 'object') {
560                        res = { status: 'ERROR' };
561                    }
562
563                    let fallbackMessage = (wcop_ajax.i18n && wcop_ajax.i18n.error_occurred) ? wcop_ajax.i18n.error_occurred : 'An error occurred. Please try again.';
564                    let responseMessage = (typeof res.message === 'string' && res.message.trim()) ? res.message : fallbackMessage;
565
566                    console.log('work: ',response);
567                    response_field.addClass('whcom_alert whcom_alert_warning whcom_alert_with_icon');
568                    response_field.html(responseMessage);
569                    whcom_show_notification(responseMessage, 'whcom_alert whcom_alert_danger');
570                    console.log("I am here");
571                    console.log(res);
572                    if (responseMessage === 'Your product has been ordered...') {
573                        window.scrollTo(0, 0);
574                    }
575                    if (res.status === "OK") {
576                        if (redirect_to_custom_field === 'yes'){
577                            window.location.href = redirect_to_custom_url;
578                        } else if (res.response_html) {
579                            response_div.html(res.response_html);
580                            if (show_direct_invoice === 'yes' && res.invoice_link) {
581                                window.location.href = res.invoice_link;
582                            }
583                        }
584                    }
585                    else if (res.status === 'invoice_generated') {
586                        /*response_div.html(res.response_html).prepend(res.response_html);*/
587                        if (res.response_html) {
588                            response_div.html(res.response_html);
589                        }
590                        if (show_direct_invoice === 'yes' && res.invoice_link) {
591                            window.location.href = res.invoice_link;
592                        }
593                    }
594                },
595                complete: function() {
596                    jQuery('div#wcop_review_checkout button.whcom_button.whcom_button_big').prop('disabled', false);
597                }
598            });
599        });
600        // Register the client through registr form shortcode
601        $(document).on('submit', '.wcop_sp_register_area', function (e) {
602            e.preventDefault();
603            let $form = $(this);
604            let response_field = $form.find('.whcom_sp_order_response');
605            let data = $(this).serializeArray();
606            response_field.show();
607            response_field.html(whcom_spinner_icon);
608            data.push({'name': 'currency', 'value': getUrlParameter('currency')});
609            jQuery.ajax({
610                url: wcop_ajax.ajax_url,
611                type: 'post',
612                data: data,
613                success: function (response) {
614                    let res = JSON.parse(response);
615                    if (res.status === "OK") {
616                        window.location.href = res.registered_client_token;
617                    } else if (res.status === "ERROR") {
618                        response_field.addClass('whcom_alert whcom_alert_warning whcom_alert_with_icon');
619                        response_field.html(res.message);
620                    }
621                }
622            });
623        });
624        // Payment gateway switching,
625        $(document).on('change', '.whcom_sp_cc_switcher', function () {
626            let checkbox = $(this);
627            let cc_options_container = $('.whcom_sp_cc_fields');
628            let merchantGatewayRaw = checkbox.data('merchant-gateways') || checkbox.data('merchent-gateway') || '';
629            let merchantGateways = merchantGatewayRaw.toString().split(',').map(function (item) {
630                return item.trim();
631            }
631).filter(Boolean);
632            if (merchantGateways.indexOf(checkbox.val()) !== -1) {
633                cc_options_container.slideDown(300);
634            } else {
635                cc_options_container.slideUp(300);
636            }
637        });
638        // Client Type Switching,
639        $(document).on('whcomTabChanged', '.wcop_sp_client_type_switch', function () {
640            let tab = $(this);
641            let type_input = $('input[name="wcop_sp_client_type"]');
642            let login_container = $('#wcop_sp_user_login');
643            let register_container = $('#wcop_sp_register_account');
644
645            login_container.find('[required]').each(function () {
646                $(this).prop('required', false).addClass('wcop_sp_required');
647            });
648            register_container.find('[required]').each(function () {
649                $(this).prop('required', false).addClass('wcop_sp_required');
650            });
651
652
653            if (tab.data('tab') === 'wcop_sp_register_account') {
654                type_input.val('register');
655                register_container.find('.wcop_sp_required').each(function () {
656                    $(this).prop('required', true).removeClass('wcop_sp_required');
657                });
658            } else {
659                type_input.val('login');
660                login_container.find('.wcop_sp_required').each(function () {
661                    $(this).prop('required', true).removeClass('wcop_sp_required');
662                });
663            }
664        });
665        $(document).on('click', '#wcop_sp_register_account_link', function (e) {
666            e.preventDefault();
667            let login_container = $('#wcop_sp_user_login');
668            let register_container = $('#wcop_sp_register_account');
669            $('#wcop_sp_register_account_link').fadeOut('', function () {
670                $('#wcop_sp_user_login_link').fadeIn('', function () {
671                    login_container.slideUp('', function () {
672                        register_container.slideDown('', function () {
673                            $('input[name="wcop_sp_client_type"]').val('register');
674                            login_container.find('[required]').each(function () {
675                                $(this).prop('required', false).addClass('wcop_sp_required');
676                            });
677                            register_container.find('[required]').each(function () {
678                                $(this).prop('required', false).addClass('wcop_sp_required');
679                            });
680                            register_container.find('.wcop_sp_required').each(function () {
681                                $(this).prop('required', true).removeClass('wcop_sp_required');
682                            });
683                        });
684                    });
685                });
686            });
687        });
688        $(document).on('click', '#wcop_sp_user_login_link', function (e) {
689            e.preventDefault();
690            let login_container = $('#wcop_sp_user_login');
691            let register_container = $('#wcop_sp_register_account');
692            $('#wcop_sp_user_login_link').fadeOut('', function () {
693                $('#wcop_sp_register_account_link').fadeIn('', function () {
694                    register_container.slideUp('', function () {
695                        login_container.slideDown('', function () {
696                            $('input[name="wcop_sp_client_type"]').val('login');
697                            login_container.find('[required]').each(function () {
698                                $(this).prop('required', false).addClass('wcop_sp_required');
699                            });
700                            register_container.find('[required]').each(function () {
701                                $(this).prop('required', false).addClass('wcop_sp_required');
702                            });
703                            login_container.find('.wcop_sp_required').each(function () {
704                                $(this).prop('required', true).removeClass('wcop_sp_required');
705                            });
706                        });
707                    });
708                });
709            });
710        });
711        $(document).on('click', '#wcop_sp_login_register_container button', function (e) {
712            e.preventDefault();
713            let button = $(this);
714            let button_text = button.html();
715            let section_container = button.closest('#wcop_billing_info');
716            let register_form = section_container.find('#wcop_sp_register_account');
717            let login_form = section_container.find('#wcop_sp_user_login');
718            let main_form = button.closest('form');
719            button.html(whcom_spinner_icon_only);
720            if (button.prop('id') === 'wcop_register_form_login_toggle') {
721                register_form.hide();
722                login_form.show();
723                button.html(button_text);
724            }
725            if (button.prop('id') === 'wcop_login_form_register_toggle') {
726                register_form.show();
727                login_form.hide();
728                button.html(button_text);
729            }
730            if (button.prop('id') === 'wcop_login_form_login_button') {
731                let login_button = login_form.find('#wcop_login_form_login_button');
732                let login_button_text = login_button.text();
733                login_button.html(whcom_spinner_icon);
734                let data = {};
735                data.action = "wcop_sp_process";
736                data.wcop_sp_what = 'process_login';
737                data.login_email = login_form.find('[name="login_email"]').val();
738                data.login_pass = login_form.find('[name="login_pass"]').val();
739                data.wcop_sp_template = main_form.find('[name="wcop_sp_template"]').val();
740                data.currency = getUrlParameter('currency');
741                jQuery.ajax({
742                    url: whcom_ajax.ajax_url,
743                    type: 'post',
744                    data: data,
745                    success: function (response) {
746                        let res = JSON.parse(response);
747                        login_button.text(login_button_text);
748                        if (res.status === 'OK') {
749                            wcop_sp_update_product_summary();
750                            whcom_show_notification(res.message, 'whcom_alert whcom_alert_success');
751                            section_container.html(res.register_html);
752                            button.html(button_text);
753                        } else {
754                            whcom_show_notification(res.message, 'whcom_alert whcom_alert_danger');
755                            console.log(res);
756                            button.html(button_text);
757                        }
758                    }
759                });
760            }
761            if (button.prop('id') === 'wcop_login_form_logout_button') {
762                let data = {};
763                data.action = "wcop_sp_process";
764                data.wcop_sp_what = 'process_logout';
765                data.wcop_sp_template = main_form.find('[name="wcop_sp_template"]').val();
766                data.currency = getUrlParameter('currency');
767                jQuery.ajax({
768                    url: whcom_ajax.ajax_url,
769                    type: 'post',
770                    data: data,
771                    success: function (response) {
772                        let res = JSON.parse(response);
773                        console.log(res);
774                        if (res.status === 'OK') {
775                            location.reload();
776                            // whcom_show_notification(res.message, 'whcom_alert whcom_alert_success');
777                            // section_container.html(res.client_html);
778                            // button.html(button_text);
779                        } else {
780                            whcom_show_notification(res.message, 'whcom_alert whcom_alert_danger');
781                            console.log(res);
782                            button.html(button_text);
783                        }
784                    }
785                });
786            }
787        });
788
789
790        // Domain first order processing starts below
791        // TLD add/remove form (whmpress)
792        $(document).on('change', 'input[name="whcom_tld_add_remove"]', function (e) {
793            e.preventDefault();
794
795            let checkbox = $(this);
796            let cart_index = checkbox.data('cart-index');
797            let checkbox_id = checkbox.prop('id');
798            let domain_action = (
799                checkbox.prop('checked')
800            ) ? 'add_item' : 'delete_item';
801
802            let title_label = $('label[for="' + checkbox_id + '"].title_label');
803
804            let icon_label = $('label[for="' + checkbox_id + '"].label_icon');
805            let add_div = $('#' + checkbox_id + '_add_sub');
806            let add_div_html = add_div.html();
807            let remove_div = $('#' + checkbox_id + '_remove_sub');
808            let remove_div_html = remove_div.html();
809
810
811            checkbox.prop("disabled", true);
812            title_label.html(whcom_spinner_icon);
813            icon_label.find('i').removeClass().addClass('whcom_icon_spinner-1 whcom_animate_spin');
814            remove_div.find('label > i').removeClass().addClass('whcom_icon_spinner-1 whcom_animate_spin');
815            add_div.find('label').html(whcom_spinner_icon);
816            if (domain_action === 'add_item') {
817                remove_div.hide();
818            } else {
819                add_div.hide();
820            }
821
822
823            let data = {};
824            data.action = "wcop_domain_first";
825            data.cart_index = cart_index;
826            data.wcop_what = "add_remove_domain";
827            data.domain_action = domain_action;
828            data.domain = checkbox.data('domain');
829            data.domain_type = checkbox.data('domain-type');
830            data.domain_price = checkbox.data('domain-price');
831            data.domain_duration = checkbox.data('domain-duration');
832            data.currency = getUrlParameter('currency');
833
834            jQuery.ajax({
835                url: wcop_ajax.ajax_url,
836                type: 'post',
837                data: data,
838                success: function (response) {
839                    let res = JSON.parse(response);
840                    title_label.html(res.message);
841                    checkbox.prop("disabled", false);
842
843
844                    remove_div.find('label > i').removeClass().addClass('whcom_icon_spinner-1 whcom_animate_spin');
845                    add_div.html(add_div_html);
846                    remove_div.html(remove_div_html);
847
848                    if (res.status === "OK") {
849                        checkbox.data('cart-index', res.cart_index);
850                        if (domain_action === 'add_item') {
851                            icon_label.find('i').removeClass().addClass('whcom_icon_ok-circled2');
852                            add_div.hide();
853                            remove_div.show();
854                        } else {
855                            icon_label.find('i').removeClass().addClass('whcom_icon_circle-empty');
856                            add_div.show();
857                            remove_div.hide();
858                        }
859                    } else {
860                        checkbox.prop('checked', false).prop('disabled', true);
861                    }
862                }
863            });
864        });
865        // TLD select continue button (whmpress, after select all desired tlds, it will move now to wcop territory)
866        $(document).on('click', '#wcop_df_domain_submit_button', function (e) {
867            e.preventDefault();
868            let button = $(this);
869            let checked = $('input[name="whcom_tld_add_remove"]:checked').length;
870            if (checked > 0) {
871                window.location.href = button.prop('href');
872            } else {
873                alert('Kindly select at least one domain');
874            }
875        });
876        // Domain Search Form (wcop, if domain is not added already by whmpress)
877        $(document).on('submit', '.wcop_df_domain_search_form', function (e) {
878            e.preventDefault();
879            let $form = $(this);
880            let container = $form.parents('#wcop_df_container');
881            let result = container.find('.wcop_df_domain_search_result_content');
882            let submit = $form.find('button');
883            let submit_val = submit.html();
884
885
886            let data = $form.serializeArray();
887            submit.html(whcom_spinner_icon);
888            result.hide();
889            data.push({'name': 'currency', 'value': getUrlParameter('currency')});
890            jQuery.ajax({
891                url: wcop_ajax.ajax_url,
892                type: 'post',
893                data: data,
894                success: function (response) {
895                    let res = JSON.parse(response);
896                    submit.html(submit_val);
897                    if (res.status === "OK") {
898                        result.html(res.response_form).show();
899                    } else {
900                        result.html(res.message).show();
901                    }
902                }
903            });
904        });
905        // Domain Submit Form (wcop, if domain is not added already by whmpress)
906        $(document).on('submit', '#wcop_df_domain_search_submit', function (e) {
907            e.preventDefault();
908            let container = $(this).parents('#wcop_df_container');
909            let data = $(this).serializeArray();
910            container.html(whcom_spinner_icon);
911            data.push({'name': 'currency', 'value': getUrlParameter('currency')});
912            jQuery.ajax({
913                url: wcop_ajax.ajax_url,
914                type: 'post',
915                data: data,
916                success: function (response) {
917                    let res = JSON.parse(response);
918                    if (res.status === "OK") {
919                        container.html(res.response_form);
920                    } else {
921                        container.html(res.message);
922                    }
923                }
924            });
925        });
926        // TLD dropdown for product selection (populate product selection form based on domain dropdown change)
927        $(document).on('change', 'input[name=wcop_df_domain_selector]', function (e) {
928            e.preventDefault();
929
930            let radio = $(this);
931            let main_container = radio.parents('.wcop_df_container');
932            let domains_container = main_container.find('.wcop_df_domain_selector_container');
933            let checked_radio = domains_container.find(':checked');
934            let cart_index = checked_radio.data('cart-index');
935            let cart_product = checked_radio.data('domain-product');
936            let domain_name = checked_radio.data('domain-name');
937            let product_wrapper = $('.wcop_df_products_wrapper');
938
939            domains_container.find('input[name=wcop_df_domain_selector]').each(function () {
940                let $curr_radio = $(this);
941                if ($curr_radio.prop('checked')) {
942                    $curr_radio.parents('label.whcom_alert').addClass('whcom_bordered_2x');
943                } else {
944                    $curr_radio.parents('label.whcom_alert').removeClass('whcom_bordered_2x');
945                }
946            });
947
948
949            product_wrapper.html(whcom_spinner_icon);
950            main_container.find('.wcop_df_select_product_domain_name').text(domain_name);
951            let data = {};
952            data.action = "wcop_domain_first";
953            data.wcop_what = "get_domain_products";
954            data.cart_index = cart_index;
955            data.cart_product = cart_product;
956            data.pids = $('input[name="pids"]').val();
957            data.gids = $('input[name="gids"]').val();
958            data.currency = getUrlParameter('currency');
959            jQuery.ajax({
960                url: wcop_ajax.ajax_url,
961                type: 'post',
962                data: data,
963                success: function (response) {
964                    let res = JSON.parse(response);
965                    if (res.status === "OK") {
966                        product_wrapper.html(res.product
966s);
967                    } else {
968                        product_wrapper.html(res.message);
969                    }
970                }
971            });
972        });
973        // Product add/remove for a tld
974        $(document).on('change', 'input[name="whcom_product_add_remove"]', function (e) {
975            e.preventDefault();
976
977            let checkbox = $(this);
978            let main_container = checkbox.parents('.wcop_df_container');
979            let checkbox_id = checkbox.prop('id');
980            let product_box = $('#wcop_df_product_' + checkbox_id);
981            product_box.find('.wcop_df_product_icon i').removeClass().addClass('whcom_icon_spinner-1 whcom_animate_spin');
982
983
984            let domains_container = main_container.find('.wcop_df_domain_selector_container');
985            let selected_domain = domains_container.find(':checked');
986            let selected_domain_container = selected_domain.parents('label');
987
988            let cart_index = selected_domain.data('cart-index');
989            $(document).find('input[name="whcom_product_add_remove"]').not(checkbox).each(function () {
990                let checkbox_other = $(this);
991                let checkbox_other_id = checkbox_other.prop('id');
992                let product_box_other = $('#wcop_df_product_' + checkbox_other_id);
993                product_box_other.find('.whcom_button').removeClass('whcom_button_danger');
994                product_box_other.find('.add_text').show();
995                product_box_other.find('.remove_text').hide();
996                product_box_other.find('.wcop_df_product_icon i').removeClass().addClass('whcom_icon_circle-empty');
997
998                checkbox_other.prop('checked', false);
999            });
1000
1001            let product_action = (
1002                checkbox.prop('checked')
1003            ) ? 'add_item' : 'delete_item';
1004
1005
1006            checkbox.prop("disabled", true);
1007
1008
1009            let data = {};
1010            data.action = "wcop_domain_first";
1011            data.wcop_what = "attach_domain_product";
1012            data.pid = checkbox.data('product-id');
1013            data.cid = checkbox.data('product-currency');
1014            data.billingcycle = checkbox.data('product-billingcycle');
1015            data.product_name = checkbox.data('product-name');
1016            data.product_price = checkbox.data('product-price');
1017            data.product_setup = checkbox.data('product-setup');
1018            data.cart_index = cart_index;
1019            data.product_action = product_action;
1020            data.currency = getUrlParameter('currency');
1021            jQuery.ajax({
1022                url: wcop_ajax.ajax_url,
1023                type: 'post',
1024                data: data,
1025                success: function (response) {
1026                    let res = JSON.parse(response);
1027                    checkbox.prop("disabled", false);
1028                    if (res.status === "OK") {
1029                        checkbox.data('cart-index', res.cart_index);
1030                        if (product_action === 'add_item') {
1031                            product_box.find('.wcop_df_product_icon i').removeClass().addClass('whcom_icon_ok-circled2');
1032                            selected_domain_container.addClass('.whcom_border_success whcom_text_success').find('.whcom_domain_product').text(checkbox.data('product-name'));
1033                            selected_domain_container.find('.whcom_pill').show();
1034                            product_box.find('.whcom_button').addClass('whcom_button_danger');
1035                            product_box.find('.add_text').hide();
1036                            product_box.find('.remove_text').show();
1037                        } else {
1038                            product_box.find('.wcop_df_product_icon i').removeClass().addClass('whcom_icon_circle-empty');
1039                            selected_domain_container.removeClass('.whcom_border_success whcom_text_success').find('.whcom_domain_product').text(selected_domain_container.data('domain-product-text'));
1040                            selected_domain_container.find('.whcom_pill').hide();
1041                            product_box.find('.whcom_button').removeClass('whcom_button_danger');
1042                            product_box.find('.add_text').show();
1043                            product_box.find('.remove_text').hide();
1044                        }
1045                    } else {
1046                        checkbox.prop('checked', false).prop('disabled', true);
1047                    }
1048                }
1049            });
1050        });
1051        // Continue when product attachment is completed (after attaching products with all/any domains)
1052        $(document).on('click', '#wcop_df_products_continue', function (e) {
1053            e.preventDefault();
1054            let button = $(this);
1055            let container = $(this).parents('#wcop_df_container');
1056
1057            let data = {};
1058            data.action = "wcop_domain_first";
1059            data.wcop_what = "domain_products_attached";
1060
1061
1062            button.html(whcom_spinner_icon);
1063            data.currency = getUrlParameter('currency');
1064            jQuery.ajax({
1065                url: wcop_ajax.ajax_url,
1066                type: 'post',
1067                data: data,
1068                success: function (response) {
1069                    let res = JSON.parse(response);
1070                    if (res.status === "OK") {
1071                        container.html(res.response_form);
1072                        window.renderWhcomTabs();
1073                    } else {
1074                        alert('Something went wrong...');
1075                        window.location.reload();
1076                    }
1077                }
1078            });
1079        });
1080        // Domains Config Form
1081        $(document).on('submit', '.wcop_df_domains_config_form', function (e) {
1082            e.preventDefault();
1083            let form = $(this);
1084            let submit = form.find('button[type="submit"]');
1085            let submit_val = submit.text();
1086            let container = form.parents('#wcop_df_container');
1087            submit.html(whcom_spinner_icon);
1088            let data = form.serializeArray();
1089            data.push({'name': 'currency', 'value': getUrlParameter('currency')});
1090            jQuery.ajax({
1091                url: wcop_ajax.ajax_url,
1092                type: 'post',
1093                data: data,
1094                success: function (response) {
1095                    let res = JSON.parse(response);
1096                    submit.text(submit_val);
1097                    if (res.status === "OK") {
1098                        container.html(res.response_form);
1099                        wcop_df_update_product_summary();
1100                        $(".whcom_sticky_item").whcom_sticky({
1101                            'parent': '.whcom_main',
1102                            'offset_top': 40
1103                        });
1104                        $('html,body').animate({
1105                            scrollTop: 100
1106                        }, 700);
1107                    } else {
1108                    }
1109                }
1110            });
1111        });
1112        // Recalculate Product Config Options
1113        $(document).on('change', '.wcop_billingcycle_selector', function (e) {
1114            e.preventDefault();
1115
1116            let select = $(this);
1117            let selected = select.find(':selected');
1118            let cart_index = select.data('cart-index');
1119            let billingcycle = selected.data('billingcycle');
1120            let product_id = select.data('product-id');
1121            let options_wrapper = $('#wcop_df_product_options_' + cart_index);
1122
1123
1124            options_wrapper.html(whcom_spinner_icon);
1125            let data = {};
1126            data.action = "wcop_domain_first";
1127            data.wcop_what = "get_product_options";
1128            data.cart_index = cart_index;
1129            data.billingcycle = billingcycle;
1130            data.product_id = product_id;
1131            data.currency = getUrlParameter('currency');
1132            jQuery.ajax({
1133                url: wcop_ajax.ajax_url,
1134                type: 'post',
1135                data: data,
1136                success: function (response) {
1137                    let res = JSON.parse(response);
1138                    if (res.status === "OK") {
1139                        options_wrapper.html(res.response_form);
1140                        wcop_df_update_product_summary();
1141                    } else {
1142                        options_wrapper.html(res.message);
1143                    }
1144                }
1145            });
1146        });
1147        // Recalculate Product Summaries
1148        $(document).on('change', '#wcop_df_container .wcop_input', function () {
1149            wcop_df_update_product_summary();
1150        });
1151        // Products Config Form
1152        $(document).on('submit', '.wcop_df_products_config_form', function (e) {
1153
1154            e.preventDefault();
1155            let form = $(this);
1156            let submit = form.find('button[type="submit"]');
1157            let submit_val = submit.text();
1158            let container = form.parents('#wcop_df_container');
1159            let response_text = container.find('.wcop_contacts_response');
1160            submit.html(whcom_spinner_icon);
1161            response_text.html(whcom_spinner_icon).show();
1162            let data = form.serializeArray();
1163            data.push({'name': 'currency', 'value': getUrlParameter('currency')});
1164            jQuery.ajax({
1165                url: wcop_ajax.ajax_url,
1166                type: 'post',
1167                data: data,
1168                success: function (response) {
1169                    let res = JSON.parse(response);
1170                    submit.text(submit_val);
1171                    if (res.status === "OK") {
1172                        container.html(res.response_form);
1173                        whcom_op_update_cart_summaries();
1174                        $('html,body').animate({
1175                            scrollTop: 100
1176                        }, 700);
1177                    } else {
1178                        response_text.html(res.message).show();
1179                    }
1180                }
1181            });
1182        });
1183        // Order Checkout
1184        $(document).on('submit', '.wcop_df_review_form', function (e) {
1185            e.preventDefault();
1186            let form = $(this);
1187            let submit = form.find('button[type="submit"]');
1188            let submit_val = submit.text();
1189            let container = form.parents('#wcop_df_container');
1190            submit.html(whcom_spinner_icon);
1191            let data = form.serializeArray();
1192            data.push({'name': 'currency', 'value': getUrlParameter('currency')});
1193            jQuery.ajax({
1194                url: wcop_ajax.ajax_url,
1195                type: 'post',
1196                data: data,
1197                success: function (response) {
1198                    let res = JSON.parse(response);
1199                    submit.text(submit_val);
1200                    if (res.status === "OK") {
1201                        container.html(res.response_form);
1202                        $('html,body').animate({
1203                            scrollTop: 100
1204                        }, 700);
1205                    }
1206                }
1207            });
1208        });
1209        $(document).on('submit', '.wcop_df_checkout_form', function (e) {
1210            e.preventDefault();
1211            let form = $(this);
1212            let submit = form.find('button[type="submit"]');
1213            let submit_val = submit.text();
1214            let container = form.parents('#wcop_df_container');
1215            let response_text = container.find('.wcop_contacts_response');
1216            submit.html(whcom_spinner_icon);
1217            response_text.html(whcom_spinner_icon).show();
1218            let data = form.serializeArray();
1219            data.push({'name': 'currency', 'value': getUrlParameter('currency')});
1220            jQuery.ajax({
1221                url: wcop_ajax.ajax_url,
1222                type: 'post',
1223                data: data,
1224                success: function (response) {
1225                    let res = JSON.parse(response);
1226                    submit.text(submit_val);
1227                    if (res.status === "OK") {
1228                        container.html(res.response_form);
1229                        $('html,body').animate({
1230                            scrollTop: 100
1231                        }, 700);
1232                    } else {
1233                        response_text.html(res.message).show();
1234                    }
1235                }
1236            });
1237        });
1238
1239        window.onload = function () {
1240            //~~SA Retrieve the product ID from the specified input field
1241            var productId = jQuery(document).find('div#wcop_sp_main .wcop_sp_add_product_form input[name="get_product_id"]').val();
1242            if (!(productId > 0)) {
1243                let urlPid = new URLSearchParams(window.location.search).get('pid');
1244                if (urlPid && parseInt(urlPid, 10) > 0) {
1245                    productId = parseInt(urlPid, 10);
1246                    jQuery(document).find('div#wcop_sp_main .wcop_sp_add_product_form input[name="get_product_id"]').val(productId);
1247                }
1248            }
1249
1250            //~~SA Check if the productId is valid (greater than 0)
1251            if (productId > 0) {
1252                //~~SA Select matching product in dropdown or radio-based layouts
1253                let selectEl = jQuery('select[id^="wcop_sp_product_select"]').first();
1254                if (selectEl.length) {
1255                    selectEl.find('option[value="' + productId + '"]').prop('selected', true);
1256                    selectEl.trigger('change').trigger('click');
1257                } else {
1258                    let radioEl = jQuery('input[name="pid"][value="' + productId + '"]').first();
1259                    if (radioEl.length) {
1260                        radioEl.prop('checked', true);
1261                        radioEl.closest('label.whcom_label_product.whcom_radio').addClass('whcom_checked');
1262                        radioEl.trigger('change').trigger('click');
1263                    }
1264                }
1265
1266            }
1267            //~~SA Optionally, you can call additional functions or perform other actions here
1268        };
1269
1270
1271        window.wcop_sp_update_product_summary = function () {
1272
1273            let prd_form = $('form.wcop_sp_add_product');
1274            let curr_temp = prd_form.find('[name="wcop_current_template"]').val();
1275            $('.wcop_sp_section_navi .my-button-item .wcop_sp_button button').prop('disabled', true);
1276            $('.wcop_sp_section_navi .my-button-item .wcop_sp_button button').text('Waiting...');
1277            if (prd_form[0]) {
1278                let prd_summary = $('.wcop_sp_order_summary');
1279                let prd_summary_spinner = $('.wcop_sp_product_summary_spinner');
1280                let prd_submit = $('.wcop_sp_product_submit');
1281                prd_submit.hide();
1282                prd_summary_spinner.show();
1283                $("#wcop_loader_id").css("opacity", "0.3");
1284                $(".wcop_loader_class").css("display", "block");
1285                $('.wcop_sp_replace_spinner').html(whcom_spinner_icon_only);
1286                if (curr_temp === '03_ease') {
1287                    $('.whcom_text_right.whcom_text_2x').html('');
1288                }
1289
1290                let data = prd_form.serializeArray();
1291                data.push({'name': 'wcop_sp_what', 'value': 'wcop_sp_generate_summary'});
1292                /*data.push({'name': 'wcop_sp_template', 'value': curr_temp });*/
1293                console.log("I am here in update summary");
1294                console.log(data);
1295                data.push({'name': 'currency', 'value': getUrlParameter('currency')});
1296                $.ajax({
1297                    url: wcop_ajax.ajax_url,
1298                    type: 'post',
1299                    data: data,
1300                    success: function (response) {
1301                        let res = JSON.parse(response);
1302                        console.log(res);
1303                        if (res.status === "OK") {
1304                            //== Change Summary section heading either it is empty or populated with content
1305                            jQuery('.whcom_empty_summary_section_heading').css("display", "none");
1306                            jQuery('.whcom_filled_summary_section_heading').css("display", "block");
1307                            if (jQuery(window).width() <= 600) {
1308                                jQuery('.wcop_sp_sidebar-mobile-summary').css("display", "block");
1309                            } else {
1310                                jQuery('.wcop_sp_sidebar-mobile-summary').css("display", "none");
1311                            }
1312                            $(window).resize(function () {
1313                                if (jQuery(window).width() <= 600) {
1314                                    jQuery('.wcop_sp_sidebar-mobile-summary').css("display", "block");
1315                                } else {
1316                                    jQuery('.wcop_sp_sidebar-mobile-summary').css("display", "none");
1317                                }
1318                            });
1319
1320
1321                            whcom_show_notification(res.message, 'whcom_alert whcom_alert_success');
1322                            $("#wcop_loader_id").css("opacity", "1");
1323                            $(".wcop_loader_class").css("display", "none");
1324                            prd_submit.show();
1325                            prd_summary_spinner.fadeOut(500);
1326                            prd_summary.html(res.summary_html.side);
1327                            if (res.summary_html.free_domain) {
1328                                $('[name="regperiod"]').prop('disabled', true).find('option').each(function () {
1329                                    $(this).prop('selected', true);
1330                                    return false;
1331                                });
1332                            } else {
1333                                $('[name="regperiod"]').prop('disabled', false);
1334                            }
1335                            if (res.summary_html.no_options) {
1336                                $('.wcop_sp_product_no_options').show();
1337                            } else {
1338                                $('.wcop_sp_product_no_options').hide();
1339                            }
1340                            if (res.summary_html.discount_message) {
1341                                $('.wcop_sp_coupon_response').html(res.summary_html.discount_message);
1342                                $('.cpn').addClass('slow');
1343                            }
1344                            $('.wcop_sp_section_navi .my-button-item .wcop_sp_button button').prop('disabled', false);
1345                            $('.wcop_sp_section_navi .my-button-item .wcop_sp_button button.next').html(' Next <i class="whcom_icon_angle-circled-right"></i>');
1346                            $('.wcop_sp_section_navi .my-button-item .wcop_sp_button button#skip').html(' skip <i class="whcom_icon_angle-circled-right"></i>');
1347                            $('.wcop_sp_section_navi .my-button-item .wcop_sp_button button.prev').html('<i class="whcom_icon_angle-circled-left"></i> Previous');
1348                            $('#wcop_choose_a_hosting .whcom_row.whcom_sleek_products .whcom_label_product.whcom_radio').css('pointer-events', 'unset');
1349                            $('#wcop_choose_a_hosting .whcom_row.whcom_sleek_products .whcom_label_product.whcom_radio').parent().siblings().children().find('.whcom_button.whcom_button_block').text('Select');
1350                            $('#wcop_choose_a_hosting .whcom_row.whcom_sleek_products .whcom_label_product.whcom_radio').parent().siblings().children().find('.whcom_button.whcom_button_block').css('opacity', '1');
1351                        } else {
1352                            whcom_show_notification(res.message, 'whcom_alert whcom_alert_danger');
1353                            prd_summary.html(res.message);
1354                        }
1355                    }
1356                });
1357            }
1358        };
1359
1360        window.wcop_df_update_product_summary = function () {
1361            let prd_form = $('form.wcop_df_products_config_form');
1362            if (prd_form[0]) {
1363                let prd_summary = prd_form.find('.wcop_df_summary_sidebar');
1364                let prd_summary_spinner = prd_form.find('.wcop_df_product_summary .whcom_icon_spinner-1');
1365                let prd_submit = prd_form.find('.wcop_df_product_submit button');
1366                prd_submit.prop('disabled', true);
1367                prd_summary_spinner.show();
1368                if (prd_form) {
1369                    let data = prd_form.serializeArray();
1370                    data.push({'name': 'wcop_what', 'value': 'products_summary'});
1371                    data.push({'name': 'currency', 'value': getUrlParameter('currency')});
1372                    $.ajax({
1373                        url: whcom_ajax.ajax_url,
1374                        type: 'post',
1375                        data: data,
1376                        success: function (response) {
1377                            let res = JSON.parse(response);
1378                            if (res.status === "OK") {
1379                                prd_submit.prop('disabled', false);
1380                                prd_summary_spinner.fadeOut(500);
1381                                prd_summary.html(res.summary_html);
1382                            } else {
1383                                prd_summary.html(res.message);
1384                            }
1385                        }
1386                    });
1387                }
1388            }
1389        };
1390
1391
1392        //By Fakhir
1393        $("#edit_domain").click(function () {
1394            $('#wcop_sp_choose_a_domain').show();
1395        });
1396
1397        $(document).ready(function () {
1398            $("#wcop_sp_domain_config").hide();
1399        });
1400
1401        $(document).ready(function () {
1402            window.scrollTo(0, 0);
1403        });
1404
1405        //== Show popup for free domains attached to the product
1406        $(document).on('click', '#myBtn', function () {
1407            document.getElementById("myModal").style.display = 'block'
1408        });
1409
1410        //== close free domain information popup on cross button click
1411        $(document).on('click', '#free_domain_modal_close', function () {
1412            document.getElementById("myModal").style.display = 'none'
1413        });
1414
1415        //== Change Summary section heading either it is empty or populated with content
1416        if (jQuery('#whcom_08_elegant_summary_section').is(':empty')) {
1417            jQuery('.whcom_empty_summary_section_heading').css("display", "block");
1418            jQuery('.whcom_filled_summary_section_heading').css("display", "none");
1419        } else {
1420            jQuery('.whcom_empty_summary_section_heading').css("display", "none");
1421            jQuery('.whcom_filled_summary_section_heading').css("display", "block");
1422        }
1423
1424        //== Show Summary Message if 03_ease Cart Summary is empty
1425        $(document).ready(function () {
1426            bold_summary_area = jQuery('#bold_summary_area');
1427            if (bold_summary_area.is(':empty')) {
1428                empty_summary_section_message = "<div style='padding: 10px; min-height: 200px' class='summary-simple-4'>";
1429                empty_summary_section_message += '<h2>Your Cart is empty</h2>';
1430                empty_summary_section_message += '<p>Search a domain or choose a service.</p>';
1431                empty_summary_section_message += '</div>';
1432                bold_summary_area.html(empty_summary_section_message);
1433            }
1434        });
1435
1436        //== Show client register section on button click
1437        $(document).on('click', '#wcop_sp_client_register_section', function () {
1438            let prd_form = $('form.wcop_sp_add_product');
1439            current_template = prd_form.find('[name="wcop_sp_template"]').val();
1440            billing_info_predecessor_container = jQuery(document).find('.wcop_billing_info_predecessor');
1441            billing_info_container = jQuery(document).find('#wcop_billing_info');
1442
1443            let data = {};
1444            data.action = 'wcop_sp_process';
1445            data.wcop_sp_what = 'wcop_include_client_section';
1446
1447            jQuery('#wcop_sp_client_register_section').html(whcom_spinner_icon);
1448            data.currency = getUrlParameter('currency');
1449            $.ajax({
1450                url: wcop_ajax.ajax_url,
1451                type: 'post',
1452                data: data,
1453                success: function (response) {
1454                    let res = JSON.parse(response);
1455                    jQuery('#wcop_sp_client_register_section').html("Register");
1456                    billing_info_predecessor_container.slideUp('', function () {
1457                        billing_info_container.slideDown('', function () {
1458                            if (current_template === '04_sleek') {
1459                                jQuery('.wcop__inner__billing__info').css("display", "block")
1460                            } else {
1461                                jQuery('#wcop_billing_info').css("display", "block");
1462                            }
1463                        });
1464                    });
1465
1466
1467                }
1468            });
1469        });
1470
1471        //== Show client login section on button click
1472        $(document).on('click', '#wcop_sp_client_login_section', function () {
1473
1474            let prd_form = $('form.wcop_sp_add_product');
1475            current_template = prd_form.find('[name="wcop_sp_template"]').val();
1476            billing_info_predecessor_container = jQuery(document).find('.wcop_billing_info_predecessor');
1477            billing_info_container = jQuery(document).find('#wcop_billing_info');
1478            let data = {};
1479            data.action = 'wcop_sp_process';
1480            data.wcop_sp_what = 'wcop_include_client_section';
1481
1482            jQuery('#wcop_sp_client_login_section').html(whcom_spinner_icon);
1483            data.currency = getUrlParameter('currency');
1484            $.ajax({
1485                url: wcop_ajax.ajax_url,
1486                type: 'post',
1487                data: data,
1488                success: function (response) {
1489                    let res = JSON.parse(response);
1490                    jQuery('#wcop_sp_client_login_section').html("Login");
1491                    jQuery('.wcop_sp_billing_tab_link').trigger('click');
1492                    if (current_template === '04_simple') {
1493                        jQuery('#wcop_sp_user_login_link').trigger("click");
1494                    }
1495                    billing_info_predecessor_container.slideUp('', function () {
1496                        billing_info_container.slideDown('', function () {
1497                            if (current_template === '04_sleek') {
1498                                jQuery('.wcop__inner__billing__info').css("display", "block")
1499                            } else {
1500                                jQuery('#wcop_billing_info').css("display", "block");
1501                            }
1502                        });
1503                    });
1504
1505                }
1506            });
1507        });
1508
1509        $(document).on('click', '.wcop_desc_toggle', function (e) {
1510            e.preventDefault();
1511            e.stopPropagation();
1512            
1513            let toggle = $(this);
1514            let container = toggle.closest('.wcop_product_description_sleek');
1515            let list = container.find('ul.wcop_desc_list');
1516            let showMore = toggle.find('.wcop_desc_show_more');
1517            let showLess = toggle.find('.wcop_desc_show_less');
1518            let hiddenItems = list.find('li.wcop_desc_hidden_item');
1519            let productCard = toggle.closest('.whcom_label_product, .wcap_li_content_item');
1520            let itemProduct = productCard.find('span.item_product');
1521
1522            // Remove matchHeight constraints to allow dynamic height with !important
1523            itemProduct.attr('style', function(i, style) {
1524                // Remove any existing height style first
1525                style = (style || '').replace(/height\s*:\s*[^;]+;?\s*/gi, '');
1526                return style + 'height: auto !important;';
1527            });
1528
1529            if (list.hasClass('wcop_desc_collapsed')) {
1530                // EXPANDING: Show hidden items with smooth slide-down animation
1531                
1532                list.removeClass('wcop_desc_collapsed').addClass('wcop_desc_expanded');
1533                
1534                // First, get the natural height while items are visible but height is 0
1535                hiddenItems.each(function() {
1536                    let item = $(this);
1537                    // Make visible but keep height at 0
1538                    item.css({
1539                        'visibility': 'visible',
1540                        'opacity': '0',
1541                        'height': '0',
1542                        'padding': '0',
1543                        'margin': '0',
1544                        'overflow': 'hidden'
1545                    });
1546                    
1547                    // Temporarily set height to auto to measure
1548                    let currentDisplay = item.css('display');
1549                    item.css('height', 'auto');
1550                    let naturalHeight = item.outerHeight(true);
1551                    item.data('natural-height', naturalHeight);
1552                    
1553                    // Reset back to 0 for animation
1554                    item.css('height', '0');
1555                });
1556                
1557                // Trigger reflow to ensure styles are applied
1558                list[0].offsetHeight;
1559                
1560                // Now animate to natural height with transition
1561                hiddenItems.each(function() {
1562                    let item = $(this);
1563                    item.css({
1564                        'transition': 'all 0.3s ease-in-out',
1565                        'opacity': '1',
1566                        'height': item.data('natural-height') + 'px',
1567                        'padding': '',
1568                        'margin': ''
1569                    });
1570                });
1571                
1572                // After transition completes, set height to auto for flexibility
1573                setTimeout(function() {
1574                    hiddenItems.css({
1575                        'height': 'auto',
1576                        'transition': '',
1577                        'overflow': 'visible'
1578                    });
1579                    // Ensure item_product height remains auto with !important
1580                    itemProduct.attr('style', function(i, style) {
1581                        style = (style || '').replace(/height\s*:\s*[^;]+;?\s*/gi, '');
1582                        return style + 'height: auto !important;';
1583                    });
1584                }, 350);
1585                
1586                showMore.hide();
1587                showLess.show();
1588                
1589            } else {
1590                // COLLAPSING: Hide items with smooth animation
1591                
1592                // First, set explicit heights for animation
1593                hiddenItems.each(function() {
1594                    let item = $(this);
1595                    item.css('height', item.outerHeight(true) + 'px');
1596                });
1597                
1598                // Trigger reflow
1599                list[0].offsetHeight;
1600                
1601                // Now animate to 0
1602                hiddenItems.css({
1603                    'opacity': '0',
1604                    'height': '0',
1605                    'padding': '0',
1606                    'margin': '0',
1607                    'overflow': 'hidden',
1608                    'transition': 'all 0.3s ease-in-out'
1609                });
1610                
1611                setTimeout(function() {
1612                    list.removeClass('wcop_desc_expanded').addClass('wcop_desc_collapsed');
1613                    hiddenItems.css('visibility', 'hidden');
1614                    // Ensure item_product height remains auto with !important
1615                    itemProduct.attr('style', function(i, style) {
1616                        style = (style || '').replace(/height\s*:\s*[^;]+;?\s*/gi, '');
1617                        return style + 'height: auto !important;';
1618                    });
1619                }, 300);
1620                
1621                showMore.show();
1622                showLess.hide();
1623            }
1624        });
1625
1626        // Initialize collapsed lists on page load - set initial state for proper height
1627        $(document).ready(function() {
1628            $('.wcop_product_description_sleek ul.wcop_desc_collapsed').each(function() {
1629                let list = $(this);
1630                let hiddenItems = list.find('li.wcop_desc_hidden_item');
1631                hiddenItems.css({
1632                    'opacity': '0',
1633                    'visibility': 'hidden',
1634                    'height': '0',
1635                    'padding': '0',
1636                    'margin': '0',
1637                    'overflow': 'hidden'
1638                });
1639            });
1640        });
1641        
1642        // Function to override matchHeight for cards with Show More toggle
1643        function wcop_override_matchHeight_for_show_more() {
1644            $('.wcop_product_description_sleek .wcop_desc_toggle').each(function() {
1645                let toggle = $(this);
1646                let productCard = toggle.closest('.whcom_label_product, .wcap_li_content_item');
1647                let itemProduct = productCard.find('span.item_product');
1648                // Set height to auto to override matchHeight with !important
1649                itemProduct.attr('style', function(i, style) {
1650                    style = (style || '').replace(/height\s*:\s*[^;]+;?\s*/gi, '');
1651                    return style + 'height: auto !important;';
1652                });
1653            });
1654        }
1655        
1656        // Override matchHeight for cards with Show More toggle
1657        // This runs on window load AFTER matchHeight has been applied
1658        $(window).on('load', function() {
1659            setTimeout(function() {
1660                wcop_override_matchHeight_for_show_more();
1661            }, 100); // Small delay to ensure matchHeight has completed
1662        });
1663        
1664        // Also override on window resize (matchHeight is called on resize)
1665        $(window).on('resize', function() {
1666            setTimeout(function() {
1667                wcop_override_matchHeight_for_show_more();
1668            }, 150); // Slightly longer delay on resize
1669        });
1670
1671        $(document).on('click', '#whmcs_validation_form button.whcom_button.whcom_text_center', function (event) {
1672            event.preventDefault()
1673            let get_email = jQuery('#whmcs_validation_form input#email').val();
1674            let get_password = jQuery('#whmcs_validation_form input#password').val();
1675            let data = {};
1676            data.action = 'wcop_sp_process';
1677            data.wcop_sp_what = 'validate_login';
1678            data.login_email = get_email;
1679            data.login_pass = get_password;
1680
1681            jQuery('#wcop_sp_client_login_section').html(whcom_spinner_icon);
1682            data.currency = getUrlParameter('currency');
1683            $.ajax({
1684                url: wcop_ajax.ajax_url,
1685                type: 'post',
1686                data: data,
1687                success: function (response) {
1688                    let res = JSON.parse(response);
1689                    console.log(res);
1690                    if (res.status === 'OK') {
1691                        jQuery("#error_message").hide();
1692                        window.location.href = res.invoice_link;
1693                    } else {
1694                        jQuery("#error_message").html(res.message).show();
1695                    }
1696
1697
1698                }
1699            });
1700        });
1701
1702        function getUrlParameter(name) {
1703            const urlParams = new URLSearchParams(window.location.search);
1704            return urlParams.get(name);
1705        }
1706
1707        window.wcop_escape_html = function(value) {
1708            return String(value || '').replace(/[&<>'"]/g, function(char) {
1709                switch (char) {
1710                    case '&': return '&amp;';
1711                    case '<': return '&lt;';
1712                    case '>': return '&gt;';
1713                    case '\'' : return '&#39;';
1714                    case '"': return '&quot;';
1715                    default: return char;
1716                }
1717            });
1718        };
1719
1720        window.wcop_indexed_to_array = function(collection) {
1721            if (Array.isArray(collection)) {
1722                return collection;
1723            }
1724            if (!collection || typeof collection !== 'object') {
1725                return [];
1726            }
1727
1728            return Object.keys(collection).sort(function(a, b) {
1729                return parseInt(a, 10) - parseInt(b, 10);
1730            }).map(function(key) {
1731                return collection[key];
1732            });
1733        };
1734
1735        window.wcop_get_domain_suggestions_container = function(responseContainer, createIfMissing) {
1736            let container = responseContainer.find('.wcop_sp_domain_suggestions');
1737            if (!container.length && createIfMissing) {
1738                container = $('<div class="wcop_sp_domain_suggestions"><div class="wcop_sp_domain_suggestions_heading"></div><div class="wcop_sp_domain_suggestions_list"></div><div class="wcop_sp_domain_suggestions_footer"></div></div>');
1739                responseContainer.append(container);
1740            }
1741            return container;
1742        };
1743
1744        window.wcop_clear_domain_suggestions = function(responseContainer) {
1745            if (!responseContainer || !responseContainer.length) {
1746                return;
1747            }
1748            responseContainer.find('.wcop_sp_domain_suggestions').remove();
1749        };
1750
1751        window.wcop_should_load_domain_suggestions = function(form, domaintype, domain, ext) {
1752            if (!(wcop_ajax && wcop_ajax.enable_domain_suggestions)) {
1753                return false;
1754            }
1755            if (form.hasClass('domain_already_in_cart')) {
1756                return false;
1757            }
1758            domaintype = String(domaintype || '').toLowerCase();
1759            if (domaintype !== 'register' && domaintype !== 'transfer') {
1760                return false;
1761            }
1762            if (!String(domain || '').trim()) {
1763                return false;
1764            }
1765            if (!String(ext || '').trim()) {
1766                return false;
1767            }
1768            return true;
1769        };
1770
1771        window.wcop_render_domain_suggestions = function(responseContainer, response, appendMode) {
1772            let container = window.wcop_get_domain_suggestions_container(responseContainer, true);
1773            let list = container.find('.wcop_sp_domain_suggestions_list');
1774            let footer = container.find('.wcop_sp_domain_suggestions_footer');
1775            let heading = container.find('.wcop_sp_domain_suggestions_heading');
1776
1777            let suggestions = window.wcop_indexed_to_array(response.suggestions);
1778            let hasMore = String(response.has_more || '') === 'yes';
1779            let nextOffset = parseInt(response.next_offset || '0', 10);
1780            let multiEnabled = String(response.multi_domains_enabled || '') === 'yes';
1781            let searchedSld = String(response.searched_sld || '');
1782            let searchedTld = String(response.searched_tld || '');
1783            let domainType = String(response.domain_type || '').toLowerCase();
1784
1785            let headingText = (wcop_ajax.i18n && wcop_ajax.i18n.domainssuggestions) ? wcop_ajax.i18n.domainssuggestions : 'Suggestions';
1786            heading.text(headingText);
1787
1788            if (!appendMode) {
1789                list.empty();
1790            }
1791
1792            let addToCartText = (wcop_ajax.i18n && wcop_ajax.i18n.add_to_cart) ? wcop_ajax.i18n.add_to_cart : 'Add to Cart';
1793            let useThisDomainText = (wcop_ajax.i18n && wcop_ajax.i18n.use_this_domain) ? wcop_ajax.i18n.use_this_domain : 'Use This Domain';
1794            let alreadyInCartText = (wcop_ajax.i18n && wcop_ajax.i18n.already_in_cart) ? wcop_ajax.i18n.already_in_cart : 'Already in Cart';
1795            let registerText = (wcop_ajax.i18n && wcop_ajax.i18n.domain_type_register) ? wcop_ajax.i18n.domain_type_register : 'Register';
1796            let transferText = (wcop_ajax.i18n && wcop_ajax.i18n.domain_type_transfer) ? wcop_ajax.i18n.domain_type_transfer : 'Transfer';
1797
1798            if (!suggestions.length && !appendMode) {
1799                let noSuggestionsText = (wcop_ajax.i18n && wcop_ajax.i18n.no_suggestions_found) ? wcop_ajax.i18n.no_suggestions_found : 'No domain suggestions found for this search.';
1800                list.html('<div class="wcop_sp_domain_suggestion_empty">' + window.wcop_escape_html(noSuggestionsText) + '</div>');
1801            } else {
1802                suggestions.forEach(function(item) {
1803                    let domain = String(item.domain || '');
1804                    let tld = String(item.tld || '');
1805                    let domaintype = String(item.domaintype || domainType || 'register');
1806                    let regperiod = parseInt(item.regperiod || '1', 10);
1807                    if (!(regperiod > 0)) {
1808                        regperiod = 1;
1809                    }
1810                    let price = String(item.price || '');
1811                    let durationLabel = String(item.duration_label || ((regperiod === 1) ? 'Year' : 'Years'));
1812                    let alreadyAdded = String(item.already_added || '') 
1812=== 'yes';
1813                    let typeLabel = (domaintype === 'transfer') ? transferText : registerText;
1814
1815                    let actionHtml = '';
1816                    if (multiEnabled) {
1817                        // if (alreadyAdded) {
1818                        //     actionHtml = '<button type="button" class="whcom_button wcop_suggestion_disabled_btn" disabled><strong>' + window.wcop_escape_html(alreadyInCartText) + '</strong></button>';
1819                        // } else {
1820                            actionHtml += '<form class="wcop_sp_attach_product_domain wcop_sp_domain_attachment_form wcop_sp_suggestion_action_form" method="post">';
1821                            actionHtml += '<input type="hidden" name="action" value="wcop_sp_process">';
1822                            actionHtml += '<input type="hidden" name="wcop_sp_what" value="add_domain_to_cart">';
1823                            actionHtml += '<input type="hidden" name="domain" value="' + window.wcop_escape_html(domain) + '">';
1824                            actionHtml += '<input type="hidden" name="domaintype" value="' + window.wcop_escape_html(domaintype) + '">';
1825                            actionHtml += '<input type="hidden" name="tld" value="' + window.wcop_escape_html(tld) + '">';
1826                            actionHtml += '<input type="hidden" name="regperiod" value="' + window.wcop_escape_html(regperiod) + '">';
1827                            actionHtml += '<button type="button" data-keep-domain-response="yes" class="whcom_button whcom_button_success wcop_multi_domain_add_btn wcop_sp_suggestion_multi_add_btn"><strong><i class="whcom_icon_basket"></i> ' + window.wcop_escape_html(addToCartText) + '</strong></button>';
1828                            actionHtml += '</form>';
1829                        //}
1830                    } else {
1831                        actionHtml += '<form class="wcop_sp_attach_product_domain wcop_sp_suggestion_action_form" method="post">';
1832                        actionHtml += '<input type="hidden" name="action" value="wcop_sp_process">';
1833                        actionHtml += '<input type="hidden" name="wcop_sp_what" value="attach_domain">';
1834                        actionHtml += '<input type="hidden" name="domain" value="' + window.wcop_escape_html(domain) + '">';
1835                        actionHtml += '<input type="hidden" name="domaintype" value="' + window.wcop_escape_html(domaintype) + '">';
1836                        actionHtml += '<input type="hidden" name="tld" value="' + window.wcop_escape_html(tld) + '">';
1837                        actionHtml += '<input type="hidden" name="regperiod" value="' + window.wcop_escape_html(regperiod) + '">';
1838                        actionHtml += '<button type="submit" class="whcom_button whcom_button_success wcop_sp_suggestion_use_btn"><strong>' + window.wcop_escape_html(useThisDomainText) + '</strong></button>';
1839                        actionHtml += '</form>';
1840                    }
1841
1842                    let rowHtml = '';
1843                    rowHtml += '<div class="wcop_sp_domain_suggestion_item">';
1844                    rowHtml += '<div class="wcop_sp_domain_suggestion_meta">';
1845                    rowHtml += '<div class="wcop_sp_domain_suggestion_name">' + window.wcop_escape_html(domain) + '</div>';
1846                    rowHtml += '<div class="wcop_sp_domain_suggestion_type">' + window.wcop_escape_html(typeLabel) + '</div>';
1847                    rowHtml += '</div>';
1848                    rowHtml += '<div class="wcop_sp_domain_suggestion_price">';
1849                    rowHtml += '<div class="wcop_sp_domain_suggestion_price_value">' + window.wcop_escape_html(price) + '</div>';
1850                    rowHtml += '<div class="wcop_sp_domain_suggestion_price_period">' + window.wcop_escape_html(regperiod + ' ' + durationLabel) + '</div>';
1851                    rowHtml += '</div>';
1852                    rowHtml += '<div class="wcop_sp_domain_suggestion_action">' + actionHtml + '</div>';
1853                    rowHtml += '</div>';
1854                    list.append(rowHtml);
1855                });
1856            }
1857
1858            container.attr('data-next-offset', String(nextOffset || 0));
1859            container.attr('data-domain', searchedSld);
1860            container.attr('data-ext', searchedTld);
1861            container.attr('data-domaintype', domainType);
1862
1863            if (hasMore) {
1864                let loadMoreText = (wcop_ajax.i18n && wcop_ajax.i18n.load_more_suggestions) ? wcop_ajax.i18n.load_more_suggestions : 'Load More';
1865                footer.html('<button type="button" class="whcom_button wcop_suggestion_load_more">' + window.wcop_escape_html(loadMoreText) + '</button>');
1866            } else {
1867                footer.empty();
1868            }
1869        };
1870
1871        window.wcop_request_domain_suggestions = function(mainForm, responseContainer, domain, ext, domaintype, offset, appendMode) {
1872            if (!(wcop_ajax && wcop_ajax.enable_domain_suggestions)) {
1873                return;
1874            }
1875
1876            let data = [];
1877            if (mainForm && mainForm.length) {
1878                data = mainForm.serializeArray();
1879            }
1880
1881            let container = window.wcop_get_domain_suggestions_container(responseContainer, true);
1882            let footer = container.find('.wcop_sp_domain_suggestions_footer');
1883
1884            if (!appendMode) {
1885                container.find('.wcop_sp_domain_suggestions_heading').text('Suggestions');
1886                container.find('.wcop_sp_domain_suggestions_list').empty();
1887                footer.html('<div class="wcop_sp_domain_suggestions_loading">' + whcom_spinner_icon + '</div>');
1888            } else {
1889                let loadBtn = container.find('.wcop_suggestion_load_more');
1890                if (loadBtn.length) {
1891                    loadBtn.prop('disabled', true).html(whcom_spinner_icon_only);
1892                }
1893            }
1894
1895            data.push({ name: 'action', value: 'wcop_sp_process' });
1896            data.push({ name: 'wcop_sp_what', value: 'get_domain_suggestions' });
1897            data.push({ name: 'domain', value: String(domain || '').trim() });
1898            data.push({ name: 'ext', value: String(ext || '').trim() });
1899            data.push({ name: 'domaintype', value: String(domaintype || '').trim() });
1900            data.push({ name: 'offset', value: parseInt(offset || '0', 10) || 0 });
1901            data.push({ name: 'currency', value: getUrlParameter('currency') || '' });
1902
1903            $.ajax({
1904                url: wcop_ajax.ajax_url,
1905                type: 'post',
1906                data: data,
1907                success: function(response) {
1908                    let res;
1909                    try {
1910                        res = JSON.parse(response);
1911                    } catch (e) {
1912                        res = { status: 'ERROR', message: '' };
1913                    }
1914
1915                    if (res.status === 'OK' && String(res.suggestions_enabled || 'yes') === 'yes') {
1916                        window.wcop_render_domain_suggestions(responseContainer, res, appendMode);
1917                    } else {
1918                        if (!appendMode) {
1919                            let message = (res && res.message) ? res.message : '';
1920                            if (!message) {
1921                                message = (wcop_ajax.i18n && wcop_ajax.i18n.error_occurred) ? wcop_ajax.i18n.error_occurred : 'An error occurred. Please try again.';
1922                            }
1923                            container.find('.wcop_sp_domain_suggestions_list').html('<div class="wcop_sp_domain_suggestion_empty">' + window.wcop_escape_html(message) + '</div>');
1924                            container.find('.wcop_sp_domain_suggestions_footer').empty();
1925                        } else {
1926                            container.find('.wcop_sp_domain_suggestions_footer').empty();
1927                        }
1928                    }
1929                },
1930                error: function() {
1931                    if (!appendMode) {
1932                        let message = (wcop_ajax.i18n && wcop_ajax.i18n.error_occurred) ? wcop_ajax.i18n.error_occurred : 'An error occurred. Please try again.';
1933                        container.find('.wcop_sp_domain_suggestions_list').html('<div class="wcop_sp_domain_suggestion_empty">' + window.wcop_escape_html(message) + '</div>');
1934                        container.find('.wcop_sp_domain_suggestions_footer').empty();
1935                    } else {
1936                        let loadMoreText = (wcop_ajax.i18n && wcop_ajax.i18n.load_more_suggestions) ? wcop_ajax.i18n.load_more_suggestions : 'Load More';
1937                        container.find('.wcop_sp_domain_suggestions_footer').html('<button type="button" class="whcom_button wcop_suggestion_load_more">' + window.wcop_escape_html(loadMoreText) + '</button>');
1938                    }
1939                }
1940            });
1941        };
1942
1943        $(document).on('click', '.wcop_suggestion_load_more', function(e) {
1944            e.preventDefault();
1945            let button = $(this);
1946            let responseContainer = $('.wcop_sp_domain_response');
1947            let container = button.closest('.wcop_sp_domain_suggestions');
1948            if (!container.length) {
1949                return;
1950            }
1951
1952            let nextOffset = parseInt(container.attr('data-next-offset') || '0', 10);
1953            let domain = String(container.attr('data-domain') || '');
1954            let ext = String(container.attr('data-ext') || '');
1955            let domaintype = String(container.attr('data-domaintype') || '');
1956            let mainForm = $('form.wcop_sp_add_product');
1957
1958            window.wcop_request_domain_suggestions(mainForm, responseContainer, domain, ext, domaintype, nextOffset, true);
1959        });
1960
1961        window.wcop_multi_domain_enabled = false;
1962        window.wcop_multi_domain_cart = window.wcop_multi_domain_cart || {};
1963
1964        window.wcop_scroll_to_product_section = function() {
1965            let productSection = $('#wcop_choose_a_hosting');
1966            if (!productSection.length) {
1967                productSection = $('select[id^="wcop_sp_product_select"], input[name="pid"]').first().closest('.wcop_sp_section, .whcom_row, .whcom_form_field');
1968            }
1969            if (productSection.length) {
1970                $('html, body').animate({
1971                    scrollTop: Math.max(productSection.offset().top - 80, 0
1971)
1972                }, 450);
1973            }
1974        };
1975
1976        window.wcop_get_selected_product_id = function(prd_form) {
1977            if (!prd_form || !prd_form.length) {
1978                return 0;
1979            }
1980
1981            let pidInputs = prd_form.find('input[name="pid"][type="radio"], input[name="pid"][type="checkbox"]');
1982            if (pidInputs.length) {
1983                let checkedPid = parseInt(pidInputs.filter(':checked').val() || '0', 10);
1984                return (checkedPid > 0) ? checkedPid : 0;
1985            }
1986
1987            let pidSelect = prd_form.find('select[id^="wcop_sp_product_select"]').first();
1988            if (pidSelect.length) {
1989                let selectedPid = parseInt(pidSelect.val() || '0', 10);
1990                return (selectedPid > 0) ? selectedPid : 0;
1991            }
1992
1993            let hiddenPid = parseInt(prd_form.find('input[name="pid"][type="hidden"]').first().val() || '0', 10);
1994            if (hiddenPid > 0) {
1995                return hiddenPid;
1996            }
1997
1998            let pid = parseInt(prd_form.find('input[name="get_product_id"]').val() || '0', 10);
1999            return (pid > 0) ? pid : 0;
2000        };
2001
2002        window.wcop_product_requires_domain = function(prd_form) {
2003            if (!prd_form || !prd_form.length) {
2004                return false;
2005            }
2006
2007            let selectedRadio = prd_form.find('input[name="pid"]:checked');
2008            if (selectedRadio.length) {
2009                return String(selectedRadio.data('domain-required') || '') 
2009=== 'yes';
2010            }
2011
2012            let selectedOption = prd_form.find('select[id^="wcop_sp_product_select"] option:selected');
2013            if (selectedOption.length) {
2014                return String(selectedOption.data('domain-required') || '') === 'yes';
2015            }
2016
2017            return false;
2018        };
2019
2020        window.wcop_ensure_hidden_input = function(prd_form, name) {
2021            let hiddenWrapper = prd_form.find('#wcop_multi_domain_attached_hidden_fields');
2022            if (!hiddenWrapper.length) {
2023                hiddenWrapper = $('<div id="wcop_multi_domain_attached_hidden_fields" style="display:none;"></div>');
2024                prd_form.append(hiddenWrapper);
2025            }
2026
2027            let input = hiddenWrapper.find('input[name="' + name + '"]');
2028            if (!input.length) {
2029                input = $('<input type="hidden" name="' + name + '" value="">');
2030                hiddenWrapper.append(input);
2031            }
2032
2033            return input;
2034        };
2035
2036        window.wcop_ensure_multi_domain_state_input = function(prd_form) {
2037            let stateInput = prd_form.find('input[name="wcop_cart_domains_json"]');
2038            if (!stateInput.length) {
2039                stateInput = $('<input type="hidden" name="wcop_cart_domains_json" value="">');
2040                prd_form.append(stateInput);
2041            }
2042            return stateInput;
2043        };
2044
2045        window.wcop_build_multi_domain_state_from_dom = function(removeCartItemId = null) {
2046            let domState = {};
2047            $('.wcop_multi_domain_cart_item').each(function() {
2048                let item = $(this);
2049                let cartItemId = item.data('cart-item-id');
2050                if (!cartItemId) return;
2051
2052                if(removeCartItemId && cartItemId == removeCartItemId) return;
2053
2054                let domain = item.find('input[name$="[domain]"]').val() || item.data('domain') || '';
2055                let domaintype = item.find('input[name$="[domaintype]"]').val() || item.data('domain-type') || '';
2056                let tld = item.find('input[name$="[tld]"]').val() || '';
2057                let regperiod = item.find('.wcop_multi_domain_regperiod_select').val() || item.find('select[name$="[regperiod]"]').val() || 1;
2058                let eppcode = item.find('.wcop_multi_domain_eppcode_input').val() || '';
2059                let addons = [];
2060                let fieldMap = {};
2061                item.find('.wcop_multi_domain_addon_checkbox:checked').each(function() {
2062                    addons.push($(this).val());
2063                });
2064
2065                item.find('[name^="wcop_cart_domains["][name*="[domainfields]"]').each(function() {
2066                    let fieldEl = $(this);
2067                    let fieldName = String(fieldEl.attr('name') || '');
2068                    let indexMatch = fieldName.match(/\[domainfields\]\[(\d+)\]/);
2069                    if (!indexMatch) {
2070                        return;
2071                    }
2072
2073                    let index = parseInt(indexMatch[1], 10);
2074                    if (!(index >= 0)) {
2075                        return;
2076                    }
2077
2078                    if (fieldEl.is(':radio') && !fieldEl.is(':checked')) {
2079                        return;
2080                    }
2081
2082                    let fieldValue = '';
2083                    if (fieldEl.is(':checkbox')) {
2084                        fieldValue = fieldEl.is(':checked') ? (fieldEl.val() || 'yes') : '';
2085                    } else {
2086                        fieldValue = fieldEl.val() || '';
2087                    }
2088
2089                    fieldMap[index] = fieldValue;
2090                });
2091
2092                let domainfields = [];
2093                Object.keys(fieldMap).sort(function(a, b) {
2094                    return parseInt(a, 10) - parseInt(b, 10);
2095                }
2095).forEach(function(key) {
2096                    domainfields.push(fieldMap[key]);
2097                });
2098
2099                domState[cartItemId] = {
2100                    id: cartItemId,
2101                    domain: domain,
2102                    domaintype: domaintype,
2103                    tld: tld,
2104                    regperiod: regperiod,
2105                    eppcode: eppcode,
2106                    addons: addons,
2107                    domainfields: domainfields
2108                };
2109            });
2110            return domState;
2111        };
2112
2113        window.wcop_sanitize_multi_domain_state = function(state) {
2114            let cleaned = {};
2115            if (!state || typeof state !== 'object') {
2116                return cleaned;
2117            }
2118
2119            Object.keys(state).forEach(function(key) {
2120                let item = state[key] || {};
2121                let safeKey = item.id || key;
2122                if (!safeKey || !item.domain || !item.domaintype) {
2123                    return;
2124                }
2125
2126                let addons = [];
2127                if (Array.isArray(item.addons)) {
2128                    addons = item.addons;
2129                } else if (item.addons && typeof item.addons === 'object') {
2130                    addons = Object.keys(item.addons).map(function(addonKey) {
2131                        return item.addons[addonKey];
2132                    });
2133                } else if (typeof item.addons === 'string' && item.addons) {
2134                    addons = [item.addons];
2135                }
2136
2137                let domainfields = [];
2138                if (Array.isArray(item.domainfields)) {
2139                    domainfields = item.domainfields;
2140                } else if (item.domainfields && typeof item.domainfields === 'object') {
2141                    domainfields = Object.keys(item.domainfields).sort(function(a, b) {
2142                        return parseInt(a, 10) - parseInt(b, 10);
2143                    }).map(function(fieldKey) {
2144                        return item.domainfields[fieldKey];
2145                    });
2146                }
2147
2148                cleaned[safeKey] = {
2149                    id: safeKey,
2150                    domain: item.domain,
2151                    domaintype: item.domaintype,
2152                    tld: item.tld || '',
2153                    regperiod: item.regperiod || 1,
2154                    eppcode: item.eppcode || '',
2155                    addons: addons,
2156                    domainfields: domainfields
2157                };
2158            });
2159
2160            return cleaned;
2161        };
2162
2163        window.wcop_get_multi_domain_cart_state = function(prd_form, removeCartItemId = null) {
2164            let stateInput = window.wcop_ensure_multi_domain_state_input(prd_form);
2165            let raw = stateInput.val();
2166            if (!raw) {
2167                let domState = window.wcop_build_multi_domain_state_from_dom(removeCartItemId);
2168                if (Object.keys(domState).length) {
2169                    return window.wcop_sanitize_multi_domain_state(domState);
2170                }
2171                return (window.wcop_multi_domain_cart && typeof window.wcop_multi_domain_cart === 'object') ? window.wcop_sanitize_multi_domain_state(window.wcop_multi_domain_cart) : {};
2172            }
2173
2174            try {
2175                let parsed = JSON.parse(raw);
2176                if (parsed && typeof parsed === 'object') {
2177                    return window.wcop_sanitize_multi_domain_state(parsed);
2178                }
2179            } catch (e) {
2180            }
2181
2182            let domState = window.wcop_build_multi_domain_state_from_dom(removeCartItemId);
2183            return window.wcop_sanitize_multi_domain_state(domState);
2184        };
2185
2186        window.wcop_sync_multi_domain_cart_state = function(prd_form, state) {
2187            let safeState = window.wcop_sanitize_multi_domain_state((state && typeof state === 'object') ? state : {});
2188            window.wcop_multi_domain_cart = safeState;
2189            let stateInput = window.wcop_ensure_multi_domain_state_input(prd_form);
2190            stateInput.val(JSON.stringify(safeState));
2191        };
2192
2193        window.wcop_render_multi_domain_shared_config = function(prd_form, serverHtml) {
2194            let domainCartContainer = $('#wcop_multi_domain_cart_container');
2195            if (!domainCartContainer.length) {
2196                return;
2197            }
2198
2199            if (!(window.wcop_get_selected_product_id(prd_form) > 0)) {
2200                $('#wcop_multi_domain_shared_config_container').remove();
2201                return;
2202            }
2203
2204            let sharedContainer = $('#wcop_multi_domain_shared_config_container');
2205            if (!sharedContainer.length) {
2206                sharedContainer = $('<div id="wcop_multi_domain_shared_config_container"></div>');
2207                domainCartContainer.after(sharedContainer);
2208            }
2209
2210            let html = String(serverHtml || '').trim();
2211            if (html) {
2212                sharedContainer.html(html).show();
2213            } else {
2214                sharedContainer.remove();
2215            }
2216        };
2217
2218        window.wcop_sync_attached_domain_fields = function(prd_form, attachedDomainId, attachedDomain, productRequiresDomain) {
2219            productRequiresDomain = !!productRequiresDomain;
2220            attachedDomain = attachedDomain || {};
2221
2222            let attachedIdInput = window.wcop_ensure_hidden_input(prd_form, 'wcop_attached_domain_id');
2223            attachedIdInput.val(attachedDomainId || '');
2224
2225            let attachedDomainFlag = window.wcop_ensure_hidden_input(prd_form, 'attached_domain');
2226            let domainInput = window.wcop_ensure_hidden_input(prd_form, 'domain');
2227            let tldInput = window.wcop_ensure_hidden_input(prd_form, 'tld');
2228            let domainTypeInput = window.wcop_ensure_hidden_input(prd_form, 'domaintype');
2229            let regperiodInput = window.wcop_ensure_hidden_input(prd_form, 'regperiod');
2230            let eppInput = window.wcop_ensure_hidden_input(prd_form, 'eppcode');
2231            let dnsInput = window.wcop_ensure_hidden_input(prd_form, 'dnsmanagement');
2232            let emailInput = window.wcop_ensure_hidden_input(prd_form, 'emailforwarding');
2233            let idProtectionInput = window.wcop_ensure_hidden_input(prd_form, 'idprotection');
2234            let domainFieldsJsonInput = window.wcop_ensure_hidden_input(prd_form, 'wcop_attached_domainfields_json');
2235
2236            if (!productRequiresDomain || !attachedDomain || !attachedDomain.domain) {
2237                attachedDomainFlag.val('');
2238                domainInput.val('');
2239                tldInput.val('');
2240                domainTypeInput.val('');
2241                regperiodInput.val('');
2242                eppInput.val('');
2243                dnsInput.val('');
2244                emailInput.val('');
2245                idProtectionInput.val('');
2246                domainFieldsJsonInput.val('[]');
2247                return;
2248            }
2249
2250            let addons = Array.isArray(attachedDomain.addons) ? attachedDomain.addons : [];
2251
2252            attachedDomainFlag.val('yes');
2253            domainInput.val(attachedDomain.domain || '');
2254            tldInput.val(attachedDomain.tld || '');
2255            domainTypeInput.val(attachedDomain.domaintype || 'register');
2256            regperiodInput.val(attachedDomain.regperiod || 1);
2257            eppInput.val(attachedDomain.eppcode || '');
2258            dnsInput.val(addons.indexOf('dnsmanagement') !== -1 ? '1' : '');
2259            emailInput.val(addons.indexOf('emailforwarding') !== -1 ? '1' : '');
2260            idProtectionInput.val(addons.indexOf('idprotection') !== -1 ? '1' : '');
2261            domainFieldsJsonInput.val(JSON.stringify(Array.isArray(attachedDomain.domainfields) ? attachedDomain.domainfields : []));
2262        };
2263
2264        window.wcop_apply_summary_html = function(prd_form, summaryHtml) {
2265            if (!(summaryHtml && summaryHtml.side)) {
2266                return;
2267            }
2268
2269            let prdSummary = prd_form.find('.wcop_sp_order_summary_sidebar, .whcom_08_elegant_summary_section, #bold_summary_area, #summary_section, .wcop_sp_order_summary');
2270            prdSummary.html(summaryHtml.side);
2271            $('.wcop_sp_order_summary').html(summaryHtml.side);
2272        };
2273
2274        window.wcop_apply_multi_domain_response = function(prd_form, res) {
2275            let state = (res && res.domain_cart) ? res.domain_cart : window.wcop_get_multi_domain_cart_state(prd_form);
2276            let hasSelectedProduct = window.wcop_get_selected_product_id(prd_form) > 0;
2277
2278            if (!hasSelectedProduct) {
2279                Object.keys(state || {}).forEach(function(key) {
2280                    let item = state[key] || {};
2281                    if (String(item.domaintype || '').toLowerCase() === 'existing') {
2282                        delete state[key];
2283                    }
2284                });
2285            }
2286
2287            window.wcop_sync_multi_domain_cart_state(prd_form, state);
2288
2289            let stateIdMap = {};
2290            Object.keys(state || {}
2290).forEach(function(id) {
2291                stateIdMap[String(id)] = true;
2292            });
2293            $('#wcop_multi_domain_cart_container .wcop_multi_domain_cart_item').each(function() {
2294                let cartItem = $(this);
2295                let cartItemId = String(cartItem.data('cart-item-id') || '');
2296                if (!stateIdMap[cartItemId]) {
2297                    cartItem.remove();
2298                }
2299            });
2300
2301            let stateKeys = Object.keys(state || {});
2302            window.wcop_multi_domain_enabled = !!stateKeys.length;
2303
2304            let paymentOptionsContainer = prd_form.find('.whcom_payment_options');
2305            let paymentOptionsHeading = prd_form.find('.wcop_payment_heading');
2306            let defaultPaymentOptions = prd_form.find('.whcom_default_payment_options');
2307            if (res && Object.prototype.hasOwnProperty.call(res, 'payment_options')) {
2308                let paymentHtml = String(res.payment_options || '');
2309                if (paymentHtml.trim()) {
2310                    paymentOptionsContainer.html(paymentHtml);
2311                    paymentOptionsHeading.show();
2312                    defaultPaymentOptions.hide();
2313                } else if (!hasSelectedProduct && !stateKeys.length) {
2314                    paymentOptionsContainer.empty();
2315                    paymentOptionsHeading.hide();
2316                    defaultPaymentOptions.show();
2317                }
2318            }
2319
2320            let attachedDomainId = (res && res.attached_domain_id) ? String(res.attached_domain_id) : '';
2321            if (!attachedDomainId || !state[attachedDomainId]) {
2322                attachedDomainId = prd_form.find('input[name="wcop_attached_domain_id"]').val() || '';
2323            }
2324            if ((!attachedDomainId || !state[attachedDomainId]) && stateKeys.length) {
2325                attachedDomainId = stateKeys[0];
2326            }
2327
2328            let attachedDomain = (res && res.attached_domain && res.attached_domain.domain) ? res.attached_domain : (state[attachedDomainId] || null);
2329            let productRequiresDomain = (res && res.product_requires_domain) ? String(res.product_requires_domain) === 'yes' : window.wcop_product_requires_domain(prd_form);
2330
2331            window.wcop_render_multi_domain_shared_config(prd_form, (res && res.multi_domain_shared_config_html) ? res.multi_domain_shared_config_html : '');
2332            let selector = $('#wcop_multi_domain_attached_domain_select');
2333            if (selector.length && attachedDomainId) {
2334                selector.val(attachedDomainId);
2335            }
2336
2337            if (!stateKeys.length) {
2338                $('#wcop_multi_domain_cart_container').hide();
2339                $('#wcop_multi_domain_shared_config_container').remove();
2340                window.wcop_sync_attached_domain_fields(prd_form, '', null, false);
2341                return;
2342            }
2343
2344            if (!hasSelectedProduct) {
2345                $('#wcop_multi_domain_shared_config_container').remove();
2346                window.wcop_sync_attached_domain_fields(prd_form, '', null, false);
2347                return;
2348            }
2349
2350            window.wcop_sync_attached_domain_fields(prd_form, attachedDomainId, attachedDomain, productRequiresDomain);
2351        };
2352
2353        window.wcop_collect_domainfields_from_item = function(cartItem) {
2354            let fieldMap = {};
2355            cartItem.find('[name^="wcop_cart_domains["][name*="[domainfields]"]').each(function() {
2356                let input = $(this);
2357                let fieldName = String(input.attr('name') || '');
2358                let indexMatch = fieldName.match(/\[domainfields\]\[(\d+)\]/);
2359                if (!indexMatch) {
2360                    return;
2361                }
2362                let index = parseInt(indexMatch[1], 10);
2363                if (!(index >= 0)) {
2364                    return;
2365                }
2366
2367                if (input.is(':radio') && !input.is(':checked')) {
2368                    return;
2369                }
2370
2371                let value = '';
2372                if (input.is(':checkbox')) {
2373                    value = input.is(':checked') ? (input.val() || 'yes') : '';
2374                } else {
2375                    value = input.val() || '';
2376                }
2377
2378                fieldMap[index] = value;
2379            });
2380
2381            let values = [];
2382            Object.keys(fieldMap).sort(function(a, b) {
2383                return parseInt(a, 10) - parseInt(b, 10);
2384            }
2384).forEach(function(key) {
2385                values.push(fieldMap[key]);
2386            });
2387            return values;
2388        };
2389
2390        window.wcop_refresh_multi_domain_server_state = function(prd_form) {
2391            if (!prd_form || !prd_form.length) {
2392                return;
2393            }
2394
2395            let data = prd_form.serializeArray();
2396            data.push({ name: 'action', value: 'wcop_sp_process' });
2397            data.push({ name: 'wcop_sp_what', value: 'get_domain_cart' });
2398            data.push({ name: 'currency', value: getUrlParameter('currency') || '' });
2399
2400            $.ajax({
2401                url: wcop_ajax.ajax_url,
2402                type: 'post',
2403                data: data,
2404                success: function(response) {
2405                    let res = JSON.parse(response);
2406                    if (res.status === 'OK') {
2407                        window.wcop_apply_multi_domain_response(prd_form, res);
2408                        window.wcop_apply_summary_html(prd_form, res.summary_html || null);
2409                    }
2410                }
2411            });
2412        };
2413
2414        window.wcop_add_domain_to_cart = function(domain, domaintype, tld, regperiod, eppcode, keepDomainResponse) {
2415            let prd_form = $('form.wcop_sp_add_product');
2416            if (!prd_form.length) return;
2417
2418            window.wcop_multi_domain_enabled = true;
2419
2420            if (String(domaintype) === 'existing' && window.wcop_get_selected_product_id(prd_form) <= 0) {
2421                let selectProductText = (wcop_ajax.i18n && wcop_ajax.i18n.select_product_first) ? wcop_ajax.i18n.select_product_first : 'Please select a product first.';
2422                whcom_show_notification(selectProductText, 'whcom_alert whcom_alert_warning');
2423                window.wcop_scroll_to_product_section();
2424                return;
2425            }
2426
2427            let wcop_sp_template = prd_form.find('[name="wcop_sp_template"]').val();
2428            let domain_cart_container = $('#wcop_multi_domain_cart_container');
2429
2430            let currentState = window.wcop_get_multi_domain_cart_state(prd_form);
2431            window.wcop_sync_multi_domain_cart_state(prd_form, currentState);
2432
2433            let data = prd_form.serializeArray();
2434            data.push({ name: 'action', value: 'wcop_sp_process' });
2435            data.push({ name: 'wcop_sp_what', value: 'add_domain_to_cart' });
2436            data.push({ name: 'domain', value: domain });
2437            data.push({ name: 'domaintype', value: domaintype });
2438            data.push({ name: 'tld', value: tld });
2439            data.push({ name: 'regperiod', value: regperiod || 1 });
2440            data.push({ name: 'eppcode', value: eppcode || '' });
2441            data.push({ name: 'wcop_sp_template', value: wcop_sp_template || '' });
2442            data.push({ name: 'currency', value: getUrlParameter('currency') || '' });
2443            
2444            $('.wcop_multi_domain_add_btn').html(whcom_spinner_icon).prop('disabled', true);
2445            
2446            //$('.wcop_sp_replace_spinner').html(whcom_spinner_icon_only);
2447            $.ajax({
2448                url: wcop_ajax.ajax_url,
2449                type: 'post',
2450                data: data,
2451                success: function(response) {
2452                    let res = JSON.parse(response);
2453                    let addToCartText = (wcop_ajax.i18n && wcop_ajax.i18n.add_to_cart) ? wcop_ajax.i18n.add_to_cart : 'Add to Cart';
2454                    $('.wcop_multi_domain_add_btn').html('<i class="whcom_icon_basket"></i> ' + addToCartText).prop('disabled', false);
2455                    
2456                    if (res.status === 'OK') {
2457                        whcom_show_notification(res.message, 'whcom_alert whcom_alert_success');
2458                        
2459                        if (res.domain_config_html) {
2460                            if (!domain_cart_container.length) {
2461                                let domainsInCartText = (wcop_ajax.i18n && wcop_ajax.i18n.domains_in_cart) ? wcop_ajax.i18n.domains_in_cart : 'Domains in Cart';
2462                                let cartHtml = '<div id="wcop_multi_domain_cart_container" class="wcop_multi_domain_cart_container">';
2463                                cartHtml += '<div class="wcop_multi_domain_cart_header">';
2464                                cartHtml += '<h4><i class="whcom_icon_globe"></i> ' + domainsInCartText + '</h4>';
2465                                cartHtml += '</div>';
2466                                cartHtml += '<div class="wcop_multi_domain_cart_items"></div>';
2467                                cartHtml += '</div>';
2468                                $('#wcop_sp_choose_a_domain').after(cartHtml);
2469                                domain_cart_container = $('#wcop_multi_domain_cart_container');
2470                            }
2471                            domain_cart_container.find('.wcop_multi_domain_cart_items').append(res.domain_config_html);
2472                            domain_cart_container.slideDown();
2473                        }
2474
2475                        window.wcop_apply_multi_domain_response(prd_form, res);
2476
2477                        let shouldKeepDomainResponse = !!keepDomainResponse || $('.wcop_sp_domain_suggestions').length > 0;
2478                        if (!shouldKeepDomainResponse) {
2479                            $('.wcop_sp_domain_response').empty();
2480                            $('form.wcop_sp_check_product_domain').each(function() {
2481                                $(this)[0].reset();
2482                            });
2483                        } else {
2484                            // let alreadyInCartText = (wcop_ajax.i18n && wcop_ajax.i18n.already_in_cart) ? wcop_ajax.i18n.already_in_cart : 'Already in Cart';
2485                            // $('.wcop_sp_domain_suggestion_action').each(function() {
2486                            //     let actionContainer = $(this);
2487                            //     let actionForm = actionContainer.find('form');
2488                            //     let actionDomain = String(actionForm.find('input[name="domain"]').val() || '');
2489                            //     let actionType = String(actionForm.find('input[name="domaintype"]').val() || '');
2490                            //     if (actionDomain.toLowerCase() === String(domain || '').toLowerCase() && actionType === String(domaintype || '')) {
2491                            //         actionContainer.html('<button type="button" class="whcom_button wcop_suggestion_disabled_btn" disabled><strong>' + window.wcop_escape_html(alreadyInCartText) + '</strong></button>');
2492                            //     }
2493                            // });
2494                        }
2495                        
2496                        jQuery('.whcom_empty_summary_section_heading').css('display', 'none');
2497                        jQuery('.whcom_filled_summary_section_heading').css("display", "block");
2498                        
2499                        window.wcop_apply_summary_html(prd_form, res.summary_html || null);
2500                        
2501                    } else {
2502                        if (res.scroll_to_product) {
2503                            window.wcop_scroll_to_product_section();
2504                        }
2505                        whcom_show_notification(res.message, 'whcom_alert whcom_alert_danger');
2506                    }
2507                },
2508                error: function() {
2509                    let addToCartText = (wcop_ajax.i18n && wcop_ajax.i18n.add_to_cart) ? wcop_ajax.i18n.add_to_cart : 'Add to Cart';
2510                    let errorText = (wcop_ajax.i18n && wcop_ajax.i18n.error_occurred) ? wcop_ajax.i18n.error_occurred : 'An error occurred. Please try again.';
2511                    $('.wcop_multi_domain_add_btn').html('<i class="whcom_icon_basket"></i> ' + addToCartText).prop('disabled', false);
2512                    whcom_show_notification(errorText, 'whcom_alert whcom_alert_danger');
2513                }
2514            });
2515        };
2516
2517        window.wcop_remove_domain_from_cart = function(cart_item_id) {
2518            let prd_form = $('form.wcop_sp_add_product');
2519            if (!prd_form.length) return;
2520            
2521            let wcop_sp_template = prd_form.find('[name="wcop_sp_template"]').val();
2522
2523            let currentState = window.wcop_get_multi_domain_cart_state(prd_form, cart_item_id);
2524            window.wcop_sync_multi_domain_cart_state(prd_form, currentState);
2525
2526            let data = prd_form.serializeArray();
2527            data.push({ name: 'action', value: 'wcop_sp_process' });
2528            data.push({ name: 'wcop_sp_what', value: 'remove_domain_from_cart' });
2529            data.push({ name: 'cart_item_id', value: cart_item_id });
2530            data.push({ name: 'wcop_sp_template', value: wcop_sp_template || '' });
2531            data.push({ name: 'currency', value: getUrlParameter('currency') || '' });
2532            
2533            let removeBtn = $('.wcop_multi_domain_remove[data-cart-item-id="' + cart_item_id + '"]');
2534            let cartItem = removeBtn.closest('.wcop_multi_domain_cart_item');
2535            removeBtn.html(whcom_spinner_icon).prop('disabled', true);
2536            $('.wcop_sp_replace_spinner').html(whcom_spinner_icon_only);
2537            $.ajax({
2538                url: wcop_ajax.ajax_url,
2539                type: 'post',
2540                data: data,
2541                success: function(response) {
2542                    let res = JSON.parse(response);
2543                    
2544                    if (res.status === 'OK') {
2545                        cartItem.slideUp(400, function() {
2546                            $(this).remove();
2547                            let domain_cart_container = $('#wcop_multi_domain_cart_container');
2548                            if (domain_cart_container.find('.wcop_multi_domain_cart_item').length === 0) {
2549                                domain_cart_container.slideUp();
2550                            }
2551                        });
2552
2553                        window.wcop_apply_multi_domain_response(prd_form, res);
2554                        
2555                        whcom_show_notification(res.message, 'whcom_alert whcom_alert_success');
2556
2557                        window.wcop_apply_summary_html(prd_form, res.summary_html || null);
2558                    } else {
2559                        let removeText = (wcop_ajax.i18n && wcop_ajax.i18n.remove) ? wcop_ajax.i18n.remove : 'Remove';
2560                        removeBtn.html('<i class="whcom_icon_trash"></i> ' + removeText).prop('disabled', false);
2561                        whcom_show_notification(res.message, 'whcom_alert whcom_alert_danger');
2562                    }
2563                },
2564                error: function() {
2565                    let removeText = (wcop_ajax.i18n && wcop_ajax.i18n.remove) ? wcop_ajax.i18n.remove : 'Remove';
2566                    let errorText = (wcop_ajax.i18n && wcop_ajax.i18n.error_occurred) ? wcop_ajax.i18n.error_occurred : 'An error occurred. Please try again.';
2567                    removeBtn.html('<i class="whcom_icon_trash"></i> ' + removeText).prop('disabled', false);
2568                    whcom_show_notification(errorText, 'whcom_alert whcom_alert_danger');
2569                }
2570            });
2571        };
2572
2573        window.wcop_update_domain_cart_item = function(cart_item_id, field, value) {
2574            let prd_form = $('form.wcop_sp_add_product');
2575            if (!prd_form.length) return;
2576            
2577            let wcop_sp_template = prd_form.find('[name="wcop_sp_template"]').val();
2578
2579            let currentState = window.wcop_get_multi_domain_cart_state(prd_form);
2580            if (!currentState[cart_item_id]) {
2581                currentState[cart_item_id] = {};
2582            }
2583            if (field === 'addons' || field === 'domainfields') {
2584                currentState[cart_item_id][field] = Array.isArray(value) ? value : [];
2585            } else {
2586                currentState[cart_item_id][field] = value;
2587            }
2588            window.wcop_sync_multi_domain_cart_state(prd_form, currentState);
2589
2590            let data = prd_form.serializeArray();
2591            data.push({ name: 'action', value: 'wcop_sp_process' });
2592            data.push({ name: 'wcop_sp_what', value: 'update_domain_cart_item' });
2593            data.push({ name: 'cart_item_id', value: cart_item_id });
2594            data.push({ name: 'wcop_sp_template', value: wcop_sp_template || '' });
2595            data.push({ name: 'currency', value: getUrlParameter('currency') || '' });
2596            $('.wcop_sp_replace_spinner').html(whcom_spinner_icon_only);
2597            $.ajax({
2598                url: wcop_ajax.ajax_url,
2599                type: 'post',
2600                data: data,
2601                success: function(response) {
2602                    let res = JSON.parse(response);
2603                    
2604                    if (res.status === 'OK') {
2605                        window.wcop_apply_multi_domain_response(prd_form, res);
2606                        window.wcop_apply_summary_html(prd_form, res.summary_html || null);
2607                    }
2608                }
2609            });
2610        };
2611
2612        $(document).on('click', '.wcop_multi_domain_remove', function(e) {
2613            e.preventDefault();
2614            let cart_item_id = $(this).data('cart-item-id');
2615            wcop_remove_domain_from_cart(cart_item_id);
2616        });
2617
2618        $(document).on('change', '.wcop_multi_domain_regperiod_select', function() {
2619            let cart_item_id = $(this).data('cart-item-id');
2620            let regperiod = $(this).val();
2621            wcop_update_domain_cart_item(cart_item_id, 'regperiod', regperiod);
2622        });
2623
2624        $(document).on('change', '.wcop_multi_domain_addon_checkbox', function() {
2625            let cart_item_id = $(this).data('cart-item-id');
2626            let cartItem = $(this).closest('.wcop_multi_domain_cart_item');
2627            let addons = [];
2628            cartItem.find('.wcop_multi_domain_addon_checkbox:checked').each(function() {
2629                addons.push($(this).val());
2630            });
2631            wcop_update_domain_cart_item(cart_item_id, 'addons', addons);
2632        });
2633
2634        $(document).on('change', '.wcop_multi_domain_eppcode_input', function() {
2635            let cart_item_id = $(this).data('cart-item-id');
2636            let eppcode = $(this).val();
2637            wcop_update_domain_cart_item(cart_item_id, 'eppcode', eppcode);
2638        });
2639
2640        $(document).on('change', '#wcop_multi_domain_attached_domain_select', function() {
2641            let prd_form = $('form.wcop_sp_add_product');
2642            if (!prd_form.length) return;
2643
2644            let selectedId = String($(this).val() || '');
2645            let state = window.wcop_get_multi_domain_cart_state(prd_form);
2646            let selectedDomain = state[selectedId] || null;
2647            let productRequiresDomain = window.wcop_product_requires_domain(prd_form);
2648            window.wcop_sync_attached_domain_fields(prd_form, selectedId, selectedDomain, productRequiresDomain);
2649            wcop_sp_update_product_summary();
2650        });
2651
2652        $(document).on('change', '.wcop_multi_domain_domain_fields input, .wcop_multi_domain_domain_fields select, .wcop_multi_domain_domain_fields textarea', function() {
2653            let cartItem = $(this).closest('.wcop_multi_domain_cart_item');
2654            let cart_item_id = cartItem.data('cart-item-id');
2655            if (!cart_item_id) {
2656                return;
2657            }
2658            let domainfields = window.wcop_collect_domainfields_from_item(cartItem);
2659            wcop_update_domain_cart_item(cart_item_id, 'domainfields', domainfields);
2660        });
2661
2662        $(document).on('click', '.wcop_multi_domain_add_btn', function(e) {
2663            e.preventDefault();
2664            let btn = $(this);
2665            let form = btn.closest('.wcop_sp_attach_product_domain, .wcop_sp_domain_attachment_form');
2666            
2667            let domain = form.find('input[name="domain"]').val();
2668            let domaintype = form.find('input[name="domaintype"]').val();
2669            let tld = form.find('input[name="tld"]').val() || form.find('select[name="ext"]').val();
2670            let regperiod = form.find('select[name="regperiod"]').val() || 1;
2671            let eppcode = form.find('input[name="eppcode"]').val() || '';
2672            let keepDomainResponse = String(btn.data('keep-domain-response') || '') === 'yes';
2673            
2674            if (!domain || !domaintype || !tld) {
2675                let completeText = (wcop_ajax.i18n && wcop_ajax.i18n.complete_domain_search) ? wcop_ajax.i18n.complete_domain_search : 'Please complete domain search first.';
2676                whcom_show_notification(completeText, 'whcom_alert whcom_alert_danger');
2677                return;
2678            }
2679            
2680            wcop_add_domain_to_cart(domain, domaintype, tld, regperiod, ep
2680pcode, keepDomainResponse);
2681        });
2682
2683        $(document).on('change', 'select[id^="wcop_sp_product_select"], input[name="pid"]', function() {
2684            let prd_form = $('form.wcop_sp_add_product');
2685            if (!prd_form.length) return;
2686
2687            let state = window.wcop_get_multi_domain_cart_state(prd_form);
2688            let hasSelectedProduct = window.wcop_get_selected_product_id(prd_form) > 0;
2689            let prunedExistingDomain = false;
2690
2691            if (!hasSelectedProduct) {
2692                Object.keys(state || {}).forEach(function(key) {
2693                    let item = state[key] || {};
2694                    if (String(item.domaintype || '').toLowerCase() === 'existing') {
2695                        delete state[key];
2696                        prunedExistingDomain = true;
2697                    }
2698                });
2699
2700                $('#wcop_multi_domain_cart_container .wcop_multi_domain_cart_item').each(function() {
2701                    let row = $(this);
2702                    let rowDomainType = String(row.find('input[name$="[domaintype]"]').val() || row.data('domain-type') || '').toLowerCase();
2703                    if (rowDomainType === 'existing') {
2704                        row.remove();
2705                    }
2706                });
2707
2708                window.wcop_sync_multi_domain_cart_state(prd_form, state);
2709            }
2710
2711            let stateKeys = Object.keys(state);
2712            if (!stateKeys.length) {
2713                window.wcop_sync_attached_domain_fields(prd_form, '', null, false);
2714                $('#wcop_multi_domain_shared_config_container').remove();
2715                if (prunedExistingDomain) {
2716                    window.wcop_refresh_multi_domain_server_state(prd_form);
2717                }
2718                return;
2719            }
2720
2721            if (!hasSelectedProduct) {
2722                window.wcop_sync_attached_domain_fields(prd_form, '', null, false);
2723                $('#wcop_multi_domain_shared_config_container').remove();
2724                window.wcop_refresh_multi_domain_server_state(prd_form);
2725                return;
2726            }
2727
2728            let selectedId = prd_form.find('input[name="wcop_attached_domain_id"]').val() || stateKeys[0];
2729            if (!state[selectedId]) {
2730                selectedId = stateKeys[0];
2731            }
2732            let selectedDomain = state[selectedId] || null;
2733            let productRequiresDomain = window.wcop_product_requires_domain(prd_form);
2734            window.wcop_sync_attached_domain_fields(prd_form, selectedId, selectedDomain, productRequiresDomain);
2735
2736            let selector = $('#wcop_multi_domain_attached_domain_select');
2737            if (selector.length) {
2738                if (productRequiresDomain && stateKeys.length > 1) {
2739                    selector.closest('.wcop_multi_domain_attached_selector').show();
2740                    selector.val(selectedId);
2741                } else {
2742                    selector.closest('.wcop_multi_domain_attached_selector').hide();
2743                }
2744            }
2745
2746            window.wcop_refresh_multi_domain_server_state(prd_form);
2747        });
2748
2749        $(document).on('click', '#edit_domain, .edit_inline_domain', function(e) {
2750            let prd_form = $(this).closest('form.wcop_sp_add_product');
2751            if (!prd_form.length || window.wcop_multi_domain_enabled) {
2752                return;
2753            }
2754
2755            e.preventDefault();
2756            let domainConfig = prd_form.find('#wcop_sp_domain_config');
2757            domainConfig.find('.wcop_sp_section_content').empty();
2758            domainConfig.hide();
2759            prd_form.find('#wcop_sp_choose_a_domain').show();
2760            window.wcop_sync_attached_domain_fields(prd_form, '', null, false);
2761            wcop_sp_update_product_summary();
2762        });
2763
2764        $(document).on('ready', function() {
2765            let prd_form = $('form.wcop_sp_add_product');
2766            if (!prd_form.length) return;
2767
2768            window.wcop_multi_domain_enabled = $('.wcop_multi_domain_add_btn').length > 0 || $('#wcop_multi_domain_cart_container').length > 0;
2769
2770            let state = window.wcop_get_multi_domain_cart_state(prd_form);
2771            if (Object.keys(state).length) {
2772                window.wcop_apply_multi_domain_response(prd_form, {
2773                    domain_cart: state,
2774                    multi_domain_shared_config_html: ''
2775                });
2776                window.wcop_refresh_multi_domain_server_state(prd_form);
2777            }
2778        });
2779
2780    }(jQuery)
2781);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.