function updateTicketSummary() {

	var fieldValue;
	var totalCount = 0;
	for(var i = 1; i < 24; i++) {
			fieldValue = parseInt(document.getElementById('r' + i).value);
			if(!isNaN(fieldValue)) {
				totalCount += fieldValue;
			}
	}
	
	document.getElementById('ticketCount').innerHTML = totalCount;
	document.getElementById('hidAmountDue').value = getAmountDue(totalCount);
	document.getElementById('amountDue').innerHTML	 = "$" + document.getElementById('hidAmountDue').value;
	document.getElementById('amountDue2').innerHTML	 = document.getElementById('amountDue').innerHTML;
	if(offerAnother(totalCount)) {
		alert('Because of the discount option of 6 tickets for $5, you can choose one more ticket without increasing your amount due.');
	}
}


function validate() {
	if(document.getElementById('hidAmountDue').value == '0') {
		alert('Please request at least one ticket before submitting your order.');
		return false;
	}
	
	if(document.getElementById('hidAmountDue').value == '') {
		alert('Please total your ticket count and amount due using the "TOTAL MY TICKETS" button before submitting your order.');
		return false;
	}
	
	if(document.getElementById('firstName').value == '') {
		alert('Please enter your first name.');
		return false;
	}
	
	if(document.getElementById('lastName').value == '') {
		alert('Please enter your last name.');
		return false;
	}
	
	if(document.getElementById('emailAddress').value == '') {
		alert('Please enter your e-mail address.');
		return false;
	}
	
	if(!document.getElementById('radPayment1').checked && !document.getElementById('radPayment2').checked) {
		alert('Please choose your payment method before submitting your order.');
		return false;
	}
	return true;
}

function getTicketAllowance(amountToSpend) {
	//tickets are $1 each, 6 for $5

	//if less than the discount price, it's 1 to 1
	if(amountToSpend < 5) return amountToSpend;
	
	//single dollar purchases
	var singleDollarPurchases = amountToSpend % 5;
	
	//groups of six for $5
	var groupsOfSix = (amountToSpend - singleDollarPurchases)/5;
	
	//sum groups of six and singles
	var totalTickets = groupsOfSix * 6 + singleDollarPurchases;
	
	return totalTickets;
}


function getAmountDue(ticketCount) {
	

	//single dollar purchases
	var singleDollarPurchases = ticketCount % 6;
	
	//groups of six for $5
	var groupsOfSix = (ticketCount - singleDollarPurchases)/6;
	
	//sum the amounts
	var amountDue = groupsOfSix * 5 + singleDollarPurchases
	
	return amountDue;
}

function offerAnother(ticketCount) {
	//if the person chooses a number of tickets
	//that is the same cost as a greater number
	//(because of the discount), return true
	
	if(ticketCount == 5) return true;
	
	if(ticketCount > 6) {
		if(ticketCount % 6 == 5) return true;
	}
	
	return(false);

}


function isInt(s) {
return false;
	var validChars = "0123456789";
	var char;
	if(s.length == 0) return false;
 
	for (i = 0; i < s.length; i++) { 
		char = s.charAt(i); 
		if (validChars.indexOf(char) == -1) {
			return false;
		}
	}
}
