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] == realSize(el.innerHTML))
			found = true
		else if (ncolors > 0 && jsarray[selectedcolor][2][cont][0] == realSize(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 (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;

	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 = realSize(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 = realSize(sizeElem.innerHTML);
		if (pform)
			pform.frmSize.value = realSize(sizeElem.innerHTML);
	}
	basePriceElem = document.getElementById('basePrice');
	regPriceElem = document.getElementById('regPrice');
	sizePriceDiv = document.getElementById('sizePriceDiv');
	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;
		} else {
			basePriceElem.innerHTML = '$' + jsarray[size][1].toFixed(2) + ' ' + s;
		}
	}
	if (regprice > 0 && regPriceElem) {
		for (cont = 0; cont <= nsizes && regPrices[cont].split(',')[0] != realSize(sizeElem.innerHTML); cont++) ;
		if (regPrices[cont].split(',')[0] == realSize(sizeElem.innerHTML)) 
			regPriceElem.innerHTML = regPrices[cont].split(',')[1];
	}
	if (sizePriceDiv) sizePriceDiv.innerHTML = '';
}

function showsizes(color) {
	var cont = 0;
	var isThere;
	var el;
	var canBuy;
	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 (realSize(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 {
					el.className = "notselectedsize";
					el.onmouseover = null;
					el.onmouseout = null;
				}
			} else {
				el.style.display = "none";
				//el.className = "notselectablesize";
				//el.onmouseover = shownotavail;
				//el.onmouseout = showavail;
			}
		}
	}
}

function showsizeprices(color) {
var prevPrice = 0;
var basePrice;
var strSize = "";
var prevSize;
var cont;
var sizePriceDiv;
var regPriceElem;
var basePriceElem;
var divText;
var text1;

	sizePriceDiv = document.getElementById('sizePriceDiv');
	basePriceElem = document.getElementById('basePrice');
	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 (basePriceElem) 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';
					}
				}
				basePrice = jsarray[color][2];
			}
			//Change tier table
			var tier_f_a;
			var tier_f_b;
			var diff = 0;
			var oldBase;
			cont = 1;
			tier_f_a = document.getElementById('tier_0');
			if (tier_f_a)
				oldBase = tier_f_a.innerHTML.substring(1);
			tier_f_b = document.getElementById('tier_1');
			while (tier_f_b) {
				tier_f_a.innerHTML = '$' + (Math.round((basePrice - diff)*100)/100).toFixed(2);
				diff = oldBase - tier_f_b.innerHTML.substring(1);
				tier_f_a = tier_f_b;
				tier_f_b = document.getElementById('tier_' + ++cont);
			}
			if (tier_f_a)
				tier_f_a.innerHTML = '$' + (Math.round((basePrice - diff)*100)/100).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);
				} 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) 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;

	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];
	if (nsizes == 0) {
		if (addCartButton) {
			addCartButton.onclick = fnAddCart;
			//addCartButton.disabled = false;
			addCartButton.src = '/img/add_to_bag_button.jpg';
		} 
	} else {
		selectedsize = -1;
		showsizes(color);
		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);
	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 = '';
	} else {
		document.getElementById('chosencolor').innerHTML = jsarray[color][5];
	}
}

function showavail(){
	return(showcolor(selectedcolor, 1));
}

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 jssizes() {
var el;
var sizedictinv = new Object;
var cont = 0;

	for (i in sizedict) {
		sizedictinv[sizedict[i]] = i
	}
	while (el = document.getElementById('size' + cont++)) {
		if (sizedictinv[el.innerHTML]) 
			el.innerHTML = sizedictinv[el.innerHTML].replace(".br.","\<BR\>");
	}
}

function realSize(htmlsize) {
var processed_size = htmlsize.replace(/<BR>/i, ".br.");
	if (sizedict[processed_size]) 
		return sizedict[processed_size]
	else
		return htmlsize;
}

function startUp() {
var el;
var cont = 0;
var isThere;
var addCartButton;
var canBuy;

	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 (realSize(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 {
							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) {
			while (el = document.getElementById('size' + cont)) {
				isThere = false;
				for (var cont2 = 0; cont2 < jsarray[selectedcolor][2].length && !isThere; cont2++) {
					if (realSize(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;
						}
					}
				} else {
					el.style.display = "none";
					//el.className = "notselectablesize";
					//el.onmouseover = shownotavail;
					//el.onmouseout = showavail;
					//el.onclick = null;
				}
				cont++;
			}
		} else {
			while (el = document.getElementById('size' + cont++))
				el.className = "notselectablesize";
		}
	} else {
		if (ncolors > 0) {
			while (el = document.getElementById('color' + cont)) {
				if (jsarray[cont][4] == 'Y') {
					/*if (cont == selectedcolor)
						el.className = "selectedcolor"
					else
						el.className = "notselectedcolor";*/
				} else {
					el.onmouseover = shownotavail;
					el.onmouseout = showavail;
					el.style.cursor = 'default';
					el.onclick = null;
				}
				cont++;
			}
		}
	}
	if (selectedcolor >= 0) 
		showsizeprices(selectedcolor)
	else
		showsizeprices(-1);
	jssizes();
}

