(function ($) {

	df.namespace ('df.checkout');


	/**
	 * @param {String} elementSelector
	 */
	df.checkout.Ergonomic = {

		construct: function (_config) { var _this = {


			init: function () {

				$(function () {

					if (0 < _this.getElement().size()) {

						$('a.df-login', _this.getElement ())
							.fancybox ({
								'titlePosition'		: 'inside',
								'transitionIn'		: 'none',
								'transitionOut'		: 'none'
							})
						;


						df.checkout.ergonomic.address.Billing
							.construct (
								{
									element: $('.df-block-address-billing', _this.getElement())
								}
							)
						;

						df.checkout.ergonomic.address.Shipping
							.construct (
								{
									element: $('.df-block-address-shipping', _this.getElement())
								}
							)
						;

						df.checkout.ergonomic.method.Shipping
							.construct (
								{
									element: $('.df-block-method-shipping', _this.getElement())
								}
							)
						;

						df.checkout.ergonomic.method.Payment
							.construct (
								{
									element: $('.df-block-method-payment', _this.getElement())
								}
							)
						;


						df.checkout.ergonomic.Review
							.construct (
								{
									element: $('.order-review', _this.getElement())
								}
							)
						;

					}

				});

			}


			,
			/**
			 * @private
			 * @returns {jQuery} HTMLElement
			 */
			getElement: function () {

				if ('undefined' == typeof this._element) {

					/** @type {jQuery} HTMLElement */
					var result =
						$(this.getElementSelector ())
					;

					this._element = result;

				}

				return this._element;
			}


			,
			/**
			 * @private
			 * @returns {String}
			 */
			getElementSelector: function () {
				return _config.elementSelector;
			}


		}; _this.init (); return _this; }


	};





})(jQuery);/**
 * Программный код,
 * который надо выполнить сразу после загрузки страницы
 */

(function ($) { $(function () {

	df.checkout.Ergonomic
		.construct (
			{
				elementSelector: '.df .df-checkout-ergonomic'
			}
		)
	;

}); })(jQuery);(function ($) {

	df.namespace ('df.checkout.ergonomic');


	df.checkout.ergonomic.Review = {

		construct: function (_config) { var _this = {


			init: function () {

			}


			,
			/**
			 * @private
			 * @returns {ShippingMethod}
			 */
			getReview: function () {
				return review;
			}


			,
			/**
			 * @private
			 * @returns {jQuery} HTMLElement
			 */
			getElement: function () {
				return _config.element;
			}


			,
			/**
			 * @private
			 * @returns {Validation}
			 */
			getValidator: function () {

				if ('undefined' === typeof this._validator) {

					/**
					 * @type {Validation}
					 */
					this._validator =
						new Validation (
							_this.getPaymentMethod().form
						)
					;
				}

				return this._validator;
			}


		}; _this.init (); return _this; }


	};





})(jQuery);(function ($) {

	df.namespace ('df.checkout.ergonomic.address');


	/**
	 * @param {jQuery} element
	 */
	df.checkout.ergonomic.address.Billing = {

		shippingAddressIsTheSame: 'df.checkout.ergonomic.address.Billing.shippingAddressIsTheSame'

		,
		construct: function (_config) { var _this = {


			init: function () {

				$(function () {


					_this.getAddress().getField ('use_for_shipping').getElement()
						.change (
							function () {
								_this.handleUseForShipping ();
							}
						)
					;

					_this.handleUseForShipping ();


					_this.getAddress().getFields()
 						.change (
							function () {

//								var validationResultAsText =
//										_this.getValidator().dfValidateSilent()
//									?
//										'OK'
//									:
//										'HAS ERRORS'
//								;
//
//								console.log (validationResultAsText);


								_this.getValidator().dfValidateFilledFieldsOnly();

								if (_this.getValidator().dfValidateSilent()) {
									_this.getBilling().save();
								}

							}
						)
					;

				});

			}



			,
			/**
			 * @public
			 * @returns {df.checkout.ergonomic.address.Billing}
			 */
			handleUseForShipping: function () {

				/** @type {Boolean} */
				var shippingAddressIsTheSame = document.getElementById ('billing:use_for_shipping_yes').checked;

				$(window)
					.trigger (
						{
							/** @type {String} */
							type: df.checkout.ergonomic.address.Billing.shippingAddressIsTheSame

							,
							/** @type {df.checkout.ergonomic.address.Shipping} */
							value: shippingAddressIsTheSame
						}
					)
				;

				return this;
			}



			,
			/**
			 * @public
			 * @returns {df.customer.Address}
			 */
			getAddress: function () {

				if ('undefined' === typeof this._address) {

					/**
					 * @type {df.customer.Address}
					 */
					this._address =
						df.customer.Address
							.construct (
								{
									element: $('#co-billing-form', _this.getElement())
									,
									type: 'billing'
								}
							)
					;
				}

				return this._address;
			}




			,
			/**
			 * @private
			 * @returns {Billing}
			 */
			getBilling: function () {
				return billing;
			}



			,
			/**
			 * @private
			 * @returns {jQuery} HTMLElement
			 */
			getElement: function () {
				return _config.element;
			}



			,
			/**
			 * @private
			 * @returns {Validation}
			 */
			getValidator: function () {

				if ('undefined' === typeof this._validator) {

					/**
					 * @type {Validation}
					 */
					this._validator =
						new Validation (
							_this.getBilling().form
						)
					;
				}

				return this._validator;
			}



		}; _this.init (); return _this; }


	};





})(jQuery);(function ($) {

	df.namespace ('df.checkout.ergonomic.address');


	df.checkout.ergonomic.address.Shipping = {

		addressChanged: 'df.checkout.ergonomic.address.Shipping.addressChanged'


		,
		construct: function (_config) { var _this = {


			init: function () {

				$(window)
					.bind (
						df.checkout.ergonomic.address.Billing.shippingAddressIsTheSame
						,
						/**
						 * @param {jQuery.Event} event
						 */
						function (event) {

							shipping.setSameAsBilling (event.value);

							_this.getElement().toggle (!event.value);

						}
					)
				;

				$(function () {

					_this.getAddress().getFields()
 						.change (
							function () {

//								var validationResultAsText =
//										_this.getValidator().dfValidateSilent()
//									?
//										'OK'
//									:
//										'HAS ERRORS'
//								;
//
//								console.log (validationResultAsText);


								_this.getValidator().dfValidateFilledFieldsOnly();

								if (_this.getValidator().dfValidateSilent()) {
									_this.getShipping().save();
								}

							}
						)
					;


				});

			}


			,
			/**
			 * @public
			 * @returns {df.customer.Address}
			 */
			getAddress: function () {

				if ('undefined' === typeof this._address) {

					/**
					 * @type {df.customer.Address}
					 */
					this._address =
						df.customer.Address
							.construct (
								{
									element: $('#co-shipping-form', _this.getElement())
									,
									type: 'shipping'
								}
							)
					;
				}

				return this._address;
			}



			,
			/**
			 * @private
			 * @returns {Shipping}
			 */
			getShipping: function () {
				return shipping;
			}



			,
			/**
			 * @private
			 * @returns {jQuery} HTMLElement
			 */
			getElement: function () {
				return _config.element;
			}



			,
			/**
			 * @private
			 * @returns {Validation}
			 */
			getValidator: function () {

				if ('undefined' === typeof this._validator) {

					/**
					 * @type {Validation}
					 */
					this._validator =
						new Validation (
							_this.getShipping().form
						)
					;
				}

				return this._validator;
			}



		}; _this.init (); return _this; }


	};





})(jQuery);(function ($) {

	df.namespace ('df.checkout.ergonomic.method');


	df.checkout.ergonomic.method.Payment = {

		construct: function (_config) { var _this = {


			init: function () {

				$(function () {

					$(document.getElementById (_this.getPaymentMethod().form))
 						.change (
							function () {

								_this.getValidator().dfValidateFilledFieldsOnly();

								if (_this.getValidator().dfValidateSilent()) {
									_this.getPaymentMethod().save();

								}

							}
						)
					;


				});

			}


			,
			/**
			 * @private
			 * @returns {ShippingMethod}
			 */
			getPaymentMethod: function () {
				return payment;
			}


			,
			/**
			 * @private
			 * @returns {jQuery} HTMLElement
			 */
			getElement: function () {
				return _config.element;
			}


			,
			/**
			 * @private
			 * @returns {Validation}
			 */
			getValidator: function () {

				if ('undefined' === typeof this._validator) {

					/**
					 * @type {Validation}
					 */
					this._validator =
						new Validation (
							_this.getPaymentMethod().form
						)
					;
				}

				return this._validator;
			}


		}; _this.init (); return _this; }


	};





})(jQuery);(function ($) {

	df.namespace ('df.checkout.ergonomic.method');


	df.checkout.ergonomic.method.Shipping = {

		construct: function (_config) { var _this = {


			init: function () {

				$(function () {

					$(document.getElementById (_this.getShippingMethod().form))
 						.change (
							function () {

								_this.getValidator().dfValidateFilledFieldsOnly();

								if (_this.getValidator().dfValidateSilent()) {
									_this.getShippingMethod().save();

								}

							}
						)
					;


				});

			}


			,
			/**
			 * @private
			 * @returns {ShippingMethod}
			 */
			getShippingMethod: function () {
				return shippingMethod;
			}


			,
			/**
			 * @private
			 * @returns {jQuery} HTMLElement
			 */
			getElement: function () {
				return _config.element;
			}


			,
			/**
			 * @private
			 * @returns {Validation}
			 */
			getValidator: function () {

				if ('undefined' === typeof this._validator) {

					/**
					 * @type {Validation}
					 */
					this._validator =
						new Validation (
							_this.getShippingMethod().form
						)
					;
				}

				return this._validator;
			}


		}; _this.init (); return _this; }


	};





})(jQuery);(function ($) {

	df.namespace ('df.customer');


	/**
	 * @param {String} type		тип адреса
	 * @param {jQuery} element
	 */
	df.customer.Address = {construct: function (_config) { var _this = {


		init: function () {

			$(function () {

			});

		}



		,
		/**
		 * @public
		 * @returns {jQuery} HTMLElement[]
		 */
		getFields: function () {

			if ('undefined' === typeof this._fields) {

				/**
				 * @type {jQuery} HTMLElement[]
				 */
				this._fields =
					$(':input', this.getElement())
				;
			}

			return this._fields;
		}



		,
		/**
		 * @public
		 * @returns {jQuery} HTMLElement
		 */
		getFieldCity: function () {
			return this.getField ('city');
		}



		,
		/**
		 * @public
		 * @returns {jQuery} HTMLElement
		 */
		getFieldCountry: function () {
			return this.getField ('country');
		}



		,
		/**
		 * @public
		 * @returns {jQuery} HTMLElement
		 */
		getFieldNameFirst: function () {
			return this.getField ('firstname');
		}



		,
		/**
		 * @public
		 * @returns {jQuery} HTMLElement
		 */
		getFieldNameLast: function () {
			return this.getField ('lastname');
		}



		,
		/**
		 * @public
		 * @returns {jQuery} HTMLElement
		 */
		getFieldNameMiddle: function () {
			return this.getField ('middlename');
		}



		,
		/**
		 * @public
		 * @returns {jQuery} HTMLElement
		 */
		getFieldPostalCode: function () {
			return this.getField ('postcode');
		}




		,
		/**
		 * @public
		 * @returns {jQuery} HTMLElement
		 */
		getFieldRegionSelect: function () {
			return this.getField ('region_id');
		}



		,
		/**
		 * @public
		 * @returns {jQuery} HTMLElement
		 */
		getFieldRegionText: function () {
			return this.getField ('region');
		}




		,
		/**
		 * @public
		 * @param {String} nameSuffix
		 * @returns {df.customer.address.Field}
		 */
		getField: function (nameSuffix) {

			if ('undefined' == typeof this._field [nameSuffix]) {

				/** @type {df.customer.address.Field} */
				var result =
					df.customer.address.Field
						.construct ({
							element:
								$(
									'[name="%fieldName%"]'
										.replace ('%fieldName%', this.getFieldName(nameSuffix))
									,
									this.getElement ()
								)
						})
				;


				this._field [nameSuffix] = result;

			}

			return this._field [nameSuffix];
		}


		,
		/**
		 * @type {df.customer.address.Field[]}
		 */
		_field: []




		,
		/**
		 * @private
		 * @returns {jQuery} HTMLElement
		 */
		getElement: function () {
			return _config.element;
		}





		,
		/**
		 * @private
		 * @param {String} nameSuffix
		 * @returns {String}
		 */
		getFieldName: function (nameSuffix) {
			return (
					(0 === this.getType().length)
				?
					nameSuffix
				:
					'%prefix%[%suffix%]'
						.replace ('%prefix%', this.getType())
						.replace ('%suffix%', nameSuffix)
			);
		}


		,
		/**
		 * @private
		 * @returns {String}
		 */
		getType: function () {
			return _config.type;
		}






	}; _this.init (); return _this; } };


})(jQuery);(function ($) {

	df.namespace ('df.customer.address');


	/**
	 * @param {jQuery} element
	 */
	df.customer.address.Field = {construct: function (_config) { var _this = {


		init: function () {

			$(function () {

			});
		}


		,
		/**
		 * @public
		 * @returns {jQuery} HTMLElement
		 */
		getElement: function () {
			return _config.element;
		}


		,
		/**
		 * @public
		 * @returns {Boolean}
		 */
		isRequired: function () {
			return this.getElement().hasClass ('required-entry');
		}




	}; _this.init (); return _this; } };


})(jQuery);function show_hide_checkbox_fields(objName,condition)
{
	document.getElementById(objName).disabled = condition==1?"disabled":"";
}
(function () {


	/**
	 * Наша задача: выделить в корзине товары-подарки.
	 */
	document.observe("dom:loaded", function() {

		if (
				window.df
			&&
				window.df.promo_gift
			&&
				window.df.promo_gift.giftingQuoteItems
		) {
			var giftingQuoteItems = window.df.promo_gift.giftingQuoteItems;

			if (giftingQuoteItems instanceof Array) {

				/**
				 * Итак, надо найти в корзине строки заказа giftingQuoteItems и выделить их.
				 */

				var $quoteItems = $$ ("#shopping-cart-table a.btn-remove");

				if (1 > $quoteItems.length) {
					$quoteItems = $$ ("#shopping-cart-table a.btn-remove2");
				}

				$quoteItems.each (function (item) {

					var url = item.href;

					if (df_validate_string (url)) {

						var quoteItemIdExp = /id\/(\d+)\//;
						var matches = url.match (quoteItemIdExp);

						if (matches instanceof Array) {


							if (1 < matches.length) {

								var quoteItemId = parseInt (matches [1]);

								if (!isNaN (quoteItemId)) {


									/**
									 * Нашли идентификатор текущего товара в корзине
									 */

									if (-1 < giftingQuoteItems.indexOf (quoteItemId)) {

										/**
										 * Эта строка заказа — подарок. Выделяем её
										 */

										var $tr = $(item).up ('tr');

										if ($tr) {
											$tr.addClassName("df-free-quote-item");
										}


										/**
										 * Подписываем подарок
										 */
										var elements = $tr.select ('.product-name');
										if (0 < elements.length) {

											var $productName = $(elements [0]);


											var $giftLabel = new Element ('div');
											$giftLabel.addClassName ('df-gift-label');
											$giftLabel
												.update (
													window.df.promo_gift.giftingQuoteItemTitle
												)
											;

											$productName
												.insert ({
													'after': $giftLabel
												})
											;
										}


									}

								}

							}
						}
					}
				});
			}
		}


	});

})();(function () {


	/**
	 * Наша задача: выделить в корзине товары-подарки.
	 */
	document.observe("dom:loaded", function() {

		if (
				window.df
			&&
				window.df.promo_gift
			&&
				window.df.promo_gift.eligibleProductIds
		) {
			var eligibleProductIds = window.df.promo_gift.eligibleProductIds;

			if (eligibleProductIds instanceof Array) {

				/**
				 * Итак, если покупатель смотрит карточку товара,
				 * и данный товар он вправе получить в подарок (выполнил условия акции),
				 * то надо внешне отразить сей факт на карточке товара
				 */

				var $addToCartForm = $('product_addtocart_form');

				if ($addToCartForm) {

					var productIdInputs = $addToCartForm.select ("input[name='product']");

					if (
							productIdInputs
						&&
							(productIdInputs instanceof Array)
						&&
							(0 < productIdInputs.length)
					) {

						var productId = parseInt ($(productIdInputs [0]).getValue ());

						if (-1 < eligibleProductIds.indexOf(productId)) {

							$addToCartForm.up ('.product-view').addClassName ('df-gift-product');



							var labelText = window.df.promo_gift.eligibleProductLabel;

							if (df_validate_string (labelText)) {

								var $giftLabel = new Element ('div');
								$giftLabel.addClassName ('df-gift-label');
								$giftLabel
									.update (
										labelText
									)
								;

								var priceBoxes = $addToCartForm.select ('.price-box');
								if (
										priceBoxes
									&&
										(priceBoxes instanceof Array)
									&&
										(0 < priceBoxes.length)
								) {
									var $priceBox = $(priceBoxes[0]);

									$priceBox
										.insert ({
											'after': $giftLabel
										})
									;
								}

							}

						}
					}
				}
			}
		}


	});

})();(function () {

	/**
	 * Наша задача:
	 * 		[*]	назначить чётным подаркам класс df-even
	 * 		[*] назначить нечётным подаркам класс df-odd
	 * 		[*] назначить первому подарку класс df-first
	 * 		[*] назначить последнему подарку класс df-last
	 */
	document.observe("dom:loaded", function() {

		var odd = true;

		var $products = $$ (".df-promo-gift .df-gift-chooser .df-side li.df-product");


		/**
		 * Обратите внимание, что $products.first() может вернуть undefined,
		 * если массив $products пуст.
		 * Аналогично и $products.last().
		 *
		 * @link http://www.prototypejs.org/api/array/first
		 */
		if ($products.first ()) {
			$products.first ().addClassName ('df-first');
		}

		if ($products.last ()) {
			$products.last ().addClassName ('df-last');
		}

		$products
			.each (
				function (product) {
					$(product).addClassName (odd ? 'df-odd' : 'df-even');
					odd = !odd;
				}
			)
		;



	});

})();(function ($) {

	$(function () {


		/** @type {HTMLElement} */
		var $container = $('.df-shipping-torg12');

		/** @type {Number} */
		var $containerWidthInMm = 275.4;


		/** @type {Number} */
		var dotsInMm = $container.width () / $containerWidthInMm;


		/** @type {Number} */
		var mmsInDot = $containerWidthInMm / $container.width ();


		/** @type {Number} */
		var pageHeightInMm = 210;

		var currentPageOrdering = 1;



		(function () {

			/** @type {Array} */
			var engines = ['webkit', 'opera', 'msie', 'mozilla'];


			$.each (engines, function (index, engine) {

				if ($.browser[engine]) {
					$container
						.addClass ('df-shipping-torg12-' + engine)
					;
				}

			});

		})();




		(function () {

			/** @type {jQuery} HTMLTableRowElement[] */
			var $rows = $('.df-orderItems tbody tr.df-orderItem');

			$rows.first().addClass ('df-first');

			$rows.last().addClass ('df-last');

		})();



		(function () {


			var $row3Input =
				$('.df-heading-left .df-row-3 .df-input .df-text', $container)
			;

			var $row5Input =
				$('.df-heading-left .df-row-5 .df-input .df-text', $container)
			;


			//$row3Input.alignBottom ();

			//$row5Input.alignBottom ();

		})();



//		(function () {
//
//			/** @type {jQuery} HTMLTableRowElement[] */
//			var $rows = $('.df-orderItems tbody tr.df-orderItem');
//
//			/** @type {?jQuery} HTMLTableRowElement */
//			var $prevRow = null;
//
//			$rows.each (function () {
//
//				/** @type {jQuery} HTMLTableRowElement */
//				var $row = $(this);
//
//				/** @type {Number} */
//				var rowBottom = $row.offset().top + $row.outerHeight(false);
//
//
//				if (rowBottom > pageHeightInMm * dotsInMm * currentPageOrdering) {
//
//					$row.addClass ('df-breakBefore');
//
//					$row.addClass ('df-firstRowOnPage');
//
//					if ($prevRow) {
//						$prevRow.addClass ('df-lastRowOnPage');
//					}
//
//					currentPageOrdering++;
//				}
//
//				$prevRow = $row;
//
//			});
//
//		})();


	});

})(jQuery);if ('undefined' !== typeof RegionUpdater) {
	RegionUpdater.prototype.update =
		function () {
			if (this.regions[this.countryEl.value]) {
				var i, option, region, def;

				if (this.regionTextEl) {
					def = this.regionTextEl.value.toLowerCase();
					this.regionTextEl.value = '';
				}
				if (!def) {
					def = this.regionSelectEl.getAttribute('defaultValue');
				}


				this.regionSelectEl.options.length = 1;
				for (regionId in this.regions[this.countryEl.value]) {

					region = this.regions[this.countryEl.value][regionId];


					/**
					 * BEGIN PATCH
					 */
					regionId = region.id;

					if ('undefined' === typeof regionId) {
						continue;
					}
					/**
					 * END PATCH
					 */

					option = document.createElement('OPTION');
					option.value = regionId;
					option.text = region.name;

					if (this.regionSelectEl.options.add) {
						this.regionSelectEl.options.add(option);
					} else {
						this.regionSelectEl.appendChild(option);
					}

					if (regionId==def || region.name.toLowerCase()==def || region.code.toLowerCase()==def) {
						this.regionSelectEl.value = regionId;
					}
				}

				if (this.disableAction=='hide') {
					if (this.regionTextEl) {
						this.regionTextEl.style.display = 'none';
					}

					this.regionSelectEl.style.display = '';
				} else if (this.disableAction=='disable') {
					if (this.regionTextEl) {
						this.regionTextEl.disabled = true;
					}
					this.regionSelectEl.disabled = false;
				}
				this.setMarkDisplay(this.regionSelectEl, true);
			} else {
				if (this.disableAction=='hide') {
					if (this.regionTextEl) {
						this.regionTextEl.style.display = '';
					}
					this.regionSelectEl.style.display = 'none';
					Validation.reset(this.regionSelectEl);
				} else if (this.disableAction=='disable') {
					if (this.regionTextEl) {
						this.regionTextEl.disabled = false;
					}
					this.regionSelectEl.disabled = true;
				} else if (this.disableAction=='nullify') {
					this.regionSelectEl.options.length = 1;
					this.regionSelectEl.value = '';
					this.regionSelectEl.selectedIndex = 0;
					this.lastCountryId = '';
				}
				this.setMarkDisplay(this.regionSelectEl, false);
			}

			// Make Zip and its label required/optional
			var zipUpdater = new ZipUpdater(this.countryEl.value, this.zipEl);
			zipUpdater.update();
		}
	;
}
