var cmbColoursId = "ctl00_cphMain_ctl00_cmbColours";
var cmbSizesId = "ctl00_cphMain_ctl00_cmbSizes";
var lblOldPriceId = "ctl00_cphMain_ctl00_lblOldPrice";
var lblPriceId = "ctl00_cphMain_ctl00_lblPrice";
var lblPriceIdZoom = "ucQuickZoom_ctl00_cphMain_ctl00_lblPrice";
var lblPriceDiscountZoomId = "ucQuickZoom_ctl00_cphMain_ctl00_lblDiscountedPrice";
var lblSingleSizeDescId = "ctl00_cphMain_ctl00_lblSingleSizeDesc";
var imgMainPhotoId = "ctl00_cphMain_ctl00_imgMainPhoto";
var hidSelectProductReferenceId = "ctl00_cphMain_ctl00_hidSelectProductReference";
var hidSingleSizeId = "ctl00_cphMain_ctl00_hidSingleSize";
var hidSizeId = "ctl00_cphMain_ctl00_hidSize";
var btnAddToCartId = "ctl00_cphMain_ctl00_btnAddToCart";
var txtQuantityId = "ctl00_cphMain_ctl00_txtQuantity";
var originalClickHandler = null;
var showAddToCartPopup = true;


function CProduct(reference, size, sizedesc, basketSize, colour, colourCode, salePriceBefore, salePriceAfter, currencySign, availability, availabilityCode, largeImage, hdImage, substitute, productID)
{
	this.reference = reference;
	this.size = size;
	this.sizedesc = sizedesc;
	this.basketSize = basketSize;
	this.colour = colour;
	this.colourCode = colourCode;
	this.salePriceBefore = salePriceBefore;
	this.salePriceAfter = salePriceAfter;
	this.currencySign = currencySign;
	this.availability = availability;
	this.availabilityCode = availabilityCode;
	this.largeImage = largeImage;
	this.hdImage = hdImage;
	this.substitute = substitute;
	this.productID = productID;
}


function CProductCarousel(id, image, title, price, link, currency)
{
	this.id = id;
	this.image = image;
	this.title = title;
	this.price = price;
	this.link = link;
	this.currency = currency;
}


function CProductAdditional(id, colour, smallImage, largeImage, mediaPath)
{
	this.id = id;
	this.colour = colour;
	this.smallImage = smallImage;
	this.largeImage = largeImage;
	this.mediaPath = mediaPath;
}


function ShowLargePhoto(imgID, imagePath, filename)
{

	// Loop ALL products so as to change the colour drop down
	for ( i = 0; i < productsArray.length; i++)
	{

		// Get Product
		product = productsArray[i];
		cmbColour = document.getElementById(cmbColoursId);
				
		if ( product.largeImage == filename )
		{
		
			selectedColour = product.colour;
		
			// Loop ALL colours to select the one
			for ( i = 0; i < cmbColour.options.length; i++)
			{	
				if ( cmbColour.options[i].value == product.colour )
				{
					cmbColour.selectedIndex = i;
					break;
				}
			}				
		
			break;
		}
		
	}

	$("#" + imgID).ImageSwitch({Type:"FadeIn", Speed:1500, StartLeft: 150, NewImage:imagePath}); 
	SizeChanged(cmbSize.options[0].value, selectedColour);
	
}


function ShowProductDescription()
{

	// Shall "read more" be displayed?
	if ( isLong == "1" )
		document.getElementById("divDescMore").style.display = "block";	
	else
		document.getElementById("divDescMore").style.display = "none";

	// Show the description
	document.getElementById("divShortProductDescription").innerText = shortDescription;
	document.getElementById("divLongProductDescription").innerText = longDescription;
	document.getElementById("divShortProductDescription").textContent = shortDescription;
	document.getElementById("divLongProductDescription").textContent = longDescription;

	document.getElementById("divLongProductDescription").style.display = "none";
	
}


function ShowLongDescription()
{

	// Reset font
	$('#divShortProductDescription').css('font-size', 11);
	$('#divLongProductDescription').css('font-size', 11);

	// Show long description
	if ( document.getElementById("divLongProductDescription").style.display == "none" )
	{
		$("#divDesc").animate({"height": "300px"}, "slow");
		document.getElementById("divShortProductDescription").style.display = "none";
		$("#divLongProductDescription").fadeIn(100);
	}

	// Show short description
	else
	{
		$("#divDesc").animate({"height": "150px"}, "slow");
		document.getElementById("divLongProductDescription").style.display = "none";
		$("#divShortProductDescription").fadeIn(100);
	}

}


function IncreaseFont()
{
	var currentFontSize = $('#divShortProductDescription').css('font-size');
	var currentFontSizeNum = parseFloat(currentFontSize, 10);
	var newFontSize = currentFontSizeNum*1.2;
	$('#divShortProductDescription').css('font-size', newFontSize);
	$('#divLongProductDescription').css('font-size', newFontSize);

	var currentHeight = $('#divDesc').css('height');
	var currentHeightNum = currentHeight.substring(0, 3);
	var newHeight = currentHeightNum*1.3;
	$("#divDesc").animate({"height": parseInt(newHeight) + "px"}, "slow");
}


function DecreaseFont()
{
	
	var currentFontSize = $('#divShortProductDescription').css('font-size');
	var currentFontSizeNum = parseFloat(currentFontSize, 10);
	
	// Font cannot be less than 11px
	if ( (currentFontSizeNum != 10) && (currentFontSizeNum != 11) )
	{	
		var newFontSize = currentFontSizeNum/1.2;
		$('#divShortProductDescription').css('font-size', newFontSize);
		$('#divLongProductDescription').css('font-size', newFontSize);

		var currentHeight = $('#divDesc').css('height');
		var currentHeightNum = currentHeight.substring(0, 3);
		var newHeight = currentHeightNum/1.3;
		$("#divDesc").animate({"height": parseInt(newHeight) + "px"}, "slow");
	}
	
}


function ShowHDZoom(productTitleFormatted, popUpWidth, popUpHeight)
{

	cmbColour = document.getElementById(cmbColoursId);
	var ctl00_cphMain_ctl00_hid400Path = GetElement("input", "ctl00_cphMain_ctl00_hid400Path");
	
	for ( i=0; i < productsArray.length; i++)
	{
	
		product = productsArray[i];
	
		if ( product.colour == cmbColour.value )
		{
			hdImagePath = ctl00_cphMain_ctl00_hid400Path.value + product.hdImage;
			break;
		}
		
	}

	url = "/popups/productZoom.aspx?productTitle=" + productTitleFormatted + "&productImage=" + hdImagePath;
	name = 'popup';
	openPopUp(url, name, popUpWidth, popUpHeight);
	
}


function GetHDZoomImage()
{

	cmbColour = document.getElementById(cmbColoursId);    
    var ctl00_cphMain_ctl00_hid400Path = GetElement("input", "ctl00_cphMain_ctl00_hid400Path");

	for ( i=0; i < productsArray.length; i++)
	{
	
		product = productsArray[i];
	
		if ( product.colour == cmbColour.value )
			return ctl00_cphMain_ctl00_hid400Path.value + product.hdImage;
		
	}
	
}


function RemoveTagAndShowZoom()
{
	document.getElementById('ctl00_cphMain_ctl00_tableTag4to11').style.zIndex = -5;
}


function RemoveZoomAndShowTag()
{
	document.getElementById('ctl00_cphMain_ctl00_tableTag4to11').style.zIndex = 5;
}


function CarouselAnimate(how)
{

	if ( carouselArray.length != 0 )
	{

		// Show previos image
		if ( how == "previous" )
		{
			if ( carouselIndex == 0 )
				carouselIndex = carouselLength-1;
			else
				carouselIndex--;		
		}	

		// Show next image
		else
		{
			if ( (carouselIndex+1) == carouselLength )
				carouselIndex = 0;
			else
				carouselIndex++;
		}

		// Get data members from object
		var nextImage = carouselArray[carouselIndex].image;
		var link = carouselArray[carouselIndex].link;
		var title = carouselArray[carouselIndex].title;
		var price = carouselArray[carouselIndex].price;
		var currency = carouselArray[carouselIndex].currency;
		
		// Show Image
		$("#imgCarousel").ImageSwitch({Type:"ScrollIn", Speed:1350, NewImage:nextImage}); 
		$("#aCarousel").attr("href", link);
		$("#aCarouselTitle").attr("href", link);
		$("#aCarousel").attr("alt", title);
		$("#aCarousel").attr("title", title);
		$("#divCarouselTitle").text(title);
		$("#divCarouselPrice").text(price + ' ' + currency);
		
	}
	
}


function ShowCarouselFirstProduct()
{

	carouselIndex = 0;
	
	try
	{
	
		if ( carouselArray.length != 0 )
		{

			var nextImage = carouselArray[0].image;
			var link = carouselArray[0].link;
			var title = carouselArray[0].title;
			var price = carouselArray[0].price;
			var currency = carouselArray[0].currency;
			
			$("#imgCarousel").attr("src", nextImage);
			$("#aCarousel").attr("href", link);
			$("#aCarouselTitle").attr("href", link);
			$("#aCarousel").attr("alt", title);
			$("#aCarousel").attr("title", title);
			$("#divCarouselTitle").text(title);
			$("#divCarouselPrice").text(price + ' ' + currency);
		
		}
		
	}
	
	catch(err)
	{
	}

}


function ChangeTab(whichOne)
{

	//Switch them all of
	$("#imgTab1").attr("src", "/productDetails/images/tabs/tab1Off_" + language + ".gif");
	$("#imgTab2").attr("src", "/productDetails/images/tabs/tab2Off_" + language + ".gif");
	$("#imgTab3").attr("src", "/productDetails/images/tabs/tab3Off_" + language + ".gif");
	
	if ( whichOne == 0 )
		$("#imgTab1").attr("src", "/productDetails/images/tabs/tab1On_" + language + ".gif");
	else if ( whichOne == 1 )
		$("#imgTab2").attr("src", "/productDetails/images/tabs/tab2On_" + language + ".gif");
	else if ( whichOne == 2 )
		$("#imgTab3").attr("src", "/productDetails/images/tabs/tab3On_" + language + ".gif");

}


function ShowSizesOfFirstProduct()
{
    
	var ctl00_cphMain_ctl00_cmbColours = GetElement("select", cmbColoursId);
	
	if( ctl00_cphMain_ctl00_cmbColours != null)
	{
		ctl00_cphMain_ctl00_cmbColours.selectedIndex = 0;
		ColourChanged(cmbSizesId, ctl00_cphMain_ctl00_cmbColours.value);
		setTimeout("ColourChanged('"+cmbSizesId+"', '" + ctl00_cphMain_ctl00_cmbColours.value + "');",100)
	}

}


var productsArray;

function ShowSizesOfFirstProduct2(prodsArray)
{
    
	productsArray = prodsArray;
	var ctl00_cphMain_ctl00_cmbColours = GetElement("select", cmbColoursId);

	if(ctl00_cphMain_ctl00_cmbColours!=null)
	{
		ctl00_cphMain_ctl00_cmbColours.selectedIndex = 0;
		ColourChanged(GetElement("select",cmbSizesId).id, ctl00_cphMain_ctl00_cmbColours.value);
		setTimeout("ColourChanged('"+GetElement("select",cmbSizesId).id+"', '"+ctl00_cphMain_ctl00_cmbColours.value+"');",100)
	}

}


function CreateAllAdditionalPhotos() 
{
    var addPhotosZoom = document.createElement("div");
    for (var i = 0; i < additionalArray.length; i++) {
        var addDiv = document.createElement("div");
        var addImage = document.createElement("img");
        $(addImage).attr("src", additionalArray[i]["smallImage"]);
        $(addImage).attr("filename", additionalArray[i]["largeImage"]);
        $(addImage).attr("order", i);
        $(addImage).click
	    (
		    function() {
		        ShowLargePhoto(imgMainPhotoId, additionalArray[parseInt($(this).attr("order"))]["mediaPath"] + additionalArray[parseInt($(this).attr("order"))]["largeImage"], additionalArray[parseInt($(this).attr("order"))]["largeImage"]); return false;
		    }
	    );
		$(addDiv).append(addImage);
		$(addPhotosZoom).append(addDiv);
    }

    return addPhotosZoom;
}


function ShowAdditionalPhotos()
{

	$("#imgAdditional1").attr("src", additionalArray[additionalIndex]["smallImage"]);
	
	$("#imgAdditional1").click
	(
		function()
		{
			ShowLargePhoto(imgMainPhotoId, additionalArray[additionalIndex]["mediaPath"] + additionalArray[additionalIndex]["largeImage"], additionalArray[additionalIndex]["largeImage"]); return false;
		}
	)
	
	$("#imgAdditional2").attr("src", additionalArray[parseInt(additionalIndex)+1]["smallImage"]);

	$("#imgAdditional2").click
	(
		function()
		{
			ShowLargePhoto(imgMainPhotoId, additionalArray[parseInt(additionalIndex)+1]["mediaPath"] + additionalArray[parseInt(additionalIndex)+1]["largeImage"], additionalArray[parseInt(additionalIndex)+1]["largeImage"]); return false;
		}
	)

	$("#imgAdditional3").attr("src", additionalArray[parseInt(additionalIndex)+2]["smallImage"]);

	$("#imgAdditional3").click
	(
		function()
		{
			ShowLargePhoto(imgMainPhotoId, additionalArray[parseInt(additionalIndex)+2]["mediaPath"] + additionalArray[parseInt(additionalIndex)+2]["largeImage"], additionalArray[parseInt(additionalIndex)+2]["largeImage"]); return false;
		}
	)

	$("#imgAdditional4").attr("src", additionalArray[parseInt(additionalIndex)+3]["smallImage"]);

	$("#imgAdditional4").click
	(
		function()
		{
			ShowLargePhoto(imgMainPhotoId, additionalArray[parseInt(additionalIndex)+3]["mediaPath"] + additionalArray[parseInt(additionalIndex)+3]["largeImage"], additionalArray[parseInt(additionalIndex)+3]["largeImage"]); return false;
		}
	)

}


function ShowAdditionalPrevious()
{
	if ( additionalIndex != 0 )
	{
		additionalIndex--;
		ShowAdditionalPhotos();
	}
}


function ShowAdditionalNext()
{
	if ( (additionalLength-additionalIndex) > 3 )
		additionalIndex++;
	else
		additionalIndex = 0;
		
	ShowAdditionalPhotos();
}


function GetElement(tagName,id)
{
	
	var elements = document.getElementsByTagName(tagName);
	
	var i =0;
	
	for(i=0;i<elements.length;i++)
	{        
		var element = elements[i];
	
		if(element.id.match(id+"$"))
			return element;
	}

	return null;

}


function ColourChanged(cmbSizeClientID, selectedColour)
{

	// Make sure to show the add to cart button in Quick Zoom if the out of stock message is currently shown
	if ( GetElement("span",lblPriceIdZoom) != null )
	{
		document.getElementById("tdPriceCart").style.display = 'block';
		document.getElementById("divOutOfStock").style.display = 'none';
	}
     
	if ( productsArray == null )
		return;

	// Get the Sizes DropDownList
	cmbSize = document.getElementById(cmbSizeClientID);
	next = 0;
	
	// It's not null if this is NOT a one-sized product
	if ( cmbSize != null )
	{
	
		// Save the currently selected sizes so as we re-choose it later on
		previouslySelectedSize = cmbSize.value;

		// Clear the sizes DropDownList
		cmbSize.options.length = 0;
	
		// Loop ALL size+colour products
		for ( i = 0; i < productsArray.length; i++)
		{

			// Get Product
			product = productsArray[i];
                
			if ( i == 0 )
				firstSize = product.size;

			// The colour of THIS product is the selected colour on the page
			if ( product.colour == selectedColour )
			{
			    
				// Create size item
				var newOption = new Option(product.sizedesc, product.size);
				                    
				// Add size item to sizes DropDownList
				cmbSize.options[next] = newOption;
								
				// If the size is the one which was selected when the colour changed, select it for the customer
				if ( cmbSize.options[next].value == previouslySelectedSize )
				{
					cmbSize.selectedIndex = next;
					firstSize = previouslySelectedSize;
				}
				
				// Get Path of the image to show
				next++;
				var ctl00_cphMain_ctl00_hid400Path = GetElement("input", "ctl00_cphMain_ctl00_hid400Path");				
				newImage = ctl00_cphMain_ctl00_hid400Path.value + product.largeImage;
				
			}

		}
		
		// Show Image
		//$("#ctl00_cphMain_ctl00_divMainPhoto").ImageSwitch({Type:"FlyBG", Speed:1500, StartLeft: 150, DivID:"ctl00_cphMain_ctl00_divMainPhoto", NewImage:newImage}); 
		$("#" + imgMainPhotoId).attr("src", newImage);
		
		// Let's say colour "Red" and size 038 are chosen and then I choose colour "Blue" which DOES
		// not have colour 038 as part of its selection. This means that SizeChanged has to be called
		// with cmbSize.options[0].value so as the size of index 0 is selected automatically.
		//
		// But, if both colours have 038, then I will call SizeChanged by passing the value of firstSize,
		// which was initialized above, so as size 038 is kept as selection, even after changing colour.
		firstSizeIsActive = false;
		
		// Loop ALL sizes to determine whether size is part of the new colour
		for ( i = 0; i < cmbSize.options.length; i++)
		{	
			if ( cmbSize.options[i].value == firstSize )
			{
				firstSizeIsActive = true;
				break;
			}
		}		
		
		// Decide how to call SizeChanged
		if ( firstSizeIsActive )		
			SizeChanged(firstSize, selectedColour);
		else if(cmbSize.options[0] != null)
			SizeChanged(cmbSize.options[0].value, selectedColour);

	}

	// Since this is a one-size item, we cannot call SizeChanged because the size DropDownList is not available
	else
	{
	
		if ( (GetElement("input","ctl00_cphMain_ctl00_btnAddToCart").onclick != ShowOutOfStockPopUp) && (GetElement("input","ctl00_cphMain_ctl00_btnAddToCart").onclick != ShowOutOfStockForQuickZoom) )
			originalClickHandler = GetElement("input","ctl00_cphMain_ctl00_btnAddToCart").onclick;
	
		for ( i=0; i < productsArray.length; i++)
		{
		
			product = productsArray[i];
		
			if ( product.colour == selectedColour )
			{

				product = productsArray[i];
				
				// There is a substitute
				if (product.substitute != " ")
				{
					$("#imgAvailability").attr("src", "/productDetails/images/substitute_" + language + ".jpg");
					$("#aSubstitution").attr("href", "/productDetails/productSubstitution.aspx?ref=" + product.reference + "&size=" + product.basketSize + "&pid=" + product.productID);
					$("#aSubstitution").attr("style", "cursor: pointer");
				}
			
				// Delayed
				else if (product.availabilityCode == "D")
					$("#imgAvailability").attr("src", "/productDetails/images/delayed_" + language + ".jpg");			
			
				// Is product out of stock?
				else if ( (product.availabilityCode == "A") ||
					(product.availabilityCode == "AC") ||
					(product.availabilityCode == "AL") ||
					(product.availabilityCode == "AP") ||
					(product.availabilityCode == "AR") ||
					(product.availabilityCode == "C") ||
					(product.availabilityCode == "CA") ||
					(product.availabilityCode == "CC") ||
					(product.availabilityCode == "E") ||
					(product.availabilityCode == "EA") ||
					(product.availabilityCode == "EE") ||
					(product.availabilityCode == "EL") ||
					(product.availabilityCode == "RF") ||
					(product.availabilityCode == "RT") ||
					(product.availabilityCode == "SM") )
				{				    
					
					// No PopUp for Quick Zoom, so I'm using the below ID to check whether we're in the Quick Zoom or SPP
					if ( GetElement("span",lblPriceIdZoom) == null )
						GetElement("input","ctl00_cphMain_ctl00_btnAddToCart").onclick = ShowOutOfStockPopUp;
					else
						GetElement("input","ctl00_cphMain_ctl00_btnAddToCart").onclick = ShowOutOfStockForQuickZoom;				
					
					$("#imgAvailability").attr("src", "/productDetails/images/outOfStock_" + language + ".jpg");

				}
					
				// Product is available
				else
				{
					$("#imgAvailability").attr("src", "/productDetails/images/inStock_" + language + ".jpg");
					GetElement("input",btnAddToCartId).onclick = originalClickHandler;
				}
				
				GetElement("span",lblPriceId).innerText = product.salePriceAfter + ' ' + product.currencySign;
				GetElement("span",lblPriceId).textContent = product.salePriceAfter + ' ' + product.currencySign;
				
				if ( GetElement("span",lblPriceIdZoom) != null )
				{
					GetElement("span",lblPriceIdZoom).innerText = product.salePriceBefore + ' ' + product.currencySign;
					GetElement("span",lblPriceIdZoom).textContent = product.salePriceBefore + ' ' + product.currencySign;
				}
				
				break;
				
			}
			
		}

	}
	
}

function SizeChanged(selectedSize, selectedColour)
{

	// Make sure to show the add to cart button in Quick Zoom if the out of stock message is currently shown
	if ( GetElement("span",lblPriceIdZoom) != null )
	{
		document.getElementById("tdPriceCart").style.display = 'block';
		document.getElementById("divOutOfStock").style.display = 'none';
	}

	$("#aSubstitution").attr("style", "cursor: default");
	$("#aSubstitution").removeAttr("href");

	if ( (GetElement("input","ctl00_cphMain_ctl00_btnAddToCart").onclick != ShowOutOfStockPopUp) && (GetElement("input","ctl00_cphMain_ctl00_btnAddToCart").onclick != ShowOutOfStockForQuickZoom) )
		originalClickHandler = GetElement("input","ctl00_cphMain_ctl00_btnAddToCart").onclick;

	for ( i=0; i < productsArray.length; i++)
	{
		
		product = productsArray[i];
		
		if ( document.getElementById(lblOldPriceId) != null )
		{		
			document.getElementById(lblOldPriceId).innerText = product.salePriceBefore;
			document.getElementById(lblOldPriceId).textContent = product.salePriceBefore;
		}
		
		if ( (product.colour == selectedColour) && (product.size == selectedSize) )
		{
		
			// Discount in Quick Zoom
			if ( document.getElementById(lblPriceDiscountZoomId) != null )
			{		
				document.getElementById(lblPriceDiscountZoomId).innerText = product.salePriceAfter + ' ' + product.currencySign;
				document.getElementById(lblPriceDiscountZoomId).textContent = product.salePriceAfter + ' ' + product.currencySign;
			}
			
			// There is a substitute
			if (product.substitute != " ")
			{
				$("#imgAvailability").attr("src", "/productDetails/images/substitute_" + language + ".jpg");
				$("#aSubstitution").attr("href", "/productDetails/productSubstitution.aspx?ref=" + product.reference + "&size=" + product.basketSize + "&pid=" + product.productID);
				$("#aSubstitution").attr("style", "cursor: pointer");
			}

			// Delayed
			else if (product.availabilityCode == "D")
				$("#imgAvailability").attr("src", "/productDetails/images/delayed_" + language + ".jpg");			
		
			// Is product out of stock?
			else if ( (product.availabilityCode == "A") ||
			     (product.availabilityCode == "AC") ||
			     (product.availabilityCode == "AL") ||
			     (product.availabilityCode == "AP") ||
			     (product.availabilityCode == "AR") ||
			     (product.availabilityCode == "C") ||
			     (product.availabilityCode == "CA") ||
			     (product.availabilityCode == "CC") ||
			     (product.availabilityCode == "E") ||
			     (product.availabilityCode == "EA") ||
			     (product.availabilityCode == "EE") ||
			     (product.availabilityCode == "EL") ||
			     (product.availabilityCode == "RF") ||
			     (product.availabilityCode == "RT") ||
			     (product.availabilityCode == "SM") )
			{	
			
				// No PopUp for Quick Zoom, so I'm using the below ID to check whether we're in the Quick Zoom or SPP
				if ( GetElement("span",lblPriceIdZoom) == null )
					GetElement("input","ctl00_cphMain_ctl00_btnAddToCart").onclick = ShowOutOfStockPopUp;
				else
					GetElement("input","ctl00_cphMain_ctl00_btnAddToCart").onclick = ShowOutOfStockForQuickZoom;				
				
				$("#imgAvailability").attr("src", "/productDetails/images/outOfStock_" + language + ".jpg");
				
			}

			// Product is available
			else
			{
				$("#imgAvailability").attr("src", "/productDetails/images/inStock_" + language + ".jpg");
				GetElement("input",btnAddToCartId).onclick = originalClickHandler;
			}
			
			GetElement("span",lblPriceId).innerText = product.salePriceAfter + ' ' + product.currencySign;
			GetElement("span",lblPriceId).textContent = product.salePriceAfter + ' ' + product.currencySign;
			
			if ( GetElement("span",lblPriceIdZoom) != null )
			{
				GetElement("span",lblPriceIdZoom).innerText = product.salePriceBefore + ' ' + product.currencySign;
				GetElement("span",lblPriceIdZoom).textContent = product.salePriceBefore + ' ' + product.currencySign;
			}
			
			break;
			
		}

	}

}


function AddQuantity(txtQuantity)
{
	oldQuantity = $("#" + txtQuantity).val();
	$("#" + txtQuantity).val(++oldQuantity);
}


function DecreaseQuantity(txtQuantity)
{
	oldQuantity = $("#" + txtQuantity).val();
	
	if ( oldQuantity != 1 )
		$("#" + txtQuantity).val(--oldQuantity);
}


function AddItemToCart(selectedSize, selectedColour)
{

	// Get the Sizes DropDownList
	cmbSize = GetElement('select',selectedSize);
    
	// Loop ALL items
	for ( i=0; i < productsArray.length; i++)
	{

		// Get item
		product = productsArray[i];

		// Sizes DropDownList is null if this is a one-size product
		if ( cmbSize != null )
		{

			// Is this the product we need?
			if ( (product.colour == selectedColour) && (product.size == cmbSize.value) )
			{
				GetElement("input",hidSelectProductReferenceId).value = product.reference;
				GetElement("input",hidSizeId).value = product.size;
				break;
			}

		}
		else
		{
		
			// Is this the product we need?
			if ( product.colour == selectedColour )
			{
				GetElement("input",hidSelectProductReferenceId).value = product.reference;
				GetElement("input",hidSizeId).value = product.size;
				break;
			}

		}

	}

}


function ShowOutOfStockPopUp()
{
	if(dhtmlmodal!=null)
	{
		outOfStockWindow = dhtmlmodal.open("OutOfStock", "iframe", "/popups/outOfStock.aspx", "", "width=400px,height=125px,center=1,resize=0,scrolling=1");
	}
	return false;
}


function ShowOutOfStockForQuickZoom()
{
	document.getElementById("tdPriceCart").style.display = 'none';
	document.getElementById("divOutOfStock").style.display = 'block';
	return false;	
}


function ChangeSizeColourViaAvailabilityGrid(selectedSize, selectedColour)
{

	// Get the Sizes DropDownList
	cmbSize = document.getElementById(cmbSizesId);

	// Loop sizes to find the one selected from the availability grid
	for ( i=0; i < cmbSize.options.length; i++)
	{	
		if ( cmbSize.options[i].value == selectedSize )
		{
			cmbSize.selectedIndex = i;
			break;
		}
	}

	// Get the Colours DropDownList
	cmbColour = document.getElementById(cmbColoursId);

	// Loop colours to find the one selected from the availability grid
	for ( i=0; i < cmbColour.options.length; i++)
	{	
		if ( cmbColour.options[i].value == selectedColour )
		{
			cmbColour.selectedIndex = i;
			break;
		}
	}
	
	// Select size + colour combination
	ColourChanged(cmbSizesId, selectedColour);

}


function GetAvailabilityGridLeft()
{

	if ( screen.width == 1024 )
		return "85";
	else if ( screen.width == 1152 )
		return "150";
	else if ( screen.width == 1280 )
		return "205";
	else
		return "85";
	
}


//-- Global variable for the "Add To Cart Modal Dialog" --//
var modalPopup;
var othersBoughtSection = false;

if(document.getElementById("ctl00_cphMain_ctl00_addToBasketDialog_divOthersBoughtSection")!=null)
	othersBoughtSection=true;

//-- Adds the product to the cart via an AJAX call, and displays the modal popup --//
function CallAddProductToCart(productId,documentId)
{    
    
	//-- Get the product info from elements on the screen --//
	var productReference = document.getElementById(hidSelectProductReferenceId).value;
	var size="";    

	//-- If the product is a single-size product... --//
	if ( document.getElementById(lblSingleSizeDescId) != null )
	   size = document.getElementById(hidSingleSizeId).value;
	else
	   size = document.getElementById(cmbSizesId).value;

	var quantity = document.getElementById(txtQuantityId).value;
	var colour = document.getElementById(cmbColoursId).options[document.getElementById(cmbColoursId).selectedIndex].value;
	var price = document.getElementById(lblPriceId).innerHTML;

	//-- Construct URL for AJAX request --//
	var url="/productDetails/addToCart.aspx?productID="+productId+"&documentID="+documentId+"&productReference="+productReference+"&size="+size+"&quantity="+quantity+"&colour="+colour+"&rand=" + Math.random();
	var req = getAjaxRequest(); //-- Defined in master.js --//

	//-- Make a Synchronous request --//
	req.open("GET", url, false);
	req.send(null);
	var serverResponse = req.responseText;  
	 
	//-- Get the XML response from the server --//
	var xml = req.responseXML;

	//-- Parse the XML to get more information --//
	var noOfItemsInCart = xml.getElementsByTagName("NoOfItemsInCart")[0].firstChild.nodeValue;
	var title= xml.getElementsByTagName("LastProductTitle")[0].firstChild.nodeValue;
	var image = xml.getElementsByTagName("LastProductImage")[0].firstChild.nodeValue;

	//-- Update the No of Items in Cart (top of page) --//
	var spanItemsInCart = document.getElementById('spanItemsInCart');
	if(spanItemsInCart==null)
	    spanItemsInCart = parent.document.getElementById('spanItemsInCart');
	
	spanItemsInCart.innerHTML = noOfItemsInCart;

    if(showAddToCartPopup==true)
    {        
	    //-- Check if the browser is IE --//
	    var IE = false;

	    if(window.ActiveXObject)
	       IE = true;        
    	   
	    //-- Set the modal size --//
	    var modalWidth="554px"
	    var modalHeightAddition=0;
         
	    //-- Perform IE-specific tasks --//
	    if(IE==true)
	    {
	       //-- IE requires a different modal size --//
	       modalWidth="540px";
	       modalHeightAddition = 14;
    	   
	       //-- Hide all the combo boxes, since otherwise they will be displayed on top of the modal in IE 6 and below --//
	       var combos = document.getElementsByTagName("select");
	       for(var i=0;i<combos.length;i++)
	       {               
		      if(combos[i].style.visibility=="visible" || combos[i].style.visibility=="")
		      {         
			     combos[i].style.visibility="hidden";
			     combos[i].setAttribute("autohidden","1");
		      }
	       }
	    }

	    var modalHeight=(240+modalHeightAddition).toString()+"px";

	    //-- The modal must have a larger height if the "others bought" section is visible --//
	    if(othersBoughtSection || document.getElementById("ctl00_cphMain_ctl00_addToBasketDialog_divOthersBoughtSection")!=null)
	    {
	        modalHeight=(477+modalHeightAddition).toString()+"px";
	        othersBoughtSection=true;
	    }
        
	    //-- Show the modal --//    
	    modalPopup=dhtmlmodal.open("addToBasketDialogModal", "div", "divAddToBasketDialog", "","width="+modalWidth+",height="+modalHeight+",center=1,scrolling=0,resize=0");
        
	    //-- Populate the Add To Basket Dialog (after the modal is shown, otherwise elements are not available) --//
	    document.getElementById('addToBasketProductId').innerHTML = productReference;
	    document.getElementById('addToBasketDialogSize').innerHTML = size;
	    document.getElementById('addToBasketDialogColour').innerHTML = colour;    
	    document.getElementById('addToBasketDialogQuantity').innerHTML = quantity;    
	    document.getElementById('ctl00_cphMain_ctl00_addToBasketDialog_imgProduct').src = image;
	    document.getElementById('addToBasketDialogPrice').innerHTML = price;
	}
    
}


function CloseModalPopup()
{
    modalPopup.hide();
   
    //-- Re-enable combos in IE --//
    if(window.ActiveXObject)
    {
        var combos = document.getElementsByTagName("select");
        for(var i=0;i<combos.length;i++)
        {               
            if(combos[i].style.visibility=="hidden"  && combos[i].attributes["autohidden"]!=null)
            {         
                combos[i].style.visibility="";
                combos[i].removeAttribute("autohidden");
            }
        }    
    }
    
}
