//$(window).load(function () {
//$(document).ready(function () {

		try{

			var ChangeBoxFormMaker = new HotelSearchFormMaker({

				//form ID
				'sElementId' : 'changebox_form',
				'bRefineGUI' : 3,
				//location form handler
				'LoactionHandler' : function(oCountryCode,oRegion,oLocationList){},

				//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 oStartD2 = Date.fromString(oStartDate.val());
					oStartDate.datePickerMultiMonth({
						numMonths: 2,
						month: oStartD2.getMonth(), //datePickerMultiMonth didnt have auto select the selected month, this is the hack way to generate it!
						year: oStartD2.getFullYear(),
						clickInput:true,
						createButton:false,
						displayClose:true,
						horizontalPosition: 0
					});

					var oEndD2 = Date.fromString(oEndDate.val());
					oEndDate.datePickerMultiMonth({
						numMonths: 2,
						month:oEndD2.getMonth(),
						year: oEndD2.getFullYear(),
						clickInput:true,
						createButton:false,
						displayClose:true,
						horizontalPosition: 0
					});

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

					try{
						oStartDate.dpSetSelected(oStartDate.val());
						oEndDate.dpSetSelected(oEndDate.val());
					}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 ) {
					//alert('> '+oNight.val()+' , '+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{
								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(oCountryCode,oRegion,oLocationList) {
					return true; //just return true, we didnt use location field
				},

				//select box option list
				'aNightList' : [1,2,3,4,5,6,7,8,9,10,11,12,13,14],
				'iNightDefault' : 1,
				'aRoomList' : [1,2,3,4],
				'iRoomDefault' : 1,
				'aAdultList' : [1,2,3],
				'iAdultDefault' : 1,
				'aChildList' : [0,1,2],
				'iChildDefault' : 0,
				'aInfantList' : [0,1,2],
				'iInfantDefault' : 0
			});

			//generate the form
			ChangeBoxFormMaker.draw();
			
	}catch(e){}
//});