var numSegmentsMultiDest = 0;

function displayMultiDestSearchSegments(){
	set_display("airCrossSell",false);
	set_display("multiDestSegmentDisplay1Label", true);
	set_display("multiDestSegmentDisplay1", isAirOnlyIncluded());
	set_display("multiDestSegmentDisplay2", isAirOnlyIncluded());
	set_display("multiDestSegmentDisplay3", isAirOnlyIncluded());
	set_display("addAnotherFlight1", true);
	set_display("addAnotherFlight2", true);
	set_display("addAnotherFlight3", true);
	
	set_display("removeAFlight1", true);
	set_display("removeAFlight2", true);
	set_display("removeAFlight3", true);
	
	set_display("RoundTripTable", false);
	set_display("airDatesDisplay", false);
	set_display("returnDate", false);
	setNumberOfSegments(3);
}

function onPageReloadForMultiCity(num){
	if(num == 6){
		set_display("multiDestSegmentDisplay6", true);
		set_display("addAnotherFlight1", false);
		set_display("addAnotherFlight2", false);
		set_display("addAnotherFlight3", false);
		set_display("removeAFlight6", true);
		
		set_display("multiDestSegmentDisplay5", true);
		set_display("removeAFlight5", true);
		
		set_display("multiDestSegmentDisplay4", true);
		set_display("removeAFlight4", true);
	} else if(num == 5){
		set_display("multiDestSegmentDisplay5", true);
		set_display("addAnotherFlight5", true);
		set_display("removeAFlight5", true);
		
		set_display("multiDestSegmentDisplay4", true);
		set_display("addAnotherFlight4", true);
		set_display("removeAFlight4", true);
	} else if (num == 4){
		set_display("multiDestSegmentDisplay4", true);
		set_display("addAnotherFlight4", true);
		set_display("removeAFlight4", true);
	} else if (num == 2){
		set_display("multiDestSegmentDisplay3", false);
		set_display("addAnotherFlight3", false);
		set_display("removeAFlight3", false);
	} else if (num == 1){
		set_display("multiDestSegmentDisplay2", false);
		set_display("multiDestSegmentDisplay3", false);
		set_display("addAnotherFlight3", false);
		set_display("removeAFlight3", false);
		
		set_display("addAnotherFlight2", false);
		set_display("removeAFlight2", false);
	}
	
	
	
}

function setNumberOfSegments(num){
	numSegmentsMultiDest = num;
	var numSegments = document.getElementById("numAirSearchSegments");
	numSegments.value = num;
}

function displayOneWaySegment(){
	set_display("airCrossSell",false);
	disableAllFlightSegments();
	set_display("multiDestSegmentDisplay1Label", false);
	set_display("multiDestSegmentDisplay1", isAirOnlyIncluded());
	setNumberOfSegments(1);
}

function displayRoundTripSegments(){
	set_display("addAnotherFlight1", false); //only in IE this is needed
    set_display("airCrossSell", (!isHotelIncluded() && !isCarIncluded() && ! isASIncluded() && isStandAloneAirAndDDAllowedForAdvancedSearch() && isAHAllowdForDD()));
	disableAllFlightSegments();
	set_display("multiDestSegmentDisplay1", true);
	set_display("multiDestSegmentDisplay1Label", false);
	set_display("returnDate", true);
	setNumberOfSegments(1);
}

function disableAllFlightSegments(){
	for (var i=1; i<=6; i++){
		set_display("multiDestSegmentDisplay"+i, false);
		set_display("addAnotherFlight"+i, false);
		set_display("removeAFlight"+i, false);	
	}
	set_display("returnDate", false);
	set_display("multiDestSegmentDisplay1Label", false);
	set_display("addAnotherFlight", false);	
	set_display("removeAFlight", false);
}

function addAnotherFlightSegment(currIndex){
	//say currIndex = 1. then, lets get all the entered data (loc & dates) and try pushing 1 down.
	//and clear the next
	//and then display the numSegmentsMultiDest+1 th segment
	var fromLoc = new Array(6) ;
	var toLoc = new Array(6) ;
	var fromStyleColor = new Array(6) ;
	var toStyleColor = new Array(6) ;
	var departDt =  new Array(6);
	var departTime =  new Array(6);
	for (var i=1; i<=numSegmentsMultiDest; i++){
			fromLoc[i] = document.getElementById("fromLocation"+i).value;
			toLoc[i] = document.getElementById("toLocation"+i).value;
			fromStyleColor[i] = document.getElementById("fromLocation"+i).style.color;
			toStyleColor[i] = document.getElementById("toLocation"+i).style.color;
			departDt[i] = document.getElementById("dateairFromDate"+i).value;
			departTime[i] = document.getElementById("fromTime"+i).value;
	}
	var k = currIndex +2;
	for (var j=currIndex+1 ; j<=numSegmentsMultiDest && k < 7; j++){
			
			document.getElementById("fromLocation"+k).value = fromLoc[j] ;
			document.getElementById("fromLocation"+k).style.color = fromStyleColor[j] ;
			document.getElementById("toLocation"+k).value = toLoc[j] ;
			document.getElementById("toLocation"+k).style.color = toStyleColor[j] ;
			document.getElementById("dateairFromDate"+k).value = departDt[j] ;
			document.getElementById("fromTime"+k).value = departTime[j] ;
			k++;
	}
	//reset fields to default text
	var filedsToReset = currIndex +1;
	document.getElementById("fromLocation"+filedsToReset).value = "" ;
	document.getElementById("toLocation"+filedsToReset).value = "" ;
	document.getElementById("fromTime"+filedsToReset).value = "AnyTime" ;
	
	//For dates, we need to set the just previous value (currIndex) to maintain the ascending order.
	document.getElementById("dateairFromDate"+filedsToReset).value = document.getElementById("dateairFromDate"+currIndex).value;
	
	resetLocFields(document.getElementById("fromLocation"+filedsToReset),
		document.getElementById("toLocation"+filedsToReset));

	setNumberOfSegments(++numSegmentsMultiDest);
	set_display("multiDestSegmentDisplay"+numSegmentsMultiDest, true);
	if(numSegmentsMultiDest <6 ){
		set_display("addAnotherFlight"+numSegmentsMultiDest, true);
	}
	set_display("removeAFlight"+numSegmentsMultiDest, true);

	if(numSegmentsMultiDest == 6){
		for(var z=1; z<=6; z++){
			set_display("addAnotherFlight"+z, false);
		}
	}
	
}

function removeAFlight(currIndex){

	//say currIndex = 1. then, lets get all the entered data (loc & dates) and try pushing 1 up.
	//and then stop dispalying the numSegmentsMultiDest+1 th segment
	var fromLoc = new Array(6) ;
	var toLoc = new Array(6) ;
	var fromStyleColor = new Array(6) ;
	var toStyleColor = new Array(6) ;
	var departDt =  new Array(6);
	var departTime =  new Array(6);
	for (var i=1; i<=numSegmentsMultiDest; i++){
			fromLoc[i] = document.getElementById("fromLocation"+i).value;
			toLoc[i] = document.getElementById("toLocation"+i).value;
			fromStyleColor[i] = document.getElementById("fromLocation"+i).style.color;
			toStyleColor[i] = document.getElementById("toLocation"+i).style.color;
			departDt[i] = document.getElementById("dateairFromDate"+i).value;
			departTime[i] = document.getElementById("fromTime"+i).value;
	}
	var k = currIndex ;
	for (var j=currIndex+1 ; j<=numSegmentsMultiDest && k <=numSegmentsMultiDest ; j++){
			
			document.getElementById("fromLocation"+k).value = fromLoc[j] ;
			document.getElementById("fromLocation"+k).style.color = fromStyleColor[j] ;
			document.getElementById("toLocation"+k).value = toLoc[j] ;
			document.getElementById("toLocation"+k).style.color = toStyleColor[j] ;
			document.getElementById("dateairFromDate"+k).value = departDt[j] ;
			document.getElementById("fromTime"+k).value = departTime[j] ;
			k++
	}
	
	set_display("multiDestSegmentDisplay"+numSegmentsMultiDest, false);
	setNumberOfSegments(--numSegmentsMultiDest);
	if(numSegmentsMultiDest < 6 ){
		for(var m=1; m<=numSegmentsMultiDest; m++){
			set_display("addAnotherFlight"+m, true);
		}
	}
	
	if(numSegmentsMultiDest == 1){
		for(var z=1; z<=6; z++){
			set_display("removeAFlight"+z, false);
		}
	}

}

function resetFlightSearchType()
{
	var types =  document.main.flightSearchType;
    types[0].checked = true;
    types[1].checked = false;
    types[2].checked = false;
    
}
function onUpdateProducts(screenActive, nRooms, maxChildren)
{  

	if (!Browser.isFullDHTML())
	{
		if (screenActive)
			document.forms["main"].submit()
		return
	}
	set_display("hotelDisplay1", isHotelIncluded() && isAirIncluded())
	for (var i=2; i<=3; i++)
		set_display("hotelDisplay"+i, isHotelIncluded() || isHotelOnlyIncluded())
	
	set_display("hotelLocationDisplay", !isHotelOnlyIncluded())
	set_display("packagesHotelList", !isHotelOnlyIncluded());
	set_display("hotelOnlyHotelList", isHotelOnlyIncluded());
	set_display("StayingIn0", isHotelIncluded());
	set_display("StayingIn1", isHotelIncluded());
	set_display("airDisplay3",isAirIncluded()&& isStandAloneAirAndDDAllowedForAdvancedSearch());	
	set_display("airCrossSell", (!isHotelIncluded() && !isCarIncluded() && !isASIncluded() && isStandAloneAirAndDDAllowedForAdvancedSearch() && isAHAllowdForDD()));
	set_display("searchButton", (!isAirOnlyIncluded() &&(isAirIncluded()||isCarIncluded() || isHotelIncluded() || isASIncluded())));
	set_display("searchFlightButton", (!isASIncluded() && !isHotelIncluded() && !isCarIncluded() && isStandAloneAirAndDDAllowedForAdvancedSearch()));
	
	set_display("airAdvancedSearch",isAirIncluded()&& isStandAloneAirAndDDAllowedForAdvancedSearch());
	set_display("directFlight2",isAirIncluded());
	if(isAirOnlyIncluded()){
		for (var i=1; i<=4; i++){
			set_display("airDisplay"+i, false);
		}
		mode = document.forms["main"].mode.value;
		if (mode == 'advanced')	{
			set_display("airDisplay4", true);
		}
		set_display("airDisplay3", true);
		
		
	    set_display("RoundTripTable", false);
	    set_display("airDatesDisplay", false);
	    
	    var types =  document.main.flightSearchType;
    	if(types[0].checked){
	    	displayRoundTripSegments();
	    } else if (types[1].checked){
	    	displayOneWaySegment();
	    } else {
	    	var numSeg = document.getElementById("numAirSearchSegments").value;
	    	disableAllFlightSegments();
	    	displayMultiDestSearchSegments();
	    	numSegmentsMultiDest = numSeg;
	    	onPageReloadForMultiCity(numSeg);
	    	setNumberOfSegments(numSegmentsMultiDest);
	    }
	} else {
		for (var i=1; i<=4; i++){
			set_display("airDisplay"+i, isAirIncluded())
		}
		set_display("airDatesDisplay", isAirIncluded());
		set_display("RoundTripTable", true);
		disableAllFlightSegments();
		resetFlightSearchType();
	}
	set_display("flightSearchTypes", isAirOnlyIncluded());
	set_display("airDisplay3",isAirIncluded()&& isStandAloneAirAndDDAllowedForAdvancedSearch());

	if (isHotelOnlyIncluded())
	{
		set_display("restEasyDisplay", true);
	}
	else
	{	
		set_display("restEasyDisplay", false);
	}		
	
	set_display("carDisplay1", isCarIncluded())//Basic Seach Car Only
	set_display("carPackageDisplay1", (isAirIncluded() || isHotelIncluded()))//Basic Seach Car for Package
	set_display("carDisplay2", (isCarIncluded() && isStandAloneCarAndDDAllowedForAdvancedSearch())) // Header Styles for Car fields.
	set_display("carDisplay3", (isCarIncluded() && isStandAloneCarAndDDAllowedForAdvancedSearch()))//Advanced Search fields for Car. Hide fields for Car Only
	set_display("carCompany",  !isCarOnlyIncluded());
	set_display("carOnlyCompany", isCarOnlyIncluded()); 
	
	
	set_display("roomText", isHotelIncluded())
	set_display("roomText1", isHotelIncluded())		

	set_display("adultList1", true)
	set_display("adultList2", false)
	set_display("minorList1", true)
	set_display("minorList2", false)

	var isAC = isProductIncluded("AC");
	
	if(isAC || isCarOnlyIncluded() || isASIncluded() || isAirOnlyIncluded() )
	{
	    document.main.rooms.selectedIndex = 0;
		nRooms = 1;
	}
	updateMinors(eval(maxChildren))

	if(isHotelIncluded() && nRooms == 2)
	{
		set_display("adultList1", false)
		set_display("adultList2", true)
		set_display("minorList1", false)
		set_display("minorList2", true)
	}
	
	//if air only comp s selected, 
	//get the current max number in the adult combo
	//if it is same as getMaxAdultsCountAirOnly(), no need to make any changes
	//else, if it is less, add the remaining number of options
	//also, on change of the adults1 combo, we need to set the hidden var as well.
	//alert(isAirOnlyIncluded());
	if(isAirOnlyIncluded()){
	    var comboObj = document.main.adults1;
		var currMaxVal = comboObj.length; //.childElementCount doesnt work in IE
		//var currSelectedIndex = comboObj.selectedIndex;
		//alert(currSelectedIndex);
		var maxVal = getMaxAdultsCountAirOnly();
		if(currMaxVal != maxVal){
			if(currMaxVal < maxVal){
				//add new option/s at the end	
				for(adtIndex=currMaxVal+1;adtIndex <= maxVal ; adtIndex++){
					var newOption = document.createElement('option');
	    			newOption.text = adtIndex;
	   	            newOption.value = adtIndex;
	   				 try {
	      				comboObj.add(newOption, null); // standards compliant; doesn't work in IE
	   				 } catch(ex) {
	     				 comboObj.add(newOption); // IE only
	   				 }
				}
			}
		} 
		//Retain the selection. 
		//AHC - user selects rooms - 2,adults - 8 and then changes to A only.
		var roomsCnt = eval(document.main.rooms.options[document.main.rooms.selectedIndex].value);
		if(roomsCnt == 2){
			var adultsCnt2 = document.main.adults2.selectedIndex;
			comboObj.selectedIndex = adultsCnt2+1;
		}
	}
	//if air only comp is not selected, we may have to remove some options 
	//get the current max number in the adult combo
	//if it is same as getMaxAdultsCountNotAirOnly(), no need to make any changes
	//else, if it is more, remove the extra number of options
	if(!isAirOnlyIncluded()){
	    var comboObj = document.main.adults1;
		var currMaxVal = comboObj.length; //.childElementCount doesnt work in IE
		var maxVal = getMaxAdultsCountNotAirOnly(); 
		if(currMaxVal != maxVal){
			if(currMaxVal > maxVal){
				//remove new option/s at the end	
				for(adtIndex=maxVal+1;adtIndex <= currMaxVal ; adtIndex++){
					comboObj.remove(comboObj.length - 1);
				}
			}
		} 
	}

	if (isHotelIncluded() && displayHotelDiffDatesOption())
	{
		if (!isAirIncluded())
		{
			set_display("hotelDatesDisplay", true);

			if (!document.forms["main"].changeHotelDates.checked)
			{
				sdt_copy("airFromDate", "hotelFromDate")
				sdt_copy("airToDate",   "hotelToDate")
			}
		}
		else
		{
			if (document.main.changeHotelDates.checked==false)
			{
				set_display("hotelDatesDisplay", false);
			}
			else
			{
				set_display("hotelDatesDisplay", true);
			}
		}
	}
	else
	{
		set_display("hotelDatesDisplay", false);
	}

	if (!isCarIncluded())
	{		
		set_display("carDatesDisplay", false)
		set_display("AdvancedSearch", true)
		set_display("carTravellersDisplay",true);
		set_display("carDriverAgeText",false);
		set_display("carDriverAgeDisplay",false);		
	}
	else if (isCarOnlyIncluded())
	{
	   var form = document.main
	   form.rooms.selectedIndex = 0;
	   form.adults1.selectedIndex = 0;   
   	   set_display("carDatesDisplay", true)
	   set_display("AdvancedSearch", true)
	   document.main.changeCarDates.checked = true	
	   set_display("carDriverAgeText",true);
	   set_display("carDriverAgeDisplay",true);
	   set_display("carTravellersDisplay",false);	
	}else 
	{
		set_display("AdvancedSearch", true)	
		set_display("carDatesDisplay", document.main.changeCarDates.checked)
		set_display("carDriverAgeText",false);
		set_display("carDriverAgeDisplay",false);
		set_display("carTravellersDisplay",true);		
	}
	if (isCarOnlyIncluded()){
		set_display("caronlyLocationDisplay",true);
		set_display("caronlyLocationDisplayPackage",false);
	}
	else{
		set_display("caronlyLocationDisplay",false);
		set_display("caronlyLocationDisplayPackage",true);
	}
	if(isASIncluded()){
		set_display("caronlyLocationDisplay",false);
		set_display("carTravellersDisplay",false);
	}
	if (!isStandAloneCarAndDDAllowedForAdvancedSearch() || isASIncluded() || !isStandAloneAirAndDDAllowedForAdvancedSearch())
	{
		set_display("AdvancedSearch", false)
		set_display("BasicSearch", false)
	}else
	{
		mode = document.forms["main"].mode.value
		if (mode == 'advanced')
		{
			set_display("AdvancedSearch", false)
			set_display("BasicSearch", true)
		}else
		{
			set_display("AdvancedSearch", true)
			set_display("BasicSearch", false)
		}	
	}	
	
	for (i=1; i<=4; i++)
		set_visibility("carTimeDisplayInHotelDate"+i, isCarIncluded() && isHotelIncluded() && !isAirIncluded() && !isHotelOnlyIncluded() && !document.main.changeCarDates.checked)

	adjustStepsSeq();
	return true
}


function onClickChangeCarDates(screenActive)
{
	if (!Browser.isFullDHTML())
	{
		if (screenActive)
			document.forms["main"].submit()
		return
	}

	var isChecked = document.forms["main"].changeCarDates.checked
	if (isChecked)
	{
		if (screenActive)
		{
			if (isAirIncluded())
			{
				sdt_copy("airFromDate", "carFromDate")
				sdt_copy("airToDate",   "carToDate")
			}
			else
			{
				sdt_copy("hotelFromDate", "carFromDate")
				sdt_copy("hotelToDate",   "carToDate")
			}
		}
		for (i=1; i<=4; i++)
			set_visibility("carTimeDisplayInHotelDate"+i, false)
	}
	else if ((!isAirIncluded()) && isHotelIncluded() && !isHotelOnlyIncluded())
	{
		for (i=1; i<=4; i++)
			set_visibility("carTimeDisplayInHotelDate"+i, true)
	}
	if (isCarOnlyIncluded())
	{
		set_display("carDatesDisplay", true)
		set_display("carDatesHelp", true)
		document.main.changeCarDates.checked = true
	}
	else
	{
		set_display("carDatesDisplay", isChecked)
		set_display("carDatesHelp", isChecked)
	}
}

function onClickChangeHotelDates(screenActive)
{
	if (!Browser.isFullDHTML())
	{
		if (screenActive)
			document.forms["main"].submit()
		return
	}
	var isChecked = false
	if(displayHotelDiffDatesOption())
	{
		isChecked = document.forms["main"].changeHotelDates.checked
		if (isChecked && isAirIncluded() && screenActive)
		{
			sdt_copy("airFromDate", "hotelFromDate")
			sdt_copy("airToDate",   "hotelToDate")
		}
				
		if(isHotelOnlyIncluded() || (isHotelIncluded() && !isAirIncluded()) )
		{
		
			set_display("hotelDatesDisplay", true)
			set_display("hotelDatesHelp", true)
		}
		else
		{
			set_display("hotelDatesDisplay", isChecked)
			set_display("hotelDatesHelp", isChecked)
		}
		
	}
}

function isAirIncluded()
{
	return isProductIncluded("A");
}

function isHotelIncluded()
{
	return isProductIncluded("H");
}

function isCarIncluded()
{
	return isProductIncluded("C");
}

function isHotelOnlyIncluded()
{
	return isHotelIncluded() && !(isAirIncluded() ||isCarIncluded());
}

function isASIncluded()
{
	return isProductIncluded("S");
}

function isAirOnlyIncluded()
{
	return isAirIncluded() && !(isHotelIncluded() ||isCarIncluded());
}
function isCarOnlyIncluded()
{
	return isCarIncluded() && !(isAirIncluded() || isHotelIncluded());
}

function onCrossSellAH(prefix,
					   maxChildren,
					   maxPassengers)
{
	var form = document.main
		
	nRooms      = 1
	MAX_CHILDREN   = eval(maxChildren)
	MAX_PASSENGERS = eval(maxPassengers)

	var adultsObject = eval("document.main."+prefix+"adults"+nRooms)
	var minorsObject = eval("document.main."+prefix+"minors"+nRooms)
	
	nAdults     = eval(adultsObject.options[adultsObject.selectedIndex].value)
	nChildren = eval(minorsObject.options[minorsObject.selectedIndex].value)
	if(nAdults+nChildren > maxPassengers)
	{
		alert(msg_0988(maxPassengers))
		set_display("adultList1", true)
		set_display("adultList2", false)
		set_display("minorList1", true)
		set_display("minorList2", false)
		adultsObject.selectedIndex = 0;
		minorsObject.selectedIndex = 0;
		isCrossSellSearch('A')
		updateMinors(eval(maxChildren))
		return false
	}
	else if(nAdults+nChildren <= 4)
	{
		set_display("adultList1", true)
		set_display("adultList2", false)
		set_display("minorList1", true)
		set_display("minorList2", false)
		var adults2Object = eval("document.main."+prefix+"adults"+2)
		var minors2Object = eval("document.main."+prefix+"minors"+2)
		adults2Object.selectedIndex = 0
		minors2Object.selectedIndex = 0
		form.rooms.selectedIndex = nRooms-1
		updateMinors(eval(maxChildren))
		return true
	}
	else if(nAdults+nChildren > 4)
	{
		nRooms = 2;
		set_display("adultList1", false)
		set_display("adultList2", true)
		set_display("minorList1", false)
		set_display("minorList2", true)
		var adults2Object = eval("document.main."+prefix+"adults"+nRooms)
		var minors2Object = eval("document.main."+prefix+"minors"+nRooms)
		<!-- Adults2List starts with min of 2 adults so subtract the number of adults by 2 to set the adults2list-->
		adults2Object.selectedIndex = nAdults - 2
		minors2Object.selectedIndex = nChildren
		form.rooms.selectedIndex = nRooms-1
		adultsObject.selectedIndex = 0
		minorsObject.selectedIndex = 0
		updateMinors(eval(maxChildren))
		return true
	}
}


function onAirOnlySearchValidate(prefix,
					   maxChildren,
					   maxPassengers)
{
	var form = document.main
	nRooms      = 1
	form.rooms.selectedIndex = nRooms-1
	
	MAX_CHILDREN   = eval(maxChildren)

	var adultsObject = eval("document.main."+prefix+"adults"+nRooms)
	var minorsObject = eval("document.main."+prefix+"minors"+nRooms)
	
	nAdults     = eval(adultsObject.options[adultsObject.selectedIndex].value)
	nChildren = eval(minorsObject.options[minorsObject.selectedIndex].value)
	if(nAdults+nChildren > maxPassengers)
	{
		alert(msg_0988(maxTravellers))
		set_display("adultList1", true)
		set_display("adultList2", false)
		set_display("minorList1", true)
		set_display("minorList2", false)
		adultsObject.selectedIndex = 0;
		minorsObject.selectedIndex = 0;
		updateMinors(eval(maxChildren))
		return false
	}
	else
	{
		updateMinors(eval(maxChildren))
		return true
	}
}

function onChangePassengersOrRooms(caller,
								   prefix,
								   maxChildren,
								   maxPassengers,
								   maxChildrenAC,
								   maxPassengersAC,
								   screenActive)
{
	var form = document.main
	var ADULTS = 'ADULTS'
	var MINORS = 'MINORS'
	var ROOMS  = 'ROOMS'
	var isAC = isProductIncluded("AC");	
	nRooms      = 1
	if (form.rooms != null)
		nRooms = eval(form.rooms.options[form.rooms.selectedIndex].value)
		
	MAX_CHILDREN   = eval(maxChildren)
	MAX_PASSENGERS = eval(maxPassengers)
	
	if(isAC || isCarOnlyIncluded() || isASIncluded() || isAirOnlyIncluded())
	{
		nRooms      = 1
		MAX_CHILDREN   = eval(maxChildrenAC)
		maxChildren = maxChildrenAC;
		if(isAirOnlyIncluded()){
			MAX_PASSENGERS = getMaxAdultsCountAirOnly();
			maxPassengers = getMaxAdultsCountAirOnly();
		} else {
			MAX_PASSENGERS = eval(maxPassengersAC)
			maxPassengers = maxPassengersAC;
		}
	}
	
	if((caller == MINORS || caller == ROOMS) && nRooms == 2)
	{	
		MAX_CHILDREN   = eval(maxChildrenAC)
		MAX_PASSENGERS = eval(maxPassengersAC)
		set_display("adultList1", false)
		set_display("adultList2", true)
		set_display("minorList1", false)
		set_display("minorList2", true)
	}
	
	if((caller == MINORS || caller == ROOMS) && nRooms == 1)
	{	
		set_display("adultList1", true)
		set_display("adultList2", false)
		set_display("minorList1", true)
		set_display("minorList2", false)
	}
	
	var adultsObject = eval("document.main."+prefix+"adults"+nRooms)
	var minorsObject = eval("document.main."+prefix+"minors"+nRooms)
	
	nAdults     = eval(adultsObject.options[adultsObject.selectedIndex].value)
	nChildren   = eval(minorsObject.options[minorsObject.selectedIndex].value)
	
	// No more than 'Max travelers' allowed
	if (isCarOnlyIncluded() || isASIncluded() || isAirOnlyIncluded())
	{
	   if(isAirOnlyIncluded()){
	   		maxTravellers = MAX_PASSENGERS; 
	   } else {
			maxTravellers = MAX_PASSENGERS - 2; 
		}
		if(nAdults + nChildren > maxTravellers)
		{
			alert(msg_0988(maxTravellers))
			adultsObject.selectedIndex = 0;
			minorsObject.selectedIndex = 0;
		}
	}
	if (!(isCarOnlyIncluded() || isASIncluded() || isAirOnlyIncluded()) && (nAdults + nChildren > MAX_PASSENGERS))
	{
		if(nRooms == 1)
			room = 'room';

		if(nRooms == 2)
			room = 'rooms'; 
		
		if(isAC)  
		{
			alert(msg_0988(MAX_PASSENGERS))
		}
		else
		{
			alert(msg_0283(MAX_PASSENGERS, nRooms, room))
		}
		adultsObject.selectedIndex = 0;
		minorsObject.selectedIndex = 0;
	}
	else
	if (isHotelIncluded() &&
		nAdults == 1 &&
		nChildren > MAX_CHILDREN)
	{
		// If 1 adult, no more than 'MAX_CHILDREN' children allowed
		
		alert(msg_0291())

		if (caller == ADULTS)
			adultsObject.selectedIndex = 0;
		else if (caller == MINORS)
			minorsObject.selectedIndex = MAX_CHILDREN;
	
	}
	else
	{
		// Room count: check even if hotel is not included.
		// But, display messages only in cases where it is
		// included.
		var maxRooms = Math.min(nAdults, 4)

			// Min rooms = 1 or 2 (depends on # of travelers: if more than 5, 2 rooms , else 1 room)
			var minRooms = (nAdults + nChildren)/4 > 1 ? 2 : 1;

			// 1 exception: 5 all adult travelers can't stay in 1 room
			if (nAdults == 5 && nChildren == 0)
				minRooms = 2;

			if (nRooms < minRooms)
			{
				if (isHotelIncluded())
				{
					alert(msg_0293())
					form.rooms.selectedIndex = minRooms-1;
				}
			}
	}
	updateMinors(eval(maxChildrenAC))
}

//Property Search
function onChangePropertySearchPassengersOrRooms(caller,
								   prefix,
								   maxChildren,
								   maxPassengers,
								   maxChildrenAC,
								   maxPassengersAC,
								   screenActive)
{
	var form = document.main
	var ADULTS = 'ADULTS'
	var MINORS = 'MINORS'
	var ROOMS  = 'ROOMS'	
	nRooms      = 1
	if (form.rooms != null)
		nRooms = eval(form.rooms.options[form.rooms.selectedIndex].value)
		
	MAX_CHILDREN   = eval(maxChildren)
	MAX_PASSENGERS = eval(maxPassengers)
	
	if((caller == MINORS || caller == ROOMS) && nRooms == 2)
	{	
		MAX_CHILDREN   = eval(maxChildrenAC)
		MAX_PASSENGERS = eval(maxPassengersAC)
		set_display("adultList1", false)
		set_display("adultList2", true)
		set_display("minorList1", false)
		set_display("minorList2", true)
	}
	
	if((caller == MINORS || caller == ROOMS) && nRooms == 1)
	{	
		set_display("adultList1", true)
		set_display("adultList2", false)
		set_display("minorList1", true)
		set_display("minorList2", false)
	}
	
	var adultsObject = eval("document.main."+prefix+"adults"+nRooms)
	var minorsObject = eval("document.main."+prefix+"minors"+nRooms)
	
	nAdults     = eval(adultsObject.options[adultsObject.selectedIndex].value)
	nChildren   = eval(minorsObject.options[minorsObject.selectedIndex].value)
	
	
	if (isHotelIncluded() &&
		nAdults == 1 &&
		nChildren > MAX_CHILDREN)
	{
		// If 1 adult, no more than 'MAX_CHILDREN' children allowed
		
		alert(msg_0291())

		if (caller == ADULTS)
			adultsObject.selectedIndex = 0;
		else if (caller == MINORS)
			minorsObject.selectedIndex = MAX_CHILDREN;
	
	}
	else
	{
		// Room count: check even if hotel is not included.
		// But, display messages only in cases where it is
		// included.
		var maxRooms = Math.min(nAdults, 4)

			// Min rooms = 1 or 2 (depends on # of travelers: if more than 5, 2 rooms , else 1 room)
			var minRooms = (nAdults + nChildren)/4 > 1 ? 2 : 1;

			// 1 exception: 5 all adult travelers can't stay in 1 room
			if (nAdults == 5 && nChildren == 0)
				minRooms = 2;

			if (nRooms < minRooms)
			{
				if (isHotelIncluded())
				{
					alert(msg_0293())
					form.rooms.selectedIndex = minRooms-1;
				}
			}
	}
	updateMinors(eval(maxChildrenAC))
}


function updateRooms(screenActive)
{
	var form         = document.main

	if (!Browser.isFullDHTML())
	{
		if (screenActive)
			form.submit()  // server-side handling where js doesn't work
		return false
	}

	nRooms = 1
	if (form.rooms != null)
		nRooms = eval(form.rooms.options[form.rooms.selectedIndex].value)

	for (var i=2; i<=4; i++)
	{
		set_display("displayRoom"+i, nRooms >= i)
	}

	return true
}


function updateMinors(maxChildren)
{
	var MAX_CHILDREN = eval(maxChildren)
	var form         = document.main
	
	if (form.rooms != null)
			nRooms = eval(form.rooms.options[form.rooms.selectedIndex].value)
	
	if(!isHotelIncluded())
	   nRooms = 1;
	   
	if (Browser.isFullDHTML())
	{
		for (var i=1; i<=MAX_CHILDREN; i++)
		{
			
			var minorElement = document.getElementById("minor"+i)
			var ageList      = document.getElementById("childAge"+i)
			var minorsElement = document.getElementById("minors"+nRooms)
			
			if (minorElement)
			{
				set_display("minor"+i, minorsElement.options[minorsElement.selectedIndex].value >= i)
				if (i==1)
				{
					set_display("minorlbl", minorsElement.options[minorsElement.selectedIndex].value >= i)
					set_display("minornote", minorsElement.options[minorsElement.selectedIndex].value >= i)
				}
				if (minorsElement.options[minorsElement.selectedIndex].value < i)
					ageList.selectedIndex=0
			}
		}
		return true
	}
}

function validateChildAges()
{
	var form = document.main
	
	if (form.rooms != null)
		nRooms = eval(form.rooms.options[form.rooms.selectedIndex].value)
	
	if(!isHotelIncluded())
	   nRooms = 1;
	if(isCarOnlyIncluded()){
   		//Reset the minors count and ages
   		var minorsElement = document.getElementById("minors"+nRooms);
		for (var i=1; i<=minorsElement.options[minorsElement.selectedIndex].value; i++)
		{
			document.getElementById("childAge"+i).value = -1;
		}
		minorsElement.value = 0;
   	
   	} else {
		var minorsElement = document.getElementById("minors"+nRooms)
		for (var i=1; i<=minorsElement.options[minorsElement.selectedIndex].value; i++)
		{
			if (eval("document.main.childAge"+i).selectedIndex == 0)
				return false
		}
	}
	return true
}

function validateSearchParms(OPT_MaxMinorsTwoRooms, OPT_MaxPassengers)
{	
   	var res = true
	if (!validateChildAges())
	{
		alert(msg_0294())
		res = false
	}
	if(isAirOnlyIncluded())
	{
		if (!onAirOnlySearchValidate('',  OPT_MaxMinorsTwoRooms, OPT_MaxPassengers))
		{
			res = false
		}
		//for air only, if there are only one segment in the multi dest search form,
	    // the search type has to be set to "OneWay"
	    var types =  document.main.flightSearchType;
	    if(numSegmentsMultiDest == 1 && types[2].checked ){
	   		types[1].checked =  true;
	    } 		
	}
	return res
}

function validateCrossSellSearchParms( OPT_MaxMinorsTwoRooms, OPT_MaxPassengers)
{
	var res = true
	if (!validateChildAges())
	{
		alert(msg_0294())
		res = false
	}	
	isCrossSellSearch('AH');
	if (!onCrossSellAH('', OPT_MaxMinorsTwoRooms, OPT_MaxPassengers))
	{
		res = false
	}
	return res
}
function checkNumOfPassengers(num,
							  maxChildren,
							  maxPassengers)
{
	var form = document.forms["main"]
	nRooms = eval(form.rooms.options[form.rooms.selectedIndex].value)
	var sum             = form.adults.options[form.adults.selectedIndex].value;
	var numOfChildren   = 0;
	var MAX_CHILDREN    = eval(maxChildren)
	var MAX_PASSENGERS  = eval(maxPassengers)

	if (MAX_CHILDREN > 0 &&
		form.childAge1.options[form.childAge1.selectedIndex].value > 0)
	{
		sum++;numOfChildren++;
	}

	if (MAX_CHILDREN > 1 &&
		form.childAge2.options[form.childAge2.selectedIndex].value > 0)
	{
		sum++;numOfChildren++;
	}

	if (MAX_CHILDREN > 2 &&
		form.childAge3.options[form.childAge3.selectedIndex].value > 0)
	{
		sum++;numOfChildren++;
	}

	if (MAX_CHILDREN > 3 &&
		form.childAge4.options[form.childAge4.selectedIndex].value > 0)
	{
		sum++;numOfChildren++;
	}

	if (sum > MAX_PASSENGERS)
	{
		if(nRooms == 1)
			room = 'room';

		if(nRooms == 2)
			room = 'rooms'; 
		
		alert(msg_0283(MAX_PASSENGERS, nRooms, room))
		
		changeBack(form, num, numOfChildren, maxChildren);
	}
}

function changeBack(form,
					num,
					numOfChildren,
					maxChildren)
{
	var MAX_CHILDREN = eval(maxChildren)

	switch (num)
	{
		case 0:
		{
			form.adults.selectedIndex = (MAX_CHILDREN - numOfChildren) ;
			break;
		}
		case 1:
		{
			form.childAge1.selectedIndex = 0;
			break;
		}
		case 2:
		{
			form.childAge2.selectedIndex = 0;
			break;
		}
		case 3:
		{
			form.childAge3.selectedIndex = 0;
			break;
		}
		case 4:
		{
			form.childAge4.selectedIndex = 0;
			break;
		}
	}
}

// To handle AH search for RoundTrip
function copyLocationAndDateForAH()
{
	copyValuesForAH("fromLocation1", "fromLocation");
	copyValuesForAH("toLocation1", "toLocation");
	copyValuesForAH("dateairToDate1", "dateairToDate");
	copyValuesForAH("dateairFromDate1", "dateairFromDate");
}
function copyValuesForAH(from, toAH)
{
	document.getElementById(toAH).value = document.getElementById(from).value;
}


