
var TOTAL_PRODUCTS = 27;

var PRICES = new Array ();
PRICES[1] = 5.60; 
PRICES[2] = 2.20; 
PRICES[3] = 42.50;
PRICES[4] = 2.20; 
PRICES[5] = 42.50;
PRICES[6] = 2.20; 
PRICES[7] = 42.50;
PRICES[8] = 7.99;
PRICES[9] = 7.99;
PRICES[10] = 2.75;
PRICES[11] = 59.40; 
PRICES[12] = 2.10; 
PRICES[13] = 45.40; 
PRICES[14] = 1.80; 
PRICES[15] = 38.90; 
PRICES[16] = 1.80; 
PRICES[17] = 38.90;
PRICES[18] = 31.75; 
PRICES[19] = 41.00; 
PRICES[20] = 59.75; 
PRICES[21] = 3.75; 
PRICES[22] = 3.75;  
PRICES[23] = 3.75;  
PRICES[24] = 4.85; 
PRICES[25] = 4.85; 
PRICES[26] = 4.85;
PRICES[27] = 4.85; 

function recalculateForm (prefix) {
	if (prefix != '') {
		prefix = '$';
	}

	subTotal = 0;

	for (var prodnum = 1; prodnum <= TOTAL_PRODUCTS; prodnum++) {
		curQty = document.getElementById('prodquan' + prodnum);
		curTot = document.getElementById('prodtotal' + prodnum);
		curSubmit = document.getElementById('prodsubmit' + prodnum);
		newTotal = ( (curQty.value) * PRICES[prodnum] );

		if (!newTotal) {
			curQty.value = 0;
			newTotal = 0;
		}

		curTot.value = prefix + newTotal.toFixed(2);
		curSubmit.value = curQty.value + ',' + PRICES[prodnum].toFixed(2)
		subTotal += parseFloat( newTotal.toFixed(2) );

		if (prodnum==18) {
			curSubmit.name = 'Special Lunchbox Loader (Save 10%) - ' +
			 ((document.getElementById('prodtype1a').checked)? document.forms[1].elements[52].value:document.forms[1].elements[53].value) + ' bars, ' +
			 document.getElementById('juiceqty1').value + ' x Apple, ' +
			 document.getElementById('juiceqty2').value + ' x Apple &amp; Blackcurrant, ' +
			 document.getElementById('juiceqty3').value + ' x Apple &amp; Pear';
		}
		if (prodnum==19) {
			curSubmit.name = 'Special Family Filler (Save 15%) - ' +
			 ((document.getElementById('prodtype2a').checked)? document.forms[1].elements[60].value:document.forms[1].elements[61].value) + ' bars, ' +
			 document.getElementById('juiceqty4').value + ' x Apple, ' +
			 document.getElementById('juiceqty5').value + ' x Apple &amp; Blackcurrant, ' +
			 document.getElementById('juiceqty6').value + ' x Apple &amp; Pear';
		}
		if (prodnum==20) {
			curSubmit.name = 'Special Pantry Packer (Save 20%) - ' +
			 ((document.getElementById('prodtype3a').checked)? document.forms[1].elements[67].value:document.forms[1].elements[68].value) + ' bars, ' +
			 document.getElementById('juiceqty7').value + ' x Apple, ' +
			 document.getElementById('juiceqty8').value + ' x Apple &amp; Blackcurrant, ' +
			 document.getElementById('juiceqty9').value + ' x Apple &amp; Pear';
		}

	}

	deliveryCost = 0;
	deliveryType = document.getElementById('delivery').value;

	if(subTotal > 100) {
		deliveryCost = subTotal * 0.11;
	} else {

		if (deliveryType == 'Melbourne') {
			deliveryCost = 5;
		} else if (deliveryType == 'Victoria') {
			deliveryCost = 7.5;
		} else {
			deliveryCost = 10;
		}

	}

	document.getElementById('subTotal').value = prefix + subTotal.toFixed(2);
	document.getElementById('deliverySubTotal').value = prefix + deliveryCost.toFixed(2);
	document.getElementById('orderTotal').value = prefix + (subTotal + deliveryCost).toFixed(2);
	
	// set juice pack qty to 0 by default
	for (var qtynum = 1; qtynum <= 9; qtynum++) {
		if (!document.getElementById('juiceqty' + qtynum).value) {
			document.getElementById('juiceqty' + qtynum).value = 0;
		}
	}

}

function validateOrderForm() {
	for (var prodnum = 1; prodnum <= TOTAL_PRODUCTS; prodnum++) {
		document.getElementById('prodtotal' + prodnum).disabled = false;
	}
	
	document.getElementById('subTotal').disabled = false;
	document.getElementById('deliverySubTotal').disabled = false;
	document.getElementById('orderTotal').disabled = false;
	
	recalculateForm('');
	
	errorNum = 0;
	errorString = '';

	loaderTotal = getJuiceTotal('Loader');
	fillerTotal = getJuiceTotal('Filler');
	packerTotal = getJuiceTotal('Packer');
	
	if ((loaderTotal != 6) && (parseInt(document.getElementById('prodquan18').value) > 0)) {
		errorNum++;
		errorString += 'Please choose 6 juices for your Lunchbox Loader\n';
	}
	else if ((parseInt(document.getElementById('prodquan18').value) == 0) && (loaderTotal > 0)) {
		errorNum++;
		errorString += 'Please select a quantity of Lunchbox Loaders for your order (or change quantity of juices to 0 if you do not wish to order)\n';
	}
	
	if ((fillerTotal != 8) && (parseInt(document.getElementById('prodquan19').value) > 0)) {
		errorNum++;
		errorString += 'Please choose 8 juices for your Family Filler\n';
	}
	else if ((parseInt(document.getElementById('prodquan19').value) == 0) && (fillerTotal > 0)) {
		errorNum++;
		errorString += 'Please select a quantity of Family Fillers for your order (or change quantity of juices to 0 if you do not wish to order)\n';
	}
	
	if ((packerTotal != 10) && (parseInt(document.getElementById('prodquan20').value) > 0)) {
		errorNum++;
		errorString += 'Please choose 10 juices for your Pantry Packer\n';
	}
	else if ((parseInt(document.getElementById('prodquan20').value) == 0) && (packerTotal > 0))	 {
		errorNum++;
		errorString += 'Please select a quantity of Pantry Packers for your order (or change quantity of juices to 0 if you do not wish to order)\n';
	}
	
	if (document.getElementById('name').value=='') {
		errorNum++;
		errorString += 'Please enter a delivery name\n';
	}
	if (document.getElementById('address').value=='') {
		errorNum++;
		errorString += 'Please enter a delivery address\n';
	}
	if (document.getElementById('suburb').value=='') {
		errorNum++;
		errorString += 'Please enter a delivery suburb\n';
	}
	if (document.getElementById('state').value=='') {
		errorNum++;
		errorString += 'Please select a delivery state\n';
	}
	if (document.getElementById('postcode').value=='') {
		errorNum++;
		errorString += 'Please enter a delivery postcode\n';
	}
	if (document.getElementById('phone').value=='') {
		errorNum++;
		errorString += 'Please enter a contact phone number\n';
	}
	if (document.getElementById('email').value=='') {
		errorNum++;
		errorString += 'Please enter an email\n';
	}
	if (document.getElementById('heardabout').value=='') {
		errorNum++;
		errorString += 'Please select where you heard about us\n';
	}
	if (document.getElementById('delivery').value=='') {
		errorNum++;
		errorString += 'Please select a delivery type\n';
	}
	if (!document.getElementById('readterms').checked) {
		errorNum++;
		errorString += 'Please tick the box when you have read the Whole Kids Privacy Policy & Terms and Conditions of Use\n';
	}

	if (errorNum > 0) {
		alert('Please fix the following form fields:\n' + errorString);
		return false;
	}
	
	document.getElementById('returnlink').value += document.getElementById('orderTotal').value;
}

function getJuiceTotal(pack)
{
	total = 0;
	if (pack == 'Loader') {
		qty1 = document.getElementById('juiceqty1').value;
		qty2 = document.getElementById('juiceqty2').value;
		qty3 = document.getElementById('juiceqty3').value;
	}
	else if (pack == 'Filler') {
		qty1 = document.getElementById('juiceqty4').value;
		qty2 = document.getElementById('juiceqty5').value;
		qty3 = document.getElementById('juiceqty6').value;	
	}
	else if (pack == 'Packer') {
		qty1 = document.getElementById('juiceqty7').value;
		qty2 = document.getElementById('juiceqty8').value;
		qty3 = document.getElementById('juiceqty9').value;
	}

	total = (parseInt(qty1) + parseInt(qty2) + parseInt(qty3))
	return total;
}
