/*#############################################################################

FILE:	SB/ataGlanceSearch.js

	Copyright (c) 2005 Navitaire Inc. All rights reserved.

	This source code is (i) proprietary to and a trade secret of Navitaire Inc.
	and (ii) protected by copyright law and international treaties.
	Unauthorized disclosure, reproduction, distribution or alteration of this
	source code, or any portion of it, may result in severe civil and criminal
	penalties and will be prosecuted to the maximum extent possible under
	the law.

	www.navitaire.com

DESCRIPTION:

	Contains javascript for the search form.  Note that this file relies on
	javascript from the jAtaGlanceSearch.wld template and dateValidation.js.

#############################################################################*/

	var searchText 	= new Object();
	var searchPrefs	= new Object();
	captureSearchText(searchText);
	captureSearchPrefs(searchPrefs);
	/*##############################################
	#
	# Function: setDest()
	#
	# Parameter: 
	#		mkt : 1 - outbound
	#		      2 - Inbound
	#			
	#
	# Description: 
	#	       Captures the selected destination city for outbound or inbound
	#	       to be put in the skylights form.
	#*/
	function setDest( mkt )
	{
		var toList		= document.getElementById( "to" + mkt + "Select" );
		eval( "document.skylightsForm.toCity" + mkt + ".value = toList.value" );
	}
	/*##############################################
	#
	# Function: resetDest()
	#
	# Parameter: 
	#		mkt : 1 - outbound
	#		      2 - Inbound
	#			
	#
	# Description: 
	#	       Resets the city options for destination depending on the selected 
	#	       origin city.
	#*/
	function resetDest( mkt )
	{
		var fromList		= document.getElementById( "from" + mkt + "Select" );
	
		//one roundtrip, this is called for market2, but we have hidden the controls
		if (!fromList)
		{
			return;
		}

		var toList		= document.getElementById( "to" + mkt + "Select" );
		
		var orig		= fromList.options[ fromList.selectedIndex ].value;
		var dest		= eval( "document.skylightsForm.toCity" + mkt + ".value" );

		toList.length	= 1;

		//toList.options[0]		= new Option("Destination");
		toList.options[0]		= new Option(toList.options[0].text);

		toList.options[0].value	= "???"

		if ( fromList.selectedIndex == 0 )
		{
			for (var i = 0; i < Airports.length; i++)
			{
				toList.length	+= 1;
				var l_ix		= toList.length - 1;
				var opt_str		= Airports[i].name;
				if ( searchPrefs.DISPLAY_AIRPORT_CITY_CODES == 'true' )
				{
					opt_str		+=  " (" + Airports[i].code + ")";
				}

				toList.options[l_ix]	= new Option( opt_str );
				toList.options[l_ix].value	= Airports[i].code;

				if ( dest == Airports[i].code )
				{
					toList.selectedIndex	= l_ix;
				}
			}
		}
		else
		{
			var air		= getAirport( orig );
			for (var i = 0; i < air.dests.length; i++)
			{
				dest_air		= getAirport( air.dests[i] );
				toList.length	+= 1;
				var l_ix		= toList.length - 1;
				var opt_str		= dest_air.name;
				if ( searchPrefs.DISPLAY_AIRPORT_CITY_CODES == 'true' )
				{
					opt_str		+=  " (" + dest_air.code + ")";
				}

				toList.options[l_ix]	= new Option( opt_str );
				toList.options[l_ix].value	= dest_air.code;

				if ( dest == dest_air.code )
				{
					toList.selectedIndex	= l_ix;
				}
			}
		}
		
		setDest( mkt );
	}
	/*##############################################
	#
	# Function: resetDest()
	#
	# Parameter: 
	#		checkDates: flag if roundtrip flight is selected
	#		 
	#			
	#
	# Description: 
	#	       Validates and adjust the inbound date drop down boxes referring to the outbound date.
	#	       
	#*/
	function resetDates(checkDates)
	{
		var m1d_ix		= document.skylightsForm.departDay1.selectedIndex;
		var m1y_ix		= document.skylightsForm.departMonth1.selectedIndex;

		if ( checkDates ) {
			if ( m1y_ix > document.skylightsForm.departMonth2.selectedIndex ) {
				document.skylightsForm.departMonth2.selectedIndex	=  m1y_ix;
				document.skylightsForm.departDay2.selectedIndex	=  m1d_ix;
			}
			if ( ( (m1y_ix) == document.skylightsForm.departMonth2.selectedIndex )
			  && ( m1d_ix > document.skylightsForm.departDay2.selectedIndex ) ) {
				document.skylightsForm.departDay2.selectedIndex	=  m1d_ix;
			}
		}
	}
	/*##############################################
	#
	# Function: validCities()
	#
	# Parameter: 
	#			
	#
	# Description: 
	#	       Verifies if both origin and destination cities are filled-up.
	#
	# Return:	
	#		True: both origin and destination is not blank
	#		false: if otherwise
	#*/
	function validCities()
	{
		var is_valid	= true;
		if ( document.skylightsForm.from1.selectedIndex == 0 )
		{
			alert( searchText.missingDepartCity );
			is_valid	= false;
		}
		else if ( document.skylightsForm.to1.selectedIndex == 0 )
		{
			alert( searchText.missingArriveCity );
			is_valid	= false;
		}
		else if ((searchPrefs.OFFER_OPEN_JAW_ROUTES == "true") && (document.skylightsForm.travel[2].checked))
		{
			is_valid = validateOpenJawConnections();
		}
		
		return is_valid;
	}
	/*##############################################
	#
	# Function: validPax()
	#
	# Parameter: 
	#			
	#
	# Description: 
	#	       Verifies if the number of passengers are valid with this conditions:
	#		1. Does not exceed the Max allowable number of passengers (setting in PREF)
	#		2. There should be 1 or more passengers
	#		3. Number of adults should be greater or equal to number of infants 
	#
	# Return:	
	#		True: satisfies all conditions
	#		false: if otherwise
	#*/
	function validPax()
	{
		var adults = document.skylightsForm.ADULT.value;
		var infants = document.skylightsForm.INFANT.value;
		var paxTotal = 0;
		
		for (i = 0; i < searchPrefs.paxTypes.length; i++)
		{
			var paxType 	= searchPrefs.paxTypes[i];
			var paxSelect	= document.getElementById(paxType +"Select");
			paxTotal		= eval(paxTotal) + eval(paxSelect.options[paxSelect.selectedIndex].value);
		}

		if ((paxTotal > searchPrefs.MAX_PASSENGERS_ALLOWED) && ( searchPrefs.MAX_PASSENGERS_ALLOWED > 0 ))
		{
			alert("\n"	+ searchText.popup_max_passenger_amount_1
						+ searchPrefs.MAX_PASSENGERS_ALLOWED
						+ searchText.popup_max_passenger_amount_2 + "\n");
			return false;
		}

		if ( paxTotal < 1)
		{
			alert(searchText.popup_missing_passenger_amount);
			return false;
		}

		if ( (adults  / infants) < 1 )
		{
			alert(searchText.popup_too_many_infants);
			document.skylightsForm.INFANT.focus();
/*
			// If there is a default, reset it
			if( document.skylightsForm.defaultADULT.value > -1 )
			{
				var adultSelect = document.skylightsForm.ADULT;
				for(x1=0; x1 < adultSelect.length; x1++)
				{
					if( adultSelect[x1].value == document.skylightsForm.defaultADULT.value )
					{
						document.skylightsForm.ADULT.selectedIndex = x1;
						break;
					}
				}
			}
			
			var infantSelect = document.skylightsForm.INFANT;
			for(x2=0; x2 < infantSelect.length; x2++)
			{
				if( (document.skylightsForm.defaultINFANT.value == -1 && infantSelect[x2].value == 0) || 
				    (document.skylightsForm.defaultINFANT.value != -1 && infantSelect[x2].value == document.skylightsForm.defaultINFANT.value) )
				{
					document.skylightsForm.INFANT.selectedIndex = x2;
					break;
				}
			}
*/			
			return false;
		}

		return true;
	}
	/*##############################################
	#
	# Function: validDates()
	#
	# Parameter: 
	#		beginDate: valid start date (set in PREFs)
	#		endDate:  valid end date (set in PREFs)
	#
	# Description: 
	#	       Checks the selected dates against the beginDate and endDate parameter
	#	       
	#
	# Return:	
	#		True: valid date/s
	#		false: if otherwise
	#*/
	function validDates(beginDate, endDate)
	{
		var is_valid			= true;
		var beginDateString		= new String (beginDate);
		var endDateString		= new String (endDate);
		var departDateString	= captureDateString("departDay1", "departMonth1");
		var arriveDateString	= captureDateString("departDay2", "departMonth2");

		date_message = searchText.popup_pre_flight_date_1 +
						searchText.popup_initial_flight_date +
						searchText.popup_pre_flight_date_2;
						
		if ( departDateString < beginDateString )
		{
			alert( date_message );
			setDate( "departDay1", "departMonth1", beginDateString );
			if( isRoundTrip() && arriveDateString < beginDateString )
				setDate( "departDay2", "departMonth2", beginDateString );
			return false;
		}

		if ( searchPrefs.final_date_used )
		{
			if( departDateString > endDateString )
			{
				is_valid = false;
				setDate( "departDay1", "departMonth1", endDateString );
			}
			if( isRoundTrip() && ( arriveDateString > endDateString ) )
			{
				is_valid = false;
				setDate( "departDay2", "departMonth2", endDateString );
			}
			if( !is_valid )
			{
				alert( searchText.popup_post_live_flight_date_1 +
						searchText.popup_final_flight_date +
						searchText.popup_post_live_flight_date_2 );
				return false;
			}
		}

		// check that dates are valid calandar dates
		if(!validateCalendarDate("departDay1", "departMonth1", true, true))
		{
			if( isRoundTrip() || ( !isOneWay() && searchPrefs.OFFER_OPEN_JAW_ROUTES == 'true' ) )
			{
				validateCalendarDate("departDay2", "departMonth2", false, true);
			}
			return false;
		}
		if(	( isRoundTrip() || ( !isOneWay() && searchPrefs.OFFER_OPEN_JAW_ROUTES == 'true' ) ) && !validateCalendarDate("departDay2", "departMonth2", true, true) )
		{
			return false;
		}

		if ( isRoundTrip() || ( !isOneWay() && searchPrefs.OFFER_OPEN_JAW_ROUTES == 'true' ) )
		{
			if ( !validateDateOverlap("departDay1", "departMonth1", "departDay2", "departMonth2", true, true) )
			{
				return false;
			}
			else if ( searchText.sameDayWarning && arriveDateString == '' + departDateString )
			{
				is_valid		= confirm( searchText.sameDayWarning );
			}
		}
		
		if (is_valid && (departDateString < 20110111))
		{
			sector1_o 	= document.skylightsForm.from1.value;
			sector1_d	= document.skylightsForm.to1.value;
			
			if ( (sector1_d == "TPE") || (sector1_o == "TPE") )
			{
				alert("Flights to/from Taipei start on 11 Jan 2011.\nPlease re-select your travel dates.");
				is_valid = false;
			}
		}
		
		return is_valid;
	}
	/*##############################################
	#
	# Function: formatDates()
	#
	# Parameter: 
	#
	# Description: 
	#	       Arranged the captured day and month values selected for inbound and outbound flights	
	#	       
	#
	#*/
	function formatDates()
	{
		document.skylightsForm.departDate1.value		= 		captureDateString("departDay1", "departMonth1");
		if ( document.skylightsForm.numberMarkets.value > 1 )
		{
			document.skylightsForm.departDate2.value	= captureDateString("departDay2", "departMonth2");
		}
	}
	/*##############################################
	#
	# Function: validateOpenJawConnections()
	#
	# Parameter: 
	#
	# Description: 
	#	       Verifies if the selected cities are valid for open jaw routes.
	#	       
	#
	# Return:	
	#		True: valid open jaw routes
	#		false: if otherwise
	#*/	
	function validateOpenJawConnections()
	{
		if ( ( ( document.skylightsForm.from1.value == document.skylightsForm.to2.value ) ||
				( document.skylightsForm.to1.value == document.skylightsForm.from2.value ) ) 
				&& ( document.skylightsForm.from2.value != '???') && ( document.skylightsForm.to2.value != '???'))
		{
			return true;
		}
		else
		{
			alert(searchText.popup_illogical_open_jaw);
			return false;
		}
	}


// Begin Oneway/Roundtrip javascript
	/*##############################################
	#
	# Function: isOneWay()
	#
	# Parameter: 
	#
	# Description: 
	#	       Returns true if one way radio button is selected, else.
	#	       false
	#
	#*/
	function isOneWay()
	{
		return ( document.skylightsForm.travel[1].checked );
	}
	/*##############################################
	#
	# Function: isRoundTrip()
	#
	# Parameter: 
	#
	# Description: 
	#	       Returns true if round trip radio button is selected, else.
	#	       false
	#
	#*/	
	function isRoundTrip()
	{
		return ( document.skylightsForm.travel[0].checked );
	}
	/*##############################################
	#
	# Function: swapTravel()
	#
	# Parameter: 
	#
	# Description: 
	#	       Dynamically changes the display in the search display
	#	       depending on the selected travel option( round trip, one way or open jaw)
	#
	#*/
	function swapTravel()
	{
		// switched to round trip
		if ( document.skylightsForm.travel[0].checked )
		{                                              			
			if(searchPrefs.ALLOWED_TRAVEL_TYPES == 'ONEWAY' && searchPrefs.OFFER_OPEN_JAW_ROUTES == 'false')
			{
				alert(searchText.popup_oneway);
				document.skylightsForm.travel[1].checked = 1;
			}
			else
			{
				document.skylightsForm.numberMarkets.value			= 2;
				showDiv(true, 'dateSel2');
				showDiv(true, 'TrvlOptLbl'); // felix added 
				if (searchPrefs.OFFER_OPEN_JAW_ROUTES == 'true')
				{
					showDiv(false, 'fromToSel2');
				}
			}
		}
		// switched to one way
		else if ( document.skylightsForm.travel[1].checked )
		{
			if(searchPrefs.ALLOWED_TRAVEL_TYPES == 'ROUNDTRIP' && searchPrefs.OFFER_OPEN_JAW_ROUTES == 'false')
			{
				alert(searchText.popup_roundtrip);
				document.skylightsForm.travel[0].checked = 1;
			}
			else
			{	
				document.skylightsForm.numberMarkets.value			= 1;
				showDiv(false, 'dateSel2');
				showDiv(false, 'TrvlOptLbl'); // felix added 
				if (searchPrefs.OFFER_OPEN_JAW_ROUTES == 'true')
				{
					showDiv(false, 'fromToSel2');
				}
			}
		}
		//switched to open jaw
		else    			
		{	if(searchPrefs.ALLOWED_TRAVEL_TYPES == 'ONEWAY' && searchPrefs.OFFER_OPEN_JAW_ROUTES == 'false')
			{
				document.skylightsForm.travel[1].checked = 1;	
				document.skylightsForm.numberMarkets.value		= 1;
				showDiv(false, 'dateSel2');
			}
			else
			{	     				
				document.skylightsForm.numberMarkets.value			= 2;	
				showDiv(true, 'dateSel2');
				showDiv(true, 'fromToSel2');			 
			}
			
		}
		if (eval(document.skylightsForm.farePriority))
		{
			showDateFlex();
		}
		//balanceHeight('wrapper', 'right', 'left');
	}
	
	/*##############################################
	#
	# Function: showDateFlex()
	#
	# Parameter: 
	#
	# Description: 
	#	       Dynamically changes the display in the search display
	#	       depending on the selected fare priority option( Standard Search or LFS)
	#
	#*/
	function showDateFlex()
	{
		var option_val = document.skylightsForm.farePriority[document.skylightsForm.farePriority.selectedIndex].value;
		var elem_id;
		for (i = 1; i <= document.skylightsForm.numberMarkets.value; i++)
		{
			elem_id = "depart" + i + "FlexBySelect";

			if (option_val == 1) // selected Stardard search mode
			{
			      
			      showDiv(true, elem_id,'inline');
			}
			else  // selected LFS search mode
			{
			      showDiv(false, elem_id );
			}
		}
	
	}
// Begin Search Submit Form javascript
	/*##############################################
	#
	# Function: submitSearch()
	#
	# Parameter: 
	#		beginDate: valid start date (set in PREFs)
	#		endDate:  valid end date (set in PREFs) 
	#
	# Description: 
	#	       Verifies all fields are in proper values, then
	#	       submits the form to start the flight availability search.
	#
	#*/
	function submitSearch(beginDate, endDate)
	{
		if ( validCities() && validDates(beginDate, endDate) && validPax())
		{
			
			finalDate();
			
			/*
			# Removing this line as the templates will concatenate 
			# the month year and day values.
			#
			#formatDates();
			*/

			/*IPR00260732 START*/
			visaPopup();
			/*IPR00260732 END*/

			document.skylightsForm.page.value 	= 'SEARCH';
			document.skylightsForm.module.value	= 'SB';			
			document.skylightsForm.event.value 	= 'search';
			document.skylightsForm.submit();

		}
	}
	/*##############################################
	#
	# Function: onLoadAtaGlance()
	#
	# Parameter: 
	#
	# Description: 
	#	       initializes the display and field values for 
	#	       search page.
	#
	#*/
	function onLoadAtaGlance()
	{
		swapTravel();
		for (i = 1; i <= document.skylightsForm.numberMarkets.value; i++)
		{
			resetDest(i);
		}
	}
/*IPR00260732 START*/
	/*##############################################
	#
	# Function: visaPopup()
	#
	# Parameter: 
	#
	# Description: 
	#	       prompts visa reminder for selected origin and destination.
	#
	#*/
	function visaPopup()
	{
		 
		sector1_o 	= document.skylightsForm.from1.value;
		sector1_d	= document.skylightsForm.to1.value;
					
		//[%- # VISA requirement warning message for INDIA or CHINA originating bookings - Matts 06.Jul.2007,10.Sep.2007,07.Dec.2007 -%]
		if ((sector1_o == "MAA" || sector1_o == "aMAA") || (sector1_o == "COK" || sector1_o == "aCOK") ||
		   (sector1_o == "BLR" || sector1_o == "aBLR") ||
		   (sector1_o == "CAN" || sector1_o == "aCAN") || (sector1_o == "HAK" || sector1_o == "aHAK") ||
		   (sector1_o == "SZX" || sector1_o == "aSZX") || (sector1_o == "XMN" || sector1_o == "aXMN"))
		{
			alert (searchText.popup_visa_reqd_warning);
		}
		
		// popup message for MNL and CRK flights
		if ((sector1_d == "MNL" || sector1_d == "aMNL"))
		{
			alert (searchText.popup_ataglance_MNL_flght);
		}
		if ((sector1_d == "CRK" || sector1_d == "aCRK"))
		{
			alert (searchText.popup_ataglance_CRK_flght);
		}
		// END popup message for MNL and CRK flights
		 
		//[%- # VISA requirement warning message for China destination bookings - Kavin 26.May.2008 -%]
		if ((sector1_d == "CAN" || sector1_d == "aCAN") || (sector1_d == "HAK" || sector1_d == "aHAK") ||
		   (sector1_d == "SZX" || sector1_d == "aSZX") || (sector1_d == "XMN" || sector1_d == "aXMN"))
		{
			alert (searchText.popup_chinavisa_reqd_warning);
		}
		
		if ( ((sector1_o == "AVV" || sector1_o == "MEL" || sector1_o == "ZZM") && (sector1_d == "ASP" || sector1_d == "ROK" || sector1_d == "MKY")) || 
			 ((sector1_d == "AVV" || sector1_d == "MEL" || sector1_d == "ZZM") && (sector1_o == "ASP" || sector1_o == "ROK" || sector1_o == "MKY")))
		{
			alert("Please Note: \n\nAlice Springs, Mackay, and Rockhampton routes will operate to/from MELBOURNE - AVALON from 10 November 2010.\n\nPlease click OK to continue.");
		}
		
		
	}
/*IPR00260732 END*/