function changeTravelMethod(choice)
{
    choice = $(choice);
    var $form = choice.parents('form');
    var transport = choice.val();
    var availableDates = [];
    
    //Un-disable any previously disabled dates
    $startdateTours = $form.find('#startdateTours');
    $startdateTours.children().remove();
    $startdateTours.append('<option value="09/04/2012" selected="selected">9 April 2012</option><option value="08/04/2012">8 April 2012</option><option value="07/04/2012">7 April 2012</option>');

    $enddateTours = $form.find('#enddateTours');
    $enddateTours.children().remove();
    $enddateTours.append('<option value="14/04/2012" selected="selected">14 April 2012</option><option value="15/04/2012">15 April 2012</option>');
    
    $form.find('#search_type').val('Tour');
    
    if(transport == 'Flight') {
	    $form.find('#search_type').val('Flight');
	    $form.find('#locationto').val('MAYR'); //Mayrhofen airport (combination of geographically-close airports)
	    $form.find('#airportto').val('MAYR'); //Mayrhofen airport (combination of geographically-close airports)
	    $form.find('#numadults').val( $form.find('#r1a').val() );
	    $form.find('#seats_adult').val( $form.find('#r1a').val() );
	    $form.find('.flight-options').show();
    } else if(transport == 'Transport') {
	    $form.find('#locationto').val('');
	    $form.find('#airportto').val('');
	    //restrict the dates to Coach-only dates
	    $startdateTours.children(":not([value='03/04/2011'][value='04/04/2011'])").remove();
	    $enddateTours.children(":not([value='09/04/2011'])").remove();
    } else if(transport == 'Make my own way' && 'Road Trip' == choice.children(':selected').text()) {
	    $form.find('#locationto').val('');
	    $form.find('#airportto').val('');
	    //restrict the dates to Road Trip-only dates
	    $startdateTours.children(":not([value='03/04/2011'])").remove();
	    $enddateTours.children(":not([value='09/04/2011'][value='10/04/2011'])").remove();
    } else {
	    $form.find('#locationto').val('SZG');
	    $form.find('#airportto').val('SZG');
    }
    
    if(transport != 'Flight' && transport != 'Flight Only') {
	    $form.find('#airportfrom').attr('disabled','disabled');
	    $form.find('.flight-options').hide();
    }
    else {
	    $form.find('#airportfrom').removeAttr('disabled');
	    $form.find('.flight-options').show();
    }
    
    //Dates change based on transport method, so update duration calculations
    updateDuration($form.find('#startdateTours'));
    
    $form.find('#airportfrom, #startdateTours, #enddateTours, #roomsTours, #r1a, #frm-booking-submit').effect('highlight', {color:'#EB018C'}, 500);
}

/**
 *  Updates the booking form's duration and booking date fields
 * @param element
 */
function updateDuration(element)
{
    //Repackage the HTML element as a JQuery object
    $element = $(element);

    var $form = $element.parents('form');
    var $startDate;
    var $endDate;
    if($element.attr('name') == 'startdate') {
        $startDate = $element.val();
        $endDate = $form.find('.enddateTours').val();
    } else {
        $startDate = $form.find('.startdateTours').val();
        $endDate = $element.val();
    }
    
    //Extract the Days from both dates (first 2 chars - date is in dd/mm/yyyy format)
    //and calculate the difference in days.
    var $days = parseInt($endDate.substr(0,2),10) - parseInt($startDate.substr(0,2),10);
    $form.find('.duration').val($days);
    //Set the startdate/enddate breakdown for Flight Only search.
    $form.find('.startdate_d').val( parseInt($startDate.substr(0,2), 10) );
    $form.find('.startdate_m').val( parseInt($startDate.substr(3,2), 10) );
    $form.find('.startdate_y').val( parseInt($startDate.substr(6,4), 10) );
    $form.find('.enddate_d').val( parseInt($endDate.substr(0,2), 10) );
    $form.find('.enddate_m').val( parseInt($endDate.substr(3,2), 10) );
    $form.find('.enddate_y').val( parseInt($endDate.substr(6,4), 10) );
    //$form.find('.tripcode').val('snowbomb'+$days); //not currently used
}

/**
 * Updates the booking form's room size field
 * @param element Room Size drop down
 */
function updateRoomSize(element)
{
    $roomsizeElement = $(element);
    var adults = $roomsizeElement.val();
    var $form = $roomsizeElement.parents('form');
    $form.find('.numadults').val(adults);
    $form.find('.seats_adult').val(adults);
}

/* Used for booking form on Accommodation listing page. */
function toggleAccommOptions(accomm_id) {
	$('#accommodation-options-'+accomm_id).toggle('slide',null,'normal');
}

function updateBookableRoomBerths() {
	var rooms = $('#roomsTours').attr('value');
	switch(rooms) {
		case '1':
			maxberth = 9;
			break;
		case '3':
			maxberth = 3;
			break;
		default:
			maxberth = 4;
	}
	
	$('#r1a').empty();
	var listHtml = '';
	for(var i=1; i <= maxberth; i++) {
		listHtml += '<option value="'+i+'">'+i+' adult'+((i > 1) ? 's' : '') + '</option>';
	}
	$('#r1a').html(listHtml);
}

