/*
Sushi's number formatter
- for all checkbox <input> tags in the first form on page, added onclick="calcTotal();" attribute for auto-calculation
- <input> field containing total value should be defined below in user-definable vars
- currently only called by 'servicesBP.html'
*/
function formatTotal() {

	//------- begin user-definable vars

	var resultfield = 'grandtotal' ; // name of result field
	var hourspermonth = 150;	// number of hours in a month

	//------- end user-definable vars

 	var total = calcTotal();
	var months = Math.ceil(eval(total / hourspermonth));
	document.forms[0].elements[resultfield].value = total.toString() + ' hours or ' + months.toString() + ' months';

}

/*
Sushi's score calculator with ranges
- for all checkbox <input> tags in the first form on page
- currently only called by 'servicesSet.html'
*/
function calcScore() {
	var results = Array();	// no touchy

	//------- begin user-definable vars

	// name of <input> field that the score will appear in
	var scorefieldname = 'scorefield';

	// name of <textarea> or <input> field that the display text will appear in
	var	displayfieldname = 'displayfield';

	// format: results[x] = Array(start of range, end of range, 'text to display');
	// all apostrophes (') need to be escaped with a \, i.e. "What\'s up! How\'s it?"
	results[0] = Array(  0, 10, 'result  0 - 10: alskdjfalskdfjalksdjflkasdjf l');
	results[1] = Array( 11, 20, 'result 11 - 20: asdlkjf aljf alkdsjf ;akljs ');
	results[2] = Array( 21, 30, 'result 21 - 30: you suck');
	results[3] = Array( 31, 40, 'result 31 - 40: you\'re ok');
	results[4] = Array( 41, 50, 'result 41 - 50: you\'re better than ok');
	results[5] = Array( 51, 60, 'result 51 - 60: blah!');
	results[6] = Array( 61, 70, 'result 61 - 70: in school, this would be a "D"');
	results[7] = Array( 71, 80, 'result 71 - 80: very good, on this test');
	results[8] = Array( 81, 90, 'result 81 - 90: excellent!');
	results[9] = Array( 91, 100, 'result 91 - 100: super-genius anti-socialite nerd!');
	results[10] = Array( 101, 99999, 'result 100 - 99999: off the charts');
	results[11] = Array( -99999, -1, 'result negative: you must be undead');

	//------- end user-definable vars

    var score = document.forms[0].elements[scorefieldname].value = parseInt(calcTotal());

	for (var i = 0; i < results.length; i++) {	// find the correct range
		if (score >= parseInt(results[i][0]) && score <= parseInt(results[i][1])) {
			document.forms[0].elements[displayfieldname].value = results[i][2];
			break;
		}
	}

}

// Sushi's total-ator for the first form on the page
// - should work for any input type on the form that contains only a number (i.e. no alpha or whitespace)
function calcTotal() {
	if (!document.getElementsByTagName) return;
    var itemarray = document.forms[0].getElementsByTagName();
	var total = 0;

	for (var i = 0; i < document.forms[0].elements.length; i++) {
		if (document.forms[0].elements[i].value != '' && isNumeric(document.forms[0].elements[i])) {
			if (document.forms[0].elements[i].checked) {
				total += parseInt(document.forms[0].elements[i].value);
			}
		}
	}

	return (total);
}

// Returns true if the string passed in is a valid number
// (no alpha characters), else it displays an error message
function isNumeric(objField) {
	var whitespace = " \t\n\r";	// whitespace characters

	var strField = new String(objField.value);
	if (isWhitespace(strField)) return true;
	for (var i = 0; i < strField.length; i++) {
		if (strField.charAt(i) < '0' || strField.charAt(i) > '9') return false;
	}
	return true;

	function isWhitespace (s) {
		// Is s empty?
		if ((s == null) || (s.length == 0)) return true;
		for (var i = 0; i < s.length; i++) {
			if (whitespace.indexOf(s.charAt(i)) == -1) return false;
		}
		return true;	// All characters are whitespace.
	}
}
