
function drpdwnColourChange(ClientId, currentArray, colourThumbsArray, productMode) {
	var drpdwnColour = document.getElementById(ClientId + '_drpdwnColour');
	var drpdwnSize = document.getElementById(ClientId + '_drpdwnSize');
	var drpdwnindex = drpdwnColour.selectedIndex;
	var SelectedColour = filterNonWordChars(drpdwnColour.options[drpdwnindex].value);
	var ChkPurchase = document.getElementById(ClientId + '_chkPurchase');

	if (drpdwnindex != 0) {
		if (productMode == 0) {
		    LoadTmpHolder(SelectedColour, arrMainImage, arrThumbImage, '');
		}
		else {
		    LoadTmpHolder(SelectedColour, 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 = filterNonWordChars(currentColourDDL.options[selectedColourIndex].value);
		for (var i = 0; i < colourThumbsArray.length; i++) {
			if (colourThumbsArray[i][3].toString().toLowerCase() == selectedColourValue.toString().toLowerCase()) {
				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 = filterNonWordChars(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 = filterNonWordChars(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() {

	var canSubmit = false;
    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");
		canSubmit = false;
	}
	else {
		sizeSelector.disabled = false;
		canSubmit = true;
	};
	
	return canSubmit;
}

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 = filterNonWordChars(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 = ''; }

	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();
	    }
	}
	document.getElementById("lblOutofStock_popup").style.display = 'none';
}
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("_active", "");
    document.getElementById('lnkPolicy_Popup').innerHTML = document.getElementById('lnkPolicy_Popup').innerHTML.replace("_active", "");
    document.getElementById('lnkDelivery_Popup').innerHTML = document.getElementById('lnkDelivery_Popup').innerHTML.replace("_active", "");

	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_active.gif" />';
	        break;
		case 'product_deliverytab':
		    document.getElementById(id).innerHTML = '<img class="product_policy" src="' + CategoryImagePath + '/product_returnstab1_active.gif" />';
			break;
		case 'product_returnstab':
		    document.getElementById(id).innerHTML = '<img class="product_delivery" src="' + CategoryImagePath + '/product_deliverytab1_active.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_active.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_active.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_active.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) > 370 | (mousePos.x - docPosition.x) < 0 | (mousePos.x - docPosition.x) > 290) {
		imgZoom.style.top = 0 - 290 + 'px';
		imgZoom.style.left = 0 - 370 + 'px';
	}
	else {
		setImagePostion(ev);
	}
}

function setImagePostion(ev) {
    
	ev = ev || window.event;
	var mousePos = mouseCoords(ev);
   //console.log(0 - ((mousePos.y - docPosition.y) * 2) + 'px');
	imgZoom.style.top = 0 - ((mousePos.y - docPosition.y) * 2) + 'px';
	imgZoom.style.left = 0 - ((mousePos.x - docPosition.x) * 2) + 'px';
}

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;
}

function positionSaveForLaterNotification() {
	$.facebox.settings.overlay = false;
	$.facebox.settings.centered = false;
	$.facebox.settings.topPosition = ($(".save-for-later").offset().top + 25) + 'px';
	$.facebox.settings.leftPosition = ($(".save-for-later").offset().left - 145) + 'px';
	$.facebox.settings.width = 350;
}

$(document).ready(function() {
    /*tabbed content*/
    $("#delivery").show();
    $("#freeReturns").show();
    $(".product-tabs > ul").tabs({selected:1});
    $("#productTabs").show();
    $("#loadingTabs").hide();
    /*Save for later*/
    $("a.product_SaveForLater").click(function() {
        positionSaveForLaterNotification();
        if (CanSubmitSeperate()) {
            var url = "/services/srvSaveForLater.asmx/SaveInventoryForLater";
            var errorMessage = '<div class="saved-for-later-notification"><p>Sorry this item could not be saved. Please try again.</p><p><a href="#" class="close">Continue shopping</a></p></div>';
            var successMessage = null;

            var parentId = $("span.ctlSeparateProduct-parentId input").val();
            var colour = $("div.colour select option:selected").val();
            var sizeId = $("div.size span.sizeguide select option:selected").val();

            var catRefineInfo = $("input#hdn-cat-refine-values").val();

            if (parentId.length > 0 && colour.length > 0 && sizeId.length > 0) {

                if (catRefineInfo.length > 0) {
                    successMessage = '<div class="saved-for-later-notification"><p>This item has been saved for 30 days.</p><p><a href="#" class="close">Continue shopping</a> | <a href="/basket/pgebasket.aspx?iid=' + parentId + '&' + catRefineInfo + '#your-saved-items">View saved items</a></p></div>';
                } else {
                    successMessage = '<div class="saved-for-later-notification"><p>This item has been saved for 30 days.</p><p><a href="#" class="close">Continue shopping</a> | <a href="/basket/pgebasket.aspx?iid=' + parentId + '#your-saved-items">View saved items</a></p></div>';
                }

                $.ajax({
                    type: "POST",
                    url: url,
                    data: "{ParentId : " + parentId + ", Colour : '" + colour + "' , SizeId : '" + sizeId + "'}",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function(data) {
                        if (data.d == true) {
                            $.facebox(successMessage);
                        } else {
                            $.facebox(errorMessage);
                        }
                    },
                    error: function() {
                        $.facebox(errorMessage);
                    }
                });
            } else {
                $.facebox(errorMessage);
            }
        }
        return false;
    });
    $("a.save-for-later-mix-and-match").click(function() {
        positionSaveForLaterNotification();

        if (CanSubmit()) {

            var url = "/services/srvSaveForLater.asmx/SaveInventorysForLater";
            var errorMessage = '<div class="saved-for-later-notification"><p>Sorry this item could not be saved. Please try again.</p><p><a href="#" class="close">Continue shopping</a></p></div>';
            var successMessage = null;

            var invSeperator = '|';
            var propertySeperator = ',';

            var seperateId = null;
            var colour = null;
            var sizeId = null;
            var parentId = null;

            var catRefineInfo = $("input#hdn-cat-refine-values").val();

            var arrSeperates = new Array();
            var arrIdx = 0;

            var strElem = null;
            var strSeperates = '';

            var redirectParentId = null;

            //loop through checked checkboxes
            $("span.select-for-purchase input:checked").each(function() {
                seperateId = $(this).parent().attr("class").replace('select-for-purchase ', '').replace('-checkbox', '');

                if (seperateId != null && seperateId.length > 0) {
                    parentId = $("span." + seperateId + "-parentId input").val();
                    colour = $("select." + seperateId + "-colour option:selected").val();
                    sizeId = $("select." + seperateId + "-size option:selected").val();

                    if (parentId.length > 0 && colour.length > 0 && sizeId.length > 0) {

                        strElem = parentId + propertySeperator + colour + propertySeperator + sizeId;

                        if (strSeperates != null && strSeperates.length > 0 && strSeperates.substring(strSeperates.length - 2, strSeperates.length - 1) != invSeperator) {
                            strSeperates += invSeperator;
                        }

                        strSeperates += strElem;

                        redirectParentId = parentId;
                    }
                }
            });

            if (strSeperates.length > 0) {

                if (catRefineInfo.length > 0) {
                    successMessage = '<div class="saved-for-later-notification"><p>This item has been saved for 30 days.</p><p><a href="#" class="close">Continue shopping</a> | <a href="/basket/pgebasket.aspx?iid=' + redirectParentId + '&' + catRefineInfo + '#your-saved-items">View saved items</a></p></div>';
                } else {
                    successMessage = '<div class="saved-for-later-notification"><p>This item has been saved for 30 days.</p><p><a href="#" class="close">Continue shopping</a> | <a href="/basket/pgebasket.aspx?iid=' + redirectParentId + '#your-saved-items">View saved items</a></p></div>';
                }

                $.ajax({
                    type: "POST",
                    url: url,
                    data: "{StrSeperates : '" + strSeperates + "'}",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function(data) {
                        if (data.d == true) {
                            $.facebox(successMessage);
                        } else {
                            $.facebox(errorMessage);
                        }
                    },
                    error: function() {
                        $.facebox(errorMessage);
                    }
                });
            } else {
                $.facebox(errorMessage);
            }
        }

        return false;
    });
});

function filterNonWordChars(text) {
    if (text) {
        if (text === '') {
            return text
        } else {
            return text.replace(/[^\w]/g, '');
        }
    } else {
        return text;
    }
}
