var validationFunctions = new Object();
validationFunctions["numeric"] = isnumeric;

function isPattern(formField,pattern) {
	var pattern = pattern || formField.getAttribute('pattern');
	var regExp = new RegExp("^"+pattern+"$","");
	var correct = regExp.test(formField.value);
	if (!correct && formField.getAttribute('patternDesc'))
		correct = formField.getAttribute('patternDesc');
	return correct;
}
function isnumeric(formField) {
	return isPattern(formField,"\\d+");
}  

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) != ".")) {
      isNum = false;
      }
    }
  if ((isNum == false) || (toCheck.length == 0) || (toCheck == null)) {
  alert("Please enter valid numerical data in all fields.");
  return false;
  }
  else {
  return true;
  }
} 
function clearForm(form) {
var elm;
	elements = form.getElementsByTagName('input');
	for( i=0, elm; elm=elements.item(i++); )
	{
		if (elm.getAttribute('type') == "text")
		{
			elm.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 computeForm(form) {
if (checkForm(form.salary.value) && checkForm(form.otherIncome.value) && checkForm(form.propertyTaxes.value) && checkForm(form.hazardInsurance.value) && checkForm(form.autoPayment.value) && checkForm(form.creditCards.value) && checkForm(form.yearsInTerm.value) && checkForm(form.interestRate.value)){
    housingRatioResult = Math.round(housingRatio(form.salary.value, form.otherIncome.value, form.propertyTaxes.value, form.hazardInsurance.value));
    
    debtRatioResult = Math.round(debtRatio(form.salary.value, form.otherIncome.value, form.propertyTaxes.value, form.hazardInsurance.value, form.autoPayment.value, form.creditCards.value));
    
      if (housingRatioResult>debtRatioResult) {
      form.qualifyPayment.value = debtRatioResult
      }
      else {
      form.qualifyPayment.value = housingRatioResult
      }
      
    var a = form.interestRate.value;
    var b = form.yearsInTerm.value;
    var c = form.qualifyPayment.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);
    var k = parseFloat(c * j);
    form.qualifyAmount.value = Math.round(k);
   }
return;
}
