function calcTicketPrice() {

	var gtotal = 0;

	var taqs = new Array();
	var tcqs = new Array();
	var tfqs = new Array();
	var tjs = new Array();
	var totals = new Array();

	// take a look at the selected date and use
	// this to decide which price set to use.

	// Convert selected date to sql format.
	var date = document.f1.year.value+document.f1.month.value+document.f1.day.value

	// Check that the selected date falls within
	// the allowed booking period.
	if (!chkdate(date)) return false;

	// get the required prices from the app. js array.
	if (date>cur_edate) {
		// get NEXT prices.
		var prices = nps;

		// record pricesetid
		document.f1.pricesetid.value = npsid;
	} else {
		// get CURRENT prices.
		var prices = cps;

		// record pricesetid
		document.f1.pricesetid.value = cpsid;
	}

	var pattern = /^\d*$/;	// 0 or more numbers and nothing else.

	// read all the values for the quantity and journey
	// types in to respective arrays.
	for (i=0;i<document.f1.elements.length;i++) {

		n=document.f1.elements[i].name.substr(2);
		type=document.f1.elements[i].name.substr(0,2);

		// check that quantities have been inputed as numbers.
		if ((type=='ta')||(type=='tc')||(type=='tf')) {
			if (!pattern.test(document.f1.elements[i].value)) {
				alert('Please enter ticket quantities as numbers.');
				document.f1.elements[i].value = '';
			}
			if (document.f1.elements[i].value==0) {
				document.f1.elements[i].value = '';
			}
		}

		// read tickets quantity into arrays |ta.= adult, tc.= child, tf.= family |array index = ticketId
		// check for discount 2for1 or child_free
		if (type=='ta') {
			taqs[n] = document.f1.elements[i].value;
		} else if (type=='tc') {
			tcqs[n] = document.f1.elements[i].value;
		} else if (type=='tf') {
			tfqs[n] = document.f1.elements[i].value;
		} else if (type=='tj') {
			tjs[n] = document.f1.elements[i].value;
		}
	}

	// calculate the total price for each ticket
	// and store in an array.
	for (i=1;i<prices.length;i++) {
		totals[i] = 0;
		if (tjs[i]=='single') {
			if (taqs[i]) {
					totals[i] += parseInt(taqs[i])*parseFloat(prices[i][0]);
			}
			if (tcqs[i]) {
				totals[i] += parseInt(tcqs[i])*parseFloat(prices[i][3]);
			}
			if (tfqs[i]) {
				totals[i] += parseInt(tfqs[i])*parseFloat(prices[i][6]);
			}
		} else if (tjs[i]=='return') {
			if (taqs[i]) {
				totals[i] += parseInt(taqs[i])*parseFloat(prices[i][1]);
			}
			if (tcqs[i]) {
				totals[i] += parseInt(tcqs[i])*parseFloat(prices[i][4]);
			}
			if (tfqs[i]) {
				totals[i] += parseInt(tfqs[i])*parseFloat(prices[i][7]);
			}
		} else if (tjs[i]=='unlimited') {
			if (taqs[i]) {
				totals[i] += parseInt(taqs[i])*parseFloat(prices[i][2]);
			}
			if (tcqs[i]) {
				totals[i] += parseInt(tcqs[i])*parseFloat(prices[i][5]);
			}
			if (tfqs[i]) {
				totals[i] += parseInt(tfqs[i])*parseFloat(prices[i][8]);
			}
		}

		if (totals[i]==0) {
			totals[i] = '';
		} else {
			// correct rounding error.
			totals[i] = Math.round((totals[i])*100)/100;
			totals[i] = totals[i].toString();

			// make sure we have 2dps
			if (totals[i].indexOf('.')==-1) {
				totals[i] += '.00';
			} else if (totals[i].indexOf('.')==totals[i].length-2) {
				totals[i] += '0';
			}
		}
	}

	for (i=0;i<document.f1.elements.length;i++) {
		if (document.f1.elements[i].name.substr(0,2)=='tp') {
			n=document.f1.elements[i].name.substr(2);
			document.f1.elements[i].value = totals[n];

			// calculate the total price for all tickets.
			if (document.f1.elements[i].value!='') {
				gtotal += parseFloat(document.f1.elements[i].value);
			}
		}
	}

	if (gtotal==0) {
		gtotal = '';
	} else {
		// deduct discount from total price
		if(document.getElementById('discount')) {
			
			gtotal -= parseFloat(document.getElementById('discount').value);
			if (gtotal < 0) 
				gtotal =0;
		}

		// correct rounding error.
		gtotal = Math.round((gtotal)*100)/100;
		gtotal = gtotal.toString();

		// make sure we have 2dps
		if (gtotal.indexOf('.')==-1) {
			gtotal += '.00';
		} else if (gtotal.indexOf('.')==gtotal.length-2) {
			gtotal += '0';
		}
	}

	document.f1.gtotal.value = gtotal;
}
