// Javascript collection for cottlehill.co.nz

function openWindow (page)  {
	window.open(page, '_blank', 'height=400,width=400,statusbar=yes');
}


/* This function will compute the total of the order by traversing the
   form elements.
*/
function calcTotal (f)  {
	var t = 0.0;	// Total price
	var b = 0;	// Total bottles

	// Given the form 'f', find all fields that represent wines/spirits
	// and grab their totals.
	for (var i = 0; i < document.forms[f].elements.length; i++)  {
		// Only deal with those that have the word '_bottles' in them.
		var idx = document.forms[f].elements[i].name.indexOf("_bottles");
		if (idx > -1)  {
			// Get the root of the field name (since this will then
			// form the base for the price field name as well.
			t += (document.forms[f].elements[i].value * document.getElementsByName(document.forms[f].elements[i].name.substring(0, idx) + "_price")[0].value);
			b += (document.forms[f].elements[i].value) * 1;
		}
	}
	// Fill in the visible (but disabled) and the hidden fields.  Only the hidden (active) field is sent to the server...the disabled field is not sent.
	document.getElementsByName("totalprc_dummy")[0].value = t;
	document.getElementsByName("totalprc")[0].value = t;
	document.getElementsByName("totalbtls_dummy")[0].value = b;
	document.getElementsByName("totalbtls")[0].value = b;
}
