// BEGIN custom javascript
jQuery(document).ready(function(){
  
    // BEGIN Donation amount text

	// Function to change donation amount text
	var changetext = function()
	{
		if(Drupal.wateraidDonationForms.model.get("frequency") == "recurring") {
			jQuery('.donation-amount-info').text("You are making a monthly donation of £" + Drupal.wateraidDonationForms.model.get("amount") + ".");
		} else {
			jQuery('.donation-amount-info').text("You are making a one-off donation of £" + Drupal.wateraidDonationForms.model.get("amount") + ".");
		}
	}
	
	// Call function on page load
	changetext();
	
	// Call function on 'Donate' button click on step 1
	jQuery("#edit-next").click(function() {
		changetext();
	});

	// Call function when user types other amount
	jQuery("#edit-donation-amount-amount-one-off-amounts-other").keyup(function() {
		changetext();
	});

	// Clear donation amount text on back button click, to ensure incorrect text is not displayed on step 1
	jQuery("span, input.webform-button--previous").click(function() {
	   jQuery('.donation-amount-info').text("");
	});
	
	// If user is on step 1...
	if(jQuery('#edit-step-1').is(':visible')) {		
        // Hide donation amount text
		jQuery('.progress-bar__donation-amount-info').addClass('hidden');
        // Insert link to 'Pay in your fundraising' form
        jQuery('#edit-actions-wizard-next').after('<a href="/uk/pay-in-your-fundraising" class="pay-in-your-fundraising-link wa-blue form-item">I would like to pay in money raised from a collection or fundraiser</a>');
	}
	
	// END Donation amount text

	// BEGIN Security icons	  
    var paymentMethodsOneOff = jQuery('.payment-methods--one-off');
	var paymentMethodsMonthly = jQuery('.payment-methods--monthly');
	var showOneOff = function() {
		paymentMethodsOneOff.removeClass('payment-methods--hidden');
		paymentMethodsMonthly.addClass('payment-methods--hidden');
	}
	var showMonthly= function() {
		paymentMethodsOneOff.addClass('payment-methods--hidden');
		paymentMethodsMonthly.removeClass('payment-methods--hidden');
	}
    
    // On page load, if frequency is one off, hide direct debit logo
	if (Drupal.wateraidDonationForms.model.get('frequency') == 'one_off') {
		showOneOff();
	} else {
		showMonthly();
	}

	// Function to handle toggling security icons
	var toggleSecurityIcons= function() {
		if (jQuery(this).attr('for') == 'edit-donation-amount-frequency-one-off') {
			showOneOff();
		}
		else {
			showMonthly();
		}
	}
	// Call function on click of one off / monthly buttons
	jQuery('#edit-donation-amount-frequency label').click(toggleSecurityIcons);
  
  // END Security icons
  
  // Add label for Stripe field
  jQuery('#edit-payment-payment-methods-one-off-methods-stripe').prepend('<label for="stripe-token">Enter your card details</label>');
});