function checkForZero(field){ 
  if (field.value == 0 || field.value.length == 0) {
    alert ("This field can't be 0!");
    field.focus(); 
  } else calculatePayment(field.form);
}

function cmdCalc_Click(form){ 
  if (form.price.value == 0 || form.price.value.length == 0) { 
    alert ("The Price field can't be 0!");
    form.price.focus(); 
  } else if (form.ir.value == 0 || form.ir.value.length == 0) {
    alert ("The Interest Rate field can't be 0!"); 
    form.ir.focus();
  } else if (form.term.value == 0 || form.term.value.length == 0) {
    alert ("The Term field can't be 0!");
    form.term.focus();
  } else calculatePayment(form);
}

function calculatePayment(form){

  princ = form.price.value - form.dp.value;
  intRate = (form.ir.value/100) / 12;
  months = form.term.value * 12;
  
  $('principle').value = addCommas(princ);
  
  if (form.type.value == 'r') {
	  pmtv = (Math.floor((princ*intRate)/(1-Math.pow(1+intRate,(-1*months)))*100)/100);
	  $('pmt').value = addCommas(pmtv);
	  $('payments').value = addCommas(months);
	  $('totalAmount').value = addCommas((months * pmtv));
	} else {
	  $('pmt').value = (Math.floor(princ * (intRate * 12) * (1/12) * 100))/100;
	  $('payments').value = 'N/A';
	  $('totalAmount').value = 'N/A';
	}
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

