
//----------------------------------------------------------------------------------------------------
// Takes the number of transactions and amount per transactions
// from the calculator input text boxes and calculates the cost for each
// type of transaction system. Then displays the answer.
//----------------------------------------------------------------------------------------------------
function calculateSavings() {
		var transactions = parseFloat(document.getElementById('calcTransactions').value);
		var amountPer = parseFloat(document.getElementById('calcAvgamount').value);
		
		var creditCostPer = (0.0222 * amountPer) + 0.30;
		var checkCostPer = 0;
		
		if(transactions > 2500) {
			checkCostPer = 1.31;
		}
		else if(transactions >200) {
			checkCostPer = 1.36;
		}
		else {
			checkCostPer = 1.41;
		}
		
		var creditTotal = transactions*creditCostPer;
		var checkTotal = transactions*checkCostPer;
		
		document.getElementById('resultCreditTransactions').value = addCommas(transactions) + '';
		document.getElementById('resultCheckTransactions').value = addCommas(transactions) + '';
		
		document.getElementById('resultCreditCostPer').value = '$' + addCommas(Math.round(creditCostPer*1000)/1000);
		document.getElementById('resultCheckCostPer').value = '$' + addCommas(Math.round(checkCostPer*1000)/1000);
		
		document.getElementById('resultCreditTotal').value = '$' + addCommas(parseInt(creditTotal));
		document.getElementById('resultCheckTotal').value = '$' + addCommas(parseInt(checkTotal));
		
		setFontSize('resultCreditTotal', creditTotal);
		setFontSize('resultCheckTotal', checkTotal);
		setFontSize('resultTotalSavings', creditTotal-checkTotal);
		
		if(creditTotal < checkTotal) {
			document.getElementById('resultCreditTotal').style.color = '#18572D';
			document.getElementById('resultCheckTotal').style.color = '#792B27';
			document.getElementById('resultTotalSavings').value = '$0';
		}
		else {
			document.getElementById('resultCreditTotal').style.color = '#792B27';
			document.getElementById('resultCheckTotal').style.color = '#18572D';
			document.getElementById('resultTotalSavings').value = '$' + addCommas(parseInt(creditTotal - checkTotal));
		}
	}
	
	//------------------------------------------------------------------------------------------------------------
	// Sets the appropriate font size of the element based on how big the 
	// number is.
	//------------------------------------------------------------------------------------------------------------
	function setFontSize(id, number) {
		if(number > 1000000) {
			document.getElementById(id).style.fontSize = '30px';
		}
		else if(number > 100000000) {
			document.getElementById(id).style.fontSize = '20px';
		}
		else if(number > 1000000000) {
			document.getElementById(id).style.fontSize = '10px';
		}
		else {
			document.getElementById(id).style.fontSize = '40px';
		}
	}
	
	//--------------------------------------------------------------------------------------------------------------
	// Adds commas to the passed in number
	//--------------------------------------------------------------------------------------------------------------
	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;
	}