var prodImageMap = new Array();


var colorSelected=false;
function showCustomPopUp(thisUrl,thisName,theseParams)
{
	remote = open(thisUrl, thisName, theseParams);
}						

//  Traverse through the itemMap array to get the colors based on productId
//  and build a new array with color ids and descriptions
function getStringColors(pidSku)
{
	var pidSkuArray = pidSku.split("|");
	productId = pidSkuArray[0];
	colorId = pidSkuArray[1];
	var strSizes = new Array();
	var j = 0;

	for(var i = 0; i < itemMap.length; i++)
	{
		if(itemMap[i].pid == productId && itemMap[i].cId == colorId)
		{
			strSizes[j] = itemMap[i].pid+"|"+itemMap[i].sku;
			j++;
			
			if (itemMap[i].avail == "OUT_OF_STOCK" || itemMap[i].avail == "NOT_AVAILABLE")
			{
				strSizes[j] = itemMap[i].sDesc + " - " +"Out Of Stock";
			}
			else
			{
				strSizes[j] = itemMap[i].sDesc;
			}
			j++;
		}
	}
	return strSizes;
}

//product id of the product displayed in the express shop
var prodId = null

//should be updated whenever the user clicks on "buy more at a time"
var grouping = 0;

//called when the user clicks on a swatch or when they change the color drop down
function colorChange(colorId, groupNumber) {
	if (groupNumber == -1) {
		groupNumber = grouping;
	}
	//colorDropDown = $('drpCustomTagProductColor_' + grouping);
	colorDropDown = $('drpCustomTagProductColor_' + groupNumber);
	//change the color drop down
	colorDropDown.value = colorId;
	//change the big image to what is showing.  update the size dropdown as well.
	//also update the border around the swatching

	if (colorId != "-1" && swatchSize > 0) {
		for (var i = 0; i < mainColorMap.length; i++) {
			if (mainColorMap[i].colorId == colorId) {
				if (mainColorMap[i].mainImg != "") {
					updateMainImage(mainColorMap[i].mainImg);
					$('displayColorSelected').innerHTML = mainColorMap[i].colorTitle;
					ShopTogether.setCurrentProductImage($('dvCustomTagProductImage').getElementsByTagName('img')[0].src);
				}

				if (mainColorMap.length >  1) {
					updateSizeDropDown(mainColorMap[i].sizes,groupNumber);
				}

				if (swatchIds.indexOf(mainColorMap[i].colorId) > 0) {
					$('swatchColor_'+mainColorMap[i].colorId+'_top').style.background="#C3DCF2";
					$('swatchColor_'+mainColorMap[i].colorId+'_left').style.background="#C3DCF2";
					$('swatchColor_'+mainColorMap[i].colorId+'_right').style.background="#C3DCF2";
					$('swatchColor_'+mainColorMap[i].colorId+'_bottom').style.background="#C3DCF2";
				}
			} else if (swatchIds.indexOf(mainColorMap[i].colorId) > 0) {
				$('swatchColor_'+mainColorMap[i].colorId+'_top').style.background="";
				$('swatchColor_'+mainColorMap[i].colorId+'_left').style.background="";
				$('swatchColor_'+mainColorMap[i].colorId+'_right').style.background="";
				$('swatchColor_'+mainColorMap[i].colorId+'_bottom').style.background="";
			}
		}
	}
}

//used to update the main image when the color changes
function updateMainImage(mainImgTag) {
	if (mainImgTag != null) {
		mainImg = $('dvCustomTagProductImage');
		mainImg.innerHTML = "<img " + mainImgTag + "/>";
	}
}
//  Dynamically fill the color dropdown based on the selection
function updateSelector(selector,items) {
	var options = selector.options;
	var selectedItem = selector.options[selector.selectedIndex].text;
	
	for (var i = 0; i < Math.min(items.length, options.length); i++)
	{
			options[i].value = items[i].id;
			options[i].text = items[i].de;
	}

	
	
		/* Append options if needed: */
			for (var i = Math.min(items.length, options.length); i <items.length; i++)
			{
			var newOpt = new Option(items[i].de,
			items[i].id,
			false,
			items[i].selected);
			options[options.length] = newOpt;
			}
		options[0].selected = true;
	

	/* Truncate selector if needed: */
	selector.length = items.length;
	
	var haveMatch = false;
	for (var i = 0; i < selector.options.length; i++)
	{
		if (selector.options[i].text == selectedItem)
		{
			selector.options[i].selected = true;
			haveMatch = true;
		}
	}
	
	if (!haveMatch)
	{
		options[0].selected = true;
	}
	syncFields(selector);
}

//  Populate color array to pass to updateSelector function
function populateSizes(sel_element1, sel_element2)
{
	var val = sel_element1.options[sel_element1.selectedIndex].value;
	var items = new Array();
	
	//  If user sets color dropdown back to "Color" reset the prod_ dropdown
	if(val == '')
	{
		var selectOneArray = new Array("","Select Size");

		items[items.length] = new Object();
		items[items.length-1].id = selectOneArray[0];
		items[items.length-1].de = selectOneArray[1];

		updateSelector(sel_element2,items);
		return;
	}

	//  If color was selected, fill colorArray
	if(val != "-1")
	{
		var selectOneArray = new Array("","Select Size");

		items[items.length] = new Object();
		items[items.length-1].id = selectOneArray[0];
		items[items.length-1].de = selectOneArray[1];
		
		var colorArray = getStringColors(val);
		for (var i = 0; i < colorArray.length; i++)
		{
			items[items.length] = new Object();
			items[items.length-1].id = colorArray[i];
			items[items.length-1].de = colorArray[++i];
		}
		updateSelector(sel_element2,items);
	}
}

function updateFields(thisDropDown,prodIndex)
{
	if (thisDropDown)
	{
		var thisImageMap = eval('imageMap_'+prodIndex);
		
		if (thisDropDown.name.match("prod_") && thisDropDown.selectedIndex > 0)
		{
			// to display product level back views.
			colorSelected =true;
			var haveImage = false;
			var pidSku = thisDropDown.options[thisDropDown.selectedIndex].value;
			var pidSkuArray = pidSku.split("|");
			var selectedItem = thisDropDown.options[thisDropDown.selectedIndex].text;
			productId = pidSkuArray[0];
			sku = pidSkuArray[1];
			
			for(var i = 0; i < itemMap.length; i++)
			{
				if(itemMap[i].pid == productId && itemMap[i].sku == sku)
				{			
					priceField = eval('document.orderForm.price_'+prodIndex);
					priceField.value = itemMap[i].price;
					
					if (thisDropDown.options[0].text != "Select Size")
					{
						if (thisImageMap)
						{
							for(var m = 0;!haveImage &&  m < thisImageMap.length; m++)
							{
								if (thisImageMap[m].name && thisImageMap[m].name == selectedItem)
								{
									haveImage = true;
									break;
								}
							}
						}
						
						styleField = eval('document.orderForm.style_'+prodIndex);
						
						if (haveImage)
						{
							styleField.value = itemMap[i].cDesc;
							break;
						}
						else
						{
							styleField.value = itemMap[i].cDesc + " - No Image Available";
							break;
						}
					}
				}
			}
		}
		else if (thisDropDown.name.match("colors_") && thisDropDown.selectedIndex > 0)
		{
			colorSelected =true; // to display product level back views.
			var pidColor = thisDropDown.options[thisDropDown.selectedIndex].value;
			var pidColorArray = pidColor.split("|");
			var productId = pidColorArray[0];
			var colorId = pidColorArray[1];
			var haveImage = false;
			styleField = eval('document.orderForm.style_'+prodIndex);
			
			for(var i = 0; i < itemMap.length; i++)
			{
				if(itemMap[i].pid == productId && itemMap[i].cId == colorId)
				{
					if (thisImageMap)
					{
						for(var m = 0;!haveImage &&  m < thisImageMap.length; m++)
						{
							if (thisImageMap[m].cid == colorId)
							{
								haveImage = true;
								break;
							}
						}
					}
					
					if (haveImage)
					{
						styleField.value = itemMap[i].cDesc;
						break;
					}
					else
					{
						styleField.value = itemMap[i].cDesc + " - No Image Available";
						break;
					}
				}
			}
		}
		else if (thisDropDown.selectedIndex == 0)
		{
			colorSelected =false;
			clearFields(thisDropDown,prodIndex);
		}
	}
}

function clearFields(thisDropDown,prodIndex)
{
	var dropName = thisDropDown.name.split("_");
	if (dropName != null && dropName.length > 1)
	{
		var imageCounterField = eval('document.orderForm.imageIndex_'+dropName[1]);
		var skuPriceRangeField = eval('document.orderForm.skuPriceRange_'+dropName[1]);

		if (skuPriceRangeField && skuPriceRangeField.value != "")
		{
			var pdtField = eval('document.orderForm.pdt_'+imageCounterField.value);
			var priceField = eval('document.orderForm.price_'+imageCounterField.value);
		
			if (pdtField.value.match("SKU"))
			{
				priceField.value = skuPriceRangeField.value;
			}
		}
		
	}
}
function swapImages(REGPath,ENHPath,prodIndex,colorFieldIndex,colorId)
{
	thisProdShot = eval('window.document.prodShot_'+prodIndex);
	thisProdShot.src = REGPath;
	thisEnh = eval('document.orderForm.enh_'+prodIndex);
	if (thisEnh)
	{
		thisEnh.value = ENHPath;
	}
	changeDropdown(prodIndex,colorFieldIndex,colorId);
}
function syncImages()
{
	orderFormElements = document.orderForm.elements;
	for (var i=0; i < orderFormElements.length; i++)
	{
		if (orderFormElements[i].type == "select-one")
		{
			var dropName = orderFormElements[i].name.split("_");
			
			if (dropName != null && dropName.length > 1)
			{
				imageCounterField = eval('document.orderForm.imageIndex_'+dropName[1]);
				swapImageDropdown(orderFormElements[i],imageCounterField.value);
				updateFields(orderFormElements[i],imageCounterField.value);
			}
		}
	}
}

function syncFields(thisField)
{
	var dropName = thisField.name.split("_");
	if (dropName != null && dropName.length > 1 && dropName[1] != null)
	{
		imageCounterField = eval('document.orderForm.imageIndex_'+dropName[1]);
		if (imageCounterField)
		{
			updateFields(thisField,imageCounterField.value);
		}
	}
}

function swapImageDropdown(dropDown,prodIndex)
{
	if (dropDown.selectedIndex > 0)
	{
		thisProdShot = eval('window.document.prodShot_'+prodIndex);
		thisEnh = eval('document.orderForm.enh_'+prodIndex);
		thisImageMap = eval('imageMap_'+prodIndex);
		
		if (thisImageMap)
		{
			var selectedItem = dropDown.options[dropDown.selectedIndex].text;
			for(var i = 0; i < thisImageMap.length; i++)
			{
				if (thisImageMap[i].name.toLowerCase() == selectedItem.toLowerCase())
				{
					thisProdShot.src = thisImageMap[i].reg;
					if (thisEnh)
					{
						thisEnh.value = thisImageMap[i].enh;
					}
				}
			}
		}
	}
}

function changeDropdown(prodIndex,colorFieldIndex,colorId)
{
	colorField = eval('document.orderForm.colors_'+colorFieldIndex);
	sizeField = eval('document.orderForm.prod_'+colorFieldIndex);
	var dropDownIndex = 0;
	
	for(var i = 0; i < colorArrayId.length; i++)
	{
		if (colorId == colorArrayId[i])
		{
			dropDownIndex = i+1;
			break;
		}
	}
	
	if (colorField  && colorField.options != null && colorField.options.length > 0)
	{
		colorField.options[dropDownIndex].selected = true;
	}
	else if (sizeField && sizeField.options != null && sizeField.options.length > 0)
	{
		sizeField.options[dropDownIndex].selected = true;
	}
	
	colorFieldString = "document.orderForm.colors_"+colorFieldIndex;
	sizeFieldString = "document.orderForm.prod_"+colorFieldIndex;
	
	if (colorField && sizeField)
	{
		populateSizes(colorField, sizeField); 
	}
	
	if (colorField)
	{
		updateFields(colorField,prodIndex);
	}
	else if (sizeField)
	{
		updateFields(sizeField,prodIndex);
	}
}




function verifyQty(toWish)
{
	var totalQty = 0;
	var nameStr;
	var nameStrValue;
	showFirstMessage = false;
	showLastMessage = true;
	haveOneError = false;

	qtyField = document.orderForm.quantity;
	nameStr = qtyField.name;

		if (isNaN(parseInt(qtyField.value)))
		{
			showFirstMessage = true;
			haveOneError = true;
		}
			
		if (!showFirstMessage)
		{
			for (j=0; j < qtyField.value.length; j++)
			{
				if( isNaN(parseInt(qtyField.value.charAt(j))) )
				{
					showFirstMessage = true;
					haveOneError = true;
					break;
				}
			}
		}		
			
		if(showFirstMessage)
		{
			alert(nameStr = " Qty should be a number which is greater than or equal to '1'.");
			qtyField.value = "1";
			qtyField.focus();
			showLastMessage = false;
		}
		else
		{
			totalQty = totalQty + parseInt(qtyField.value);
		}

	if (!haveOneError && totalQty > 0)
	{
		if(toWish == "yes")
		{
			document.orderForm.wlName.value = "default";
		}
		else
		{
			document.orderForm.wlName.value = "";
		}
		document.orderForm.submit();	
	}
	else if(showLastMessage)
	{
		alert("You must choose a quantity other than \"0\" before clicking on \"Add to Cart.\"");
	}
}

//changes the size drop down so that it contains only sizes for the selected color.
function updateSizeDropDown(sizes,groupNumber) {
	//sizeDropDown = $('drpCustomTagProductSize_' + grouping);
	//colorDropDown = $('drpCustomTagProductColor_' + grouping);
	sizeDropDown = $('drpCustomTagProductSize_' + groupNumber);
	colorDropDown = $('drpCustomTagProductColor_' + groupNumber);
	//set the size drop to default if color is at default
	if(colorDropDown.value == "-1") {
		sizeDropDown.value = "-1";
	} else {
		//reset the size drop
		sizeDropDown.length=1;
		firstSplit = sizes.split("~");
		if (sizeDropDown.type != "hidden") {
			for (var i = 0; i < firstSplit.length-1; i++) {
				secondSplit = firstSplit[i].split("|");
				var newOpt = document.createElement("OPTION");
				newOpt.value = secondSplit[0];
				newOpt.text = secondSplit[1];
				sizeDropDown.options[sizeDropDown.options.length] = newOpt;
			}
		}
	}
}

function verifyFields(submitProcess) {
	//check that a color, size, and qty have been entered.
	//check all color,size,qty groupings
	document.getElementById('prodCounter').value = grouping + 1;
	var colorDropArr = new Array();
	var sizeDropArr = new Array();
	var qtyArr = new Array();
	var prodSelectedArr = new Array();
	var colorSelectedArr = new Array();
	var sizeSelectedArr = new Array();
	var qtySelectedArr = new Array();
	for (var i = 0; i <= grouping; i++) {
		colorDropArr[i] = $('drpCustomTagProductColor_' + i);
		sizeDropArr[i] = $('drpCustomTagProductSize_' + i);
		qtyArr[i] = $('qty_' + i);

		prodSelectedArr[i] = false;
		colorSelectedArr[i] = false;
		sizeSelectedArr[i] = false;
		qtySelectedArr[i] = false;

		if (colorDropArr[i].value != "-1"
				&& sizeDropArr[i].value != "-1"
				&& !isNaN(parseInt(qtyArr[i].value))
				&& qtyArr[i].value > 0) {
		//	alert('colorArr['+i+']::'+ colorDropArr[i].value);
		//	alert('sizeArr['+i+']::'+sizeDropArr[i].value);
		//	alert('qtyArr['+i+']::'+ qtyArr[i].value);
			prodSelectedArr[i] = true;
			colorSelectedArr[i] = true;
			sizeSelectedArr[i] = true;
			qtySelectedArr[i] = true;
			continue;
		}
		if (colorDropArr[i].value != "-1") {
			colorSelectedArr[i] = true;
		}
		if (sizeDropArr[i].value != "-1") {
			sizeSelectedArr[i] = true;
		}
		if (!isNaN(parseInt(qtyArr[i].value)) && qtyArr[i].value > 0) {
			qtySelectedArr[i] = true;
		}
	}
	var prodSelected = false;
	for (var i = 0; i <= grouping; i++) {
		if (prodSelectedArr[i] == true) {
			prodSelected = true;
			continue;
		}
		if (colorSelectedArr[i] == true && sizeSelectedArr[i] == true) {
			alert("Invalid quantity specified.");
			return;
		} else if (colorSelectedArr[i] == true) {
			alert("Please select a size.");
			return;
		} else if (sizeSelectedArr[i] == true) {
			alert("Please select a color.");
			return;
		}
	}

	if (prodSelected == false) {
		alert("Please select a color.");
		return;
	}

	//if we got this far everything should be ok.  
	//before the form can be submitted the prod_N has to be set with prodId|sku
	for (var i = 0; i <= grouping; i++) {
		colorDrop = $('drpCustomTagProductColor_' + i);
		sizeDrop = $('drpCustomTagProductSize_' + i);

		skuId = "";
		for (var k = 0; k < mainSizeMap.length; k++) {
			if (mainSizeMap[k].sizeId == sizeDrop.value && mainSizeMap[k].colorId == colorDrop.value) {
				skuId = mainSizeMap[k].sku;
				break;
			}
		}
		$('prod_' + i).value = prodId + "|" + skuId;
		//set global array of objects to show added products in minicart
		if (prodId != null && prodId != '' && skuId != null && skuId != '') {
			loadNewCartProducts(prodId, skuId);
		}
	}

	//submit the form
	if (submitProcess == "wishlist") {
		document.orderForm.wlName.value = "wlName";
		document.orderForm.submit();
		return;
	} else {
		//start - code added for defect id: 35662
		if(document.orderForm.minicartDisplay.value == "true")
		{
			ajaxAddToCart(document.orderForm);			
		}
		else
		{
			document.orderForm.submit();
		}
		//ajaxAddToCart(document.orderForm);
		//end - code added for defect id: 35662
	}
}

var oldclass="front";
function swapImage(prodIndex,frontback,arrowImageName,pixelImage,selectedTd,nonSelectedTd) {
	var upImage= document.getElementById(arrowImageName);
	var pixelImage = document.getElementById(pixelImage);
	if(upImage)
	{
		upImage.src='/images/arrow_up.jpg';
	}
	if(pixelImage)
	{
		pixelImage.src='/images/pixel.gif';
	}
	/*var curSelectedTd = document.getElementById(selectedTd);
	var curNonSelectedTd = document.getElementById(nonSelectedTd);
	if(curSelectedTd)
	{
		curSelectedTd.className=("boldView");
	}
	if(curNonSelectedTd)
	{
		curNonSelectedTd.className=("normalView");
	}*/
	document.getElementById(oldclass).className=("normalView");
	document.getElementById(selectedTd).className=("boldView");
	oldclass=selectedTd;
	if(colorSelected == false)
	{
		var thisProdShot = eval('window.document.prodShot_'+prodIndex);
		if(thisProdShot)
		{
			thisProdShot.src= prodImageMap[0].frontback;
			if(frontback=="back")
			{
				thisProdShot.src= prodImageMap[0].back;
			}
			else
			{	
				thisProdShot.src= prodImageMap[0].main;
			}
		}
		
	}
	else
	{
		prodDropDown = eval('document.orderForm.prod_'+prodIndex);
		colorDropDown = eval('document.orderForm.colors_'+prodIndex);
		var colorId;
		var productId ;

		if(colorDropDown && colorDropDown.selectedIndex > 0)
		{
		var pidColor=colorDropDown.options[colorDropDown.selectedIndex].value;
		var pidColorArray = pidColor.split("|");
		colorId= pidColorArray[1];
		productId = pidColorArray[0];
		}

		else if(prodDropDown && prodDropDown.selectedIndex >0 )
		{
			var pidSku = prodDropDown.options[prodDropDown.selectedIndex].value;
			var pidSkuArray = pidSku.split("|"); 
			var sku =pidSkuArray[1];
			productId = pidSkuArray[0];
			
			for(var m=0;m < itemMap.length ; m++)
			{
				if(itemMap[m].pid == productId && itemMap[m].sku == sku)
						{	

						   colorId= itemMap[m].cId;
						 }
			 }

			
		}

		
		var thisImageMap = eval('imageMap_' + prodIndex);

		for(i=0; thisImageMap && i<thisImageMap.length; i++)
		{

			if(frontback=="back")
			{
				if( colorId==null )
				{
					thisProdShot = eval('window.document.prodShot_'+prodIndex);
					thisProdShot.src = thisImageMap[i].back;
				}
			
				else if( thisImageMap[i].cid == colorId )	
				{
					if(thisImageMap[i].back.length > 0)
					{
						thisProdShot = eval('window.document.prodShot_'+prodIndex);
						thisProdShot.src = thisImageMap[i].back;		
						break;
					}
				}
			}
			else if(frontback=="main")
			{
				if( colorId==null )
				{
					thisProdShot = eval('window.document.prodShot_'+prodIndex);
					thisProdShot.src = thisImageMap[i].enh;
				}
			
				else if( thisImageMap[i].cid == colorId )	
				{
					if(thisImageMap[i].enh.length > 0)
					{
						thisProdShot = eval('window.document.prodShot_'+prodIndex);
						thisProdShot.src = thisImageMap[i].enh;		
						break;
					}
				}

			}
			
		}	

	}
}