var ASLyricContestCheckout = ASLyricContestCheckout || {}; ASLyricContestCheckout = { Fields: { // Each group corresponds to a set of fields associated with an individual entry // the groups are numbered according to the submission number. So for example, // if a user was submitting four entries, group four would corresponds to the // fields associated with the fourth entry. // // The group objects contain a list of all fields that are associated with an // entry, as well as the required fields. This data is then referenced by // various functions. Groups: { 1: { inputs: jQuery('#billing_song_title_field, #billing_enter_your_lyrics_here_field, #billing_co-writers_field'), required_inputs: jQuery('#billing_song_title_field, #billing_enter_your_lyrics_here_field') }, 2: { inputs: jQuery('#billing_second_entry_song_title_field, #billing_entry_2_enter_your_lyrics_here_field, #billing_entry_2_co-writers_field'), required_inputs: jQuery('#billing_second_entry_song_title_field, #billing_entry_2_enter_your_lyrics_here_field') }, 3: { inputs: jQuery('#billing_third_entry_song_title_field, #billing_entry_3_enter_your_lyrics_here_field, #billing_entry_3_co-writers_field'), required_inputs: jQuery('#billing_third_entry_song_title_field, #billing_entry_3_enter_your_lyrics_here_field') }, 4: { inputs: jQuery('#billing_fourth_entry_song_title_field, #billing_entry_4_enter_your_lyrics_here_field, #billing_entry_4_co-writers_field'), required_inputs: jQuery('#billing_fourth_entry_song_title_field, #billing_entry_4_enter_your_lyrics_here_field') }, 5: { inputs: jQuery('#billing_entry_5_song_title_field, #billing_entry_5_enter_your_lyrics_here_field, #billing_entry_5_co-writers_field'), required_inputs: jQuery('#billing_entry_5_song_title_field, #billing_entry_5_enter_your_lyrics_here_field') }, 'address': { inputs: jQuery('.wcf-embed-checkout-form #billing_address_1_field, .wcf-embed-checkout-form #billing_address_2_field, .wcf-embed-checkout-form #billing_city_field, .wcf-embed-checkout-form #billing_state_field, .wcf-embed-checkout-form #billing_postcode_field, .wcf-embed-checkout-form #billing_country_field, .wcf-embed-checkout-form #account_password_field'), required_inputs: jQuery('.wcf-embed-checkout-form #billing_address_1_field, .wcf-embed-checkout-form #billing_city_field, .wcf-embed-checkout-form #billing_state_field, .wcf-embed-checkout-form #billing_postcode_field, .wcf-embed-checkout-form #billing_country_field, .wcf-embed-checkout-form #account_password_field') } }, // Display inputs for a specific group Show: function(group){ ASLyricContestCheckout.Fields.Groups[group].inputs.removeClass('wcf-hide-field'); if(typeof ASLyricContestCheckout.Fields.Groups[group].required_inputs !== 'undefined'){ ASLyricContestCheckout.Fields.Groups[group].required_inputs.each(function(){ ASLyricContestCheckout.Fields.MakeRequired(jQuery(this)); }); } }, // Hide inputs for a specific group Hide: function(group){ ASLyricContestCheckout.Fields.Groups[group].inputs.addClass('wcf-hide-field'); if(typeof ASLyricContestCheckout.Fields.Groups[group].required_inputs !== 'undefined'){ ASLyricContestCheckout.Fields.Groups[group].required_inputs.each(function(){ ASLyricContestCheckout.Fields.MakeOptional(jQuery(this)); }); } }, // Delete inputs data for a specific group. Truncate: function(group){ ASLyricContestCheckout.Fields.Groups[group].inputs.each(function(){ jQuery(this).find('.input-text').val(''); // Use class so that we can trigger both textarea and input field. }); }, MakeRequired: function(field){ field.addClass('validate-required'); field.find('label .optional').remove(); if(field.find('label .required').length < 1){ field.find('label').append('*'); } }, MakeOptional: function(field){ field.removeClass('validate-required'); field.find('.field-required').removeClass('field-required'); field.find('label .required').remove(); if(field.find('label .optional').length < 1){ field.find('label').append('(optional)'); } } }, // Adjust group field display based on the current selection UpdateForm: function(params){ // Make sure the parameter was passed before attempting to interact with it, and that can be cast to an integer if(typeof params.number_of_entries !== 'undefined'){ // Make sure that we only run this code for numeric group IDs (i.e. exclude the address fields) if(!isNaN(params.number_of_entries)){ // Cast the parameter to an integer var entry_count = parseInt(params.number_of_entries); // Loop through field groups Object.keys(ASLyricContestCheckout.Fields.Groups).forEach(function(value, index){ if(!isNaN(value)){ // Show any groups that are at or below the number of submissions selected if(parseInt(value) <= entry_count){ ASLyricContestCheckout.Fields.Show(value); } else { // Hide any groups that are greater than the number of submissions selected ASLyricContestCheckout.Fields.Hide(value); // Delete data for any groups that are greater than the number of submissions selected. ASLyricContestCheckout.Fields.Truncate(value); } } }); } else { // This should never happen, but if it does, we at least let the user know // that something went wrong, and that they should contact support if the // problem persists. The JS alert is a crude method, but works in a pinch alert('Unable to update the form, please try refreshing the page. If this issue persists, please contact us for assistance.'); } } }, // When the user selects how many submissions they are submitting, the selected // input should be passed to this function. This will glean the necessary data // from the DOM and trigger the function that actually updates the form HandleQuantitySelection: function(selected_option_input){ var option_data = JSON.parse(selected_option_input.parents('.wcf-qty-row').attr('data-options')); var quantity = parseInt(option_data.quantity); ASLyricContestCheckout.UpdateForm({ option_data: option_data, number_of_entries: quantity }); }, ToggleCreateAccountCheckbox: function(){ // Update the "Create Account" checkbox (which should be hidden) field // value if the user has opted to purchase the membership order bump if(jQuery('#wcf-bump-order-cb').length > 0 && jQuery('#wcf-bump-order-cb').is(':checked')){ jQuery('#createaccount').prop('checked', true).trigger('change'); } else { jQuery('#createaccount').prop('checked', false).trigger('change'); } }, ToggleAddressFields: function(){ // Show or hide the address fields depending on whether or not the // create account checkbox is selected // value if the user has opted to purchase the membership order bump if(jQuery('.wcf-checkout-fields-wrapper .woocommerce-account-fields .form-row.create-account').length > 0 && jQuery('#wcf-bump-order-cb').length > 0 && jQuery('#wcf-bump-order-cb').is(':checked')){ ASLyricContestCheckout.Fields.Show('address'); } else { ASLyricContestCheckout.Fields.Hide('address'); } }, ToggleMembershipOffer: function(){ var exclude_from = [ 'logged-in', 'member-logged-in' ]; var should_hide_offer = false; exclude_from.forEach(function(value, index){ if(jQuery('body').hasClass(value)){ should_hide_offer = true; } }); if(should_hide_offer){ jQuery('.wcf-bump-order-wrap').remove(); } }, Initialize: function(){ // Loop through our groups and their inputs and update the DOM as necessary Object.keys(ASLyricContestCheckout.Fields.Groups).forEach(function(value, index){ if(!isNaN(value)){ // Hide the links that would normally be used to show the hidden fields ASLyricContestCheckout.Fields.Groups[value].inputs.find('> a').hide(); // Add CSS classes to the input wrappers for a nicer UI ASLyricContestCheckout.Fields.Groups[value].inputs.each(function(index){ // Applies CSS classes to the first field wrapper in the group if(index == 0){ jQuery(this).addClass('border-top extra-top-padding'); } // Applies CSS classes to any field wrapper jQuery(this).addClass('border-sides entry-group-field contest-submission-field'); // Applies CSS classes to the last field wrapper in the group if(index == (ASLyricContestCheckout.Fields.Groups[value].inputs.length - 1)){ jQuery(this).addClass('border-bottom extra-lower-padding separated'); } }); } else { switch(value){ case 'address': ASLyricContestCheckout.Fields.Groups[value].inputs.each(function(index){ jQuery(this).addClass('entry-group-field address-field'); if(index < 2){ jQuery(this).addClass('border-top extra-top-padding no-border-top-mobile'); } if(index == 0 || index == 2){ jQuery(this).addClass('border-left'); } if(index == 1 || index == 4){ jQuery(this).addClass('border-right'); } if(index == 5){ jQuery(this).addClass('border-sides'); } // Applies CSS classes to the last field wrapper in the group if(index == (ASLyricContestCheckout.Fields.Groups[value].inputs.length - 1)){ jQuery(this).addClass('border-bottom border-sides extra-lower-padding separated-mobile'); } }); break; } } }); // Check if our custom style block already exists, and if not, add it to the if(jQuery('#custom-contest-checkout-styles').length < 1){ // This style block contains some custom CSS. Since everything relies on JS to work and // we don't want to make any adjustments unless the JS is applied, it makes sense to simply // add it here instead of including a separate CSS file. If it were a lot of CSS, we could // format it differently or move it into a separate file that we could then link to from here jQuery('head').append(''); } // Check to see if the option to create an account is present if(jQuery('.wcf-checkout-fields-wrapper .woocommerce-account-fields .form-row.create-account').length > 0){ // If users can create an account, then we hide the checkbox from view jQuery('.wcf-checkout-fields-wrapper .woocommerce-account-fields .form-row.create-account').addClass('wcf-hide-field'); // Update the value of the hidden checkbox field based on whether or not the // user has opted to purchase the membership through the order bump feature ASLyricContestCheckout.ToggleCreateAccountCheckbox(); jQuery(document).on('change', '#wcf-bump-order-cb', function(){ ASLyricContestCheckout.ToggleCreateAccountCheckbox(); ASLyricContestCheckout.ToggleAddressFields(); }); // Check to see if the element exists before modifying it if(jQuery('#account_password_field').length > 0){ jQuery('#account_password_field').find('label').html('Set Membership Account Password *'); } } // Show or hide the address fields based on whether the create account checkbox // exists on the page and whether or not they have opted for the order bump ASLyricContestCheckout.ToggleAddressFields(); // When the page loads, update the checkout form based on the pre-selected option if(jQuery('.wcf-qty-options .wcf-single-sel:checked').length > 0){ ASLyricContestCheckout.HandleQuantitySelection(jQuery('.wcf-qty-options .wcf-single-sel:checked')); } // When the user selects an option, update the form jQuery(document).on('change', '.wcf-qty-row .wcf-item-selector input.wcf-single-sel', function(e){ ASLyricContestCheckout.HandleQuantitySelection(jQuery(this)); }); if( jQuery('.wcf-bump-order-field-wrap input').length && jQuery('.wcf-bump-order-field-wrap input').is( ":checked")) // bydefault uncheck order bumb { setTimeout(() => { jQuery('.wcf-bump-order-field-wrap input').click(); console.log('order-bump state'); jQuery(".wcf-bump-order-desc").html(function(_, html) { return html.replace("40% off", "40% off"); }); }, 2000); } jQuery(".wcf-bump-order-desc").html(function(_, html) { return html.replace("40% off", "40% off"); }); setTimeout(() => { jQuery('.woocommerce-checkout-review-order-table').before(jQuery('#billing_country_field')); jQuery('#billing_country_field').after(jQuery('#billing_address_1_field')); jQuery('#billing_address_1_field').after(jQuery('#billing_address_2_field')); jQuery('#billing_address_2_field').after(jQuery('#billing_state_field')); jQuery('#billing_state_field').after(jQuery('#billing_city_field')); jQuery('#billing_city_field').after(jQuery('#billing_postcode_field')); jQuery("#billing_country_field, #billing_state_field, #billing_postcode_field, #billing_address_2_field, #billing_address_1_field, #billing_city_field").css({"border-left": "none","border-right": "none"}); jQuery('#billing_country_field label span, #billing_address_1_field label span, #billing_city_field label span, #billing_state_field label span, #billing_postcode_field label span').removeClass('optional'); jQuery('#billing_country_field label span, #billing_address_1_field label span, #billing_city_field label span, #billing_state_field label span, #billing_postcode_field label span').addClass('required'); jQuery('#billing_country_field label span, #billing_address_1_field label span, #billing_city_field label span, #billing_state_field label span, #billing_postcode_field label span').text('*'); }, 2000); setInterval(() => { jQuery('#billing_country_field').removeClass('wcf-hide-field'); jQuery('#billing_address_1_field').removeClass('wcf-hide-field'); jQuery('#billing_address_2_field').removeClass('wcf-hide-field'); jQuery('#billing_city_field').removeClass('wcf-hide-field'); jQuery('#billing_state_field').removeClass('wcf-hide-field'); jQuery('#billing_postcode_field').removeClass('wcf-hide-field'); }, 1000); } }; jQuery(document).ready(function(){ // Initialize our custom checkout code ASLyricContestCheckout.Initialize(); }); // If the user is logged in and has a membership already, then we should hide // the membership upsell. We are not waiting on the document to be ready, or // else the users will be able to see the block while the page loads (since // it is being hidden via JS). ASLyricContestCheckout.ToggleMembershipOffer(); ; jQuery(document).ready(function() { AswSongQuantityMaximum = function() { return 5; }; AswGetSelectedSongQuantity = function() { try { var selectedSongs = jQuery('div.wcf-qty-row input.wcf-single-sel[type="radio"]:checked'); var selectedQuantity = selectedSongs.closest('div.wcf-qty-row').data().options.quantity; return selectedQuantity !== undefined ? selectedQuantity : 1; } catch (e) { // no op } }; AswSelectSongQuantityOption = function(index) { try { var productRadio = jQuery('div.wcf-qty-row input.wcf-single-sel[type="radio"]'); if (productRadio !== undefined) { productRadio[index].click(); } } catch (e) { // no op } }; AswToggleAddRemoveButtons = function() { try { var selectedSongQuantity = AswGetSelectedSongQuantity(); if (selectedSongQuantity < AswSongQuantityMaximum()) { jQuery('a.asw-button-add-song').show(); } else { jQuery('a.asw-button-add-song').hide(); } if (selectedSongQuantity > 1) { jQuery('a.asw-button-remove-song').show(); } else { jQuery('a.asw-button-remove-song').hide(); } } catch (e) { // no op } }; AswAddAddRemoveButtons = function() { try { jQuery('div#customer_details') .append( '
' + 'Add Another Entry' + '  ' + 'Remove This Entry' + '
' ); AswToggleAddRemoveButtons(); jQuery('a.asw-button-add-song').on('click', function() { var selectedSongQuantity = AswGetSelectedSongQuantity(); //console.log('selectedSongQuantity=', selectedSongQuantity, 'max=', AswSongQuantityMaximum()); if (selectedSongQuantity >= 1 && selectedSongQuantity < AswSongQuantityMaximum()) { AswSelectSongQuantityOption(selectedSongQuantity); AswToggleAddRemoveButtons(); } }); jQuery('a.asw-button-remove-song').on('click', function() { var selectedSongQuantity = AswGetSelectedSongQuantity(); //console.log('selectedSongQuantity=', selectedSongQuantity); if (selectedSongQuantity >= 2) { AswSelectSongQuantityOption(selectedSongQuantity - 2); // -2 because we want to remove one, and index is 0 based AswToggleAddRemoveButtons(); } }); } catch (e) { // no op } // Hide the number of songs options from the user jQuery('div.wcf-product-option-wrap').hide(); }; AswAddAddRemoveButtons(); });; /** * Select the order bump from the checkout page if it's not already selected. */ const checkOrderBump = { /** * Initialize. * * @return {void} */ init() { this.orderBumpCheckbox = document.getElementById('wcf-bump-order-cb'); this.checkOrderBump(); }, /** * Check the order bump and select. * * @return {void} */ checkOrderBump() { if (this.orderBumpCheckbox) { if (false === this.orderBumpCheckbox.checked) { this.orderBumpCheckbox.click(); } } }, }; jQuery(document).ready(function () { checkOrderBump.init(); }); ;