function clearForm(form) {
  form.income.value = "";
  form.other.value = "";
  form.taxes.value = "";
  form.insurance.value = "";
  form.auto.value = "";
  form.cards.value = "";
  form.term.value = "";
  form.rate.value = "";
  form.payment.value = "";
  form.amount.value = "";
  form.interest.value = "";
  form.housePrice.value = "";
  form.down.value = "";
}

function housingRatio(income, other, taxes, insurance) {
  housing = eval(income * .28) + eval(other * .28) - taxes - insurance;
  return housing;
}

function debtRatio(income, other, taxes, insurance, auto, cards) {
  debt = eval(income * .36) + eval(other * .36) - taxes - insurance - auto - cards;
  return debt;
}

function housePrice(amount, down) {
	price = eval(amount * 1 + down * 1);
	return price;
}

function checkForm(toCheck) {
  isNum = true;
  for (j = 0; j < toCheck.length; j++) {
    if (((toCheck.substring(j,j+1) < "0") || (toCheck.substring(j,j+1) > "9")) && (toCheck.substring(j,j+1) != ".") && (toCheck.substring(0,1) != "-")) {
      isNum = false;
    }
  }
  if ((isNum == false)) {
  alert("Please enter valid numerical data in all fields.");
  return false;
  }
  else { return true; }
}

function interest(payment, term, amount) { 
	interest2 = eval(payment * (term * 12)) - amount; 
	return interest2;
}

function computeForm(form) {
  if (checkForm(form.income.value) && checkForm(form.other.value) && checkForm(form.taxes.value) && checkForm(form.insurance.value) && checkForm(form.auto.value) && checkForm(form.cards.value) && checkForm(form.term.value) && checkForm(form.rate.value) && checkForm(form.down.value)) {

  form.income.value = Math.abs(form.income.value);
  form.other.value = Math.abs(form.other.value);
  form.taxes.value = Math.abs(form.taxes.value);
  form.insurance.value = Math.abs(form.insurance.value);
  form.auto.value = Math.abs(form.auto.value);
  form.cards.value = Math.abs(form.cards.value);
  form.term.value = Math.abs(form.term.value);
  form.rate.value = Math.abs(form.rate.value);
  form.down.value = Math.abs(form.down.value);
  
  if (form.rate.value > 25) {
  	form.payment.value = "";
  	form.amount.value = "";
  	form.interest.value = "";
  	form.housePrice.value = "";
  	alert('The Interest Rate cannot be higher than 25%!');
  } else {
    
	housingRatioResult = Math.round(housingRatio(form.income.value, form.other.value, form.taxes.value, form.insurance.value));

    debtRatioResult = Math.round(debtRatio(form.income.value, form.other.value, form.taxes.value, form.insurance.value, form.auto.value, form.cards.value));

    if (housingRatioResult > debtRatioResult)
	    form.payment.value = debtRatioResult + ".00";
    else 
    	form.payment.value = housingRatioResult + ".00";

    var a = form.rate.value;
    var b = form.term.value;
    var c = form.payment.value;
    var d = parseFloat(a / 1200);
    var e = parseFloat(b * 12);
    var f = parseFloat(1 + d);
    var g = parseFloat(Math.pow(f, e));
    var h = parseFloat(1 / g);
    var i = parseFloat(1 - h);
    var j = parseFloat(i / d);
		if (isNaN(j))
			j = 0;
    var k = parseFloat(c * j);
	var l = form.amount.value;
    form.amount.value = Math.round(k) * 1 + ".00";
	form.interest.value = Math.round(interest(c, b, form.amount.value)) + ".00";
	form.housePrice.value = housePrice(form.amount.value, form.down.value) * 1 + ".00"; 

	if (form.amount.value < 0)
		form.amount.value = 0;
	if (form.payment.value < 0)
		form.payment.value = 0;
	if (form.interest.value < 0)
		form.interest.value = 0;
	if (form.housePrice.value < 0)
		form.housePrice.value = 0;
	}
 }
return;
}


