var areaIds = [
	"kitchen", "laundry", "dining", "lounge", "study", "rumpus", "bathroom_1",
	"bathroom_2", "numExtraBathrooms", "extraBathroomArea", "numExtraRooms",
	"extraRoomArea", "bedroom_1", "ensuite",  "wardrobe", "bedroom_2", 
	"bedroom_3", "bedroom_4", "numExtraBedrooms", "extraBedroomArea", 
	"garage", "decks", "circulation"
]; 

var defaultAreaValues = [
	8, 4, 12, 20, 9, 20, 6,
	4, 0, 0, 0,
	0, 16, 4, 4, 12,
	12, 0, 0, 0,
	36, 9, 0.1
]; 

var smallAreaValues = [
	4, 2, 6, 16, 0, 0, 4,
	0, 0, 0, 0,
	0, 16, 0, 0, 9,
	9, 0, 0, 0,
	0, 6, 0.1
];

var largeAreaValues = [
	16, 6, 16, 30, 12, 30, 8,
	8, 0, 0, 0,
	0, 20, 8, 9, 15,
	15, 15, 0, 0,
	36, 16, 0.2
];

var kitchen;
var laundry;
var dining; 
var lounge; 
var study; 
var rumpus; 
var bathroom_1;
var bathroom_2; 
var numExtraBathrooms; 
var extraBathroomArea; 
var numExtraRooms;
var extraRoomArea; 
var bedroom_1; 
var ensuite;  
var wardrobe; 
var bedroom_2; 
var bedroom_3; 
var bedroom_4; 
var numExtraBedrooms; 
var extraBedroomArea; 
var garage; 
var decks; 
var circulation;

var indoorArea;
var outdoorAreas;
var garageArea;
var totalArea;

var indoorCost;
var outdoorCost;
var garageCost;
var totalCost;

function initialiseEstimator()
{
	for (var i = 0; i < areaIds.length; i++)
	{
		if (areaIds[i] != "circulation")
		{
			document.getElementById(areaIds[i]).value = defaultAreaValues[i];
		}
	}	
	indoorArea = document.getElementById("indoorArea");
	outdoorAreas = document.getElementById("outdoorAreas");	
	garageArea = document.getElementById("garageArea");	
	totalArea = document.getElementById("totalArea");
	indoorCost = document.getElementById("indoorCost");	
	outdoorCost = document.getElementById("outdoorCost");
	garageCost = document.getElementById("garageCost");
	totalCost = document.getElementById("totalCost");
	kitchen = document.getElementById("kitchen");
	laundry = document.getElementById("laundry");  
	dining = document.getElementById("dining");  
	lounge = document.getElementById("lounge");
	study = document.getElementById("study"); 
	rumpus = document.getElementById("rumpus");
	bathroom_1 = document.getElementById("bathroom_1"); 
	bathroom_2 = document.getElementById("bathroom_2"); 
	numExtraBathrooms = document.getElementById("numExtraBathrooms");
	extraBathroomArea = document.getElementById("extraBathroomArea");
	numExtraRooms = document.getElementById("numExtraRooms");
	extraRoomArea = document.getElementById("extraRoomArea");
	bedroom_2 = document.getElementById("bedroom_2");
	bedroom_3 = document.getElementById("bedroom_3");
	bedroom_4 = document.getElementById("bedroom_4");
	bedroom_1 = document.getElementById("bedroom_1");
	numExtraBedrooms = document.getElementById("numExtraBedrooms"); 
	extraBedroomArea = document.getElementById("extraBedroomArea");  
	ensuite = document.getElementById("ensuite");
	wardrobe = document.getElementById("wardrobe"); 
	garage = document.getElementById("garage"); 
	decks = document.getElementById("decks");
	circulation = document.getElementById("circulation");
	
	clearTotals();
}

function clearTotals()
{
	indoorArea.value = "";
	outdoorAreas.value = "";
	garageArea.value = "";
	totalArea.value = "";
	
	indoorCost.value = "";
	outdoorCost.value = "";
	garageCost.value = "";
	totalCost.value = "";
}

function updateAreas()
{
	var areaValues = defaultAreaValues;
	
	if (document.getElementById("smallProject").checked)
	{
		areaValues = smallAreaValues;
	}
	else if (document.getElementById("largeProject").checked)
	{
		areaValues = largeAreaValues;
	}
	
	for (var i = 0; i < areaIds.length; i++)
	{
		if (areaIds[i] != "circulation")
		{
			document.getElementById(areaIds[i]).value = areaValues[i];
		}
		else
		{
			var selectElement = document.getElementById("circulation");
			if (document.getElementById("largeProject").checked)
			{
				selectElement.selectedIndex = 2;
			}
			else 
			{
				selectElement.selectedIndex = 0;
			}
		}
	}	
	
	clearTotals();
}

function updateSquareMeterCosts()
{
	var costCategories= [
		"new-excellent-basic",
		"new-excellent-standard",
		"new-excellent-luxury",
		"new-good-basic",
		"new-good-standard",
		"new-good-luxury",
		"new-difficult-basic",
		"new-difficult-standard",
		"new-difficult-luxury",
		"reno-excellent-basic",
		"reno-excellent-standard",
		"reno-excellent-luxury",
		"reno-good-basic",
		"reno-good-standard",
		"reno-good-luxury",
		"reno-difficult-basic",
		"reno-difficult-standard",
		"reno-difficult-luxury"
	];
	
	var costsPerCategory = [
		1500,
		1700,
		1900,
		
		1605,
		1819,
		2033,
		
		1800,
		2040,
		2280,
		
		3000,
		3400,
		3800,
		
		3210,
		3638,
		4066,
		
		3600,
		4080,
		4560
	];
	
	// Get the values of the various radio buttons
	var category = (document.getElementById("new").checked) ? "new-" : "reno-";
	category += (document.getElementById("excellent").checked) ? "excellent-" : (document.getElementById("good").checked) ? "good-" : "difficult-";
	category += (document.getElementById("basic").checked) ? "basic" : (document.getElementById("standard").checked) ? "standard" : "luxury";
	
	var costIndex = 0;
	for (var i = 0; i < costCategories.length; i++)
	{
		if (costCategories[i] == category)
		{
			costIndex = i;
		}
	}
	
	var indoorAreaCostPerSquareMetre = parseFloat(costsPerCategory[costIndex]);
	document.getElementById("perMetreIndoor").value = formatNumber(indoorAreaCostPerSquareMetre, 2, false, false);
	clearTotals();
}

function calcAreas()
{
	if (!validateAreas())
	{
		return;
	}
	
	var totalIndoorArea = parseFloat(kitchen.value);    
	totalIndoorArea += parseFloat(laundry.value);  
	totalIndoorArea += parseFloat(dining.value);
	totalIndoorArea += parseFloat(lounge.value);
	totalIndoorArea += parseFloat(study.value);
	totalIndoorArea += parseFloat(rumpus.value);
	totalIndoorArea += parseFloat(bathroom_1.value);
	totalIndoorArea += parseFloat(bathroom_2.value);
	totalIndoorArea += parseFloat(numExtraBathrooms.value) * parseFloat(extraBathroomArea.value);
	totalIndoorArea += parseFloat(numExtraRooms.value) * parseFloat(extraRoomArea.value);
	totalIndoorArea += parseFloat(bedroom_1.value);
	totalIndoorArea += parseFloat(bedroom_2.value);
	totalIndoorArea += parseFloat(bedroom_3.value);
	totalIndoorArea += parseFloat(bedroom_4.value);
	totalIndoorArea += parseFloat(numExtraBedrooms.value) * parseFloat(extraBedroomArea.value);
	totalIndoorArea += parseFloat(ensuite.value);
	totalIndoorArea += parseFloat(wardrobe.value);

	totalIndoorArea *= 1.0 + parseFloat(circulation.options[circulation.selectedIndex].value);

	indoorArea.value = formatNumber(totalIndoorArea, 2, false, false);
	outdoorAreas.value = formatNumber(parseFloat(decks.value), 2, false, false);
	garageArea.value = formatNumber(parseFloat(garage.value), 2, false, false);
	totalArea.value = formatNumber(totalIndoorArea + parseFloat(decks.value) + parseFloat(garage.value), 2, false, false);
	
	indoorCost.value = "";
	outdoorCost.value = "";
	garageCost.value = "";
	totalCost.value = "";
}

function calcCosts()
{
	if (isNaN(parseFloat(indoorArea.value)) ||
		isNaN(parseFloat(outdoorAreas.value)) ||
		isNaN(parseFloat(garageArea.value)) ||
		isNaN(parseFloat(totalArea.value)))
	{
		alert("Debe calcular el total de area primero.");
		return;
	}
	
	if (isNaN(parseFloat(document.getElementById("perMetreIndoor").value)) ||
		isNaN(parseFloat(document.getElementById("perMetreOutdoor").value)) ||
		isNaN(parseFloat(document.getElementById("perMetreGarage").value)))
	{
		alert("los costos por metro cuadrado no deben estar en blanco - interior, garage, decks.");
		return;
	}
	
	var costPart1 = parseFloat(indoorArea.value) * parseFloat(document.getElementById("perMetreIndoor").value);
	indoorCost.value = formatNumber(costPart1, 2, false, false);
	var costPart2 = parseFloat(outdoorAreas.value) * parseFloat(document.getElementById("perMetreOutdoor").value);
	outdoorCost.value = formatNumber(costPart2, 2, false, false);
	var costPart3 = parseFloat(garageArea.value) * parseFloat(document.getElementById("perMetreGarage").value);
	garageCost.value = formatNumber(costPart3, 2, false, false);
	totalCost.value = formatNumber(costPart1 + costPart2 + costPart3, 2, false, false);
}

var formatNumber = function(num, decimalNum, bolLeadingZero, bolParens)
/* IN - num:            the number to be formatted
	    decimalNum:     the number of decimals after the digit
	    bolLeadingZero: true / false to use leading zero
	    bolParens:      true / false to use parenthesis for - num

  RETVAL - formatted number
*/
{
   var tmpNum = num;

   // Return the right number of decimal places
   tmpNum *= Math.pow(10,decimalNum);
   tmpNum = Math.floor(tmpNum);
   tmpNum /= Math.pow(10,decimalNum);

   var tmpStr = new String(tmpNum);

   // See if we need to hack off a leading zero or not
   if (!bolLeadingZero && num < 1 && num > -1 && num !=0)
	   if (num > 0)
		   tmpStr = tmpStr.substring(1,tmpStr.length);
	   else
		   // Take out the minus sign out (start at 2)
		   tmpStr = "-" + tmpStr.substring(2,tmpStr.length);                        


   // See if we need to put parenthesis around the number
   if (bolParens && num < 0)
	   tmpStr = "(" + tmpStr.substring(1,tmpStr.length) + ")";


   return tmpStr;
}

function validateAreas()
{
	for (var i = 0; i < areaIds.length; i++)
	{
		if (areaIds[i] != "circulation")
		{
			if (isNaN(parseFloat(document.getElementById(areaIds[i]).value)))
			{
				alert("Asegurese que todas las areas contienen números.");
				return false;
			}
		}
	}	
	
	return true;
}
