document.observe("dom:loaded", function() {
	if($('pk_loc')) {
		$('pk_loc').selectedIndex = 0;
		$('rt_loc').options.length = 1;
	}
});

function updateDateControls(cal) {
	var date = cal.date;
	var selectMonth = $("pk_month");
	selectMonth.selectedIndex = date.getMonth();
	var selectDay = $("pk_day");
	selectDay.selectedIndex = (date.getDate() - 1);
	var selectYear = $("pk_year");
	var currentDate = new Date();
	selectYear.selectedIndex = (date.getFullYear() - currentDate.getFullYear());
}

function isValidEmail(email) {
	var regexp = /^\S+\@[a-zA-Z0-9\.-]+\.[a-zA-Z0-9]{2,4}$/;
	return regexp.test(email);
}

function chooseDestination(idx) {
	var cst_destination = $('rt_loc');
	var opts = cst_destination.options;
	opts.length = 0;
	opts[0] = new Option(" - Seleziona la destinazione - ", "");
	switch(idx) {
		case 1:
		case 2:
			opts[1] = new Option("Hotel Ilio - Capo S. Andrea", "Hotel Ilio - Capo S. Andrea");
			break;
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
			opts[1] = new Option("Piombino - Porto Traghetti", "Piombino - Porto Traghetti");
			break;
	}
	//cst_destination.options.length = 0;
	opts[1].selected = true;
}

function validateStep1() {
	var pickup_year = $('pk_year').value;
	var pickup_month = $('pk_month').value;
	var pickup_day = $('pk_day').value;
	var pickup_date = pickup_month + "/" + pickup_day + "/" + pickup_year;

	var start_day = new Date(pickup_date);
	var today = new Date();

	var lead_days = 0;
	var difference = 0;
	
	var errors = new Array();

	difference = ( start_day.getTime() + 86400000 ) - today.getTime();
	lead_days = Math.round(difference / (1000 * 60 * 60 * 24));

	if((start_day.getTime() + 86400000) < today.getTime()) { errors.push(ARRIVAL_DATE_TOO_EARLY); }
	if(lead_days < 1) { errors.push(ARRIVAL_DATE_LEAD_DAYS); }
	if($('pk_number').selectedIndex == 0) { errors.push(PASSENGERS_NOT_SELECTED); }
	if($('pk_loc').selectedIndex == 0) { errors.push(ARRIVAL_PLACE_NOT_SELECTED); }
	if($('rt_loc').selectedIndex == 0) { errors.push(DESTINATION_NOT_SELECTED); }
	
	if(errors.length > 0) { alert(errors.join('\n')); }
	return errors.length == 0;
}

function validateStep2() {
	var errors = new Array();
	
	if ($('name').value == '') { errors.push(NAME_NOT_GIVEN); }
	if ($('last_name').value == '') { errors.push(SURNAME_NOT_GIVEN); }
	if ($('state').selectedIndex == 0) { errors.push(PROVINCE_NOT_SELECTED); }
	if ($('phone_1').value == '' && $('phone_2').value == '') { errors.push(PHONE_NOT_GIVEN); }
	if (!isValidEmail($('email').value)) { errors.push(INVALID_EMAIL); }
	if (!$('privacy_yes').checked) { errors.push(PRIVACY_NOT_ACCEPTED); }

	if(errors.length > 0) { alert(errors.join('\n')); }
	return errors.length == 0;
}
