var sDownTotal;
var sTradeInTotal;
function cleanUpValue(valueToClean) {
	return setDecPlaces(valueToClean, 2, true);
}

function setDecPlaces(numStr, newDecPlaces, addTrailingZerosFlag) {

  numStr = numStr.toString(); // make sure numStr is a string
  var decIndex = numStr.indexOf('.');
  var decPlaces = 0;
  if (decIndex != -1) {
    decPlaces = numStr.length - decIndex - 1;
  }
  
  if (newDecPlaces == 0 && decIndex != -1) {
    numStr = numStr.substring(0, decIndex); // strip decimal point
  } else if (decPlaces > newDecPlaces) {
    numStr = numStr.substring(0, (decIndex + newDecPlaces + 1)); // remove decimal places
  } else if (decPlaces < newDecPlaces && addTrailingZerosFlag) { // add trailing zeros if necessary
    if (decIndex == -1) {
      numStr = numStr + "."; // add a decimal point if there isn't one
    }
    for (; decPlaces < newDecPlaces; decPlaces++) { // add enough zeros such that decPlaces = newDecPlaces
      numStr = numStr + "0";
    }
  }

  return NullToZero(numStr);
}

function NullToZero(theNumber) {
  if (theNumber == null || theNumber == "" || isNaN(theNumber)) {
    return "0";
  } else {
    return theNumber;
  }
}
function cleanAll(f,x) {
	f.payOff.value = cleanUpValue(NullToZero(f.payOff.value));
	f.tradeIn.value = cleanUpValue(NullToZero(f.tradeIn.value));
	f.salesPrice.value = cleanUpValue(NullToZero(f.salesPrice.value));
	f.addtionalPrice.value = cleanUpValue(NullToZero(f.addtionalPrice.value));
	f.interestRate.value = NullToZero(f.interestRate.value);
	f.paymentLength.value = NullToZero(f.paymentLength.value);
	f.dollarDown.value = cleanUpValue(NullToZero(f.dollarDown.value));
	f.percentDown.value = NullToZero(f.percentDown.value);
	setTrade(f);
	setDown(f,x);
}
function setDown(f, x) {

	if (x == "dollar") {
		f.percentDown.value = "0";
		f.dollarDown.value = cleanUpValue(NullToZero(f.dollarDown.value));
		var sAmountDownTotal = parseFloat(f.dollarDown.value);
		sDownTotal = parseFloat(sAmountDownTotal);
	} else if (x == "percent") {
		f.dollarDown.value = "0.00";
		var sPercentDownTotal = parseFloat(f.percentDown.value);
		sDownTotal = parseFloat(f.salesPrice.value) * (parseFloat(sPercentDownTotal) / 100);
	} else if (f.dollarDown.value != "0.00") {
		var sAmountDownTotal = parseFloat(f.dollarDown.value);
		sDownTotal = parseFloat(sAmountDownTotal);
	} else if (f.percentDown.value != "0") {
		var sPercentDownTotal = parseFloat(f.percentDown.value);
		sDownTotal = parseFloat(f.salesPrice.value) * (parseFloat(sPercentDownTotal) / 100);		
	} else {
		sDownTotal = 0;
	}
	f.DownTotal.value = cleanUpValue(sDownTotal);
}
function setTrade(f) {
	sTradeInTotal = parseFloat(f.tradeIn.value) - parseFloat(f.payOff.value);
	f.TradeInTotal.value = cleanUpValue(sTradeInTotal);
}
function DoCalculator(f) {
	var valid = true;
	var payOff = f.payOff;
	var tradeIn = f.tradeIn;
	var salesPrice = f.salesPrice;
	var addtionalPrice = f.addtionalPrice;
	var interestRate = f.interestRate;
	var paymentLength = f.paymentLength;
	var dollarDown = f.dollarDown;
	var percentDown = f.percentDown;
	cleanAll(f);
	var errors = "";
	if(salesPrice.value <= 0) {
		errors += "- Sales Price\n";
		valid = false;
	}
	if(interestRate.value <= 0) {
		errors += "- Interest Rate\n";
		valid = false;
	}
	if(paymentLength.value <= 0) {
		errors += "- Length in Months\n";
		valid = false;
	}
	if(!valid) {
		alert("You need to enter information into the following field(s) before your estimated monthly payment can be calculated:\n\n" + errors);
		return;
	}

	var sSalesTotal = parseFloat(salesPrice.value) + parseFloat(addtionalPrice.value) - parseFloat(sDownTotal) - parseFloat(sTradeInTotal);
	var sMonthlyPayment = (parseFloat(interestRate.value) / 100 / 12) * parseFloat(sSalesTotal)
	var sMonthlyPayment = parseFloat(sMonthlyPayment) / (1 - Math.pow(((parseFloat(interestRate.value) / 100 / 12 + 1)), -parseFloat(paymentLength.value)));
	
	if (parseFloat(sMonthlyPayment) > 0) {
		f.MonthlyPayment.value = cleanUpValue(parseFloat(sMonthlyPayment));
		f.AmountFinanced.value = cleanUpValue(sSalesTotal);
	} else {
		f.MonthlyPayment.value = "0.00";
		f.AmountFinanced.value = "0.00";
	}
}
