function calculate(formname,loan_subtype) {

	var month = 0;
	var term,loan_type;
	var residual = 0;
	var i=0;				// index variable
	var term_residual=0;
	var a = 0;				// advance payments flag variable

	var loanAmount = removeBits(formname.FINANCE_AMOUNT.value);
	var rate = removeBits(formname.Rate.value);

	if (formname.quotetype[0].checked) {
		loan_type = "CONSUMER";
	}
	else {
	    loan_type = "COMMERCIAL";
	}

	if (loan_type == "CONSUMER") {
		i = formname.TermYears.options.selectedIndex;
		term = removeBits(formname.TermYears.options[i].value);
		residual = 0;
		a = CONSUMER_ADVANCE_PAYMENTS;			// 1 or 0

		// Define PV, Present Value, as the amount for later calculation,
		// inlcuding the brokerage, defined as a dollar value (if > 1) or
		// a percentage if a decimal fraction.
		if (CONSUMER_BROKERAGE > 1) { // Dollar value...
			PV = loanAmount + (CONSUMER_BROKERAGE * (1 + BROKERAGE_GST));
		}
		else if ((CONSUMER_BROKERAGE > 0) && (CONSUMER_BROKERAGE <= 1)) { // Percentage value...
			PV = loanAmount + (loanAmount * CONSUMER_BROKERAGE * (1 + BROKERAGE_GST));
		}
		else { PV = loanAmount; // no brokerage
		}

//		alert("Private quote selected for $"+loanAmount+" ...total=$"+PV);

		// CHECK REQUIRED INPUT HAS BEEN RECEIVED...
		if (loanAmount == 0) {
			alert("You must specify a Finance Amount."); }
		else if (term == 0) {
			alert("You must specify a Term for the loan (in YEARS)."); }
		else if (rate == 0) {
			alert("You must specify an Interest Rate for the loan.");
		}
		
		// RESET COMMERCIAL LOAN SUBTYPE OPTIONS...
	    formname.comm_light.options.selectedIndex = 0;
		formname.comm_salesrep.options.selectedIndex = 0;
		formname.comm_traveller.options.selectedIndex = 0;
    	formname.comm_heavy.options.selectedIndex = 0;
	
	}  // END CONSUMER LOAN PREPARATION
	
	else {	// loan_type == "COMMERCIAL"

		a = COMMERCIAL_ADVANCE_PAYMENTS;			// 1 or 0

		// Define PV, Present Value, as the amount for later calculation,
		// inlcuding the brokerage, defined as a dollar value (if > 1) or
		// a percentage if a decimal fraction.
		if (COMMERCIAL_BROKERAGE > 1) { // Dollar value...
			PV = loanAmount + (COMMERCIAL_BROKERAGE * (1 + BROKERAGE_GST));
		}
		else if ((COMMERCIAL_BROKERAGE > 0) && (COMMERCIAL_BROKERAGE <= 1)) { // Percentage value...
			PV = loanAmount + (loanAmount * COMMERCIAL_BROKERAGE * (1 + BROKERAGE_GST));
		}
		else { PV = loanAmount; // no brokerage.
		}

		
		// DETERMINE WHICH LOAN SUBTYPE SELECTED...
		if (loan_subtype == "comm_light") {
          i = formname.comm_light.options.selectedIndex;
		  term_residual = formname.comm_light.options[i].value; 
		  formname.comm_salesrep.options.selectedIndex = 0;
		  formname.comm_traveller.options.selectedIndex = 0;
		  formname.comm_heavy.options.selectedIndex = 0;
		  formname.Residual.value = "";
		  // new code 
		  formname.TermYears.options.selectedIndex = formname.comm_light.options.selectedIndex + 1;		}
		else if (loan_subtype == "comm_salesrep") {
          i = formname.comm_salesrep.options.selectedIndex;
		  term_residual = formname.comm_salesrep.options[i].value;
		  formname.comm_light.options.selectedIndex = 0;
		  formname.comm_traveller.options.selectedIndex = 0;
		  formname.comm_heavy.options.selectedIndex = 0;
		  formname.Residual.value = "";
		  // new code 
		  formname.TermYears.options.selectedIndex = formname.comm_salesrep.options.selectedIndex + 1;		
		}
		else if (loan_subtype == "comm_traveller") {
          i = formname.comm_traveller.options.selectedIndex;
		  term_residual = formname.comm_traveller.options[i].value; 
		  formname.comm_light.options.selectedIndex = 0;
		  formname.comm_salesrep.options.selectedIndex = 0;
		  formname.comm_heavy.options.selectedIndex = 0;
		  formname.Residual.value = "";
		  // new code 
		  formname.TermYears.options.selectedIndex = formname.comm_traveller.options.selectedIndex + 1;	
		}
		else if (loan_subtype == "comm_heavy") {
          i = formname.comm_heavy.options.selectedIndex;
		  term_residual = formname.comm_heavy.options[i].value; 
		  formname.comm_light.options.selectedIndex = 0;
		  formname.comm_salesrep.options.selectedIndex = 0;
		  formname.comm_traveller.options.selectedIndex = 0;
		  formname.Residual.value = "";
		  // new code 
		  formname.TermYears.options.selectedIndex = formname.comm_heavy.options.selectedIndex + 1;	
//		  alert("HEAVY...term_resid="+term_residual+"  i="+i);
        }
		else if (loan_subtype == "calcbutton") {
		  // Check all, as subtype has not been passed by selection
		  // of a commercial loan subtype residual...
          if (formname.comm_light.options.selectedIndex) {
          	i = formname.comm_light.options.selectedIndex;
            term_residual = formname.comm_light.options[i].value; 
		  }
          else if (formname.comm_salesrep.options.selectedIndex) {
          	i = formname.comm_salesrep.options.selectedIndex;
            term_residual = formname.comm_salesrep.options[i].value; 
		  }
          else if (formname.comm_traveller.options.selectedIndex) {
          	i = formname.comm_traveller.options.selectedIndex;
            term_residual = formname.comm_traveller.options[i].value; 
		  }
		  else if (formname.comm_heavy.options.selectedIndex) {
          	i = formname.comm_heavy.options.selectedIndex;
            term_residual = formname.comm_heavy.options[i].value; 
		  }
		  else {
          	i = formname.TermYears.options.selectedIndex;
            term_residual = formname.TermYears.options[i].value; 
		  }

//		  alert("HEAVY...term_resid="+term_residual+"  i="+i);
        }
    
		else { term_residual = 0;
		}

		// CHECK REQUIRED INPUT HAS BEEN RECEIVED...
		if (loanAmount == 0) {
			alert("You must specify a Finance Amount."); }
		else if (rate == 0) {
			alert("You must specify an Interest Rate for the loan.");
		}
		
		// EXTRACT LOAN SUBTYPE, TERM and RESIDUAL (FV) from passed info,
		// WITH RESIDUAL CONVERTED TO DOLLAR VALUE...
		term = Math.floor(term_residual);
		residual = loanAmount * (term_residual - Math.floor(term_residual)); 

//		alert("Term_resid="+term_residual+"Loan="+PV+" Term="+term+" Residual="+residual);
		
		// RESET OTHER COMM LOAN SUBTYPE OPTIONS...
		
	
	} // END COMMERCIAL LOAN PREPARATION	




	// SETUP VARIABLES FOR FORMULA...
	var r_month = (rate/12)/100;		     // Interest rate per month
	var r_week = (rate*(7/365.25))/100;  // Interest rate per week

	var n_month = term * 12;		 // Number of payment (monthly)
	var n_week = term * (365.25/7);  // Number of payments (weekly)

	var FV = residual;					// Final value amount

	// FORMULA...
	
	var top = PV - (FV* (Math.pow(1+r_month,-n_month)));
	var bot = ((1 - Math.pow(1+r_month,-n_month + a)) / r_month) + a;	
	
//	alert("bit="+bit+" PV="+PV+" a="+a+" r="+r_month+" n="+n_month+" TOP = "+top+"  BOT ="+bot);
	
	var month = top / bot;

	var top = PV - (FV* (Math.pow(1+r_week,-n_week)));
	var bot = ((1 - Math.pow(1+r_week,-n_week + a)) / r_week) + a;	
	var week = top / bot;



	   	formname.Monthly.value = commas(rounding(month));

//    	week = eval(month * (12/52));
    	formname.Weekly.value = commas(rounding(week));
    	formname.Residual.value = commas(rounding(residual));

}


