/*
 * Sorrentonline utils
 * jQuery tools
 * dev by: d.m.maresca
 * mail: domenico.maresca@gmail.com
 * ver: 2.1
 */
 
function bookingPopup(myform)
{
	var checkout = new Date;
	var checkoutDay = $('#booking select[name=checkout_monthday]').val();
	var checkoutMonthYear = $('#booking select[name=checkout_year_month]').val().split('-');
	checkout.setDate(checkoutDay);
	checkout.setYear(checkoutMonthYear[0]);
	checkout.setMonth(checkoutMonthYear[1]);
	//checkout.setDate(checkout.getDate() + 1);
	var windowname='booking';
	var url = $('#booking').attr('action');
	var params = ''
				+ '?id=' + $('#booking input[name=id]').val()
				+ '&bo11_id=' +  $('#booking input[name=bo11_id]').val()
				+ '&struttura=' + $('#booking input[name=struttura]').val()
				+ '&adulti=' + $('#booking input[name=adulti]').val()
				+ '&lng=' + $('#booking input[name=lng]').val()
				+ '&checkin_monthday=' + $('#booking select[name=checkin_monthday]').val()
				+ '&checkin_year_month=' + $('#booking select[name=checkin_year_month]').val()
				+ '&checkout_monthday=' + checkout.getDate()
				+ '&checkout_year_month=' + ((checkout.getYear()>1900) ? checkout.getYear() : 1900+checkout.getYear() ) + '-' + checkout.getMonth()
				+ '&ord'
	;
	window.open(url+params, windowname, 'width=795,height=580,scrollbars=1,resizable=yes,top=0,left=0');
	return false;
}

function set_days(date,target,add){
	var setToday=false;
	var today;
	if(typeof(date)=="string"){
		date = document.getElementById(date);
		setToday=true;
	}
	var days_ddl = document.getElementById(target);
	var dropdownValue = -1;
	try{
		dropdownValue = days_ddl[days_ddl.selectedIndex].value;
	}catch(e){}
	svuotaddl(days_ddl)
	var value = date.options[date.selectedIndex].value;
	var valori = value.split("-");
	var giorni = new Array("Dom","Lun","Mar","Mer","Gio","Ven","Sab");
	var date = new Date();
	if(add!=null && add>0) date.setDate(date.getDate()+add);
	date.setMonth(valori[1]-1);
	date.setYear(valori[0])
	today=date.getDate();
	var ultimo = ultimomese(valori[1],valori[0]);
	var calendarBox=false;
	for(i=1;i<=ultimo;i++){
		date.setDate(i);
		days_ddl.options[i-1]=new Option('',i,false,false);
		days_ddl.options[i-1].innerHTML = giorni[date.getDay()]+' '+(i);
		if((setToday==true) && i==today){
			days_ddl.options[i-1].selected=true;
			calendarBox = true;
		}
		if(calendarBox==false && i==dropdownValue){
			days_ddl.options[i-1].selected=true;
		}
	}
}

function ultimomese(mese,anno) {
	d = new Date(anno,mese,0);
	return d.getDate();
}

function svuotaddl(ddl){
	var num_opt = ddl.options.length;
	for(a=num_opt;a>=0;a--){
		if(ddl.options[a]!=null) ddl.options[a].selected=false;
		ddl.options[a]=null;
	}
}

function setupDatepicker(target,ddl_day,ddl_monthyear){
	$(target)
			.datePicker({createButton:false})
			.bind('click',function(){$(this).dpDisplay();this.blur();return false;})
			.bind(
				'dateSelected',
				function(e, selectedDate, $td)
				{
					updateddl(selectedDate,ddl_day,ddl_monthyear);
					if (ddl_day == 'checkin_monthday_h') {
						updateddl(selectedDate, 'checkout_monthday_h', ddl_monthyear);
						setDDLBiSelection('checkin_monthday_h','checkout_monthday_h',1);
						setDDLBiSelection('checkin_year_month_h','checkout_year_month_h');
					}
				}
			);
	if (ddl_day == 'checkin_monthday_h') {
		document.getElementById('checkin_monthday_h').onchange=function(){
			setDDLBiSelection('checkin_monthday_h','checkout_monthday_h',1);
		};
	}
	if (ddl_monthyear == 'checkin_year_month_h') {
		document.getElementById('checkin_year_month_h').onchange=function(){
			setDDLBiSelection('checkin_year_month_h','checkout_year_month_h');
		};
	}
}

function setDDLBiSelection(ddl_a,ddl_b,add){
	if(add==undefined) add=0;
	var selIdx = document.getElementById(ddl_a).selectedIndex;
	document.getElementById(ddl_b).selectedIndex = selIdx+add;
}

function updateddl(calendar,ddl_day,ddl_monthyear) {
	
	days = document.getElementById(ddl_day);	
	month_years = document.getElementById(ddl_monthyear);	
	var sel_date=new Array();
	if(calendar!=null){
		sel_date.push(calendar.getDate());
		sel_date.push((calendar.getMonth()+1));
		if(calendar.getYear()>1900) sel_date.push(calendar.getYear());
		else sel_date.push((calendar.getYear()+1900));
	}else{
		var td = new Date();
		sel_date = new Array();
		sel_date.push(td.getDate());
		sel_date.push((td.getMonth()+1));
		if(td.getYear()>1900) sel_date.push(td.getYear());
		else sel_date.push((td.getYear()+1900));
	}
	var today = new Date();
	var date = new Date(sel_date[2],sel_date[1]-1,sel_date[0]);
	svuotaddl(days);
	svuotaddl(month_years);
	var giorni = new Array("Dom","Lun","Mar","Mer","Gio","Ven","Sab");
	var mesi = new Array("Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre");	
	var ultimo = ultimomese(sel_date[1],sel_date[2]);
	for(i=1;i<=ultimo;i++){
		date.setDate(i);
		days.options[i-1]=new Option('',i,false,false);
		days.options[i-1].innerHTML = giorni[date.getDay()]+' '+(i);
		if(sel_date[0]==i) days.options[i-1].selected=true;
	} 
	
	for(var i=0,a=sel_date[2],z=0; i<=1; i++,a++){
		if(a==sel_date[2]) start_month=sel_date[1];
		else start_month=1;
		for(var j=start_month,k=0; j<=12; j++,k++){
			month_years.options[z]=new Option('',(a+'-'+j),false,false);
			month_years.options[z++].innerHTML = mesi[j-1] + " " + a;
			if(a==sel_date[2] && j==start_month) month_years.options[z-1].selected=true;
		}
	}
}

$(document).ready(function(){
	try{
		updateddl(null,'checkin_monthday_h','checkin_year_month_h');
		updateddl(null,'checkout_monthday_h','checkout_year_month_h');
		set_days('checkin_year_month_h','checkin_monthday_h');
		set_days('checkout_year_month_h','checkout_monthday_h',1);
		setupDatepicker('label.checkin_cal_h','checkin_monthday_h','checkin_year_month_h');
		setupDatepicker('label.checkout_cal_h','checkout_monthday_h','checkout_year_month_h');
		$('#checkin_year_month_h').change(function(){set_days('checkin_year_month_h','checkin_monthday_h');set_days('checkout_year_month_h','checkout_monthday_h',1);});
		$('#checkout_year_month_h').change(function(){set_days('checkout_year_month_h','checkout_monthday_h',1);});
	}catch(e){}
});