// Written by Michael Roebuck, March 15/2000 for FCC

function CalcLoanPayments(thePrinciple,theInterestRate,theYears,theMonths)
{//

// This function will calculate the Loan Payments in Annual, SemiAnnual, Quarterly and Monthly schedules

var DF=document.forms.Calculator;
var DFR=document.forms.theResults;
var Principle = eval(stripTheCommas(thePrinciple.value));
var InterestRate = eval(theInterestRate.value);





if(theYears.value!="")

		{
			var Years = theYears.value;// This will retrieve the Years value and set Years to 0 if nothing entered
		}

	else

	{	Years = 0;}



//

if(theMonths.value!="")
		{
			var Months = theMonths.value;//This will retrieve the Months value and set Months to 0 if nothing entered

		}

	else{	Months = 0;}



var AmortizationPeriod = parseInt((Years*12)) + (parseInt(Months));// This will calculate the length of time in months of the loan 

var FCCsMaximumLoanPeriod = 348;// Currently, FCC does not give loans for more than 29 years. Therefore 29years*12months = 348 payments 
if(AmortizationPeriod>FCCsMaximumLoanPeriod)
	{
	alert("FCC does not support loans for more than 29 years (348 months).");
	DF.txtYears.value="";// Clear the Year input box if there is an error
	DF.txtMonths.value="";// Clear the Month input box if there is an error
	DFR.txtAnnual.value="";// Clear the Annual input box on the "theResults" form
	DFR.txtSemiAnnual.value=""; // Clear the SemiAnnual input box on the "theResults" form
	DFR.txtQuarterly.value=""; // Clear the Quarterly input box on the "theResults" form
	DFR.txtMonthly.value=""; // Clear the Monthly input box on the "theResults" form
	}//

else
if(AmortizationPeriod==0)
	{
	alert("Please enter an Amortization period greater than 0.");
	DF.txtYears.value="";// Clear the Year input box if there is an error
	DF.txtMonths.value="";// Clear the Month input box if there is an error
	DFR.txtAnnual.value="";// Clear the Annual input box on the "theResults" form
	DFR.txtSemiAnnual.value=""; // Clear the SemiAnnual input box on the "theResults" form
	DFR.txtQuarterly.value=""; // Clear the Quarterly input box on the "theResults" form
	DFR.txtMonthly.value=""; // Clear the Monthly input box on the "theResults" form
	}
	else{
Periods = new Array(3);
Periods[0] = 1;//Annually (once per year)
Periods[1] = 2;//Semi-Annually (twice per year)
Periods[2] = 4;//Quarterly (four times per year)
Periods[3] = 12;//Montly (twelve times per year)

Increment = new Array(3);//
Increment[0] = 12;//Annually (once every 12 months)
Increment[1] = 6;//Semi-Annually(once every 6 months)
Increment[2] = 3;//Quarterly (once every 3 months)
Increment[3] = 1;//Monthly (once every month)

Answer = new Array(3);

PIRarray = new Array(3);// define an array to hold all 4 values of the Periodic Interest Rate

for(x=0; x<=3; x++)
	{
		Answer[x] = CalcPeriodicInterest(InterestRate,Periods[x],parseInt(AmortizationPeriod),Increment[x],Principle);// Periodic Interest Rate is returned for the CalPeriodicInterest function
		Answer[x] = convertToCurrency(Answer[x]);//convert the answer to a currency type
		//Answer[x] = Answer[x]*100;//CHANGE for more or less accuracy
		//Answer[x] = Math.floor(Answer[x]);//
		//Answer[x] = Answer[x]/100;//CHANGE
	}
		DisplayAnswer();
		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////
function CalcPeriodicInterest(theInterestRate,theCompoundPeriods,theAmortizationPeriod,theIncrement,thePrinciple)
{// This function is the core calculation of the Periodic Interest
var I = theInterestRate;// Interest 
var C = theCompoundPeriods;// # of Compound Periods 
var N = C;// # of Installments/Year
var Amortization = theAmortizationPeriod;// Current Amortization
var Increment = theIncrement;//
var Principle = thePrinciple;

var Answer = 0;
var PeriodicRate = 0;

PeriodicRate = (Math.pow(1+((I/100)/N),(C/N))) - 1;
//// 
Answer = Principle/((1-(1/(Math.pow((1+PeriodicRate),(Amortization/Increment)))))/PeriodicRate); 
return Answer;
}
//////////////////////////////////////////////////////////////////////////////////////////////
function alterInterest(whichWay)
{
// This function will cause the interest reading to go up or down depending on which button is pressed
// The 2 buttons that activate this function are named txtLowerInterest and txtRaiseInterest
// The parameter "whichWay" can be either "Up" or "Down"
// 

	var DF=document.forms.Calculator;
	
	NewInterest=(Number(document.forms.Calculator.txtInterestRate.value));
	NewInterest = NewInterest*10000;
	
	
	if(whichWay=="Up")// User clicks on the Raise Interest button
	{
				
				NewInterest = NewInterest + 1000;
				NewInterest = NewInterest/10000 ;
				document.forms.Calculator.txtInterestRate.value = NewInterest;
    }
	if(whichWay=="Down")// User clicks on the Lower Interest button
	{
		NewInterest = NewInterest - 1000;
		NewInterest = NewInterest/10000 ;
		DF=document.forms.Calculator.txtInterestRate.value = NewInterest;
    }


}
//////////////////////////////////////////////////////////////////////////////////////////////

function DisplayAnswer()
{
// This function will display the Annual, SemiAnnual, Quarterly, and Monthly payments.
// It will  put alert the user and place a "0" in each field if the dollar amount was "0" (if there was no dollar input,
// the fields would display "NaN.00", which would confuse the user.

	var DF2=document.forms.theResults;// Set DF2 to this value  
if(Answer[0]&&Answer[1]&&Answer[2]&&Answer[3]!="NaN.00")
	{

	
	DF2.txtAnnual.value =  putTheCommas(Answer[0]);// 
	DF2.txtSemiAnnual.value =  putTheCommas(Answer[1]);//	
	DF2.txtQuarterly.value =  putTheCommas(Answer[2]);//
	DF2.txtMonthly.value =  putTheCommas(Answer[3]);//
	}
	else
	{
		alert("Please enter a dollar amount greater than 0.");
	DF2.txtAnnual.value = "" ;// 
	DF2.txtSemiAnnual.value = "";//
	DF2.txtQuarterly.value = "";//
	DF2.txtMonthly.value = "";//

	}
}

//////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////
function checkTheNum(input)
 {	
	var oldnum;
	var newnum;
	var ch;//
	oldnum = input.value;
	newnum ="";
	for(var i=0; i<oldnum.length; i++) {		
		ch = oldnum.charAt(i);
		if(ch != ",") {			
		newnum += ch;
		}
	}		

return parseFloat(newnum);

}
/////////////////////////////////////////////////////////////////////////////////////////

function checkTheDollarValue(input)
 {	  
		checkIt(input);// Filters out blank spaces and fills the box with a 0 if necessary when user deletes
		var DFR=document.forms.theResults;
		input.value = checkTheNum(input);
	   	input.value = parseFloat(input.value);
      	if(input.value == "NaN"){ 
	
          	alert("Please do not enter values other than numbers.");     
			input.value = "";		
			focus(input);	 
		DFR.txtAnnual.value="";// Clear the Annual input box on the "theResults" form
		DFR.txtSemiAnnual.value=""; // Clear the SemiAnnual input box on the "theResults" form
		DFR.txtQuarterly.value=""; // Clear the Quarterly input box on the "theResults" form
		DFR.txtMonthly.value=""; // Clear the Monthly input box on the "theResults" form
  
			} 
       else {                
	if(parseFloat(input.value) < 0) {
          input.value = "0";
			 }
       
	
	}
	input.value = convertToCurrency(input.value);
	input.value = putTheCommas(input.value);
	
}
/////////////////////////////////////////////////////////////////////////////////////////
function putTheCommas(input)
	 {    
		var result, len;
   		input += "";
    		len = input.indexOf(".");

 	if(len == -1) {      
		len = input.length;
      		result = ""; 
  	 }

  	 else
	 {       
		result = input.substring(len, input.length);
  	  }
  	  while(len > 3) {        
		result = input.substring(len-3, len) + result;
      	  	result = "," + result;
   		len -= 3;
  	  }  

result = input.substring(0, len) + result;//Line 230
return result;
}
/////////////////////////////////////////////////////////////////////////////////////////
function stripTheCommas(input)
	 {       
		 var newnum;
		 var ch;
		 newnum ="";
	for(var i=0; i<input.length; i++) {                
		 ch = input.charAt(i);
        if(ch != ","){                        
				newnum += ch;
         	   }
       }
        return parseFloat(newnum);
}
/////////////////////////////////////////////////////////////////////////////////////////
function checkTheTime(input)
 {	  //
		checkIt(input);
		input.value = checkTheNum(input);
	   	input.value = parseFloat(input.value);
		
      	if(input.value == "NaN"){ 
		
			
          	alert("Please do not enter values other than numbers.");     
			input.value = "";		
			focus(input);	   
			
			
			}
       else {                
	if(parseFloat(input.value) < 0) {
          input.value = "";
			 }
       
	
	}
	
	
}
///////////////////////////////////////////////////////////////////////////////
function checkTheInterest(input)
 {	  //
		checkIt(input);
		input.value = checkTheNum(input);
	   	input.value = parseFloat(input.value);
		
      	if(input.value == "NaN"){ 
		
			
          	alert("Please do not enter values other than numbers.");     
			input.value = "";		
			focus(input);	   
			
			
			}
		 if(input.value <= "0"){ 
		
			
          	alert("Please enter an interest rate higher than 0%.");     
			input.value = "7.0";		
			focus(input);	   
			
			
			}

       else {                
	if(parseFloat(input.value) < 0) {
          input.value = "";
			 }
       
	
	}
	
	
}
////////////////////////////////////////////////////////////////////////////////////////////////

function checkIt(input)
{



	if(input.value==""||input.value==" ")
	{
		input.value="0";
	}
	return input;
}
////////////////////////////////////////////////////////////////////////////
function convertToCurrency(x){
// This function converts input and output values to standard currency format
// example A:  500000 becomes 500000.00 
// example B:  500000.1 becomes 500000.10

                                    numberToRound=eval(x);
   		IntermediaryNumber=Math.abs((Math.round(numberToRound*100)/100));
		IntermediaryString=""+IntermediaryNumber;
  		if (IntermediaryString.indexOf(".")==-1){IntermediaryString+=".00"}
  		firstString=IntermediaryString.substr(0,IntermediaryString.indexOf("."));
		firstNumber=firstString-0;
   		lastString=IntermediaryString.substr(IntermediaryString.indexOf("."));
		while (lastString.length<3){lastString+="0"}

		ReturnValue = firstString + lastString;    //-- Put numbers in parentheses if negative.
 		if (numberToRound<0) { ReturnValue="("+ ReturnValue+")"} 
		return ReturnValue;
		
}
// -->

