

/**
 * Se recalculan los precios al cambiar de radio.
 */
function recalcula_precios(radio) {
	var id = radio.id;
	var id_arr = id.split('_');
	var codigo_hotel = id_arr[1];
    
	var precio_total = 0;
	var inputs = $('input:radio:checked');
    
	$(inputs).each(function(indice, elemento) {
		var elm_id = elemento.id;
		var elm_arr = elm_id.split('_');
		var elm_codigo_hotel = elm_arr[1];
    

		if (elm_codigo_hotel == codigo_hotel) {
			var valor = elemento.value;
			var valor_arr = valor.split('@');
			precio_total += parseFloat(valor_arr[1]);
		}

	});
	$('#total_hotel_' + codigo_hotel).html(number_format(precio_total,2,',','.'));
	calcula_precio_medio_noche(codigo_hotel);
}

function calcula_precio_medio_noche(codigo_hotel) {
    var total = $('#total_hotel_' + codigo_hotel).html();
    var noches=parseInt($('#total_noches').html());
    total=str2float(total);
	var media = parseFloat(total / noches);
	$('#importe_medio_por_noche').html(number_format(media,2,',','.'));
}

function number_format(number, decimals, dec_point, thousands_sep) {
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}

function str2float(str){
    str=str.replace('.','');
    str=str.replace(',','.');
    return parseFloat(str);
}

