	
	jQuery.fn.sameAsShipping = function() {
		return this.each(function() {
			var root = $(this);
			
			root.bind('change', function() {
				var fields = root.closest('fieldset').find('input, select');
				var checked = root.attr('checked');
				
				fields.each(function() {
					var field = $(this);
					var id = field.attr('id');
					
					var shipId = '#'+id.replace('billing_', 'shipping_');
					if ($(shipId).length > 0) {
						if (checked == true) {
							
							// Bind an event to update the billing field when
							// the shipping field is changed.
							$(shipId).bind('change', function() {
								field.val($(this).val());
							});
							// Trigger a shipping field change to set the billing field.
							$(shipId).trigger('change');
							// Mark the billing field readonly.
							$(this).attr('readonly', true);
						}
						else {
							// Unbind the even thats syncs shipping to billing.
							$(shipId).unbind('change');
							// Make the billing field editable.
							$(this).attr('readonly', false);
						}
					}
				});
			});
			
			root.trigger('change');
		});
	};
