// JavaScript Document

var setSelectBoxesCountry = function(){
	
	$('city').getChildren().inject($('options_city'));
	$('hotel').getChildren().inject($('options_hotels'));
	
	$('options_city').getElements( '.country_id_' + $('country').value ).inject($('city'));
	$('options_hotels').getElements( '.city_id_' + $('city').value ).inject($('hotel'));
	
	$('ccpSelectCountry').value = $('country').value;
	$('ccpSelectCity').value = $('city').value;
	$('ccpSelectHotel').value = $('hotel').value;
}

var setSelectBoxesCity = function(){

	$('hotel').getChildren().inject($('options_hotels'));
	
	$('options_hotels').getElements( '.city_id_' + $('city').value ).inject( $('hotel') );
	
	$('ccpSelectCity').value = $('city').value;
	$('ccpSelectHotel').value = $('hotel').value;
}

var setSelectBoxesHotel = function(){
	$('ccpSelectHotel').value = $('hotel').value;
}

window.addEvent('domready', function(){
	
	if($('country') && $('city') && $('hotel'))
		setSelectBoxesCountry();
	
	if($('city') && $('hotel'))
		setSelectBoxesCity();
	
	if($('country'))
		$('country').addEvent('change', setSelectBoxesCountry);
	
	if($('city'))
		$('city').addEvent('change', setSelectBoxesCity);
	
	if($('hotel'))
		$('hotel').addEvent('change', setSelectBoxesCity);
		
		
	var jetzt = new Date();
	var morgen = jetzt.clone().increment('day');
	var uebermorgen = jetzt.clone().increment('day', 2);
	
	
	$('calArrivalDateField').value = jetzt.format("%Y-%m-%d");
	$('calDepartureDateField').value = morgen.format("%Y-%m-%d");
	
	
	var d1 = new DatePicker($('calArrivalDateField'), {
		pickerClass		: 'datepicker_vista', 
		format			:'d.m.Y',
		inputOutputFormat	:'Y-m-d',
		onSelect: function(a){
			var nights =  parseInt( $('nights').value );				
			var departure = Date.parse($('calArrivalDateField').value).increment('day', nights);
			
			d2.input = $('calDepartureDateField');
			d2.visual = $('calDepartureDateField').getNext();
			d2.select({
				day: 	(departure.format("%d").toInt()), 
				month: 	(departure.format("%m").toInt() -1 ),  // month werden ab 0 gez�hlt
				year:  	(departure.format("%Y").toInt())
			});
			
			
			if( new Date().clearTime().diff( Date.parse($('calArrivalDateField').value), 'day') < 0){
				alert('arrival date has to be in future');
			}				
		}
	});
	
	var d2 = new DatePicker($('calDepartureDateField'), {
		pickerClass		: 'datepicker_vista',
		format			:'d.m.Y',
		inputOutputFormat	:'Y-m-d',
		onSelect: function(d){				
			var a = Date.parse( $('calArrivalDateField').value );
			d.clearTime();
			
			var nights = a.diff(d, 'day' );
			if(nights <= 0){
				alert('departure has to be after arrival!');
			}else{					
				$$('select#nights option').each(function(opt){
					if(opt.value == nights) 
						opt.selected = true;
				});
			}
		}
	});		
	
	$('nights').addEvent('change', function(e){
		var nights =  parseInt(e.target.value);				
		var departure = Date.parse($('calArrivalDateField').value).increment('day', nights);
		
		d2.input = $('calDepartureDateField');
		d2.visual = $('calDepartureDateField').getNext();
		d2.select({
			day: 	(departure.format("%d").toInt()), 
			month: 	(departure.format("%m").toInt() -1 ),  // month werden ab 0 gez�hlt
			year:  	(departure.format("%Y").toInt())
		});

	});	
	
	
	var occupancy_fields = $$('select[name=numberOfAdults]').combine($$('.child_selects')).combine($$('select[name=prsRoomCount]'));

	occupancy_fields.addEvent('change', function(){
		var temp_val = "room_1:numberOfAdults=";
		temp_val += $('numberOfAdults').value;
		temp_val += ";childrenAges=";
		ages_array = new Array();
		$$('.child_selects').each(function(i){
			if(i.value != '')
				ages_array.push( i.value );
		});
		temp_val += ages_array.join(",");
		var rooms = 1;
		$$('select[name=prsRoomCount]').each(function(i) {
			if(i.value > 1){
				for(var y=1;y<i.value;y++) {
					rooms++;
					temp_val += "-room_"+rooms+":numberOfAdults=";
					temp_val += $('numberOfAdults').value;
					temp_val += ";childrenAges=";
				}
			}
		});
		$('occupancy').value = temp_val ;
	});
});
