browser = navigator.userAgent;

if (browser.indexOf("Mozilla/2") > -1) {

        if(confirm("This page requires a newer version of Netscape to view.  Do you want to upgrade now?")) {

                        window.location.href="http://www.netscape.com/download/index.html";

                }else{

                        history.go(-1);

                        exit;

                }

}

//Test browser for old 3.0 engine

browser = navigator.userAgent;

slowscriptmsg = "";

if (browser.indexOf("MSIE") > -1) {

        if (browser.indexOf("Windows 3.1") > -1) {

                if (browser.indexOf("MSIE 4.") > -1) {

                        slowscriptmsg = "<HR>Workaround for Win3.1 Explorer 4.0 users: If you encounter an alert indicating that this script is running too slowly, click \"No\" to the question, \"Do you want to abort this script?\"  Otherwise, results may be inaccurate.<HR>";

                }

        }



     var version;

     var requiredVersion = 2;

     if (typeof(ScriptEngineMajorVersion) + "" == "undefined") version = 1;

     else version = ScriptEngineMajorVersion();

    

     if ((version < requiredVersion) &&

          confirm("This page requires a newer version of JScript to view.  Do you want to upgrade now?"))

     {

        location.href = "http://www.microsoft.com/msdownload/scripting.htm";

     }

}



function fail() {

        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 = "--------";

}







//temp storage for unchangeable text

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 getOMP() {

        var theval = parseFloat(strip(document.calc.oldamt.value)) * (getORC() / (1 - (1 / Math.pow((getORC()+1),getOTC()))));

        return(theval);

   }



   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 getRB() {

        var theval = getOFVI()-getOFVA();



        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 getNMP() {

        var theval = getNEWAMT() * getNRC() / (1 - 1 / Math.pow((getNRC()+1),getNTC()));

        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()) {

        //set each field like excel

document.calc.OMP.value = format(roundfloat(getOMP()));





document.calc.NMP.value = format(roundfloat(getNMP()));



//correct odd rounding from carried decimals

//document.calc.MONTHSAVINGS.value = format(roundfloat(getMONTHSAVINGS()));









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;

}



//prettytime = prettytime + yearamt;



//document.calc.BREAKEVEN.value = Math.round(getBREAKEVEN()) + " months";

document.calc.BREAKEVEN.value = prettytime;



//document.calc.ADDITIONALINTEREST.value = format(roundfloat(getADDITIONALINTEREST()));

document.calc.NPM.value =  getNPM();

document.calc.PAYSMADEZERO.value = 0;

document.calc.RNP.value =  getRNP();

document.calc.NTC.value =  getNTC();



// suggest a loan amount

document.calc.RB.value =  format(roundfloat(getRB()));

document.calc.OIPD.value =  format(roundfloat(getOIPD()));

document.calc.NIPDZERO.value = format("0.00");

document.calc.OIPTD.value =  format(roundfloat(getOIPTD()));

document.calc.NEWTOTINTcopy.value =  format(roundfloat(getNEWTOTINT()));

        }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 > 2006) {

 alert("The year must be an integer between 1950 and 2006");

 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);



}
