$(function() {

	// booking form initialization

	// tabbing 
	var calOpen;
	
	tabs = $('#bookingForm .tabs li');
	panels = $('#bookingForm .panel');

	$('#bookingForm .tabs li a').click(function() {

		if (calOpen) closeCalendarById(calOpen);

		tabs.removeClass('selected');
		panels.css({'display':'none'});
		qs = this.href.split('?')[1];
		target = null;

		vars = qs.split('&');

		$.each(vars,function(i,val) {
			if (val.split('=')[0] == 'activeTab') target = val.split('=')[1];
		});

		if (target != null) {
			target = $('#'+target);
			$(this).parent().addClass('selected');
			if (target.hasClass('panel')) {
				target.show();
			} else {
				target.parents('div.panel').show();
			};
		}
		return false;
	});

	// events
	$('#bookingForm select').each(function() {
		this.onmousewheel = function() {
			return false;
		}
	});

	// give each form (except hotel search) a validation call on submit
	$('#bookingForm form:not(#hotelSearch)').submit(function() {
		return submitTest.check();
	});

	// logic for return/one-way field controls
	$('input[name=isreturn]').click(function() {
		isRoundTrip = (this.value == 'yes') ? true : false;
		doRoundTrip(isRoundTrip);
	});

	// update flight form when search type changes
	$('input[name=search_type]').click(function() {
		bookingTypeChanged();
	});

	// helper function for US hotel rooms
	roomItems = $('.hotelSearchUS .room');

	// get current selected rooms (allowing for select persistence on reload etc)
	roomsOnLoad = $('.hotelSearchUS select#rooms').val();

	//hide person-per-room units for unselected rooms (i.e. higher values than the rooms select is set to)
	roomItems.slice(roomsOnLoad).hide();

	// display appropriate number of person-per-room units when number of rooms changes	
	$('.hotelSearchUS select#rooms').change(function() {
		roomItems.hide().slice(0,this.value).show();
	});
});

//IE enter does not work where we have display:none at load time.
$(function(){
    $('input').keydown(function(e){
        if (e.keyCode == 13) {
            $(this).parents('form').submit();
            return false;
        }
    });
});


