	function handleProductChange(li)
	{
		oProductSelect = document.getElementById('inpProductID_'+li);
		intProductID = oProductSelect.options[oProductSelect.selectedIndex].value;
		
		if (intProductID=='CUSTOM')
		{
			return handleChooseCustomLine(li);
		}
		else
		{
			ensureNotCustomLine(li);
		}
		
		oVarSelect = document.getElementById('inpQuantVar_'+li);
		oVarSelect.options.length = 0;

		oQuant = document.getElementById('Quant_'+li);

		oBackImageDiv = document.getElementById('backimagediv_'+li);
		oFrontImageDiv = document.getElementById('frontimagediv_'+li);
		oImageOptionsDiv = document.getElementById('imageoptionsdiv_'+li);
		
		oRemoveVATDiv = document.getElementById('removevat_'+li);

		if (intProductID==0)
		{
			newOption = new Option('*CHOOSE SIZE & QUANTITY*', '', true, true);
			oQuant.style.display = 'none';
		}
		else if (arrProduct[intProductID]['type'][0]=='byunit')
		{
			newOption = new Option('*CHOOSE SIZE*', '', true, true);
			oQuant.style.display = 'inline';
			oVarSelect.style.width = '275px';
		}
		else
		{
			newOption = new Option('*CHOOSE SIZE & QUANTITY*', '', true, true);
			oQuant.style.display = 'none';
			oVarSelect.style.width = '350px';
		}
		oVarSelect.options[0] = newOption;
		
		// if there is no artwork, remove them
		if (intProductID!=0 && arrProduct[intProductID]['type'][1]=='1')
		{
			oBackImageDiv.style.display = '';
			oFrontImageDiv.style.display = '';
			oImageOptionsDiv.style.display = '';
		}
		else
		{
			oBackImageDiv.style.display = 'none';
			oFrontImageDiv.style.display = 'none';
			oImageOptionsDiv.style.display = 'none';		
		}
		
		// if they can zero the vat, put it in
		if (intProductID!=0 && arrProduct[intProductID]['type'][5]==1)
		{
			oRemoveVATDiv.style.display = '';
		}
		else
		{
			oRemoveVATDiv.style.display = 'none';
		}
		
		if (intProductID!=0)
		{
			for (i in arrProduct[intProductID])
			{
			
				if (i!='type')
				{
					newOption = new Option(arrProduct[intProductID][i][0], i, false, false);
					oVarSelect.options[oVarSelect.options.length] = newOption;
				}
			}
		}		
		calcTotals();
		
	}
	
	function handleChooseCustomLine(li)
	{
		// need to remove the stuff we don't want
		oVarSelect = document.getElementById('inpQuantVar_'+li);
		oQuant = document.getElementById('Quant_'+li);
		oBackImageDiv = document.getElementById('backimagediv_'+li);
		oFrontImageDiv = document.getElementById('frontimagediv_'+li);
		oImageOptionsDiv = document.getElementById('imageoptionsdiv_'+li);
		oRemoveVATDiv = document.getElementById('removevat_'+li);
		
		oVarSelect.style.display = 'none';
		oQuant.style.display = 'none';
		oBackImageDiv.style.display = 'none';
		oFrontImageDiv.style.display = 'none';
		oImageOptionsDiv.style.display = 'none';
		oRemoveVATDiv.style.display = 'none';
		
		// show the stuff we do want
		oCustomFields1 = document.getElementById('customfields1_'+li);
		
		oCustomFields1.style.display = '';
		
		calcTotals();
	}

	function ensureNotCustomLine(li)
	{
		// need to remove the stuff we don't want
		oVarSelect = document.getElementById('inpQuantVar_'+li);
		oQuant = document.getElementById('Quant_'+li);
		oBackImageDiv = document.getElementById('backimagediv_'+li);
		oFrontImageDiv = document.getElementById('frontimagediv_'+li);
		oImageOptionsDiv = document.getElementById('imageoptionsdiv_'+li);
		
		oVarSelect.style.display = '';
		oQuant.style.display = '';
		oBackImageDiv.style.display = '';
		oFrontImageDiv.style.display = '';
		oImageOptionsDiv.style.display = '';
		
		// show the stuff we do want
		oCustomFields1 = document.getElementById('customfields1_'+li);
		
		if (oCustomFields1) oCustomFields1.style.display = 'none';
		//calcTotals();
	}

	function handleVarChange()
	{
		calcTotals();
	}

	function handleOnloadVar()
	{
		
		//for (var li = 0; li < lineCount; li++)
		var li;
		for (li in lineIDs)
		{
			// call handle product change to get the list of vars and quantities up
			handleProductChange(li);
			
			
			// see if one should be pre-selected
			var oOrigQuantVar = document.getElementById('inpOrigQuantVar_'+li);
			if (oOrigQuantVar.value != '0|0')
			{
				oVarSelect = document.getElementById('inpQuantVar_'+li);
				oVarSelect.value = oOrigQuantVar.value;
			}
				
		}
		calcTotals();
	}

	function GetPrice(intProductID, strVarKey, QuantityElement, strType)
	{
		
		if (strVarKey.length == 0 || strVarKey=='0') {
			return 0;
		}

		if (strType=='DELPRICE')
		{
			var price = arrProduct[intProductID][strVarKey][2];
		}
		else
		{
			var price = arrProduct[intProductID][strVarKey][1];
		}

		if (arrProduct[intProductID]['type'][0]=='byunit')
		{
			value = parseInt(QuantityElement.value);
			if (isNaN(value)) {
				if (strType=='PRICE') alert('Quantity must be a round number!');
				return 0;
			}
			else 
			{
				if (value<1)
				{
					if (strType=='PRICE') alert('Quantity must be 1 or greater!');
					return 0;
				}
				QuantityElement.value = value;
				return value * price;
			}
		}
		else
		{
			return price;
		}
	
	}
	
	
	function calcTotals()
	{

		var arrDelivery = new Array();
		var arrVAT = new Array();
		var intDelTotal = 0;
		var intTotal = 0;
		var intDiscount = 0;
		var intVAT = 0;
		var bCollect = false;
		var bUseCustomDel = false;
		var intDiscountRate = intGlobalDiscount;
		if (document.getElementById('inpUseCustomDel') != null)
		{
			bUseCustomDel = document.getElementById('inpUseCustomDel').value;
		}

		// get the discount
		var oDiscountRate = document.getElementById('inpDiscountRate');
		if (oDiscountRate && !isNaN(oDiscountRate.value) && oDiscountRate.value >= 0) intDiscountRate = oDiscountRate.value;
		//if (!isNaN(oDiscountRate.value)) alert('got it');
		
		
		if (getDeliveryMethod()=='Collection')
		{
			bCollect = true;
		}
		
		//for (var li = 0; li < lineCount; li++)
		var li;
		for (li in lineIDs)
		{
			
			// Get the product and the quantity etc. (for each line)
			var oProductSelect = document.getElementById('inpProductID_'+li);
			var intProductID = oProductSelect.options[oProductSelect.selectedIndex].value;

			if (intProductID=='CUSTOM')
			{
				// a custom line - collect the price
				var oPrice = document.getElementById('lineprice_'+li);

				var oCustomQuant = document.getElementById('inpCustomQuant_'+li);
				var oCustomUnitPrice = document.getElementById('inpCustomUnitPrice_'+li);
				var oCustomVAT = document.getElementById('inpCustomVAT_'+li);

				

				// get the price
				var price = parseFloat(oCustomQuant.value * oCustomUnitPrice.value.replace(',', ''));
				
				if (!isNaN(parseFloat(oCustomUnitPrice.value))) 
				{	
					strUnitPrice = new NumberFormat(parseFloat(oCustomUnitPrice.value.replace(',', ''))).toFormatted();
					oCustomUnitPrice.value = strUnitPrice;
				}
		
				// get the delivery price
				var delprice = 0;
	
				// set it on the line
				var strPrice = new NumberFormat(price).toFormatted();
	
				oPrice.innerHTML = '&pound;'+strPrice;		
				
				if (oCustomVAT.value > 0)
				{
					var vatrate = oCustomVAT.value;
					
					if (arrVAT[vatrate]) arrVAT[vatrate] += price*(100 - intDiscountRate) / 100 + 0.0000000001;
					else arrVAT[vatrate] = price*(100 - intDiscountRate) / 100  + 0.0000000001;
				}
				
				intTotal += parseFloat(price);			
				
			}
			else
			{

				var oVarSelect = document.getElementById('inpQuantVar_'+li);
				var intVarKey = oVarSelect.options[oVarSelect.selectedIndex].value;
	
				var oQuant = document.getElementById('inpQuant_'+li);
				var oPrice = document.getElementById('lineprice_'+li);
				var oRemoveVAT = document.getElementById('inpRemoveVAT'+li);
	
				// get the price
				var price = parseFloat(GetPrice(intProductID, intVarKey, oQuant, 'PRICE'));
		
				// get the delivery price
				var delprice = parseFloat(GetPrice(intProductID, intVarKey, oQuant, 'DELPRICE'));
	
				// set it on the line
				var strPrice = new NumberFormat(price).toFormatted();
	
				oPrice.innerHTML = '&pound;'+strPrice;
				
				// if we have a price greater than 0 add this line to the totals for VAT and delivery
				if (price)
				{
			
					// get the VAT rate
					var vatrate = parseFloat(arrProduct[intProductID][intVarKey][3]);
					
					if (oRemoveVAT.checked && arrProduct[intProductID][intVarKey][5]==1)
					{
						vatrate = 0;
					}
					
					// site id
					var siteid = arrProduct[intProductID][intVarKey][4];
					
					if (!bCollect)
					{
						if (arrDelivery[siteid]) arrDelivery[siteid] += delprice;
						else arrDelivery[siteid] = delprice;
					}
					if (arrVAT[vatrate]) arrVAT[vatrate] += price*(100 - intDiscountRate) / 100 + 0.0000000001;
					else arrVAT[vatrate] = price*(100 - intDiscountRate) / 100  + 0.0000000001;
					intTotal += parseFloat(price);
					
				}		
				
			}

		}
		
		if (!bCollect)
		{
			
			if (bUseCustomDel==1)
			{
				// using custom delivery 
				var oCustomDel = document.getElementById('inpCustomDel');
				if (!isNaN(parseFloat(oCustomDel.value))) 
				{	
					
					intDelTotal = parseFloat(oCustomDel.value);
					document.getElementById('inpCustomDel').value = new NumberFormat(intDelTotal).toFormatted();
				}
				
			}
			else
			{
				for (i in arrDelivery)
				{
					intDelTotal += parseFloat(arrDelivery[i]) + parseFloat(arrSites[i]);
				}
			}
		}
		
		for (i in arrVAT)
		{
			intVAT += roundNumber((parseFloat(arrVAT[i]) * parseFloat(i) + 0.0000000001), 2);
		}
		
		// add delivery VAT
		intVAT += roundNumber((intDelTotal * 0.20) + 0.0000000001, 2);
		
		intDiscount = (intTotal)*intDiscountRate / 100  + 0.0000000001;
		
		//alert('VAT = '+ intVAT+'; Del = '+intDelTotal+'; Total = '+intTotal);
		intTotal += intVAT + intDelTotal - intDiscount + 0.0000000001;
		
		//alert('VAT = '+ intVAT+'; Del = '+intDelTotal+'; Total = '+intTotal);

		strDelTotal = new NumberFormat(intDelTotal).toFormatted();
		strVATTotal = new NumberFormat(intVAT).toFormatted();
		strDiscount = new NumberFormat(intDiscount).toFormatted();
		strTotal = new NumberFormat(intTotal).toFormatted();
		
		// update the display
		document.getElementById('delTotal').innerHTML = '&pound;' + strDelTotal;
		document.getElementById('discount').innerHTML = '-&pound;' + strDiscount;
		document.getElementById('VATTotal').innerHTML = '&pound;' + strVATTotal;
		document.getElementById('Total').innerHTML = '&pound;' + strTotal;
		
	}

	function roundNumber(num, decplaces) {
		return Math.round(num*Math.pow(10,decplaces))/Math.pow(10,decplaces);
	}
	
	function getDeliveryMethod()
	{
		oUseAltAddress = document.frmMain.elements['inpDeliveryMethod'];
		if (oUseAltAddress == null) return '';
		for (i=0; i < oUseAltAddress.length; i++)
		{
			if (oUseAltAddress[i].checked) var thisvalue = oUseAltAddress[i].value;
		}
		return thisvalue;
	}
	
	function handleAltAddressChange() {
		
		var thisvalue = getDeliveryMethod();
		oDelRow = document.getElementById('deladdressrow');
		
		if (thisvalue == 'Alternative')
		{
			oDelRow.style.display = '';
		}
		else
		{
			oDelRow.style.display = 'none';
		}
		calcTotals();
	}

	function handleSpecialInst() {
		oAddSpecialInst = document.getElementById('inpAddSpecialInst');
		oTextarea = document.getElementById('specialinstrow');
		if (oAddSpecialInst.checked)
		{
			oTextarea.style.display = '';
		}
		else
		{
			oTextarea.style.display = 'none';
		}		
	}
	
	function handleFeedback() 
	{
		oAddFeedback = document.getElementById('inpAddFeedback');
		oTextarea = document.getElementById('feedbackrow');
		if (oAddFeedback.checked)
		{
			oTextarea.style.display = '';
		}
		else
		{
			oTextarea.style.display = 'none';
		}		
	}
	
	function handleDiscountChange()
	{
		// is the discount rate OK
		var oDiscountRate = document.getElementById('inpDiscountRate');
		if (oDiscountRate && !isNaN(oDiscountRate.value) && oDiscountRate.value >= 0) 
		{
			intOldDiscountRate = intDiscountRate = oDiscountRate.value;
			calcTotals();
		}
		else
		{
			if (intOldDiscountRate >= 0) oDiscountRate.value =  intOldDiscountRate;
			else oDiscountRate.value = 0;
		}
		
	}
	
	function toggleCustomDel()
	{
		var oDelTotal = document.getElementById('delTotal');
		var oDelCustom = document.getElementById('delCustom');
		var oDelToggle = document.getElementById('delToggle');
		var oUseCustomDel = document.getElementById('inpUseCustomDel');
		
		
		if (oDelTotal.style.display == 'none')
		{
			oDelTotal.style.display = 'block'
			oDelCustom.style.display = 'none'
			oDelToggle.innerText = 'Custom delivery';			
			oUseCustomDel.value = 0;
		}
		else
		{
			oDelTotal.style.display = 'none'
			oDelCustom.style.display = 'block'
			oDelToggle.innerText = 'Auto delivery';
			oUseCustomDel.value = 1;
		}
		calcTotals();	
	}
