/**
 * Utility function to format numbers with custom separators
 * @param nStr: The number to be formatted, as a string or number.
 * @param inD: The decimal character for the input, such as '.' for the number 100.2
 * @param outD: The decimal character for the output, such as ',' for the number 100,2
 * @param sep: The separator character for the output, such as ',' for the number 1,000.2
 * @return String: Formatted number
 */
function addSeparatorsNF(nStr, inD, outD, sep)
{
	nStr += '';
	var dpos = nStr.indexOf(inD);
	var nStrEnd = '';
	if (dpos != -1) {
		nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
		nStr = nStr.substring(0, dpos);
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) {
		nStr = nStr.replace(rgx, '$1' + sep + '$2');
	}
	return nStr + nStrEnd;
}

function availableDays(date) {
	//console.log(date);
	//console.log(date.getYear());
	for (i = 0; i < avDays.length; i++) {
		if (date.getFullYear() == avDays[i][0]
				&& date.getMonth() == avDays[i][1] - 1
				&& date.getDate() == avDays[i][2]) {
			return [true, avDays[i][2] + '_day'];
		}
	}
	return [false, ''];
}

function loadPlaces(obj) {
	var group = jQuery(obj).attr('value');
	var v_trempat = jQuery('#trempat_' + group).text();
	var v_ciclo = jQuery('#ciclo_' + group).text();

	$('#l_trempat').html(v_trempat);
	$('#reservation_trempat').attr('disabled', !(parseInt(v_trempat) > 0));
	$('#l_ciclo').html(v_ciclo);
	$('#reservation_ciclorail').attr('disabled', !(parseInt(v_ciclo) > 0));
	$('#l_trempat, #l_ciclo').parent().effect("highlight", {color: '#708F00', mode: 'show'}, 500);
}

function calculatePrice()
{
	var price = $('#price').val();
	var trempat = parseInt($('#reservation_trempat').val());
	var ciclo = parseInt($('#reservation_ciclorail').val());
	var totalPrice = price * (trempat + ciclo);
	var formattedPrice = addSeparatorsNF(totalPrice.toFixed(2), '.', ',', '.');
	
	$('#total-price').html(formattedPrice + ' &euro');
	$('#total-price').parent().effect("highlight", {color: '#708F00', mode: 'show'}, 500);
}

function calendar(currentDay, shortDay, fullMonth){
	var dAny = currentDay.substring(6, 10);
	var dMes = currentDay.substring(3, 5);
	var dDia = currentDay.substring(0, 2);
	iMes = parseInt(dMes, 10) - 1;
	var dateObj = new Date(dAny, iMes, dDia);
	
	$('#calendar').datepicker({
		dateFormat: 'dd/mm/yy', // formato de fecha que se usa en España
		altField: 'input#reservation_day',
		//dayNames: ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sabado'], // días de la semana
		dayNamesMin: shortDay, // días de la semana (versión super-corta)
		//dayNamesShort: ['Dom', 'Lun', 'Mar', 'Mie', 'Jue', 'Vie', 'Sab'], // días de la semana (versión corta)
		defaultDate: dateObj,
		firstDay: 1, // primer día de la semana (Lunes)
		//maxDate: '+12m', // fecha máxima
		minDate: new Date,
		monthNames: fullMonth, // meses
		//monthNamesShort: ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'], // meses
		navigationAsDateFormat: true,
		beforeShowDay: availableDays,
		onSelect: selectedDay
	});
}
