//
// Filename:	utils.js
// Description:	Provides support for client-side activities
//

// roundToTwo(num)
// Rounds number to two deceimal places
function roundToTwo(num) { 
	num -= 0; 
	num = (Math.round(num*100))/100; 
	return (num == Math.floor(num))? num + '.00' : ( (num*10 == Math.floor(num*10))? num + '0' : num); 
}

// calculateTotal()
// Calculates subtotal and total values for Office Space Calculator
function calculateTotal() {
	var subtotal = 0;
	var total = 0;
	var size;
	var qty;

	// Define array of line items
	var lineItems = ['ExecutiveOffices','MiddleManagementOffices','OpenSpace','SmallConferenceRoom','LargeConferenceRoom','Receptionist','SmallReception','LargeReception','ServerRoom','TelecomRoom','Kitchen','OtherArea1','OtherArea2'];

	// Iterate over line items and calculate/display subtotals and total
	for ( i = 0; i < lineItems.length; i++ ) {
		// Get size and quantity values
		size = document.getElementById(lineItems[i] + 'Size').value;
		qty = document.getElementById(lineItems[i] + 'Qty').value;

		// If size and quantity are both valid numbers, calculate subtotal
		if ( (size != '') && (qty != '') && (! isNaN(size)) && (! isNaN(qty)) ) {
			subtotal = size * qty;
		} else {
			subtotal = 0;
		}

		// Display subtotal and add to running total
		document.getElementById(lineItems[i] + 'Total').innerHTML = subtotal;
		total = total + subtotal;
	}

	// Calculate and Display totals
	document.getElementById('SubtotalDisplay').innerHTML = total;
	document.getElementById('Subtotal').value = total;

	document.getElementById('CirculationFactorDisplay').innerHTML = roundToTwo(total * 0.15);
	document.getElementById('CirculationFactor').value = roundToTwo(total * 0.15);

	document.getElementById('TotalUsableDisplay').innerHTML = roundToTwo(total * 1.15);
	document.getElementById('TotalUsable').value = roundToTwo(total * 1.15);

	var lossFactorPercentage = document.getElementById('LossFactorPercentage');
	lossFactorPercentage = lossFactorPercentage.options[lossFactorPercentage.selectedIndex].value;
	document.getElementById('LossFactorDisplay').innerHTML = roundToTwo(total * 1.15 * lossFactorPercentage / 100);
	document.getElementById('LossFactor').value = roundToTwo(total * 1.15 * lossFactorPercentage / 100);

	document.getElementById('TotalDisplay').innerHTML = roundToTwo(total * 1.15 * (1 + (lossFactorPercentage / 100)));
	document.getElementById('Total').value = roundToTwo(total * 1.15 * (1 + (lossFactorPercentage / 100)));
}

// resetOscForm()
// Resets form values and totals on Office Space Calculator form
function resetOscForm() {
	document.getElementById('OscForm').reset();
	calculateTotal();
}

// showTab(idx, total)
// Shows specified tab's contents
function showTab(idx, total) {
	// Highlight selected tab, de-highlight others
	for ( i = 1; i <= total; i++ ) {
		// Get reference to current tab
		tab = document.getElementById('Tab' + i);

		// If current tab is selected, highlight.  Otherwise, de-highlight
		if ( i == idx ) {
			tab.className = tab.className.replace('tab', 'selectedTab');
		} else {
			tab.className = tab.className.replace('selectedTab', 'tab');
		}
	}

	// Show selected tab's contents.  Hide other contents.
	for ( i = 1; i <= total; i++ ) {
		// Get reference to current content
		tabContent = document.getElementById('tabContent' + i);

		// If current tabContent is selected, show.  Otherwise, hide
		if ( i == idx ) {
			tabContent.style.display = 'block';
		} else {
			tabContent.style.display = 'none';
		}
	}

}

// setColor(str, clr)
// Sets specified tab's text color to specified color
function setColor(str, clr) {
	var ele = document.getElementById(str + 'Tab').childNodes[0];
	ele.style.color = '#' + clr;
}

//
// setFilename(src)
//
// Sets the value of the Filename field using src field's value
//
function setFilename(src) {
	var value = src.value;
	var target = document.getElementById('Filename');

	// Replace white-space characters with underscore
	value = value.replace(/[\t\s]/g, "_");

	// Remove non-alphanumeric characters
	value = value.replace(/\W/g, "");

	// Set target value
	target.value = value;
}
