
jQuery(document).ready(function(){
	/* Preload images for main menu. */
	jQuery.preloadImages("/Images/website/menu-hover-home.gif","/Images/website/menu-hover-delivery.gif","/Images/website/menu-hover-about.gif","/Images/website/menu-hover-contact.gif","/Images/website/menu-hover-british.gif");
	
	/* Hide empty "no sub-catalogs" and "no products" messages. */
	jQuery('.catalogueTable .catalogueItemNotFound').closest('table').addClass('HideTable');
	jQuery('.productTable .productItemNotFound').closest('table').addClass('HideTable');
	
	/* Style ecommerce breadcrumbs. */
	var ecombreadcrumbs = document.getElementById('EComBreadCrumbs');
	if( ecombreadcrumbs ){
		ecombreadcrumbs.innerHTML = ecombreadcrumbs.innerHTML.replace(/:/g,"&raquo;");
		ecombreadcrumbs.innerHTML = ecombreadcrumbs.innerHTML.replace(/;/g,":");
	}
	
	/* Link in catalog images. */
	jQuery('.ShopCatalogLayout').each(function() {
		jQuery('.SCLink', this).attr('href', jQuery('.SCName a', this).attr('href'));
	});
	
	/* Set main image in large product layout lightbox link value. */
	jQuery('#LPImage a').attr('href',jQuery('#LPImage img').attr('src'));
	
	/* Hide/Show "Add To Cart" form. */
	jQuery('#LPAddToCart').hide();
	jQuery('#ButtonAddToCart').click(function(){
		jQuery('#LPAddToCart').toggle(400);
		return false;
	});
	
	/* Add date picker to "Add To Cart" form. */
	jQuery.datepick.setDefaults({dateFormat: 'dd/mm/yy'});
	jQuery('#RecipientDate').datepick({minDate: +1, maxDate: +400});
	jQuery('#RecipientDate').datepick('option', 'dateFormat', 'dd/mm/yy');
	
	/* Add plus sign to Product Attribute prices. */
	

	/* Pre-select shipping option. 
	var shippingOptions = document.getElementById('ShippingOptions');
	if( shippingOptions ){
		shippingOptions.selectedIndex = 1;
		shippingOptions.onchange();
	} */
});

/* jQuery plugin to preload images */
jQuery.preloadImages = function(){
	for(var i = 0; i<arguments.length; i++){
		jQuery("<img>").attr("src", arguments[i]);
	}
}

/**
*	function customAddToCart(catalogID,purchaseAction)
*	catalogID: ID of current catalog that the product is in.
*	purchaseAction: true = Buy Now, false = Add to Cart. 
**/
function customAddToCart(catalogID,purchaseAction){
	var productDetails = collectAddress();
	if( productDetails != false ){
		if( jQuery('#HideCaptureDetails textarea.productTextarea').length > 0 ){
			jQuery('#HideCaptureDetails textarea.productTextarea').val(productDetails);
			var productTextareaID = jQuery('#HideCaptureDetails textarea.productTextarea').attr('id');
			var productID = (productTextareaID.split('_'))[1];
			AddToCart(catalogID,productID,'',4,purchaseAction); 
			return false;
		}else{
			alert('Error [Capture Details]\n' + 
				  'You are unable to purchase this product due to a system error.\n ' + 
				  'Please notify us of this error and the product in question.');
			return false;
		}
	}else{
		return false;
	}
}

function collectAddress(){
	var recipientName = document.getElementById("RecipientName").value;
	var recipientAddress = document.getElementById("RecipientAddress").value;
	var recipientCity = document.getElementById("RecipientCity").value;
	var recipientState = document.getElementById("RecipientState").value;
	var recipientPostcode = document.getElementById("RecipientPostcode").value;
	var recipientCountry = document.getElementById("RecipientCountry").value;
	var recipientPhone = document.getElementById("RecipientPhone").value;
	var recipientMessage = document.getElementById("RecipientMessage").value;
	var recipientDate = document.getElementById("RecipientDate").value;
	var recipientInstructions = document.getElementById("RecipientInstructions").value;
	var recipientComments = document.getElementById("RecipientComments").value;
	
	
	var formAlert = "";
	if(recipientName == ""){formAlert += "Please enter Recipient Name\n";}
	if(recipientAddress == ""){formAlert += "Please enter Address\n";}
	if(recipientCity == ""){formAlert += "Please enter City\n";}
	if(recipientState == ""){formAlert += "Please enter State\n";}
	if(recipientPostcode == ""){formAlert += "Please enter Postcode\n";}
	if(recipientCountry == ""){formAlert += "Please select Country\n";}
	if(recipientPhone == ""){formAlert += "Please enter Phone Number\n";}
	if(recipientMessage == ""){formAlert += "Please enter Gift Card Message\n";}
	if(recipientDate == ""){formAlert += "Please enter Preferred Delivery Date\n";}
	
	
	if(formAlert != ""){
		alert(formAlert);
		return false;
	}else{
		var collectedInfo = recipientName + "; " + recipientAddress + "; " + recipientCity + "; " + recipientState + "; " + recipientPostcode + "; " + recipientCountry + "; " + recipientPhone + "; " + recipientMessage + "; " + recipientDate + "; " + recipientInstructions + "; " + recipientComments;
		return collectedInfo;
	}
}

function showCreditCardDetails(val){							
	if (!document.getElementById('CreditCardDetails')){
		return;
	}
	if (val != 1) {
		document.getElementById('CreditCardDetails').style.display = 'none';				
	}else{
		document.getElementById('CreditCardDetails').style.display = 'inline';
	}
}	
