
function onLoad() {
	if (getObj("selCategory").value != origCategoryId) categoryChanged();
	else if (origProdCatId != "")
		if (getObj("selProduct").value != origProdCatId) productChanged();
		else if (origSubProdCatId != "")
			if (getObj("selSubProduct").value != origSubProdCatId) subProductChanged();
	var styleCnt, colorSelObj;
	for (styleCnt = 0; styleCnt < stylesCount; styleCnt++) {
		if (getObj("size" + styleCnt)) sizeChanged(styleCnt);
		colorSelObj = getObj("color" + styleCnt);
		if (colorSelObj) {
			if (colorSelObj.options) if (colorSelObj.offsetWidth > 208) colorSelObj.style.width = "208px";}}
}
function categoryChanged() {
	var parentId = getObj("selCategory").value; var selProdObj = getObj("selProduct");
	buildCatSelectOptions(selProdObj, parentId, ""); productChanged(); //was Last
}
function productChanged() {
	var parentId = getObj("selProduct").value, subCatIds, divSubProdObj = getObj("divSubProduct");
	if (categories[parentId]) subCatIds = categories[parentId]["subCats"]; else subCatIds = new Array();
	if (subCatIds.length > 0) {
		buildCatSelectOptions(getObj("selSubProduct"), parentId, ""); //was First
		divSubProdObj.style.display = "block"; subProductChanged();}
	else {
		divSubProdObj.style.display = "none"; buildSizeSelectOptions(parentId);}
}
function subProductChanged() {
	buildSizeSelectOptions(getObj("selSubProduct").value);
}
function buildCatSelectOptions(selObj, parentId, viewAllPos) {
	var subCatCnt, subCatIds = categories[parentId]["subCats"], subCatId; var optionPos = 0;
	if (viewAllPos == "First") optionPos = makeSelectOption(selObj, optionPos, "View All ", "ALL");
	for (subCatCnt = 0; subCatCnt < subCatIds.length; subCatCnt++) {
		subCatId = subCatIds[subCatCnt]; 
		optionPos = makeSelectOption(selObj, optionPos, categories[subCatId]["name"], subCatId);}
	if (viewAllPos == "Last") optionPos = makeSelectOption(selObj, optionPos, "View All", "ALL");
	selObj.options.length = optionPos;
}
function buildSizeSelectOptions(sizesCatId) {
	var sizingIds = new Array(), sizes, selObj, optionPos, sizeCnt, sizeDisp = "none";
	if (categories[sizesCatId]) sizingIds = categories[sizesCatId]["sizingIds"];
	if (sizingIds.length == 1) {
		sizes = sizing[sizingIds[0]]; selObj = getObj("selSize"); optionPos = 0;
		optionPos = makeSelectOption(selObj, optionPos, "All Sizes ", "ALL");
		var sizingTypeIds = categories[sizesCatId]["sizingTypeIds"];
		var sizeInfo, useSize, sizingTypeCnt, sizingTypeId;
		for (sizeCnt = 0; sizeCnt < sizes.length; sizeCnt++) {
			sizeInfo = sizes[sizeCnt]; useSize = false; sizingTypeCnt = 0;
			while (!useSize && (sizingTypeCnt < sizingTypeIds.length)) {
				sizingTypeId = sizingTypeIds[sizingTypeCnt]; sizingTypeCnt++;
				if (sizeInfo["SizingTypeIds"][sizingTypeId]) {useSize = true;
					optionPos = makeSelectOption(selObj, optionPos, sizeInfo["Size"], sizeInfo["Id"]);}}}
		selObj.options.length = optionPos; sizeDisp = "block";}
	getObj("divSizeLbl").style.display = sizeDisp; getObj("divSize").style.display = sizeDisp;
}
function makeSelectOption(selObj, position, text, value) {
	var optionObj, isSelected; isSelected = false; if (position == 0) isSelected = true;
	if (position >= selObj.options.length) {
		addSelectOption(selObj, checkOptionText(text), value, isSelected);}
	else {
		optionObj = selObj.options[position]; optionObj.text = checkOptionText(text); optionObj.value = value;
		optionObj.selected = isSelected;}
	return ++position;
}
function checkOptionText(text) {
	if (text.indexOf("&uuml;") >= 0) {
		var utilDivObj = getObj("utilDiv"); utilDivObj.innerHTML = text; return utilDivObj.innerHTML;}
	else return text;
}
function submitClicked() {
	var categoriesPath = getObj("selCategory").value; var prodCatId = getObj("selProduct").value;
	categoriesPath += "*" + prodCatId; var divSubProdObj = getObj("divSubProduct"), sizeId = "";
	if (divSubProdObj.style.display != "none") categoriesPath += "*" + getObj("selSubProduct").value;
	if (getObj("divSize").style.display != "none") sizeId = getObj("selSize").value;
	var showSoldOuts = "n"; if (getObj("showSoldOuts").checked) showSoldOuts = "y";
	var url = urlBase + "?scpath=" + categoriesPath + "&ssi=" + sizeId;
	window.location.href = url + "&sso=" + showSoldOuts;
}
function getObj(objId) {return document.getElementById(objId);}
var style, colorId, imageObj, size, sizeInfo, prefix, stylesColorInfo = new Array();
var stylesColorImages = new Object(), sizes = new Object(), currentColor = new Object();
var styleColorIds, styleColorNames, styleSizes, styleDispSizes;
var availabilityInfo, styleCnt, swatches = new Object(), swatchesScrollPixels = new Object();
var styleNames = new Object(), stylesColorImageIds = new Object(), styleHrefs = new Array();
function sizeChanged(styleCnt) {
	var sizeSelObj = getObj("size" + styleCnt);
	var newSize = sizeSelObj.value; var sizeInfo = sizes[styleCnt][newSize];
	var currentColorId = currentColor[styleCnt], colorCnt, colorId, colorDisp, colorOption;
	var colorSelObj = getObj("color" + styleCnt); var colorSelected;
	if (colorSelObj.options) {
		for (colorCnt = 0; colorCnt < sizeInfo.length; colorCnt++) {
			colorId = sizeInfo[colorCnt]['colorId'];
			colorDisp = sizeInfo[colorCnt]['colorDisp'].replace(/&nbsp;/gi," ");
			colorSelected = false; if (colorId == currentColorId) colorSelected = true;
			if (colorCnt >= colorSelObj.options.length) {
				addSelectOption(colorSelObj, colorDisp, colorId, colorSelected);}
			colorOption = colorSelObj.options[colorCnt]; colorOption.value = colorId;
			colorOption.text = colorDisp; colorOption.selected = colorSelected;}
		colorSelObj.options.length = sizeInfo.length;}
	else {
		getObj("colorDiv" + styleCnt).innerHTML = sizeInfo[0]['colorDisp'].replace(/&nbsp;/gi," ");}
}
function colorChanged(styleCnt) {
	var newColorId = getObj("color" + styleCnt).value, currentColorId = currentColor[styleCnt];
	currentColor[styleCnt] = newColorId;
	document.images["styleImage"+styleCnt].src = stylesColorImages[styleCnt][newColorId].src;
	var imageId = stylesColorImageIds[styleCnt][newColorId], styleHref = styleHrefs[styleCnt];
	if (imageId != "") styleHref += "&simg=" + imageId;
	getObj("styleImgLink" + styleCnt).href = styleHref;
	var swatchFrameObj = getObj(styleCnt + "_" + currentColorId);
	if (swatchFrameObj != null) {
		swatchFrameObj.style.backgroundColor = "#ffffff"; swatchFrameObj.style.borderColor = "#ffffff";}
	swatchFrameObj = getObj(styleCnt + "_" + newColorId);
	if (swatchFrameObj != null) {
		swatchFrameObj.style.backgroundColor = imgBrdrClr; swatchFrameObj.style.borderColor = imgBrdrClr;}
	scrollSwatchIntoView(styleCnt, newColorId);
}
function scrollSwatchIntoView(styleCnt, colorId) {
	if (swatches[styleCnt]["count"] > 6) {
		var pixelsPerPos = swatchesScrollPixels[styleCnt + "_Sw"];
		var colorPos = swatches[styleCnt][colorId];
		var scrollDivObj = getObj("tsbScroll_" + styleCnt + "_Sw_1");
		var leftPixels = parseInt(scrollDivObj.style.left) * -1;
		var firstDisplayedPos = (leftPixels / pixelsPerPos) + 1;
		var newFirstDisplayedPos = firstDisplayedPos;
		if (colorPos < firstDisplayedPos) newFirstDisplayedPos = colorPos;
		if (colorPos > (firstDisplayedPos + 5)) newFirstDisplayedPos = colorPos - 5;
		scrollDivObj.style.left = (((newFirstDisplayedPos-1)*pixelsPerPos)*-1) + "px";
		tsbUpdateArrowDisplay(styleCnt + "_Sw", null);}
}
function swatchClicked(styleCnt, colorId) {
	getObj("color" + styleCnt).value = colorId; colorChanged(styleCnt);
}
var serverResponse, serverResponseId = null, serverRequestTime, addingToBag = false;
function showMsg(message) {
	getObj("msgBoxTMsgDiv").innerHTML = message; getObj("msgBoxBMsgDiv").innerHTML = message;
	var boxDisplay = "block"; if (message == "") boxDisplay = "none";
	getObj("msgBoxTDiv").style.display = boxDisplay; getObj("msgBoxBDiv").style.display = boxDisplay;
}
function makeUid() {
	var dateTime = new Date(); return dateTime.getTime() + Math.random();
}
function resetQtys() {
	var styleCnt, qtySelObj;
	for (styleCnt = 0; styleCnt < stylesColorInfo.length; styleCnt++) {
		qtySelObj = getObj("qty" + styleCnt); if (qtySelObj != null) qtySelObj.value = 0;}
}
function addToBagClicked() {
	if (addingToBag == false) {
		var errMsg = "", styleCnt, style, qtySelObj, qty, styleName, sizeSelObj, size;
		var sku, skus = "", qtys = "", totalQty = 0, hasQtySel = false, fromCatIds = "";
		for (styleCnt = 0; (styleCnt < stylesColorInfo.length) && (errMsg == ""); styleCnt++) {
			style = stylesColorInfo[styleCnt]["style"]; qtySelObj = getObj("qty" + styleCnt); qty = 0;
			if (qtySelObj != null) {qty = qtySelObj.value; hasQtySel = true;}
			if (qty > 0) {
				styleName = styleNames[styleCnt]; totalQty += qty;
				sizeSelObj = getObj("size" + styleCnt); size = "";
				if (sizeSelObj != null) size = sizeSelObj.value;
				if (errMsg == "") {
					prefix = stylesColorInfo[styleCnt]["prefix"];
					sku = prefix + style + "-" + currentColor[styleCnt];
					if (size != "" && size.toLowerCase() != "onesize") sku += "-" + size;
					if (skus != "") {skus += "|"; qtys += "|"; fromCatIds += "|";}
					skus += sku; qtys += qty; fromCatIds += stylesColorInfo[styleCnt]["categoryId"];}}}
		if (errMsg == "" && totalQty < 1) {
			if (hasQtySel == true) {
				errMsg = "Please select a quantity for the items you want to add to your bag.";}
			else {errMsg = "There are no items available to add to your bag.";}}
		if (errMsg != "") showMsg("<span class=\"errorTxt\">" + errMsg + "</span>");
		else {
			addingToBag = true; availabilityInfo = ""; var msg = "This item is";
			if (totalQty > 1) msg = "These items are"; showMsg(msg + " being added to your bag . . .");
			var params = "skus=" + skus + "&qtys=" + qtys + "&all=usessi";
			params += "&from=VA|||||" + paramsList + "&fromCatIds=" + fromCatIds;
			var dateTime = new Date(); serverRequestTime = dateTime.getTime();
			serverResponse = ""; var newScriptObj = document.createElement("SCRIPT");
			newScriptObj.type = "text/javascript";
			newScriptObj.src = "xt_addItemsToBag.asp?uid=" + makeUid() + "&" + params;
			document.getElementsByTagName('body')[0].appendChild(newScriptObj);
			serverResponseId = window.setInterval("checkForServerResponse();", 500);}}
}
function checkForServerResponse() {
	if (serverResponse == "") {
		var dateTime = new Date(); var elapsedMillis = dateTime.getTime() - serverRequestTime;
		if (elapsedMillis > 40000) {
			window.clearInterval(serverResponseId); window.location.href = bagUrl + makeUid();
			addingToBag = false;}}
	else {
		addingToBag = false; window.clearInterval(serverResponseId);
		var responseArray = serverResponse.split(US), result = responseArray[0], requestedQty;
		var msg = "", add2BagTImgObj = getObj("add2BagTImg"), add2BagBImgObj = getObj("add2BagBImg"), skusStatus;
		var skusStatusArray, skuStatusArray, skuCnt;
		var beginCOBttnTDivObj = getObj("beginCOBttnTDiv"), beginCOBttnBDivObj = getObj("beginCOBttnBDiv");
		if (result == "Error") {
			msg = "A server error has occurred.<br>Please contact customer service.";
			window.location.href = pageErrorUrl;}
		else availabilityInfo = responseArray[4];
		if (result == "Added") {
			resetQtys();
			var qtyAdded = responseArray[3]; skusStatus = responseArray[5]; msg = "This item has";
			if (qtyAdded > 1) msg = "These items have"; msg += " been placed in your bag";
			if (skusStatus == "") msg += ".";
			else {
				msg += ":"; skusStatusArray = skusStatus.split(GS); var avail, status, availLC;
				for (skuCnt = 0; skuCnt < skusStatusArray.length; skuCnt++) {
					skuStatusArray = skusStatusArray[skuCnt].split(FS);
					avail = skuStatusArray[2]; availLC = avail.toLowerCase(); status = avail;
					if (availLC != "available") {
						status = "<a class=\"errorTxt\" href=\"javascript:";
						if (availLC.substr(0,3) == "low") status += "openHelpWindow('136')";
						else status += "showItemAvailability()"; status += ";\">" + avail + "</a>";}
					msg += vSpace(5) + "Item: " + skuStatusArray[0] + "<br>" + skuStatusArray[1];
					msg += "<br>Availability: " + status;}}
			beginCOBttnTDivObj.style.display = "block"; beginCOBttnBDivObj.style.display = "block";
			add2BagTImgObj.src = add2BagLoImg.src; add2BagBImgObj.src = add2BagLoImg.src;
			if (window.cmCreateShopAction5Tag && window.cmDisplayShop5s) {
				var coremetricsData = responseArray[6].split(GS); var cdCnt, skuData;
				for (cdCnt = 0; cdCnt < coremetricsData.length; cdCnt++) {
					skuData = coremetricsData[cdCnt].split(FS);
					cmCreateShopAction5Tag(skuData[0], skuData[1], skuData[2], skuData[3],
						skuData[4], skuData[5]);}
				cmDisplayShop5s();}}
		if (result == "SoldOut") {
			requestedQty = responseArray[2];
			window.setTimeout("showItemAvailability();",100); skusStatus = responseArray[5];
			msg = "<span class=\"errorTxt\">The quantity you requested for the following item";
			if (requestedQty > 1) msg += "s"; msg += " is no longer available:";
			skusStatusArray = skusStatus.split(GS);
			for (skuCnt = 0; skuCnt < skusStatusArray.length; skuCnt++) {
				skuStatusArray = skusStatusArray[skuCnt].split(FS);
				msg += vSpace(5) + "Item: " + skuStatusArray[0] + "<br>" + skuStatusArray[1];}
			msg += vSpace(5) + "<a class=\"errorTxt\" href=\"javascript:";
			msg += "showItemAvailability();\">Size and color availability</a></span>";
			beginCOBttnTDivObj.style.display = "none"; beginCOBttnBDivObj.style.display = "none";
			add2BagTImgObj.src = add2BagHiImg.src; add2BagBImgObj.src = add2BagHiImg.src;}
		showMsg(msg);}
}
function showItemAvailability() {
	window.open(prodAvailUrl + "?src=bag", "ProductAvailability",
		"width=600,height=500,top=99,left=99,scrollbars=Yes,resizable=Yes");
}
function vSpace(pixels) {
	var html = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>";
	html += "<td><img src=\"images/global/shim.gif\" border=\"0\" height=\"" + pixels;
	return html + "\" width=\"1\"></td></tr></table>";
}
