function updateGarages(country_id, target_select){
	$(target_select).options.length = 0;

	new Ajax.Request('index.php?module=frontend&event=ajax_get_garages', {
		parameters: {
			country_id: country_id
		},
		onSuccess: function(transport) {
			var options = transport.responseJSON;
			options.each(function(option){
				var myNewOption = new Option(option.name, option.id);
				var optsLen = $(target_select).options.length;
				$(target_select).options[optsLen] = myNewOption;
			});
		}
	});
}

function updateDropoffGarages(country_id, target_select){
	$(target_select).options.length = 0;

	new Ajax.Request('index.php?module=frontend&event=ajax_get_dropoff_cities', {
		parameters: {
			country_id: country_id
		},
		onSuccess: function(transport) {
			var options = transport.responseJSON;
			options.each(function(option){
				var myNewOption = new Option(option.name, option.id);
				var optsLen = $(target_select).options.length;
				$(target_select).options[optsLen] = myNewOption;
			});
			updateDropoffpoints($(target_select).value);
		}
	});
}


function updateDropoffpoints(garage_id){

	$('dropoffpoint').options.length = 0;

	new Ajax.Request('index.php?module=frontend&event=ajax_get_dropoffpoints', {
		parameters: {
			garage_id: garage_id
		},
		onSuccess: function(transport) {
			var options = transport.responseJSON;
			options.each(function(option){
				if (option.id) {
					var myNewOption = new Option(option.address, option.id);
					var optsLen = $('dropoffpoint').options.length;
					$('dropoffpoint').options[optsLen] = myNewOption;
				}
			});
			calculatePrice();
		}
	});
}

function calculatePrice() {
	var price = 0;
	price += parseFloat(rentprice);
//	console.log('Auto rendihind', rentprice, price);


	var pickuppoint_id = $('pickuppoint_id').value;
	price += parseFloat(pickuppoints_prices[pickuppoint_id]);
//	console.log('Pickuppoint', pickuppoints_prices[pickuppoint_id], price);


	var dropoffpoint_id = $('dropoffpoint').value;
	price += parseFloat(dropoffpoint_prices[dropoffpoint_id]);
//	console.log('Dropoffpoint', dropoffpoint_prices[dropoffpoint_id], price);


	equipment_prices.each(function(equipment) {
		if ($('equipment_'+equipment.id).type == 'text') {
			if (parseFloat($('equipment_'+equipment.id).value) > 0) {
				price += parseFloat(parseFloat($('equipment_'+equipment.id).value) * parseFloat(equipment.price));
//				console.log('Auto varustuse hind INP', parseFloat($('equipment_'+equipment.id).value * parseFloat(equipment.price)), price);
			}
		} else if ($('equipment_'+equipment.id).type == 'checkbox' && $('equipment_'+equipment.id).checked) {
			price += parseFloat(equipment.price);
//			console.log('Auto varustuse hind CB', equipment.price, price);
		}
	});

	if ($('extra_insurance') != null && $('extra_insurance').value > 0) {
		var extra = $('extra_insurance').value;

		if (typeof ensurance_prices[extra] == "undefined") {

		} else {

			price += parseFloat(ensurance_prices[extra]);
//			console.log('Auto kindlustus', ensurance_prices[extra], price);

		}
	}

	$('price').innerHTML = '€ '+tsep(Math.round(price * 100)/100);
	if ($('sum')) {
		$('sum').value = Math.round(price * 100)/100;
	}
}

function tsep(n,swap) {
	var ts=" ", ds="."; // thousands and decimal separators
	if (swap) { ts=","; ts="."; } // swap if requested

	var ns = String(n),ps=ns,ss=""; // numString, prefixString, suffixString
	var i = ns.indexOf(".");
	if (i!=-1) { // if ".", then split:
		ps = ns.substring(0,i);
		ss = ds+ns.substring(i+1);
	}
	return ps.replace(/(\d)(?=(\d{3})+([.]|$))/g,"$1"+ts)+ss;
}

function setmakseviis(value) {
	// Set button visually active
	$('makseviis_1').className = '';
	$('makseviis_2').className = '';
	$('makseviis_3').className = '';
	$('makseviis_'+value).className = 'halo';

	// FULL sum
	if (value == 1) {
		$('price').innerHTML = '€ '+tsep(Math.round(price * 100)/100);
		$('payment_form').show();
		$('pp_amount').value = Math.round(price * 100)/100;
		$('pp_custom').value = value;
	// Pay bron fee
	} else if (value == 2) {
		$('price').innerHTML = '€ '+tsep(Math.round(bronfee * 100)/100);
		$('payment_form').show();
		$('pp_amount').value = Math.round(bronfee * 100)/100;
		$('pp_custom').value = value;
	}
}

function updateEndDate() {
  show_start();
  show_end();
	setTimeout(function(){
		var start_date = $('algus').value;
		var start_hour = $('start_time_hour').value;
		var start_minute = $('start_time_minute').value;
		new Ajax.Request('enddate.php', {
			parameters: {
				start_date: start_date,
				start_hour: start_hour,
				start_minute: start_minute
			},
			onSuccess: function(transport) {
				var data = transport.responseJSON;
				$('lopp').value = data.end_date;
				$('end_time_hour').value = data.end_hour;
				$('end_time_minute').value = data.end_minute;
			}
		});
	},100);
}

function updateCarPrice(order_id, car_id) {
	new Ajax.Request('admin.php?module=orders&event=ajax_get_car_price', {
		parameters: {
			order_id: order_id,
			car_id: car_id
		},
		onSuccess: function(transport) {
			window.rentprice = transport.responseText;
			calculatePrice();
		}
	});
}
