	// functions to control if the results will be in square metres or square feet
	var _mode = 1; // begins with metrer mode (default)
	var _metrer_mode = 1;
	var _feet_mode = 2;
	var SMALL = 0;
	var SMALL_TEXT = 'small';
	var MEDIUM = 1;
	var MEDIUM_TEXT = 'medium';
	var LARGER = 2;
	var LARGER_TEXT = 'larger';
	var CHART_WIDTH = 560;
	
	function getMode() {
		return _mode;
	}
	
	function changeMode(mode) {
		if (_mode != mode) {
			_mode = mode;
			updateInfoFrontOffice();
			updateInfoWorkSpaces();
			updateInfoAncillary();
		}
	}
	
	function getModeText(){
		var metrer_mode_text = 'metrer';
		var feet_mode_text = 'feet';
		
		if (_mode == _metrer_mode) {
			return metrer_mode_text;
		} else
		if (_mode == _feet_mode) {
			return feet_mode_text;
		}
	}
	
	// end of mode functions
	
	// metrerToFeet convertion
	function metrerToFeet(metrer) {	
		var FEET_BASE = 10.2354;
    var result = metrer * FEET_BASE;
		return result;
	}
	
	// basic function to add / remove rooms
	// used by all the rooms 
	function addRemoveRoom(reference, qtty) {
		if ((reference + qtty) >= 0) {
			return reference += qtty;
		}
		return 0;
	}
	
	// update the input with the value related with the recent added/removed element
	function updateInput(elementid, idx, reference) {
		if (idx == SMALL) {
			document.getElementById(elementid + SMALL_TEXT).value = reference[idx][TOTAL];
		} else if (idx == MEDIUM) {
			document.getElementById(elementid + MEDIUM_TEXT).value = reference[idx][TOTAL];
		} else {
			document.getElementById(elementid + LARGER_TEXT).value = reference[idx][TOTAL];
		}
	}


	var TOTAL = 0;
	var METRERS =  1;
	
	// FrontOffice functions
	// reception
	var _reception = [[0, 200],[0, 400],[0, 600]];
	function addRemoveReception(elementid, idx, qtty) {
		_reception[idx][TOTAL] = addRemoveRoom(_reception[idx][TOTAL], qtty);
		updateInput(elementid, idx, _reception);
		updateInfoFrontOffice();
	}
	
	// meeting rooms
	var _meetingrooms = [[0, 100],[0, 200],[0, 300]];
	function addRemoveMeetingRooms(elementid, idx, qtty) {
		_meetingrooms[idx][TOTAL] = addRemoveRoom(_meetingrooms[idx][TOTAL], qtty);
		updateInput(elementid, idx, _meetingrooms);
		updateInfoFrontOffice();
	}
	
	// dinning and entertainment rooms
	var _dinning = [[0, 50],[0, 100],[0, 200]];
	function addRemoveDinningRooms(elementid, idx, qtty) {
		_dinning[idx][TOTAL] = addRemoveRoom(_dinning[idx][TOTAL], qtty);
		updateInput(elementid, idx, _dinning);
		updateInfoFrontOffice();
	}
	
	// mail rooms
	var _mail = [[0, 50],[0, 100],[0, 200]];
	function addRemoveMailRooms(elementid, idx, qtty) {
		_mail[idx][TOTAL] = addRemoveRoom(_mail[idx][TOTAL], qtty);
		updateInput(elementid, idx, _mail);
		updateInfoFrontOffice();
	}
	
	// computer rooms
	var _computer = [[0, 50],[0, 100],[0, 200]];
	function addRemoveComputerRooms(elementid, idx, qtty) {
		_computer[idx][TOTAL] = addRemoveRoom(_computer[idx][TOTAL], qtty);
		updateInput(elementid, idx, _computer);
		updateInfoFrontOffice();
	}
	
	// computer rooms
	var _staff = [[0, 50],[0, 100],[0, 200]];
	function addRemoveStaffRooms(elementid, idx, qtty) {
		_staff[idx][TOTAL] = addRemoveRoom(_staff[idx][TOTAL], qtty);
		updateInput(elementid, idx, _staff);
		updateInfoFrontOffice();
	}
	
	// computer rooms
	var _libraries = [[0, 50],[0, 100],[0, 200]];
	function addRemoveLibrariesRooms(elementid, idx, qtty) {
		_libraries[idx][TOTAL] = addRemoveRoom(_libraries[idx][TOTAL], qtty);
		updateInput(elementid, idx, _libraries);
		updateInfoFrontOffice();
	}
	
	// showers rooms
	var _showers = [[0, 50],[0, 100],[0, 200]];
	function addRemoveShowersRooms(elementid, idx, qtty) {
		_showers[idx][TOTAL] = addRemoveRoom(_showers[idx][TOTAL], qtty);
		updateInput(elementid, idx, _showers);
		updateInfoFrontOffice();
	}
	
	// showers rooms
	var _otheroffice = [[0, 50],[0, 100],[0, 200]];
	function addRemoveOtherRooms(elementid, idx, qtty) {
		_otheroffice[idx][TOTAL] = addRemoveRoom(_otheroffice[idx][TOTAL], qtty);
		updateInput(elementid, idx, _otheroffice);
		updateInfoFrontOffice();
	}
	
	function getFrontOfficeTotal() {

		var result = 0;
		for (var i = 0; i <= 2; i++){
			result += _reception[i][TOTAL] * _reception[i][METRERS];
			result += _meetingrooms[i][TOTAL] * _meetingrooms[i][METRERS];
			result += _dinning[i][TOTAL] * _dinning[i][METRERS];
			result += _mail[i][TOTAL] * _mail[i][METRERS];
			result += _computer[i][TOTAL] * _computer[i][METRERS];
			result += _staff[i][TOTAL] * _staff[i][METRERS];
			result += _libraries[i][TOTAL] * _libraries[i][METRERS];
			result += _showers[i][TOTAL] * _showers[i][METRERS];
			result += _otheroffice[i][TOTAL] * _otheroffice[i][METRERS];
		}
		if (getMode() == _feet_mode) {
			result = metrerToFeet(result);
		}
		return result;
	}
	
	function updateInfoFrontOffice(){
		var elementName = "lblfrontoffice";
    var frontOfficeTotal = getFrontOfficeTotal().toFixed(2);
		document.getElementById(elementName).innerHTML = 'Front Office ' + 
						'<span>' + frontOfficeTotal + '</span> ' +
						'<small>square ' + getModeText() + '</small>';
		updateInfoTotal();
	}
	
	// end FrontOffice functions

	// Workspaces functions
	// plan workstations
	var _workstation = [[0, 200],[0, 400],[0, 600]];
	function addRemoveWorkstation(elementid, idx, qtty) {
		_workstation[idx][TOTAL] = addRemoveRoom(_workstation[idx][TOTAL], qtty);
		updateInput(elementid, idx, _workstation);
		updateInfoWorkSpaces();
	}
	
	// plan support areas
	var _support = [[0, 100],[0, 200],[0, 300]];
	function addRemoveSupportAreas(elementid, idx, qtty) {
		_support[idx][TOTAL] = addRemoveRoom(_support[idx][TOTAL], qtty);
		updateInput(elementid, idx, _support);
		updateInfoWorkSpaces();
	}
	
	// meeting spaces
	var _meeting = [[0, 50],[0, 100],[0, 200]];
	function addRemoveMeetingSpaces(elementid, idx, qtty) {
		_meeting[idx][TOTAL] = addRemoveRoom(_meeting[idx][TOTAL], qtty);
		updateInput(elementid, idx, _meeting);
		updateInfoWorkSpaces();
	}
	
	//closed offices for one person
	var _closedoffice = [[0, 50],[0, 100],[0, 200]];
	function addRemoveClosedOffice(elementid, idx, qtty) {
		_closedoffice[idx][TOTAL] = addRemoveRoom(_closedoffice[idx][TOTAL], qtty);
		updateInput(elementid, idx, _closedoffice);
		updateInfoWorkSpaces();
	}
	
	// other closed offices
	var _otherclosed = [[0, 50],[0, 100],[0, 200]];
	function addRemoveOtherClosed(elementid, idx, qtty) {
		_otherclosed[idx][TOTAL] = addRemoveRoom(_otherclosed[idx][TOTAL], qtty);
		updateInput(elementid, idx, _otherclosed);
		updateInfoWorkSpaces();
	}
	
	// paper filing areas
	var _paperfiling = [[0, 50],[0, 100],[0, 200]];
	function addRemovePaperFiling(elementid, idx, qtty) {
		_paperfiling[idx][TOTAL] = addRemoveRoom(_paperfiling[idx][TOTAL], qtty);
		updateInput(elementid, idx, _paperfiling);
		updateInfoWorkSpaces();
	}
	
	// other workspaces
	var _otherworkspaces = [[0, 50],[0, 100],[0, 200]];
	function addRemoveOtherWorkspaces(elementid, idx, qtty) {
		_otherworkspaces[idx][TOTAL] = addRemoveRoom(_otherworkspaces[idx][TOTAL], qtty);
		updateInput(elementid, idx, _otherworkspaces);
		updateInfoWorkSpaces();
	}
	
	function getWorkSpacesTotal() {

		var result = 0;
		for (var i = 0; i <= 2; i++){
			result += _workstation[i][TOTAL] * _workstation[i][METRERS];
			result += _support[i][TOTAL] * _support[i][METRERS];
			result += _meeting[i][TOTAL] * _meeting[i][METRERS];
			result += _closedoffice[i][TOTAL] * _closedoffice[i][METRERS];
			result += _otherclosed[i][TOTAL] * _otherclosed[i][METRERS];
			result += _paperfiling[i][TOTAL] * _paperfiling[i][METRERS];
			result += _otherworkspaces[i][TOTAL] * _otherworkspaces[i][METRERS];
		}
		if (getMode() == _feet_mode) {
			result = metrerToFeet(result);
		}
		return result;
	}
	
	function updateInfoWorkSpaces(){
		var elementName = "lblworkspaces";
    var workSpaceTotal = getWorkSpacesTotal().toFixed(2);
		document.getElementById(elementName).innerHTML = 'Work Spaces ' + 
						'<span>' + workSpaceTotal + '</span> ' +
						'<small>square ' + getModeText() + '</small>';
		updateInfoTotal();
	}
	
	// end WorkSpaces functions
	
	// Ancillary functions
	// on-site storage
	var _onsitestorage = [[0, 200],[0, 400],[0, 600]];
	function addRemoveSiteStorage(elementid, idx, qtty) {
		_onsitestorage[idx][TOTAL] = addRemoveRoom(_onsitestorage[idx][TOTAL], qtty);
		updateInput(elementid, idx, _onsitestorage);
		updateInfoAncillary();
	}
	
	// coffee/vending areas
	var _coffee = [[0, 100],[0, 200],[0, 300]];
	function addRemoveSupportAreas(elementid, idx, qtty) {
		_coffee[idx][TOTAL] = addRemoveRoom(_coffee[idx][TOTAL], qtty);
		updateInput(elementid, idx, _coffee);
		updateInfoAncillary();
	}
	
	// copying and stationery
	var _copying = [[0, 50],[0, 100],[0, 200]];
	function addRemoveCopying(elementid, idx, qtty) {
		_copying[idx][TOTAL] = addRemoveRoom(_copying[idx][TOTAL], qtty);
		updateInput(elementid, idx, _copying);
		updateInfoAncillary();
	}
	
	//first aid
	var _firstaid = [[0, 50],[0, 100],[0, 200]];
	function addRemoveFirstAid(elementid, idx, qtty) {
		_firstaid[idx][TOTAL] = addRemoveRoom(_firstaid[idx][TOTAL], qtty);
		updateInput(elementid, idx, _firstaid);
		updateInfoAncillary();
	}
	
	// other ancillary
	var _otherancillary = [[0, 50],[0, 100],[0, 200]];
	function addRemoveOtherAncillary(elementid, idx, qtty) {
		_otherancillary[idx][TOTAL] = addRemoveRoom(_otherancillary[idx][TOTAL], qtty);
		updateInput(elementid, idx, _otherancillary);
		updateInfoAncillary();
	}
	
	function getAncillaryTotal() {

		var result = 0;
		for (var i = 0; i <= 2; i++){
			result += _onsitestorage[i][TOTAL] * _onsitestorage[i][METRERS];
			result += _coffee[i][TOTAL] * _coffee[i][METRERS];
			result += _copying[i][TOTAL] * _copying[i][METRERS];
			result += _firstaid[i][TOTAL] * _firstaid[i][METRERS];
			result += _otherancillary[i][TOTAL] * _otherancillary[i][METRERS];
		}
		if (getMode() == _feet_mode) {
			result = metrerToFeet(result);
		}
		return result;
	}
	
	function updateInfoAncillary(){
		var elementName = "lblancillary";
    var ancillaryTotal = getAncillaryTotal().toFixed(2);
		document.getElementById(elementName).innerHTML = 'Ancillary Spaces ' + 
						'<span>' + ancillaryTotal + '</span> ' +
						'<small>square ' + getModeText() + '</small>';
		updateInfoTotal();
	}
	
	// end Ancillary functions
	
	function updateInfoTotal(){
		var elementName = "lbltotal";
		var total = getAncillaryTotal() + getFrontOfficeTotal() + getWorkSpacesTotal();
		document.getElementById(elementName).innerHTML = 'Total ' + 
						'<span>' + total.toFixed(2) + '</span> ' +
						'<small>square ' + getModeText() + '</small>';
		updateChart();
	}
	
	function getPercFrontOffice() {
		var total = getAncillaryTotal() + getFrontOfficeTotal() + getWorkSpacesTotal();
		return (getFrontOfficeTotal() / total * 100);
	}
	
	function 	getPercWorkSpaces() {
		var total = getAncillaryTotal() + getFrontOfficeTotal() + getWorkSpacesTotal();
		return (getWorkSpacesTotal() / total * 100);
	}
	
	function getPercAncillarySpaces() {
		var total = getAncillaryTotal() + getFrontOfficeTotal() + getWorkSpacesTotal();
		return (getAncillaryTotal() / total * 100);
	}
	
	function getPercChartWidth(perc) {
		return (perc * CHART_WIDTH / 100)
	}
	
	function updateChart() {
		var elementName = "chartbar";
		var total = getAncillaryTotal() + getFrontOfficeTotal() + getWorkSpacesTotal();
		if (total > 0) {
			showElement(elementName);
		} else {
			hideElement(elementName);
		}
		
		percFrontOffice = getPercChartWidth(getPercFrontOffice());
		percWorkSpaces = getPercChartWidth(getPercWorkSpaces());
		percAncillarySpaces = getPercChartWidth(getPercAncillarySpaces());
		updateElementWidth('chtfrontoffice', 0, percFrontOffice);
		updateElementWidth('chtworkspaces', Math.max(0, percFrontOffice - 11), percWorkSpaces);
		updateElementWidth('chtancillaryspaces', Math.max(0, percWorkSpaces + percFrontOffice - 22), percAncillarySpaces);
		updateChartLegend('legfrontoffice', getPercFrontOffice());
		updateChartLegend('legworkspaces', getPercWorkSpaces());
		updateChartLegend('legancillaryspaces', Math.abs((getPercFrontOffice() + getPercWorkSpaces()) - 100));
		
		updateGrossValue();
	}
	
	function updateChartLegend(elementId, perc) {
		element = document.getElementById(elementId);
		span = element.getElementsByTagName('span');
		span[0].innerHTML = perc.toFixed(2) + '%';
	}
	
	function updateElementWidth(elementId, marginLeft, width) {
		width = Math.min(width, CHART_WIDTH - 22);
		element = document.getElementById(elementId);
		element.style.width = (Math.round(width)) + 'px';
		element.style.marginLeft = (Math.round(marginLeft)) + 'px';
	}

	function hideElement(elementid){
		document.getElementById(elementid).style.display = 'none';
	}
	
	function showElement(elementid){
		document.getElementById(elementid).style.display = 'block';
	}
	
	function showIntroduction(){
		showElement('introduction');
		hideElement('frontoffice');
		hideElement('workspaces');
		hideElement('ancillary');
		hideElement('chart');
	}
	
	function showFrontOffice() {
		hideElement('introduction');
		showElement('frontoffice');
		hideElement('workspaces');
		hideElement('ancillary');
		hideElement('chart');
	}
	
	function showWorkSpaces() {
		hideElement('introduction');
		hideElement('frontoffice');
		showElement('workspaces');
		hideElement('ancillary');
		hideElement('chart');
	}
	
	function showAncillary(){
		hideElement('introduction');
		hideElement('frontoffice');
		hideElement('workspaces');
		showElement('ancillary');
		hideElement('chart');
	}

	function showChart(){
		hideElement('introduction');
		hideElement('frontoffice');
		hideElement('workspaces');
		hideElement('ancillary');
		showElement('chart');
	}
	
	var grossPerc = 15;
	var GROSS_MAX = 20;
	var GROSS_MIN = 10;
	
	function updateGrossValue() {
		var total = getAncillaryTotal() + getFrontOfficeTotal() + getWorkSpacesTotal();
		var perc = total * (grossPerc + 100) / 100;

		document.getElementById('grossperc').value = grossPerc + '%';
		element = document.getElementById('grosstotal');
		strong = element.getElementsByTagName('strong');
		strong[0].innerHTML = perc.toFixed(2);
	}
	
	function addRemoveGross(value) {
		var aux = (grossPerc + value);
		if (aux <= GROSS_MAX && aux >= GROSS_MIN) {
			grossPerc+=value;
			updateGrossValue();
		}
	}
	