function datatype (int,amt,pmt,dpt) {
 this.int = int;
 this.amt = amt;
 this.pmt = pmt;
 this.dpt = dpt;
}


function MaxMortgage(form) {
 with (document.calculator) {
   base = new datatype(int.value, amt.value, pmt.value, dpm.value);
 }
 checkvals (base);

 with (base) {
  effrate = Math.pow(1 + int/200, 1/6);
  maxmorg = (pmt * (1 - Math.pow(effrate, -12*amt))/(effrate - 1)) + dpt;
  maxmorg = (Math.round(maxmorg * 100)) / 100;

  document.calculator.hmc.value = "$" + prettyPrint(maxmorg.toString());
  } 
}


function checkvals(obj) {
 obj.int = parseFloat(obj.int); 
 obj.amt = parseFloat(obj.amt);
 obj.pmt = parseFloat(obj.pmt);
 obj.dpt = parseFloat(obj.dpt);

 if (isNaN(obj.amt))  {
  alert ("Amortization required.");
  obj.amt = 25;
  document.calculator.amt.value = obj.amt;
 }
 if (isNaN(obj.int))  {
  alert ("Interest required.");
  obj.int = 5.5;
  document.calculator.int.value = obj.int;
 }
 
 if (isNaN(obj.pmt)) {
  alert ("Payment required.");
  obj.pmt = 1000;
  document.calculator.pmt.value = obj.pmt;
 } 
 else if (obj.pmt < 100) {
    alert ("$100 minimum payment");
    obj.pmt = 100;
    document.calculator.pmt.value = obj.pmt;
   } 
 if (obj.int <= 0) {
   alert ("Interest must be > 0.");
   obj.int = 5.50;
   document.calculator.int.value = obj.int;
 }
 if (obj.int > 30) {
  alert ("30% Max. interest.");
  obj.int = 30;
  document.calculator.int.value = obj.int; 
 }
 if (obj.amt < 1) {
   alert ("1 year minimum amortization.");
   obj.amt = 1;
   document.calculator.amt.value = obj.amt;
 }
 if (obj.amt > 30) {
  alert("Maximum mortgage period is 30 years.");
  obj.amt = 30;
  document.calculator.amt.value = obj.amt;
 }
 return (obj)
} 

function prettyPrint(val) {
  
  var decpos = val.indexOf (".");
  var endpos = val.length;
 
  if (decpos == -1) { 
    val = val + ".00";
	return (val);
  }
  if (endpos-decpos == 2) {
    val = val + "0";
	return (val);
  }
  if (endpos-decpos == 3) {
    return (val);
  }  
  else {
   alert ("Unexpected error.");
   return (val);
  }
}

function isInt(elm) {
var elmstr = elm.value + ""; 
    if (elmstr == "") return false;
    for (var i = 0; i < elmstr.length; i++) {
        if (elmstr.charAt(i) < "0" ||
            elmstr.charAt(i) > "9") {
        return false;
        }
    }
return true;
}