/* RETURNS THE SELECTED INDEX VALUE OF SELECT LISTS IN THE CALCULATOR TO BE USED IN CALCULATIONS */
function getIndex(n){return n.selectedIndex;}

function calcRdefine(intrate,compound, freq){
 return Math.pow((1.0 + ((intrate/100)/compound)),(compound/freq))-1.0;}

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);
}


/* THIS FUNCTION CALCULATES THE LOAN TO VALUE RATIO */
function LTVcalc(MORTGAGE, MORTGAGE2, APPRAISE){
return (MORTGAGE/APPRAISE) + (MORTGAGE2/APPRAISE);
}


function Ratios(PAY1, PAY2, HEAT, TAX, DEBT, INCOME){
return (PAY1/INCOME)+(PAY2/INCOME)+(HEAT/INCOME)+(TAX/INCOME)+(DEBT/INCOME);
}

/* THIS FUNCTION CALCULATES THE MONTHLY MORTGAGE PAYMENT BASED ON THE USER'S INPUT */
function calcPay(MORTGAGE, AMORT, INRATE, COMPOUND, FREQ){
var compound = COMPOUND/12;
var monTime = AMORT * 12;
var RATE = (INRATE*1.0)/100;
var yrRate = RATE/COMPOUND;
var rdefine    = Math.pow((1.0 + yrRate),compound)-1.0;
var PAYMENT = (MORTGAGE*rdefine * (Math.pow((1.0 + rdefine),monTime)))/  ((Math.pow((1.0 + rdefine),monTime)) - 1.0);
if(FREQ==12){
return PAYMENT;}
if(FREQ==26||FREQ==24){
return PAYMENT/2.0;}
if(FREQ==52){
return PAYMENT/4.0;}
}

function retTerm(n){
if(n==0){return 0;}
if(n==1){return 6;}
if(n==2){return 12;}
if(n==3){return 24;}
if(n==4){return 36;}
if(n==5){return 60;}
if(n==6){return 84;}
if(n==7){return 120;}
}

function retFreq(n){
if(n==0){return 0;}
if(n==1){return 12;}
if(n==2){return 24;}
if(n==3){return 26;}
if(n==4){return 52;}
}

function retAmort(n){
if(n==0){return 0;}
if(n==1){return 5;}
if(n==2){return 10;}
if(n==3){return 15;}
if(n==4){return 20;}
if(n==5){return 25;}
if(n==6){return 30;}
if(n==7){return 35;}
if(n==8){return 40;}
}

function calcTotal(MORTGAGE, LTV){
if(LTV>.75&&LTV<=.80){
return MORTGAGE*1.0125;}
if(LTV>.80&&LTV<=.85){
return MORTGAGE*1.02;}
if(LTV>.80){
return MORTGAGE*1.025;}
if(LTV<=.75){
return MORTGAGE}
}

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

function quickCook(MTGAMT,AMORT,RATE,TERM,FREQ){

var expire = new Date ();
expire.setTime (expire.getTime() + (numDays * 24 * 3600000));/* 2 MONTHS */
/*
var qkData = " " ;
qkData = qkData + '`' + MTGAMT + '`' + AMORT + '`' + RATE + '`' + TERM + '`' + FREQ;
document.cookie = qk +"=" + escape(qkData) +"; expires=" + expire.toGMTString()+"; path=/" ;
}
*/

/* VALIDATES ALL THE FIELDS AND 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 compute(form){

if(document.paycalc.desterm.selectedIndex == 0){
fixpro('Mortgage Term\n','You have not selected an appropriate option.');return false;}

if(document.paycalc.PFREQ.selectedIndex == 0){
fixpro('Payment Frequency\n','You have not selected an appropriate option.');return false;}

if(document.paycalc.NAMORT.selectedIndex == 0){
fixpro('Amortization Period\n','You have not selected an appropriate option.');return false;}

/*
if((document.paycalc.NAMORT.value == null || document.paycalc.NAMORT.value.length == 0)|| (document.paycalc.NAMORT.value < 1 || document.paycalc.NAMORT.value > 40)){
fixpro('Amortization Period\n','Please enter a number between 1 and 40.');return false;}
*/

if((document.paycalc.mortamt.value == null || document.paycalc.mortamt.value.length == 0)|| (document.paycalc.mortamt.value <10000|| document.paycalc.mortamt.value > 1000000) )
{fixpro('Mortgage Amount\n','Please enter a number between 10000 and 1000000.');return false;}

if((document.paycalc.rate.value == null || document.paycalc.rate.value.length == 0)||(document.paycalc.rate.value < 2 || document.paycalc.rate.value > 15))
{fixpro('Interest Rate\n','Please enter a number between 2 and 15.');return false;}

if(retTerm(document.paycalc.desterm.selectedIndex)/12>retAmort(document.paycalc.NAMORT.value)){
fixpro('Mortgage Term\n','The selected mortgage term is greater than the amortization period entered.\nIncrease the amortization period or choose a shorter mortgage term.');return false;}

term = retTerm(document.paycalc.desterm.selectedIndex);
freq = retFreq(document.paycalc.PFREQ.selectedIndex);
amort = retAmort(document.paycalc.NAMORT.selectedIndex);
/*
amort = document.paycalc.NAMORT.value;
*/
mortgage = document.paycalc.mortamt.value;
intrate = document.paycalc.rate.value;
var payment = calcPay(mortgage, amort, intrate, 2, freq);

document.paycalc.mainpay.value = '$' + roundPen(payment);
/*
document.paycalc.mainyr1.value = '$' + roundPen(calcBal(mortgage,intrate,2,freq,payment,(12/(12/freq))));
document.paycalc.mainyr2.value = '$' + roundPen(calcBal(mortgage,intrate,2,freq,payment,(24/(12/freq))));
document.paycalc.mainyr3.value = '$' + roundPen(calcBal(mortgage,intrate,2,freq,payment,(36/(12/freq))));
document.paycalc.mainyr5.value = '$' + roundPen(calcBal(mortgage,intrate,2,freq,payment,(60/(12/freq))));
document.paycalc.mainyr10.value = '$' + roundPen(calcBal(mortgage,intrate,2,freq,payment,(120/(12/freq))));

quickCook(mortgage,amort,intrate,term,freq);
*/

return true;
}

/* 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;}
}
*/

/* LOADS THE AMORTIZATION SCHEDULE FILE ONCE IT IS CERTAIN THAT ALL VALUES ENTERED INTO THE FORM ARE CORRECT. */

/*
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)){
var timere = compute(document.forms[1]);
if(timere==true){setTimeout("document.form2.submit()",100);}
}
else{
if(compute(document.forms[1])==true){document.form2.submit();}}
}

function amortonLink()
{

if(compute(document.forms[1])==true){document.form2.submit();return false;}
else{return false;}
}
*/

function payBal()
{
if(navigator.appVersion.substring(0,3) == 2.0 &&  navigator.appName.substring(0,8)=="Netscape" && navigator.appVersion.indexOf("Macintosh")>=0){
	setTimeout("compute(document.forms[1])",200);
}
else{compute(document.forms[1]);}
}

/*
function browTest()
{
if((navigator.appVersion.substring(0,3) == 3.0 || navigator.appVersion.substring(0,3) == 4.0)&& navigator.appName.substring(0,8)=="Netscape"){
	return true;
}
else{
	return false;
}
}
*/

/*
function makeImg()
{
if(browTest()==true){
Img = new Image();
Img.src = "images/back_on.gif"; 
document.images.Back_button.src=Img.src;
}
else{}

} 

function clearImg()
{
if(browTest()==true){
OrImg= new Image();
OrImg.src="images/back_off.gif";
document.images.Back_button.src=OrImg.src;
}
else{}
}

function loadImg()
{
if(browTest()==true){
Img = new Image();
Img.src = "images/back_on.gif"
}
else{}
}

function openWindow(url, name) {popupWin = window.open(url, name, 'scrollbars,resizable,dependent,toolbar,menubar,width=520,height=400,left=10,top=10')
}

function OpenPopup(page,popupwin,winwidth,winheight) {
	openpopup = 
window.open(page,popupwin,"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=no,width="
+ winwidth + ",height="
+ winheight + ",left=0,top=0");
	openpopup.opener.name = "opener";
}

*/


