// Tuition and Fee Calculator for Program Pages


function calcTexts() {
	texts = 0;
	for (i=1; document.getElementById("texts"+i)!=null; i++) {
		textChecked = document.getElementById("texts"+i).checked;
		if (textChecked == true) textCount = document.getElementById("texts"+i).value;
		else textCount = 0;
		texts += parseFloat(textCount);
	}		
	return texts;
}

function calcStudent()
{
	for (var i=0; i < document.cost.student.length; i++){
	   if (document.cost.student[i].checked){
	      return document.cost.student[i].value;
	   }
	}
}



function formCalc(){
	//retreive form data
	var length = document.cost.length.value;
	var regFee = document.cost.regFee.value;
	var hours = document.cost.hours.value;
	var monthlyFee = document.cost.monthlyFee.value;
	var student = calcStudent();

	// Validate the 'hours' input to make sure it's less than 40
	    if((hours>40) || (hours<1) || (hours == null)) {
		 alert("Students can enroll in a maximum of 40 hours per week. Please enter a number between 1 and 40.");
		 return false;
		}
	    if(isNaN(hours)){
		 alert("Please enter a number between 1 and 40 for the number of hours per week you wish to enroll in.");
		 return false;
		}
	
	// Validate the 'length' input to allow for input on the MS Office program
	var testMS = document.cost.MS;
	if(testMS != null){
	  if(document.cost.MS.value == "true"){
	    if((length>631) || (length<60) || (length == null)) {
		 alert("Students taking courses in this program can take a minimum of 60 hours and a maximum of 630 hours. Please select the courses you would like to take and put the total number of hours in the 'Length of Program' field.");
		 return false;
		}
	    if(isNaN(length)){
		 alert("Students taking courses in this program can take a minimum of 60 hours and a maximum of 630 hours. Please select the courses you would like to take and put the total number of hours in the 'Length of Program' field.");
		 return false;
		}
	  }
	}
	
	//calculate total of texts/supplies checked
	var textsTotal = calcTexts();
	
	//calculate length of program in months and monthly fee total
	var months = length/hours/4.5;
	months = Math.round(months * 10) / 10; //round months to 1 decimal place

	var monthlyFeeTotal = Math.ceil(months) * monthlyFee; //round months up to whole number and multiply by monthly fee

	
	
	// Now determine the tuition rate based on the 'hours' var.
	var tuitionRate = 1.5;


	//calculate the totals to be displayed
	tuitionTotal = tuitionRate * parseInt(length) * parseInt(student);
	tuitionTotal = Math.round(tuitionTotal*100)/100; //round tuition 2 decimal places

	feeTotal = parseFloat(monthlyFeeTotal) + parseFloat(regFee);

	programTotal = tuitionTotal + feeTotal + parseFloat(textsTotal);

	monthlyCost = (Math.round((tuitionTotal/months)*100)/100)+parseFloat(monthlyFee);


	//display the program total, monthly payment, and program length in months
	document.getElementById("total").value = programTotal.toFixed(2);
	document.getElementById("lengthTotal").value = months;
	document.getElementById("monthlyCost").value = monthlyCost;
						 
}


