   function toggle_visibility(element_id) {
      if (document.getElementById(element_id).style.visibility == "visible") {
         document.getElementById(element_id).style.visibility = "hidden";
      } else {
         document.getElementById(element_id).style.visibility = "visible";
      }
   }
      
   function calculate(){
      var principal = document.loandata.principal.value;
      var payments = document.loandata.years.value * 12;
      if (document.loandata.interest.value < 1){
         var interest = document.loandata.interest.value / 12
      }
      else{
         var interest = document.loandata.interest.value / 100 / 12
      }
      principal -= document.loandata.downpayment.value;
      
      var x = Math.pow(1 + interest, payments);
      var monthly = (principal*x*interest)/(x-1);
      
      if (!isNaN(monthly) &&
         (monthly != Number.POSITIVE_INFINITY) &&
         (monthly != Number.NEGATIVE_INFINITY)) {
         
         document.loandata.payment.value = round(monthly);
         document.loandata.total.value = round(monthly * payments);
         document.loandata.totalinterest.value =
            round((monthly * payments) - principal);
         }
         
         else {
            document.loandata.payment.value = "";
            document.loandata.total.value = "";
            document.loandata.totalinterest.value = "";
            }
      }
         
   function round(x) {
      return Math.round(x*100)/100;
   }

