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 = '';
	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(stringNum) 
{
	//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, elClicked) {
	// 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;}
	
	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;
					
			}
			
		}

	}
	
	//sandford - txtPrice might not exist, e.g. if showWholePriceInProductOption=y
	if (txtPrice) {
	    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('.',',');}
	    }
	}
	
	function sortNumber(a,b)
  {
    return a - b;
  }
  
	selections = (selections.sort(sortNumber));
	var selection = selections.join('-');
	var isOutOfStock = false;
	
	//-----------------------------------------------------------------
	//sandford - dynamically update codenumber if possible...
	var sCodeNumber = aCodeNumbers[selections + ',vcn']
	if (sCodeNumber) {
	    var elCodeNumber = document.getElementById('VCodeNumber');
	    if (elCodeNumber) {
	        elCodeNumber.innerHTML = sCodeNumber;
	    }
	}
	
	//-----------------------------------------------------------------
	//sandford - 'disable' all other options
	if (elClicked) {
	    //console.log(outOfStockItems)
    	
	    //get all inputs in the current form
	    //var aAllInputs = document.getElementsByTagName('input');
	    var aAllInputs = elClicked.form.elements
    	
	    // loop through all label elements
	    for (var aI = 0; aI < aAllInputs.length; aI++) {
	        //only option radios...
	        if(aAllInputs[aI].name.substring(0,6)=='OPT_ID') {
	            //only inputs from groups other than the selected one...
    	        
    	        
	            if(aAllInputs[aI].name != elClicked.name) {
	            //console.debug(element.name,aAllInputs[aI].name,aAllInputs[aI].id,aAllInputs[aI].value) 
	            //oos1[7]='815-1092';
    	        
	            aAllInputs[aI].disabled = false;
    	        
	            var lblInput = document.getElementById('lbl_'+aAllInputs[aI].id)
			    if (lblInput) lblInput.style.color = ""
			    var lblInput = document.getElementById('lbp_'+aAllInputs[aI].id)
			    if (lblInput) lblInput.style.opacity = ""
			    var imgInput = document.getElementById('img_'+aAllInputs[aI].id)
			    if (imgInput) imgInput.style.opacity = ""			
    	        
    	        
	            // Does this combination exist in out outofstock array?
	            for(i=0; i<outOfStockItems.length; i++) {
	                //console.log(outOfStockItems[i] , elClicked.value + '-' + aAllInputs[aI].value)
		            if ((outOfStockItems[i] == aAllInputs[aI].value + '-' + elClicked.value)||(outOfStockItems[i] == elClicked.value + '-' + aAllInputs[aI].value)) {
			            aAllInputs[aI].disabled = true;
			            aAllInputs[aI].checked = false;
    			        
			            var lblInput = document.getElementById('lbl_'+aAllInputs[aI].id)
			            if (lblInput) lblInput.style.color = "gray"
			            var lblInput = document.getElementById('lbp_'+aAllInputs[aI].id)
			            if (lblInput) lblInput.style.opacity = ".2"
			            var imgInput = document.getElementById('img_'+aAllInputs[aI].id)
			            if (imgInput) imgInput.style.opacity = ".4"			        
			            //break;
		            }
	            }
      
	            }
	        }
	    }
	    //break;
	}
	//-----------------------------------------------------------------
	
	
	// 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);
	}
}			
