/**
 * Get the value from the given field (stripped of commas and pound signs)
 */
function getCleanValue (field)
{
	var value = getValue(field);
	value = value.replace ('£', '');
	value = value.replace (',', '');
	return Number(value);
}

/**
 * Format the given number by adding commas and rounding to the given number of decimal places
 */
function numberFormat (number, decimalPlaces)
{
	var num = new Number (number);
	return num.toFixed (decimalPlaces);
}

function sumFixedCosts ()
{
	var fixedCostFields = new Array ('Labour', 'Depreciation', 'Office_space', 'Management', 'Copy_requests', 'Other_fixed_costs');
	
	var total = 0;	
	for (var field in fixedCostFields) {
		total += Number(getCleanValue(fixedCostFields[field]));
	}
	
	return total;
}

function sumVariableCosts ()
{
	var variableCostFields = new Array ('Paper', 'Envelopes', 'Toner_etc', 'Other_variable_costs');
	
	var total = 0;	
	for (var field in variableCostFields) {
		total += Number(getCleanValue(variableCostFields[field]));
	}
	
	return total;
}

function changeField (field)
{
	// Validate field
	if (fieldOK (field, 'Money', field.replace('_', ' '), '')) {
		calculateTotals();				// Show totals
	}
}

function calculateTotals()
{
	var docCount = getCleanValue ('Document_count');
	
	// Calculate & display fixed costs
	var fixedCosts = sumFixedCosts ();
	var fixedCostsObj = getObject ('fixedCosts');
	fixedCostsObj.innerHTML = '£' + numberFormat(fixedCosts, 2);

	// Calculate & display variable costs
	var variableCostPerDoc = sumVariableCosts ();
	var variableCostPerDocObj = getObject ('variableCostPerDoc');
	variableCostPerDocObj.innerHTML = '£' + numberFormat(variableCostPerDoc, 2);

	// Calculate & display total costs ex postage
	var costsExPostage = (docCount * variableCostPerDoc) + Number(fixedCosts);
	var costsExPostageObj = getObject('totalCostsExPostage');
	costsExPostageObj.innerHTML = '£' + numberFormat(costsExPostage, 2);
		
	// Calculate & display total costs per doc ex-postage
	var costsPerDoc = costsExPostage / docCount;
	var costsPerDocObj = getObject('costsPerDocExPostage');
	costsPerDocObj.innerHTML = '£' + numberFormat(costsPerDoc, 2);

	// Calculate & display total postage 
	var stamps = getCleanValue ('Stamps');
	var totalPostageObj = getObject('totalPostage');
	var totalPostage = stamps * docCount;
	totalPostageObj.innerHTML = '£' + numberFormat(totalPostage, 2);
	
	// Calculate & display total monthly costs 
	var totalCosts = costsExPostage + totalPostage;
	var totalCostsObj = getObject('totalCosts');
	totalCostsObj.innerHTML = '£' + numberFormat(totalCosts, 2);
	
	// Calculate & display total monthly costs  per doc
	var totalCostPerDoc = totalCosts / docCount;
	var totalCostPerDocObj = getObject('totalCostPerDoc');
	totalCostPerDocObj.innerHTML = '£' + numberFormat(totalCostPerDoc, 2);
	
	// Calculate & display total monthly costs for a range of percentage posts
	var quarterPostObj = getObject('quarterPost');
	quarterPostObj.innerHTML = '£' + numberFormat(totalPostage * 0.25, 2);	
	var halfPostObj = getObject('halfPost');
	halfPostObj.innerHTML = '£' + numberFormat(totalPostage * 0.5, 2);	
	var threequartersPostObj = getObject('threequartersPost');
	threequartersPostObj.innerHTML = '£' + numberFormat(totalPostage * 0.75, 2);	
	var fullPostObj = getObject('fullPost');
	fullPostObj.innerHTML = '£' + numberFormat(totalPostage, 2);	

}