// Scripts for commerce-related features
var i_wholesale = function() {
	return {
		init: function() {
			i_wholesale.addSkipAddressCheckBox();
		},
		addSkipAddressCheckBox: function() {
			var billing_textbox = document.getElementById('i-wholesale-billing-address');
			
			if (billing_textbox) {
				// Create checkbox and label
				var check_wrapper = document.createElement('div');
				var skip_checkbox = document.createElement('input');
				var skip_label    = document.createElement('label');
				var label_text    = document.createTextNode('My billing address is the same');
				
				check_wrapper.setAttribute('id', 'i-wholesale-skip-address-wrap');
				check_wrapper.className = 'inline-label';
				check_wrapper.style.margin = '1em 0';
				
				skip_checkbox.setAttribute('id', 'i-wholesale-skip-address');
				skip_checkbox.setAttribute('type', 'checkbox');
				
				skip_checkbox.onclick = function() {
					// Hide/display billing text box parent based on checkbox
					var billing_textbox = document.getElementById('i-wholesale-billing-address');
					var billing_wrap    = billing_textbox.parentNode;
					
					if (this.checked) {
						billing_wrap.style.display = 'none';
						
						// Clear shipping fields
						var address_textbox = document.getElementById('i-wholesale-mailing-address');
						
						billing_textbox.value = address_textbox.value;
					} else {
						billing_wrap.style.display = 'block';
					}
				}
				
				skip_label.setAttribute('for', 'i-wholesale-skip-address');
				skip_label.appendChild(label_text);
				
				check_wrapper.appendChild(skip_checkbox);
				check_wrapper.appendChild(skip_label);
				
				// Add checkbox and label to page
				var billing_wrap = billing_textbox.parentNode;
				billing_wrap.parentNode.insertBefore(check_wrapper, billing_wrap);
			}
		}
	};
}();

i_scripts.addDOMLoadedListener(i_wholesale.init);
