// Finger Food Quote Calculator

var totCost = 0;
function calcCost() 
	{
	var form = document.getElementById("choices");
	var totA = 0;
	var totB = 0;
	var totC = 0;
	var eskies = 1;									// Number fo eskies required = guests / x
	var wstaff = 1;									// Number of wait staff = guests / 40
	var guests = 0;
	var gPack = new Array(4);						// Initialize pack sizes array 
		gPack[0] = 30;
		gPack[1] = 50;
		gPack[2] = 80;
		gPack[3] = 120;
	var rPack = new Array(3);						// This is where the prices are stored
		rPack[0] = new Array (7.95, 10.95, 12.95);
		rPack[1] = new Array (8.95, 11.95, 13.95);
		rPack[2] = new Array (9.95, 14.95, 16.95);
	totCost = 0;
	i = 0;
	for(ir = 0; ir < 3; ir +=1 ) 					// Three ranges - Value, Delux, Premium
		{
		for (ip = 0; ip <3; ip +=1)					// Three variety mixes within each range
			{
			for (ig = 0; ig < 4; ig +=1)			// Four different packs catering for 30, 50, 80 and 120 guests
				{
				if (form.elements[i].value != "" ) 
					{
						nInput = parseInt(form.elements[i].value);		// Number of packs requested
						nGuests = nInput * gPack[ig];					// Calc number of guests based on pack requested
						// alert (ir + " " + ip + " " + ig +" " + i + " " + gPack[ig] + "guests at " + "$" + rPack[ir][ip]);
						totCost = totCost + (nGuests *  rPack[ir][ip]);  	// Increment total cost 
						guests = guests + nGuests;							// Increment total costs												
					}
				i +=1														// step through all entries
				}
			}
		}
	// alert (guests + " " + totCost);
	eskies = 	Math.ceil(guests / 30);						// Work out how many eskies required (1 per 30 guests)
	wstaff = 	Math.ceil(guests / 40);						// Work out how many wait staff (1 per 40 guests)
	if (form.fcater[0].checked == false)             	// If full catering NOT required 
		{totCost = totCost + 50 + 5 * eskies;			// add $50 delivery and $5 per esky 
		if (form.xtras[0].checked == true)				// and add cents per guest for extras pack if required
		{totCost = totCost + .5 * guests;}				// All above are included in full catering price
		}
	else
		{totCost = totCost + 100 + 80 * wstaff;}
	if (form.drinks[0].checked == true)
		{totCost = totCost + 11.95 * guests;}
	if (form.soft[0].checked == true)
		{totCost = totCost + 4.95 * guests;}
	if (form.oven[0].checked == true)
		{totCost = totCost + 100;}
		
	document.getElementById("textfield").value = formatCurrency(totCost);
	}
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
// return (((sign)?'':'-') + num + '.' + cents);
}
