// Scripts for commerce-related features
var i_commerce = function() {
	return {
		init: function() {
			i_commerce.showHideStateCountry('billing');
			i_commerce.showHideStateCountry('ship');
			i_commerce.addSkipShippingCheckBox();
			i_commerce.addTextBoxFeatures();
		},
		showHideStateCountry: function(field_type) {
			var state_dropdown = document.getElementById(field_type + '-state');
			
			if (state_dropdown) {
				var other_textbox    = document.getElementById(field_type + '-state-other');
				var country_dropdown = document.getElementById(field_type + '-country');
				
				// Add new option to state drop-down if other countries exist
				if (country_dropdown.options.length > 1 || country_dropdown.options[0].value != 'US') {
					var non_us_option = document.createElement('option');
					var non_us_text   = document.createTextNode('Outside of US');
				
					non_us_option.setAttribute('value', '');
					non_us_option.appendChild(non_us_text);
				
					state_dropdown.appendChild(non_us_option);
				}
				
				// Hide country and state(other) fields unless selected
				if (other_textbox.value == '') {
					other_textbox.parentNode.style.display    = 'none';
					country_dropdown.parentNode.style.display = 'none';
					country_dropdown.value                    = 'US';
				}
				
				// Allow new option to show hidden fields
				state_dropdown.onchange = function() {
					field_type = (this.id == 'billing-state') ? 'billing' : 'ship';
					
					var other_textbox    = document.getElementById(field_type + '-state-other');
					var country_dropdown = document.getElementById(field_type + '-country');
					
					if (this.value == '') {
						// Show fields
						other_textbox.parentNode.style.display    = 'block';
						country_dropdown.parentNode.style.display = 'block';
					} else {
						// Hide fields
						other_textbox.parentNode.style.display    = 'none';
						country_dropdown.parentNode.style.display = 'none';
					}
				}
			}
		},
		addSkipShippingCheckBox: function() {
			var ship_fieldset = document.getElementById('icarus-commerce-delivery-info');
			
			if (ship_fieldset) {
				// 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('Deliver to my billing address');
				
				check_wrapper.setAttribute('id', 'icarus-commerce-skip-ship-wrap');
				check_wrapper.style.margin = '1em 0';
				
				skip_checkbox.setAttribute('id', 'icarus-commerce-skip-ship');
				skip_checkbox.setAttribute('type', 'checkbox');
				
				skip_checkbox.onclick = function() {
					// Hide/display delivery fieldset based on checkbox
					var ship_fieldset = document.getElementById('icarus-commerce-delivery-info');
					
					if (this.checked) {
						ship_fieldset.style.display = 'none';
						
						// Clear shipping fields
						var fname_textbox       = document.getElementById('ship-first-name');
						var lname_textbox       = document.getElementById('ship-last-name');
						var address_textbox     = document.getElementById('ship-address');
						var address2_textbox    = document.getElementById('ship-address2');
						var city_textbox        = document.getElementById('ship-city');
						var other_textbox       = document.getElementById('ship-state-other');
						var postal_code_textbox = document.getElementById('ship-postal-code');
						
						var state_dropdown      = document.getElementById('ship-state');
						var country_dropdown    = document.getElementById('ship-country');
						
						fname_textbox.value = lname_textbox.value = address_textbox.value = address2_textbox.value = city_textbox.value = other_textbox.value = postal_code_textbox.value = '';
						state_dropdown.selectedIndex = country_dropdown.selectedIndex = 0;
					} else {
						ship_fieldset.style.display = 'block';
					}
				}
				
				skip_label.setAttribute('for', 'icarus-commerce-skip-ship');
				skip_label.appendChild(label_text);
				
				check_wrapper.appendChild(skip_checkbox);
				check_wrapper.appendChild(skip_label);
				
				// Add checkbox and label to page
				ship_fieldset.parentNode.insertBefore(check_wrapper, ship_fieldset);
			}
		},
		addTextBoxFeatures: function() {
			var textboxes = i_scripts.getElementsByClassName('icomm-textbox');
			
			for (var n = 0, el; el = textboxes[n++];) {
				// Adds listeners to clear or reset the values for text boxes with a class of "icomm-textbox".
				i_scripts.addListener(el, 'focus', i_commerce.clearTextBoxValue);
				i_scripts.addListener(el, 'blur', i_commerce.resetTextBoxValue);
				
				// Clear value on form submission if default value hasn't changed
				var parent = el.parentNode;
				
				while (parent.nodeName.toLowerCase() != 'form') {
					parent = parent.parentNode;
				}
				
				i_scripts.addListener(parent, 'submit', (function(el){return function(e){if (el.value == el.defaultValue) el.value = '';}})(el));
				
				// Add calendar popup feature
				if (el.value.toLowerCase().indexOf('date') > -1) {
					// Instantiate calendar
					icomm_cal = new CalendarPopup('icomm-cal-div');
					
					// Create calendar link
					var link_el = document.createElement('a');
					var div_el  = document.createElement('div');
					
					link_el.id               = link_el.name = 'icomm-cal-' + n;
					link_el.href             = '#';
					link_el.style.marginLeft = "3px";
					link_el.innerHTML        = '<img src="/add-ons/icarus-commerce/calendar.gif" align="top" border="0" alt="Select a date">';
					
					i_scripts.addListener(link_el, "click", (function(el, link_id) {return function(e) {icomm_cal.select(el, link_id, 'MM/dd/yyyy'); var evt = e || event; if (evt.preventDefault) evt.preventDefault(); else evt.returnValue = false;}})(el, link_el.id));
					
					div_el.id = 'icomm-cal-div';
					div_el.style.position = 'absolute';
					div_el.style.backgroundColor = '#fff';
					
					el.parentNode.insertBefore(link_el, el.nextSibling);
					
					el.parentNode.appendChild(div_el);
				}
			}
		},
		clearTextBoxValue: function(e) {
			var el = (e.target) ? e.target : event.srcElement;
			if (el.value == el.defaultValue) el.value = '';
		},
		resetTextBoxValue: function(e) {
			var el = (e.target) ? e.target : event.srcElement;
			if (el.value == '') el.value = el.defaultValue;
		}
	};
}();

i_scripts.addDOMLoadedListener(i_commerce.init);
