window.addEventListener('DOMContentLoaded', function(){
	[...document.querySelectorAll('[data-copy-source]')].map(function(e){
		e.addEventListener('click', function(event){
			let target_id = event.target.getAttribute('data-copy-source');
			let source = document.getElementById(target_id);
			if(source){
				source.select();
				source.setSelectionRange(0, 99999); /* For mobile devices */
				const value = source.value.trim();
				if(value.length === 0){
				  return alert('Key not yet generated.');
				}
				if(document.execCommand('copy')){
					return alert('Copied.');
				}
				navigator.clipboard.writeText(value).then(function(){
					return alert('Copied.');
				}, function(){
					return alert('Copy failed.');
				});
			}
		});
	});
	document.getElementById('bbmsl-gateway-select-all').addEventListener('click', function(){
		[...document.querySelectorAll('.payment-method input[type="checkbox"]')].map(function(e){e.checked = true;});
	});
	document.getElementById('bbmsl-gateway-deselect-all').addEventListener('click', function(){
		[...document.querySelectorAll('.payment-method input[type="checkbox"]')].map(function(e){e.checked = false;});
	});
	document.getElementById('toggle_site_checkbox').addEventListener('change', function () {
		[...document.querySelectorAll('.toggle_site')].map(function (e) { e.classList.remove('disabled'); });
		if (this.checked) {
			document.getElementById('toggle_site_live').classList.add('disabled');
		} else {
			document.getElementById('toggle_site_testing').classList.add('disabled');
		}
	});
	
	document.getElementById('bbmsl-gateway-woocommerce-column-select-all').addEventListener('click', function(){
		[...document.querySelectorAll('.woocommerce-column input[type="checkbox"]')].map(function(e){e.checked = true;});
	});
	document.getElementById('bbmsl-gateway-woocommerce-column-deselect-all').addEventListener('click', function(){
		[...document.querySelectorAll('.woocommerce-column input[type="checkbox"]')].map(function(e){e.checked = false;});
	});
});