function pmt(rate, nper, pv, fv) {
  var rVal;

  if (rate==0) {
    rVal=-(fv + pv)/nper;
  } else {
    ir=Math.pow(1 + rate,nper);
    rVal=-((rate * (fv + ir * pv))/(ir-1));
  }
  return rVal;
}

function cleanNumber(strNum) {
  if (!strNum) return strNum;
  strNum = strNum.replace(/[\$\,%]/g,'');
  return strNum;
}

function format(value, lead, sep) {
  var strValue = new String(value);
  var len = strValue.length;
  var n;
  var strRet = '';
  var ctChar = 3 - (len%3);

  if (ctChar == 3) ctChar =0;

  for (n=0; len > n; n++) {
    if (ctChar == 3) {
      strRet += sep;
      ctChar = 0;
    }
    ctChar++;
    strRet += strValue.substring(n,n+1);
  }

  if (lead == '%') {
    return strRet + lead;
  } else {
    return lead + strRet;
  }
}

function numFormat(elem, lead, sep) {
  if (elem.value == '') {
    elem.value = format('0', lead, sep);
    return true;
  }

  var value = parseInt(cleanNumber(elem.value), 10);

  if (lead == '$') {
    if (0 > value) {
      alert('You have exceeded the range for the price.\nPlease check your information and try again.');
      value = 0;
    }

    if (value > 100000000) {
      alert('You have exceeded the range for the price.\nPlease check your information and try again.');
      value = 100000000;
    }
  }

  if (isNaN(value)) {
    alert('You have entered an incorrect character on this field. \nPlease check your information and try again.');
    elem.value = format('0', lead, sep);
    elem.focus();
    return false;
  }
  elem.value = format(value, lead, sep);
  return true;
}

function dpCalc(dp,pr) {
  var price = pr;
  //var percent = dp;
  var rvalue = 0;

  rvalue = price * (dp/100);
  return rvalue;
}

function calcRate(lfform, fErrors, cid) {
  if (lfform == null) return;

  var price = cleanNumber(lfform.ap.value);
  var priceErr = false;

  var dpIndex = lfform.dp.selectedIndex;
  var dpValue = lfform.dp.options[dpIndex].value;
  var downpayment;

  // find the type of downpayment
  var j=lfform.dpt.length;
  for (i=0; i<j; i++){
    if(lfform.dpt[i].checked)  var radioVal = lfform.dpt[i].value
  }

  if(radioVal == 'd') {
    downpayment = cleanNumber(lfform.dpd.value);
  } else {
    downpayment = dpCalc(dpValue,price);
  }

  var intIndex = lfform.ir.selectedIndex;
  var intValue = lfform.ir.options[intIndex].value;
  var numpayments = 360; // 30 Years Fixed Mortgage

  var elin1 = null;
  if(lfform.elin1 != null) {
    elin1 = lfform.elin1.value;
  }

  var elin2 = null;
  if(lfform.elin2 != null) {
    elin2 = lfform.elin2.value;
  }

  numFormat(lfform.ap,'$',',');

  if ((price == 0) && (fErrors != 0)) {
    alert('Please enter an asking price that is greater than zero.');
    lfform.ep.value = '';

    if(lfform.elout1 != null) {
      lfform.elout1.value = '';
    }

    if(lfform.elout2 != null) {
      lfform.elout2.value = '';
    }
  } else {
    var payment = parseInt(-pmt(intValue/1200, numpayments, price - downpayment,0));

    var paymentEl1 = null;
    if(elin1 != null) {
      paymentEl1 = parseInt(-pmt(elin1/1200, numpayments, price - downpayment,0));
    }

    var paymentEl2 = null;
    if(elin2 != null) {
      paymentEl2 = parseInt(-pmt(elin2/1200, numpayments, price - downpayment,0));
    }

    if (payment < 0) {
      lfform.ep.value = '';
      if(elout1 != null) {
        lfform.elout1.value = '';
      }

      if(elout2 != null) {
        lfform.elout2.value = '';
      }
    } else {
      lfform.ep.value = payment;
      numFormat(lfform.ep,'$',',');

      if(paymentEl1 != null) {
        lfform.elout1.value = paymentEl1;
        numFormat(lfform.elout1,'$',',');
      }

      if(paymentEl2 != null) {
        lfform.elout2.value = paymentEl2;
        numFormat(lfform.elout2,'$',',');
      }
    }
  }
}

  function calcRateAndUpdate(lfform, fErrors, cid, url, calc) {
    calcRate(lfform, fErrors);

    var price = cleanNumber(lfform.ap.value);

    var dpIndex = lfform.dp.selectedIndex;
    var dpValue = lfform.dp.options[dpIndex].value;

    var intIndex = lfform.ir.selectedIndex;
    var intValue = lfform.ir.options[intIndex].value;

    // find the type of downpayment
    var j=lfform.dpt.length;
    for (i=0; i<j; i++){
      if(lfform.dpt[i].checked)  var radioVal = lfform.dpt[i].value
    }

    var dpAmt  = cleanNumber(lfform.dpd.value);
    var dpRate = dpValue;

    var type;
    if(radioVal == 'd') {
      type = 2;
    } else {
      type = 1;
    }

    //AJAX call to update client defaults
    if (cid != null) {
//      setCalcValuePrefs(cid, type, dpRate, dpAmt, intValue, url);
    }
  }

  function setCalcValuePrefs(id, type, dpRate, amt, intRate, url)
	{
		//var url = 'http://yourserver/app/get_sales';
    var pars = 'id=' + id + '&type=' + type + '&dpRate=' + dpRate + '&amt=' + amt + '&intRate=' + intRate;

    var myAjax = new Ajax.Request(
			url,
			{
				method: 'get',
				parameters: pars
			});

	}


