					
function goInStock(id) {
	var outOfStock = document.getElementById('outofstockline' + id);
	var inStock = document.getElementById('addtobasketline' + id);
	var outOfStockMessage = document.getElementById('outofstockmessage' + id);
	outOfStock.style.display = 'none';
	outOfStock.style.visibility = 'hidden';
	outOfStockMessage.style.display = 'none';
	outOfStockMessage.style.visibility = 'hidden';
	inStock.style.display = '';
	inStock.style.visibility = 'visible';
	
}

function goOutOfStock(id) {
	var outOfStock = document.getElementById('outofstockline' + id);
	var inStock = document.getElementById('addtobasketline' + id);
	var outOfStockMessage = document.getElementById('outofstockmessage' + id);
	inStock.style.display = 'none';
	inStock.style.visibility = 'hidden';
	outOfStockMessage.style.display = '';
	outOfStockMessage.style.visibility = 'visible';
	outOfStock.style.display = '';
	outOfStock.style.visibility = 'visible';
}
								
function ExtractNum(stringNo) 
{
	//We're doing string functions to make sure that we're getting the price after "(+" of each option labels
	stringNo = stringNo.slice(stringNo.lastIndexOf("(+"),stringNo.length);

	var parsedNo = ""; 
	for(var n=0; n<stringNo.length; n++) 
	{
		var i = stringNo.substring(n,n+1); 
		if(i=="1"||i=="2"||i=="3"||i=="4"||i=="5"||i=="6"||i=="7"||i=="8"||i=="9"||i=="0"||i==".")
		parsedNo += i; 
	} 
	if (parsedNo.length > 0) { 
	return parsedNo;} else {return 0;}
}
 	

function checkStock(id,outOfStockItems, SelectedOptionData) {
	// Build up out options selections
	var txtPrice = document.getElementById('txtPrice' + id);
	var origPrice = document.getElementById('origPrice' + id);
	var txtPriceEx = document.getElementById('txtPriceEx' +id);
	var blnIsValidOption = true;
	
	var selections = new Array();
	var selectionCount = 0;
	var numOptionsTotal = 0;
	for (i=0;i<document.getElementById('options' + id).elements.length;i++) {
		var element = document.getElementById('options' + id).elements[i];
		if(element.name.substring(0,6)=='OPT_ID') {
			switch(element.type)
			{
				case 'checkbox':
					// is this checkbox selected?
					if(element.checked == true) {
						// use this ID
						selections[selectionCount]=element.value;
						selectionCount++;
												
						// find all labels
						var labels = document.getElementsByTagName('label');
						// loop through all label elements
							for (var m = 0; m < labels.length; m++) {
								var label = labels[m];
								var labelFor = label.htmlFor;					
								if (labelFor == element.id) {
										numOptionsTotal = numOptionsTotal + parseFloat(ExtractNum(label.innerHTML));
								}							
							}
					} else {
						// otherwise we have to get out the nocheckvalue
						var nocheck = document.getElementById('options' + id).elements['NOCHECK_' + element.name]
						selections[selectionCount]=nocheck.value;
						selectionCount++;
					}
					break;
					
				case 'radio':
					if(element.checked == true) {
						selections[selectionCount]=element.value;
						selectionCount++;
						// find all labels
						var labels = document.getElementsByTagName('label');
						// loop through all label elements
							for (var m = 0; m < labels.length; m++) {
								var label = labels[m];
								var labelFor = label.htmlFor;					
								if (labelFor == element.id) {
										numOptionsTotal = numOptionsTotal + parseFloat(ExtractNum(label.innerHTML));
								}							
							}
					}
					break;
					
				case 'select-one':
					var Index = element.selectedIndex;
					selections[selectionCount]=element.value;
					selectionCount++;
					numOptionsTotal = numOptionsTotal + parseFloat(ExtractNum(element.options[Index].text));
					break;

				default:
					break;
					
			}
			
		}

	}
	
	txtPrice.value=( parseFloat(origPrice.value) +  parseFloat(numOptionsTotal)).toFixed(2);
	if (txtPriceEx != null){
		var numTax = document.getElementById('numTax' + id);
		txtPriceEx.value = (txtPrice.value * numTax.value).toFixed(2);
	}
	
	//selections = (selections.sort());
	var selection = selections.join('-');

	if(blnIsValidOption)
	{

	// see if we need to update the pricing information for the version 

	// Loop through the Version Attribute

		for(i=0; i<SelectedOptionData.length; i++)

		{
			blnIsValidOption = false;
			var VersionPrice = 0;
			var VersionCode = "";

			// Split the options [0] from the price [1] and code [2]

			var VersionAttributes = SelectedOptionData[i].split('|');
		
	
			if(selection==VersionAttributes[0])
			// The Options Selected Match something in the VersionAttributes Array so we've
			// Got a valid combination
			{
			
				blnIsValidOption = true;
				VersionPrice = VersionAttributes[1];
				VersionCode = VersionAttributes[2];
				VersionText = VersionAttributes[3];
				break;
			}
	
		}
	}


	//Is it a valid option? If yes then update price, code, show the add button
	if(blnIsValidOption)
	{
		document.getElementById('prod-desc'+id).innerHTML = VersionText + "<br />" + VersionCode;
		// Update the price if it's not already the same value. If we update
		// And it's the same price we get a weird flashing effect
//		var CurrentPrice = txtPrice.value;
//		if(VersionPrice!=CurrentPrice)
//		{
//			txtPrice.value= VersionPrice;
//		}
		//Show the Price Info
//		document.getElementById('PriceLine'+id).style.display="";
//		document.getElementById('PriceLineDisabled'+id).style.display = "none";
		//Show The Quantity
//		document.getElementById('QTYAdd'+id).style.display = "";
//		document.getElementById('QTYAddDisabled'+id).style.display = "none";
		

		
	}
	else
	{
		//Bad Choice! Hide price and code, disable add button
		
//		document.getElementById('prod-desc'+id).innerHTML = "";
//		txtPrice.value = "";
//		document.getElementById('PriceLine'+id).style.display="none";
//		document.getElementById('QTYAdd'+id).style.display = "none";
//		document.getElementById('QTYAddDisabled'+id).style.display = "";
//		document.getElementById('PriceLineDisabled'+id).style.display = "";
	}

	
	
	var isOutOfStock = false;
	
	// Does this combination exist in out outofstock array?
	for(i=0; i<outOfStockItems.length; i++) {
		if(outOfStockItems[i]==selection) {
			isOutOfStock = true;
			break;
		}
	}
	
	if(isOutOfStock) {
		goOutOfStock(id);
	} else {
		goInStock(id);
	}
}
						
