// VariablePeriods.js * offers the possibility to use periods in months instead of years
//	created: 	13 mar 2006 by Be Value/JvdH
//	last edit:	14 mar 2006 by Be Value/JvdH
//
//	uses 'indexTerms' array from rateSheet

var monthLabel = " mesi";

//this overwrites the function in calculatorController.js
//the period is no longer the number of years, but the index for the ratesheet; we have to find the corresponding number of months
function cctl_DetermineNrOfFurtherPaymentsForRegular(period, frequency){
	
	var freqPerYear;
	switch (frequency.toUpperCase()) {
		case "MONTH":
			freqPerYear=12;
			break;
		case "BIMONTHLY":
			freqPerYear=6;
			break;
		case "QUARTER":
			freqPerYear=4;
			break;
		case "3PERYEAR":
			freqPerYear=3;
			break;
		case "SEMIANNUAL":
			freqPerYear=2;
			break;
		case "YEAR":
			freqPerYear=1;
			break;
	}

	var months = getNbrOfMonths(period);
	var returnValue = months * freqPerYear / 12
	
	if (Math.floor(returnValue)<returnValue){
		returnValue=0;
	}

	return (returnValue - 1);

}

//this overwrites the function in printcontroller.js
function pctl_DetermineNumberOfPayments(leaseFrequency, leasePeriod) {
	return (cctl_DetermineNrOfFurtherPaymentsForRegular(leasePeriod, leaseFrequency) + 1);
}

//this function is called in calc**.html; before load of calculatorController or calculatorControllerAddition
function loadPeriodsFromRateSheet(){

	var pe = document.getElementById("period");
	if (pe!=null){
		pe.options.length=0;
		
	
		if (indexTerms!=null){
			pe.options.length=indexTerms.length;
			for (var i=0;i<indexTerms.length;i++){
				pe.options[i]=new Option(""+indexTerms[i]+monthLabel,i+1);
			}
		}
	}
}

function getNbrOfMonths(rateIndex){

	if ((indexTerms!=null)&&(rateIndex>0)){
		return indexTerms[rateIndex-1];
	}

	return 0;

}