//*******************************************
//  This calculator was originaly based on a script by Daniel C. Peterson
// but with the permission of WebWinder Services has been modified and extended
// to provide more advanced functionality and reporting.
//  
// The original script remains the copywrite of Daniel C. Perterson while the extensions 
// are the copyright of RefinanceCalculatorMortgage.org and may not be reproduced.
//  These extension are mainly based around extnding the interface and providing calculations and graphing
// related to the principal balance by month and the cumulative interst and non-financed closing costs by month.
//
// The copyright for the original script is as follows:
//
//DO NOT REMOVE THIS COPYWRITE INFO!
//Mortgage Refinancing Calculator
//2001 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 04/16/2001
//Last Modified: 06/01/2009
//This script may not be copied, edited, distributed or reproduced
//without express written permission from
//Daniel C. Peterson of Web Winder Website Services
//For commercial use rates, contact:
//Dan Peterson:
//Web Winder Website Services
//P.O. Box 11
//Bemidji, MN  56619
//dan@webwinder.com
//http://www.webwinder.com
//Commercial User Licence #:6615-1387-64-1343
//Commercial Licence Date:2010-02-11
//*******************************************



function computeMonthlyPayment(prin, numPmts, intRate) {

var pmtAmt = 0;

if(intRate == 0) {
   pmtAmt = prin / numPmts;
} else {
   
   if (intRate >= 1.0) {
     intRate = intRate / 100.0;
   }
   intRate /= 12;

   var pow = 1;
   for (var j = 0; j < numPmts; j++)
      pow = pow * (1 + intRate);

   pmtAmt = (prin * pow * intRate) / (pow - 1);

}

return pmtAmt;

}




function computeFixedInterestCost(principal, intRate, pmtAmt) { 

   var i = eval(intRate);
   if(i >= 1) {
   i /= 100;
   }
   i /= 12;

   var prin = eval(principal);
   var intPort = 0;
   var accumInt = 0;
   var prinPort = 0;
   var pmtCount = 0;
   var testForLast = 0;


   //CYCLES THROUGH EACH PAYMENT OF GIVEN DEBT
   while(prin > 0) {

      testForLast = (prin * (1 + i));

      if(pmtAmt < testForLast) {
         intPort = prin * i;
         accumInt = eval(accumInt) + eval(intPort);
         prinPort = eval(pmtAmt) - eval(intPort);
         prin = eval(prin) - eval(prinPort);
      } else {
      //DETERMINE FINAL PAYMENT AMOUNT
      intPort = prin * i;
      accumInt = eval(accumInt) + eval(intPort);
      prinPort = prin;
      prin = 0;
      }

      pmtCount = eval(pmtCount) + eval(1);

      if(pmtCount > 1000 || accumInt > 1000000000) {
         prin = 0;
      }

   }

return accumInt;

}




function computeNPR(principal, intRate, pmtAmt) {

   var i = eval(intRate);
   if(i >= 1) {
   i /= 100;
   }
   i /= 12;

   var prin = eval(principal);
   var intPort = 0;
   var accumInt = 0;
   var prinPort = 0;
   var pmtCount = 0;
   var testForLast = 0;


   //CYCLES THROUGH EACH PAYMENT OF GIVEN DEBT
   while(prin > 0) {

      testForLast = (prin * (1 + i));

      if(pmtAmt < testForLast) {
         intPort = prin * i;
         accumInt = eval(accumInt) + eval(intPort);
         prinPort = eval(pmtAmt) - eval(intPort);
         prin = eval(prin) - eval(prinPort);
      } else {
      //DETERMINE FINAL PAYMENT AMOUNT
      intPort = prin * i;
      accumInt = eval(accumInt) + eval(intPort);
      prinPort = prin;
      prin = 0;
      }

      pmtCount = eval(pmtCount) + eval(1);

      if(pmtCount > 1000 || accumInt > 1000000) {
         prin = 0;
      }

   }

return pmtCount;

}




function fn(num, places, comma) {

var isNeg=0;

    if(num < 0) {
       num=num*-1;
       isNeg=1;
    }

    var myDecFact = 1;
    var myPlaces = 0;
    var myZeros = "";
    while(myPlaces < places) {
       myDecFact = myDecFact * 10;
       myPlaces = Number(myPlaces) + Number(1);
       myZeros = myZeros + "0";
    }
    
	onum=Math.round(num*myDecFact)/myDecFact;
		
	integer=Math.floor(onum);

	if (Math.ceil(onum) == integer) {
		decimal=myZeros;
	} else{
		decimal=Math.round((onum-integer)* myDecFact)
	}
	decimal=decimal.toString();
	if (decimal.length<places) {
        fillZeroes = places - decimal.length;
	   for (z=0;z<fillZeroes;z++) {
        decimal="0"+decimal;
        }
     }

   if(places > 0) {
      decimal = "." + decimal;
   }

   if(comma == 1) {
	integer=integer.toString();
	var tmpnum="";
	var tmpinteger="";
	var y=0;

	for (x=integer.length;x>0;x--) {
		tmpnum=tmpnum+integer.charAt(x-1);
		y=y+1;
		if (y==3 & x>1) {
			tmpnum=tmpnum+",";
			y=0;
		}
	}

	for (x=tmpnum.length;x>0;x--) {
		tmpinteger=tmpinteger+tmpnum.charAt(x-1);
	}


	finNum=tmpinteger+""+decimal;
   } else {
      finNum=integer+""+decimal;
   }

    if(isNeg == 1) {
       finNum = "-" + finNum;
    }

	return finNum;
}




function sn(num) {

   num=num.toString();


   var len = num.length;
   var rnum = "";
   var test = "";
   var j = 0;

   var b = num.substring(0,1);
   if(b == "-") {
      rnum = "-";
   }

   for(i = 0; i <= len; i++) {

      b = num.substring(i,i+1);

      if(b == "0" || b == "1" || b == "2" || b == "3" || b == "4" || b == "5" || b == "6" || b == "7" || b == "8" || b == "9" || b == ".") {
         rnum = rnum + "" + b;

      }

   }

   if(rnum == "" || rnum == "-") {
      rnum = 0;
   }

   rnum = Number(rnum);

   return rnum;

}

function computeForm(form) {

   var alert_txt = "";
   var sum_cell = document.getElementById("summary");

   var pmt1 = sn(document.calc.payment.value);
   var prin = sn(document.calc.principal.value);
   var i1 = sn(document.calc.intRate.value);

   if(document.calc.principal.value == "") {
      alert("Please enter the your mortgage's current principal balance.");
      document.calc.principal.focus();
   } else
   if(document.calc.payment.value == "") {
      alert("Please enter the amount of your mortgage payment.");
      document.calc.payment.focus();
   } else 
   if(document.calc.intRate.value == "") {
      alert("Please enter your mortgage's current annual interest rate.");
      document.calc.intRate.focus();
   } else 
   if((prin * (i1/100/12)) > pmt1) {
      alert_txt += "The payment amount you entered ($" + fn(pmt1,2,1) + ") is too small to pay off";
      alert_txt += " your mortgage ($" + fn(prin,2,1) + ")  within an accepted time frame. Please";
      alert_txt += " increase the payment amount until you no longer receive this message.";
      alert(alert_txt);
      document.calc.payment.focus();
   } else
   if(document.calc.intRate2.value == "") {
      alert("Please enter the annual interest rate you'll be refinancing at.");
      document.calc.intRate2.focus();
   } else
   if(document.calc.nper2.value == "") {
      alert("Please enter the number of years you are refinancing for.");
      document.calc.nper2.focus();
   } else
   if(document.calc.closingCost.value == "") {
      alert("Please enter the closing cost percentage points.");
      document.calc.closingCost.focus();
   } else
   if(sn(document.calc.intRate2.value) > sn(document.calc.intRate.value)) {
      alert_txt = "You've entered a refinancing rate that is higher than your present rate.  ";
      alert_txt += "The refinancing rate must be lower than your present rate in ";
      alert_txt += "order for this calculator to work.";
      alert(alert_txt);
   } else {





      var prin1 = prin;

      var closeCostAmt = 0;
      var VcloseCost = sn(document.calc.closingCost.value);
      if(document.calc.ptsDol.selectedIndex == 0) {
         var closeCostPerc = Number(VcloseCost) / 100;
         closeCostAmt = closeCostPerc * prin;
      } else {
         closeCostAmt = VcloseCost;
      }

      var i2 = sn(document.calc.intRate2.value);

      var v_orgInt = computeFixedInterestCost(prin, i1, pmt1);
      document.calc.origInt.value = "$" + fn(v_orgInt,2,1);

      var prin2 = 0;

      if(document.calc.yesNo.selectedIndex == 0) {
         prin2 = prin;
      } else {
         prin2 = Number(prin) + Number(closeCostAmt);
      }

      var v_years_2 = sn(document.calc.nper2.value);
      var Vnper2 = v_years_2 * 12;

      var fpayment2 = computeMonthlyPayment(prin2, Vnper2, i2);
      fpayment2 = Math.round(fpayment2*100)/100;
      document.calc.payment2.value = "$" + fn(fpayment2,2,1);


      var fmoSave = Number(pmt1) - Number(fpayment2);

      document.calc.moSave.value = "$" + fn(fmoSave,2,1);
		
      var ftotInt2 = computeFixedInterestCost(prin2, i2, fpayment2);
      document.calc.totInt2.value = "$" + fn(ftotInt2,2,1);

      var fintSave = Number(v_orgInt) - Number(ftotInt2);

      if(fintSave <= 0) {
         document.calc.intSave.value = "$0.00";
      } else {
         document.calc.intSave.value = "$" + fn(fintSave,2,1);
      }

      var prin3 = prin2;
      var prin4 = prin;

      var intPort3 = 0;
      var intPort4 = 0;

      var prinPort3 = 0;
      var prinPort4 = 0;

      var accumInt3 = 0;
      var accumInt4 = 0;

      var accumPrin3 = 0;
      var accumPrin4 = 0;

      var amortIntSave = 0;

      var count3 = 0;

      while(amortIntSave < closeCostAmt) {

         intPort3 = prin3 * (i2/100/12);
         intPort4 = prin4 * (i1/100/12);

         prinPort3 = Number(fpayment2) - Number(intPort3);
         prinPort4 = Number(pmt1) - Number(intPort4);

         prin3 = Number(prin3) - Number(prinPort3);
         prin4 = Number(prin4) - Number(prinPort4);

         accumPrin3 = Number(accumPrin3) + Number(prinPort3);
         accumPrin4 = Number(accumPrin4) + Number(prinPort4);

         accumInt3 = Number(accumInt3) + Number(intPort3);
         accumInt4 = Number(accumInt4) + Number(intPort4);

         amortIntSave = Number(accumInt4) - Number(accumInt3);

         count3 = Number(count3) + Number(1);

         if(count3 > 600) {break; } else {continue; }

      }


      document.calc.closeMo.value = count3;

      var fnetSave = Number(fintSave) - Number(closeCostAmt);
   
      var pmtUpDown = "";
      if(fpayment2 > pmt1) {
         pmtUpDown = "increase by $" + fn(Number(fpayment2) - Number(pmt1),2,1) + "";
      } else {
         pmtUpDown = "decrease by $" + fn(Number(pmt1) - Number(fpayment2),2,1) + "";
      }

      var intSaveYesNo = "";
      if(v_orgInt < ftotInt2) {
         intSaveYesNo = "pay an additional $" + fn(Number(ftotInt2) - Number(v_orgInt),2,1) + " in";
         intSaveYesNo += " interest charges over the life of the mortgage.";
      } else {
         intSaveYesNo = "save $" + fn(Number(v_orgInt) - Number(ftotInt2),2,1) + " in ";
         intSaveYesNo += "interest charges over the life of the mortgage. However, in order ";
         intSaveYesNo += "for this refinancing to yield any savings at all you will need to ";
         intSaveYesNo += "stay in your current home for at least " + count3 + " months.  ";
         intSaveYesNo += "That's how long it will take for the monthly interest savings to ";
         intSaveYesNo += "offset the closing costs attributable to refinancing.";
      }

      if(fnetSave <= 0) {
         document.calc.netSave.value = "$0.00";
      } else {
         document.calc.netSave.value = "$" +fn(fnetSave,2,1);
      }

      var v_summary = "If you refinance your current " + fn(i1,2,0) + "% ";
      v_summary += "mortgage to a " + fn(i2,2,0) + "% mortgage, your ";
      v_summary += "monthly payment will " + pmtUpDown + " and you will " + intSaveYesNo + "";

      sum_cell.innerHTML = "<font face='arial'><small>" + v_summary + "</small></font>";

   }
	graphPrincipleSeries(prin, i1, pmt1, prin2, i2, fpayment2);	
      if(document.calc.yesNo.selectedIndex == 0) {
		graphCumulativeSeries(prin, i1, pmt1, prin2, i2, fpayment2, closeCostAmt);	
      } else {
		graphCumulativeSeries(prin, i1, pmt1, prin2, i2, fpayment2, 0);	
      }
      clicky.log('#calculate/home');

}


function clear_results(form) {

   var sum_cell = document.getElementById("summary");
   sum_cell.innerHTML = "Summary:";

   document.calc.origInt.value = "";
   document.calc.payment2.value = "";
   document.calc.moSave.value = "";
   document.calc.totInt2.value = "";
   document.calc.intSave.value = "";
   document.calc.closeMo.value = "";
   document.calc.netSave.value = "";
   
   clearSeries();

}

function reset_calc(form) {

   if(confirm("Are you sure you want to reset the calculator to the default entries?")) {

      clear_results(document.calc);
      document.calc.reset();


   }

}

function clearSeries(){
    var d1 = [];
        d1.push([0,0]);
    
    var d2 = [];
        d2.push([0,0]);

    
    $.plot($("#placeholder"), [
        { label: "Without Refinancing",  data: d1},
        { label: "With Refinancing",  data: d2},
    ], {
        series: {
            lines: { show: true },
            points: { show: false }
        },
        grid: {
            backgroundColor: { colors: ["#fff", "#eee"] }
        }
    });
    $.plot($("#placeholder2"), [
        { label: "Without Refinancing",  data: d1},
        { label: "With Refinancing",  data: d2},
    ], {
        series: {
            lines: { show: true },
            points: { show: false }
        },
        grid: {
            backgroundColor: { colors: ["#fff", "#eee"] }
        }
    });

}

function graphPrincipleSeries(principle1, intRate1, pmtAmt1, principle2, intRate2, pmtAmt2) {
    var d1 = computePrincipleSeries(principle1, intRate1, pmtAmt1)
    
    var d2 = computePrincipleSeries(principle2, intRate2, pmtAmt2)

    
    $.plot($("#placeholder"), [
        { label: "Without Refinancing",  data: d1},
        { label: "With Refinancing",  data: d2}
    ], {
        series: {
            lines: { show: true },
            points: { show: false }
        },      
        zoom: {
            interactive: true
        },
        pan: {
            interactive: true
        },


        grid: {
            backgroundColor: { colors: ["#fff", "#eee"] }
        }
    });


}

function computePrincipleSeries(principal, intRate, pmtAmt) { 

   var i = eval(intRate);
   if(i >= 1) {
   i /= 100;
   }
   i /= 12;

   var prin = eval(principal);
   var intPort = 0;
   var accumInt = 0;
   var prinPort = 0;
   var pmtCount = 0;
   var testForLast = 0;

 	var d1 = [];
 	var k = 0;
    d1.push([k, prin]);
    

   //CYCLES THROUGH EACH PAYMENT OF GIVEN DEBT
   while(prin > 0) {

      testForLast = (prin * (1 + i));

      if(pmtAmt < testForLast) {
         intPort = prin * i;
         accumInt = eval(accumInt) + eval(intPort);
         prinPort = eval(pmtAmt) - eval(intPort);
         prin = eval(prin) - eval(prinPort);
         k = k + 1;
	    d1.push([k, prin]);
      } else {
      //DETERMINE FINAL PAYMENT AMOUNT
      intPort = prin * i;
      accumInt = eval(accumInt) + eval(intPort);
      prinPort = prin;
      prin = 0;
         k = k + 1;
	    d1.push([k, prin]);
      }

      pmtCount = eval(pmtCount) + eval(1);

      if(pmtCount > 1000 || accumInt > 1000000000) {
         prin = 0;
      }

   }

return d1;

}

function graphCumulativeSeries(principle1, intRate1, pmtAmt1, principle2, intRate2, pmtAmt2, nfClose) {
    var d1 = computeCumInterestandNonFinClosingSeries(principle1, intRate1, pmtAmt1, 0)
    
    var d2 = computeCumInterestandNonFinClosingSeries(principle2, intRate2, pmtAmt2, nfClose)

    
    $.plot($("#placeholder2"), [
        { label: "Without Refinancing",  data: d1},
        { label: "With Refinancing",  data: d2}
    ], {
        series: {
            lines: { show: true },
            points: { show: false }
        },      
        zoom: {
            interactive: true
        },
        pan: {
            interactive: true
        },


        grid: {
            backgroundColor: { colors: ["#fff", "#eee"] }
        }
    });


}


function computeCumInterestandNonFinClosingSeries(principal, intRate, pmtAmt, nonFinClosing) { 

   var i = eval(intRate);
   if(i >= 1) {
   i /= 100;
   }
   i /= 12;

   var prin = eval(principal);
   var nfClose = eval(nonFinClosing);
   var intPort = 0;
   var accumInt = 0;
   var prinPort = 0;
   var pmtCount = 0;
   var testForLast = 0;

 	var d1 = [];
 	var k = 0;
    d1.push([k, nfClose]);
    

   //CYCLES THROUGH EACH PAYMENT OF GIVEN DEBT
   while(prin > 0) {

      testForLast = (prin * (1 + i));

      if(pmtAmt < testForLast) {
         intPort = prin * i;
         accumInt = eval(accumInt) + eval(intPort);
         prinPort = eval(pmtAmt) - eval(intPort);
         prin = eval(prin) - eval(prinPort);
         k = k + 1;
	    d1.push([k, accumInt + nfClose]);
      } else {
      //DETERMINE FINAL PAYMENT AMOUNT
      intPort = prin * i;
      accumInt = eval(accumInt) + eval(intPort);
      prinPort = prin;
      prin = 0;
         k = k + 1;
	    d1.push([k, accumInt + nfClose]);
      }

      pmtCount = eval(pmtCount) + eval(1);

      if(pmtCount > 1000 || accumInt > 1000000000) {
         prin = 0;
      }

   }

return d1;

}

