	var messageInABottle = false;	
	
	jQuery(document).ready(function($){
			// this prepopulates the dates in calendars if there are dates in the querystring
			if ( getCalendar("depart") || getCalendar("depart2") || getCalendar("depart3") || getCalendar("depart4") ) {
					if ( getCalendar("depart") )
					{
						active_depart = 'depart';
						active_return = 'return';
					}
					else if ( getCalendar("depart2") )
					{
						active_depart = 'depart2';
						active_return = 'return2';
					}
					else if ( getCalendar("depart3") )
					{
						active_depart = 'depart3';
						active_return = 'return3';
					}
					else if ( getCalendar("depart4") )
					{
						active_depart = 'depart4';
						active_return = 'return4';
					}
					
					if ( $_get("day").length > 0 && $_get("yearmonth").length > 0 )
					{
						_departday = parseInt( $_get("day") ) + 1;
						_returnDay = _departday + 14;
						_returnYearMonth =  $_get("yearmonth") ;
						if ( _returnDay > 28 ) {
								_returnYearMonth = mdf_fixMonth( _returnDay, _returnYearMonth );
								_returnDay = mdf_fixDay( _returnDay, _returnYearMonth );
						}
						_departdate = escape($_get("yearmonth") + "/" + ( _departday ));
						_returndate = escape( _returnYearMonth + "/" + ( _returnDay ));
						split_yearmonth = $_get("yearmonth").split("/");
						split_returnYearMonth = _returnYearMonth.split("/");
						
						// set the values in the flgiht search box
						getCalendar( active_depart )._setDate( split_yearmonth[0], split_yearmonth[1] - 1, _departday );
						getCalendar( active_return )._setDate( split_returnYearMonth[0], split_returnYearMonth[1] - 1, _returnDay );
					}
					if ( $_get("TravelClass") )
						$("#TravelClass").val( $_get("TravelClass") );
					if ( !messageInABottle && $_get("showreturnmessage") ) {
							$("#booking_form table.tabledata").prepend("<tr><td colspan=4><p style=\"font-size: 120%; font-weight: bold; color: #000;\">Select your return date and press Search Flights to check availability.</p></td></tr>");
							messageInABottle = true;
					}
			}
            
            // Hide the oneway selector if the query string has suggested so
            if ( $_get("hideoneway") )
            {
                $( 'label[for=TripTypeOneWay], #TripTypeOneWay').remove();
                $( '#TripTypeReturn' ).hide();
                $( 'label[for=TripTypeReturn]' ).append(' Airfare').wrapInner('<span></span>');
            }
            
            if ( $_get("showtips") )
            {
                $( '#priceinfo').remove();
                $('p.df_tip').html('<strong>Tip:</strong> Enter Your Return Date.');
                $( '#tips' ).show();
            }
            
            // Grey out all fields except return date if the query string has suggested so
           
            if ( $_get("lockallfieldsexceptreturn") || $_get("lockallfieldsexceptreturnandorigin") )
            {
                var elements = 'input[name=CityFrom], input[name=CityTo], select[name=CityTo], select[name=TravelClass]';
                if(!$_get("lockallfieldsexceptreturnandorigin"))
                {
                    elements += ', select[name=CityFrom]';
                }
                $( elements ).each(function(){
                   var input = ('<input type="hidden" name="' + $(this).attr('name') + '" id="' + $(this).attr('id') + '" value="' + $.trim($(this).val()) + '" />');
                   var text = $.trim($(this).val().toUpperCase());
                   if($(this).attr('name') == 'CityFrom' || $(this).attr('name') == 'CityTo')
                   {
                       if( airports[text] )
                           text = airports[text];
                   }
                   var span = $('<span>' + text.toLowerCase() + '</span>').css('text-transform','capitalize');
                   $(this).before(input).before(span).remove();
                });
                
               // put a yellow background on the return date
               $('#return2_day,#return2_month').css('background','#ffffad');
               
               // Tweak some styles to make it look nice after all the abuse above. It's messy but it works
               $('#booking_form td[width=230]').css('width', '190px');
               $('#booking_form td.classrow').css({height: '25px', paddingTop: '9px'});
               
               // set the month to the same as the depart month and blank out the day
               $('#return2_month').prepend('<option value=""></option>').val('');
               $('#return2_day').prepend('<option value=""></option>').val('');
                
               // add validation to the return day now that we've added a blank option
               $("form.validate_flight_search").submit( function(){
                    if ( $("#return2_day", this).val() == "" ) {
                        alert( "Please select a return date");
                        $("#return2_day", this).focus();
                        return false;
                    }
                    if ( $("#return2_month", this).val() == "" ) {
                        alert( "Please select a return month");
                        $("#return2_month", this).focus();
                        return false;
                    }
               });
            }
			
	});
	
	function mdf_fixDay( day, monthyear ) {
									
			dateparts = monthyear.split("/");
			month = dateparts[1];
			year = dateparts[0];
			
			if ( month == 2 )
					return parseInt(day) - 28;
			else if ( day > 29 )
					return parseInt(day) - 29;
			else
					return day;		
	
	}
	
	function mdf_fixMonth( day, monthyear) {
	
			dateparts = monthyear.split("/");
			month = dateparts[1];
			year = dateparts[0];
			
			if ( month == 2 || day > 29 ) {
					returnMonth = parseInt(month) + 1;
					returnYear = year;
					if ( returnMonth > 12 ) {
							returnMonth = 1;
							returnYear = parseInt(returnYear) + 1;
					}
					return returnYear + "/" + returnMonth;
	
			} else {
					return monthyear;		
			}
	
	}