if(!selectDestinationLabel) {
	var selectDestinationLabel= 'Please select a destination';
}

if(!selectDepartureCityLabel) {
	var selectDepartureCityLabel= 'Please select departure city';
}


try{
	var oHolidayRefineSearchFormMaker = new HolidaySearchFormMaker({

		//form ID
		'sElementId' : 'holiday_refinesearchform',
		'bRefineGUI': 0,

		//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,
				horizontalPosition: 0
			});

			var oEndD = Date.fromString(oEndDate.val());
			oEndDate.datePickerMultiMonth({
				numMonths: monthToShow,
				month:oEndD.getMonth(),
				year: oEndD.getFullYear(),
				clickInput:true,
				displayClose: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();
				if(d.asString() == oStartDate.val()){
					alert('Your CHECK IN date must be a  least 1 day from now');
					oStartDate.val(d.addDays(1).asString());
				}else{
					//update end date base on selected night
					oEndDate.val(mData2); 
					oEndDate.dpSetSelected(oEndDate.val());
				}
			}
			//endDate change
			if(ichange==2){
				if(mData1<1){
					alert('Your CHECK OUT date must  be greater than CHECK IN date');
					oEndDate.val(mData2); //auto correction for invalid date
					oEndDate.dpSetSelected(oEndDate.val());
				}else{
					//update night selection box
					if(mData1>14){
						alert('Your interval between CHECK IN and  CHECK OUT date must be less than 14 days');
						var oEndD = Date.fromString(oStartDate.val());
						oEndDate.dpSetSelected(oEndD.addDays(14).asString());
						oNight.selectOptions(14+'');
					}else{
						mData1--;
						oNight.selectOptions(mData1+'');
					}
				}
			}

			//night change
			if(ichange==3){
				//update end date base on night
				oEndDate.val(mData2); 
				oEndDate.dpSetSelected(oEndDate.val());
			}
			return true;
		}, 
		'SubmitValidator' : function(oType, oDestination, oAirPort) {

			if(oDestination.val()=='') {
				alert(selectDestinationLabel);
				return false;
			}
			if(oType.val()=='Holiday+Flight' &&  oAirPort.val()=='') {
				alert(selectDepartureCityLabel);
				return false;
			}
			return true;
		},

		//select box option list
		'sHolidayTypesSource' : '/holidays/theme-list?true=1',
		'sHolidayTypesDefault' :'I don\'t mind',
		'sHolidayDestinationSource' : '/holidays/town-list?true=1',
		'sDestinationsDefault' :'',
		'sHolidayAirportSource' : '/holidays/departure-airport-list?true=1', 
		'sDepartureAirPortDefault' :'',

		'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],
		'iAdultDefault' : 2,
		'aChildList' : [0,1,2],
		'iChildDefault' : 0,
		'aInfantList' : [0,1,2],
		'iInfantDefault' : 0

	});

	//generate the form
	oHolidayRefineSearchFormMaker.draw();

}catch(e){}
