﻿
jQuery(function() {

	//switch the password text field to a password field
	jQuery('.password dt a').click(function() {
		//we can't simply change the type from text to password, so we create and move value.
		var fld = jQuery(this).parents('dl').find('input');
		if (fld.attr('type') == 'text') {
			fld.after('<input type="password" name="' + fld.attr('name') + '" value="' + fld.val() + '" class="field" />').remove();
			jQuery(this).text('Show my password');
		}
		else {
			fld.after('<input type="text" name="' + fld.attr('name') + '" value="' + fld.val() + '" class="field" />').remove();
			jQuery(this).text('Hide my password');
		}
		bindValidationRules(jQuery(this).parents('dl'));
		return false;
	});

	//make delivery addresses that have delivery dates available show a popup
	jQuery('.haveDeliveryDates').click(function() {
		clickFire('/checkout_3_date.asp', function(p) {
			p.find('#deliveryDateList .more').hide();
			p.find('.moreDates').click(function() {
				jQuery('#deliveryDateList .more').show();
				jQuery(this).hide();
				return false;
			});
		});
		return false;
	});

	//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('.checkoutSectionShowHide').text('hide');
	//checkout collapsible blocks that don't have input fields / textareas / cv2 fields can be hidden by default.
	jQuery('.checkoutSectionShowHideBlock.hideDefault').hide().parent().find('.checkoutSectionShowHide').text('show');
	jQuery('.checkoutSectionShowHide').click(function() {
		var block = jQuery(this).parent().siblings('.checkoutSectionShowHideBlock');
		block.toggle();
		jQuery(this).text(block.is(':visible') ? 'hide' : 'show');
		return false;
	});

	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;
	});

});
