var blnComma = false;

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 = 'block';
	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 = 'block';
	outOfStockMessage.style.visibility = 'visible';
	outOfStock.style.display = 'inline';
	outOfStock.style.visibility = 'visible';
}

function ExtractNum(stringNum) 
{
	var stringNo;
	//We're doing string functions to make sure that we're getting the price after "(+" of each option labels
	stringNo = stringNum.slice(stringNum.lastIndexOf("(+"),stringNum.length);

	var blnIsNegative = false;

	if (stringNo.length < 2)
	{
		//It didn't find "(+" so it must be "(-"
		stringNo = stringNum.slice(stringNum.lastIndexOf("(-"),stringNum.length);
		blnIsNegative = true;
	}

	if (stringNum.lastIndexOf("(+") == -1 && stringNum.lastIndexOf("(-") == -1)
	{
		stringNo = 0;
	}

	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(i==",")
		{
			blnComma = true;
			parsedNo += "."; 
		}
	} 

	if (parsedNo.length > 0) 
	{
		if (blnIsNegative) 
		{
			return '-' + parsedNo;
		} 
		else 
		{
			return parsedNo;
		} 
	} 
	else 
	{
		return 0;
	}
}

function checkStock(id,outOfStockItems, SelectedOptionData) {

	// VERSION ATTRIBUTES INFO
	// Contains information relevant to selected combination in
	// The Following format:
	// Selected-OptionIDs [0] | Version Price [1] | Delivery Time [2] | Weight [3] | RRP [4] | Code [5]

	// Build up out options selections

	var txtPrice = document.getElementById('txtPrice' + id);
	var origPrice = document.getElementById('origPrice' + id);
	var txtPriceEx = document.getElementById('txtPriceEx' +id);
	var numFixedPlaces = document.getElementById('numFixedPlaces' +id);

	var selections = new Array();
	var selectionCount = 0;
	var numOptionsTotal = 0;

	var strPrice = "s" + origPrice.value;
	if (strPrice.indexOf(",") > 0) {blnComma = true;}

	var i;

	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.replace(',','.')) +  parseFloat(numOptionsTotal)).toFixed(numFixedPlaces.value);
	if (txtPriceEx != null)
	{
		var numTax = document.getElementById('numTax' + id);
		txtPriceEx.value = (txtPrice.value.replace(',','.') * numTax.value).toFixed(numFixedPlaces.value);
	}
	if (blnComma)
	{
		txtPrice.value= txtPrice.value.replace('.',',');
		if (txtPriceEx != null) {txtPriceEx.value = txtPriceEx.value.replace('.',',');}
	}
	//	selections = (selections.sort());
	var selection = selections.join('-');
	// outOfStockItems array no longer used, so we check to see if it *isn't* in the version array.
	
	// #### OM : 2011-09-06 : oos array *is* still used; check that first:
	var isOutOfStock = false;
	var oosLen = outOfStockItems.length;
	for (i = 0; i < oosLen; i++) {
		if (outOfStockItems[i] == selection) {
			isOutOfStock = true;
			break;
		}
	}
	
	if (!isOutOfStock) {
		// #### OM : 2011-09-06 : if it's not in the oos array, make sure it *is* in the version array:
		isOutOfStock = true;
		
		for(i=0; i<VersionAttributeArray.length; i++)
		{
			if(VersionAttributeArray[i].split('|')[0]==selection)
			{
				isOutOfStock = false;
				break;
			}
		}
	}

	if(isOutOfStock)
	{
		goOutOfStock(id);
	}
	else
	{
		goInStock(id);
	}

	// see if we need to update the pricing information for the customer group
	// Loop through the customer price array

	for(i=0; i<SelectedOptionData.length; i++)
	{
		// Split the options [0] from the price [1]
		var VersionAttributes = SelectedOptionData[i].split('|');

		// Does the price option match the selected form fields?
		if(VersionAttributes[0]==selection)
		{
			//The option matches so we update the price
			// Update Price INC. VAT
			
			if(VersionAttributes[1]!='£0.00' && VersionAttributes[1]!='£9,999.00')
			{
				txtPrice.innerHTML = VersionAttributes[1];
//				txtPrice.value=VersionAttributes[1];
			}
			else
			{
				txtPrice.value='N/A'
			}

			// Do we need to update the EXC. Tax? This field won't
			// exist if the site isn't set to show prices exc. VAT so first
			// check it's there to update or we'll get a javascript error
			if (txtPriceEx != null)
			{
				// The exc. text field is there
				txtPrice.value = (txtPrice.value * numTax.value).toFixed(2);
			}

			// UPDATE THE DELIVERY TIMES
			if(VersionAttributes[2]!=0)

			{
				// update the delivery div container - only if the value isn't the default
				// zero days
				document.getElementById('DeliveryTimesContainer').innerHTML = "<b>up to "+VersionAttributes[2]+" days</b>";
			}	
			else
			{
				// it's 0 days so we empty the container
				document.getElementById('DeliveryTimesContainer').innerHTML = "&nbsp;"
			}

			// UPDATE THE VERSION WEIGHTS
			if(VersionAttributes[3]!=0)
			{
				// update the delivery div container - only if the value isn't the default
				// zero days
				document.getElementById('VersionWeightContainer').innerHTML = "<b>"+VersionAttributes[3]+" kg</b>";
			}
			else
			{
				// 0 weight so empty the container
				document.getElementById('VersionWeightContainer').innerHTML = "&nbsp;"
			}

//			// UPDATE THE RRP
//			if(VersionAttributes[4]!='0.00' && VersionAttributes[4]!='9,999.00')
//			{
//				// update the delivery div container - only if the value isn't the default
//				// zero or 99999
//				document.getElementById('VersionRRPContainer').innerHTML = "<b>&pound;"+VersionAttributes[4]+"</b>";
//			}	
//			else
//			{
//				// it's 0 days so we empty the container
//				document.getElementById('VersionRRPContainer').innerHTML = "&pound; N/A"
//			}
			
			// UPDATE THE version code
			if(VersionAttributes[5]!='')
			{
				// update the version code
				// zero or 99999
				document.getElementById('VersionCodeContainer').innerHTML = "<b>"+VersionAttributes[5]+"</b>";
			}	
			else
			{
				// it's 0 days so we empty the container
				document.getElementById('VersionRRPContainer').innerHTML = "&pound; N/A"
			}
		}			
	}
}			

/*****************  PINDAR KW 31/07/2008 ***************/
/* THE FOLLOWING CODE UPDATES THE VERSION PRICE
/* WHEN THE VERSION IS SET TO DISPLAY AS DROP DOWN
/* (NO PRICE) AS FOR SOME REASON THEY WHO SHALL
/* NOT BE NAMED DECIDED NOT TO INCLUDE IT
/* OR THAT IT WASN'T NEEDED - ER, HELLO???!!!
/*******************************************************/
function PindarUpdateVersionPrice(PriceArray)
{
	var selection=document.AddToBasket.V1.value;
	var txtPrice = document.getElementById('txtPrice');

	for(i=0; i<PriceArray.length; i++)
	{
		// Split the version id [0] from the price [1] and delivery [2]
		var ThePrice = PriceArray[i].split('|');

		// Does the price option match the selected form fields?
		if(ThePrice[0]==selection)
		{
			//The option matches so we update the price
			// Update Price INC. VAT
			// Only need to update if we're displaying drop downs
			// with no names
			if (txtPrice != null || ThePrice[1]!='9,999.00')
			{
				txtPrice.value=ThePrice[1];
			}

			if(ThePrice[1]!='0.00' && ThePrice[1]!='9,999.00')
			{
				// update the price div container - only if the value isn't the default
				// 0 or 99999.00
				document.getElementById('VersionPriceContainer').innerHTML = "<b>&pound; "+ThePrice[1]+"</b>";
			}
			else
			{
				document.getElementById('VersionPriceContainer').innerHTML = "";
			}

			if(ThePrice[2]!=0)
			{
				// update the delivery div container - only if the value isn't the default
				// zero days
				document.getElementById('DeliveryTimesContainer').innerHTML = "<b>up to "+ThePrice[2]+" days</b>";
			}
			else
			{
				document.getElementById('DeliveryTimesContainer').innerHTML = "";
			}

			if(ThePrice[3]!='0.00' || ThePrice[3]!='9,999.00')
			{
				// update the RRP div container - only if the value isn't the default
				// zero price or 99999
				document.getElementById('VersionRRPContainer').innerHTML = "<b>&pound; "+ThePrice[3]+"</b>";
			}
			else
			{
				document.getElementById('VersionRRPContainer').innerHTML = "";
			}

			if(ThePrice[4]!=0)
			{
				// update the weight div container - only if the value isn't the default
				// zero 
				document.getElementById('VersionWeightContainer').innerHTML = "<b>"+ThePrice[4]+"</b>";
			}	
			else
			{
				document.getElementById('VersionWeightContainer').innerHTML = "";
			}
		}
	}
}
