// ==================
// CONSTANTS
// ==================
var ARR_RLAT = [
	{ text: "Never", value: 0 },
	{ text: "Rarely", value: 0.25 },
	{ text: "Sometimes", value: 0.5 },
	{ text: "Often", value: 0.75 },
	{ text: "Always", value: 1 }
];

var ARR_STAFF_NUMS = [ 1,2,3,4,5,10,15,20,30,50,70,150,300,500,1000,2000,5000 ];
var ARR_HRS_PWK = [ 1,2,3,4,5,6,8,10,15,20,30,40 ];
var ARR_AVG_WAGE = [ 10000,20000,30000,40000,50000,60000,80000,100000,125000,150000,200000 ];
var ARR_COMMENT_ID = [
	"comment_RLAT_R",
	"comment_RLAT_L",
	"comment_RLAT_A",
	"comment_RLAT_T",
	"comment_totalRLAT",
	"comment_Mgr",
	"comment_TotalPwk",
	"comment_netGainPwk",
	"comment_netGainPyr"
];

// ==================
// GLOBAL VARIABLES
// ==================
var g_totalRlat = 0;				// total rlat

var g_totalMgrPwk = 0;				// total cost of manager time p/week
var g_totalOtherPwk = 0;			// total cost of other staff time p/week
var g_totalCostPwk = 0;				// total cost p/week (total cost of manager + total cost of other staff)
var g_totalROIPwk = 0;				// total roi p/week

var g_totalCostPyr = 0;				// total cost p/year
var g_totalROIPyr = 0;				// total roi p/year


// ==================
// FUNCTIONS
// ==================

function showInfo(id) {
	for (var i = 0; i < ARR_COMMENT_ID.length; i++) {
		var comment_id = ARR_COMMENT_ID[i];
		var obj = document.getElementById(comment_id);
		if (!obj) continue;
		
		// hide other comments
		if (comment_id != id) {
			obj.style.display = "none";
			continue;
		}
		
		// toggle the current comment
		var obj_display = obj.style.display;
		obj.style.display = (obj_display == "none") ? "block" : "none";
	}
}

// print the value in thousand format (insert commas every thousand)
function thousandFormat(value) {
	var str_out = "";
	var counter = 0;
	var abs_value = Math.abs(value);
	var arr_str_value = new String(abs_value).split("");
	
	// need to insert comma every thousand
	for (var i = arr_str_value.length - 1; i >= 0; i--) {
		str_out = arr_str_value[i] + str_out;
		counter++;
		
		// insert the comma
		if ((i > 0) && (counter >= 3)) {
			str_out = "," + str_out;
			counter = 0;
		}
	}
	
	// add the minux sign
	if (value < 0) {
		str_out = "-" + str_out;
	}
	
	return str_out;
}

// print the value in dollar format
function dollarFormat(value) {
	var str_out = thousandFormat(value);

	// set the negative sign
	if (value < 0) {
		str_out = "-$" + str_out.substr(1);
	} else {
		str_out = "$" + str_out;
	}
	
	return str_out;
}

// populate rlat combo box
function populateRLATComboBox(id) {
	// get the combo box
	var sel_obj = document.getElementById(id);
	if (!sel_obj) return;
	
	// create the values for combo box
	for (var i = 0; i < ARR_RLAT.length; i++) {
		var rlat_elem = ARR_RLAT[i];
		
		// create the value
		var opt_obj = document.createElement("option");
		opt_obj.value = rlat_elem.value;
		opt_obj.text = rlat_elem.text;
		
		// add the value to combo box
		try {
			sel_obj.add(opt_obj, null);	// standards compliant
		} catch (e) {
			sel_obj.add(opt_obj);		// IE only
		}
	}
}

// populate the combox box from given list of values
function populateComboBox(id, arr_elems, dollar_format) {
	// get the combo box
	var sel_obj = document.getElementById(id);
	if (!sel_obj) return;
	
	// create the values for combo box
	for (var i = 0; i < arr_elems.length; i++) {
		var elem = arr_elems[i];
		
		// create the value
		var opt_obj = document.createElement("option");
		opt_obj.value = elem;
		opt_obj.text = (dollar_format) ? dollarFormat(elem) : thousandFormat(elem);
		
		// add the value to combo box
		try {
			sel_obj.add(opt_obj, null);	// standards compliant
		} catch (e) {
			sel_obj.add(opt_obj);		// IE only
		}
	}
}

// calculate potential values
function calculatePotentialValues() {
	// reset the total value
	g_totalRlat = 0;
	
	// relation
	var sel_obj = document.getElementById("RLAT_R");
	if (sel_obj) {
		var value = parseFloat(sel_obj.options[sel_obj.selectedIndex].value, 10);
		if (!isNaN(value)) {
			g_totalRlat += (value * 0.2);
		}
	}

	// learning
	var sel_obj = document.getElementById("RLAT_L");
	if (sel_obj) {
		var value = parseFloat(sel_obj.options[sel_obj.selectedIndex].value, 10);
		if (!isNaN(value)) {
			g_totalRlat += (value * 0.2);
		}
	}
	
	// alignment
	var sel_obj = document.getElementById("RLAT_A");
	if (sel_obj) {
		var value = parseFloat(sel_obj.options[sel_obj.selectedIndex].value, 10);
		if (!isNaN(value)) {
			g_totalRlat += (value * 0.2);
		}
	}
	
	// traction
	var sel_obj = document.getElementById("RLAT_T");
	if (sel_obj) {
		var value = parseFloat(sel_obj.options[sel_obj.selectedIndex].value, 10);
		if (!isNaN(value)) {
			g_totalRlat += (value * 0.4);
		}
	}
	
	// show the total value
	obj = document.getElementById("totalRLAT");
	if (obj) obj.innerHTML = Math.round(g_totalRlat * 100) + "%";
}

// calculate the total cost of manager time
function calculateCostPwkMgr() {
	var wage = 0;
	var hours = 0;
	var staff = 0;
	
	// reset the total cost
	g_totalMgrPwk = 0;

	// get senior manager wage
	var sel_obj = document.getElementById("MgrAvgWage");
	if (sel_obj) {
		wage = parseInt(sel_obj.options[sel_obj.selectedIndex].value, 10);
	}
	
	// get senior manager hour
	sel_obj = document.getElementById("MgrHoursPwk");
	if (sel_obj) {
		hours = parseInt(sel_obj.options[sel_obj.selectedIndex].value, 10);
	}
	
	// get senior manager staff numbers
	sel_obj = document.getElementById("MgrStaffNums");
	if (sel_obj) {
		staff = parseInt(sel_obj.options[sel_obj.selectedIndex].value, 10);
	}
	
	// calculate the total (round to nearest integer)
	g_totalMgrPwk = ((wage / 48) / 40) * hours * staff;
	
	// show the total
	var obj = document.getElementById("costPwkMgr");
	if (obj) obj.innerHTML = dollarFormat(Math.round(g_totalMgrPwk));
}

// calculate the total cost of other staff time
function calculateCostPwkOther() {
	var wage = 0;
	var hours = 0;
	var staff = 0;
	
	// reset the total cost
	g_totalOtherPwk = 0;

	// get other staff wage
	var sel_obj = document.getElementById("OtherAvgWage");
	if (sel_obj) {
		wage = parseInt(sel_obj.options[sel_obj.selectedIndex].value, 10);
	}
	
	// get other staff hour
	sel_obj = document.getElementById("OtherHoursPwk");
	if (sel_obj) {
		hours = parseInt(sel_obj.options[sel_obj.selectedIndex].value, 10);
	}
	
	// get other staff staff numbers
	sel_obj = document.getElementById("OtherStaffNums");
	if (sel_obj) {
		staff = parseInt(sel_obj.options[sel_obj.selectedIndex].value, 10);
	}
	
	// calculate the total (round to nearest integer)
	g_totalOtherPwk = ((wage / 48) / 40) * hours * staff;
	
	// show the total
	var obj = document.getElementById("costPwkOther");
	if (obj) obj.innerHTML = dollarFormat(Math.round(g_totalOtherPwk));
}

// calculate total cost per week
function calculateCostPwkTotal() {
	g_totalCostPwk = g_totalMgrPwk + g_totalOtherPwk;
	
	var obj = document.getElementById("costPwkTotal");
	if (obj) obj.innerHTML = dollarFormat(Math.round(g_totalCostPwk));
}

// calculate roi cost per week
function calculateCostPwkROI() {
	if (g_totalCostPwk == 0) {
		g_totalROIPwk = 0;
	} else {
		if (g_totalRlat < 0.5) {
			g_totalROIPwk = g_totalRlat * g_totalCostPwk;
		} else if (g_totalRlat < 0.85) {
			g_totalROIPwk = (((g_totalRlat - 0.5) * 1.43) + 0.5) * g_totalCostPwk;
		} else {
			g_totalROIPwk = ((g_totalRlat * (20 / 3)) - ((1400 / 3) / 100)) * g_totalCostPwk;
		}
	}
	
	var obj = document.getElementById("costPwkROI");
	if (obj) obj.innerHTML = dollarFormat(Math.round(g_totalROIPwk));
}

// calculate total cost per year
function calculateCostPyrTotal() {
	g_totalCostPyr = g_totalCostPwk * 48;
	
	var obj = document.getElementById("costPyrTotal");
	if (obj) obj.innerHTML = dollarFormat(Math.round(g_totalCostPyr));
}

// calculate total roi per year
function calculateCostPyrROI() {
	g_totalROIPyr = g_totalROIPwk * 48;
	
	var obj = document.getElementById("costPyrROI");
	if (obj) obj.innerHTML = dollarFormat(Math.round(g_totalROIPyr));
}

// calculate net gain per week
function calculateNetGainPwk() {
	var net_gain = 0;
	
	if ((g_totalCostPwk + g_totalROIPwk) == 0) {
		net_gain = 0;
	} else {
		net_gain = g_totalROIPwk - g_totalCostPwk;
	}
	
	var obj = document.getElementById("netGainPwk");
	if (obj) obj.innerHTML = dollarFormat(Math.round(net_gain));
}

// calculate net gain per year
function calculateNetGainPyr() {
	var net_gain = 0;
	
	if ((g_totalCostPyr + g_totalROIPyr) == 0) {
		net_gain = 0;
	} else {
		net_gain = g_totalROIPyr - g_totalCostPyr;
	}
	
	var obj = document.getElementById("netGainPyr");
	if (obj) obj.innerHTML = dollarFormat(Math.round(net_gain));
}

// calculate cost
function calculateCost() {
	// calculate potential values
	calculatePotentialValues();
	
	// calculate cost per week
	calculateCostPwkMgr();
	calculateCostPwkOther();
	calculateCostPwkTotal();
	calculateCostPwkROI();
	
	// calculate cost per year
	calculateCostPyrTotal();
	calculateCostPyrROI();
	
	// calculate net gains
	calculateNetGainPwk();
	calculateNetGainPyr();
}
