var  	valueCowSignals, valueCowSignalsTen, valueCowSignalOnes, valueFertility, valueFromCalfToHeifer, valueHooves, valueRoboticMilking, valueUdderHealth, subTotal, gstRate, pstRate, gst, pst, handling, total, costCowSignals, costFertility, costFromCalfToHeifer, costHooves, costRoboticMilking, costUdderHealth; //Global variables



gst = 0;
subTotal = 0;
total = 0;
handling = 3;
costCowSignals = 24.90;
costFertility = 21.90;
costFromCalfToHeifer = 15.90;
costHooves = 15.90;
costRoboticMilking = 32.90;
costUdderHealth = 21.90;



//Use round() function to convert a "value" to "dpoints" decimal places

function round(value,dpoints){
value= makeNumber(value)
if(!dpoints) dpoints=2
var expr= ""+Math.round(value*Math.pow(10,dpoints))
while (expr.length <= dpoints) {
expr="0"+expr}
var dp=expr.length-dpoints
return (expr.substring(0,dp)+"."+expr.substring(dp,expr.length));
}

//Use toCurrency() function to convert "value to "currency", where the default is "$".

function toCurrency (value, currency) {
	if(isNaN(parseFloat(value))) {
	alert("The value is not a number")
	}else 	{if(!currency) currency="$"
		var newValue = currency+round(value)
		return newValue
		}
	}

//Use makeNumber() function to convert an expression to a number, useful for form entries which include "$" 	
//and commas.

function makeNumber(result){
	var newResult="", decpoint=0;
	if(!isNaN(result)) return result;
	else result= ""+result
	for(i=0;i<result.length;i++){
	if( result.charAt(i) == "." || !isNaN (parseInt(result.charAt(i))) )
	newResult+=result.charAt(i)
		}

	for(i=0;i<newResult.length;i++){
	if (newResult.charAt(i)==".") { decpoint++}

	}
	if (decpoint>1) {return ( alert("\""+result+"\" has "+decpoint+"\n decimal points")) }else{
	return parseFloat(newResult)}

	}

	// Use commaThis() function to convert an expression to a number with commas e.g. 10000 to 10,000

	function commaThis(value)
	{
	var newblock=""
	value=makeNumber(value);
	value+=""
	var dp= (value.indexOf(".") != -1)? value.indexOf("."):0
	var trailing= (dp==0)? "":value.substring(dp,value.length);
	if(trailing) value=value.substring(0,dp)
	if(value.length>3)
	{
		var offset=(value.length)%3
		var block=(offset>0)? value.substring(offset,value.length):value
		for(i=0;i<block.length;i++){
		if((i%3)==0) newblock+=","
		newblock+=block.charAt(i)

	}
	block=newblock.substring(1)

	if(offset>0){
	return (value.substring(0,offset)+","+block+trailing)	}
	else{
	return block+trailing }
	}
	else {
	return value+trailing }
	}

	//Use commaCurrency() to convert an expression or value to currency with commas.

	function commaCurrency(value)
	{
	value=toCurrency(value)
	value=value.substring(1)
	value=commaThis(value)
	value="$"+value
	while (value.length<10)
		{value=" "+value}
	return value
	}

	function taxRate()
	{
	var rate=0.05;
	
	if(document.f.buyerProv.value == ".05"){rate = 0.05};
	if(document.f.buyerProv.value == ".05"){rate = 0.05};
	if(document.f.buyerProv.value == ".05"){rate = 0.05};
	if(document.f.buyerProv.value == ".05"){rate = 0.05};
	if(document.f.buyerProv.value == ".13"){rate = 0.13};
	if(document.f.buyerProv.value == ".13"){rate = 0.13};
	if(document.f.buyerProv.value == ".05"){rate = 0.05};
	if(document.f.buyerProv.value == ".13"){rate = 0.13};
	if(document.f.buyerProv.value == ".05"){rate = 0.05};
	if(document.f.buyerProv.value == ".05"){rate = 0.05};
	if(document.f.buyerProv.value == ".05"){rate = 0.05};
	if(document.f.buyerProv.value == ".05"){rate = 0.05};
	if(document.f.buyerProv.value == ".05"){rate = 0.05};
	return rate;
	}
	
	

	function calc()
	{
	setCowSignalsQuantity(document.f.cowSignalsQuantityTens.value,document.f.cowSignalsQuantity.value);
	setFertilityQuantity(document.f.FertilityQuantityTens.value,document.f.FertilityQuantity.value);
	setCalfToHeiferQuantity(document.f.calftoHeiferQuantityTens.value,document.f.calftoHeiferQuantity.value);
	setHoovesQuantity(document.f.hoovesQuantityTens.value,document.f.hoovesQuantity.value);
	setRoboticMilkingQuantity(document.f.roboticMilkingQuantityTens.value,document.f.roboticMilkingQuantity.value);
	setUdderHealthQuantity(document.f.udderHealthQuantityTens.value,document.f.udderHealthQuantity.value);
	handling=3;

	if(document.f.buyerEmail$.value != '' && document.f.buyerEmail$.value == document.f.buyerEmail2$.value)
		{
		gstRate=taxRate();
		//gstRate = 0.05;
		subTotal=makeNumber(valueCowSignals*costCowSignals + valueFertility * costFertility +  valueFromCalfToHeifer * costFromCalfToHeifer + valueHooves * costHooves + valueRoboticMilking * costRoboticMilking + valueUdderHealth * costUdderHealth  + handling);
		if(subTotal == 3) {subTotal = 0};
		if(subTotal == 0) {handling = 0};
		gst = makeNumber(subTotal*gstRate);
		total = subTotal + gst;
		document.f.subTotal$.value=commaCurrency(subTotal);
		document.f.handling$.value=commaCurrency(handling);
		document.f.gst$.value=commaCurrency(gst);
		document.f.total$.value=commaCurrency(total);
		}
	else
		{
		alert("The emails are not identical! Please re-enter.");
		}

	}




	function setCowSignalsQuantity(recQuantityTens,recQuantity)
	{
	valueCowSignals = makeNumber(recQuantityTens) + makeNumber(recQuantity);
	document.f.cowSignalsExtended$.value=commaCurrency(valueCowSignals*costCowSignals);

	} 


	function setFertilityQuantity(recQuantityTens,recQuantity)
	{
	valueFertility = makeNumber(recQuantityTens) + makeNumber(recQuantity);
	document.f.fertilityExtended$.value=commaCurrency(valueFertility*costFertility);
	}





	function setCalfToHeiferQuantity(recQuantityTens,recQuantity)
	{
	valueFromCalfToHeifer = makeNumber(recQuantityTens) + makeNumber(recQuantity);
	document.f.calfToHeiferExtended$.value=commaCurrency(valueFromCalfToHeifer*costFromCalfToHeifer);
	}





	function setHoovesQuantity(recQuantityTens,recQuantity)
	{
	valueHooves = makeNumber(recQuantityTens) + makeNumber(recQuantity);
	document.f.hoovesExtended$.value=commaCurrency(valueHooves*costHooves);	}





	function setRoboticMilkingQuantity(recQuantityTens,recQuantity)
	{
	valueRoboticMilking = makeNumber(recQuantityTens) + makeNumber(recQuantity);
	document.f.roboticMilkingExtended$.value=commaCurrency(valueRoboticMilking*costRoboticMilking);
	}





	function setUdderHealthQuantity(recQuantityTens,recQuantity)
	{
	valueUdderHealth = makeNumber(recQuantityTens) + makeNumber(recQuantity);
	document.f.udderHealthExtended$.value=commaCurrency(valueUdderHealth*costUdderHealth);
	}

