﻿
jQuery(function () {


	//show or hide billing address on the new card page
	jQuery('#cBillingAddress').click(function () {
		if (jQuery(this).filter(':checked').length > 0) {
			jQuery('#co_billing').slideUp('slow');
		} else {
			jQuery('#co_billing').slideDown('slow');
		}
	});
	if (jQuery('#cBillingAddress:checked').length > 0) {
		jQuery('#co_billing').hide();
	}

	jQuery('input[name=placeOrder]').click(function (e) {
		if (jQuery('.popupIfEmpty input').val() == '') {
			clickFire(jQuery('.popupIfEmpty').clone().wrap('<form id="cv2form"/>').parent().append('<h1>Please enter your security number</h1><input type="submit" class="button09" value="Place your order"/>'), function (box) {
				jQuery(box).find('form').submit(function () {
					var field = jQuery(this).find('input[type=text]');
					if (field.val().length < 3) {
						field.focus();
						return false;
					}
					jQuery('input[name=' + field.attr('name') + ']').val(field.val());
					closePop();
					jQuery('input[name=placeOrder]').click();
					return false;
				});
			});
			e.stopImmediatePropagation();
			return false;
		}
		return true;
	});

	jQuery('.checkoutPopup').click(function () {
		jQuery('#popup').show();
	});

	jQuery('.extraLines').hide();
	jQuery('.showAnotherLine').show().click(function () {
		jQuery(this).remove();
		jQuery('.extraLines:first').removeClass('extraLines').show();
		return false;
	});

	jQuery('.toggleDeliveryDayWeeks').toggle(function () {
		jQuery('table.datePickerTable tr.period0').hide();
		jQuery('table.datePickerTable tr.period1').show();
		jQuery(this).text('Previous 4 weeks');
		return false;
	}, function () {
		jQuery('table.datePickerTable tr.period1').hide();
		jQuery('table.datePickerTable tr.period0').show();
		jQuery(this).text('Next 4 weeks');
		return false;
	});

	var originalUrl = window.location.href;
	var showCheckoutForm = function (e, btn) {
		$('.basketColShade:visible').not(e).hide();
		$(e).filter(':hidden').css({ top: '50px', opacity: '0' }).show(0).animate({ top: '0', opacity: '1' }, 200, 'swing', function () { $(this).css({ opacity: '' }); });
		$('.edsNiceButton').addClass('edsNiceButtonDisabled');
		$(btn).removeClass('edsNiceButtonDisabled');
		$('.announceBox').hide();
		if (window.history.replaceState != undefined) {
			if ($(btn).attr("href"))
				window.history.replaceState(null, null, $(btn).attr("href"));
			else
				window.history.replaceState(null, null, originalUrl);
		}
	};
	$('.cashBtn').click(function () {
		showCheckoutForm('#payByCash', this);
		return false;
	});
	$('.existingCardBtn').click(function () {
		showCheckoutForm('#payByExistingCard', this);
		return false;
	});
	$('.newCardBtn').click(function () {
		showCheckoutForm('#payByNewCard', this);
		return false;
	});
	$('.loginBtn').click(function () {
		showCheckoutForm('#login', this);
		return false;
	});
	$('.registerBtn').click(function () {
		showCheckoutForm('#register', this);
		return false;
	});
	$('.myaddressBtn').click(function () {
		showCheckoutForm('#myaddress', this);
		return false;
	});
	$('.otherAddressesBtn').click(function () {
		showCheckoutForm('#otheraddresses', this);
		return false;
	});

	$('.processOrder').click(function () {
		$(this).closest('form').hide();
		var span = $('.processingOrder').show();
		setTimeout(function () { span.text(span.text() + ".") }, 500);
		setTimeout(function () { span.text(span.text() + ".") }, 1500);
		setTimeout(function () { span.text(span.text() + ".") }, 3500);
		setTimeout(function () { span.text(span.text() + ".") }, 6000);
	});

	//make print buttons work
	jQuery('.printOrder').show().click(function () {
		window.print();
		return false;
	});

});

