///////////////////////////////////////////////////////////////////////////////////////////////
// General calculation functions                                                             //
//                                                                                           //
// 1. getInteger(theNumber) takes a string [theNumber] and returns a decimal integer, but if //
//    [theNumber] is empty, it returns and empty string.                                     //
///////////////////////////////////////////////////////////////////////////////////////////////

function getInteger(theNumber) {
	if(theNumber != '') {
		theNumber = parseInt(theNumber,10);
	}
	return theNumber;
}

////////////////////////////////////////////////////////////////////////////////////////////
// Flip Card subtotalling functions                                                       //
//                                                                                        //
// 1. flipCardSubtotal() calculates the subtotal of the Flip Cards section, and puts this //
//    value into the 'Flip Cards Subtotal' field.                                         //
////////////////////////////////////////////////////////////////////////////////////////////

function flipCardSubtotal() {
	var u2Field = document.theForm.u2FlipCards;
	var u3Field = document.theForm.u3FlipCards;
	var totalField = document.theForm.fcSubtotal;

	var quantity1 = getInteger(u2Field.value);
	var quantity2 = getInteger(u3Field.value);

	if(isNaN(quantity1) && quantity1 != '')	{
		alert("The following field contains a non-valid entry:\n\n2 Unit Maths Flip Cards.\n\nThe invalid entry has been removed.");
		u2Field.value = '';
	}
	else if(isNaN(quantity2) && quantity2 != '') {
		alert("The following field contains a non-valid entry:\n\n3 Unit Maths Flip Cards.\n\nThe invalid entry has been removed.");
		u3Field.value = '';
	}
	else {
		totalField.value = (quantity1 * 35) + (quantity2 * 35) + ".00";

		u2Field.value = quantity1;
		u3Field.value = quantity2;
	}
	getGrandTotal();
}

//////////////////////////////////////////////////////////////////////////////////////////////
// Holiday Course Subtotalling arrays and functions                                         //
//                                                                                          //
// 1. holidayCourse_prefixes and holidayCourse_suffixes are 2 arrays used to create the     //
//    field names that are used in this section. Putting one value from each array together //
//    makes a field name, e.g. 'preMaths' + 'Jul'.                                          //
// 2. holidayCourse_prefixesFull and holidayCourse_siffixesFull are verbose names for       //
//    the field names, used in error alerts.                                                //
// 3. The function holidayCourseSubtotal() calculates the subtotal for the Holiday Course   //
//    section and puts the subtotal into the 'hcbSubtotal' field.                           //
//////////////////////////////////////////////////////////////////////////////////////////////

var holidayCourse_prefixes =     new Array("y10AdvMaths","preMaths","preMathsEx","prePhysics","preChemistry","HSCMaths","HSCMathsEx1","HSCMathsEx2","HSCPhysics","HSCChemistry");
var holidayCourse_prefixesFull = new Array("Year 10 Advanced Mathematics","Preliminary Mathematics","Preliminary Mathematics [Extension]","Preliminary Physics","Preliminary Chemistry","HSC Mathematics","HSC Mathematics [Extension 1]","HSC Mathematics [Extension 2]","HSC Physics","HSC Chemistry");
var holidayCourse_suffixes =     new Array("Apr","Jul","Oct");
var holidayCourse_suffixesFull = new Array("April","July","October");

var holidayCourse_prices = new Array(new Array("65","175"),new Array("65","175"),new Array("65","175"),new Array("65","175"),new Array("65","175"),new Array("65","175"),new Array("65","175"),new Array("65","175"),new Array("65","175"),new Array("65","175"));

function holidayCourseSubtotal() {
	var totalField = document.theForm.hcbSubtotal;
	var alertString = '';
	var finalTotal = new Number();
	var errorFieldsArray = new Array();

	for(i = 0; i < holidayCourse_prefixes.length; i++) {
		for(j = 0; j < holidayCourse_suffixes.length; j++) {
			currentField = eval("document.theForm." + holidayCourse_prefixes[i] + holidayCourse_suffixes[j]);
			currentFieldData = getInteger(currentField.value);
			if(isNaN(currentFieldData) && currentFieldData != '') {
				errorFieldsArray[errorFieldsArray.length] = new Array(i,j);
			}
			else {
				currentField.value = currentFieldData;
				if(holidayCourse_suffixes[j] == 'All') {
					finalTotal += currentFieldData * holidayCourse_prices[i][1];
				}
				else {
					finalTotal += currentFieldData * holidayCourse_prices[i][0];
				}
			}
		}
	}
	if(errorFieldsArray.length != 0) {
		alertString = "The following field contains a non-valid entry:\n\n";
		for(k = 0; k < errorFieldsArray.length; k++) {
			alertString += holidayCourse_prefixesFull[errorFieldsArray[k][0]] + ", " + holidayCourse_suffixesFull[errorFieldsArray[k][1]] + "\n";
			currentField = eval("document.theForm." + holidayCourse_prefixes[errorFieldsArray[k][0]] + holidayCourse_suffixes[errorFieldsArray[k][1]]);
			currentField.value = '';
		}
		alertString += "\nThe invalid entry in this field has been removed.";
		alert(alertString);
	}
	totalField.value = finalTotal + ".00";
	getGrandTotal();
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Comprehensive Notes and Booklets Subtotalling arrays and functions                            //
//                                                                                               //
// 1. comprehensiveNotes_fieldNames array holds the field names for the quantity fields that are //
//    in this section.                                                                           //
// 2. comprehensiveNotes_fieldNamesLong array holds the verbose names for the quantity fields,   //
//    used in error alerts.                                                                      //
// 3. comprehensiveNotes_prices array holds the prices that correspond to the different purchase //
//    options.                                                                                   //
// 4. the comprehensiveNotesSubtotal() function calculates the quantity * unit price for each    //
//    of the options, and puts this value into the 'cnbSubtotal' field.                          //
///////////////////////////////////////////////////////////////////////////////////////////////////

var comprehensiveNotes_fieldNames =     new Array("y10AdvMathsBooks","preMathsBooks","preMathsExBooks","prePhysicsBooks","preChemistryBooks","HSCMathsBooks","HSCMathsEx1Books","HSCMathsEx2Books","HSCPhysicsBooks","HSCChemistryBooks");
var comprehensiveNotes_fieldNamesLong = new Array("Year 10 Advanced Mathematics","Preliminary Maths","Preliminary Mathematics [Extension]","Preliminary Physics","Preliminary Chemistry","HSC Mathematics","HSC Mathematics [Extension 1]","HSC Mathematics [Extension 2]","HSC Physics","HSC Chemistry");
var comprehensiveNotes_prices =         new Array("675","675","675","675","675","675","675","675","675","675");

function comprehensiveNotesSubtotal() {
	var finalTotal = new Number;
	var formReference = document.theForm;
	var fieldReference = '';
	var subtotalReference = document.theForm.cnbSubtotal;
	var errorFieldsArray = new Array();

	for(i = 0; i < comprehensiveNotes_fieldNames.length; i++) {
		fieldReference = eval("document.theForm." + comprehensiveNotes_fieldNames[i]);
		fieldValue = getInteger(fieldReference.value);
		if(isNaN(fieldValue) && fieldValue != '') {
			errorFieldsArray[errorFieldsArray.length] = new Array(comprehensiveNotes_fieldNames[i],comprehensiveNotes_fieldNamesLong[i]);
		}
		else {
			finalTotal += comprehensiveNotes_prices[i] * fieldValue;
		}
	}

	if(errorFieldsArray.length > 0) {
		var alertString = "The following field contains a non-valid entry:\n\n";
		for(j = 0; j < errorFieldsArray.length; j++) {
			alertString += errorFieldsArray[j][1] + "\n";
			currentField = eval("formReference." + errorFieldsArray[j][0]);
			currentField.value = '';
		}
		alertString += "\nThe invalid entry in this field has been removed.";
		alert(alertString);
	}
	subtotalReference.value = finalTotal + ".00";
	getGrandTotal();
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Grand Total calculating function                                                                 //
//                                                                                                  //
// 1. this function simply gets the sum of the fields 'fcSubtotal','hcbSubtotal', and 'cnbSubtotal' //
//    and puts the result into the 'grandTotal' field.                                              //
//////////////////////////////////////////////////////////////////////////////////////////////////////

function getGrandTotal() {
	grandTotalReference = document.theForm.grandTotal;
	var fcSubtotalValue = parseInt(document.theForm.fcSubtotal.value,10);
//	var hcSubtotalValue = parseInt(document.theForm.hcbSubtotal.value,10);
//	var cnSubtotalValue = parseInt(document.theForm.cnbSubtotal.value,10);
	var grandTotal = '';

//	grandTotal = fcSubtotalValue + hcSubtotalValue + cnSubtotalValue;
// rob asked for the Corurse Books & Notes and Holiday Courses to be removed
	grandTotal = fcSubtotalValue;
	grandTotalReference.value = grandTotal + ".00";
}