PageSourceSearch

https://winthervin.dk/wp-content/plugins/pw-gift-cards-disabled/assets/js/pw-gift-cards.js?ver=1.407

js winthervin.dk collected 2026-10-08 17:52:29 UTC 19,974 bytes, 512 lines download raw bytes

1function pwgc_init() {
2    var checkBalanceForm = jQuery('#pwgc-balance-form');
3    if ( checkBalanceForm.length ) {
4        checkBalanceForm.on('submit', function(e) {
5            pwgc_check_balance();
6
7            e.preventDefault();
8            return false;
9        });
10        pwgc_check_balance();
11    }
12
13    jQuery('#pwgc-balance-reload').off('click.pwgc').on('click.pwgc', function(e) {
14        var cardNumber = jQuery(this).attr('data-card-number');
15        var productUrl = jQuery(this).attr('data-url');
16        if (cardNumber) {
17            var separator = '?';
18            if (productUrl.includes('?')) {
19                separator = '&';
20            }
21
22            location.href = productUrl + separator + pwgc.reload_key + '=' + cardNumber;
23        }
24        e.preventDefault();
25        return false;
26    });
27
28    jQuery('#pwgc-manual-debit').off('click.pwgc').on('click.pwgc', function(e) {
29        jQuery(this).prop('disabled', true);
30        var errorMessage = jQuery('#pwgc-balance-error');
31        errorMessage.text('');
32
33        var cardNumber = jQuery(this).attr('data-card-number');
34        if (cardNumber) {
35            var amount = prompt( pwgc.i18n.debit_amount_prompt );
36            if (amount) {
37                var note = prompt( pwgc.i18n.debit_note_prompt );
38                if (note !== null) {
39                    var balance = jQuery('#pwgc-balance-amount');
40                    var balanceMessage = jQuery('#pwgc-balance-message');
41
42                    jQuery.post(pwgc.ajaxurl, {'action': 'pw-gift-cards-debit', 'card_number': cardNumber, 'amount': amount, 'note': note, 'security': pwgc.nonces.debit_balance}, function(result) {
43                        balance.html(result.balance);
44                        balanceMessage.html(result.message);
45                    }).fail(function(xhr, textStatus, errorThrown) {
46                        if (errorThrown) {
47                            errorMessage.text(errorThrown);
48                        } else {
49                            errorMessage.text('Unknown ajax error');
50                        }
51                    });
52                }
53            }
54        }
55        jQuery(this).prop('disabled', false);
56        e.preventDefault();
57        return false;
58    });
59
60    jQuery(document.body).on('updated_wc_div', pwgc_bind_redeem_form);
61    jQuery(document.body).on('updated_shipping_method', pwgc_bind_redeem_form);
62    pwgc_bind_redeem_form();
63
64    jQuery(document.body).on('updated_wc_div', pwgc_bind_remove_link);
65    jQuery(document.body).on('updated_checkout', pwgc_bind_remove_link);
66    jQuery(document.body).on('updated_shipping_method', pwgc_bind_remove_link);
67    pwgc_bind_remove_link();
68
69    jQuery('#pwgc-message').on('input propertychange', function() {
70        pwgc_message_characters_remaining();
71    });
72    pwgc_message_characters_remaining();
73
74    jQuery('form.variations_form').on('show_variation', function() {
75        jQuery('#pwgc-purchase-container').show();
76        pwgc_toggle_quantity();
77        pwgc_toggle_custom_amount_visibility();
78    });
79
80    jQuery('form.variations_form').on('hide_variation', function() {
81        jQuery('#pwgc-purchase-container').hide();
82        pwgc_toggle_quantity();
83        pwgc_toggle_custom_amount_visibility();
84    });
85
86    var attributeSelector = jQuery(document.getElementById(pwgc.denomination_attribute_slug));
87    if (attributeSelector.length == 0) {
88        attributeSelector = jQuery(document.getElementsByName('attribute_' + pwgc.denomination_attribute_slug));
89    }
90
91    if (attributeSelector.val()) {
92        jQuery('#pwgc-purchase-container').show();
93    } else {
94        jQuery('#pwgc-purchase-container').hide();
95    }
96
97    pwgc_toggle_custom_amount_visibility();
98
99    jQuery('#pwgc-custom-amount').on('blur', function() {
100        jQuery('#pwgc-custom-amount-error').text('');
101
102        var amount = jQuery(this).val();
103
104        amount = amount.replace(pwgc.thousand_separator, '');
105        amount = amount.replace(pwgc.decimal_separator, '.');
106        amount = amount.replace(/[^0-9.]/g,'');
107        if (amount) {
108            var floatAmount = parseFloat(amount);
109            var container = jQuery('#pwgc-purchase-container');
110            var minAmount = parseFloat(container.attr('data-min-amount'));
111            var maxAmount = parseFloat(container.attr('data-max-amount'));
112
113            if (floatAmount < minAmount) {
114                jQuery('#pwgc-custom-amount-error').html(pwgc.i18n.min_amount_error + minAmount.toFixed(pwgc.decimal_places).replace('.', pwgc.decimal_separator));
115            } else if (floatAmount > maxAmount) {
116                jQuery('#pwgc-custom-amount-error').html(pwgc.i18n.max_amount_error + maxAmount.toFixed(pwgc.decimal_places).replace('.', pwgc.decimal_separator));
117            } else {
118                jQuery(this).val(floatAmount.toFixed(pwgc.decimal_places).replace('.', pwgc.decimal_separator).replace(/\B(?=(\d{3})+(?!\d))/g, pwgc.thousand_separator));
119            }
120        } else {
121            jQuery('#pwgc-custom-amount-error').html(pwgc.i18n.custom_amount_required_error);
122        }
123    });
124
125    jQuery('#pwgc-to').on('blur', function() {
126        var recipients = jQuery(this).val();
127        if (recipients) {
128            // For clarity, ensure we do a comma followed by a space.
129            // Babel translation of this line:
130            // jQuery(this).val(recipients.trim().split(/[ ,]+/).map(item=>item.trim()).join(', '));
131
132            // This is compatible with IE11
133
134            recipients = recipients.trim().split(/[ ,]+/).map(function (item) {
135                return item.trim();
136            });
137
138            if (pwgc.allow_multiple_recipients != 'yes' && recipients.length > 1) {
139                recipients = [recipients[0]];
140            }
141
142            jQuery(this).val(recipients.join(', '));
143        }
144
145        pwgc_toggle_quantity();
146    });
147
148    jQuery('.variations_form').on( 'found_variation.wc-variation-form', function() {
149        pwgc_toggle_quantity();
150    });
151
152    jQuery('.variations_form').on( 'reset_data', function() {
153        pwgc_toggle_quantity();
154    });
155
156    jQuery('.variations_form').on( 'submit', function(e) {
157        if (jQuery('#pwgc-to').length) {
158            var recipients = jQuery('#pwgc-to').val().split(/[ ,]+/);
159            var badRecipients = [];
160
161            for (var i = 0; i < recipients.length; i++) {
162                if (!pwgc_is_email(recipients[i])) {
163                    badRecipients.push(recipients[i]);
164                }
165            }
166
167            if (badRecipients.length) {
168                alert(pwgc.i18n.invalid_recipient_error + '\n\n' + badRecipients.join('\n'));
169                e.preventDefault();
170                return false;
171            }
172        }
173    });
174
175    jQuery('.show-pw-gift-card').off('click.pwgc').on('click.pwgc', function(e) {
176        jQuery('.checkout_pw_gift_card').slideToggle(400, function() {
177            jQuery('.checkout_pw_gift_card').find(':input:eq(0)').focus();
178        });
179
180        e.preventDefault();
181        return false;
182    });
183
184    jQuery('#pwgc-apply-gift-card-checkout').off('click.pwgc').on('click.pwgc', function(e) {
185        pwgc_checkout_redeem_gift_card(jQuery(this));
186        e.preventDefault();
187        return false;
188    });
189
190    jQuery('#pwgc-redeem-gift-card-number').off('keypress.pwgc').on('keypress.pwgc', function(e) {
191        if (e.keyCode == 13) {
192            pwgc_checkout_redeem_gift_card(jQuery('#pwgc-apply-gift-card-checkout'));
193
194            e.preventDefault();
195            return false;
196        }
197    });
198
199    var deliveryDateField = document.getElementById('pwgc-delivery-date');
200    if (deliveryDateField) {
201        var today = new Date();
202        today.setDate(today.getDate());
203
204        var nextYear = new Date();
205        nextYear.setDate(nextYear.getDate() + 365);
206
207        var picker = new Pikaday({
208            field: deliveryDateField,
209            minDate: today,
210            maxDate: nextYear,
211            format: pwgc.pikaday.format,
212            firstDay: pwgc.pikaday.firstDay,
213            i18n: {
214                previousMonth : pwgc.i18n.previousMonth,
215                nextMonth     : pwgc.i18n.nextMonth,
216                months        : [pwgc.i18n.jan, pwgc.i18n.feb, pwgc.i18n.mar, pwgc.i18n.apr, pwgc.i18n.may, pwgc.i18n.jun, pwgc.i18n.jul, pwgc.i18n.aug, pwgc.i18n.sep, pwgc.i18n.oct, pwgc.i18n.nov, pwgc.i18n.dec],
217                weekdays      : [pwgc.i18n.sunday, pwgc.i18n.monday, pwgc.i18n.tuesday, pwgc.i18n.wednesday, pwgc.i18n.thursday, pwgc.i18n.friday, pwgc.i18n.saturday],
218                weekdaysShort : [pwgc.i18n.sun, pwgc.i18n.mon, pwgc.i18n.tue, pwgc.i18n.wed, pwgc.i18n.thu, pwgc.i18n.fri, pwgc.i18n.sat]
219            },
220        });
221    }
222
223    jQuery('#pwgc-email-preview-container').off('load.pwgc').on('load.pwgc', function() {
224        jQuery('#pwgc-email-preview-container').hide();
225
226        var iFrameBody = jQuery(this).contents().find('body');
227        if (iFrameBody.length) {
228            jQuery('#pwgc-email-preview-container').show();
229
230            // Set the iFrame to the height of the content, plus a little extra.
231            jQuery(this).css('height', ( 30 + iFrameBody.prop('scrollHeight') ) + "px" );
232
233            // Disable the Redeem button.
234            iFrameBody.find('#pwgc-email-redeem-button a').off('click.pwgc').on('click.pwgc', function(e) {
235                e.preventDefault();
236            });
237        }
238        jQuery('#pwgc-email-preview-button').prop( 'disabled', false );
239    });
240
241    jQuery('#pwgc-email-preview-button').off('click.pwgc').on('click.pwgc', function(e) {
242        pwgc_show_email_preview();
243        e.preventDefault();
244    });
245
246    jQuery('#pwgc-email-design-id').off('change.pwgc').on('change.pwgc', function(e) {
247       pwgc_show_email_preview();
248    });
249}
250
251function pwgc_show_email_preview() {
252    var forms = document.getElementsByClassName('variations_form');
253    if (forms.length > 0 && !forms[0].reportValidity()) {
254        return;
255    }
256
257    jQuery('#pwgc-email-preview-button').prop( 'disabled', true );
258    jQuery('#pwgc-email-preview-container').contents().find('body').html('...');
259    jQuery('#pwgc-email-preview-container').show();
260
261    var previewUrl = pwgc.preview_email_url;
262
263    amount = jQuery('#pwgc-custom-amount').val();
264    amount = amount.replace(pwgc.thousand_separator, '');
265    amount = amount.replace(pwgc.decimal_separator, '.');
266    amount = amount.replace(/[^0-9.]/g,'');
267
268
269    previewUrl += '&pwgc_number=' + pwgc.example_gift_card_number;
270    previewUrl += '&variation_id=' + jQuery('.variation_id').val();
271    previewUrl += '&design_id=' + jQuery('#pwgc-email-design-id').val();
272    previewUrl += '&to=' + encodeURIComponent(jQuery('#pwgc-to').val());
273    previewUrl += '&from=' + encodeURIComponent(jQuery('#pwgc-from').val());
274    previewUrl += '&message=' + encodeURIComponent(jQuery('#pwgc-message').val());
275    previewUrl += '&other_amount=' + encodeURIComponent(amount);
276    previewUrl += '&pdf=' + pwgc.preview_email_pdf;
277
278    if (jQuery('#pwgc-recipient-name').length) {
279        previewUrl += '&recipient_name=' + encodeURIComponent(jQuery('#pwgc-recipient-name').val());
280    }
281
282    jQuery('#pwgc-email-preview-container').attr('src', previewUrl);
283}
284
285function pwgc_pad(n, width, z) {
286    z = z || '0';
287    n = n + '';
288    return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
289}
290
291function pwgc_is_email(email) {
292    var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
293    return regex.test(email);
294}
295
296function pwgc_toggle_quantity() {
297    if (jQuery('#pwgc-to').length) {
298        var recipients = jQuery('#pwgc-to').val().split(/[ ,]+/);
299        if (recipients.length > 1) {
300            jQuery('#pwgc-recipient-count').text(recipients.length);
301            jQuery('#pwgc-quantity-one-per-recipient').show();
302            jQuery('input.qty').val('1');
303            jQuery('.quantity').hide();
304        } else {
305            jQuery('#pwgc-quantity-one-per-recipient').hide();
306            jQuery('.quantity').show();
307        }
308    }
309}
310
311function pwgc_toggle_custom_amount_visibility() {
312    var showOtherAmount = false;
313    var attributeSelector = jQuery(document.getElementById(pwgc.denomination_attribute_slug));
314    if (attributeSelector.length != 0) {
315        showOtherAmount = (attributeSelector.val() == pwgc.other_amount_prompt);
316    } else {
317        var selectedRadioButton = jQuery("input[type='radio'][name='attribute_" + pwgc.denomination_attribute_slug + "']:checked");
318        if (selectedRadioButton.length > 0) {
319            showOtherAmount = (selectedRadioButton.val() == pwgc.other_amount_prompt);
320        } else {
321            attributeSelector = jQuery(document.getElementsByName('attribute_' + pwgc.denomination_attribute_slug));
322            showOtherAmount = (attributeSelector.val() == pwgc.other_amount_prompt);
323        }
324    }
325
326    if (showOtherAmount) {
327        jQuery('#pwgc-custom-amount-form').show();
328        if (pwgc.auto_focus_other_amount) {
329            jQuery('#pwgc-custom-amount').focus();
330        }
331    } else {
332        jQuery('#pwgc-custom-amount').val('');
333        jQuery('#pwgc-custom-amount-form').hide();
334    }
335}
336
337function pwgc_check_balance() {
338    var cardNumber = jQuery('#pwgc-balance-number');
339    if (!cardNumber.val()) { return; }
340
341    jQuery('#pwgc-balance-error').text('');
342    jQuery('#pwgc-balance-activity,#pwgc-balance-expiration-date-container,#pwgc-balance-reload,#pwgc-manual-debit').hide();
343    jQuery('#pwgc-balance-amount').html(pwgc.balance_check_icon);
344
345    jQuery.post(pwgc.ajaxurl, {'action': 'pw-gift-cards-balance', 'card_number': cardNumber.val(), 'security': pwgc.nonces.check_balance}, function( result ) {
346        if (result.success) {
347
348            jQuery('#pwgc-balance-amount').html(result.data.balance);
349
350            if (result.data.expiration_date) {
351                jQuery('#pwgc-balance-expiration-date').html(result.data.expiration_date);
352                jQuery('#pwgc-balance-expiration-date-container').show();
353            }
354
355            jQuery('#pwgc-balance-activity').html(result.data.activity).show();
356            jQuery('#pwgc-balance-reload,#pwgc-manual-debit').attr('data-card-number', result.data.card_number).show();
357
358        } else {
359            jQuery('#pwgc-balance-error').text(result.data.message);
360            jQuery('#pwgc-balance-amount').text('');
361            jQuery('#pwgc-balance-activity,#pwgc-balance-expiration-date-container,#pwgc-balance-reload,#pwgc-manual-debit').hide();
362        }
363        cardNumber.focus();
364
365    }).fail(function(xhr, textStatus, errorThrown) {
366        if (errorThrown) {
367            jQuery('#pwgc-balance-error').text(errorThrown);
368        } else {
369            jQuery('#pwgc-balance-error').text('Unknown Error');
370        }
371        jQuery('#pwgc-balance-amount').text('');
372        jQuery('#pwgc-balance-reload,#pwgc-manual-debit').hide();
373        cardNumber.focus();
374    });
375}
376
377function pwgc_bind_remove_link() {
378    jQuery('.pwgc-remove-card').off('click.pwgc').on('click.pwgc', function(e) {
379        var cardNumber = jQuery(this).attr('data-card-number');
380        var checkoutContainer = jQuery(this).parents( '.woocommerce-checkout-review-order' );
381
382        if (checkoutContainer.length) {
383            checkoutContainer.addClass( 'processing' ).block({
384                message: null,
385                overlayCSS: {
386                    background: '#fff',
387                    opacity: 0.6
388                }
389            });
390        }
391
392        jQuery.post(pwgc.ajaxurl, {'action': 'pw-gift-cards-remove', 'card_number': cardNumber, 'security': pwgc.nonces.remove_card}, function( result ) {
393            if (checkoutContainer.length) {
394                jQuery( '.woocommerce-error, .woocommerce-message' ).remove();
395                checkoutContainer.removeClass( 'processing' ).unblock();
396                jQuery( document.body ).trigger( 'update_checkout', { update_shipping_method: false } );
397            } else {
398                jQuery( document.body ).trigger( 'wc_update_cart' );
399            }
400        }).fail(function(xhr, textStatus, errorThrown) {
401            if (errorThrown) {
402                alert(errorThrown);
403            } else {
404                alert('Unknown Error');
405            }
406        });
407
408        e.preventDefault();
409        return false;
410    });
411}
412
413function pwgc_bind_redeem_form() {
414    jQuery('#pwgc-redeem-form').off('submit.pwgc').on('submit.pwgc', function(e) {
415        var redeemButton = jQuery('#pwgc-redeem-button');
416
417        pwgc_redeem_gift_card(redeemButton);
418
419        e.preventDefault();
420        return false;
421    });
422
423    jQuery('#pwgc-apply-gift-card,#pwgc-redeem-button').off('click.pwgc').on('click.pwgc', function(e) {
424        pwgc_redeem_gift_card(jQuery(this));
425
426        e.preventDefault();
427        return false;
428    });
429
430    jQuery('#pwgc-redeem-gift-card-number').off('keypress.pwgc').on('keypress.pwgc', function(e) {
431        if (e.keyCode == 13) {
432            pwgc_redeem_gift_card(jQuery('#pwgc-apply-gift-card'));
433
434            e.preventDefault();
435            return false;
436        }
437    });
438}
439
440function pwgc_redeem_gift_card(redeemButton) {
441    var cardNumber = jQuery('#pwgc-redeem-gift-card-number');
442    var errorContainer = jQuery('#pwgc-redeem-error');
443
444    errorContainer.text('');
445    redeemButton.attr('data-apply-text', redeemButton.attr('value')).attr('value', redeemButton.attr('data-wait-text')).prop('disabled', true);
446
447    jQuery.post(pwgc.ajaxurl, {'action': 'pw-gift-cards-redeem', 'card_number': cardNumber.val(), 'security': pwgc.nonces.apply_gift_card}, function( result ) {
448        if (result.success) {
449            jQuery( document.body ).trigger( 'wc_update_cart' );
450        } else {
451            errorContainer.text(result.data.message);
452            redeemButton.attr('value', redeemButton.attr('data-apply-text')).prop('disabled', false);
453            cardNumber.focus();
454        }
455    }).fail(function(xhr, textStatus, errorThrown) {
456        if (errorThrown) {
457            errorContainer.text(errorThrown);
458        } else {
459            errorContainer.text('Unknown Error');
460        }
461        redeemButton.attr('value', redeemButton.attr('data-apply-text')).prop('disabled', false);
462        cardNumber.focus();
463    });
464}
465
466function pwgc_message_characters_remaining() {
467    var charsRemaining = pwgc.max_message_characters;
468
469    var messageElement = jQuery('#pwgc-message').val();
470    if (messageElement) {
471        charsRemaining -= messageElement.length;
472    }
473
474    jQuery('#pwgc-message-characters-remaining').text(charsRemaining);
475}
476
477function pwgc_checkout_redeem_gift_card(redeemButton) {
478    var errorContainer = jQuery('#pwgc-redeem-error');
479    var cardNumber = jQuery('#pwgc-redeem-gift-card-number');
480
481    errorContainer.text('');
482    redeemButton.attr('data-apply-text', redeemButton.attr('value')).attr('value', redeemButton.attr('data-wait-text')).prop('disabled', true);
483
484    jQuery.post(pwgc.ajaxurl, {'action': 'pw-gift-cards-redeem', 'card_number': cardNumber.val(), 'security': pwgc.nonces.apply_gift_card}, function( result ) {
485        var form = jQuery('.checkout_pw_gift_card');
486        if (result.success) {
487            form.slideUp();
488            cardNumber.val('');
489            jQuery( document.body ).trigger( 'update_checkout', { update_shipping_method: false } );
490        } else {
491            errorContainer.text(result.data.message);
492            cardNumber.focus();
493        }
494        redeemButton.attr('value', redeemButton.attr('data-apply-text')).prop('disabled', false);
495    }).fail(function(xhr, textStatus, errorThrown) {
496        if (errorThrown) {
497            errorContainer.text(errorThrown);
498        } else {
499            errorContainer.text('Unknown Error');
500        }
501        redeemButton.attr('value', redeemButton.attr('data-apply-text')).prop('disabled', false);
502        cardNumber.focus();
503    });
504}
505
506if ( document.readyState !== 'loading' ) {
507    pwgc_init();
508} else {
509    document.addEventListener('DOMContentLoaded', function () {
510        pwgc_init();
511    });
512}

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.