// Simple Mortgage Calculator V2.0
// Copyright 2001-2006 SidePix Inc and the Canadian Equity Group Inc (www.canequity.com).
// Authour: Daryl Maksysmec
// This JavaScript code may be used freely as long this this header remains intact.
function Calculate(){
  var form=document.calculator;
  c=2;
  m=form.mort.value.replace(/[^0-9.]/g, '');
  r=form.rate.value.replace(/[^0-9.]/g, '');
  a=form.amor.value.replace(/[^0-9.]/g, '');
  f=sI(form.freq.selectedIndex);
  if((m==null||m.length==0)||(m<10000||m>10000000)){
    alert('Amount to Mortgage:\nPlease enter a number between 10000 and 10000000.');return false;}
  if((r==null||r.length==0)||(r<1||r>25)){
    alert('Interest Rate:\nPlease enter a number between 1.0 and 25.0.');return false;}
  if((a==null||a.length==0)||(a<1||a>30)){
    alert('Amortization Period:\nPlease enter a number between 1 and 30.');return false;}
  var p=cP(c,m,r,a,f);
  form.pay.value=rD(p);
  form.y01.value=rD(cB(c,m,r,f,p,(12/(12/f))));
  form.y02.value=rD(cB(c,m,r,f,p,(24/(12/f))));
  form.y03.value=rD(cB(c,m,r,f,p,(36/(12/f))));
  form.y05.value=rD(cB(c,m,r,f,p,(60/(12/f))));
  form.y10.value=rD(cB(c,m,r,f,p,(120/(12/f))));
  return true;
}
function sI(n){
  if(n==0)return 12;
  if(n==1)return 24;
  if(n==2)return 26;
  if(n==3)return 52;
}
function rD(n){
  if(n > 0){
  if (n.toFixed)
  return '$'+n.toFixed(2);
  return Math.round(n*100)/100;
}
else return 'N/A';
}
function cB(c,m,r,f,p,t){
  R=Math.pow((1.0+((r/100)/c)),(c/f))-1.0;;
  return (m*(Math.pow((1.0+R),(t))))-((p*((Math.pow((1.0+R),(t)))-1.0))/R);
}
function cP(c,m,r,a,f){
  a=a*12;
  var R=Math.pow((1.0+((r/100)/c)),(c/12))-1.0;
  var P=(m*R*(Math.pow((1.0+R),a)))/((Math.pow((1.0+R),a))-1.0);
  if(f==12) return P;
  if(f==52) return P/4.0;
  return P/2.0;
}
