
function fail() {

        document.calc.OMP.value = "--------";

        document.calc.NMP.value = "--------";

        document.calc.MONTHSAVINGS.value = "--------";

        document.calc.BREAKEVEN.value = "--------";

        document.calc.NPM.value =  "--------";

        document.calc.PAYSMADEZERO.value = "--------";

        document.calc.RNP.value = "--------";

        document.calc.NTC.value = "--------";

        document.calc.RB.value = "--------";

        document.calc.OIPD.value = "--------";

        document.calc.NIPDZERO.value = "--------";

        document.calc.OIPTD.value = "--------";

        document.calc.NEWTOTINTcopy.value = "--------";

}


temptext = "";



   function getORC() {

        var theval = parseFloat(strip(document.calc.oldrate.value))/1200;

        return(theval);

   }



   function getOTC() {

        var theval = parseFloat(document.calc.oldterm.value)*12;

        return(theval);

   }

   function getNMP() {

        //var theval = getNEWAMT() * getNRC() / (1 - 1 / Math.pow((getNRC()+1),getNTC()));
	var compound = 2/12;
	var monTime = parseFloat(document.calc.newterm.value) * 12;
	var RATE = (parseFloat(strip(document.calc.newrate.value))*1.0)/100;
	var yrRate = RATE/2;
	var rdefine    = Math.pow((1.0 + yrRate),compound)-1.0;
	//var theval = (parseFloat(strip(document.calc.RB.value)) *rdefine * (Math.pow((1.0 + rdefine),monTime)))/  ((Math.pow((1.0 + rdefine),monTime)) - 1.0);
var theval = (parseFloat(getRB()) *rdefine * (Math.pow((1.0 + rdefine),monTime)))/  ((Math.pow((1.0 + rdefine),monTime)) - 1.0);
 
        return(theval);

   }

   function getOMP() {

       // var theval = parseFloat(strip(document.calc.oldamt.value)) * (getORC() / (1 - (1 / Math.pow((getORC()+1),getOTC()))));

//Canada 
var compound = 2/12;

//var monTime = 25 * 12;
var monTime = parseFloat(document.calc.oldterm.value) * 12;

//var RATE = (8.5*1.0)/100;
var RATE = (parseFloat(strip(document.calc.oldrate.value))*1.0)/100;

var yrRate = RATE/2;
var rdefine    = Math.pow((1.0 + yrRate),compound)-1.0;

//var theval = (200000*rdefine * (Math.pow((1.0 + rdefine),monTime)))/  ((Math.pow((1.0 + rdefine),monTime)) - 1.0);
var theval = (parseFloat(strip(document.calc.oldamt.value)) *rdefine * (Math.pow((1.0 + rdefine),monTime)))/  ((Math.pow((1.0 + rdefine),monTime)) - 1.0);

        return(theval);

   }

   function getRB() {
        //var theval = getOFVI()-getOFVA();
        var theval=calcBal(parseFloat(strip(document.calc.oldamt.value)),parseFloat(strip(document.calc.oldrate.value)),2,12,getOMP(),(12*(parseInt(thisyear)-parseFloat(document.calc.oldyear.value)))+parseInt(thismonth)-parseInt(document.calc.oldmonth.options[document.calc.oldmonth.selectedIndex].value));
        return(theval);
   }

function calcBal(mortgage,intrate,compound,freq,payment,term){
rdefine = calcRdefine(intrate,compound, freq);
return (mortgage*(Math.pow((1.0 + rdefine),(term)))) -  ((payment * ((Math.pow((1.0 + rdefine),(term))) - 1.0))/rdefine);
}

function calcRdefine(intrate,compound, freq){
 return Math.pow((1.0 + ((intrate/100)/compound)),(compound/freq))-1.0;}

   function getNPM() {

        var theval = (12*(parseInt(thisyear)-parseFloat(document.calc.oldyear.value)))+parseInt(thismonth)-parseInt(document.calc.oldmonth.options[document.calc.oldmonth.selectedIndex].value);





        return(theval);

   }

   function getRNP() {

        var theval = getOTC()-getNPM();





        return(theval);

   }

   function getOFVI() {

        var theval = parseFloat(strip(document.calc.oldamt.value)) * Math.pow((getORC() + 1),getNPM());

        return(theval);

   }

   function getOFVA() {

        var theval = getOMP() * ((Math.pow((getORC()+1),getNPM())-1) / getORC());

        return(theval);

   }




   function getNEWAMT() {

        var theval = getRB();





        return(theval);

   }

   function getNRC() {

        var theval = parseFloat(strip(document.calc.newrate.value))/1200;





        return(theval);

   }

   function getNTC() {

        var theval = parseFloat(document.calc.newterm.value)*12;





        return(theval);

   }


   function getMONTHSAVINGS() {

        var theval = getOMP()-getNMP();





        return(theval);

   }

   function getBREAKEVEN() {

        var theval = parseFloat(strip(document.calc.newfees.value))/getMONTHSAVINGS();





        return(theval);

   }

   function getOLDTOTINT() {

        var theval = getOMP()*getOTC()-parseFloat(strip(document.calc.oldamt.value));





        return(theval);

   }

   function getOIPD() {

        var theval = getOMP()*(getOTC()-getRNP())-parseFloat(strip(document.calc.oldamt.value))+getRB();





        return(theval);

   }

   function getOITBP() {

        var theval = getOLDTOTINT()-getOIPD();





        return(theval);

   }

   function getNEWTOTINT() {

        var theval = getNMP()*getNTC()-getNEWAMT();





        return(theval);

   }











///addl functions

   function getNEWFEES() {

        var theval = strip(parseFloat(document.calc.newfees.value));





        return(theval);

   }

   function getOIPTD() {

        var theval = getOLDTOTINT()-getOIPD();





        return(theval);

   }



   function getADDITIONALINTEREST() {

        var theval = getNEWTOTINT()-getOIPTD();





        return(theval);

   }



function roundfloat(anum) {

    var snum = new String(anum);

        var dotidx = snum.indexOf(".");





        var newval;

        var newsval;

        var deci;

        if (dotidx > -1) {

                deci = snum.substring(dotidx,snum.length) * 100;

        deci = Math.round(deci);

        if (deci < 100) {

               newval = parseFloat(snum.substring(0,dotidx) + "." + deci);

            }else{

               newval = parseFloat(snum.substring(0,dotidx)) + 1;

                }

       //add superfluous zeros

       newsval = new String(newval);

       dotidx = newsval.indexOf(".");





       if (dotidx > -1) {

                //alert(newsval.substring(dotidx,newsval.length).length);

          if (newsval.substring(dotidx,newsval.length).length < 3) {

                        newsval += "0";

                        return(newsval);

          }

       }

       return(newsval);

    }else{

        snum += ".00";

        return(snum);

    }

}





function format(anum) {

  var negative = false;





  if (anum < 0) {

    anum = anum*-1;





    negative = true;





  }



  var thestring = new String(anum);

  var decindex = thestring.indexOf('.');





  var intstring = "";

  var decstring = "";

  var buildstring = "";

  var numstring = "";

  if (decindex > -1) {

        intstring = thestring.substring(0,decindex);

        decstring = thestring.substring(decindex,thestring.length);

  }else{

        intstring = thestring;

  }

  var numlen = intstring.length;

  var curidx = numlen;

        if (numlen > 3) {

           while (curidx > -1) {

                        buildstring = intstring.charAt(curidx) + buildstring;

                        numstring = intstring.charAt(curidx) + numstring;

                        curidx--;

                        if (numstring.length == 3) {

                                if (curidx != -1) {

                                buildstring = "," + buildstring;

                                numstring = "";

                            }

                        }

           }

           //prepend first digit

          // buildstring = intstring.charAt(0) + buildstring;

        }else{

          buildstring = intstring;

        }

  buildstring = "$" + buildstring + decstring;



  if (negative) {

     buildstring = "-" + buildstring;

  }





  return(buildstring);

}



function strip(astring) {

 var newstring = "";

 var achar = "XXX";

 for (i=0;i<astring.length;i++) {

    achar = astring.charAt(i,1);





    if (achar != "$" && achar != "," && achar != "%") {

        newstring += achar





    }

 }

 return (newstring);

}





thedate = new Date();

thismonth = thedate.getMonth() + 1;

thisyear = thedate.getYear();
if (thisyear < 1000) thisyear += 1900;







function calculate() {



        if (validate()) {


document.calc.OMP.value = format(roundfloat(getOMP()));

document.calc.NMP.value = format(roundfloat(getNMP()));

var threemint = parseFloat(document.calc.oldrate.value) / 400 * roundfloat(getRB());

var intdif =( ((( parseFloat(document.calc.oldrate.value) - parseFloat(document.calc.newrate.value) ) * roundfloat(getRB()) )/ 1200) *  (parseFloat(document.calc.mortterm.value)*12)-(((12*(parseInt(thisyear)-parseFloat(document.calc.oldyear.value)))+parseInt(thismonth)-parseInt(document.calc.oldmonth.options[document.calc.oldmonth.selectedIndex].value)))) ;

var a=((( parseFloat(document.calc.oldrate.value) - parseFloat(document.calc.newrate.value) ) * roundfloat(getRB()) )/ 1200);
var b=(parseFloat(document.calc.mortterm.value)*12)-(((12*(parseInt(thisyear)-parseFloat(document.calc.oldyear.value)))+parseInt(thismonth)-parseInt(document.calc.oldmonth.options[document.calc.oldmonth.selectedIndex].value)));
var c=a*b;

//alert (c);
//alert(threemint);
//alert( intdif );

if (b>0){
  if ( threemint < c ) {

	document.calc.newfees.value = format(roundfloat(c));
	
	}else{

	document.calc.newfees.value = format(roundfloat( threemint));

    }
}else{
document.calc.newfees.value = format(roundfloat( 0.00 ));
}
	
var temp = format(parseFloat(strip(document.calc.OMP.value) - strip(document.calc.NMP.value)));

var anumstring = new String(temp);

var adecindex = anumstring.indexOf('.');

var monthsval = "";

  if (adecindex > -1) {

        var aintstring = anumstring.substring(0,adecindex);

        var adecstring = anumstring.substring(adecindex+1,anumstring.length);

     if (adecstring.length > 2) {

		var tenths = anumstring.charAt(adecindex+1);

		var hundredths = anumstring.charAt(adecindex+2);

		var thousandths = anumstring.charAt(adecindex+3);

		var decis = "";

          if (parseInt(thousandths) > 5) {

             decis = new String(tenths) + new String(hundredths);

          	 decis = parseInt(decis) + 1;

          	 decis = new String(decis);

          	 if (decis.length < 2) {

          	   decis = "0" + decis;

          	 }

          }else{

             decis = new String(tenths) + hundredths;

          }

        monthsval = aintstring + "." + decis;

     }else{

     	monthsval = anumstring + "0";

     }

 }else{

 	monthsval = anumstring;

 }


document.calc.MONTHSAVINGS.value = monthsval;


//breakout years

var yearamt = getBREAKEVEN();


var negative = false;


if (yearamt < 0) {

  negative = true;


  yearamt = yearamt * -1;

}



var yearp = 0;





var monthp = 0;





var y = 0;





while(y<yearamt) {

 monthp++;

 if (monthp == 12) {

        yearp++;

        monthp = 0;





 }

 y++

}

var prettytime = "";

if (yearp > 0) {

  prettytime = yearp + " yrs";

}

if (monthp > 0) {

  prettytime = prettytime + " " + monthp + " mo";

}

if (negative) {

  prettytime = "-" + prettytime;

}



document.calc.BREAKEVEN.value = prettytime;


document.calc.RB.value =  format(roundfloat(getRB()));

        }else{

document.calc.OMP.value = "--------";

document.calc.NMP.value = "--------";

document.calc.MONTHSAVINGS.value = "--------";

document.calc.BREAKEVEN.value = "--------";

//document.calc.ADDITIONALINTEREST.value = "--------";

document.calc.NPM.value =  "--------";

document.calc.PAYSMADEZERO.value = "--------";

document.calc.RNP.value = "--------";

document.calc.NTC.value = "--------";



document.calc.RB.value = "--------";

document.calc.OIPD.value = "--------";

document.calc.NIPDZERO.value = "--------";

document.calc.OIPTD.value = "--------";

document.calc.NEWTOTINTcopy.value = "--------";

        }

}



function basedoncurrent() {

        document.calc.RB.value =  format(roundfloat(getRB()));

}







function validate() {

        var returnval = true;



thevalue = parseInt(document.calc.oldyear.value);

document.calc.oldyear.value = thevalue;

if (thevalue < 1950 || thevalue > 2003) {

 alert("The year must be an integer between 1950 and 2003");

 document.calc.oldyear.focus();

 returnval = false;

}



//restrict input between 0 and 1,000,000,000

thevalue = parseFloat(strip(document.calc.oldamt.value));

if (thevalue < 0 || thevalue > 1000000000) {

 alert("The loan amount must be between 0 and 1,000,000,000");

 document.calc.oldamt.focus();

 returnval = false;

}



//restrict input to integer between 5 and 40

thevalue = parseInt(document.calc.oldterm.value);

document.calc.oldterm.value = thevalue;

if (thevalue < 5 || thevalue > 40) {

 alert("The old term must be an integer between 5 and 40");

 document.calc.oldterm.focus();

 returnval = false;

}

thevalue = parseInt(document.calc.newterm.value);

document.calc.newterm.value = thevalue;

if (thevalue < 5 || thevalue > 40) {

 alert("The new term must be an integer between 5 and 40");

 document.calc.newterm.focus();

 returnval = false;

}





//restrict input to decimal value between 2.000 and 20.000

thevalue = parseFloat(strip(document.calc.oldrate.value));

if (thevalue < 2 || thevalue > 20) {

 alert("The old rate must be a decimal value between 2.000 and 20.000");

 document.calc.oldrate.focus();

 returnval = false;

}

//restrict input to decimal value between 2.000 and 20.000

thevalue = parseFloat(document.calc.newrate.value);

if (thevalue < 2 || thevalue > 20) {

 alert("The new rate must be a decimal value between 2.000 and 20.000");

 document.calc.newrate.focus();

 returnval = false;

}



//restrict user input to between 0 and 1,000,000

thevalue = parseFloat(strip(document.calc.newfees.value));

if (thevalue < 0 || thevalue > 1000000) {

 alert("The new fees value must be between 0 and 1000000");

 document.calc.newfees.focus();

 returnval = false;

}

document.calc.newfees.value = format(thevalue);



if (returnval == false) {

   alert("Nothing Calculated!");

}

return(returnval);



}

