var departSearchParam = "&searchableOnly=false&locationType=airport";
var destinationSearchParam = "&searchableOnly=false&locationType=airport";
var searchType = "";
if (document.getElementById("searchPanel-selector") != null) {
	chosenOpt = OJ("#searchPanel-selector input[name='Search/searchType'][checked]");
	searchType = chosenOpt[0].value;
} else {
	searchType = document.getElementById("searchType").value;
}
OJ( function() {
	assignEvents();
	refreshSearchType();
	document.getElementById("searchPanel-form").style.display = "block";
	calcNumNights(document.getElementById("departDateCO"), document.getElementById("returnDateCO"), document.getElementById("numNights"));
});
function assignEvents() {
	OJ("#numRooms").change( function() {
		changeNumberRooms(this, this.selectedIndex + 1);
	});
	OJ("#searchPanel-selector input[name='Search/searchType']").click(
			function() {
				changeSearchType(this.value);
			});
	OJ("div.searchPanel-searchButtonClass a").click( function() {
		OJ(this).parents("form").submit();
	});
	OJ("#searchPanel-rooms").each( function() {
		OJ(this).find("select.childSelector").change( function() {
			addChildAges(this);
		});
	});
	var _1;
	OJ("input.selectInput").focus( function() {
		_1 = this.value;
		this.select();
	});

	OJ("#departDateCO").change( function() {
		calcNumNights(document.getElementById("departDateCO"), document.getElementById("returnDateCO"), document.getElementById("numNights"));
	});

	OJ("#returnDateCO").change( function() {
		calcNumNights(document.getElementById("departDateCO"), document.getElementById("returnDateCO"), document.getElementById("numNights"));
	});
}
function refreshSearchType() {
	changeSearchType(searchType);
}
function setUiOption(_2) {
	OJ("#searchPanel-selector input[value='" + _2 + "']").click();
}
/*This is a function to reset the class names that are added/removed in changeSearchType*/
function resetClassAttributes(){

	document.getElementById("destinationField").className = "fromTo z300";
}

function changeSearchType(_3) {
	var _4 = OJ("#plan_trip_common");
	_4.find("#searchType")[0].value = _3;
	_4.find("#searchTypevalidator")[0].value = _3;
	resetClassAttributes();
	
	if (_3 == "F") {
		departSearchParam = flightDepartSearch;
		destinationSearchParam = flightDestinationSearch;
		
		chosenOpt = OJ("#searchPanel-common-selector input[name='Search/flightType'][checked]");
		if (document.getElementById("flightType-Return").value == "return") {
			document.getElementById("calendarSearch-block").style.display = "block";
			document.getElementById("returnDateSelector").style.display = "block";
			document.getElementById("return-time-selector").style.display = "block";
		} else {
			document.getElementById("calendarSearch-block").style.display = "none";
			document.getElementById("returnDateSelector").style.display = "none";
			document.getElementById("return-time-selector").style.display = "none";
		}
		toggleMoreOptions();
		if (chosenOpt[0]) {
			toggleOneWayFlights(chosenOpt[0].value);
		}
	}
	if (_3 == "H") {
		_4.find("#roomDisplayAdultAge")[0].innerHTML = "("
				+ (hotelMaxChildAge + 1) + "+)";
		_4.find("#roomDisplayChildAge")[0].innerHTML = "(0-" + hotelMaxChildAge
				+ ")";
		document.getElementById("destinationField").className = "hotel fromTo z300";
		destinationSearchParam = hotelDestinationSearch;
		toggleMoreOptions();
	}
	if (_3 == "HF") {
		_4.find("#roomDisplayAdultAge")[0].innerHTML = "("
				+ (hotelMaxChildAge + 1) + "+)";
		_4.find("#roomDisplayChildAge")[0].innerHTML = "(0-" + hotelMaxChildAge
				+ ")";
		departSearchParam = packageDepartSearch;
		destinationSearchParam = HFDestinationSearch;
		toggleMoreOptions();
	}
}

function toggleOneWayFlights(_7) {
	if (document.getElementById(_7).value == "oneway") {
		document.getElementById("returnDateSelector").style.display = "block";
		//document.getElementById("returnCO").style.display = "block";
		if (document.getElementById("return-time-selector")) {
			document.getElementById("return-time-selector").style.display = "block";
			document.getElementById("return-time-selector").style.visibility = "visible";
		}
		document.getElementById("returnFlexibility").style.display = "block";
		document.getElementById("calendarSearch-block").style.display = "block";
		document.getElementById(_7).value='return';
		updateCheckboxValue("calendarSearchCheck", false);
	} else {
		if (document.getElementById(_7).value == "return") {
			document.getElementById("returnDateSelector").style.display = "none";
			//document.getElementById("returnCO").style.display = "none";
			if (document.getElementById("return-time-selector")) {
				document.getElementById("return-time-selector").style.display = "none";
				document.getElementById("return-time-selector").style.visibility = "hidden";
			}
			document.getElementById("returnFlexibility").style.display = "none";
			document.getElementById("calendarSearch-block").style.display = "none";
			document.getElementById(_7).value='oneway';
		}
	}
}
function toggleOneWayFlightsByString(type) {
	if (type == "oneway") {

		document.getElementById("returnDateSelector").style.display = "none";
		if (document.getElementById("return-time-selector")) {
			document.getElementById("return-time-selector").style.display = "none";
			document.getElementById("return-time-selector").style.visibility = "hidden";
		}
		document.getElementById("returnFlexibility").style.display = "none";
		document.getElementById("calendarSearch-block").style.display = "none";
		//document.getElementById(_7).value='return';
		updateCheckboxValue("calendarSearchCheck", false);
	} else {
		if (type == "return") {

			document.getElementById("returnDateSelector").style.display = "block";
			if (document.getElementById("return-time-selector")) {
				document.getElementById("return-time-selector").style.display = "block";
				document.getElementById("return-time-selector").style.visibility = "visible";
			}
			document.getElementById("returnFlexibility").style.display = "block";
			document.getElementById("calendarSearch-block").style.display = "block";
			//document.getElementById(_7).value='oneway';
		}
	}
}
function toggleMoreOptions() {
	searchType = document.getElementById("searchType").value;
	if (!document.getElementById("Search-moreOptions").checked) {
		document.getElementById("airlinePrefBlock").style.display = "none";
		document.getElementById("additionalOptionsBlock").style.display = "none";
		document.getElementById("cabinBlock").style.display = "none";
		document.getElementById("directFlight-block").style.display = "none";
		document.getElementById("searchPanel-hotel-additional-options").style.display = "none";
		updateInputValue("propertyType", "");
		updateInputValue("locationType", "");
	//	updateInputValue("hotelName", "");
		updateInputValue("roomRating", "");
		document.getElementById("Search-moreOptions").value = "false";
	} else {
		flightSearch = searchType.indexOf("F") != -1
				|| searchType.indexOf("A") != -1;
		hotelSearch = searchType.indexOf("H") != -1;
		if (flightSearch) {
			document.getElementById("airlinePrefBlock").style.display = "block";
			document.getElementById("additionalOptionsBlock").style.display = "block";
			document.getElementById("cabinBlock").style.display = "block";
			document.getElementById("directFlight-block").style.display = "block";
		}
		if (hotelSearch) {
			document.getElementById("searchPanel-hotel-additional-options").style.display = "block";
		}
		document.getElementById("Search-moreOptions").value = "true";
	}
}
function updateInputValue(_8, _9) {
	input = document.getElementById(_8);
	if (input != null) {
		input.value = _9;
	}
}
function updateCheckboxValue(_a, _b) {
	input = document.getElementById(_a);
	if (input != null) {
		input.checked = _b;
	}
}
function toggleCarSelectDropOff() {
	if (!document.getElementById("carSelectorDropOffDiff").checked
			&& (document
					.getElementById("Search-VehicleInformation-dropOffLocation").value == document
					.getElementById("Search-VehicleInformation-pickUpLocation").value || document
					.getElementById("carSelectorDropOffSame").checked)) {
		document.getElementById("Search-VehicleInformation-dropOffLocation").value = document
				.getElementById("Search-VehicleInformation-pickUpLocation").value;
		document
				.getElementById("Search-VehicleInformation-dropOffLocation_input").value = document
				.getElementById("Search-VehicleInformation-pickUpLocation_input").value;
		document.getElementById("searchPanel-car-selector-dropOff-location").style.display = "none";
		document.getElementById("carSelectorDropOffDiffLocation").style.display = "block";
		document.getElementById("carSelectorDropOffSameLocation").style.display = "none";
		document.getElementById("carSelectorDropOffSame").checked = false;
	} else {
		document.getElementById("searchPanel-car-selector-dropOff-location").style.display = "block";
		document.getElementById("carSelectorDropOffDiffLocation").style.display = "none";
		document.getElementById("carSelectorDropOffSameLocation").style.display = "block";
		document.getElementById("carSelectorDropOffDiff").checked = false;
	}
}
function setVisibleAdditionalOptions(_c) {
	var _d = "none";
	if (_c) {
		_d = "block";
	}
	document.getElementById("searchPanel-car-selector-additionalOptions").style.display = _d;
}
function changeNumberRooms(el, _f) {
	rooms = OJ("#searchPanel-rooms");
	if (el != null) {
		rooms = OJ(el).parents("div.searchPanel-hotel-rooms");
	}
	for (roomCategories = 0; roomCategories < rooms.length; roomCategories++) {
		roomCategory = OJ(rooms[roomCategories]);
		roomList = roomCategory.children("#roomContainer").children("div.searchPanel-hotel-room");
		for (j = 0; j < roomList.length; j++) {
			room = OJ(roomList[j]);
			if (j < _f) {
				room.css("display", "block");
			} else {
				room.css("display", "none");
			}
		}
	}
	updateHotelPassengers();
}
function setupChildAges() {
	OJ("#searchPanel-rooms").find(".childSelector")
			.each( function() {
				addChildAges(this);
			});
}
function addChildAges(_10) {

	var roomNumber = OJ(_10).attr("id").split("_")[1];
	
	parents1 = OJ("#searchPanel-room" + roomNumber);
	numChildRoom = OJ("#numChildrenRoom_" + roomNumber).val();
	var _13 = false;
	
	if (numChildRoom > 0) {
		_13 = true;
	}
	
	for (i = 1; i <= maxChildren; i++) {
		if (i <= numChildRoom) {
			OJ("#room" + roomNumber + "AgeFieldChild" + i).css(
					"display", "block");
		} else {
			OJ("#room" + roomNumber + "AgeFieldChild" + i).css(
					"display", "none");
		}
	}
	
	if (_13) {
		OJ("#child-ages-room" + roomNumber).css("display", "block");
	} else {
		OJ("#child-ages-room" + roomNumber).css("display", "none");
	}
	
}
function addEventChildAges(_14) {
	cform = _14.form;
	numChildren = parseInt(cform["eventChildPassengers"].value);
	var _15 = (numChildren > 0);
	for (i = 1; i <= maxChildren; i++) {
		if (i <= numChildren) {
			OJ("#eventAgeSelection").find("#eventAgeFieldChild" + i).css(
					"display", "block");
		} else {
			OJ("#eventAgeSelection").find("#eventAgeFieldChild" + i).css(
					"display", "none");
		}
	}
	if (_15) {
		OJ("#eventAgeSelection").find(".event-child-ages").css("display",
				"block");
		changeEventChildAge();
	} else {
		OJ("#eventAgeSelection").find(".event-child-ages").css("display",
				"none");
	}
}
function changeEventChildAge() {
	var _16 = OJ("#plan_trip_common").find("#searchType")[0].value;
	var _17 = false;
	var _18 = 0;
	OJ(".eventAgeSelect").each( function() {
		age = parseInt(this.value);
		if (age > 0 && age <= eventMaxInfantAge) {
			_18++;
		}
	});
}
function addFlightChildAges(_19) {
	cform = _19.form;
	numChildren = parseInt(cform["childPassengers"].value);
	var _1a = false;
	if (numChildren > 0) {
		_1a = true;
	}
	for (i = 1; i <= maxChildren; i++) {
		if (i <= numChildren) {
			OJ("#plan_trip_common").find("#flightAgeFieldChild" + i).css(
					"display", "block");
		} else {
			OJ("#plan_trip_common").find("#flightAgeFieldChild" + i).css(
					"display", "none");
		}
	}
	if (_1a) {
		OJ("#plan_trip_common").find(".flight-child-ages").css("display",
				"block");
		changeFlightChildAge();
	} else {
		OJ("#plan_trip_common").find(".flight-child-ages").css("display",
				"none");
		OJ("#infantSeatSelection").css("display", "none");
	}
}
function changeChildAge() {
	var _1b = OJ("#plan_trip_common").find("#searchType")[0].value;
	var _1c = false;
	var _1d = 0;
	OJ("#roomContainer select.roomAgeSelect").each( function() {
		age = parseInt(this.value);
		if (age > 0) {
			if (_1b == "H") {
				if (age <= hotelMaxInfantAge) {
					_1d++;
				}
			} else {
				if (age <= packageMaxInfantAge) {
					_1d++;
				}
			}
		}
	});
	if (_1d > 0) {
	} else {
		OJ("#infantSeatSelection").css("display", "none");
	}
}
function changeFlightChildAge() {
	var _1e = OJ("#plan_trip_common").find("#searchType")[0].value;
	var _1f = false;
	var _20 = 0;
	OJ(".flightAgeSelect").each( function() {
		age = parseInt(this.value);
		if (age > 0 && age <= airMaxInfantAge) {
			_20++;
		}
	});
	if (_20 > 0) {
	} else {
		OJ("#infantSeatSelection").css("display", "none");
	}
}
function setDropOffValues() {
	document.getElementById("Search-VehicleInformation-dropOffLocation_input").value = document
			.getElementById("Search-VehicleInformation-pickUpLocation_input").value;
	document.getElementById("Search-VehicleInformation-dropOffLocation").value = document
			.getElementById("Search-VehicleInformation-pickUpLocation").value;
}
function onPopUp(_21) {
	OJ(".modalClose")[0].innerHTML = "Close Window";
	_21.container.show();
	_21.data.show();
	_21.overlay.show();
}
function pickTheFirstLocation() {
	var _22 = new Array(
			"Search-OriginDestinationInformation-Destination-location",
			"Search-OriginDestinationInformation-Origin-location",
			"Search-VehicleInformation-pickUpLocation",
			"Search-VehicleInformation-dropOffLocation");
	for (i = 0; i < _22.length; i++) {
		var _23 = document.getElementById(_22[i] + "_choices");
		var _24 = document.getElementById(_22[i]).value;
		var _25 = false;
		if (_24 == "" || _23.innerHTML.indexOf(_24) == -1) {
			_25 = true;
		}
		var _26 = getValueByAttribute(_23, "id");
		var _27 = getValueByAttribute(_23, "name");
		if (_25 && document.getElementById(_22[i]).value != _26
				&& _23.innerHTML != "" && _26 != "" && _27 != "") {
			document.getElementById(_22[i]).value = _26;
			document.getElementById(_22[i] + "_input").value = _27;
		}
	}
	document.getElementById("plan_trip_common").method = "POST";
}
function getValueByAttribute(_28, _29) {
	if (_28.firstChild != null && _28.firstChild.firstChild != null) {
		return _28.firstChild.firstChild.getAttribute(_29);
	} else {
		return "";
	}
}

function changeStars(stars){

	document.getElementById("allStars").className = "checkbox";
	document.getElementById("5Stars").className = "checkbox";
	document.getElementById("4Stars").className = "checkbox";
	document.getElementById("3Stars").className = "checkbox";
	document.getElementById("2Stars").className = "checkbox";
	document.getElementById("1Stars").className = "checkbox";

	if(stars == "all"){
		document.getElementById("allStars").className = "checkbox checked";
		OJ("#roomRating").val(1);
	}
	else if(stars == "5"){
		document.getElementById("5Stars").className = "checkbox checked";
		OJ("#roomRating").val(5);
	}
	else if(stars == "4"){
		document.getElementById("4Stars").className = "checkbox checked";
		OJ("#roomRating").val(4);
	}
	else if(stars == "3"){
		document.getElementById("3Stars").className = "checkbox checked";
		OJ("#roomRating").val(3);
	}
	else if(stars == "2"){
		document.getElementById("2Stars").className = "checkbox checked";
		OJ("#roomRating").val(2);
	}
	else if(stars == "1"){
		document.getElementById("1Stars").className = "checkbox checked";
		OJ("#roomRating").val(1);
	}
}

OJ(function() {
	OJ.datepicker.setDefaults(
	);

	var lang = OJ("#dateLanguage").val();
	OJ.extend(OJ.datepicker._defaults, OJ.datepicker.regional[lang]);

	var departDateArray = OJ("#departDateCO").val().split("/");
	var departDate = new Date();
	departDate.setFullYear(departDateArray[2], departDateArray[1]-1, departDateArray[0]);

	var returnDateArray = OJ("#returnDateCO").val().split("/");
	var returnDate = new Date();
	returnDate.setFullYear(returnDateArray[2], returnDateArray[1]-1, returnDateArray[0]);
	console.log(departDate.toDateString());

	var dateFormatRegional;
	if(lang=="zh")
		dateFormatRegional="yy-mm-dd";
	else
		dateFormatRegional="D, dd M y";
	
	OJ("#departCO").datepicker({
		dateFormat:dateFormatRegional,
		altFormat: "dd/mm/yy",
		altField: "#departDateCO",
		beforeShow: customRange,
		defaultDate: departDate,
		onSelect: function(dateText, inst){
			var dateArray = OJ("#departDateCO").val().split("/");
			OJ("#departDayCO").val(dateArray[0]);
			OJ("#departMonthCO").val(dateArray[1]);
			OJ("#departYearCO").val(dateArray[2]);
			OJ("#departDateCO").change();
			
			newDepartDate = new Date(dateArray[2],Number(dateArray[1])-1,dateArray[0]);
			newReturnDate = new Date(dateArray[2],Number(dateArray[1])-1,Number(dateArray[0])+7);
			var oldReturnDateArray = OJ("#returnDateCO").val().split("/");
			oldReturnDate = new Date(oldReturnDateArray[2],Number(oldReturnDateArray[1])-1,oldReturnDateArray[0]);
			if(oldReturnDate<=newDepartDate)
			{
				newReturnMonth=newReturnDate.getMonth()+1;
				if(newReturnMonth<10)
					newReturnMonth="0"+newReturnMonth;
				
				newReturnDay=newReturnDate.getDate();
				if(newReturnDay<10)
					newReturnDay="0"+newReturnDay;
				
				var td = document.createElement('td');
				var a = document.createElement('a');
				a.innerHTML = newReturnDay;
				td.appendChild(a);
				
				DP_jQuery.datepicker._selectDay('#returnCO',newReturnDate.getMonth(),newReturnDate.getFullYear(), td);

				OJ("#returnDateCO").val(newReturnDay+"/"+newReturnMonth+"/"+newReturnDate.getFullYear());
				OJ("#returnDayCO").val(newReturnDay);
				OJ("#returnMonthCO").val(newReturnMonth);
				OJ("#returnYearCO").val(newReturnDate.getFullYear());
				OJ("#returnDateCO").change();
			}
		}
	});

	OJ("#returnCO").datepicker({
		dateFormat:dateFormatRegional,
		altFormat: "dd/mm/yy",
		altField: "#returnDateCO",
		beforeShow: customRange,
		defaultDate: returnDate,
		onSelect: function(dateText, inst){
			var dateArray = OJ("#returnDateCO").val().split("/");
			OJ("#returnDayCO").val(dateArray[0]);
			OJ("#returnMonthCO").val(dateArray[1]);
			OJ("#returnYearCO").val(dateArray[2]);
			OJ("#returnDateCO").change();
		}
	});
	
function customRange(input) 
{ 
	var min = new Date(); //This is todays date
		var dateMin = min; 
		var dayRange = 1;  // This is the number of days after todays date to block out
		
		if (input.id == "departCO") 
		{
			dateMin.setDate(dateMin.getDate() + dayRange);
		}
		else if (input.id == "returnCO")
		{
			if (OJ("#departCO").datepicker("getDate") != null) 
			{
				dateMin = OJ("#departCO").datepicker("getDate");
			}
		}
		return {
				minDate: dateMin 
			}; 
}

});
function toggleServiceMenu(){
	var serviceSelection = document.getElementById("serviceSelection");
	var displayVal = serviceSelection.style.display;
	
	if(displayVal == "none"){
		serviceSelection.style.display = "block";
	}
	else if(displayVal == "block"){
		serviceSelection.style.display = "none";
	}

}

function embedChangeSearchType(newSearchType){
	toggleServiceMenu();
	changeSearchType(newSearchType);

	if(newSearchType == 'F'){
		document.getElementById("serviceLabel").innerHTML = document.getElementById("title-bar-flight-a").innerHTML;
	}
	else if(newSearchType == 'H'){
		document.getElementById("serviceLabel").innerHTML = document.getElementById("title-bar-hotel-a").innerHTML;
	}
	else if(newSearchType == 'HF'){
		document.getElementById("serviceLabel").innerHTML = document.getElementById("title-bar-flightHotel-a").innerHTML;
	}

}

function changeTopBar(newSearchType){
	OJ("#title-bar-flight").removeClass("active");
	OJ("#title-bar-hotel").removeClass("active");
	OJ("#title-bar-flightHotel").removeClass("active");
	
	if(newSearchType == 'F'){
		OJ("#title-bar-flight").addClass("active");
	}
	else if(newSearchType == 'H'){
		OJ("#title-bar-hotel").addClass("active");
	}
	else if(newSearchType == 'HF'){
		OJ("#title-bar-flightHotel").addClass("active");
	}
}
