

/*
 * Format a number with comma separations for thousands (n,nnn,nnn.nn)
 * The number is rounded to the number of decimal points specified by 'decimals'.
 * If the input number doesn't have a fractional portion, no decimals are returned.
 */

function roiFormatNumber(val, decimals) {
	
	var roundingFactor = 1.0;
	var dtmp = decimals;
	while(dtmp > 0) {
		roundingFactor *= 10;
		dtmp--;
	}

	/*
	 * This rounds of the number to the desired number of decimals and converts the result to a string.
	 * The rounding works like this:
	 * Math.round() rounds a number to the nearest integer. In order to get some decimal points, we need
	 * to multiply the input with 10 once for each decimal, round off the reminding decimals and then
	 * divide by 10 once for each decimal.
	 * For example:
	 * If your input is 101.52 and you specified 1 decimal, we would multiply 101.52 by 10 = 1015.2.
	 * Math.round(1015.2) returns 1015.
	 * 1015 / 10 comes out to 101.5 .
	 *
	 * We don't actually multiply by 10 repeatedly below. This is instead handled by calculating the
	 * rounding factor above.
	 */ 
	var base = '' + Math.round(val * roundingFactor) / roundingFactor;

	/*
	 * split off the decimals and save for later:
	 */
	var dec = '';
	if(base.indexOf('.') >= 0) {
		var ind = base.indexOf('.');
		dec = base.substring(ind+1);
		base = base.substring(0, ind);
	}

	var result = '';

	/*
	 * Add a comma for each multiple of 1000:
	 */
	while(base.length > 3) {
		ind = base.length % 3;
		if(ind == 0)
			ind = 3;
		result = result + base.substring(0, ind) + ',';
		base = base.substring(ind);
	}
	result = result + base;

	/* Tack on the decimals: */
	if(dec != '') {
		while(dec.length < decimals) {
			dec = '' + dec + '0';
		}
		result = result + '.' + dec;
			
	}

	return result;
}


/*
 * roiCalculate:
 * Validates input and performs the calculations.
 */
function roiCalculate() {

	/*
	 * Retrieve all the inputs:
	 */
	var firmInitials = document.forms['roiForm'].firmInitials.value;
	var avgRate  = parseFloat(document.forms['roiForm'].avgRate.value);
	var countConsultants = parseInt(document.forms['roiForm'].countConsultants.value);


	/* Verify the hourly rate: */
	var check = '' + avgRate;
	if(check == 'NaN') {
		window.alert('Please enter a numeric value for your average billing rate');
		return;
	}

	/* Verify number of consultants: */
	check = '' + countConsultants;
	if(check == 'NaN') {
		window.alert('Please enter a numeric value for your number of FTE consultants on staff');
		return;
	}


	/*
	 * Calculate yearly ROI:
	 * The formula is average rate * number of consultants * 12 months * 4 hours.
	 * This corresponds to a 2.5% increase because you're adding one billed (working) half-day per month.
	 * Since there are about 20 working days in a month, adding 1, 8 hour day is a 5% increase.
	 */
	var result = avgRate * countConsultants * 12 * 4;
	var resultYR2 = result * 1.035; // Add 3.5% for year 2
	var resultYR3 = resultYR2 * 1.035;
	var resultYR4 = resultYR3 * 1.035;


	/*
	 * Format the numbers:
	 */
	var resultString = roiFormatNumber(result, 0);
	var resultStringYR2 = roiFormatNumber(resultYR2, 0);
	var resultStringYR3 = roiFormatNumber(resultYR3, 0);
	var resultStringYR4 = roiFormatNumber(resultYR4, 0);

	// Display the 'Print' button:
	var printButton = document.getElementById('roiPrintButton');
	if(printButton != null)
		printButton.style.display = 'inline';


	// Display the 'title' portion of the result:
	var outputTitle = document.getElementById('roiResult');
	if(outputTitle != null)
		outputTitle.style.display = 'block';

	// Show the acronym in the last line. If an acronym wasn't given, it will be substituted for 'your'
	// to result in "your bottom line" instead of "XYZ's bottom line".
	var acronymOut = document.getElementById('roiAcronymOutput');
	if(acronymOut != null) {
		if((firmInitials == null) || (firmInitials.length == 0)) {
			acronymOut.innerHTML = 'your ';
		} else {
			acronymOut.innerHTML = firmInitials + '\'s ';
		}
	}


	// Display the 'bottom line impact':
	var outputField = document.getElementById('roiResultValue');
	if(outputField != null) {
		/* Un-comment the following line and comment out the next line to display the result in a div
		 * instead of in a text box.
		 * NOTE!!! This also requires a change to roi.html !!!
		 */
		//outputField.innerHTML = '$' + resultString;
		outputField.value = '$' + resultString;
	}

	// Display year 2 and on
	var outputYear2 = document.getElementById('roiResultYear2');
	if(outputYear2 != null) {
		outputYear2.innerHTML = '$' + resultStringYR2;
	}

	var outputYear3 = document.getElementById('roiResultYear3');
	if(outputYear3 != null) {
		outputYear3.innerHTML = '$' + resultStringYR3;
	}

	var outputYear4 = document.getElementById('roiResultYear4');
	if(outputYear4 != null) {
		outputYear4.innerHTML = '$' + resultStringYR4;
	}
}


/*
 * Clear the form, hide the results, print button and result blurb.
 */
function roiClear() {
	document.forms['roiForm'].firmInitials.value = '';
	document.forms['roiForm'].avgRate.value = '';
	document.forms['roiForm'].countConsultants.value = '';

	var outputBlock = document.getElementById('roiResult');
	if(outputBlock != null)
		outputBlock.style.display = 'none';

	var printButton = document.getElementById('roiPrintButton');
	if(printButton != null)
		printButton.style.display = 'none';
	var outputField = document.getElementById('roiResultValue');
	if(outputField != null)
		outputField.innerHTML = '&nbsp;';
}
