
function initialize_calc() {
	if (!cities || !fullprices || !srprices || !childprices) {
		alert('The calculator component isn\'t properly configured!');
		return;
	}
	
	initialize_cities($('#city-from'));
	initialize_cities($('#city-to'));

	$('#result').hide();
	
	$('#calculate-button').click(function() {
		calculate();
		return false;
	});
}

function initialize_cities(select_element) {
	for (var i = 0; i < cities.length; i++) {
		select_element.append('<option value="' + i + '">' + cities[i] + '</option>');
	}
}

function calculate() {
	var total = 0;
	var from = parseInt($('#city-from').val());
	var to = parseInt($('#city-to').val());
	var surcharge = $('#calculator-percentage').val();
	
	var currentArray = eval($('#person-type').val() + 'prices');
	
	if (currentArray.length > from && currentArray[from].length > to) {
		total = currentArray[from][to];		// set the base price
		total += total * surcharge/100;		// add the surcharge
		total = Math.round(total);			// round it
	} else {
		alert('Sorry, but there is no price available to your location.');
		if ($('#result').is(':visible')) {
			$('#result').slideUp('fast');
		}
		return;
	}
	
	var slidedown = function() {
		$('#result').slideDown('fast');
	}

	if ($('#result').is(':visible')) {
		$('#result').slideUp('fast', function() {
			$('#result-value').html('CAD ' + total);
			slidedown();
		});
	} else {
		$('#result-value').html('$' + total);
		slidedown();
	}
}
