
function interestFreePeriod(intOnlyPer){return intOnlyPer.selectedIndex}

/*
*/

function period(per){
	if (per.selectedIndex == 0) {return 12;}
	if (per.selectedIndex == 1) {return 26;}
	if (per.selectedIndex == 2) {return 52;}
}

/*
*/

function validate(field){

  if (field.value == "" || field.value=="0")
  {
    alert("Two of the three top boxes and the interest rate must be filled in. \n The computer will then calculate the other (empty) box. \n You have not filled in enough of the boxes.");
    field.focus();
    return (false);
  }

  var validInput = "0123456789.";
  var checkingData = field.value;
  var allValid = true;
  for (i = 0;  i < checkingData.length;  i++)
  {
    ch = checkingData.charAt(i);
    for (j = 0;  j < validInput.length;  j++)
      if (ch == validInput.charAt(j))
        break;
        
    if (j == validInput.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please Note: do not place '$' or '%' or other signs in the input boxes. \n The only entries which will work are numbers (including decimal points). \n This box has come up because one of the input boxes has signs other than numbers in it");
    field.focus();
    return (false);
  }
  return (true);
}

/*
*/

function rounding(x){return Math.round((100*x))/100}



function calcRebate(form)
{
sum= form.txtLoanAmount.value

if (sum < 150000) {rebate=0}
	else{
    	if (sum < 299999){rebate=250}
      		else{
        	if (sum < 499999){rebate=500}
          		else{
            		if (sum < 749999){rebate=750}
              			else{
                		if (sum < 1000000){rebate=1250}
                  			else{
                     			if (sum < 1250000){rebate=1750}
                       				else{rebate=2000}                                                                  
                              		}  
                          	}
                      	}
		}
	}
  
form.txtPeachRebate.value= "$" + rounding(rebate)
}



function calcPeachSavings(form)
{
 i = parseInt(form.txtInterestRate.value)
 n = parseInt(form.txtLoanYears.value)
 m = period(form.cboPaymentPeriod)
rebate=parseInt(form.txtPeachRebate.value)

	irate= i / (m * 100)

	savings= rebate * Math.pow( (1 + irate), (n * m ) )

	form.txtPeachSavings.value= "$" + Math.round(savings)
}


/*
*	calcTotal find the total amount paid for the loan over the loan calculation period (duration)
*
*/

function calcTotal(form)
{ 

ioRep=parseInt(form.txtLoanRepayment.value);
amount = form.txtLoanAmount.value
interestRate = form.txtInterestRate.value
ioPeriod = interestFreePeriod(form.cboInterestOnlyPeriod)
frequency = period(form.cboPaymentPeriod)
irate = interestRate/ (frequency * 100)
term = form.txtLoanYears.value
rep = (irate  * amount) / (1 - Math.pow((1+irate) , -( (term-ioPeriod) * frequency) ) ) 

if(parseInt(form.duration.value)==0) {form.duration.value = form.txtLoanYears.value}

duration = parseInt(form.duration.value)

x=( rep * ( duration - ioPeriod) + ioRep * ioPeriod ) * frequency

var totalInt = 0
var new_totalInt = 0
var new_principal = 0
var reduction = 0
var interest = 0
var principal = amount

for( k=0 ; k<((duration-ioPeriod)*frequency) ; k++ )
{
	
	interest = ( irate * principal)	

	reduction = (rep - interest)

	new_principal = principal - reduction
	new_totalInt = totalInt + interest

	principal = new_principal
	totalInt = new_totalInt
}

totalInt += (ioRep * ioPeriod * frequency)


form.txtTotalAmount.value= "$" + Math.round(x)
form.txtTotalInterest.value= "$" + Math.round(totalInt)
form.txtTotalPrincipal.value= "$" + Math.round( x-totalInt )

}

/*


*/

function calcLoanAmount(form){
  
if (validate(form.txtLoanYears))             {
loanTermYear=form.txtLoanYears.value 

  if (validate(form.txtLoanRepayment1))         {
  loanRepayment1=form.txtLoanRepayment1.value

    if (validate(form.txtInterestRate))         {
    interestRate=form.txtInterestRate.value; 
  
    irate=interestRate/ (period(form.cboPaymentPeriod) * 100)

    x=  (loanRepayment1 / irate) * (1 - Math.pow( (1+irate), -(loanTermYear * period(form.cboPaymentPeriod)))) 

    form.txtLoanAmount.value=Math.round((x/10))*10;

    interestOnly= form.txtLoanAmount.value * irate
     
    if (interestFreePeriod(form.cboInterestOnlyPeriod)>0){
	form.txtLoanRepayment.value="$"+ Math.round(interestOnly) 
    }
    else form.txtLoanRepayment.value="0"

    calcRebate(form)
    calcPeachSavings(form)
    calcTotal(form)
                                                    
   }
  } 
 }
}

/*


*/

function calcLoanRepayment(form){

  if (validate(form.txtLoanAmount))               {
  loanAmount=form.txtLoanAmount.value

    if (validate(form.txtLoanYears))                {
    loanTermYear=form.txtLoanYears.value

      if (validate(form.txtInterestRate) )            {
      interestRate=form.txtInterestRate.value; 

    irate=interestRate/ (period(form.cboPaymentPeriod) * 100)

    interestOnly= loanAmount * irate
       
    if (interestFreePeriod(form.cboInterestOnlyPeriod)>0){
	
	form.txtLoanRepayment.value="$"+ Math.round(interestOnly) 
    }
    else {form.txtLoanRepayment.value="0"}


	loanAmount=loanAmount - interestOnly * interestFreePeriod(form.cboInterestOnlyPeriod)


        var x = (irate  * loanAmount) / (1 - Math.pow((1+irate) , -((loanTermYear-interestFreePeriod(form.cboInterestOnlyPeriod)) * period(form.cboPaymentPeriod)))            ) 

        form.txtLoanRepayment1.value=rounding(x);
        calcRebate(form)
	calcPeachSavings(form)
	calcTotal(form)
                        
                                                       }
                                                     }
                                                   }
}

/*


*/

function calcLoanTerm(form){

  if (validate(form.txtLoanAmount))              {
  loanAmount=form.txtLoanAmount.value

    if (validate(form.txtLoanRepayment1))           {
    loanRepayment=form.txtLoanRepayment1.value

      if (validate(form.txtInterestRate) )           {
      interestRate=form.txtInterestRate.value; 

        irate=interestRate/ (period(form.cboPaymentPeriod) * 100)
        
    interestOnly= loanAmount * irate
       
    if (interestFreePeriod(form.cboInterestOnlyPeriod)>0){

	form.txtLoanRepayment.value="$"+ Math.round(interestOnly) 
    }
    else form.txtLoanRepayment.value="0"

        if(irate * loanAmount <= loanRepayment) {
        
        var x = -   (  Math.log( 1 -   loanAmount * irate / loanRepayment)    /       Math.log(1 + irate)     )/period(form.cboPaymentPeriod)
        form.txtLoanYears.value=Math.round(x)
	
	calcRebate(form)
	calcPeachSavings(form)
        calcTotal(form)
                                                       }

else{   alert("The Loan Repayments are too small. At this Rate you will never pay off your Home Loan")
	form.txtLoanRepayment1.focus()
}

                                                     }
                                                   }
                                                 }
}

/*


*/


function compdiff(form1,form2,dform){

//calcLoanRepayment(form1)
//calcLoanRepayment(form2)


	compd(form1.txtLoanAmount,form2.txtLoanAmount,dform.txtLoanAmount);
	compd(form1.txtLoanYears,form2.txtLoanYears,dform.txtLoanYears);
//	compd(form1.cboInterestOnlyPeriod,form2.cboInterestOnlyPeriod,dform.cboInterestOnlyPeriod);

	dform.cboInterestOnlyPeriod.value=Math.abs(form1.cboInterestOnlyPeriod.value-form2.cboInterestOnlyPeriod.value)
	
	v1=form1.txtLoanRepayment1.value*period(form1.cboPaymentPeriod)/period(dform.cboPaymentPeriod);
	v2=form2.txtLoanRepayment1.value*period(form2.cboPaymentPeriod)/period(dform.cboPaymentPeriod);
	dform.txtLoanRepayment1.value=(rounding(v1-v2))
	
	compd(form1.duration,form2.duration,dform.duration);
	compd(form1.txtInterestRate,form2.txtInterestRate,dform.txtInterestRate);
	compd(form1.txtMonthlyFees,form2.txtMonthlyFees,dform.txtMonthlyFees);
	compd(form1.txtLoanRepayment,form2.txtLoanRepayment,dform.txtLoanRepayment)
	compd(form1.txtPeachRebate,form2.txtPeachRebate,dform.txtPeachRebate)
	compd(form1.txtTotalAmount,form2.txtTotalAmount,dform.txtTotalAmount)
	compd(form1.txtTotalInterest,form2.txtTotalInterest,dform.txtTotalInterest)
	compd(form1.txtTotalPrincipal,form2.txtTotalPrincipal,dform.txtTotalPrincipal)
	compd(form1.txtPeachSavings,form2.txtPeachSavings,dform.txtPeachSavings)

}

/*


*/

function compd( a,b,c )
{

	if (val(a)&&val(b)){
		d=parseInt(a.value);
		e=parseInt(b.value);
		c.value= Math.abs(d-e);
	}
	else 	c.value= "";
}	


/*


*/

function val(a){

	if ((a.value==null)||(a.value==""))
		return false;
	return true;
}

/*


*/

function parseInt(input) {
	var output = "";
	var digits = "0123456789.";
	for (var i = 0; i < input.length; i++) {
		if (digits.indexOf(input.charAt(i)) == -1);
		else output = output + input.charAt(i);
	}
	return output;
}

/*


*/

function clrForms(form,dform){

clr(form);
clr(dform);

}

/*


*/

function clr(form){

form.txtLoanAmount.value="";
form.txtLoanYears.value="";
form.cboInterestOnlyPeriod.value=0;
form.txtLoanRepayment1.value="";
form.txtInterestRate.value="";
form.cboPaymentPeriod.value=1;
form.txtMonthlyFees.value="0";
form.txtLoanRepayment.value="";
form.txtPeachRebate.value="";
form.txtTotalAmount.value="";
form.txtTotalInterest.value="";
form.txtTotalPrincipal.value="";
form.txtPeachSavings.value="";
}


