function floor(number)
{
  return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}

function loanpayment()
{
  var mi = document.temps.IR.value / 1200;
  var base = 1;
  var mbase = 1 + mi;
  for (i=0; i<document.temps.YR.value * 12; i++)
  {
    base = base * mbase
  }
  document.temps.PI.value = floor(document.temps.LA.value * mi / ( 1 - (1/base)))
  document.temps.MT.value = floor(document.temps.AT.value / 12)
  document.temps.MI.value = floor(document.temps.AI.value / 12)
  var dasum = document.temps.LA.value * mi / ( 1 - (1/base)) +
	document.temps.AT.value / 12 + 
	document.temps.AI.value / 12;
  document.temps.MP.value = floor(dasum);
}

//Fade Transitions//

var calc = new Array();

calc[0] = "intro";
calc[1] = "loanpayment";
calc[2] = "refinance";
calc[3] = "amortization";
calc[4] = "yr15v30";
calc[5] = "affordability";
function showCalc (active){
	var i=0;
	while (i<6) 
	{
		if(calc[i]!=active)
		{
			if(calc[i]==activecalc)
			{
				Effect.Appear(calc[i], {from:1.0, to:0.0});
				var t=setTimeout("Element.hide('"+calc[i]+"');",1000);
			}
		}
		i++;
	}
	
	if(active!=activecalc)
	{
		Effect.Appear(active, {from:0.0, to:1.0});
		activecalc=active;
	}
}

//Refi Script//

function NumFormat( __StringNumber ) {
 InString = _Trim(__StringNumber);
 OutString = "";
 InLength = InString.length;
 for(__x=InLength; __x!=0; __x--) {
  if(OutString.length == 3 | OutString.length == 7 | OutString.length == 11) {
   OutString = "," + OutString;
   OutString = InString.substring(__x-1,__x) + OutString;
  } else {
   OutString = InString.substring(__x-1,__x) + OutString;
  }
 }
 return OutString;
}

function _Trim( str ) {
	astr = String( str );
	t_str = String("");
	LastCharAt = 0;
	for(x=astr.length-1;x>=0;x--) {
		if( astr.charAt(x) != " " ) {
			LastCharAt = x+1;
			break;
		}
	}
	return astr.substring(0,LastCharAt);
}

function computeForm(form) {

    var pmt1 = form.payment.value;
    var prin1 = form.principal.value;
    var intPort1 = 0;
    var prinPort1 = 0;
    var accumInt1 = 0;
    var accumPrin1 = 0;
    var i1 = form.intRate.value;

    if (i1 > 1.0) {
        i1 = i1 / 100.0;
        //BF
        //form.intRate.value = i1;
    }

    var i1  = i1  / 12;
    var i2 = form.intRate2.value;

       if (i2 > 1.0) {
           i2 = i2 / 100.0;
			//BF
           //form.intRate2.value = i2;
       }

       var i2  = i2  / 12;
    var count1 = 0;

    while(prin1 > 0) {
        intPort1 = prin1 * i1;
        prinPort1 = pmt1 - intPort1;
        prin1 = prin1 - prinPort1;
        accumPrin1 = accumPrin1 + prinPort1;
        accumInt1 = accumInt1 + intPort1;
        count1 = count1 + 1;
        if(count1 > 600) {break; } else {continue; }
       }

    form.origInt.value = "$" + parseInt(accumInt1,10);
    var pow = 1;
    for (var j = 0; j < form.nper2.value *12; j++)
        pow = pow * (1 + i2);
    var fpayment2 = (form.principal.value * pow * i2) / (pow - 1);
    form.payment2.value = "$" + parseInt(fpayment2,10) + "." + parseInt(fpayment2 % 1 * 100,10);
   var fmoSave = form.payment.value - fpayment2;
   form.moSave.value = "$" + parseInt(fmoSave,10) + "." + parseInt(fmoSave % 1 *100,10);
   var ftotInt2 = (fpayment2 * form.nper2.value *12) - form.principal.value;
   form.totInt2.value = "$" + parseInt(ftotInt2,10);
   var fintSave = accumInt1 - ftotInt2;
		if (fintSave < 0) 
			fintSave *= (-1);
    form.intSave.value = "$" + parseInt(fintSave,10);
    form.closeMo.value = parseInt(form.closingCost.value / fmoSave,10);
   var fnetSave = fintSave - form.closingCost.value;
		if (fnetSave < 0)
			fnetSave *= (-1);
   form.netSave.value = "$" + parseInt(fnetSave,10);
    //form.summary.value = "If you refinance your current " + parseInt(form.intRate.value *100,10) + "." + parseInt(form.intRate.value *100 % 1 *100,10) + "% mortgage to a " + parseInt(form.intRate2.value *100,10) + "." + parseInt(form.intRate2.value *100 % 1 *100,10) + "% mortgage, your monthly payment will drop by " + form.moSave.value + " and you will save " + form.intSave.value + " in interest charges over the life of the mortgage.  However, in order for this refinancing to yield any savings at all you will need to stay in your current home for at least " + form.closeMo.value + " months.  That's how long it will take for your monthly payment savings to offset the closing costs attributable to refinancing.";
                
}



function clearForm(form)
{

    form.principal.value = "";
    form.payment.value = "";
    form.interest.value = "";
    form.interest2.value = "";
    form.term.value = "";
    form.closingCost.value = "";
    form.payment2.value = "";
    form.savings.value = "";
    form.breakeven.value = "";
    form.IntSaved.value = "";
    form.netSave.value = "";
    form.Rinterest.value = "";
}

    function computeField(input) {
        if (input.value != null && input.value.length != 0)
            input.value = "" + NumFormat(parseInt(input.value));

}

function _StrTran( _str2b_chg, _substr2_look4, _substr2_sub ) {
//=====================================================================
// function _StrTran( _str2b_chg, _substr2_look4, _substr2_sub )
//
//
//=====================================================================
 var _Str_2B_Chg = String( _str2b_chg );
 var _Str_2_Look_4 = String( _substr2_look4 );
 var _Str_2_Sub = String( _substr2_sub );
 var _Out_Str = "";

 _len2chg = _Str_2B_Chg.length;
 _len2lk4 = _Str_2_Look_4.length;
 for( _stepr=0; _stepr<_len2chg; _stepr++ ) {
  if(_Str_2B_Chg.substring(_stepr,_stepr+_len2lk4) == _Str_2_Look_4)
   _Out_Str += _Str_2_Sub;
  else
   _Out_Str += _Str_2B_Chg.substring(_stepr,_stepr+_len2lk4)
 }
 return _Out_Str;
}

// Amortization script:


function amor_formatNum(Vnum) {

   if(Vnum > 99000000) {
   alert("Sorry, this will not generate numbers larger that 99 million.");
   focus();
   } else {
   
   var V10million = parseInt(Vnum / 10000000);

   var V1million = (Vnum % 10000000)  / 1000000;
      if(V1million / 1000000 == 1) {
      V1million = 1;
      } else
      if(V1million < 1) {
      V1million = "0";
     } else {
      V1million = parseInt(V1million,10);
     }

    var V100thousand = (Vnum % 1000000)  / 100000;
      if(V100thousand / 100000 == 1) {
      V100thousand = 1;
      } else
      if(V100thousand < 1) {
      V100thousand = "0";
     } else {
      V100thousand = parseInt(V100thousand,10);
     }

   var V10thousand = (Vnum % 100000)  / 10000;
      if(V10thousand / 10000 == 1) {
      V10thousand = 1;
      } else
      if(V10thousand < 1) {
      V10thousand = "0";
      } else {
      V10thousand = parseInt(V10thousand,10);
      }

   var V1thousand = (Vnum % 10000)  / 1000;
      if(V1thousand / 1000 == 1) {
      V1thousand = 1;
      } else
      if(V1thousand < 1) {
      V1thousand = "0";
     } else {
      V1thousand = parseInt(V1thousand,10);
     }

   var Vhundreds = (Vnum % 1000)  / 100;
      if(Vhundreds / 100 == 1) {
      Vhundreds = 1;
      } else
      if(Vhundreds < 1) {
      Vhundreds = "0";
     } else {
      Vhundreds = parseInt(Vhundreds,10);
     }

   var Vtens = (Vnum % 100)  / 10;
      if(Vtens / 10 == 1) {
      Vtens = 1;
      } else
      if(Vtens < 1) {
      Vtens = "0";
     } else {
      Vtens = parseInt(Vtens,10);
     }

   var Vones = (Vnum % 10)  / 1;
      if(Vones / 1 == 1) {
      Vones = 1;
      } else
      if(Vones < 1) {
      Vones = "0";
     } else {
      Vones = parseInt(Vones,10);
     }

var Vcents = 0;

if(Vnum % 1 * 100 < 1) {
   Vcents = 0;
   } else {
   Vcents = parseInt(((eval(Vnum % 1) * 100)),10);
   }



 if(Vcents < 1) {
  Vcents = "00";
  }
  else
  if(Vcents % 10 == 0) {
  Vcents = Vcents + "0";
  }
  else
  if(Vcents % 10 == Vcents) {
  Vcents = "0" + Vcents;
  } else {
  Vcents = Vcents;
  }

  if(Vcents == "900") {
  Vcents = "90";
  } else
  if(Vcents == "800") {
  Vcents = "80";
  } else 
  if(Vcents == "700") {
  Vcents = "70";
  } else 
  if(Vcents == "600") {
  Vcents = "60";
  } else 
  if(Vcents == "500") {
  Vcents = "50";
  } else 
  if(Vcents == "400") {
  Vcents = "40";
  } else 
  if(Vcents == "300") {
  Vcents = "30";
  } else
  if(Vcents == "200") {
  Vcents = "20";
  } else
  if(Vcents == "100") {
  Vcents = "10";
  } else {
  Vcents = Vcents;
  }

   
   var Vformat = "";

   if(Vnum >= 10000000) {
   Vformat = (V10million + "" + V1million + "," + V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 1000000) {
   Vformat = (V1million + "," + V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 100000) {
   Vformat = (V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 10000) {
   Vformat = (V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 1000) {
   Vformat = (V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 100) {
   Vformat = (Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 10) {
   Vformat = (Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 1) {
   Vformat = (Vones + "." + Vcents);
   } else {
   Vformat = ("0." + Vcents);
   }

  return Vformat;

  }
}


function amor_computeForm(form) {

if(form.principal.value == "" || form.principal.value == 0) {
   alert("Please enter an amount in Line #2.");
   form.principal.focus();
   } else
      if(form.intRate.value == "" || form.intRate.value == 0) {
      alert("Please enter an amount in Line #3.");
      form.intRate.focus();
   } else
      if(form.numMonths.value == "" || form.numMonths.value == 0) {
      alert("Please enter an amount in Line #4.");
      form.numMonths.focus();
   } else {

var Vprincipal = form.principal.value;

var intRate = form.intRate.value;

if (intRate > 1.0) {

        intRate = intRate / 100.0;
        } else {
        intRate = intRate;
        }

var numMonths = form.numMonths.value;
var numPeriods = 0;
var divisor = 0;

if(form.interval.selectedIndex == 0) {
   intRate = intRate / 12;
   numPeriods = numMonths;
   divisor = 1;
   } else
if(form.interval.selectedIndex == 1) {
   intRate = intRate / 4;
   numPeriods = numMonths / 3;
   divisor = 3;
   } else
if(form.interval.selectedIndex == 2) {
   intRate = intRate / 2;
   numPeriods = numMonths / 6;
   divisor = 6;
   } else {
   intRate = intRate;
   numPeriods = numMonths / 12;
   divisor = 12;
   }



//FIGURE PAYMENT NEEDED TO PAY OFF $1,000

    var factor = 1;

    for (var j = 0; j < numPeriods; j++) {

        factor = factor * (eval(1) + eval(intRate));
        }


    var periodPmt = (Vprincipal * factor * intRate) / (eval(factor) - eval(1));
    var moPmt = periodPmt / divisor;

    form.moPmt.value = "$" + amor_formatNum(moPmt);
    form.HmoPmt.value = moPmt;
    form.HperiodPmt.value = periodPmt;
    form.HintRate.value = intRate;
    form.Hdivisor.value = divisor;
    form.HnumMonths.value = numMonths;
    form.HnumPeriods.value = numPeriods;

//END VARIFICATION IF STATEMENT
   }
    
}


function amor_createReport(form) {

//GRAB VARIABLES

var aPrin = eval(form.principal.value);

var aIntRate = eval(form.HintRate.value);

var aNPer = eval(form.HnumPeriods.value);

var anumMonths = eval(form.HnumMonths.value);

var adivisor = eval(form.Hdivisor.value);


if(form.HmoPmt.value == 0 || form.HmoPmt.value == "") {
   alert("Please compute the payment before creating the schedule.");
   } else {

var aPmt = form.HmoPmt.value;

var aIntPort = 0;
var aAccumInt = 0;
var aPrinPort = 0;
var aAccumPrin = 0;
var aCount = 0;
var monthCount = 0;
var aPmtRow = "";
var aPmtNum = 0;
var mIntPort = 0;

var Vmonth = eval(form.month.selectedIndex) + eval(1);
var Vday = eval(form.day.selectedIndex) + eval(1);
var Vyear = eval(form.year.selectedIndex) + eval(1990);
var loanDate = (Vmonth + "/" + Vday + "/" + Vyear);

while(eval(aCount) < eval(aNPer)) {
   aIntPort = aPrin * aIntRate;
   monthCount = 0;
   mIntPort = aIntPort

   while(eval(monthCount) < eval(adivisor)) {
   
   if(mIntPort > aPmt) {
      aIntPort = aPmt;
      aPrinPort = 0;
      } else 
      if(mIntPort <= 0) {
      aIntPort = 0;
      aPrinPort = aPmt;
      } else {
      aPrinPort = eval(aPmt) - eval(mIntPort);
      aIntPort = eval(aPmt) - eval(aPrinPort);
      }

   mIntPort = eval(mIntPort) - eval(aIntPort);
   aPrin = eval(aPrin) - eval(aPrinPort); 
   aAccumInt = eval(aAccumInt) + eval(aIntPort);
   aAccumPrin = eval(aAccumPrin) + eval(aPrinPort);
   aPmtNum = eval(aPmtNum) + eval(1);
 
   Vmonth = Vmonth + 1;
   if(Vmonth == 13) {
      Vmonth = 1;
      Vyear = Vyear + 1;
      } else {
      Vmonth = Vmonth;
      Vyear = Vyear;
      }
   pmtString = (Vmonth + "/" + Vday + "/" + Vyear);

   aPmtRow = ("" + aPmtRow + "<TR><TD ALIGN=CENTER bgcolor='#FFFFFF'><span class='headerBlack'>" + aPmtNum + "</span></TD><TD ALIGN=RIGHT><span class='bodytext'>" + pmtString + "</span></TD><TD ALIGN=RIGHT><span class='bodytext'>" + amor_formatNum(aPrinPort) + "</span></TD><TD ALIGN=RIGHT><span class='bodytext'>" + amor_formatNum(aIntPort) + "</span></TD><TD ALIGN=RIGHT><span class='bodytext'>" + amor_formatNum(aPrin) + "</span></TD></TR>");

   monthCount = eval(monthCount) +  eval(1);
   }
//end inner loop
   aCount = eval(aCount) + eval(1);
      if(aCount > 600) {
         alert("Using your current entries you will never pay off this loan.");
         break;
         } else {
         continue;
         }
    }

var part1 = ("<HEAD><TITLE>Amortization Schedule</TITLE><style>body {background-color: #FFFFFF;}.headerBlack { font-family: Verdana, Arial, Helvetica, sans-serif;  font-size: 12px;  font-style: normal;  line-height: normal;  font-weight: bold;  font-variant: normal;  text-transform: none;  color: #000000;  text-decoration: none; }.bodytext {  font-family: Verdana, Arial, Helvetica, sans-serif;  font-size: 12px;  font-style: normal;  line-height: normal;  font-weight: normal;  font-variant: normal;  text-transform: none;  color: #000000;  text-decoration: none; }</style></HEAD>" + "<BODY background='white' bgcolor='white'><BR><center><b><span class='headerBlack'>Amortization Schedule</span><BR><BR><b><span class='headerBlack'>Created for: " + form.customerName.value+"</span>");

var part2 = ("<CENTER><TABLE BORDER=0 CELLPADDING=4><TR><TD COLSPAN=5><span class='headerBlack'>Loan Date:</span><span class='bodytext'> " + loanDate + "</span><BR><span class='headerBlack'>Principal:</span><span class='bodytext'> $" + amor_formatNum(form.principal.value) + "</span><BR><span class='headerBlack'># of Payments:</span><span class='bodytext'> " + anumMonths + "</span><BR><span class='headerBlack'>Interest Rate:</span><span class='bodytext'> " + amor_formatNum(aIntRate * 12 / adivisor * 100) + "%</span><BR><span class='headerBlack'>Payment:</span><span class='bodytext'> $" + amor_formatNum(form.HmoPmt.value) + "</span></TD></TR><TR><TD COLSPAN=5><CENTER><span class='headerBlack'>Schedule of Payments</span><BR><span class='bodytext'>Please allow for slight rounding differences.</CENTER></TD></TR><TR><TD bgcolor='#FFFFFF'><span class='headerBlack'>Payment #</span></TD><TD bgcolor='#FFFFFF' ALIGN=RIGHT><span class='headerBlack'>Date</span></TD><TD bgcolor='#FFFFFF' ALIGN=RIGHT><span class='headerBlack'>Principal</span></TD><TD bgcolor='#FFFFFF' ALIGN=RIGHT><span class='headerBlack'>Interest</span></TD><TD bgcolor='#FFFFFF' ALIGN=RIGHT><span class='headerBlack'>Balance</span></TD></TR>");

var part3 = ("" + aPmtRow + "");

var part4 = ("<TR><TD colspan=2 bgcolor='FFFFFF'><span class='headerBlack'>Totals</span></TD><TD ALIGN=RIGHT><span class='headerBlack'>" + amor_formatNum(aAccumPrin) + "</span></TD><TD><span class='headerBlack'>" + amor_formatNum(aAccumInt) + "</span></TD><TD align=right><span class='bodytext'>0.00</span></TD></TR></center></TD></TR></TABLE></BODY></HTML>");

var schedule = (part1 + "" + part2 + "" + part3 + part4 + "");

  reportWin = window.open("","Amortization","width=500,height=300,scrollbars=yes");
  reportWin.document.write(schedule);
  reportWin.document.close();

  }
}




function amor_clearForm(form) {
   form.customerName.value = "";
   form.principal.value = "";
   form.intRate.value = "";
   form.numMonths.value = "";
   form.moPmt.value = "";
   form.HmoPmt.value = "";
   form.HperiodPmt.value= "";
   form.Hdivisor.value = "";
   form.HintRate.value= "";
   form.HnumMonths.value= "";
   form.HnumPeriods.value= "";
   }

// 15 vs 30 year mortage

    function yr_checkNumber(input, min, max, msg) {
        msg = msg + " field has invalid data: " + input.value;
        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;
            }
        }
        var num = 0 + str
        if (num < min || max < num) {
            alert(msg + " not in range [" + min + ".." + max + "]");
            return false;
        }
        input.value = str;
        return true;
    }

function yr_NumFormat( __StringNumber ) {
 InString = _Trim(__StringNumber);
 OutString = "";
 InLength = InString.length;
 for(__x=InLength; __x!=0; __x--) {
  if(OutString.length == 3 | OutString.length == 7 | OutString.length == 11) {
   OutString = "," + OutString;
   OutString = InString.substring(__x-1,__x) + OutString;
  } else {
   OutString = InString.substring(__x-1,__x) + OutString;
  }
 }
 return OutString;
}

function _Trim( str ) {
	astr = String( str );
	t_str = String("");
	LastCharAt = 0;
	for(x=astr.length-1;x>=0;x--) {
		if( astr.charAt(x) != " " ) {
			LastCharAt = x+1;
			break;
		}
	}
	return astr.substring(0,LastCharAt);
}

    function yr_computeForm(form) {
	//
	// Insure all needed fields are input!
	//
        if ((form.yrterm.value == null || form.yrterm.value.length == 0) ||
            (form.interest.value == null || form.interest.value.length == 0) ||
            (form.principal.value == null || form.principal.value.length == 0)) {
            return;
        }
	//
	// Insure all input fields are within valid limits!
	//
        if (!yr_checkNumber(form.yrterm, 1, 480, "# of payments") ||
            !yr_checkNumber(form.interest, 0, 99, "Interest")) {
            form.payment.value = "Invalid";
            return;
        }
	var thenumberofpayments = form.yrterm.value * 12;
	var theinterestrate = form.interest.value;
	var theprincipal = _StrTran(form.principal.value,",","");


	//
	// Check / Set interest rate to correct value!
	//
        var i = theinterestrate;
		if (i == 0) 
			i = .001;

        if (i > 1.0) {
            i = i / 100.0;
            //form.interest.value = i;
        }
	//
	// Calculate payments!
	//
        i /= 12;
        var pow = 1;
        	for (var j = 0; j < thenumberofpayments; j++)
            	pow = pow * (1 + i);
		var mpaymnt = (theprincipal * pow * i) / (pow - 1);


        form.payment.value = "$"+NumFormat(parseInt(mpaymnt));
		form.yrpayment.value = "$"+NumFormat(parseInt(mpaymnt*12));
		form.tpayment.value = "$"+NumFormat(parseInt((mpaymnt*12)*(form.yrterm.value)));
		form.mprinciple.value = "$"+NumFormat(parseInt(((mpaymnt*12)*(form.yrterm.value))-(_StrTran(form.principal.value,",",""))));

	}	


    function yr_computeForm2(form) {

        if ((form.yrterm2.value == null || form.yrterm2.value.length == 0) ||
            (form.interest.value == null || form.interest.value.length == 0)) {
            return;
        }

        if (!yr_checkNumber(form.yrterm2, 1, 480, "# of payments") ||
            !yr_checkNumber(form.interest, 0, 99, "Interest")) {
            form.bipayment.value = "Invalid";
            return;
        }
	var thenumberofpayments = form.yrterm2.value * 12;
	var thenumberofpayments2 = form.yrterm.value * 12;
	var theinterestrate = form.interest.value;
	var theprincipal =  _StrTran(form.principal.value,",","");

        i /= 12;
        var pow = 1;
        	for (var j = 0; j < thenumberofpayments2; j++)
            	pow = pow * (1 + i);
		var mpaymnt = (theprincipal * pow * i) / (pow - 1);


        var i = theinterestrate;
		if (i == 0) 
			i = .001;

        if (i > 1.0) {
            i = i / 100.0;
            //form.interest.value = i;
        }

        i /= 12;
        var pow = 1;
        	for (var j = 0; j < thenumberofpayments; j++)
            	pow = pow * (1 + i);
		var xpayment = (theprincipal * pow * i) / (pow - 1);

        form.bipayment.value = "$"+NumFormat(parseInt(xpayment));
		form.biyrpayment.value = "$"+NumFormat(parseInt(xpayment*12));
		form.bitpayment.value = "$"+NumFormat(parseInt(xpayment*12*form.yrterm2.value));
		form.bprinciple.value = "$"+NumFormat(parseInt((xpayment*12*form.yrterm2.value)-_StrTran(form.principal.value,",","")));
//		form.savings2.value = "$"+NumFormat(parseInt(((form.payment.value*12)*form.yrterm.value)-(_StrTran(form.principal.value,",","")))-parseInt(((form.bipayment.value*12)*form.yrterm2.value)-(_StrTran(form.principal.value,",",""))));

	}

    function yr_clearForm(form) {
        form.yrterm.value = "";
        form.interest.value = "";
        form.principal.value = "";
    }

    function yr_computeField(input) {
        if (input.value != null && input.value.length != 0)
            input.value = "" + NumFormat(parseInt(input.value));

}

function _StrTran( _str2b_chg, _substr2_look4, _substr2_sub ) {
//=====================================================================
// function _StrTran( _str2b_chg, _substr2_look4, _substr2_sub )
//
//
//=====================================================================
 var _Str_2B_Chg = String( _str2b_chg );
 var _Str_2_Look_4 = String( _substr2_look4 );
 var _Str_2_Sub = String( _substr2_sub );
 var _Out_Str = "";

 _len2chg = _Str_2B_Chg.length;
 _len2lk4 = _Str_2_Look_4.length;
 for( _stepr=0; _stepr<_len2chg; _stepr++ ) {
  if(_Str_2B_Chg.substring(_stepr,_stepr+_len2lk4) == _Str_2_Look_4)
   _Out_Str += _Str_2_Sub;
  else
   _Out_Str += _Str_2B_Chg.substring(_stepr,_stepr+_len2lk4)
 }
 return _Out_Str;
}

// Affordability script

	function calcMonthlyAfford() {
			var v_Income,
					v_OutstandingDebt,
					v_RecommendMonthly,
					v_formName = document.calc;

			if ( confirmAffordValues() ) {
			v_Income = roundFloat(makeNumValid(v_formName.Income.value));
			v_OutstandingDebt = roundFloat(makeNumValid(v_formName.OutstandingDebt.value)); 
			v_RecommendMonthly = Math.floor(((((v_Income/12)-v_OutstandingDebt)*0.28))*100)/100; 

			// if recommended monthly payment is negative, set to 0
			if ( v_RecommendMonthly <0 ) {
				v_RecommendMonthly = 0;
			}
			v_formName.RecommendMonthly.value = addCommas(roundFloat(v_RecommendMonthly));
			v_MonthlyAffordHold = v_formName.RecommendMonthly.value;
			}
	}

	function confirmAffordValues () {
			var v_form = document.calc,
					v_Income = v_form.Income.value,
					v_OutstandingDebt = v_form.OutstandingDebt.value,
					v_tIncome,
					v_tOutstandingDebt,
					v_errorflag = 0,
					v_errorstr,
					v_returnval;
	
			// get values from form
			v_tIncome = parseFloat(makeNumValid(v_Income));
			v_tOutstandingDebt = parseFloat(makeNumValid(v_OutstandingDebt));
	
			if ( v_tOutstandingDebt <0 ) { // N3.x bug; returns -1 when set 0
				v_tOutstandingDebt = 0;
			}
			if ( v_tIncome == '' || isNaN(v_tIncome) ) {
				v_form.Income.value = 0;
				v_tIncome = 0;
			}
			// income > 0 or <100,000,000
			if ( v_tIncome <0 || v_tIncome > 100000000 ) {
					v_errorstr = "Sorry.	$" + v_Income + " is an invalid income.\nPlease enter an income between $0.00 and $100,000,000.00";
		v_form.Income.value = '';
		v_errorflag = 1;
			// outstanding debt <0 or > balance	
			} else if ( v_tOutstandingDebt <0 || v_tOutstandingDebt > v_tIncome ){
					v_errorstr = "Sorry.	Your outstanding debt of $" + v_OutstandingDebt + " must be \nless than your income of $" + v_Income;
		v_form.OutstandingDebt.value = '';
		v_errorflag = 1;
			}

			// display error and clear result
			if ( v_errorflag ) {
				alert(v_errorstr);
	clearRecommendMonthly();
	
	v_returnval = 0;
			} else {
				v_returnval = 1;
			}
			return ( v_returnval );
	}

	function clearRecommendMonthly () {
		var v_form = document.calc;

		v_form.RecommendMonthly.value = '';
	}
	
	                function removeCommas( t_value ) {
                        var ctr;
                        var newstr = t_value;
                        var commapos;
                        var strlen = t_value.length - 1;

                        commapos = newstr.indexOf(',');
                        while ( commapos != -1 ) {
                                        newstr = newstr.substring(0,commapos) +  
                                                                         newstr.substring(commapos + 1, newstr.length );

                commapos = newstr.indexOf(',');
                         }
                         return(newstr);
        }
                function addCommas( u_value ) {
                        var ctr;
                        var decCtr=0;
                        var decIndex;
                        var decIncr = 3;
                        var newstr = u_value;
                        var strlen = u_value.length;

                        decIndex = u_value.indexOf('.');
                        if ( decIndex == -1 ) 
                                        decIndex = strlen; 

                        for ( ctr = decIndex-1; ctr > 0; ctr-- ) {
                                        decCtr++;
                                        if ( decCtr == 3 ) { 
                                                        newstr = newstr.substring(0,ctr) +
                                                                                         "," +
                                                                                         newstr.substring(ctr, strlen );
                                                        strlen++;
                                                        decCtr = 0; 
                                        }
                        }

                 return(newstr);
        }
                function makeNumValid( p_value ) {
                        var strchar;
                        var strctr;
                        var strlen;
                        var instr = p_value;
                        var newstr;
                        var tempstr;

                                                if( instr.indexOf('.') != instr.lastIndexOf('.') ) 
                                        return(-1); 

                                                newstr = removeCommas( instr );

                                                strlen = newstr.length;
                        tempstr = '';
                        for( strctr=0; strctr <strlen; strctr++) {
                strchar = newstr.charAt(strctr);
                if( (strchar == '.') || (strchar == '0') || (strchar == '1') || (strchar == '2') 
                                         || (strchar == '3') || (strchar == '4') || (strchar == '5') || (strchar == '6') 
                                         || (strchar == '7') || (strchar == '8') || (strchar == '9')) 
                                                        tempstr = tempstr + strchar;
                        }
                        newstr = tempstr;

                        if( newstr == "" ) 
                                        return(0);
                        else 
                                        return(newstr); 
        }
        
                function roundFloat( s_value ) {
                        var newval;

                        s_value = parseFloat(s_value);
                        newval = (Math.floor(s_value*100))/100;
                        newval = newval + "";

                        if ( newval.indexOf('.') == -1 ) 
                                        newval = newval + ".00"; 

                        return(newval);
        }

