addLoadEvent(function () {
	confirmWishlistDeletion();
	validateWishlistsForm();
});

function confirmWishlistDeletion() {
	var yourWishlistsList = $('wishlists_yourWishlistsList');
	if (!yourWishlistsList) return;
	
	var yourWishlistsSpans = yourWishlistsList.getElementsByTagName('span');

	// Loop through spans within the Your Wishslists lists
	for (var i = 0; i < yourWishlistsSpans.length; i++) {
		
		// If they contain an <a>
		var deleteWishlistLink = yourWishlistsSpans[i].getElementsByTagName('a')[0];
		if (!deleteWishlistLink) return

		// When said <a> is clicked
		deleteWishlistLink.onclick = function () {
			wishlistId = this.getAttribute('rel');
			wishlistName = $('wishlists_wishlist' + wishlistId + 'Link').innerHTML;

			// If the user confirms they want to delete the wishlist
			if (confirm('Are you sure you want to delete the "' + wishlistName + '" wishlist?')) {

				// Store the ID of the wishlist to be deleted in a hidden field and submit the form
				$('hiddenDeleteWishlist').value = wishlistId;
				$('wishlists_form').submit();
			}
			return false;
		}
	}
}

function validateWishlistsForm() {
	var form = $('wishlists_form');
	var inputWishlistCode = $('inputWishlistCode');

	if (!form) return;

	form.onsubmit = function () {
		if (!validate(inputWishlistCode, 'Please enter a wishlist code', hasSubstance(inputWishlistCode.value))) return false;
	};
}