
try {
	var oFlightRefineSearchFormMaker = new FlightSearchFormMaker({

			//form ID
			'sElementId' : 'flight_refinesearchform',

			//location form handler
			'LoactionHandler' : function(oCountryCode, oRegion, oLocationList, oElementId) {

						//Auto compliter start
						var oAutoCompleter = new AutoCompleter({
							'sUrl' : '/flights/airport-list?pattern=SEARCH_KEYWORD',
							'iCharLimit' : 2,
							'iRenderLimit' : 10,
							'sElementId' : oElementId,
							'sResultShowAllCSS' : {},
							'sResultShowMinCSS' : {},
							'bNoEscForSearchField':false,
							'bShowSearchFieldAfterEsc':true,
							'bPersisField':false,
							'bHideSearchField':true,
							'onInputClick':function() {
							},
							'onStaticInputClick':function() {
								switch(oElementId) {
									case 'departure_location_search':
										$('#flight_refinesearchform #departure_search_input').val('');

										$('#flight_refinesearchform .show_search_field_departure').hide();
										break;

									case 'destination_location_search':
										$('#flight_refinesearchform #destination_search_input').val('');
										$('#flight_refinesearchform .show_search_field_destination').hide();
										break;
								}
							},

							'onData' : function(oData, sKeyword) {
								var sDivOut = [];
								var h =0;
								//loop through data
								for ( var i in oData ) {
									for ( var j in oData[i] ) {
										sDivOut.push({
										  'id':h,
										  'value':{'i':i,'j':j}, 
										  'displaytxt':''+j+', '+i, 
										  'count':oData[i][j],
										  'display': '<div style="float:left;">'+j+', '+i+'</div> <div class="__cl" style="float:right;"></div> <div style="clear:both;"/>'
										});
										h++;
									}
								} 
								return sDivOut;
							},

							'onSelect' : function(id,value,Display){
								//this will let you define how to handle selected data
								oCountryCode.val(value.i);
								oRegion.val(''); //PYO2 didnt have region
								oLocationList.val(value.j);
							}
						});
						oAutoCompleter.start();
						//Auto completer end
					},

			//will pass in jquery object and element name, 
			//so we can manipulate it here with 3rd party date picker
			'DateFormat':'dd-mm-yyyy',
			'DateHandler' : function( oStartDate, oEndDate, oNight ) {
								
						oStartDate.attr('readOnly', true);
						oEndDate.attr('readOnly', true);
						var monthToShow = 2;

						//Initial date correction
						var iAddDaysAhead = 1;
						var d = new Date();

						try {
							//set the date field if the original date field is blank
							if(oStartDate.val() == undefined || oStartDate.val() == '' ) { 
								oStartDate.val(d.addDays(iAddDaysAhead).asString()); 
							}
							if(oEndDate.val() == undefined || oEndDate.val() == '' ) { 
								oEndDate.val(d.addDays(parseInt(oNight.val())).asString()); 
							}
						} catch(e){}

						var oStartD = Date.fromString(oStartDate.val());
						oStartDate.datePickerMultiMonth({
							numMonths: monthToShow,
							month: oStartD.getMonth(), //datePickerMultiMonth didnt have auto select the selected month, this is the hack way to generate it!
							year: oStartD.getFullYear(),
								clickInput:true,
								displayClose:true,
								//selectMultiple:true
								horizontalPosition: 0
							});

							var oEndD = Date.fromString(oEndDate.val());
							oEndDate.datePickerMultiMonth({
								numMonths: monthToShow,
								month:oEndD.getMonth(),
								year: oEndD.getFullYear(),
								clickInput:true,
								displayClose:true,
								//selectMultiple:true
								horizontalPosition: 0
							});

							$(document).bind('keyup keypress', function(e) {
								if(e.which==27) {
									oStartDate.dpClose();
									oEndDate.dpClose();
								}
							});

							try {
								oStartDate.dpSetSelected(oStartDate.val(), true, true);
								oEndDate.dpSetSelected(oEndDate.val(), true, true);
							} catch(e){}
							return true;
						}, 


			// indate/outdate checker, trigger on change, return true for OK
			// ichange, 1 = start date, 2 = end date, 3 = night change
			'DateValidator' : function(ichange, oStartDate, oEndDate, oNight, mData1, mData2) {


									//StartDate change
									if(ichange==1) {
										var d = new Date();

										var darr=oStartDate.val().split('-');
//										alert(darr[0]+' '+darr[1]+' '+darr[2]);

										var d2=new Date(parseInt(darr[2],10), parseInt(darr[1],10)-1, parseInt(darr[0],10));
										var one_day=1000*60*60*24;
										var days=Math.ceil((d2.getTime()-d.getTime())/(one_day));

//										alert("now="+d.asString()+"\nd2=="+d2.asString()+"\nstartdate="+oStartDate.val()+"\ndays="+days+' nights='+oNight.val()+' md1='+mData1+' md2='+mData2);

//										if(d.asString() == oStartDate.val()) {
										if(days<5) {
											alert('Your departure date must be a  least 5 days from now');
											oStartDate.val(d.addDays(5).asString());
										} else {
											var darr=oEndDate.val().split('-');
											var ed=new Date(parseInt(darr[2],10), parseInt(darr[1],10)-1, parseInt(darr[0],10));
											var days=Math.ceil((ed.getTime()-d2.getTime())/(one_day));
//											alert('xxxx:'+days);	

											if(days<1) {
//   												ed.setDate(d2.getDate()+10);
												ed=new Date(d2.getTime() + 10*24*60*60*1000);

//    												alert(ed.asString());

												//update end date 
												oEndDate.val(ed.asString()
); 
												oEndDate.dpSetSelected(oEndDate.val());

											}

/*
											//update end date base on selected night
											oEndDate.val(mData2); 
											oEndDate.dpSetSelected(oEndDate.val());
*/
										}
										
									}

									//EndDate change
									if(ichange==2) {
										if(mData1<1){
											alert('Return date must be later than departure date');
											oEndDate.val(mData2); //auto correction for invalid date
											oEndDate.dpSetSelected(oEndDate.val());
										} else {
											//update night selection box
											if(mData1>365){
												alert('Your interval between departure/return dates must be less than 1 year');
												var oEndD = Date.fromString(oStartDate.val());
												oEndDate.dpSetSelected(oEndD.addDays(14).asString());
												oNight.selectOptions(14+'');
											}else{
												oNight.selectOptions(mData1+'');
											}
										}
									}

									return true;
								}, 

			'oNumberNight' : [1,2,3,4,5,6,7,8,9,10,11,12,13,14],
			'sNumberNightDefault' :4,

			'aRoomList' : [1,2,3,4],
			'iRoomDefault' : 1,

			'aAdultList' : [1,2,3,4],
			'iAdultDefault' : 2,
			'aChildList' : [0,1,2],
			'iChildDefault' : 0,
			'aInfantList' : [0,1,2],
			'iInfantDefault' : 0
	});

	//generate the form
	oFlightRefineSearchFormMaker.draw();


	$('#flight_refinesearchform .show_search_field_departure').click(function() {
		$('#departure_location_search .search_inputstatic').trigger('click', null);
	});

	$('#flight_refinesearchform .show_search_field_destination').click(function() {
		$('#destination_location_search .search_inputstatic').trigger('click', null);
	});

	/**
	 * Handle the click on the radio-botton to switch between one-way and return flights.
	 */
	$('#flight-type-one-way').click(function() {$('#flight-return-date').hide();});
	$('#flight-type-return').click(function() {$('#flight-return-date').show();});

}
catch(e) {}
