					
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';
	
	//#### PINDAR : KW : 24/09/08 : Show Price Line If Available

	if (document.getElementById('pricesField')!=null)
	{
		var PriceLine = document.getElementById('pricesField');
		PriceLine.style.display = '';
		PriceLine.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';
	
	//#### PINDAR : KW : 24/09/08 : Hide Price Line If Not Available
	//#### PINDAR : KW : Don't want to show this as most prices will
	//#### PINDAR : KW : Show as 99999.00 Which look bad! So, we
	//#### PINDAR : KW : hide the text and input field
	if (document.getElementById('pricesField')!=null)
	{	
		var PriceLine = document.getElementById('pricesField');
		PriceLine.style.display = 'none';
		PriceLine.style.visibility = 'hidden';
	}
}
								
function ExtractNum(stringNo) 
{
	//Pindar - KW - Do we need to change the price? We only need to do this if there's a price break
	// The existing code will update anything that's numeric!
	if(stringNo.indexOf("+")==0 || stringNo.indexOf("+")>0)
	{
	//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;}
	}
	else
	{return 0;}
	}
 	

function checkStock(id,outOfStockItems, CustomerPrices, ProductID) {
	// Build up out options selections
	var txtPrice = document.getElementById('txtPrice' + id);
	var origPrice = document.getElementById('origPrice' + id);
	var txtPriceEx = document.getElementById('txtPriceEx' +id);
	
	//Pindar - KW - Variable for Option - default to select box
	var OptionDisplayType="select-one";
	
	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) {
						//Pindar - KW - Set the Option Type
						OptionDisplayType="checkbox";
						// 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]
						//Pindar - KW - Set the Option Type
						OptionDisplayType="checkbox";
						selections[selectionCount]=nocheck.value;
						selectionCount++;
					}
					break;
					
				case 'radio':
					if(element.checked == true) {
						selections[selectionCount]=element.value;
						//Pindar - KW - Set the Option Type
						OptionDisplayType="radio";
						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;
					//Pindar - KW - Set the Option Type
						OptionDisplayType="select-one";
					selectionCount++;
					numOptionsTotal = numOptionsTotal + parseFloat(ExtractNum(element.options[Index].text));
					break;

				default:
					break;
					
			}
			
		}

	}
	
	// Are we doing pricing? Not used for default site and gives an error if called
	if(origPrice!=null)
	{
		
			txtPrice.value=(parseFloat(origPrice.value) +  parseFloat(numOptionsTotal)).toFixed(2);

			if (txtPriceEx != null){
				var numTax = document.getElementById('numTax' + id);
				txtPrice.value = (txtPrice.value * numTax.value).toFixed(2);
				

			}
	}
	
		
				//Pindar KW - Check if we need to sort
			if(OptionDisplayType=='radio')
			{	
				
				//sort the options - if we do this on select boxes it breaks!
				selections = (selections.sort());
			}
			
			var selection = selections.join('-');
			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);

			}
			
			// see if we need to update the pricing information for the customer group
			// Loop through the customer price array
			if(origPrice!=null)
	{	
				var i;
				for(i=0; i<CustomerPrices.length; i++)
				{
				// Split the options [0] from the price [1]
				var OptionPriceArray = CustomerPrices[i].split('|');
				// Does the price option match the selected form fields?
			//	alert(OptionPriceArray[0]+" :: "+selection);
				if(OptionPriceArray[0]==selection)
					{
					//The option matches so we update the price
					// Update Price INC. VAT
					if (txtPrice != null)
					{
					txtPrice.value=OptionPriceArray[1]		
					}
					// 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
								txtPriceEx.value = (txtPrice.value * numTax.value).toFixed(2);
								alert(txtPriceEx.value);
							}

					}
			}
				
	}
	
//#### PINDAR : KW : See if the options selected are available or not.
//#### PINDAR : KW : need to replace "," in selections with "|" to 
//#### PINDAR : KW : Match the data in the available options array

UserSelectedOptions = selections.toString();

//UserSelectedOptions = UserSelectedOptions.replace(/,/g,"|");
//var HaveProduct = false;

//var kt;
//for(kt=0; i<AvailableOptions.length; kt++)
//{
//	if(UserSelectedOptions==AvailableOptions[i])
//	{
//	HaveProduct = true;
//	}
//}

// Are we already out of stock for this customer? If we are then
// We don't need to check this as it may overwrite the previous
// Out Of Stock control
if(isOutOfStock == false)
{
	var PageURL="/includes/-Pindar_CheckProduct.asp";
	var QueryString = "OptionGroups="+selectionCount+"&ProductID="+ProductID+"&Selection="+UserSelectedOptions;
    var xmlHttpReq = false;
	var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
   	self.xmlHttpReq.open("POST", PageURL, true);
  	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
  	self.xmlHttpReq.setRequestHeader("Content-length", QueryString.length);
	
    self.xmlHttpReq.onreadystatechange = function() {
    //	if (self.xmlHttpReq.readyState == 1) {
     //      	document.getElementById('outofstockmessage' + id).innerHTML = "Wait";
	// }	
        if (self.xmlHttpReq.readyState == 4) {
            //document.getElementById('outofstockmessage' + id).innerHTML = self.xmlHttpReq.responseText;
             UpdatePage(self.xmlHttpReq.responseText, id);			
        }
    }
	self.xmlHttpReq.send(QueryString);

}



	//#### PINDAR : KW : 24/09/08 : See if we need to make this item
	//#### PINDAR : KW : Appear as Out Of Stock - Versions/Combinations that aren't
	//#### PINDAR : KW : In the versions table appear with a price of '99999.00' so
	//#### PINDAR : KW : If we have that value in the price field, we hide it
		
	if(txtPrice!=null && txtPrice.value=='99999.00')
	{
		goOutOfStock(id);	
	}
}

function UpdatePage(ResponseText, id)
{
	if(ResponseText=="False")
	{
		goOutOfStock(id);
	}
	else
	{
		goInStock(id);
	}
}

	

						