function CalculateTotal(frm) {
var order_total = 0;
for (var i=0; i < frm.elements.length; ++i) {// Run through all the form fields
form_field = frm.elements[i]; // Get the current field
form_name=form_field.name;
if (form_name.substring(0,4) == "ITEM") {   // Is it a "product" field?
item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1));// If so, extract the price from the name
item_quantity = parseInt(form_field.value); // Get the quantity
if (item_quantity >= 0) {// Update the order total
order_total += item_quantity * item_price;
}
}
}
frm.TOTAL.value = round_decimals(order_total, 2);// Display the total rounded to two decimal places
}

// Calculate BUS Passes

function BusCalculateTotal(frm) {
var order_total = 0;
for (var i=0; i < frm.elements.length; ++i) {// Run through all the form fields
form_field = frm.elements[i]; // Get the current field
form_name=form_field.name;
if (form_name.substring(0,7) == "BUSPASS") {   // Is it a "product" field?

// Set Bus Pass Price
item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1));// If so, extract the price from the name
item_quantity = parseInt(form_field.value); // Get the quantity
if (item_quantity >= 0) {// Update the order total
order_total += item_quantity * item_price;
}
}
}
frm.TOTAL.value = round_decimals(order_total, 2);// Display the total rounded to two decimal places
}




function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals);
    var result2 = Math.round(result1);
    var result3 = result2 / Math.pow(10, decimals);
    return pad_with_zeros(result3, decimals);
}

function pad_with_zeros(rounded_value, decimal_places) {
var value_string = rounded_value.toString(); // Convert the number to a string
var decimal_location = value_string.indexOf("."); // Locate the decimal point
if (decimal_location == -1) { // Is there a decimal point?
decimal_part_length = 0;  // If no, then all decimal places will be padded with 0s
value_string += decimal_places > 0 ? "." : "";  // If decimal_places is greater than zero, tack on a decimal point
}else{
decimal_part_length = value_string.length - decimal_location - 1; // If yes, then only the extra dec. places will be padded with 0s
}
var pad_total = decimal_places - decimal_part_length;     // Calculate the number of decimal places that need to be padded with 0s
if (pad_total > 0) {
for (var counter = 1; counter <= pad_total; counter++)         // Pad the string with 0s
value_string += "0";
}
return value_string;
}


function checkForm(form){
	if(form.name.value == ""){
		alert("Please enter your name.");
		form.name.focus();
		return false;
	}
	if(form.address1.value == ""){
		alert("Please enter your street address.");
		form.address1.focus();
		return false;
	}
	if(form.city.value == ""){
		alert("Please enter your city.");
		form.city.focus();
		return false;
	}
	if(form.state.value == ""){
		alert("Please enter your state.");
		form.state.focus();
		return false;
	}
	if(form.zip.value == ""){
		alert("Please enter your zip code.");
		form.zip.focus();
		return false;
	}
	if(form.email.value.indexOf("@") < 0 || 
	   form.email.value.indexOf(".") < 0 || 
	   form.email.value == ""){
		alert("A valid e-mail address is required.");
		form.email.focus();
		return false;
	}
	if(form.TOTAL.value == "" || form.TOTAL.value == null || form.TOTAL.value == "0.00"){
		alert("You have not ordered any items.  Please enter the quantity of the items you would like to purchase");
		return false;
	}
return true;	
}
function validate(form){
	if(checkForm(form) == false){
		return false;
	}else{
	return true;
	}
}


function checkFormBus(form){
	
	var affil = "";
	
	
	if(form.name.value == ""){
		alert("Please enter your name.");
		form.name.focus();
		return false;
	}
	
	// Add check for selection box.
	
	if(	form.alumni.checked == false &&   
    	form.faculty.checked == false &&
    	form.staff.checked == false &&
    	form.existseason.checked == false &&
    	form.notseason.checked == false){
		alert("Please select a checkbox of affiliation");
		form.alumni.focus();
		return false;
	}
	
	// Set Affiliation field
	if(	form.alumni.checked == true ){
		affil = "alumni_";
	}
	
	if(	form.faculty.checked == true ){
		affil += "faculty_";
	}
	
	if(	form.staff.checked == true ){
		affil += "staff_";
	}
	
	if(	form.existseason.checked == true ){
		affil += "existseason_";
	}
	
	if(	form.notseason.checked == true ){
		affil += "notseason_";
	}

	form.college.value = affil;

	//  Affiliation field  Set
	
	if(form.address1.value == ""){
		alert("Please enter your street address.");
		form.address1.focus();
		return false;
	}
	if(form.city.value == ""){
		alert("Please enter your city.");
		form.city.focus();
		return false;
	}
	if(form.state.value == ""){
		alert("Please enter your state.");
		form.state.focus();
		return false;
	}
	if(form.zip.value == ""){
		alert("Please enter your zip code.");
		form.zip.focus();
		return false;
	}
	if(form.email.value.indexOf("@") < 0 || 
	   form.email.value.indexOf(".") < 0 || 
	   form.email.value == ""){
		alert("A valid e-mail address is required.");
		form.email.focus();
		return false;
	}
	if(form.TOTAL.value == "" || form.TOTAL.value == null || form.TOTAL.value == "0.00"){
		alert("Please enter the quantity of bus passes you would like to purchase");
		return false;
	}
return true;	
}
function validatebus(form){
	if(checkFormBus(form) == false){
		return false;
	}else{
	return true;
	}
}
