if(typeof ecommercecheckout == 'undefined') {
var ecommercecheckout = {};

ecommercecheckout.addressCopy = function(form, destLabel, sourceLabel) {
	var sourceCon = Element.select(form, "fieldset."+destLabel).pop();
	var destCon = Element.select(form, "fieldset."+sourceLabel).pop();
	var fld;
	for(var i = 0; fld = cmscontacteditor.addressFields[i]; i++) {
		var dest = form[destLabel+"_"+fld];
		var src = form[sourceLabel+"_"+fld];
		if (dest && src)
			dest.value = src.value;
	}
	cmscontacteditor.countrySelectUpdate({ target: Element.select(sourceCon, "div.country select").pop() });
	cmscontacteditor.countrySelectUpdate({ target: Element.select(destCon, "div.country select").pop() });
}

ecommercecheckout.billingSameAsShippingChange = function(evt) {
	var checkbox = Event.element(evt);
	if (!checkbox.checked)
		return;
	var form = checkbox.form;
	ecommercecheckout.addressCopy(form, "billing", "shipping");
	ecommercecheckout.uncheckBillingCopyCustomer();
}

ecommercecheckout.billingSameAsCustomerChange = function(evt) {
	var checkbox = Event.element(evt);
	if (!checkbox.checked)
		return;
	var form = checkbox.form;
	ecommercecheckout.addressCopy(form, "billing", "customer");
	ecommercecheckout.uncheckBillingCopyShipping();
}

ecommercecheckout.shippingSameAsCustomerChange = function(evt) {
	var checkbox = Event.element(evt);
	if (!checkbox.checked)
		return;
	var form = checkbox.form;
	ecommercecheckout.addressCopy(form, "shipping", "customer");
}

ecommercecheckout.buttonRecordClick = function (evt) {
	var button = Event.findElement(evt, 'button');
	if (button.disabled)
		return;
	var input = button.form.elements['buttonclicked'];
	if (button && input)
		input.value = button.name;
}

ecommercecheckout.paymentMethodInputChange = function (evt) {
	var el = Event.element(evt);
	if (el.selector && el.selector.type == 'radio')
		el.selector.checked = true;
}

// Browser issues with disabling the button:
// 1. Safari/Konqueror do not like changing the button element content before
//		submitting the form, the submit will be ignored.
// 2. IE does not like having the button disabled before the standard event
//		processing is done, it will not process.
// 3. IE does not like using the event object in a function called by setTimeout(...).
ecommercecheckout.submitButtonDisable = function (evt) {
	var button = Event.findElement(evt, 'button');
	if (button.disabled) {
		Event.stop(evt);
		return;
	}
	setTimeout(function() { ecommercecheckout.submitButtonDisable2(button) }, 10);
}

ecommercecheckout.submitButtonDisable2 = function (button) {
	var pleaseWaitEl = $$('div.checkout span.wait').pop();
	if (pleaseWaitEl && pleaseWaitEl.firstChild) {
		while (button.firstChild) button.removeChild(button.firstChild);
		button.appendChild(pleaseWaitEl.firstChild);
	} else {
		cms.setInnerHTML(button, '<span>Please wait...</span>');
	}
	var form = button.form;
	var el, list;
	list = $A(form.getElementsByTagName('button'));
	while (el = list.pop()) {
		el.disabled = 'true';
	}
}

ecommercecheckout.buttonHandler = function (evt) {
	ecommercecheckout.buttonRecordClick(evt);
	ecommercecheckout.submitButtonDisable(evt);
}

ecommercecheckout.uncheckBillingCopyAll = function () {
	ecommercecheckout.uncheckBillingCopyShipping();
	ecommercecheckout.uncheckBillingCopyCustomer();
}

ecommercecheckout.uncheckBillingCopyShipping = function () {
	var el, list = $$("div.shipping_same_as_billing input, div.billing_same_as_shipping input, input.billing_same_as_shipping");
	while (el = list.pop())
		el.checked = false;
}

ecommercecheckout.uncheckBillingCopyCustomer = function () {
	var el, list = $$("div.billing_same_as_customer input, input.billing_same_as_customer");
	while (el = list.pop())
		el.checked = false;
}

ecommercecheckout.uncheckShippingCopyCustomer = function () {
	var el, list = $$("div.shipping_same_as_customer input, input.shipping_same_as_customer");
	while (el = list.pop())
		el.checked = false;
}

cms.executeOnWindowLoadOrAfter(function() {
	var el, list;

	list = $$('div.checkout button');
	while (el = list.pop()) {
		Event.observe(el, 'click', ecommercecheckout.buttonHandler, false);
	}
		
	// copy shipping address to billing address
	var slist = $$("div.shipping_same_as_billing input, div.billing_same_as_shipping input, input.billing_same_as_shipping"); // the first is not named correctly, but may be used by existing CSS
	while (el = slist.pop()) {
		var checkbox = el;
		var fld;
		for(var i = 0; fld = cmscontacteditor.addressFields[i]; i++) {
			var f = el.form["shipping_"+fld];
			if (f && f != el)
				Event.observe(f, (f.click) ? 'click':'change', function() { ecommercecheckout.uncheckBillingCopyShipping(); }, false);
			f = el.form["billing_"+fld];
			if (f && f != el)
				Event.observe(f, (f.click) ? 'click':'change', function() { ecommercecheckout.uncheckBillingCopyAll(); }, false);
		}
		
		Event.observe(el, 'click', ecommercecheckout.billingSameAsShippingChange, false);
	}

	// copy customer address to billing address
	var clist = $$("div.billing_same_as_customer input, input.billing_same_as_customer");
	while (el = clist.pop()) {
		var checkbox = el;
		var fld;
		for(var i = 0; fld = cmscontacteditor.addressFields[i]; i++) {
			var f = el.form["customer_"+fld];
			if (f && f != el)
				Event.observe(f, (f.click) ? 'click':'change', function() { ecommercecheckout.uncheckBillingCopyCustomer(); }, false);
			f = el.form["billing_"+fld];
			if (f && f != el)
				Event.observe(f, (f.click) ? 'click':'change', function() { ecommercecheckout.uncheckBillingCopyAll(); }, false);
		}
		
		Event.observe(el, 'click', ecommercecheckout.billingSameAsCustomerChange, false);
	}
	
	// copy customer address to shipping address
	var dlist = $$("div.shipping_same_as_customer input, input.shipping_same_as_customer");
	while (el = dlist.pop()) {
		var checkbox = el;
		var fld;
		for(var i = 0; fld = cmscontacteditor.addressFields[i]; i++) {
			var f = el.form["customer_"+fld];
			if (f && f != el)
				Event.observe(f, (f.click) ? 'click':'change', function() { ecommercecheckout.uncheckShippingCopyCustomer(); }, false);
			f = el.form["shipping_"+fld];
			if (f && f != el)
				Event.observe(f, (f.click) ? 'click':'change', function() { ecommercecheckout.uncheckShippingCopyCustomer(); }, false);
		}
		
		Event.observe(el, 'click', ecommercecheckout.shippingSameAsCustomerChange, false);
	}
	
	// activates payment method radio when data is changed
	var list = $$('div.payment_method');
	while (el = list.pop()) {
		var radio;
		var i = 0;
		Element.select(el, "input", "select").each(function(input) {
			if (input.name == 'payment_method') {
				radio = input;
			}
			else if (radio) {
				if (input.type == 'hidden')
					return;
				input.selector = radio;
				Event.observe(input, (input.click) ? 'click':'change', ecommercecheckout.paymentMethodInputChange, false);
			}
		});
	}
});


}

