// This script is for checking stock levels on simple options and if the users selection is found to be out of stock the :-
// buy button is removed and replaced with an out of stock warning.
function checkstock(){
	if ( typeof stockData != 'undefined'){					
		// set up a variable that holds the number of simple options drop down count ie: are we using colours sizes and options
		var optionObjCount = 0;
		// set up a variable that holds the number of simple options that has been selected count
		var optionObjSelecetedCount = 0;					
		// check for the existance of colours
		if (document.le_form.color){
			// Increment the options object count
			optionObjCount++;
			// check to see if the user has made a selection
			thisselection=document.le_form.color.selectedIndex;
			if (document.le_form.color[thisselection].value != ""){
				selectedcolor=document.le_form.color[thisselection].value;
				// If they have made a selection increment the selected options count
				optionObjSelecetedCount++;
			}
		}	
		// check for the existance of options
		if (document.le_form.specialoptions){
			// Increment the options object count
			optionObjCount++;
			// check to see if the user has made a selection
			thisselection=document.le_form.specialoptions.selectedIndex;
			if (document.le_form.specialoptions[thisselection].value != ""){
				selectedoptionTemp=document.le_form.specialoptions[thisselection].value;
				selectedoptionArray = selectedoptionTemp.split('|')
				selectedoption = selectedoptionArray[0];					
				// If they have made a selection increment the selected options count
				optionObjSelecetedCount++;
			}
		}						
		// check for the existance of sizes
		if (document.le_form.size){
			// Increment the options object count
			optionObjCount++;
			// check to see if the user has made a selection
			thisselection=document.le_form.size.selectedIndex;
			if (document.le_form.size[thisselection].value != ""){
				selectedsizeTemp=document.le_form.size[thisselection].value;		
				selectedsizeArray = selectedsizeTemp.split('|')
				selectedsize = selectedsizeArray[0];					
				// If they have made a selection increment the selected options count
				optionObjSelecetedCount++;
			}
		}
		// If the number of option objects matches the number of selected option objects then we can perform a stock check on the stock level object "stockData"
		if (optionObjCount == optionObjSelecetedCount){
			// Loop round the length of the stockData array
			for(sd=0; sd < stockData.length; sd++){
				// Set the buy button and out of stock element to there default visablity
				if (document.le_form && document.le_form.addtocart)
					document.le_form.addtocart.style.display = '';
				if (document.getElementById('stock-warning'))	
					document.getElementById('stock-warning').style.display = 'none';
				// Find the selected size within the "stockData" object
				if (typeof selectedsize == 'undefined' || stockData[sd].size == selectedsize){
					// Find the selected specialoption within the "stockData" object
					if (typeof selectedoption == 'undefined' || stockData[sd].specialoptions == selectedoption){
						// Find the selected color within the "stockData" object
						if (typeof selectedcolor == 'undefined' || stockData[sd].color == selectedcolor){
							// Now that we have a match check the stock level
							if (stockData[sd].stocklevel == 0){
								// If the stock is zero hide the buy button and show out of stock
								if (document.le_form && document.le_form.addtocart)
									document.le_form.addtocart.style.display = 'none';
								if (document.getElementById('stock-warning'))										
									document.getElementById('stock-warning').style.display = '';
								break;
							}
						}
					}	
				}	
			}
		}
	}
}
