
// currencies
var currencySelected = 1; //default
var currencyCount = 4;
var currencies = new Array ('','AED','EUR','USD','GBP','Custom');
var currenciesExchangeRates = new Array (0,1,0.1903,0.2724,0.1687,1);

// family
var familySelected = 1;
var familyCount = 5;
var livingCost = 600;
var totalLivingCost = 0;

var housingSelected = 2;
var housingCount = 4;
var housingCostCount = 5;
var totalHousing = 0;

var bedroomsSelected = 1;
var bedroomsCount = 3;

var housingCost = new Array(
			   0,28000,40000,60000,80000, // cheap 1 (int city) 21.12.2009
			   0,55000,70000,100000,120000, // fair 2 (marina)
			   0,70000,90000,120000,160000, // expat 3 (jbr)
			   0,0,    0,    120000,180000); // villa 4 (green community)

var utilitiesCost = new Array (
			   0, 400, 500, 600, 700, // apts
			   0, 0,   0,   1000, 1200); // villas

var internetCost = 300;

var carsSelected = 2;
var carsCount = 8;
var totalCars = 0;
var carsCost = new Array (
				0, // no car
				32000, // yaris
				50000, // golf
				100000, // bmw 3
				115000, // bmw 5
				100000, // 4x4
				180000, // lux 4x4 x5
				280000); // dream

var totalCost = 0;



function writeOutput(target,content){
	$(target).innerHTML = content;
}

function getTotal () {
	
	totalCost = totalHousing + totalCars + totalLivingCost;
	
	writeOutput('calculatortotal','<p class="title">Your salary needs to be at least<\/p><strong>' + Math.round(convertCurrency(totalCost/12)) + '<\/strong> ' + currencies[currencySelected] + '\/month or <strong>' + Math.round(convertCurrency(totalCost)) + '<\/strong> ' + currencies[currencySelected] + '\/year<p class="description1">to provide you a home, transportation and meals only<\/p><p class="description2">with this much money you will survive but keep in mind that you need to ask for health insurance, annual tickets to home for the whole family and have a drink at least once in a month<\/p>');
}


function calcMouseOver(elementName,elementId,action) {
	var element = document.getElementById(elementName + elementId);

	// out
	element.className = element.className.replace(/over/g,'') + ' '; // if out

	// over
	if (action == 'over')
		element.className = element.className + action;
}

function calcMouseClick (elementName,elementId,elementCount,elementSelected) {
	var element = document.getElementById(elementName + elementId);

//	alert(element);
	// select if mouse is over 
	element.className = element.className.replace(/over/g,'') + ' '; // if out
	// select 
	element.className = element.className + ' selected';

	// unselect previously selected element
	if (elementSelected != -1 && elementSelected != elementId) {
		var tempElement = document.getElementById(elementName + elementSelected);
		tempElement.className = tempElement.className.replace(/selected/g,'');
	}
}

function selectCurrency(selected) {
	
	if (selected != -1 ) {
		calcMouseClick('currency',selected,currencyCount,currencySelected);
		currencySelected=selected;
	}
	
	if (currencySelected != 1)
		writeOutput('exchangerate','<p><strong>Exchange rate:</strong> 1 AED = ' + currenciesExchangeRates[currencySelected] + ' ' + currencies[currencySelected] + ' (20 Dec 2009)</p>');
	else writeOutput('exchangerate','');

	selectBedrooms(-1);
	selectHousing(-1);
	selectCars(-1);
	selectFamily(-1);

	getTotal();
}

function convertCurrency (input) {
	return input * currenciesExchangeRates[currencySelected];
}

function selectHousing(selected) {
	if (selected != -1) {
		calcMouseClick('housing',selected,housingCount,housingSelected);
		housingSelected = selected;
	}
	
	var bedrooms = bedroomsSelected;
	
	if (housingSelected == 4 && bedrooms <2)
		bedrooms = 2;
		
	var housingIndex = housingCostCount*(housingSelected-1) + bedrooms + 1;
	var totalUtilities=0;
	if (housingSelected == 4) // villa
		totalUtilities = utilitiesCost[housingCostCount+bedrooms+1]*12;
	else
		totalUtilities = utilitiesCost[bedrooms+1]*12;
	
	totalHousing = housingCost[housingIndex] + totalUtilities + internetCost*12;
	
	writeOutput('housingtotal','<p class="subtotalheader"><strong>Total housing: <\/strong><strong>' + Math.round(convertCurrency(totalHousing/12))+ '<\/strong> &nbsp;' + currencies[currencySelected] + ' \/ month &nbsp;or &nbsp;<strong>' + Math.round(convertCurrency(totalHousing))+ '<\/strong> &nbsp;' + currencies[currencySelected] + ' \/ year</p>' + 																																											'<p class="totaldescription"><strong>Including:</strong><br>Rent: '+ Math.round(convertCurrency(housingCost[housingIndex]/12))+ ' ' + currencies[currencySelected] + ' \/ month, ' + Math.round(convertCurrency(housingCost[housingIndex]))+ ' ' + currencies[currencySelected] + ' \/ year<br>' +
																																																																																																																																																																					'Utilities (electricity & water & air-conditioning): ' +  Math.round(convertCurrency(totalUtilities/12))+ ' ' + currencies[currencySelected] + ' \/ month, ' + Math.round(convertCurrency(totalUtilities))+ ' ' + currencies[currencySelected] + ' \/ year<br>' + 

'Landline phone, internet, TV (basic package): ' +  Math.round(convertCurrency(internetCost))+ ' ' + currencies[currencySelected] + ' \/ month, ' + Math.round(convertCurrency(internetCost*12))+ ' ' + currencies[currencySelected] + ' \/ year<br>' + 

'</p>');

	getTotal();
}

function selectBedrooms(selected) {

	if (selected != -1) {
		calcMouseClick('bedrooms',selected,bedroomsCount,bedroomsSelected);
		bedroomsSelected = selected;
	}
	
	var housingColIndex = bedroomsSelected+1;
	var housingIndex; // array index
	
	for (var i=0;i<4;i++) {
		housingIndex = housingCostCount*i + housingColIndex;

		if (i == 3 && bedroomsSelected < 2)  // if villa & <2 beds
			housingIndex = housingCostCount*i + 3;
		
		writeOutput('housingupd' + (i+1), Math.round(convertCurrency(housingCost[housingIndex])/12) + ' ' + currencies[currencySelected] + ' \/ month<br>' + Math.round(convertCurrency(housingCost[housingIndex])) + ' ' + currencies[currencySelected] + ' \/ year');
	}
	
	selectHousing(-1);
}

function selectCars(selected) {
	if (selected != -1) {
		calcMouseClick('cars',selected,carsCount,carsSelected);
		carsSelected = selected;
	}
	
	var insuranceCost = carsCost[carsSelected-1]*0.05/12;
	var carCost = carsCost[carsSelected-1]*1.3/60;
	var petrolCost = 350;
	
	if (selected != 0)
		petrolCost = 0;
	
	totalCars = (carCost + insuranceCost)*12;
	
	writeOutput('transportationtotal','<p class="subtotalheader"><strong>Total transport: <\/strong><strong>' + Math.round(convertCurrency(totalCars/12))+ '<\/strong> &nbsp;' + currencies[currencySelected] + ' \/ month &nbsp;or &nbsp;<strong>' + Math.round(convertCurrency(totalCars))+ '<\/strong> &nbsp;' + currencies[currencySelected] + ' \/ year</p>' +
																																																																															   '<p class="totaldescription"><strong>Including:</strong><br>Car price: '+ Math.round(convertCurrency(carsCost[carsSelected-1])) + ' ' + currencies[currencySelected] + ' or 5 years loan installments ' + Math.round(convertCurrency(carCost))+ ' ' + currencies[currencySelected] + ' \/ month, ' + Math.round(convertCurrency(carCost*12))+ ' ' + currencies[currencySelected] + ' \/ year<br>' +
																																																																																																																																																																					'Insurance: ' +  Math.round(convertCurrency(insuranceCost))+ ' ' + currencies[currencySelected] + ' \/ month, ' + Math.round(convertCurrency(insuranceCost*12))+ ' ' + currencies[currencySelected] + ' \/ year<br>' + 

'Petrol (very rough estimate, fuel is cheap here though): ' +  Math.round(convertCurrency(petrolCost))+ ' ' + currencies[currencySelected] + ' \/ month, ' + Math.round(convertCurrency(petrolCost*12))+ ' ' + currencies[currencySelected] + ' \/ year<br>' + 

'</p>');
	
	getTotal();
}

function selectFamily(selected) {
	if (selected != -1)
		familySelected = selected;
	
	totalLivingCost = livingCost*familySelected*12;
	
	writeOutput('livingtotal','<p class="subtotalheader"><strong>Total living: <\/strong><strong>' + Math.round(convertCurrency(totalLivingCost/12))+ '<\/strong> &nbsp;' + currencies[currencySelected] + ' \/ month &nbsp;or &nbsp;<strong>' + Math.round(convertCurrency(totalLivingCost))+ '<\/strong> &nbsp;' + currencies[currencySelected] + ' \/ year</p>' +																																																																														   '<p class="totaldescription"><strong>Including:</strong><br>Meals, washing liquids, etc. & lunch in a cafe once a week: '+ Math.round(convertCurrency(livingCost)) + ' ' + currencies[currencySelected] + ' per family member per month' 
																																																																															   /* +
																																																																																																																																																																					'Insurance: ' +  Math.round(convertCurrency(insuranceCost))+ ' ' + currencies[currencySelected] + ' \/ month, ' + Math.round(convertCurrency(insuranceCost*12))+ ' ' + currencies[currencySelected] + ' \/ year<br>' + 

'Petrol (very rough estimate, fuel is cheap here though): ' +  Math.round(convertCurrency(petrolCost))+ ' ' + currencies[currencySelected] + ' \/ month, ' + Math.round(convertCurrency(petrolCost*12))+ ' ' + currencies[currencySelected] + ' \/ year<br>' + 

'</p>'*/);
	
	getTotal();
	
}