function clearSelect(selectBox)
{
	if ( selectBox )
	{
		selectBox.options.length = 0;
		selectBox.options[0]=new Option("select", "", true, false)
	}
}

function fillSelect(selectBox, dataArray)
{
	if ( selectBox )
	{
		clearSelect(selectBox);

		for ( i=0; i<dataArray.length; i++ )
		{
			selectBox.options[i+1]=new Option(dataArray[i][0], dataArray[i][1], true, false);
		}
	}
}


function changeAttr1(selectBox)
{
	//  fill attr2 box with contents of stockArray[selectBox.value]
	fillSelect(document.product.attr2, stockArray[selectBox.value]);


	//  change the pricing
	changePrice();
}



function chkForm(theForm)
{
	errorMsg = "";

	var regex=/\d/

	if ( !theForm.quantity.value.match(regex) )
	{
		errorMsg = "- please enter an order quantity\n";
	}

	if ( errorMsg )
	{
		alert("The following errors have occurred:\n" + errorMsg);
		return false;
	}

	return true;
}


function changePrice(value)
{

	if ( value > 1000 )
	{
		alert('Please contact us on 0117 954 7002 or email enquiries@teamcalendars.co.uk for orders of over 1000 calendars.');

		return false;
	}

	xi = priceArray[document.product.attr1.value]['x'];
	yi = priceArray[document.product.attr1.value]['y'];

	usePrice = csSolve(value);
	discountAmount = (discountPct/100) * usePrice;
	usePriceDiscounted = usePrice - discountAmount;
	
	document.product.seasonal_discount.value = CurrencyFormatted(discountAmount);
	//document.product.price_ex.value = CurrencyFormatted(usePrice);
	//document.product.price_inc.value = CurrencyFormatted(usePrice * VAT);
	document.product.price_ex.value = usePrice;
	document.product.price_inc.value = usePrice * VAT;
	
	document.product.use_price.value = usePrice;
	
	document.product.price_dis_ex.value = CurrencyFormatted(usePriceDiscounted);
	document.product.price_dis_inc.value = CurrencyFormatted(usePriceDiscounted * VAT);

}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);

	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}



