/* The top three functions are for Mortcalc1 style sheet.*/	
function 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 = parseFloat(str)
    if (num < min || max < num) {
      alert(msg + " not in range [" + min + ".." + max + "]");
      return false;
    }
    input.value = str;
    return true;
}
  
function computeField(input) {
  if (input.value != null && input.value.length != 0)
    input.value = "" + eval(input.value);
  computeForm(input.form);
}
  
function computeForm(form) {
  if ((form.payments.value == null || form.payments.value.length == 0) ||
      (form.RATE.value == null || form.RATE.value.length == 0) ||
      (form.LIST_PRICE.value == null || form.LIST_PRICE.value.length == 0)) {
      return;
  }
  
  if (!checkNumber(form.payments, 1, 40, "# of years") ||
      !checkNumber(form.RATE, .001, 99, "Interest") ||
      !checkNumber(form.LIST_PRICE, 100, 10000000, "Loan Amount")) {
      form.payment.value = "Invalid";
      return;
  }
  
  var i = form.RATE.value / 100.0 / 12;
  //if (i > 1.0) {
  //  i = i / 100.0;
  //}
  //i /= 12;
  var pow = 1;
  for (var j = 0; j < (12*form.payments.value); j++)
    pow = pow * (1 + i);
  form.payment.value = Math.round(((form.LIST_PRICE.value - form.DP.value) * pow * i) / (pow - 1)*100)/100
}

	
function calculate() {
  var month_int = document.temps.interest.value / 1200;
  var base = 1;
  var mbase = 1 + month_int;
  for (i=0; i<document.temps.years.value * 12; i++) {
    base = base * mbase
  }
  document.temps.prin_int.value = floor(document.temps.loanamt.value * month_int / ( 1 - (1/base)))
  document.temps.month_tax.value = floor(document.temps.loanamt.value * .0125 / 12)
  document.temps.month_insur.value = floor(document.temps.loanamt.value *.0035 / 12)
  var total = (document.temps.loanamt.value * month_int / ( 1 - (1/base))) +
    (document.temps.loanamt.value * .0125 / 12) +
  	(document.temps.loanamt.value *.0035 / 12);
  document.temps.month_payment.value = floor(total);
}

/* This functions is used for both Mortcalc2 & 3 style sheet.*/
function floor(number) {
	return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}

/* The three functions below are for Mortcalc3 style sheet.*/	
function updateLoan() {
	var PP = document.fcalc.PP.value;
	var DP = document.fcalc.DP.value;
	var LA = floor(PP - DP);
	var AT = floor(PP * .0125);
	var AI = floor(PP * .0035);
	document.fcalc.LA.value = LA;
	document.fcalc.AT.value = AT;
	document.fcalc.AI.value = AI;
	dosum();
}

function dosum() {
	var mi = document.fcalc.IR.value / 1200;
	var base = 1;
	var mbase = 1 + mi;
	for (i=0; i<document.fcalc.YR.value * 12; i++) {
		base = base * mbase
	}
	document.fcalc.PI.value = floor(document.fcalc.LA.value * mi / ( 1 - (1/base)))
	document.fcalc.MT.value = floor(document.fcalc.AT.value / 12)
	document.fcalc.MI.value = floor(document.fcalc.AI.value / 12)
	var dasum = document.fcalc.LA.value * mi / ( 1 - (1/base)) +
	document.fcalc.AT.value / 12 +
	document.fcalc.AI.value / 12;
	document.fcalc.MP.value = floor(dasum);
}

function updateFlds() {
	menuNum = document.fcalc.mortgage.selectedIndex;
	menuVal = document.fcalc.mortgage.options[menuNum].value
	substring = menuVal.split(",");
	var v0 = substring[0].split(","); 
	var v1 = substring[1].split(","); 

	document.fcalc.YR.value = v0;
	document.fcalc.IR.value = v1;

	dosum();
}

