// AddShopItem - Used to lookup ProductIndex, get values then call compute to add them.
function AddShopItem(ProductName,FormName) {
	//alert("trying to add product" + ProductName);
	ProductIndex = null;
	// Lookup Product Index
	for(ShopIndex = 0; ShopIndex < ShopProducts.length; ShopIndex++) {
		if(ShopProducts[ShopIndex].Name == ProductName) {
			//alert("trying for one" + ProductName + " " + ShopIndex);
			ProductIndex = ShopIndex;
			break;
		}
	}
	// Make sure we have a product
	if(ProductIndex != null) {
		ThisName = ShopProducts[ProductIndex].Name;
		ThisTitle = ShopProducts[ProductIndex].Title;
		ThisPrice = ShopProducts[ProductIndex].Price;
		ThisOptionGroup = ShopProducts[ProductIndex].OptionGroup;
		// Call Compute
		IsAddItemOK = compute(ThisPrice,ThisTitle,ThisName,FormName,ThisOptionGroup);
		// Do we need to auto view the cart?
		if(IsAddItemOK && ShopCartAutoView && (ShopCartURL != '')) {
			// Show Cart in a remote ?
			if(ShopCartRemote) {
				// Is remote already open?
				CartRemote = window.open(ShopCartURL,ShopCartTarget,ShopCartRemoteOptions);
				if (CartRemote.opener == null)	{
				 	CartRemote.opener = window;
					CartRemote.opener.name = "opener";
				}
					
			// Open cart in the target window
			} else {
				// Get Ref to target window
				CartWindow = eval(ShopCartTarget);
				CartWindow.location.href = ShopCartURL;
			}
		}
	}
}
/* Write Buy Product Lines
ProductGroup - If passed, then display all products in that group.
ProductNames - List of ProdcutNames to display.
*/
function ShowProducts(FormName,WriteOutputOK,ProductGroup) {
	// Do we have the skills (Browser OK?)
	if(IsBrowserOK()){
		//alert(ProductNameArray.length);
		ShopLength = ShopProducts.length ;
		ProductIndexArray = new Array();
		NameLength = ShowProducts.arguments.length;
		ShowIndex = 0;
		// Use Product Group ?
		if(ProductGroup != '' || ProductGroup != null) {
			//alert("trying for group " + ShopLength + " " + ProductGroup);
			// Loop Over Products. Grab any belong to this Group.
			for(ShopIndex = 0; ShopIndex < ShopLength; ShopIndex++) {
				if(ShopProducts[ShopIndex].ProductGroup == ProductGroup) {
					ProductIndexArray[ShowIndex] = ShopIndex;
					ShowIndex++;
				}
			}
		} 
		// We have ProductNames to lookup and add.
		if(ShowProducts.arguments.length > 3) {
		//alert("got some Product Names" + NameLength);
		//alert("got some Product Names" + ShowProducts.arguments.length);	
			// Loop over each Product Name passed
			for (NameIndex= 3;NameIndex < ShowProducts.arguments.length;NameIndex++){
				ThisProductName = ShowProducts.arguments[NameIndex];
				
				// Loop Over Products. Grab any that match
				for(ShopIndex = 0; ShopIndex < ShopLength; ShopIndex++) {
					if(ShopProducts[ShopIndex].Name == ThisProductName) {
						//alert("trying for one" + ThisProductName + " " + NameIndex + " " + ShowIndex);
						ProductIndexArray[ShowIndex] = ShopIndex;
						ShowIndex++;
					}
				}
			}
		}
		// Build Output Strings
		// Is this just one item or its more?
		ShopProductHeaderText = (ProductIndexArray.length > 1) ? ShopProductHeaderTextMultiple : ShopProductHeaderTextSingle;
	
		ThisOutput = '<table'+ShopProductTable +'><form action="#" name="'+ FormName+'" id="ShopItems"><tr'+ShopProductHeaderRow+'><td colspan="3" align="center"><font'+ShopProductHeaderFont+'>'+ShopProductHeaderText+ '</font></td></tr>';
		//alert(IndexLength);
		LastOptionGroup = '';
		ThisOptionGroupSelect = '';
		LastOptionGroupOptions = '';
		ThisOptionGroupOptions = '';
		// QuantityMax is used to build Qantity options.
		LastQuantityMax = 0;
		ThisQuantitySelect = '';
		LastQuantityOptions = '';
		ThisQuantityOptions = '';
		
		for (i= 0;i < ProductIndexArray.length;i++){
			ProductIndex = ProductIndexArray[i];
			ThisOptionGroupSelect = '';
			ThisOptionGroupOptions = '';
			// Lookup OptionGroup if we have one.
			if(ShopProducts[ProductIndex].OptionGroup != '' || ShopProducts[ProductIndex].OptionGroup == null){
				ThisOptionGroup = ShopProducts[ProductIndex].OptionGroup;
				// Build Option Select Box
				ThisOptionGroupSelect = ShopOptionTitle+':<select name="' + ShopProducts[ProductIndex].Name + '_Style" size="1" ' + ShopOptionStyle + '>\n';
				//alert("We have options to get " + ThisOptionGroup);
				// Does this product use the same option group as the last? If so just use last.
				if(LastOptionGroup == ShopProducts[ProductIndex].OptionGroup){
					ThisOptionGroupOptions = LastOptionGroupOptions;
				// Find Matching option group
				} else {
					for(OptionGroupIndex = 0; OptionGroupIndex < ShopOptionGroups.length; OptionGroupIndex++) {
						if(ShopOptionGroups[OptionGroupIndex][0] == ThisOptionGroup) {	
							OptionLength = ShopOptionGroups[OptionGroupIndex].length;
							for(OptionIndex=1; OptionIndex < OptionLength; OptionIndex++){
								ThisOptionGroupOptions += '<option value="'+ ShopOptionGroups[OptionGroupIndex][OptionIndex]+ '">' + ShopOptionGroups[OptionGroupIndex][OptionIndex]+ '</option>\n';
							}
													
						}
						//alert(ShopOptionGroups[OptionGroupIndex][0] +" "+ ThisOptionGroup);
					}
					// Update Last Group
					LastOptionGroup = ThisOptionGroup;
					LastOptionGroupOptions = ThisOptionGroupOptions;
				}
				// Close Options Select
				ThisOptionGroupSelect += LastOptionGroupOptions + '</select>'
			} else {
				ThisOptionGroupSelect = '&nbsp;';
			}
			
			
			// Can we have more than one of these ?
			if(ShopProducts[ProductIndex].QuantityMax > 1) {
				// Open Quantity Selects
				ThisQuantitySelect = 'Qty:<select name="' + ShopProducts[ProductIndex].Name + '_Quantity" size="1"' + ShopQuantityStyle + '">';
				// Use Last Quantity Option List?
				if(LastQuantityMax == ShopProducts[ProductIndex].QuantityMax) {
					ThisQuantityOptions = LastQuantityOptions;
				} else {
					for(Quantity = 1; Quantity <= ShopProducts[ProductIndex].QuantityMax; Quantity++) {
						SelectedString = (Quantity == 1)? ' selected':'';
						ThisQuantityOptions += '<option value="' + Quantity + '"' +SelectedString + ' >' + Quantity + '</option>\n';
					}
					
					LastQuantityMax = ShopProducts[ProductIndex].QuantityMax;
					LastQuantityOptions = ThisQuantityOptions;
				}
				// Close Selects
				ThisQuantitySelect += ThisQuantityOptions + '</select>';
			// Just display Quantity
			} else {
				ThisQuantitySelect = ShopProducts[ProductIndex].QuantityMax;
			}
			
			ThisPrice = ShopDefaultCurrency + AddDecimal(ShopProducts[ProductIndex].Price);
			
			// Action Links
			ThisProductNameQuoted = "'" + ShopProducts[ProductIndex].Name + "','"+FormName+"'";
			ThisAddItemString = '<a href="javascript:AddShopItem('+ThisProductNameQuoted+ ');">Add Item</a></font>';
			ThisShowCartString = ShopCartShowLink;
			ThisPriceQuoted = "'" +AddDecimal(ShopProducts[ProductIndex].Price)+ "'";
			ThisConvertPriceString = '<a href="javascript:CurrencyPopup('+ThisPriceQuoted +');">Convert Price</a>';
				
			ThisOutput += '<tr'+ShopProductTitleRow+'><td colspan="2"><font'+ShopProductTitleFont +'>'+ ShopProducts[ProductIndex].Title + '</font></td><td align="right"><font'+ShopProductTitleFont +'>'+ ThisPrice + '</font></td></tr>';
			ThisOutput += '<tr'+ShopProductOptionsRow+'><td align="right"><font'+ShopProductOptionsFont +'>'+ThisOptionGroupSelect + '</font></td>';
			ThisOutput += '<td align="right"><font'+ShopProductOptionsFont +'>' + ThisQuantitySelect + '</font></td>';
			ThisOutput += '<td align="right"><font'+ShopProductOptionsFont +'>' +ThisAddItemString +'</td></tr>';
			ThisOutput += '<tr'+ShopProductOptionsRow+'><td colspan="3" align="right"><font'+ShopProductOptionsFont +'>'+ThisShowCartString+ ' | '+ThisConvertPriceString+'</font></td></tr>';
			
		}
		// Close Product Tables
		ThisOutput += '</form></table>';
	
	// Broswer doesn't have what it takes.
	} else {
		ThisOutput = ShopBrowserErrorText;
	}
	// Write Output
	if(WriteOutputOK){
		document.writeln(ThisOutput);
	}
	return ThisOutput;
}