


//**************************************************************************************
// Checks if the given string is a number with an exception of a "."
//**************************************************************************************
function isNumber(input) {
   for (var i=0;i<input.length;i++) {
       var oneChar = input.substring(i, i+1)
       if (oneChar < "0" || oneChar > "9") {
          if (oneChar != "." ) { 
             return(false)
          }
       }
   }
   return(true)
}

//**************************************************************************************
// Validates that all required fields are filled out
//**************************************************************************************

//clears the form
function funReset()
{
		document.calculator.amount.value="";
		document.calculator.rate.value="";
		document.calculator.years.value="";
		document.calculator.tax.value="";
		document.calculator.insurance.value="";
		document.calculator.monthlyTax.value="";
		document.calculator.monthlyInsurance.value="";
		document.calculator.monthlyTotal.value="";
		document.calculator.monthlyPayment.value="";
		
		document.calculator.amount.focus();

}


function validate(path)
{
		
		
		if((document.calculator.amount.value=="") || (document.calculator.years.value=="") ||(document.calculator.rate.value==""))
		{
			alert("Please make sure that all required fields are filled out!");
			document.calculator.amount.focus();
			
			
		}
		else if(!isNumber(document.calculator.amount.value))
		{
			alert("Please make sure that the LOAN AMOUNT field is a valid number!");
			document.calculator.amount.value="";
			document.calculator.amount.focus();
		}
		else if(!isNumber(document.calculator.rate.value))
		{
			alert("Please make sure that the INTEREST RATE field is a valid number!");
			document.calculator.rate.value="";
			document.calculator.rate.focus();

		}
		else if(!isNumber(document.calculator.years.value))
		{
			alert("Please make sure that the Years field is a valid number!");
			document.calculator.years.value="";
			document.calculator.years.focus();

		}
		else
		{	
			if(path=="cmdCalculate")
			{
				calcPayment();
			}
			else if (path=="cmdAmortize")
			{
				amortizeTable();
			}
		}
		
}

//**************************************************************************************
//  Formats the number
//**************************************************************************************
function format(number)
{
 
 return(Math.floor(number*Math.pow(10,2))/Math.pow(10,2));

}

//**************************************************************************************
// Calculates monthly payments
//**************************************************************************************
 function calcPayment()
{
	
	
	var term, interestRate, payment, amount,payment2, insurance, tax;
	interestRate=(document.calculator.rate.value/100)/12;
	term=(document.calculator.years.value)*12;
	amount=document.calculator.amount.value;
	payment=(amount*(Math.pow(1+interestRate,term))* interestRate)/((Math.pow(1+interestRate,term))-1);
	payment=format(payment);   
	insurance=format(document.calculator.insurance.value/12);
	tax=format(document.calculator.tax.value/12);

	document.calculator.monthlyPayment.value=calcRound(payment);
	document.calculator.monthlyInsurance.value=calcRound(insurance);
	document.calculator.monthlyTax.value=calcRound(tax);
	document.calculator.monthlyTotal.value=calcRound(payment+insurance+tax);
	return(payment);

}

//**************************************************************************************
// Round off to cents
//**************************************************************************************

function calcRound(num) {
   result="$"+Math.floor(num)+"." 
   n = result.length
   if (num>1000 && num<999999) {  
     result="$"+result.substring(1,n-4)+","+result.substring(n-4,n)
   }
   if (num>1000000) {  
     result = "$"+result.substring(1,n-7)+","+result.substring(n-7,n-4)+","+result.substring(n-4,n)
   }
   var cents=100*(num-Math.floor(num))+0.5
   result += Math.floor(cents/10)
   result += Math.floor(cents%10)
   return(result)
}

//**************************************************************************************
// Displays a table of payments
//**************************************************************************************
function amortizeTable()
{
      text = ("<HEAD><TITLE>Amortization Table</TITLE></HEAD>");
      text = (text +"<BODY><BR><BR>");
      text = (text +"<CENTER><H2><FONT COLOR=red>Amortization Table</FONT></H2>");
      text = (text +"<UL><FONT SIZE=-1>The following table is based on the information entered in the calculator form.</FONT></UL>");
      text = (text +"<UL><FONT SIZE=+1 COLOR=red>Mortgage Amount: </FONT>" +document.calculator.amount.value);
      text = (text +"<BR><FONT SIZE=+1 COLOR=red>  Interest Rate: </FONT>" + document.calculator.rate.value + " %");
      text = (text +"<BR><FONT SIZE=+1 COLOR=red>Mortgage Length: </FONT>" +document.calculator.years.value + " Years </UL>");
      text = (text +"<BR><CENTER><table border='1' width='100%'>");
      text = (text +"<TR><TD ALIGN=CENTER BGCOLOR=red><FONT COLOR=WHITE><B>Year</B></FONT></TD><TD ALIGN=RIGHT BGCOLOR=red><FONT COLOR=WHITE><B>Interest&nbsp;</B></FONT></TD><TD ALIGN=RIGHT BGCOLOR=red><FONT COLOR=WHITE><B>Principal&nbsp;</B></FONT></TD><TD ALIGN=RIGHT BGCOLOR=red><FONT COLOR=WHITE><B>Balance&nbsp;</B></FONT></TD></TR>\n");
      amortize();
      text = (text +"</TABLE></CENTER>");
      msgWindow=window.open("","displayWindow","toolbar=no,width=500,height=300,directories=no,status=no,scrollbars=yes,resize=no,menubar=no")
      msgWindow.document.write(text)
      msgWindow.document.close()
   
}

function amortize()
{
	 var currInt = 0
     var currPrin = 0
     
     prevBalance = document.calculator.amount.value
     InterestRate = (document.calculator.rate.value /100) / 12
	 
     MonthlyPayment = calcPayment();
     currStart = "2003";
     for(i=1;i<=30;i++)
	 {
		for(j=1;j<=12;j++) 
		{
			periodInt = prevBalance * InterestRate
			periodPrin = MonthlyPayment - periodInt
			currBal = prevBalance - periodPrin
			currInt += periodInt
			currPrin += periodPrin
			prevBalance = currBal
		}
		if( currBal <= 0 )
		{ 
         currBal = 0
		}
		text = (text +"<TR><TD ALIGN=CENTER>"+ currStart +"</TD><TD ALIGN=RIGHT>"+ calcRound(currInt) +"&nbsp;</TD><TD ALIGN=RIGHT>"+ calcRound(currPrin) +"&nbsp;</TD><TD ALIGN=RIGHT>"+ calcRound(currBal)+"&nbsp;</TD></TR>");
		currInt = 0
		currPrin = 0
		currStart = parseInt(currStart)
		currStart += 1
		if(currBal<=0) 
		{
         return(true)
		}       
	 }
   return (true)
}



