

pcd = 'paycalcdata'; /* SAVING CALCULATOR DATA */

pd = 'paydata'; /* SAVING CALCULATOR DATA FOR AM SCHEDULE */

numDays = 60;  /* DAYS UNTIL COOKIE EXPIRES (EG. 183 DAYS = 6 MONTHS) */



//------------------------------------



/*THE FOLLOWING TWO FUNCTIONS CHECK THAT ALL NUMERIC VALUES ARE REAL NUMBER AND REMOVE DOUBLE DECIMALS */

function check(a) 

{

var pest = 0;

var b = "";

for(i=0;i<=a.length;i++){

   var u = a.charAt(i);

   if((u>="0"&&u<="9")||u=="."){

     	if(u=="."){

      		var pest = pest+1;

      		if(pest==2){

      			break;

      		}

      	}

		var b = b + u;

	}

}

return b;

}



//------------------------------------



function doSum(a){ 

a.value = check(a.value);

}



//------------------------------------



/* TESTS VERSIONS FOR WHICH WILL SUPPORT POP UP WINDOWS */

function versTest()

{

var one = '';

var two = '';

if ((navigator.appName.substring(0,8)=="Netscape" && (navigator.appVersion.substring(0,3) == "3.0" ||  navigator.appVersion.substring(0,3) =="4.0"))){

	var one='true';

}

if((navigator.appName.substring(0,9) == "Microsoft" && navigator.appVersion.substring(0,3) == "3.0" && navigator.appVersion.indexOf("Macintosh")>=0)){

	var two='true';

}

if(one=='true' || two=='true' || (navigator.appName.substring(0,9) == "Microsoft" && navigator.appVersion.indexOf("MSIE 3.0")>=0 && navigator.appVersion.indexOf("Windows 3.1")>=0)){

	return true;

}

else{

	return false;

}

}



//------------------------------------



/* TESTS IF VERSION IS MSIE 3.0 FOR MAC */

function msTest()

{

if(navigator.appName.substring(0,9) == "Microsoft" && (navigator.appVersion.substring(0,3) == "3.0" && navigator.appVersion.indexOf("Macintosh")>=0)){

	return true;

}

else{

	return false;

}

}



//------------------------------------



function nineTest()

{

if(navigator.appName.substring(0,9) == "Microsoft" && (navigator.appVersion.substring(0,3)=="3.0" || navigator.appVersion.indexOf("MSIE 3.0")>=0) && (navigator.appVersion.indexOf("Macintosh")==-1 || navigator.appVersion.indexOf("Windows 3.1")== -1)){

	return true;

}

else{

	return false;

}

}



//------------------------------------



/* OPENS POP UP WINDOW TO DISPLAY VALIDATION MESSAGES IN NETSCAPE 3.0 AND 4.0 */

function fixpro(n,q)

{

if(versTest() == true||nineTest()==true){

	if(msTest()==true){

		var winNam='';

	}

	else{

		var slash = location.href.lastIndexOf("/")+1;

		var filNam = location.href.substring(0,slash);

		var winNam = filNam+'empty.html';

	}

	fix = window.open(winNam,'FIX','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=300,height=100');

	if(navigator.appName.substring(0,8) == "Netscape"){

		fix.focus();

	}

	fix.document.write('<html><head><title>Calculators</title>');

	fix.document.write('</head><body bgcolor=ffffff><form name="fixer">');

	fix.document.write('<font size=2 face="Arial, Helvetica" color=306798>'+n+'</font><p><font size=2 FACE="Arial, Helvetica">'+q+'<p>');

	fix.document.write('<center><input type=button value=OK onClick=self.close()>');

	fix.document.write('</center></form></body></html>');

	fix.document.close();

}

else{

	alert(n+'\n'+q)

}

}



//------------------------------------



/* FUNCTION CONFIRMS THAT THE VALUE ENTERED INTO A FIELD FALLS WITHIN THE PRE-DETERMINED MINIMUM AND MAXIMUM VALUES, AND DISPLAYS AN ERROR MESSAGE WITH THE ALLOWABLE NUMERIC RANGE FOR THE FIELD DATA IN A POP UP VALIDATION WINDOW */

function checkNumber(input, min, max, msg)

{

var str = input.value;

for (var i = 0; i < str.length; i++) {

	var ch = str.substring(i, i + 1)

	if ((ch < "0" || "9" < ch) && ch != '.') {

    	alert(msg);

    	return false;

	}

}

if(input.value!=""){

	var num = 0 + str

    if (num < min || max < num) {

    	var sendn = msg+":";

   		var sendq = "You have entered " + input.value + ". Please enter a number between " + min + " and " + max + ".";

  		fixpro(sendn,sendq);

		return false;

	}

	input.value = str;

	return true;

}

}



//------------------------------------



/* CALLS UPON THE FUNCTIONS TO DETERMINE IF THE NUMBERS ENTERED ARE VALID AND TO CALCULATE THE RESULTS OF THE ENTERED DATA FOR EXAMPLE - MORTGAGE PAYMENT, GDS AND TDS RATIOS, AND LOAN TO VALUE. THIS FUNCTION IS EXECUTED EVERYTIME A VALUE IS CHANGED IN A FIELD */

function computeField(input,min,max,msage)

{

doSum(input);

checkNumber(input,min,max,msage);

} 



//------------------------------------



function validAm(input,min,max,msg)

{

doSum(input);

var py = document.prpaycalc;

if(input.name == "NAMORTY"){

	py.NAMORTM.value = 0;

}

if(input.name == "NAMORTM" && py.NAMORTY.value==40){

	fixpro('Question 3: Initial Amortization Period:','The entered amortization period cannot exceed 40 years.');

	input.value = 0;

}

if(input.value > max || input.value < min){

	checkNumber(input,min,max,msg);

	if(input.name == "NAMORTM"){

		input.value = 0;

	}

}

}



//------------------------------------



function amortLink()

{

if(navigator.appVersion.substring(0,3) == 2.0 &&  navigator.appName.substring(0,8)=="Netscape" && (navigator.appVersion.indexOf("Macintosh")>=0||navigator.appVersion.indexOf("PowerPC")>=0)){

	if(validResults()==true && computeResults(2)==true){

		setTimeout("document.amlump.submit()",100);

	}

}

else{

	if(validResults()==true && computeResults(2)==true){

		document.amlump.submit();

	}

}

}



//------------------------------------



function amortonLink()

{

if(validResults==true && validLumps('valid')==true && computeResults(2)==true){

	document.amlump.submit();return false;

}

else{

	return false;

}

}



//------------------------------------



function roundReg(n)

{

if(n > 0){

	pennies = n*100;

	pennies = Math.round(pennies);

	strPennies = "" + pennies;

	len = strPennies.length;

	if (len<3 ) {

		var times=3-len;

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

			strPennies=0+strPennies;

		}

	}

	len = strPennies.length;

	return strPennies.substring(0, len - 2) + "." + strPennies.substring(len -2, len);

}

else 

	return 0;

}



//------------------------------------



function validResults()

{

var py = document.prpaycalc;

if((py.MORTAMT.value == null|| py.MORTAMT.value.length == 0)|| (py.MORTAMT.value <1000|| py.MORTAMT.value > 10000000) ){

	fixpro('Question 1: Mortgage Amount','Please enter a number between 1000 and 10000000 or click the \'Compute\' button if all other fields contain values.');return false;

}





if((py.RATE.value == null || py.RATE.value.length == 0)||(py.RATE.value < 1 || py.RATE.value > 30)){

	fixpro('Question 2: Interest Rate','Please enter a number between 1.0 and 30.0 or click the \'Compute\' button if all other fields contain values..');return false;

}



if((py.NAMORTY.value == null || py.NAMORTY.value.length == 0)|| (py.NAMORTY.value < 1 || py.NAMORTY.value > 40) &&(py.NAMORT.value == null || py.NAMORTM.value.length == 0)|| (py.NAMORTM.value < 0 || py.NAMORTM.value > 12)){

	fixpro('Question 3: Initial Amortization Period','Please enter a value between 1 and 40 years or click the \'Compute\' button if all other fields contain values..');return false;

}

if(py.NAMORTM.value==""){

	py.NAMORTM.value = "0"

}



if((py.MAINPAY.value == null|| py.MAINPAY.value.length == 0) || (py.MAINPAY.value<0 || py.MAINPAY.value > 10000000)){

	fixpro('Question 4: Mortgage Payment','Please enter a number between 0 and 10000000 or click the \'Compute\' button if all other fields contain values..');return false;

}	


/*
if(py.PFREQ.selectedIndex == 0){

	fixpro('Question 5: Payment Frequency','You have not answered this question, click on the PLEASE CHOOSE button to select your option.');return false;

}
*/


return true;

}



//------------------------------------



/* CALCULATES VALUES TO BE ENTERED INTO THE TEXT BOXES AT THE BOTTOM OF THE PAGE WHEN THE USER CLICKS ON COMPUTE OR COMPUTE AMORTIZATION */

function computeResults(n)

{

var py = document.prpaycalc;



//MORTGAGE SCENARIO INFORMATION

var mortgage = py.MORTAMT.value*1.0;

var intRate = (py.RATE.value*1.0)/100;

var amortMon = (py.NAMORTM.value*1.0)+(py.NAMORTY.value*1.0*12);

var paymentx = py.MAINPAY.value*1.0;

var payment=paymentx;

var payFreq = py.PFREQ.selectedIndex*1.0;

var accel = py.ACCSEL.selectedIndex*1.0;

var numPay1 = calcNumPay(payFreq,1);

var numPay2 = calcNumPay(payFreq,2);

var compFreq = 2;

var intFact = calcIntFact(intRate,compFreq,numPay1);

var compFact = calcCompFact(amortMon,intFact,numPay2);



//CALCULATE REVISED AMORTIZATION FOR COMPARISON

var baseRevAm = calcAmort(mortgage,payment,intFact);

var baseRevAmMonths = Math.round((baseRevAm/numPay2)*12);



//CALCULATE TOTAL INTEREST FOR REGULAR MORTGAGE

var regIntBal = calcBal(mortgage,intFact,payment,baseRevAm);

var regularInterest = (payment*baseRevAm)-(mortgage-regIntBal);



//LUMP SUM PAYMENT DECLARATION

var lumpAmt = py.LUMPAMT.value*1.0;

var lumpTime = py.LUMPYRS.value*1.0;



//MORTGAGE PAYMENT INCREASE DECLARATION

var incpayAmt = py.INCPAY.value*1.0;

var incpayType = py.INCTYPE.selectedIndex*1.0;

var incpayTime = py.INCYEARS.value*1.0;



//SOLVE FOR REVISED AMORTIZATION & INTEREST SAVED;



var principalBal = mortgage;

var prepayInterest = 0;

var annualCount = 1;

var aYear = 0;

var lastInterest=0;                                                   

for(payAdd = 1; principalBal>0&&payAdd<=baseRevAm; payAdd ++){

	//SET FLAG IF THIS PAYMENT IS AN ANNIVERSARY

	if(payAdd/Math.floor(numPay1)==annualCount){

		var aYear = 1;}

		

	//INCREASE MORTGAGE PAYMENT IF REQUESTED

	if(aYear==1&&incpayTime>=annualCount){

		var payment = (incpayType==0) ? roundReg((1.0+(incpayAmt/100))*payment)*1.0 : payment+incpayAmt*1.0; }

	

	//INTEREST AND PRINCIPAL PAYMENT	

	var interestPayment = roundReg(principalBal * intFact)*1.0;

  	var principalPayment =  roundReg(payment - interestPayment)*1.0;

  	var checkBal = (principalPayment > principalBal) ? principalBal : principalPayment;

  	var principalBal =  roundReg(principalBal-checkBal)*1.0;

  	

  	//LUMPSUM PAYMENT IF REQUESTED

  	if(aYear==1&&lumpTime>=annualCount){

  		var checkLump = (lumpAmt > principalBal) ? principalBal : lumpAmt;

  		var principalBal = roundReg(principalBal - checkLump)*1.0;}	

  		

	var prepayInterest =  roundReg(prepayInterest+interestPayment)*1.0;

	var realPayment = roundReg(interestPayment+checkBal)*1.0;

	if(principalBal==0){var newPayAdd = payAdd;}

	//RESETS THE ANNIVERSARY 

	if(aYear==1){

		var annualCount = annualCount+1;

		var aYear = 0;}

}





//REVISED AMORTIZATION PERIOD & INTEREST SAVINGS DONE

if(principalBal!=0){var newPayAdd = baseRevAm;}

if(realPayment<(payment/2)){var newPayAdd = newPayAdd-1;}



var revAmortMon = Math.round((newPayAdd/numPay2)*12);



var savedMonths = baseRevAmMonths - revAmortMon;

var savedYrs = Math.floor(savedMonths/12);

var savedMos = savedMonths % 12;

var savedInterest = roundReg(regularInterest - prepayInterest);



//RETURN RESULTS

document.getElementById("AMSAVEY").value=savedYrs;
//py.AMSAVEY.value = savedYrs;

document.getElementById("AMSAVEM").value = savedMos;

document.getElementById("AMINTSAVE").value = savedInterest;

if(n==2){

quickCook(mortgage,paymentx,intFact,numPay2,baseRevAm,intRate,accel,lumpAmt,lumpTime,incpayAmt,incpayType,incpayTime);

}



return true;

}



//------------------------------------



function calcPay(mortgage,intfact,compfact){

return roundReg((mortgage*intfact*compfact)/(compfact-1))*1.0;

}



//------------------------------------



function calcIntFact(intrate,compfreq,numpay1){

return Math.pow((1+intrate/compfreq),(compfreq/numpay1))-1;

}



//------------------------------------



function calcCompFact(amort,intfact,numpay2){

var amortPays = Math.floor((amort/12)*numpay2);

return Math.pow((1+intfact),amortPays);

}



//------------------------------------

function calcBal(mortgage,intfact,payment,whatPay){

return roundReg((mortgage*(Math.pow((1.0 + intfact),(whatPay)))) -  ((payment * ((Math.pow((1.0 + intfact),(whatPay))) - 1.0))/intfact))*1.0;

}



//------------------------------------

function calcAccelPay(payment,numpay1){

return roundReg((payment*13)/Math.floor(numpay1))*1.0;

}



//------------------------------------



/*RETURNS CALCULATED AMORTIZATION PERIOD*/

function calcAmort(mortgage,payment,intfact)

{

return Math.round(Math.log(payment/(payment-mortgage*intfact))/Math.log(1+intfact));

}



//------------------------------------



function calcReverseAccel(payment,numpay1){

return roundReg(((payment*Math.floor(numpay1))/13))*1.0;

}



//------------------------------------



function calcMtgAmount(intfact,payment,whatPay){

return roundReg(payment*((Math.pow((1.0 + intfact),(whatPay)))-1)/(intfact*(Math.pow((1.0 + intfact),(whatPay)))));

}



//------------------------------------



/*RETURNS CALCULATED INTEREST RATE*/

function calcRate(mortgage,amortMon,numPay1,numPay2,payment)

{

if (amortMon!=0) {

var lowgs = 0;

var highgs = 99;

var solvePay=0;

var lastRate = 49.5;

var gsRate = (highgs+lowgs)/2;

var chkDif = 10;

var chkPay=10;

for(gsRate;((chkDif>=0.001)||(chkPay>=0.01)&&gsRate>=0.0001);gsRate=(highgs+lowgs)/2){

var intFact = calcIntFact((gsRate/100),2,numPay1);

var compFact = calcCompFact(amortMon,intFact,numPay2);

var solvePay = calcPay(mortgage,intFact,compFact)*1.0;

if(solvePay<payment){

	var lowgs=gsRate;

	var chkPay = roundReg(payment - solvePay)*1.0;}

else{

	var highgs=gsRate;

	var chkPay = roundReg(solvePay- payment)*1.0;}

	

newRate = (highgs+lowgs)/2;

if(gsRate>newRate){

	var chkDif = gsRate-newRate;}

else{

	var chkDif = newRate-gsRate;}

if(chkDif<0.001||chkPay<0.01){

break;}

}

return gsRate;

}





else {

return 0;

}

}



//-----------------------------------------------



function calcNumPay(freq,n){

if(n==1){

	var numD = 365;

}

if(n==2){

	var numD = 365.25;

}

if(freq==0){

	return 12;

}

if(freq==1){

	return 24;

}

if(freq==2){

	return (numD/14);

}

if(freq==3){

	return (numD/7);

}

}



//------------------------------------





/* SAVES COOKIE CONTAINING DATA TO BE USED IN AMORTIZATION SCHEDULE */

function quickCook(mortgage,payment,intFact,numPay2,baseRevAm,intRate,accel,lumpAmt,lumpTime,incpayAmt,incpayType,incpayTime)

{

var pdData = " " ;

var pdData = pdData + '`' + mortgage + '`'  + payment + '`' + intFact + '`' + numPay2 + '`' + baseRevAm + '`'  + 0 + '`'  + 0 + '`'  + 3 + '`'  + 1 + '`'  +intRate + '`' + accel + '`' + 0+ '`' +lumpAmt+ '`' +lumpTime+ '`' +incpayAmt+ '`' +incpayType+ '`' +incpayTime;

document.cookie = pd +"=" + escape(pdData) +";path=/" ;

}





//------------------------------------



function prepayCook(startLump,startLumpT,timeLump,incrLump,incrLumpT)

{

var preyears = document.prpaycalc.LUMPYRS.value*1.0;

var expire = new Date ();

expire.setTime (expire.getTime() + (numDays * 24 * 3600000));/* 2 MONTHS */

var ppData = " ";

var ppData = ppData + '`' + startLump + '`' + startLumpT + '`' + timeLump + '`' + incrLump + '`' + incrLumpT;

document.cookie = ppd +"=" + escape(ppData) +"; expires=" + expire.toGMTString()+"; path=/" ;

}



//------------------------------------



/* OPENS WINDOW USED TO DISPLAY HELP MESSAGES WHEN THE USER CLICKS ON A HELP BUTTON. THE HELP MESSAGE DISPLAYED IS DETERMINED IN THE ARRAY WHICH IS REFERENCED ACCORDING TO THE HELP BUTTON WHICH WAS CLICKED */

function winopen(name)

{

var linkit = "help/"+name;

if(versTest() == true||nineTest()==true){	

	qc=window.open(linkit,'helpscreen','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=250,height=180');

	if(navigator.appName.substring(0,8) == "Netscape"){

		qc.focus();

	}

}

else{

	location.href=linkit;

}

}



//------------------------------------



function controlCompute(n,x)

{

if(navigator.appVersion.substring(0,3) == 2.0 &&  navigator.appName.substring(0,8)=="Netscape" && navigator.appVersion.indexOf("Macintosh")>=0){

	if(validResults()==true){

		setTimeout("computeResults(n)",200);

		if(x==1){

			saveWin();

		}

		if(x==2){

			sumWin();

		}

	}

}

else{

	if(validResults()==true){

		computeResults(n);

		if(x==1){

			saveWin();

		}

		if(x==2){

			sumWin();

		}

	}

}

}



//------------------------------------



function saveWin()

{



if(versTest() == true||nineTest()==true){	

	sw=window.open('prepay_save.html','savings','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=250,height=180');

	if(navigator.appName.substring(0,8) == "Netscape"){

		sw.focus();

	}

}

else{

	location.href='prepay_save.html';

}h

}



//------------------------------------



function sumWin()

{



if(versTest() == true||nineTest()==true){	

	sumW=window.open('prepayanalysis.html','summary','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=550,height=300');

	if(navigator.appName.substring(0,8) == "Netscape"){

		sumW.focus();

	}

}

else{

	location.href='prepayanalysis.html';

}

}



//------------------------------------



//------------------------------------



function controlMortgage(n)

{
dijit.byId("pre_payment").show();
if(navigator.appVersion.substring(0,3) == 2.0 &&  navigator.appName.substring(0,8)=="Netscape" && navigator.appVersion.indexOf("Macintosh")>=0){

	if(validResults()==true){

		setTimeout("calcMortgage(3)",200);

		if(n==1){

			controlCompute(1,0);

		}

	}

}

else{

	if(validResults()==true){

		calcMortgage(3);

		if(n==1){

			controlCompute(1,0);

		}

	}

}

}



//------------------------------------



function validLumps(field)

{

return true;

}

	

//------------------------------------



//FUNCTION CALCULATES MISSING ELEMENT IN MORTGAGE SCENARIO



function calcMortgage(calcField){

var py = document.prpaycalc;



//DECLARE VARIABLES FOR KNOWN INFO

if(calcField!=0){var mortgage = py.MORTAMT.value*1.0;}

if(calcField!=1){var intRate = (py.RATE.value*1.0)/100;}

if(calcField!=2){var amortMon =(py.NAMORTM.value*1.0)+(py.NAMORTY.value*1.0*12);}

if(calcField!=3){var payment = py.MAINPAY.value*1.0;}	



var accel = py.ACCSEL.selectedIndex*1.0;

var payFreq = py.PFREQ.selectedIndex*1.0;

var compFreq = 2;

var numPay1 = calcNumPay(payFreq,1);

var numPay2 = calcNumPay(payFreq,2);

	

//SOLVE FOR MORTGAGE AMOUNT

if(calcField==0){

	if(accel==1){

		var regPayment = calcReverseAccel(payment,numPay1);

		var mortgage = calcMtgAmount(calcIntFact(intRate,compFreq,12),regPayment,amortMon);

	}

	if(accel==0){

		var initialAmPays = Math.floor((amortMon/12)*numPay2);

		var intFact = calcIntFact(intRate,compFreq,numPay1);

		var mortgage = calcMtgAmount(intFact,payment,initialAmPays);

	}

	py.MORTAMT.value=roundReg(mortgage);

}



//SOLVE FOR INTEREST RATE

if(calcField==1){

	if(accel==1){

		var regPayment = calcReverseAccel(payment,numPay1);

		var intRate = calcRate(mortgage,amortMon,numPay1,numPay2,regPayment);}

	if(accel==0){

		var intRate = calcRate(mortgage,amortMon,numPay1,numPay2,payment);

	}

	py.RATE.value=intRate;

}



//SOLVE FOR AMORTIZATION PERIOD



if(calcField==2){

	if(accel==1){

		var regPayment = calcReverseAccel(payment,numPay1);

		var amortMon2 = calcAmort(mortgage,regPayment,calcIntFact(intRate,compFreq,12));

		var initAmortMon = Math.round((amortMon2/12)*12);

	}

	if(accel==0){

		var intFact = calcIntFact(intRate,compFreq,numPay1);

		var amortMon2 = calcAmort(mortgage,payment,intFact);

		var initAmortMon = Math.round((amortMon2/numPay2)*12);

	}

	py.NAMORTM.value = initAmortMon % 12;

	py.NAMORTY.value = Math.floor(initAmortMon/12);

}
 
 

//SOLVE FOR MORTGAGE PAYMENT

if(calcField==3){

	if(accel==1){

		var monIntFact = calcIntFact(intRate,compFreq,12);

		var monCompFact = calcCompFact(amortMon,monIntFact,12);

		var monPayment = calcPay(mortgage,monIntFact,monCompFact);

		var payment = calcAccelPay(monPayment,numPay1);}

	if(accel==0){

		var intFact = calcIntFact(intRate,compFreq,numPay1);

		var compFact = calcCompFact(amortMon,intFact,numPay2);

		var payment = calcPay(mortgage,intFact,compFact);

	}

	py.MAINPAY.value=roundReg(payment);

}





}


