function validateMoney(moneyValue)
{
	if(!moneyValue.match(/^[0-9]{1,}\.[0-9]{1,2}$/) && !moneyValue.match(/^[0-9]{1,}$/))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function validatePercent(percentValue)
{
	if(!percentValue.match(/^[0-9]{1,}\.[0-9]{1,2}$/) && !percentValue.match(/^[0-9]{1,}$/))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function validateInteger(integerValue)
{
	if(!integerValue.match(/^[0-9]{1,}$/))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function displayCommas(theValue)
{
	theValue += '';
	x = theValue.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;
}

function displayTwoDecimals(theFloat)
{
	var theFloat2 = theFloat.toString();
	var theFloatReturn = "";
	var theFloatSplit = theFloat2.split(".");
	if(theFloatSplit.length != 2)
	{
		theFloatReturn = theFloat2 + '.00';
	}
	else
	{
		var theFloatDecimal = theFloatSplit[1];
		if(theFloatDecimal.length == 0)
		{
			theFloatReturn = theFloat2 + '.00';
		}
		else if(theFloatDecimal.length == 1)
		{
			theFloatReturn = theFloat2 + '0';
		}
		else
		{
			theFloatReturn = theFloat2;
		}
	}
	
	return theFloatReturn;
}

function roundToDecimals(num, dec)
{
	var result = Math.round(num*Math.pow(10, dec))/Math.pow(10, dec);
	return result;
}

function calculatePayment()
{
	var price = document.estimator.price.value;
	var down = document.estimator.down.value;
	var rate = document.estimator.rate.value;
	var tax = document.estimator.tax.value;
	var trade = document.estimator.trade.value;
	var months = document.estimator.months.value;
	var priceResult = validateMoney(price);
	var downResult = validateMoney(down);
	var rateResult = validatePercent(rate);
	var taxResult = validatePercent(tax);
	var tradeResult = validateMoney(trade);
	var monthsResult = validateInteger(months);
	var errorString = new Array();
	
	if(!priceResult)
	{
		errorString.push('Price');
	}
	if(!downResult)
	{
		errorString.push('Down Payment');
	}
	if(!rateResult)
	{
		errorString.push('Rate');
	}
	if(!taxResult)
	{
		errorString.push('Tax');
	}
	if(!tradeResult)
	{
		errorString.push('Trade In');
	}
	if(!monthsResult)
	{
		errorString.push('Loan Term');
	}
	if(errorString.length > 0)
	{
		alert('There is an error with your entered values. Please check the following fields: ' + errorString.join(', ') + ' (numbers and decimals only).');
	}
	else
	{
		if(months == 0)
		{
			months = 1;
		}
		rate = rate/(12*100);
		tax = tax/100;
		if(tax != 0)
		{
			tax = price * tax;
		}
		var subtotal = parseFloat(price) + parseFloat(tax);
		var credits = parseFloat(down) + parseFloat(trade);
		var difference = subtotal - credits;
		var uno = 1.0 + parseFloat(rate);
		var dos = 0 - parseInt(months);
		var bottom = 1 - Math.pow(uno, dos);
		if(rate == 0)
		{
			var monthly = difference/months;
		}
		else
		{
			var monthly = difference * (rate/bottom);
		}
		var total = monthly * months;
		var outsubtotal = roundToDecimals(subtotal, 2);
		var outcredits = roundToDecimals(credits, 2);
		var outtotal = roundToDecimals(total, 2);
		var outmonthly = roundToDecimals(monthly, 2);
		document.getElementById('subtotal').innerHTML = displayCommas(displayTwoDecimals(outsubtotal));
		document.getElementById('credits').innerHTML = displayCommas(displayTwoDecimals(outcredits));
		document.getElementById('total').innerHTML = displayCommas(displayTwoDecimals(outtotal));
		document.getElementById('monthly').innerHTML = displayCommas(displayTwoDecimals(outmonthly));
	}
}
