function hopEmptySelectBox(theSel)
{
	var selLength=theSel.length;
	for(i=selLength-1;i>=0;i--)
	{
		removeOption(theSel,i);
	}
}

// Remove an option from a SELECT form field
function removeOption(theSel, theIndex)
{
	if(theIndex>=0)
	{
		theSel.options[theIndex]=null;
	}	
}

function addOption(theSel, theText, theValue)
{
	var newOpt = new Option(theText, theValue);
	var selLength = theSel.length;
	theSel.options[selLength] = newOpt;
}
function optionExists(theSel, theValue)
{
	var theSelLength=theSel.length;
	for(j=0;j<theSelLength;j++)
	{
		if(theSel.options[j].value==theValue)
		{
			return true;
		}
	}
	return false;
}

function selectOption(theSel, theValue) {
	var theSelLength=theSel.length;
	for(j=0;j<theSelLength;j++)
	{
		if(theSel.options[j].value==theValue)
		{
			theSel.selectedIndex = j;
			return true;
		}
	}
	return false;
}

function updateStockLevel(productOption) {
	var elems = productOption.form.elements;
	var stockCodeArr=new Array();
	var productID = null;
	var nothingSelected = false;
	
	var optionChangedName = productOption.name;
	var optionChangedNumber = optionChangedName.substring(7,8);
	var optionChangedValue = productOption.options[productOption.selectedIndex].value;
	
	productID = elems['product'].value;
	
	for(var i=0; i<elems.length;i++) {
		var elem = elems[i];
		if(elem.name.substring(0,6)=='option') {
			currentOptionValue = elem.options[elem.selectedIndex].value;
			var optionID = elem.name.substring(7,8);
			if(optionID > optionChangedNumber) {
				hopEmptySelectBox(elem);
				if(optionChangedValue == 0) {
					addOption(elem, productOption.options[0].text);
				}
				else {
					addOption(elem, hopProductOptions[productID][optionID][0],0);
				}
				for (var j in hopProducts[productID]) {
					//alert('key is: ' + j + ', value is: ' +hopProducts[productID][j]);
					var bits = j.split("-");
					if(bits[0] == optionChangedValue) {
						addOption(elem, hopProductOptions[productID][optionID][bits[1]], bits[1]);
						//alert(hopProductOptions[productID][optionID][bits[1]]);
						//alert(bits[0] + ":" + bits[1]);
					}
				}
			}
			if(!selectOption(elem, currentOptionValue)) {
				elem.selectedIndex = 0;
			}
			stockCodeArr[i] = elem.options[elem.selectedIndex].value;
			if(stockCodeArr[i] == '0') {
				nothingSelected = true;
			}
		}
	}
	var stockDisplay = document.getElementById('stockdisplay'+productID);
	if(stockDisplay) {
		if(!top.hopProductMsg.InStock) {
			top.hopProductMsg = new Object();
			top.hopProductMsg.InStock = 'In Stock';
			top.hopProductMsg.OnBackorder = 'On Backorder';
			top.hopProductMsg.NotDefined = '&nbsp;';
			top.hopProductMsg.NotSelected = 'Select an Option';
			top.hopProductMsg.ComboUnavailable = 'This combination of options is not available';			
		}
		var stockCode = stockCodeArr.join("-");
		var stockQTY = hopProducts[productID][stockCode];
		var stockText = '&nbsp;';
		if(nothingSelected == true) {
			stockText = top.hopProductMsg.NotSelected;
		}
		else {
			if(Number(stockQTY) > 0) {
				stockText = top.hopProductMsg.InStock;
			}
			else if(stockQTY == '') {
				stockText = top.hopProductMsg.NotDefined;
			}
			else if (!stockQTY) {
				stockText = top.hopProductMsg.ComboUnavailable;
			}
			else {
				stockText = top.hopProductMsg.OnBackorder;
			}
		}
		stockDisplay.innerHTML = stockText;
	}
}