// Scripts for commerce-related features
var i_commerce = function() {
	return {
		init: function() {
			i_commerce.addSkipShippingCheckBox();
			i_commerce.addTextBoxFeatures();
			//i_commerce.addDropDownFeatures();
		},
		addSkipShippingCheckBox: function() {
		var ship_fieldset = document.getElementById('ship_choice_toggle');
			
		if(ship_fieldset) {
		var fname_textbox       = document.getElementById('ship_first_name');
                var fname_textbox_val   = fname_textbox.value;
                var lname_textbox       = document.getElementById('ship_last_name');
                var lname_textbox_val   = lname_textbox.value;
                var middle_name_textbox = document.getElementById('ship_middle_name');
                var mid_name_txtbox_val = middle_name_textbox.value;
                var address_textbox     = document.getElementById('ship_address');
                var address_textbox_val = address_textbox.value;
                var address2_textbox    = document.getElementById('ship_address2');
                var address2_txtbx_val  = address2_textbox.value;
                var city_textbox        = document.getElementById('ship_city');
                var city_textbox_val    = city_textbox.value;
		var state_textbox       = document.getElementById('ship_state');
                var state_textbox_val   = state_textbox.value;
                var postal_code_textbox = document.getElementById('ship_postal_code');
                var post_code_txtbx_val = postal_code_textbox.value;

                var country_dropdown    = document.getElementById('ship_country');
                var country_dropd_val   = $('#ship_country').val();
                 $('#shipping-address-wrap').hide('', function() {});
                ship_fieldset.onchange = function() {
                    // Hide/display delivery fieldset based on checkbox
			if($("#ship_choice_toggle").val() == 0) {
			   $('#shipping-address-wrap').show('', function() {});		           
			} else {
			// restore the values
                        $('#ship_first_name').val(fname_textbox_val);
                        $('#ship_last_name').val(lname_textbox_val);
                        $('#ship_middle_name').val(mid_name_txtbox_val);
                        $('#ship-address').val(address_textbox_val);
                        $('#ship_address2').val(address2_txtbx_val);
                        $('#ship_city').val(city_textbox_val);
                        $('#ship_postal_code').val(post_code_txtbx_val);
                        $('#shipping-address-wrap').hide('', function() {});
					}
				}
			}
		},
		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;
		},
		addDropDownFeatures: function() {
			var dropdowns = i_scripts.getElementsByClassName('icomm-dropdown');
			
			// Get product's base price
			var price_div  = document.getElementById('prod-price');
			var prod_split = price_div.innerHTML.split(' $');
			
			i_commerce.prod_price = Number(prod_split[prod_split.length - 1]);
			
			for (var n = 0, el; el = dropdowns[n++];) {
				// Add price adjustment for product option drop-downs
				i_scripts.addListener(el, 'change', i_commerce.updateOptionPricing);
			}
		},
		updateOptionPricing: function(e) {
			var el        = (e.target) ? e.target : event.srcElement;
			var sel_value = el.options[el.selectedIndex].firstChild.nodeValue;
			var price_div = document.getElementById('prod-price');
			
			if (sel_value.indexOf(' +$') > 0) {
				// Update price by adding additional cost to original price
				var value_split = sel_value.split(' +$');
				var value_price = value_split[value_split.length - 1];
				
				var total_price = i_commerce.prod_price + Number(value_price);
				
				price_div.innerHTML = '<strong>Price:</strong> $' + total_price;
			} else if (sel_value.indexOf(' ($') > 0) {
				// Update price to match value price
				var value_split = sel_value.split(' ($');
				var value_price = value_split[value_split.length - 1].replace(')', '');
				
				price_div.innerHTML = '<strong>Price:</strong> $' + value_price;
			} else {
				price_div.innerHTML = '<strong>Price:</strong> $' + i_commerce.prod_price;
			}
		}
	};
}();

i_scripts.addDOMLoadedListener(i_commerce.init);

/*jQuery(document).ready(function() {
    (function($) {
        $.QueryString = (function(a) {
            if (a == "") return {};
            var b = {};
            for (var i = 0; i < a.length; ++i)
            {
                var p=a[i].split('=');
                if (p.length != 2) continue;
                b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
            }
            return b;
        })(window.location.search.substr(1).split('&'))
    })(jQuery);
    
    $('#i-commerce-confirm').click(function() {
        if($.QueryString["token"] == undefined && $('#cc_pay:checked').length == 0 && $('input[name="payment_check"]:checked').length == 0) {
            alert('Please Select a Payment Method!');
            return false;
        }
    });
});*/

