
var defaultpricedivtext = "";

function selectChanged(selectidx) {
	for ( var i=selectidx+1; i<numselects; i++ ) {
		var elSelNext = document.getElementById("groupedbasketformselect"+i);
		elSelNext.selectedIndex = 0;
	}

	return displayGroupedBasketForm();
}

// Function to display multiple select boxes and update the price and availability div
// if only one product matches
function displayGroupedBasketForm() {

	if ( defaultpricedivtext == "" ) {
		var elPriceDiv = document.getElementById("basketpricediv");
		defaultpricedivtext = elPriceDiv.innerHTML;
	}

	if (typeof numselects == "undefined") {
		// This form does not contain multiple selects
		return displayPrice();
	}

	// Reset display flags
	for ( var productidx=0; productidx<pd.length; productidx++ ) {
		d[productidx] = 1;
	}

	// Set display flag to 0 if a selected option does not match
	for ( var selectidx=0; selectidx<numselects; selectidx++ ) {
		var elSel = document.getElementById("groupedbasketformselect"+selectidx);
		var optionidx = elSel.selectedIndex;
		var optiontext = elSel.options[optionidx].text;
		if ( optionidx > 0 ) {
			for ( var productidx=0; productidx<pd.length; productidx++ ) {
				if ( optiontext != s[selectidx][productidx] ) {
					d[productidx] = false;
				}
			}
		}
	}

	// Create new option array for each unselected list, populate list with new array
	var prevselectok = true;
	for ( var selectidx=0; selectidx<numselects; selectidx++ ) { // loop through each select box in turn
		var elSel = document.getElementById("groupedbasketformselect"+selectidx);
		var optionidx = elSel.selectedIndex;
		var optiontext = elSel.options[optionidx].text;
		if ( prevselectok == true ) {
			elSel.disabled = false;
		} else {
			elSel.disabled = true;
			continue;
		}
		if ( optionidx == 0 ) { // If an option is not selected already
			prevselectok = false;
			var optarr = new Array();
			for ( var productidx=0; productidx<pd.length; productidx++ ) {
				if ( d[productidx] == true ) {
					var optexists = false;
					var optadded = false;
					var storeopt = "";
					for ( var i=0; i<optarr.length; i++ ) {
						if ( optadded == true ) {
							var swap = optarr[i];
							optarr[i] = storeopt;
							storeopt = swap;
							continue;
						}
						if ( optarr[i] == s[selectidx][productidx] ) {
							optexists = 1;
							break;
						} else if ( optarr[i] > s[selectidx][productidx] ) {
							optadded = 1;
							storeopt= optarr[i];
							optarr[i] = s[selectidx][productidx];
							continue;
						}
					}
					if ( optadded == true ) {
						optarr[optarr.length] = storeopt;
					} else if ( optexists == false ) {
						optarr[optarr.length] = s[selectidx][productidx];
					}
				}
			}
			// Recreate Options list from optarr
			var origlength = elSel.options.length-1; // ignore index 0
			var newlength = optarr.length;
			for ( i=1; i<=newlength; i++ ) {
				if ( i <= origlength ) {
					elSel.options[i].text = optarr[i-1];
					elSel.options[i].value = optarr[i-1];
				} else {
					var elOptNew = document.createElement('option');
  					elOptNew.text = optarr[i-1];
					elOptNew.value = optarr[i-1];
					try {
						elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
					}
					catch(ex) {
						elSel.add(elOptNew); // IE only
					}
				}
				if ( elSel.options[i].text == optiontext ) {
					elSel.selectedIndex = i;
				}
			}
			if ( origlength > newlength ) {
				for ( i=origlength; i>newlength; i-- ) {
					elSel.options[i].text = "Removed";
					elSel.options[i].value = "Removed";
					elSel.remove(i);
				}
			}
		}
	}

	var elPriceDiv = document.getElementById("basketpricediv");
	if ( prevselectok == true ) {
		var numproducts = 0;
		var selectedidx = 0;
		for ( var productidx=0; productidx<pd.length; productidx++ ) {
			if ( d[productidx] == true ) {
				numproducts++;
				selectedidx = productidx;
			}
		}
		if ( numproducts > 1 ) {
			alert("Fatal Error: Num Products Selected = "+numproducts+"\nPlease contact Site Administrator");
			return false;
		}
		var elSelAddStock = document.getElementById("groupedbasketformaddstockcode");
		elSelAddStock.value = pd[selectedidx];
		elPriceDiv.innerHTML = getPriceDiv(selectedidx);
	} else {
		elPriceDiv.innerHTML = defaultpricedivtext;
	}
	return true;
}

// Function to update the price and availability div
// when there is only one select box.
function displayPrice() {
	var elSel = document.getElementById("groupedbasketformselect0");
	var selectedidx = elSel.selectedIndex;
	var elPriceDiv = document.getElementById("basketpricediv");
	if ( selectedidx == 0 ) {
		elPriceDiv.innerHTML = defaultpricedivtext;
	} else {
		elPriceDiv.innerHTML = getPriceDiv(selectedidx-1);
	}
}

function getPriceDiv(selectedidx) {
	var pricediv = "<table width=\"100%\"><tr><th width=\"60%\"></th><th width=\"40%\"></th></tr>\n";
	pricediv = pricediv + "<tr>";
	pricediv = pricediv + "<td>"

	pricediv = pricediv + pc[selectedidx];

	pricediv = pricediv + "</td>";
	pricediv = pricediv + "<td>";

	pricediv = pricediv + "</td>";
	pricediv = pricediv + "</tr></table>";
	pricediv = pricediv + "<div><a href=\"/product.php?category=" + escape(cat) + "&product=" + escape(pd[selectedidx])+"\"><span style=\"font-weight: bold; text-decoration: underline; font-size: 11px; color: #0c5296;\">Click for Product Information or to Add To Wish List</a></span>";
	return pricediv;
}
