// turn on or off javascript functionality on product detail page (product.aspx)
var jsSwitchOn = true;
//var BackOrderMsg = "Your selection is temporarily out-of-stock. To order the item, please click 'Add to Cart' and it will be shipped to you as soon as it is available (normally 5-15 days).";
//var DropShipMsg = "This item ships directly from the vendor. To order the item, please click 'Add to Cart' and it will be shipped to you as soon as it is available (normally 5-15 days).";

function FillWidthDropDown(widthDropDown, colorDropDown, sizeId, widthId, activeControl, sSku)
{
	if (!jsSwitchOn)
	{
		return jsSwitchOn;
	}
	
	var i = 0;
	ClearDropDown(widthDropDown, true);
	
	if (colorDropDown != null && colorDropDown.options.length > 1)
	{
		ClearDropDown(colorDropDown, true);
	}
	if (widthDropDown != null)
	{
		for (var key in widthArray)
		{
			for (var j=0; j<productArray.length; j++)
			{
				if (sSku == productArray[j].shortSku && sizeId == productArray[j].sizeId && key == productArray[j].widthId)
				{
					if (widthDropDown.options[i].value != key)
					{
						i++;
						widthDropDown.options[i] = new Option(widthArray[key], key);		
					}
				}
			}
		}
		if (widthDropDown.options.length == 2)
		{
			SetSelectedIndex(widthDropDown, widthDropDown.options[1].value);
			if (colorDropDown != null)
				FillColorDropDown(colorDropDown, sizeId, widthDropDown.options[1].value, activeControl, sSku);
		}
	}
	else
	{
		if (colorDropDown != null)
		{
			FillColorDropDown(colorDropDown, sizeId, widthId, activeControl, sSku);
		}
	}
	
	return true;
}

function FillColorDropDown(colorDropDown, sizeId, widthId, activeControl, sSku)
{
	if (!jsSwitchOn)
	{
		return jsSwitchOn;
	}
	
	var i = 0;
	ClearDropDown(colorDropDown, true);
	
	for (var key in colorArray)
	{
		for (var j=0; j<productArray.length; j++)
		{
			if (sSku == productArray[j].shortSku && sizeId == productArray[j].sizeId && (widthId == "" || widthId == "0" || widthId == productArray[j].widthId) && key == productArray[j].colorId)
			{
				if (colorDropDown.options[i].value != key)
				{
					i++;
					colorDropDown.options[i] = new Option(colorArray[key], key);
				}
			}
		}
	}
	
	if (colorDropDown.options.length == 2)
	{
		SetSelectedIndex(colorDropDown, colorDropDown.options[1].value);
		DisplayMessage(StockMessageValidatorByValue(sizeId, widthId, colorDropDown.options[1].value, sSku), activeControl, sSku);
	}
	
	return true;
}

function SetSelectedIndex(dd, idValue)
{
	if (dd != null)
	{
		for (var i=0; i<dd.options.length; i++)
		{
			if (dd.options[i].value == idValue)
			{
				dd.selectedIndex = i;
				break;
			}
		}
	}
}

function SetSelectedId(dd, idHidden)
{
	if (dd != null)
	{
		idHidden.value = dd.options[dd.selectedIndex].value;
	}
}

function ClearDropDown(dd, clearAll)
{
	if (!jsSwitchOn)
	{
		return jsSwitchOn;
	}
	
	if (dd != null && clearAll)
	{
		for (i=dd.options.length-1; i>0; i--)
		{
			dd[i] = null;
		}
		dd.selectedIndex = 0;
	}

	if (dd != null)
	{
		var stockIconDisplay = document.getElementById(dd.id.split("_")[0] + "_stockIconDisplay");
		stockIconDisplay.style.display = "none";
	}

}

function FillDropDown(sizeDropDown, widthDropDown, colorDropDown, activeControl, sSku)
{
	if (!jsSwitchOn)
	{
		return jsSwitchOn;
	}
	
	var i = 0;
	var selectedIndexWidth = "";
	var selectedIndexColor = "";
	var selectedIndexSize = "";
	
	if (sizeDropDown != null)
		selectedIndexSize = sizeDropDown.options[sizeDropDown.selectedIndex].value;
	
	if (sizeDropDown != null && widthDropDown != null)
	{
		selectedIndexWidth = widthDropDown.options[widthDropDown.selectedIndex].value;
		ClearDropDown(widthDropDown, true);
		
		for (var key in widthArray)
		{
			for (var j=0; j<productArray.length; j++)
			{
				if (sSku == productArray[j].shortSku && selectedIndexSize == productArray[j].sizeId && key == productArray[j].widthId)
				{
					if (widthDropDown.options[i].value != key)
					{
						i++;
						widthDropDown.options[i] = new Option(widthArray[key], key);		
					}
				}
			}
		}
		
		if (widthDropDown.options.length == 2)
		{
			SetSelectedIndex(widthDropDown, widthDropDown.options[1].value);
			selectedIndexWidth = widthDropDown.options[1].value;
		}
		else
		{
			SetSelectedIndex(widthDropDown, selectedIndexWidth);
		}
	}
	
	i = 0;
	if ((sizeDropDown != null || widthDropDown != null) && colorDropDown != null)
	{
		selectedIndexColor = colorDropDown.options[colorDropDown.selectedIndex].value;
		ClearDropDown(colorDropDown, true);
		
		for (var key in colorArray)
		{
			for (var j=0; j<productArray.length; j++)
			{
				if ((selectedIndexSize == "" || selectedIndexSize == productArray[j].sizeId) && (selectedIndexWidth == "" || selectedIndexWidth == productArray[j].widthId) && key == productArray[j].colorId)
				{
					if (colorDropDown.options[i].value != key)
					{
						i++;
						colorDropDown.options[i] = new Option(colorArray[key], key);		
					}
				}
			}
		}
		
		if (colorDropDown.options.length == 2)
		{
			SetSelectedIndex(colorDropDown, colorDropDown.options[1].value);
			DisplayMessage(StockMessageValidatorByValue(selectedIndexSize, selectedIndexWidth, colorDropDown.options[1].value, sSku), activeControl, sSku);
		}
		else
		{
			SetSelectedIndex(colorDropDown, selectedIndexColor);
		}
	}
	
	return true;
}

function StockMessageValidatorByValue(size, width, color, sSku)
{
	var show = "";
	
	for (var j=0; j<productArray.length; j++)
	{	
		if (	(sSku == productArray[j].shortSku)
				&& (size == "" || size == productArray[j].sizeId) 
				&& (width == "" || width == productArray[j].widthId)
				&& (color == "" || color == productArray[j].colorId)	)
		{
			if (productArray[j].ecometryStatus == "T1")
			{
				show = document.forms[0].DropShipMsg.value;
			}
			else if (productArray[j].inventoryStatus.indexOf("In Stock") == -1)
			{
				if (productArray[j].ecometryStatus == "T2")
					show = document.forms[0].DropShipMsg.value;
				else
					show = document.forms[0].BackOrderMsg.value;
			}
			break;
		}
	}
	
	return show;
}

function StockMessageValidator(ddSizeId, ddWidthId, ddColorId, clearArrow, sSku)
{
	if (!jsSwitchOn)
	{
		return jsSwitchOn;
	}
	
	var show = "";
	
	var size = "";
	var width = "";
	var color = "";
	
	if (ddSizeId != null) size = ddSizeId.options[ddSizeId.selectedIndex].value;
	if (ddWidthId != null) width = ddWidthId.options[ddWidthId.selectedIndex].value;
	if (ddColorId != null) color = ddColorId.options[ddColorId.selectedIndex].value;
	
	for (var j=0; j<productArray.length; j++)
	{
		if (	(sSku == productArray[j].shortSku)
				&& (size == "" || size == productArray[j].sizeId) 
				&& (width == "" || width == productArray[j].widthId)
				&& (color == "" || color == productArray[j].colorId)	)
		{
			if (productArray[j].inventoryStatus.indexOf("In Stock") == -1)
			{
				if (productArray[j].ecometryStatus == "T2")
					show = document.forms[0].DropShipMsg.value;
				else
					show = document.forms[0].BackOrderMsg.value;
			}
			break;
		}
	}
	
	if (clearArrow != false)
		ClearArrowImage(ddSizeId, ddWidthId, ddColorId, "");
	
	return show;
}

function DisplayMessage(showMsg, activeControl, sSku)
{
	if (!jsSwitchOn)
	{
		return jsSwitchOn;
	}
	
	var productStock = document.getElementById(activeControl);
	
	if (productStock == null) return;
	
	var countAvailableComponent = document.getElementById(activeControl.split("_")[0] + "_countAvailableComponent")
	if (countAvailableComponent.value == "0")
	{
		showMsg = document.forms[0].NotAvailabilityMsg.value;
		var r = document.referrer.toUpperCase();

		if (r.indexOf(HomePage) > -1)
		{
			showMsg = showMsg + "<a href='" + r + "'>Click here to continue shopping</a>";
		}
		else
		{
			showMsg = showMsg + "<a href='Default.aspx'>Click here to continue shopping</a>";
		}
	}
	
	if (showMsg.length > 0)
	{
		productStock.style.display = "block";
		productStock.innerHTML = "<font color='red'>" + showMsg + "</font>";
		errorMsg = productStock.innerHTML.stripTags();

		pageId = "PRODUCT: " + document.forms[0].productNameHidden.value + " (" + document.forms[0].productShortSkuHidden.value + ")";
		cmCreateFormErrorTag(pageId, errorMsg);
	}
	else
	{
		productStock.style.display = "none";
	}
	
	var preName = productStock.id.split("_")[0] + "_buyButton";
	ChangeArrowImage(null, null, null, preName, sSku);
}

function ChangeArrowImage(ddSizeId, ddWidthId, ddColorId, imgBuyButtonArrowName, sSku)
{
	if (!jsSwitchOn)
	{
		return jsSwitchOn;
	}
	
	var imgSize;
	var imgWidth;
	var imgColor;
	var imgBuyButton;
	var imgArrow = "Images/ddarrow.gif";
	var suffixArrow = "Arrow";
	
	ClearArrowImage(ddSizeId, ddWidthId, ddColorId, imgBuyButtonArrowName);

	if (isShowArrow)
	{
		if (ddSizeId != null && ddSizeId.selectedIndex == 0 && document[ddSizeId.id + suffixArrow] != null)
		{
			imgSize = document[ddSizeId.id + suffixArrow];
			imgSize.src = imgArrow;
		}
		else if (ddWidthId != null && ddWidthId.selectedIndex == 0 && document[ddWidthId.id + suffixArrow] != null)
		{
			imgWidth = document[ddWidthId.id + suffixArrow];
			imgWidth.src = imgArrow;
		}
		else if (ddColorId != null && ddColorId.selectedIndex == 0 && document[ddColorId.id + suffixArrow] != null)
		{
			imgColor = document[ddColorId.id + suffixArrow];
			imgColor.src = imgArrow;
		}
		else
		{
			if (document[imgBuyButtonArrowName + suffixArrow] != null)
			{
				imgBuyButton = document[imgBuyButtonArrowName + suffixArrow];
				if (CheckArrowOnButton(imgBuyButtonArrowName))
				{
					imgBuyButton.src = imgArrow;
				}
			}
		}
	}
	
	DisplayInStockIcon(imgBuyButtonArrowName.split("_")[0], sSku);
}

function CheckArrowOnButton(imgBuyButtonArrowName)
{
	var show = true;
	var form = document.forms[0];
	var el;
	
	if (document.getElementById(imgBuyButtonArrowName) == null || productArray.length == 0)
	{
		return false;
	}
	
	for (i = 0; i < form.elements.length; i++ )
	{
		el = form.elements[i];
		if (el.type.toUpperCase().indexOf("SELECT") > -1 && imgBuyButtonArrowName.substring(0,3) == el.name.substring(0,3))
		{
			if (el.selectedIndex == 0)
			{
				show = false;
				break;
			}
		}
	}
		
	return show;
}

function DisplayInStockIcon(activeControl, sSku)
{
	var stockIconDisplay = document.getElementById(activeControl + "_stockIconDisplay");
	var countComponent = document.getElementById(activeControl + "_countComponent");
	var ddSizeId;
	var ddWidthId;
	var ddColorId;
	var show = true;
	
	if (document.getElementById(activeControl + "_buyButton") == null || productArray.length == 0)
	{
		return false;
	}

	if (stockIconDisplay != null)
	{
		if (countComponent.value == "1")
		{
			ddSizeId = document.getElementById(activeControl + "_ddSizeId");
			ddWidthId = document.getElementById(activeControl + "_ddWidthId");
			ddColorId = document.getElementById(activeControl + "_ddColorId");
			
			if (ddSizeId != null && ddSizeId.selectedIndex == 0 || ddWidthId != null && ddWidthId.selectedIndex == 0 || ddColorId != null && ddColorId.selectedIndex == 0)
				show = false;
			
			if (show && StockMessageValidator(ddSizeId, ddWidthId, ddColorId, false, sSku) != "")
				show = false;
		}
		else
		{
			for (i=0; i<parseInt(countComponent.value); i++)
			{
				ddSizeId = document.getElementById(activeControl + "_kitSize_" + i);
				ddWidthId = document.getElementById(activeControl + "_kitWidth_" + i);
				ddColorId = document.getElementById(activeControl + "_kitColor_" + i);
				
				if (ddSizeId != null && ddSizeId.selectedIndex == 0 || ddWidthId != null && ddWidthId.selectedIndex == 0 || ddColorId != null && ddColorId.selectedIndex == 0)
				{
					show = false;
					break;
				}
			}
			if (show)
			{
				for (i=0; i<parseInt(countComponent.value); i++)
				{
					ddSizeId = document.getElementById(activeControl + "_kitSize_" + i);
					ddWidthId = document.getElementById(activeControl + "_kitWidth_" + i);
					ddColorId = document.getElementById(activeControl + "_kitColor_" + i);
				
					if (StockMessageValidator(ddSizeId, ddWidthId, ddColorId, false, sSku) != "")
					{
						show = false;
						break;
					}
				}
			}

		}
		
		if (show)
			stockIconDisplay.style.display = "block";
		else
			stockIconDisplay.style.display = "none";
	}
}

function ClearArrowImage(ddSizeId, ddWidthId, ddColorId, imgBuyButtonArrowName)
{
	var imgSize;
	var imgWidth;
	var imgColor;
	var imgBuyButton;
	var imgSpace = "Images/spacer.gif";
	var suffixArrow = "Arrow";
	
	if (imgBuyButtonArrowName != "" && document[imgBuyButtonArrowName + suffixArrow] != null)
	{
		imgBuyButton = document[imgBuyButtonArrowName + suffixArrow];
		imgBuyButton.src = imgSpace;
	}

	if (isShowArrow)
	{
		if (ddSizeId != null && document[ddSizeId.id + suffixArrow] != null)
		{
			imgSize = document[ddSizeId.id + suffixArrow];
			imgSize.src = imgSpace;
		}
		if (ddWidthId != null && document[ddWidthId.id + suffixArrow] != null)
		{
			imgWidth = document[ddWidthId.id + suffixArrow];
			imgWidth.src = imgSpace;
		}
		if (ddColorId != null && document[ddColorId.id + suffixArrow] != null)
		{
			imgColor = document[ddColorId.id + suffixArrow];
			imgColor.src = imgSpace;
		}
	}
}

// Used by the website
// Rwilliams
// Business admin tools phase II
// 08/01/2006
function ViewAdditionalImage(width, height)
{
	//productIdHidden
	if (document.forms[0].elements['productIdHidden'].value.length > 0)
	{
		var productId = document.forms[0].elements['productIdHidden'].value;
		if(productId > 0)
		{
			popUp("ProductAdditionalImagePopup.aspx?imageviewtype=1&productId=" + productId,width+100,height+100);
		}
	}
}

// Used by the website
// Rwilliams
// Multi view for products
// 08/21/2006
function ViewMultiView()
{
	//productIdHidden
	if (document.forms[0].elements.productIdHidden.value.length > 0)
	{
		var productId = document.forms[0].elements.productIdHidden.value;
		if(productId > 0)
		{
			var url = "multiangleproductviewspopup.aspx?productId=" + productId;
			var strName = 'MultiAngleProductView' + productId;
			var width = 606;
			var height = 593;
			var top, left;
			if(!top)
			{
				if(height){
					top = (screen.height / 2 - height / 2);
				}
				else{
					top = 50;
				}
			}
			if(!left)
			{
				if(width){
					left = (screen.width / 2 - width / 2);
				}
				else{
					left = 100;
				}
			}
			
			window.open(url,strName,'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,left=' + left + ',top=' + top + ',width=' + width + ',height=' + height);				
		}
	}
}

function ViewEnlargeImage()
{
	if (document.forms[0].elements['enlargeFilenameHidden'].value.length > 0)
	{
		var filename = document.forms[0].elements['enlargeFilenameHidden'].value;
					
		if (document.forms[0].elements['enlargeIsZoomHidden'].value == 'True')
		{
				//popUp("http://s7ondemand1.scene7.com/s7ondemand/zoom/dhtml_zoom.jsp?company=BenchmarkBrands&sku=" + filename,460,380,'',"Zoom");
			popUp("ProductZoomPopup.aspx?scene7name=" + filename, 600, 475);
		}
		else
		{
			popUp("ProductImagePopup.aspx?scene7name=" + filename, 450, 450);
		}
	}
	else
	{
		popUp("ProductImagePopup.aspx?filename=" + document.forms[0].elements['enlargeImagePathHidden'].value,450,450);
	}
}

function EmailAFriend()
{
	popUp("EmailFriendPopup.aspx?productId=" + document.forms[0].elements['productIdHidden'].value, 500,400);
}

function TellAFriend()
{
	popUp("TellAFriendPopup.aspx?productId=" + document.forms[0].elements['productIdHidden'].value, 450,450);
}
/*
function AskTheExpert()
{
	popUp("CustomerContactPopup.aspx?productId=" + document.forms[0].elements['productIdHidden'].value, 500,300);
}*/
function MoreInformation()
{
	popUp("ProductInformationPopup.aspx?productId=" + document.forms[0].elements['productIdHidden'].value, 400, 400);
}
function ProductEnhancement(type, height, width)
{
	if (type=='SizeChartImage')
		popUp("ProductEnhancementPopup.aspx?filename=" + document.forms[0].elements['sizeChartImagePathHidden'].value, width, height);
}
function SizeChart()
{
	popUp("SizeChartPopup.aspx?productId=" + document.forms[0].elements['productIdHidden'].value, 750, 350);
}

function setProductSizeWidthColor(colorId, sizeId, widthId, colorSwatch)
{
	if (sizeId != '' && document.forms[0].elements[ddSizeId] != null)
	{
		SetSelectedIndex(document.forms[0].elements[ddSizeId], sizeId);
	}
	
	if (widthId != '' && document.forms[0].elements[ddWidthId] != null)
	{
		FillWidthDropDown(document.forms[0].elements[ddWidthId], document.forms[0].elements[ddColorId], sizeId, widthId, "", imgProductId);
		SetSelectedIndex(document.forms[0].elements[ddWidthId], widthId);
	}
	
	if (colorId != '' && document.forms[0].elements[ddColorId] != null)
	{
		FillColorDropDown(document.forms[0].elements[ddColorId], sizeId, widthId, "", imgProductId);
		SetSelectedIndex(document.forms[0].elements[ddColorId], colorId);

		changeImage(document.forms[0].elements[ddColorId], document.forms[0].elements['productShortSkuHidden'].value, colorSwatch);
	}
	
	DisplayMessage(StockMessageValidator(document.forms[0].elements[ddSizeId], document.forms[0].elements[ddWidthId], document.forms[0].elements[ddColorId], true, imgProductId), productStockValidator, imgProductId);
	
	// re-submit form to clear validation errors
	//document.forms[0].submit();
}

function ChangeColor(filename, colorId)
{
	var img = document.getElementById('productImage');
	img.src = 'http://s7ondemand1.scene7.com/is/image/BenchmarkBrands/' + filename + '?$200x200$';

	var isFind = false;
	
	if (ddColorId != "" && document.forms[0].elements[ddColorId] != null)
	{
		for (n=0; n<document.forms[0].elements[ddColorId].options.length; n++)
		{
			if (colorId == document.forms[0].elements[ddColorId].options[n].value)
			{
				document.forms[0].elements[ddColorId].selectedIndex = n;
				isFind = true;
				break;
			}
		}
		
		if (!isFind)
			document.forms[0].elements[ddColorId].selectedIndex = 0;
			
		DisplayMessage(StockMessageValidator(document.forms[0].elements[ddSizeId], document.forms[0].elements[ddWidthId], document.forms[0].elements[ddColorId], true, imgProductId), productStockValidator, imgProductId);
		var preName = productStockValidator.split("_")[0] + "_buyButton";
		ChangeArrowImage(document.forms[0].elements[ddSizeId], document.forms[0].elements[ddWidthId], document.forms[0].elements[ddColorId], preName, imgProductId);
	}

	document.forms[0].elements['enlargeFilenameHidden'].value = filename;
}
function changeImage(oElement, shortsku, colorSwatch)
{
	var k = oElement.selectedIndex;
	if (k != 0 && colorSwatch != "False")
	{
		var re = new RegExp (' ', 'gi') ;
		var re2 = new RegExp ('/', 'gi');
		var filename = shortsku + '_' + oElement.options[k].text.toUpperCase().replace(re, '_').replace(re2,'_');
		var img = document.getElementById('productImage');
		img.src = 'http://s7ondemand1.scene7.com/is/image/BenchmarkBrands/' + filename + '?$200x200$';	
		document.forms[0].elements['enlargeFilenameHidden'].value = filename;
	}
}


function onMouseOverOnColorSwatch(obj, colorName) 
{
	var div = document.getElementById("div_title");

	if (navigator.userAgent.toLowerCase().indexOf("safari") > -1 && navigator.platform.toLowerCase().indexOf("win") > -1)
	{
	/* // getting a object position.
		var posx = posy = 0;
		if (obj.offsetParent)
		{
			posx = obj.offsetLeft
			posy = obj.offsetTop
			while (obj = obj.offsetParent)
			{
				posx += obj.offsetLeft
				posy += obj.offsetTop
			}
		}
 */
		// getting a mouse position.
		var posx = 0;
		var posy = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY)
		{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY)
		{
			posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
		
		if (div != null)
		{
			div.innerHTML = colorName;
			div.style.left = posx;
			div.style.top = posy + 20;
			div.style.display = "block";
		}
	}
}

function onMouseOutOnColorSwatch() 
{
	var div = document.getElementById("div_title");
	if (div != null)
		div.style.display = "none";
}


