function getClickedSize(clickedsize) {
var el;
var found = false;
var cont = 0;

	if (ncolors > 0 && selectedcolor == -1) {
		alert("Please select a color first");
		return(-1);
	}
	el = document.getElementById('size' + clickedsize);
	while (!found && cont < nsizes) {
		if (ncolors == 0 && jsarray[cont][0] == el.innerHTML)
			found = true
		else if (ncolors > 0 && jsarray[selectedcolor][2][cont][0] == el.innerHTML) 
			found = true;
		cont++;
	}
	if (found)
		return(cont - 1)
	else
		return(-1);
}

function fnAddCart() {
var ret;
	scroll(0,0);
	if (nsizes > 0 && ncolors > 0) 
		ret = addCart(jsarray[selectedcolor][2][getClickedSize(selectedsize)][2])
	else if (nsizes > 0) 
		ret = addCart(jsarray[getClickedSize(selectedsize)][2])
	else if (ncolors > 0)
		ret = addCart(jsarray[selectedcolor][3])
	else 
		ret = addCart(jsarray[1]);
if (to_shoppingcart) setTimeout("window.location.href = '/pages/misc/secure/shopcart.asp';",1000);
	if (nsizes > 1) {
		selectsize(-1);
		selectedsize = -1;
	}
	
	if (ncolors > 1) {
		selectcolor(-1);
		selectedcolor = -1;
	}
	
	return(ret);
}

function addReminder() {
	if (nsizes > 0 && ncolors > 0)
		alert('Remember to select a color and a size')
	else if (nsizes > 0) 
		alert('Remember to select a size')
	else if (ncolors > 0)
		alert('Remember to select a color');
	return(false);
}

function selectsize(clickedsize) {
var addCartButton;
var size;
var cont = 0;
var found = false;
var pform;
var sizeElem;
var basePriceElem;
var regPriceElem;
var sizePriceDiv;
var extPriceDiv;
var	extbasePrice = document.getElementById('extbasePrice');

	sizeElem = document.getElementById('size' + selectedsize);
	addCartButton = document.getElementById('addToCart');
	pform = document.getElementById('productform');
	if (clickedsize == -1 && sizeElem) {
		sizeElem.className = 'notselectedsize';
		//addCartButton.disabled = true;
		addCartButton.src = '/img/add_to_bag_button_light.jpg';
		addCartButton.onclick = addReminder;
		document.getElementById('chosensize').innerHTML = '';
		if (pform) {
			pform.frmSize.value = '';
			pform.frmQty.value = '1';
		}
		return;
	}
	
	size = getClickedSize(clickedsize);
	if (size == -1) return;
	
	if (ncolors > 0 && selectedcolor < 0) return;
	if ((ncolors > 0 && jsarray[selectedcolor][2][size][3] == 'N') ||
		(ncolors == 0 && jsarray[size][3] == 'N')) {
		sizeElem = document.getElementById('size' + clickedsize);
		if (sizeElem) document.getElementById('chosensize').innerHTML = sizeElem.innerHTML + " not available";
		return;
	}

	if (ncolors == 0 || selectedcolor >= 0) {
		if (selectedsize >= 0) {
			if (sizeElem) 
				sizeElem.className = 'notselectedsize';
		}

		selectedsize = clickedsize;
		sizeElem = document.getElementById('size' + selectedsize);

		if (sizeElem)
			sizeElem.className = 'selectedsize';

		if (addCartButton && ((document.getElementById('colors') && selectedcolor != -1) || !document.getElementById('colors'))) {
			//addCartButton.disabled = false;
			addCartButton.onclick = fnAddCart;
			addCartButton.src = '/img/add_to_bag_button.jpg';
		}

		document.getElementById('chosensize').innerHTML = sizeElem.innerHTML;
		if (pform)
			pform.frmSize.value = sizeElem.innerHTML;
	}
	basePriceElem = document.getElementById('basePrice');
	regPriceElem = document.getElementById('regPrice');
	sizePriceDiv = document.getElementById('sizePriceDiv');
	extPriceDiv = document.getElementById('extPriceDiv');
	if (basePriceElem) {
		var s;
		cont = basePriceElem.innerHTML.indexOf(" ");
		if (cont == -1) 
			s = ''
		else
			s = basePriceElem.innerHTML.substr(cont);
		if (ncolors > 0) {
			basePriceElem.innerHTML = '$' + jsarray[selectedcolor][2][size][1].toFixed(2) + ' ' + s;
			if (extPriceDiv) extPriceDiv.innerHTML = "Normal price: $" + ext_orig_price[jsarray[selectedcolor][2][size][2]].toFixed(2) + ".<br>For " + ext_name + " customers:";
			if (extbasePrice) extbasePrice.innerHTML = "";
		} else {
			basePriceElem.innerHTML = '$' + jsarray[size][1].toFixed(2) + ' ' + s;
			if (extPriceDiv) extPriceDiv.innerHTML = "Normal price: $" + ext_orig_price[jsarray[size][2]].toFixed(2) + ".<br>For " + ext_name + " customers:";
			if (extbasePrice) extbasePrice.innerHTML = "";
		}
	}
	if (regprice > 0 && regPriceElem) {
		for (cont = 0; cont <= nsizes && regPrices[cont].split(',')[0] != sizeElem.innerHTML; cont++) ;
		if (regPrices[cont].split(',')[0] == sizeElem.innerHTML) 
			regPriceElem.innerHTML = regPrices[cont].split(',')[1];
	}
	if (sizePriceDiv) sizePriceDiv.innerHTML = '';
}

function showsizes(color) {
//Returns # of clickable sizes
	var cont = 0;
	var isThere;
	var el;
	var canBuy;
	var numAvailSizes = 0;

	while (el = document.getElementById('size' + cont++)) {
		if (color == -1 && ncolors > 1) {
			el.className = "notselectablesize";
			el.onmouseover = null;
			el.onmouseout = null;
		} else {
			isThere = false;
			for (var cont2 = 0; cont2 < jsarray[color][2].length && !isThere; cont2++) {
				if (el.innerHTML == jsarray[color][2][cont2][0])
					isThere = true;
			}
			if (isThere) {
				el.style.display = "block";
				canBuy = jsarray[color][2][--cont2][3];
				if (canBuy == 'N') {
					el.className = "notselectablesize";
					el.onmouseover = shownotavail;
					el.onmouseout = showavail;
				} else if (canBuy == '1') {
					el.className = "notselectedsize";
					el.onmouseover = show1avail;
					el.onmouseout = showavail;
					numAvailSizes += 1;
				} else if (canBuy == '2') {
					el.className = "notselectedsize";
					el.onmouseover = show2avail;
					el.onmouseout = showavail;
					numAvailSizes += 1;
				} else {
					el.className = "notselectedsize";
					el.onmouseover = null;
					el.onmouseout = null;
					numAvailSizes += 1;
				}
			} else {
				el.style.display = "none";
				//el.className = "notselectablesize";
				//el.onmouseover = shownotavail;
				//el.onmouseout = showavail;
			}
		}
	}
	return(numAvailSizes);
}

function showsizeprices(color) {
var prevPrice = 0;
var basePrice;
var strSize = "";
var prevSize;
var cont;
var sizePriceDiv;
var regPriceElem;
var basePriceElem;
var extBasePriceElem;
var divText;
var text1;

	sizePriceDiv = document.getElementById('sizePriceDiv');
	basePriceElem = document.getElementById('basePrice');
	extBasePriceElem = document.getElementById('extbasePrice');
	regPriceElem = document.getElementById('regPrice');
	if (sizePriceDiv) {
		if (color >= 0) {
			if (nsizes > 0) {
				for (cont = 0; cont < jsarray[color][2].length; cont++) {
					if (strSize == "") {
						strSize = jsarray[color][2][cont][0];
						prevPrice = jsarray[color][2][cont][1];
						basePrice = prevPrice;
						divText = '<table cellspacing="0" cellpadding="0">';
						if (ext_orig_price[jsarray[color][2][cont][2]] && extBasePriceElem) {
							var ext_full = ext_orig_price[jsarray[color][2][cont][2]];
							extBasePriceElem.innerHTML = '<div style="text-decoration:line-through">$' + ext_full + '<\/div> ' + ext_name + ': ';
						}
						basePriceElem.innerHTML = '$' + basePrice.toFixed(2);
					} else {
					//alert(cont + '.' + jsarray[color][2]);
						if (jsarray[color][2][cont][1] != prevPrice) {
						//alert(jsarray[color][2][cont][1]  + '..' + prevPrice + '..' + basePrice + ':' + strSize + ':' + prevSize);
							if (strSize != prevSize) strSize += " - " + prevSize;
							if (strSize.trim() != "")
								text1 = strSize + " add $"
							else
								text1 = "";
							if (prevPrice - basePrice >= .01)
								divText += '<tr><td class="prod_text1"><b>' + text1 + (Math.round((prevPrice - basePrice)*100)/100).toFixed(2) + '</b></td></tr>';
							prevPrice = jsarray[color][2][cont][1];
							strSize = jsarray[color][2][cont][0];
						}
						prevSize = jsarray[color][2][cont][0];
					}
				}
				if (strSize != prevSize) strSize += " - " + prevSize;
				if (strSize.trim() != "")
					text1 = strSize + " add $"
				else
					text1 = "";
				if (prevPrice - basePrice >= .01) 
					divText += '<tr><td class="prod_text1"><b>' + text1 + (Math.round((prevPrice - basePrice)*100)/100).toFixed(2) + '</b></td></tr>';
				if (divText != '<table cellspacing="0" cellpadding="0">')
					divText += '</table>';
				if (basePriceElem) {
					if (jsarray[color][4] == '1') {
						basePriceElem.style.color = 'red';
						basePriceElem.innerHTML += " SALE!";
					} else
						basePriceElem.style.color = 'black';
				}

			} else {
				//divText = '<b class="prod_text1">$' + jsarray[color][2] + '</b>';
				divText = '';
				if (basePriceElem) {
				//divText = '<b class="prod_text1">$' + jsarray[color][2] + '</b>';
					if (jsarray[color][6] == '1') {
						basePriceElem.innerHTML = "$" + jsarray[color][2].toFixed(2) + " SALE!";
						basePriceElem.style.color = 'red'
					} else {
						basePriceElem.innerHTML = "$" + jsarray[color][2].toFixed(2);
						basePriceElem.style.color = 'black';
					}
					if (ext_orig_price[jsarray[color][3]] && extBasePriceElem) {
						var ext_full = ext_orig_price[jsarray[color][3]];
						extBasePriceElem.innerHTML = '<div style="text-decoration:line-through">$' + ext_full + '<\/div> ' + ext_name + ': ';
					}
					//basePriceElem.innerHTML = '$' + basePrice.toFixed(2);
				}
			}
		} else if (nsizes > 0 && ncolors > 0) {
			divText = ''
		} else if (nsizes > 0) {
			for (cont = 0; cont < jsarray.length; cont++) {
				if (strSize == "") {
					strSize = jsarray[cont][0];
					prevPrice = jsarray[cont][1];
					basePrice = prevPrice;
					divText = '<table cellspacing="0" cellpadding="0">';
					if (basePriceElem) {
						basePriceElem.innerHTML = '$' + basePrice.toFixed(2);
						if (ext_orig_price[jsarray[cont][2]] && extBasePriceElem) {
							var ext_full = ext_orig_price[jsarray[cont][2]];
							extBasePriceElem.innerHTML = '<div style="text-decoration:line-through">$' + ext_full + '<\/div> ' + ext_name + ': ';
						}
						//basePriceElem.innerHTML = '$' + basePrice.toFixed(2);
					}
				} else {
				//alert(cont + '.' + jsarray[color][2]);
					if (jsarray[cont][1] != prevPrice) {
					//alert(jsarray[color][2][cont][1]  + '..' + prevPrice + '..' + basePrice + ':' + strSize + ':' + prevSize);
						if (strSize != prevSize) strSize += " - " + prevSize;
						if (strSize.trim() != "")
							text1 = strSize + " add $"
						else
							text1 = "";
						if (prevPrice - basePrice >= .01)
							divText += '<tr><td class="prod_text1"><b>' + text1 + (Math.round((prevPrice - basePrice)*100)/100).toFixed(2) + '</b></td></tr>';
						prevPrice = jsarray[cont][1];
						strSize = jsarray[cont][0];
					}
					prevSize = jsarray[cont][0];
				}
			}
			if (strSize != prevSize) strSize += " - " + prevSize;
			if (strSize.trim() != "")
				text1 = strSize + " add $"
			else
				text1 = "";
			if (prevPrice - basePrice >= .01) 
				divText += '<tr><td class="prod_text1"><b>' + text1 + (Math.round((prevPrice - basePrice)*100)/100).toFixed(2) + '</b></td></tr>';
			if (divText != '<table cellspacing="0" cellpadding="0">')
				divText += '</table>'
			else
				divText = '';
			if (basePriceElem) {
				if (jsarray[0][4] == '1') {
					basePriceElem.style.color = 'red'
					basePriceElem.innerHTML += " SALE!";
				} else
					basePriceElem.style.color = 'black';
			}
		} else if (ncolors > 0) {
			for (cont = 0; cont < jsarray.length; cont++) {
				if (strSize == "") {
					strSize = jsarray[cont][0];
					prevPrice = jsarray[cont][2];
					basePrice = prevPrice;
					divText = '<table cellspacing="0" cellpadding="0">';
					if (basePriceElem) {
						if (ext_orig_price[jsarray[cont][3]]) {
							var ext_full = ext_orig_price[jsarray[cont][3]];
							basePriceElem.innerHTML = '$' + basePrice.toFixed(2) + '(' + ext_full + ')';
						} else {
							basePriceElem.innerHTML = '$' + basePrice.toFixed(2);
						}
					}
				} else {
					if (jsarray[cont][1] != prevPrice) {
						if (strSize != prevSize) strSize += " - " + prevSize;
						if (strSize.trim() != "")
							text1 = strSize + " add $"
						else
							text1 = "";
						if (prevPrice - basePrice >= .01)
							divText += '<tr><td class="prod_text1"><b>' + text1 + (Math.round((prevPrice - basePrice)*100)/100).toFixed(2) + '</b></td></tr>';
						prevPrice = jsarray[cont][2];
						strSize = jsarray[cont][0];
					}
					prevSize = jsarray[cont][0];
				}
			}
			if (strSize != prevSize) strSize += " - " + prevSize;
			if (strSize.trim() != "")
				text1 = strSize + " add $"
			else
				text1 = "";
			if (prevPrice - basePrice >= .01) 
				divText += '<tr><td class="prod_text1"><b>' + text1 + (Math.round((prevPrice - basePrice)*100)/100).toFixed(2) + '</b></td></tr>';
			if (divText != '<table cellspacing="0" cellpadding="0">')
				divText += '</table>'
			else
				divText = '';
			/*if (basePriceElem) {
				if (jsarray[color][6] == '1') {
					basePriceElem.style.color = 'red'
					basePriceElem.innerHTML += " SALE!";
				} else
					basePriceElem.style.color = 'black';
			}*/
		} else {
			if (basePriceElem) {
				basePriceElem.innerHTML = '$' + jsarray[0].toFixed(2);
				if (jsarray[3] == '1') {
					basePriceElem.style.color = 'red'
					basePriceElem.innerHTML += " SALE!";
				}
			}
			divText = '';
		}
		if (divText != '') 
			sizePriceDiv.innerHTML = divText;
	}
	if (regprice > 0 && regPriceElem) 
		regPriceElem.innerHTML = regprice.toFixed(2);
}

function selectcolor(color) {
var colorElem;
var addCartButton;
var pform;
var regDivElem;
var	extPriceDiv = document.getElementById('extPriceDiv');
var	extbasePrice = document.getElementById('extbasePrice');
var canBuyColor = true;

	if (color != -1 && ((nsizes == 0 && jsarray[color][4] == 'N') || (color == selectedcolor))) return;
	addCartButton = document.getElementById('addToCart');
	pform = document.getElementById('productform');
	if (selectedcolor >= 0)
		document.getElementById('color' + selectedcolor).style.borderColor = '#FFFFFF';
		
	if (color == -1) {
		//addCartButton.disabled = true;
		addCartButton.src = '/img/add_to_bag_button_light.jpg';
		addCartButton.onclick = addReminder;
		document.getElementById('chosencolor').innerHTML = '';
		if (pform) {
			pform.frmColor.value = '';
			pform.frmQty.value = '1';
		}
		showsizes(-1);
		return;
	}
	
	document.getElementById('color' + color).style.borderColor = '#000000';
	selectedcolor = color;
	if (pform)
		pform.frmColor.value = jsarray[color][0];

	regDivElem = document.getElementById('regDiv');
	if (nsizes == 0) {
		if (regDivElem) {
			if (jsarray[color][6] == '0') 
				regDivElem.style.visibility = "hidden"
			else
				regDivElem.style.visibility = "visible";
		}
		if (addCartButton) {
			addCartButton.onclick = fnAddCart;
			//addCartButton.disabled = false;
			addCartButton.src = '/img/add_to_bag_button.jpg';
		} 
	} else {
		if (regDivElem) {
			if (jsarray[color][4] == '0') 
				regDivElem.style.visibility = "hidden"
			else
				regDivElem.style.visibility = "visible";
		}
		selectedsize = -1;
		if (showsizes(color) == 0) 
			canBuyColor = false;
		if (addCartButton) {
			//addCartButton.disabled = true;
			addCartButton.src = '/img/add_to_bag_button_light.jpg';
			addCartButton.onclick = addReminder;
		}
	}
	showsizeprices(color);
	//document.getElementById('frameProduct').src = '/frames/showproduct.asp?style=' + style + '&color=' + jsarray[color][0] + '&embr=' + canEmbr;
	//document.getElementById('frameCrossSell').src = '/frames/CrossSell.asp?ItemNumber=' + style + '&ItemColor=' + jsarray[color][0];
	document.getElementById('frameProduct').contentWindow.location.replace('/frames/showproduct.asp?style=' + style + '&color=' + jsarray[color][0] + '&embr=' + canEmbr + (canBuyColor?'':'&avail=.'));
	document.getElementById('frameCrossSell').contentWindow.location.replace('/frames/CrossSell.asp?ItemNumber=' + style + '&ItemColor=' + jsarray[color][0]);
	
	if (nsizes > 0) {
		document.getElementById('chosencolor').innerHTML = jsarray[color][3];
		document.getElementById('chosensize').innerHTML = '';
		if (extPriceDiv) extPriceDiv.innerHTML = '';
	} else {
		document.getElementById('chosencolor').innerHTML = jsarray[color][5];
		if (extPriceDiv) extPriceDiv.innerHTML = "Normal price: $" + ext_orig_price[jsarray[color][3]].toFixed(2) + ".<br>For " + ext_name + " customers:";
		if (extbasePrice) extbasePrice.innerHTML = "";
	}
}

function showavail(){
	return(showcolor(selectedcolor, 999999));
}

function show1avail(){
	return(showcolor(selectedcolor, 1));
}

function show2avail(){
	return(showcolor(selectedcolor, 2));
}

function shownotavail() {
	if (nsizes > 0)
		return(showcolor(selectedcolor, 0))
	else
		return(showcolor(-1, '.'));
}

function showcolor(color, avail) {
var frameProduct;
	frameProduct = document.getElementById('frameProduct');
	if (ncolors > 0) {
		if (color < 0) color = 0;
		if ((color != showncolor || avail != shownavail) && frameProduct) {
			frameProduct.contentWindow.location.replace('/frames/showproduct.asp?style=' + style + '&color=' + jsarray[color][0] + '&avail=' + avail + '&embr=' + canEmbr);
			//document.getElementById('frameProduct').src = '/frames/showproduct.asp?style=' + style + '&color=' + jsarray[color][0] + '&avail=' + avail + '&embr=' + canEmbr;
			showncolor = color;
			shownavail = avail;
		}
	} else if (avail != shownavail && frameProduct) {
			frameProduct.contentWindow.location.replace('/frames/showproduct.asp?style=' + style + '&avail=' + avail + '&embr=' + canEmbr);
			shownavail = avail;
	}
}

function startUp() {
var el;
var cont = 0;
var isThere;
var addCartButton;
var canBuy;
var regDivElem;
var extPriceDiv;
var extbasePrice;
var availtext = "";

	regDivElem = document.getElementById('regDiv');
	addCartButton = document.getElementById('addToCart');
	if (addCartButton) {
		if ((nsizes > 1 && selectedsize < 0) || (ncolors > 1 && selectedcolor < 0)) {
			//addCartButton.disabled = true;
			addCartButton.src = '/img/add_to_bag_button_light.jpg';
			addCartButton.onclick = addReminder;
		} else
			addCartButton.onclick = fnAddCart;
	}
	if (nsizes > 0) {
		if (ncolors == 0) {
			while (el = document.getElementById('size' + cont)) {
				isThere = false;
				for (var cont2 = 0; cont2 < jsarray.length && !isThere; cont2++) {
					if (el.innerHTML == jsarray[cont2][0])
						isThere = true;
				}
				if (isThere) {
					if (cont == selectedsize)
						el.className = "selectedsize"
					else {
						canBuy = jsarray[--cont2][3];
						if (canBuy == 'N') {
							el.className = "notselectablesize";
							el.onmouseover = shownotavail;
							el.onmouseout = showavail;
						} else if (canBuy == '1') {
							el.className = "notselectedsize";
							el.onmouseover = show1avail;
							el.onmouseout = showavail;
						} else if (canBuy == '2') {
							el.className = "notselectedsize";
							el.onmouseover = show2avail;
							el.onmouseout = showavail;
						} else {
							el.className = "notselectedsize";
							el.onmouseover = null;
							el.onmouseout = null;
						}
					}
				} else {
					el.className = "notselectablesize";
					el.onmouseover = shownotavail;
					el.onmouseout = showavail;
					//el.onclick = null;
				}
				cont++;
			}
		} 
		else if (ncolors == 1 || selectedcolor >=0 || selectedsize >= 0) {
			availtext = "&avail=.";
			while (el = document.getElementById('size' + cont)) {
				isThere = false;
				for (var cont2 = 0; cont2 < jsarray[selectedcolor][2].length && !isThere; cont2++) {
					if (el.innerHTML == jsarray[selectedcolor][2][cont2][0])
						isThere = true;
				}
				if (isThere) {
					if (cont == selectedsize)
						el.className = "selectedsize"
					else {
						canBuy = jsarray[selectedcolor][2][--cont2][3];
						if (canBuy == 'N') {
							el.className = "notselectablesize";
							el.onmouseover = shownotavail;
							el.onmouseout = showavail;
						} else {
							el.className = "notselectedsize";
							el.onmouseover = null;
							el.onmouseout = null;
							availtext = "";
						}
					}
				} else {
					el.style.display = "none";
					//el.className = "notselectablesize";
					//el.onmouseover = shownotavail;
					//el.onmouseout = showavail;
					//el.onclick = null;
				}
				cont++;
			}
			if (availtext != "") 
				document.getElementById('frameProduct').contentWindow.location.replace('/frames/showproduct.asp?style=' + style + '&color=' + jsarray[selectedcolor][0] + '&embr=' + canEmbr + availtext);
		} else {
			while (el = document.getElementById('size' + cont++))
				el.className = "notselectablesize";
		}
	} else {
		if (ncolors > 0) {
			while (el = document.getElementById('color' + cont)) {
				if (jsarray[cont][4] == 'N') {
					el.onmouseover = shownotavail;
					el.onmouseout = showavail;
					el.style.cursor = 'default';
					el.onclick = null;
				}
				cont++;
			}
		}
	}
	if (selectedcolor >= 0) {
		if (regDivElem) {
			if ((nsizes > 0 && jsarray[selectedcolor][4] == '0') || (nsizes == 0 && jsarray[selectedcolor][6] == '0')) 
				regDivElem.style.visibility = "hidden"
			else
				regDivElem.style.visibility = "visible";
		}
		showsizeprices(selectedcolor);
		if (selectedsize >= 0) selectsize(selectedsize);
		extPriceDiv = document.getElementById('extPriceDiv');
		extbasePrice = document.getElementById('extbasePrice');
		if (extPriceDiv && nsizes == 0) {
			extPriceDiv.innerHTML = "Normal price: $" + ext_orig_price[jsarray[selectedcolor][3]].toFixed(2) + ".<br>For " + ext_name + " customers:";
			extbasePrice.innerHTML = "";
		}
	} else
		showsizeprices(-1);
}

