/* Support Javascript for Stever Robbins's overload assessment.
   (c) 2005 by Stever Robbins. All rights reserved in all media.
*/

var unitvalue  = 25
var unitsingle = "sushi dinner"
var unitplural =  "sushi dinners"


// from Javascript.internet.com:
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}
//

function parseIntZero(x) {
	returnedValue = parseInt(x)
	returnedValue = isNaN(returnedValue) ? 0 : returnedValue
	return (returnedValue)
}

function selectUnits(units) {

if		(units == "sushi") {
		document.getElementById("sushiButton").checked = true
		unitvalue  = 25
		unitsingle ="sushi dinner"; unitplural = "sushi dinners"
		}
else if (units == "caribbean") {
		document.getElementById("caribButton").checked = true
		unitvalue  = 1200
		unitsingle = "Caribbean vacation"; unitplural = "Caribbean vacations"
		}
else if (units == "familydays") {
		document.getElementById("familyButton").checked = true
		unitvalue  = 1000
		unitsingle = "day with your family"; unitplural = "days with your family" 
		}
else	{ // "other" units
		document.getElementById("faveOtherButton").checked = true
		unitvalue  = parseIntZero(document.getElementById("othercost").value)
		unitsingle = document.getElementById("otherdescrip").value
		unitplural = unitsingle + "s"
		}
}

function minsToTextPrefix(xMins,hoursPerWeek,unitsPrefix) {
	xHours = Math.floor(xMins/60)
	xMins  = xMins- xHours * 60

	xWeeks = Math.floor(xHours/hoursPerWeek)
	xHours = xHours - xWeeks * hoursPerWeek

	xDays  = Math.floor(xHours / (hoursPerWeek/5))
	xHours = xHours - xDays * (hoursPerWeek/5)

	xYears = Math.floor(xWeeks/52)
	xWeeks = xWeeks - xYears * 52

	txt = ""
	unitsPrefix = "&nbsp;" + unitsPrefix
	if (xYears != 0) { txt += xYears + unitsPrefix + "year"   + (xYears != 1 ? "s, " : ", ") }
	if (xWeeks != 0) { txt += xWeeks + unitsPrefix + "week"   + (xWeeks != 1 ? "s, " : ", ") }
	if (xDays  != 0) { txt += xDays  + unitsPrefix + "day"    + (xDays  != 1 ? "s, " : ", ") }
	if (xHours != 0) { txt += xHours + unitsPrefix + "hour"   + (xHours != 1 ? "s, " : ", ") }
	if (xMins  != 0) { txt += xMins  + unitsPrefix + "minute" + (xMins  != 1 ? "s, " : ", ") }
	txt = txt.substring(0, txt.length - 2)
	return (txt)
}

function minsToText(xMins,hoursPerWeek) {
	return minsToTextPrefix(xMins,hoursPerWeek,"")
}


function computeEverythingPrefix(numEmployees,unitsPrefix) {
	numEmployees       = parseIntZero(numEmployees)
	hoursWorkedPerWeek = parseIntZero(document.getElementById("weekhours").value)
	yearlySalary       = parseIntZero(document.getElementById("yearlysalary").value)
	
	// Calculated the paid minutes wasted in each day, in each week, and over the course of the year
	minsPerDay   = parseIntZero(document.getElementById("hours").value) * 60 + parseIntZero(document.getElementById("mins").value)
	minsPerDay   = minsPerDay  * numEmployees
	minsPerWeek  = minsPerDay  * 5
	minsPerYear  = minsPerWeek * 52
	
	// Calculate the salary per minute (note: assuming all vacation time is paid time)
	minutesWorkedPerYear = 52 * hoursWorkedPerWeek * 60
	minuteSalary = yearlySalary / minutesWorkedPerYear
	dollarValueOfWaste = Math.round(minsPerYear * minuteSalary)

	// Give them the waste report in dollars and cents
	document.getElementById("wastedtimeperweek").innerHTML  = minsToTextPrefix(minsPerWeek, hoursWorkedPerWeek, unitsPrefix)
	document.getElementById("wastedtimeperyear").innerHTML  = minsToTextPrefix(minsPerYear, hoursWorkedPerWeek, unitsPrefix)
	document.getElementById("dollarvalueofwaste").innerHTML = formatCurrency(dollarValueOfWaste)

	// Give them the waste report in terms of what really matters (e.g. sushi dinners)
	if (document.getElementById("unitexplanation")) {
		unitValueOfWaste   = Math.floor(dollarValueOfWaste / unitvalue * 100) / 100
		unitValueOfWaste   = (unitValueOfWaste < 1) ? Math.round(unitValueOfWaste * 100) + "% of a " + unitsingle : unitValueOfWaste + "&nbsp;" + unitplural;
		unitExplanation    = "In terms that mean the most to you (a " + unitsingle + " at " + formatCurrency(unitvalue) + "), "
		unitExplanation   += "the time you're spending futzing with your email "
		unitExplanation   += "is worth<br /><span id=\"unitamount\">" + unitValueOfWaste + "!</span>"
	    document.getElementById("unitexplanation").innerHTML     = unitExplanation;
	    document.getElementById("unitexplanation").style.display = "";
		}
}

function computeEverything(numEmployees) {
	return computeEverythingPrefix(numEmployees,"")
}
							
function nextSection(button,nextId) {
    document.getElementById(nextId).style.display = 'block';
	button.style.display = 'none';
}
