

function putFocus(){
 document.forms[0].elements[0].focus();
 }

function next(form){
 if (form.payment.value==""){
   alert("Please try to calculate a monthly payment.  Enter any estimated amount.");
    putFocus();
  }else{
   window.location="48.htm"}
 }

function test_it(entry) {
 if (entry.value!=null && entry.value.length!=0) {
   entry.value=""+ eval(entry.value);
   }
  computeForm(entry.form);
  }

function computeForm(form) {
 if ((form.months.value==null || 
    form.months.value.length==0) || 
    (form.rate.value==null || form.rate.value.length==0) || 
    (form.financed.value == null || form.financed.value.length==0)) {
   return;
   }
 
 if (!alrt_msg(form.months,1,300,"Your Number of Payments") ||
     !alrt_msg(form.rate,1,9.00,"Your Interest Rate") ||
     !alrt_msg(form.financed,100,200000,"Your Principal Amount")) {
     form.payment.value="Click reset.";
   return;
 }
 
 var i=form.rate.value;
 ffv=0; //ffv = form financed value
 ffv+=form.financed.value;
 fmv=0; //fmv = form months value
 fmv+=form.months.value;
 if (i > 1.0) {
     i=(i/100.0);
    var fmt=""; //fmt = form months total
  fmt+=i;
   fmt=fmt.substring(0,6);  
  }
 
 i/=12;
 var isn=1;
 for (var j=0;j<form.months.value;j++)
  isn=isn*(1+i);
  form.payment.value=(form.financed.value*isn*i)/(isn-1);
  //fpv = form payment value
 fpv=0;
  fpv+=form.payment.value;
  var fmt="";
  var add=9;
  fmt+=form.payment.value;

  for (var i=0;i<fmt.length;i++) {
   if (fmt.charAt(i)==".") {
    add=i+3;
    i=fmt.length;
    }
   }
  
 fmt=fmt.substring(0,add);  
 fcalc=((fmv*fpv)-ffv);
  var fmtb="";
  var add=9;
  fmtb+=fcalc;
  
 for (var i=0;i<fmtb.length;i++) {
   if (fmtb.charAt(i)==".") {
    add=i+3;
    i=fmtb.length;
    }
   }
 
 form.salary.value=(form.payment.value*12.0000001/.08)//.08
 var fmtc="";
 var addc=9
 fmtc+=form.salary.value;
 
 for (var i=0;i<fmtc.length;i++) {
   if (fmtc.charAt(i)==".") {
    addc=i+1;
    i=fmtc.length
    }
 }

 fmtb=fmtb.substring(0,add);
 fmtc=fmtc.substring(0,addc);
  form.payment.value="   $"+fmt+"";   
  form.interests.value="   $"+fmtb+"";
  form.salary.value=" "+fmtc+"00";
 
 if (fmt<50) {
 for (var p=120;p>0;p--) {
    p=p*(1+i);
     var trx=(form.financed.value*p*i) / (p-1);
         if (trx>=50) {
           break;
 }
 }
 //form.months.value=((form.financed.value+fmtb)/50);
 //form.months.value = Math.round((Math.round(form.financed.value) + Math.round(fmtb)) / 50);
 form.months.value = Math.round(-Math.log(1-((form.rate.value/12)/100)*form.financed.value/50) / Math.log(1+((form.rate.value/12)/100)));
 form.payment.value="50.00"; 
 form.interests.value = form.payment.value*form.months.value-form.financed.value;
 alert("Even though this calculated monthly payments of $"+fmt+", federal regulations require you to make monthly payments of at least $50.00.");
 } 
}

 function reset_it(form) {
  form.months.value="120";
  form.rate.value="6.8";
  form.financed.value="";
  form.payment.value="";
  putFocus();
 }

 function alrt_msg(entry,low,high,prompt) {
  prompt=" "+prompt +" entry is out of bounds. "+entry.value;
  var scratch=entry.value;
  for (var i=0;i<scratch.length;i++) {
   var letter=scratch.substring(i,i+1);
   if ((letter<"0" || "9"<letter) && letter!='.') {
     alert(prompt);
    return false;
    }
  }
 
 var errtst=parseFloat(scratch)
  if (errtst<low || high<errtst) {
   alert("Sorry. "+prompt+ " is not a possible value for the Stafford Loan program.  Enter a value "
   +"between "+low+" to "+high+".  (Don't enter commas.)");
   return false;
  }
 
 entry.value=scratch;
 return true;
 }

