function ltrim(str) { 
	var re = /\s*((\S+\s*)*)/; 
	return str.replace(re, '$1'); 
} 

function rtrim(str) { 
	var re = /((\S+\s*)*)\s*/; 
	return str.replace(re, '$1'); 
} 

function trim(str) { 
	return rtrim(ltrim(str)); 
} 

function empty(str) { 
	if (trim(str) == '') { 
		return true; 
	} 
	return false; 
} 

function get_sum(fObj, aFields) { 
	var sum = 0; 
	for (var i = 0; i < aFields.length; i++) {
		eval('var num = fObj.'+ aFields[i] +'.value;'); 
		if (empty(num)) { 
			continue; 
		} else if (isNaN(num)) { 
			continue; 
		} else { 
			sum = sum + parseFloat(num); 
		} 
	}
	return sum.toFixed(0); 
} 

function calculate(fObj, what) { 

	// Total Personnel. 
	if (what == 'cost_personnel_total') { 
		var aFields = Array(
			'cost_personnel_gomc', 
			'cost_personnel_matching', 
			'cost_personnel_federal'
    ); 
		fObj.cost_personnel_total.value = get_sum(fObj, aFields); 
		return true; 
	// Total Fringe. 
	} else if (what == 'cost_fringe_total') { 
		var aFields = Array(
			'cost_fringe_gomc', 
			'cost_fringe_matching', 
			'cost_fringe_federal'
    ); 
		fObj.cost_fringe_total.value = get_sum(fObj, aFields); 
		return true; 
	// Total Travel. 
	} else if (what == 'cost_travel_total') { 
		var aFields = Array(
			'cost_travel_gomc', 
			'cost_travel_matching', 
			'cost_travel_federal'
    );
		fObj.cost_travel_total.value = get_sum(fObj, aFields); 
		return true; 
	// Total Equipment. 
	} else if (what == 'cost_equipment_total') { 
		var aFields = Array(
			'cost_equipment_gomc', 
			'cost_equipment_matching', 
			'cost_equipment_federal'
    );
		fObj.cost_equipment_total.value = get_sum(fObj, aFields); 
		return true; 
	// Total Supplies. 
	} else if (what == 'cost_supplies_total') { 
		var aFields = Array(
			'cost_supplies_gomc', 
			'cost_supplies_matching', 
			'cost_supplies_federal'
    );
		fObj.cost_supplies_total.value = get_sum(fObj, aFields); 
		return true; 
	// Total Contractual. 
	} else if (what == 'cost_contractual_total') { 
		var aFields = Array(
			'cost_contractual_gomc', 
			'cost_contractual_matching', 
			'cost_contractual_federal'
    );
		fObj.cost_contractual_total.value = get_sum(fObj, aFields); 
		return true; 
	// Total Other. 
	} else if (what == 'cost_other_total') { 
		var aFields = Array(
			'cost_other_gomc', 
			'cost_other_matching', 
			'cost_other_federal'
    );
		fObj.cost_other_total.value = get_sum(fObj, aFields); 
		return true; 
	// Overall Total GOMC. 
	} else if (what == 'cost_total_gomc') { 
		var aFields = Array(
			'cost_personnel_gomc', 
			'cost_fringe_gomc', 
			'cost_travel_gomc', 
			'cost_equipment_gomc', 
			'cost_supplies_gomc', 
			'cost_contractual_gomc', 
			'cost_other_gomc'
    );
		fObj.cost_total_gomc.value = get_sum(fObj, aFields); 
		return true; 
	// Overall Total Matching. 
	} else if (what == 'cost_total_matching') { 
		var aFields = Array(
			'cost_personnel_matching', 
			'cost_fringe_matching', 
			'cost_travel_matching', 
			'cost_equipment_matching', 
			'cost_supplies_matching', 
			'cost_contractual_matching', 
			'cost_other_matching'
    );
		fObj.cost_total_matching.value = get_sum(fObj, aFields); 
		return true; 
	// Overall Total Other Federal. 
	} else if (what == 'cost_total_federal') { 
		var aFields = Array(
			'cost_personnel_federal', 
			'cost_fringe_federal', 
			'cost_travel_federal', 
			'cost_equipment_federal', 
			'cost_supplies_federal', 
			'cost_contractual_federal', 
			'cost_other_federal'
    );
		fObj.cost_total_federal.value = get_sum(fObj, aFields); 
		return true; 
	// Overall Total. 
	} else if (what == 'cost_total_grand') { 
		var aFields = Array(
			'cost_total_gomc', 
			'cost_total_matching', 
			'cost_total_federal'
    );
		fObj.cost_total_grand.value = get_sum(fObj, aFields); 
		return true; 
	}

} 

function big_calculation(fObj) { 
	var aFields = new Array(
		'cost_personnel_total', 
		'cost_fringe_total', 
		'cost_travel_total', 
		'cost_equipment_total', 
		'cost_supplies_total', 
		'cost_contractual_total', 
		'cost_other_total', 
		'cost_total_gomc', 
		'cost_total_matching', 
		'cost_total_federal', 
		'cost_total_grand'
	);
	for (var i = 0; i < aFields.length; i++) {
		calculate(fObj, aFields[i]); 
	} 
} 