
function drpdwnColourChange(ClientId, currentArray, colourThumbsArray, productMode) {
	var drpdwnColour = document.getElementById(ClientId + '_drpdwnColour');
	var drpdwnSize = document.getElementById(ClientId + '_drpdwnSize');
	var drpdwnindex = drpdwnColour.selectedIndex;
	var SelectedColour = drpdwnColour.options[drpdwnindex].value;
	var ChkPurchase = document.getElementById(ClientId + '_chkPurchase');

	if (drpdwnindex != 0) {
		if (productMode == 0) {
			LoadTmpHolder(drpdwnColour.options[drpdwnindex].value, arrMainImage, arrThumbImage, '');
		}
		else {
			LoadTmpHolder(drpdwnColour.options[drpdwnindex].value, arrMainImage, arrThumbImage, '_popup');
		}
		if (productMode != 1) {
			PopulateImages('');
		}
	}
	else {
		if (ChkPurchase) {
			ChkPurchase.disabled = true;
			ChkPurchase.parentNode.disabled = true;
		}
	}
	PopulateDropDowns(ClientId, currentArray);
	showSelectedColourImage(ClientId, drpdwnColour, colourThumbsArray);
	HideVideo();
}

function showSelectedColourImage(id, currentColourDDL, colourThumbsArray) {
	var image = document.getElementById(id + '_imgMainImage');
	if (image != null) {
		var selectedColourIndex = currentColourDDL.selectedIndex;
		var selectedColourValue = currentColourDDL.options[selectedColourIndex].value;
		for (var i = 0; i < colourThumbsArray.length; i++) {
			if (colourThumbsArray[i][3] == selectedColourValue) {
				image.src = colourThumbsArray[i][0];
			}
		}
	}
}

var selectedSizeIndex;
function drpdwnSizeChange(drpdwnSize, clientId, currentArray) {
	var SizeIndex = drpdwnSize.selectedIndex;
	var SizeId = drpdwnSize.options[SizeIndex].value;
	var ColourIndex = document.getElementById(clientId + '_drpdwnColour').selectedIndex;
	var Colour = document.getElementById(clientId + '_drpdwnColour').options[ColourIndex].value;
	var ChkPurchase = document.getElementById(clientId + '_chkPurchase');

	if (SizeIndex != 0) {
		if (InStock(SizeId, Colour, currentArray) == false) {
			//alert('Please select a size that is in stock');
			drpdwnSize.selectedIndex = 0;
			if (ChkPurchase) {
				ChkPurchase.disabled = true;
				ChkPurchase.parentNode.disabled = true;
				ChkPurchase.checked = false;
			}
		}
		else {
			if (ChkPurchase) {
				ChkPurchase.disabled = false;
				ChkPurchase.parentNode.disabled = false;
			}
			for (x = 0; x < currentArray.length; x++) {
				if (SizeId == currentArray[x][0] && Colour == currentArray[x][2]) {
					selectedSizeIndex = SizeIndex;
					break;
				};

			};
		};
	}
	else {
		selectedSizeIndex = SizeIndex;
		if (ChkPurchase) {
			ChkPurchase.disabled = true;
			ChkPurchase.parentNode.disabled = true;
			ChkPurchase.checked = false;
		}
	};
}

function PopulateDropDowns(clientId, currentArray) {
	if (currentArray.length == 0) { return false; };
	var drpdwnColour = document.getElementById(clientId + '_drpdwnColour');
	var ColourIndex = drpdwnColour.selectedIndex;
	var Colour = drpdwnColour.options[ColourIndex].value;
	var drpdwnSize = document.getElementById(clientId + '_drpdwnSize');
	var SizeIndex = drpdwnSize.selectedIndex;
	if (SizeIndex == -1) { SizeIndex = 0 };
	var SizeId = (drpdwnSize.options.length > 0 ? drpdwnSize.options[SizeIndex].value : -1);
	var ChkPurchase = document.getElementById(clientId + '_chkPurchase');

	var w = 0;
	var x = 0;
	var y = 0;
	var z;
	var blnFoundSize;
	var arrInStockSizeId = new Array();

	drpdwnSize.options.length = 0;
	oNewOption = new Option();
	oNewOption.value = -1;
	oNewOption.text = "Select Size";
	drpdwnSize.options[drpdwnSize.length] = oNewOption;
	if (ColourIndex == 0) {
		drpdwnSize.disabled = true;
		for (x = 0; x < currentArray.length; x++) {
			if (currentArray[x][1] == 'No Size') {
				document.getElementById(clientId + '_pnlSize').style.display = "none";
			};
		};
	}
	else {
		drpdwnSize.disabled = false;

		for (x = 0; x < currentArray.length; x++) {
			blnFoundSize = false;
			for (z = 0; z < drpdwnSize.options.length; z++) {
				if (drpdwnSize.options[z].value == currentArray[x][0]) {
					blnFoundSize = true;
					break;
				};
			};
			if (blnFoundSize == false && Colour == currentArray[x][2]) {
				y += 1;
				oNewOption = new Option();
				oNewOption.value = currentArray[x][0];
				if (currentArray[x][3] == 'True') {
					if (currentArray[x][1] != 'No Size') {
						if (currentArray[x][4] == '') {
							oNewOption.text = currentArray[x][1];
						}
						else {
							oNewOption.text = currentArray[x][1] + ' - ' + currentArray[x][4];
						}
					}
					else {
						oNewOption.text = currentArray[x][1];
					}
					arrInStockSizeId[arrInStockSizeId.length] = currentArray[x][0];

					if (currentArray[x][0] == SizeId && ColourIndex != 0) {
						w = y;
						selectedSizeIndex = y;
					};
				}
				else {
					oNewOption.text = currentArray[x][1] + ' - Not Available';
				};

				var priceElement = document.getElementById(clientId + '_lblProductPrice');
				var lastPriceElement = document.getElementById(clientId + '_lblProductPreviousPrice');
				var rrpElement = document.getElementById(clientId + '_lblRRP');
				var redSaveElement = document.getElementById(clientId + '_lblRedSave')

				if (parseFloat(currentArray[x][6]) > parseFloat(currentArray[x][5])) {
					priceElement.innerHTML = currentArray[x][7] + '' + currentArray[x][6] + '' + currentArray[x][8];
					if (lastPriceElement) {
						lastPriceElement.innerHTML = 'NOW ' + currentArray[x][7] + '' + currentArray[x][5] + '' + currentArray[x][8];
					}
				} else {
					priceElement.innerHTML = currentArray[x][7] + '' + currentArray[x][5] + '' + currentArray[x][8];
					if (lastPriceElement) {
						lastPriceElement.innerHTML = '';
					}
				}

				if (parseFloat(currentArray[x][9]) > 0) {
					var pricetxt = priceElement.innerHTML;

					rrpElement.innerHTML = 'RRP ' + currentArray[x][7] + '' + currentArray[x][10] + '' + currentArray[x][8] + '<br />';
					redSaveElement.innerHTML = 'SAVE ' + currentArray[x][7] + '' + currentArray[x][9] + '' + currentArray[x][8];
					redSaveElement.innerHTML += ' (' + currentArray[x][11] + '%)';
					priceElement.innerHTML = pricetxt;
				} else {
					if (rrpElement) {
						rrpElement.innerHTML = '';
					}
				}
				drpdwnSize.options[drpdwnSize.length] = oNewOption;
				if (w != 0) { drpdwnSize.selectedIndex = w; };
			};
		};
		if (arrInStockSizeId.length == 1 && ColourIndex != 0) {
			for (z = 0; z < drpdwnSize.options.length; z++) {
				if (drpdwnSize.options[z].value == arrInStockSizeId[0]) {
					drpdwnSize.selectedIndex = z;
					if (ChkPurchase) {
						ChkPurchase.disabled = false;
						ChkPurchase.parentNode.disabled = false;
					}
					if (drpdwnSize.options[z].text == 'No Size') {
						document.getElementById(clientId + '_pnlSize').style.display = "none";
					};
				};
			};
		};
	};
	if (drpdwnSize.options.length == 2) {
		drpdwnSize.selectedIndex = 1;
		drpdwnSize.disabled = true;
	};
}

function InStock(SizeId, Colour, currentArray) {
	var i;
	if (SizeId == -1 && Colour != -1) {
		for (i = 0; i < currentArray.length; i++) {
			if (currentArray[i][2] == Colour && currentArray[i][3] == 'True') {
				return true;
			};
		};
	}
	else if (Colour == -1 && SizeId != -1) {
		for (i = 0; i < currentArray.length; i++) {
			if (currentArray[i][0] == SizeId && currentArray[i][3] == 'True') {
				return true;
			};
		};
	}
	else if (Colour != -1 && SizeId != -1) {
		for (i = 0; i < currentArray.length; i++) {
			if (currentArray[i][0] == SizeId && currentArray[i][2] == Colour && currentArray[i][3] == 'True') {
				return true;
			};
		};
	}
	else {
		return false;
	};
	return false;
}
function CanSubmitSeperate() {
    sizeSelector = $('#separates_till_box').find('div.size select');
    colourSelector = $('#separates_till_box').find('div.colour select');

    isColourSelected = (colourSelector.attr("selectedIndex") > 0);
    isSizeSelected = (sizeSelector.attr("selectedIndex") > 0);

	if (!isColourSelected || !isSizeSelected) {
		//alert("Please select from the available size and colour options");
		return false;
	}
	else {
		sizeSelector.disabled = false;
		return true;
	};
}

function CanSubmit() {
    //TODO: id references need to be thrown into this function instead - hardcoding for now.
	var blnHasChecked
	for (var i = 1; i <= 6; i++) {
		if (document.getElementById("ctl00_ContentMainPage_ctlSeparate" + i + "_chkPurchase") && document.getElementById("ctl00_ContentMainPage_ctlSeparate" + i + "_chkPurchase").checked == true) {
			blnHasChecked = true;
		}
	}
	if (blnHasChecked) {
		return true;
	}
	else {
		//alert("Please select at least one item for purchase");
		return false;
	};
}

function SetPopUpData(Title, InvLongDescription, ImageSrc, CareInfo, AdditionalInfo, SKU, ClientId, DefaultColour) {

	var strImageMainSrc;
	var drpdwnColour = document.getElementById(ClientId + '_drpdwnColour');
	var SelectedColour;
	var LoadingImage = document.getElementById('content_product_loading').innerHTML;

	if (drpdwnColour) { var drpdwnindex = drpdwnColour.selectedIndex; }
	else { var drpdwnindex = 0 }

	if (drpdwnindex == 0) {
		SelectedColour = DefaultColour;
	}
	else {
		SelectedColour = drpdwnColour.options[drpdwnindex].value;
	}

    //load the images into temp holders
	eval("LoadTmpHolder(SelectedColour, arrSepImage_" + ClientId + ", arrThumbImage_" + ClientId + ", '_popup');");

    $('#dvMainImage_popup').html('');
	document.getElementById("imgThumb2_popup_container").innerHTML = "";
	document.getElementById("imgThumb3_popup_container").innerHTML = "";
	document.getElementById("imgThumb4_popup_container").innerHTML = "";

	PopulateImages('_popup');

	document.getElementById("dvMainImage_popup").innerHTML = document.getElementById("imgMainImage_popup_tempContainer").innerHTML;
	document.getElementById("imgThumb2_popup_container").innerHTML = document.getElementById("imgThumb2_popup_tempContainer").innerHTML;
	document.getElementById("imgThumb3_popup_container").innerHTML = document.getElementById("imgThumb3_popup_tempContainer").innerHTML;
	document.getElementById("imgThumb4_popup_container").innerHTML = document.getElementById("imgThumb4_popup_tempContainer").innerHTML;

	if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
		window.onresize = SetPopupPosition;
	}
	else if (/Safari[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
		window.onresize = SetPopupPosition;
	}
	else {
		window.onresize = SetPopupPosition;
	}

	SetPopupPosition();

	overlayContainer = $('#overlay-box-container'); //find the overlay box container so that we can find child classes from it

    productPrice = overlayContainer.find(".product-price-details");
    productPreviousPrice = overlayContainer.find(".product-previous-price");
    productRedSave = overlayContainer.find(".redsave");
    productRRP = overlayContainer.find(".product_rrp");
    productOutOfStock = overlayContainer.find(".product-out-of-stock");

	if (document.getElementById(ClientId + "_lblProductPrice")) { productPrice.innerHTML = document.getElementById(ClientId + "_lblProductPrice").innerHTML; }
	if (document.getElementById(ClientId + "_lblProductPreviousPrice")) { productPreviousPrice.innerHTML = document.getElementById(ClientId + "_lblProductPreviousPrice").innerHTML; }
	if (document.getElementById(ClientId + "_lblRedSave")) { productRedSave.innerHTML = document.getElementById(ClientId + "_lblRedSave").innerHTML; }
	if (document.getElementById(ClientId + "_lblRRP")) { productRRP.innerHTML = document.getElementById(ClientId + "_lblRRP").innerHTML; }
	if (document.getElementById(ClientId + "_pnlOutofStock")) { productOutOfStock.show(); }	else { productOutOfStock.hide(); }

	productTitle = overlayContainer.find(".product_title");
    productDescription = overlayContainer.find(".product_description");
    productCode = overlayContainer.find(".product-code");
    additionalInfo = overlayContainer.find(".additional-info");
    careInfo = overlayContainer.find(".care-info");

	productTitle.html(Title);
	productDescription.html(InvLongDescription);
	productCode.html(SKU);
	additionalInfo.html(AdditionalInfo);
	careInfo.html(CareInfo);

    //hide dropdown menus on the underlying page for IE6 show-through problem
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
		var ieversion = new Number(RegExp.$1)
		// capture x.x portion and store as a number
		if (ieversion == 6) {
		   $('select').hide();
		}
	}
}

function OnPopupOk() {
	if (document.getElementById("lblProductPreviousPrice_popup")) {document.getElementById("lblProductPreviousPrice_popup").innerHTML = ''; }
	if (document.getElementById("lblRedSave_popup")) {document.getElementById("lblRedSave_popup").innerHTML = '';}
	if (document.getElementById("lblRRP_popup")) {document.getElementById("lblRRP_popup").innerHTML = '';}
	document.getElementById("lblOutofStock_popup").style.display = 'none';

	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
		var ieversion = new Number(RegExp.$1)
		// capture x.x portion and store as a number
		if (ieversion >= 6) {
			$('select').show();
		}
	}
}
function SetPopupPosition() {

	/*var popup = document.getElementById("pnlProductPopUpInfo");
	var popupUnderlay = document.getElementById("popupUnderlay");

	if (document.documentElement.clientHeight > 558) {

		popup.style.top = (document.documentElement.clientHeight / 2 + document.documentElement.scrollTop - 2) + 'px';
		popupUnderlay.style.top = (document.documentElement.clientHeight / 2 + document.documentElement.scrollTop - 2) + 'px';
	}
	else {
		popup.style.top = (277 + 50 + document.documentElement.scrollTop) + 'px';
		popupUnderlay.style.top = (277 + 50 + document.documentElement.scrollTop) + 'px';
	};

	if (document.documentElement.clientWidth > 740) {

		popup.style.left = (document.documentElement.clientWidth / 2 + document.documentElement.scrollLeft) + 'px';
		popupUnderlay.style.left = (document.documentElement.clientWidth / 2 + document.documentElement.scrollLeft) + 'px';
	}
	else {
		popup.style.left = (345 + 50 + document.documentElement.scrollLeft) + 'px';
		popupUnderlay.style.left = (345 + 50 + document.documentElement.scrollLeft) + 'px';
	};*/
}

function SwitchInfoTabPopup(elementToShow, id, image) {
	/* This is called by the PopUp MessageBox to reset the styles and set appropriate tab. Kept this distinct from other javascript functions to avoid confusion */

	//reset all the styles.
	document.getElementById('lnkCare_Popup').innerHTML = document.getElementById('lnkCare_Popup').innerHTML.replace("_over", "");
	document.getElementById('lnkPolicy_Popup').innerHTML = document.getElementById('lnkPolicy_Popup').innerHTML.replace("_over", "");
	document.getElementById('lnkDelivery_Popup').innerHTML = document.getElementById('lnkDelivery_Popup').innerHTML.replace("_over", "");

	document.getElementById('spanProductInfo_ProductCare_Popup').style.display = 'none';
	document.getElementById('spanProductInfo_ReturnsPolicy_Popup').style.display = 'none';
	document.getElementById('spanProductInfo_Delivery_Popup').style.display = 'none';

	//change the image highlighting on the button headers.
	switch (image) {
		case 'product_caretab':
			document.getElementById(id).innerHTML = '<img class="product_care" src="' + CategoryImagePath + '/product_caretab1_over.gif" />';
			break;
		case 'product_deliverytab':
			document.getElementById(id).innerHTML = '<img class="product_policy" src="' + CategoryImagePath + '/product_returnstab1_over.gif" />';
			break;
		case 'product_returnstab':
			document.getElementById(id).innerHTML = '<img class="product_delivery" src="' + CategoryImagePath + '/product_deliverytab1_over.gif" />'
			break;
	}
	// finally change the style to show the appropriate tab.
	document.getElementById(elementToShow).style.display = 'block';
}

function GroupDisplay(Postfix) {

	document.getElementById('spanProductInfo_ProductCare').style.display = 'none';
	document.getElementById('lnkCare').innerHTML = '';

	document.getElementById('spanProductInfo_ReturnsPolicy').style.display = 'none';
	document.getElementById('lnkPolicy').innerHTML = '';

	document.getElementById('spanProductInfo_Delivery').style.display = 'none';
	document.getElementById('lnkDelivery').innerHTML = '';

	ASOS.rollOvers(CategoryImagePath, 'divButtons', 'gif', 'product_caretab');
	ASOS.rollOvers(CategoryImagePath, 'divButtons', 'gif', 'product_deliverytab');
	ASOS.rollOvers(CategoryImagePath, 'divButtons', 'gif', 'product_returnstab');

	switch (Postfix) {
		case 'ProductCare':
			document.getElementById('spanProductInfo_ProductCare').style.display = 'block';
			code = '<img class="product_care" src="' + CategoryImagePath + '/product_caretab1_over.gif" />';
			document.getElementById('lnkCare').innerHTML = code; //put code into anchor
			break;

		case 'ReturnsPolicy':
			document.getElementById('spanProductInfo_ReturnsPolicy').style.display = 'block';
			code = '<img class="product_policy" src="' + CategoryImagePath + '/product_returnstab1_over.gif" />';
			document.getElementById('lnkPolicy').innerHTML = code; //put code into anchor
			break;

		case 'Delivery':
			document.getElementById('spanProductInfo_Delivery').style.display = 'block';
			code = '<img class="product_delivery" src="' + CategoryImagePath + '/product_deliverytab1_over.gif" />';
			document.getElementById('lnkDelivery').innerHTML = code; //put code into anchor
			break;
	}
}

function GroupDisplay_Popup(Postfix) {

    document.getElementById('spanProductInfo_ProductCare_Popup').style.display = 'none';
    document.getElementById('lnkCare_Popup').innerHTML = '';

    document.getElementById('spanProductInfo_Delivery_Popup').style.display = 'none';
    document.getElementById('lnkPolicy_Popup').innerHTML = '';

    document.getElementById('spanProductInfo_ReturnsPolicy_Popup').style.display = 'none';
    document.getElementById('lnkDelivery_Popup').innerHTML = '';

    ASOS.rollOvers(CategoryImagePath, 'divButtons_Popup', 'gif', 'product_caretab');
    ASOS.rollOvers(CategoryImagePath, 'divButtons_Popup', 'gif', 'product_deliverytab');
    ASOS.rollOvers(CategoryImagePath, 'divButtons_Popup', 'gif', 'product_returnstab');

    switch (Postfix) {
        case 'ProductCare':
            document.getElementById('spanProductInfo_ProductCare_Popup').style.display = 'block';
            code = '<img class="product_care" src="' + CategoryImagePath + '/product_caretab1_over.gif" />';
            document.getElementById('lnkCare_Popup').innerHTML = code; //put code into anchor
            break;

        case 'ReturnsPolicy':
            document.getElementById('spanProductInfo_ReturnsPolicy_Popup').style.display = 'block';
            code = '<img class="product_policy" src="' + CategoryImagePath + '/product_returnstab1_over.gif" />';
            document.getElementById('lnkPolicy_Popup').innerHTML = code; //put code into anchor
            break;

        case 'Delivery':
            document.getElementById('spanProductInfo_Delivery_Popup').style.display = 'block';
            code = '<img class="product_delivery" src="' + CategoryImagePath + '/product_deliverytab1_over.gif" />';
            document.getElementById('lnkDelivery_Popup').innerHTML = code; //put code into anchor
            break;
    }
}

//Move Image Start
var imgZoom = null;
var zoomContainer = null;
var docPosition = null;

function setZoom(ZoomContainer, ImgZoom) {
	imgZoom = document.getElementById(ImgZoom);
	zoomContainer = document.getElementById(ZoomContainer);
	docPosition = getPosition(zoomContainer);
   //console.log("zoom");
    imgZoom.style.position="absolute";
    imgZoom.style.cursor="crosshair";
	imgZoom.onmousemove = setImagePostion;
	imgZoom.onload = setImageStartPostion;
}

function setImageStartPostion(ev) {

	ev = ev || window.event;
	var mousePos = mouseCoords(ev);

	//if ((mousePos.y - docPosition.y) < 0 | (mousePos.y - docPosition.y) > 460 | (mousePos.x - docPosition.x) < 0 | (mousePos.x - docPosition.x) > 890) {
		imgZoom.style.bottom = -90 + 'px'; //- 90 + 'px';
		imgZoom.style.left = -360 + 'px';// - 360 + 'px';
	//}
	//else {
	//	setImagePostion(ev);
	//}
}

function setImagePostion(ev) {

	ev = ev || window.event;
	var mousePos = mouseCoords(ev);
    //console.log(0 - ((mousePos.y - docPosition.y) * 2) + 'px');
	//if(imgZoom.width == "1335" && imgZoom.height == "1600") {

    imgZoom.style.top = 0 - ((mousePos.y - docPosition.y) * 1.605) + 'px'; // > a img sobe
    imgZoom.style.left = 0 - ((mousePos.x - docPosition.x) * 1.695) + 'px'; // > a img vai para esquerda(mostra mais do lado direito)

//    imgZoom.style.top = 0 - ((mousePos.y - docPosition.y) * 1.605) + 'px'; // > a img sobe
//    imgZoom.style.left = 0 - ((mousePos.x - docPosition.x) * 1.598) + 'px'; // > a img vai para esquerda(mostra mais do lado direito)

		//imgZoom.style.left = -30 - ((mousePos.x - docPosition.x) * 0.092) + 'px';
	/*} else {
		imgZoom.style.top = 0 - ((mousePos.y - docPosition.y) * 0.735) + 'px';
		//imgZoom.style.left = -30 - ((mousePos.x - docPosition.x) * 0.31) + 'px';
		imgZoom.style.left = 0 - ((mousePos.x - docPosition.x) * 0.31) + 'px';
	}*/
	//alert(imgZoom.width);
	//alert(imgZoom.height);
}

function mouseCoords(ev) {
	if (ev.pageX || ev.pageY) {
		return { x: ev.pageX, y: ev.pageY };
	}
	return {
		x: ev.clientX + document.documentElement.scrollLeft - document.body.clientLeft,
		y: ev.clientY + document.documentElement.scrollTop - document.body.clientTop
	};
}

function getPosition(obj) {
	var left = 0;
	var top = 0;

	if (obj.offsetParent) {

		while (obj != null) {
		    {
				left += obj.offsetLeft + (obj.clientLeft == null ? 0 : obj.clientLeft);
				top += obj.offsetTop + (obj.clientTop == null ? 0 : obj.clientTop);
				obj = obj.offsetParent;
			}
		}
    }

	return { x: left, y: top };
}

function mouseUp() {
	dragObject = null;
}
