//**********************************************************************
// NOTE:  AjaxCommon.js is required for some of the functions defined
// in this file to run.
//
// NOTE:  All global variables used by this file are prefixed with 
// "as_" (i.e., airport suggest) in an effort to prevent potential 
// conflicts with global variables declared in other JS files or blocks
// that are used within the same pages.
//**********************************************************************

//GLOBAL VARIABLES
var as_minInputText = 2;
var as_defaultTextColor = "#999999";
var as_customTextColor = null;

//DEPARTURE AIRPORT GLOBAL VARIABLES
var as_deptInputField = new Array();
var as_deptValueField = new Array();
var as_deptSuggestDiv = new Array();
var as_deptSuggestFrame = new Array();
var as_deptCurrentInput = new Array();
var as_deptScrollIndex = new Array();
var as_deptListIcon = new Array();
var as_deptOrigin = "DEPT";
var as_deptDefaultText = "Type your departure";
var as_deptList = null;

//DESTINATION AIRPORT GLOBAL VARIABLES
var as_destInputField = new Array();
var as_destValueField = new Array();
var as_destSuggestDiv = new Array();
var as_destSuggestFrame = new Array();
var as_destCurrentInput = new Array();
var as_destScrollIndex = new Array();
var as_destListIcon = new Array();
var as_destOrigin = "DEST";
var as_destDefaultText = "Type your destination";
var as_destList = null;

//To calculate the calendar position relative to a specific container
//element rather than relative to the entire window, add the class
//specified below to the desired container element.
var as_posRefClass = "asPosRef";



//**********************************************************************
// Name			: initDeptAirportSuggest
// Description	: Initializes global variables which will be used to
//				  manipulate the departure airport form fields and
//				  suggestion list.
// Input		: departureNr - Unique numeric value indicating the
//								departure section within the page with
//								which the specified variables are
//								associated,as some pages will include 
//								multiple departure sections.
//				  inputField - Text box into which the user will enter
//				 			   departure airport input
//				  valueField - Hidden field which will be used to store
//							   the departure airport code if/when one is
//							   selected
//				  suggestDiv - Div section where the departure airport
//							   suggestion list should be placed
//				  suggestFrame - iFrame to be placed in the same location
//								 as the div section; this must be done
//								 to work around an IE bug which would
//								 otherwise allow select lists to appear
//								 on top of the div
// Output		: N/A
//**********************************************************************
function initDeptAirportSuggest(departureNr, inputField, valueField, suggestDiv, suggestFrame) {    
    as_deptInputField[departureNr] = inputField;
    as_deptValueField[departureNr] = valueField;
    as_deptSuggestDiv[departureNr] = suggestDiv;
    as_deptSuggestFrame[departureNr] = suggestFrame;
    as_deptCurrentInput[departureNr] = "";
	as_deptScrollIndex[departureNr] = -1;
	
	if (as_deptInputField[departureNr].style.color) {		
		as_customTextColor = as_deptInputField[departureNr].style.color;
	}
	       
    //Form fields have been prepopulated with previous search data  
    if (as_deptValueField[departureNr].value.length > 0) {    	
    	var displayText = getAirportDisplayText(as_deptValueField[departureNr].value, false);
    	if (displayText == null) {
    		displayText = as_deptValueField[departureNr].value;
    	}
    	
    	as_deptInputField[departureNr].value = displayText;
    	as_deptCurrentInput[departureNr] = displayText;
    }
    else {
    	as_deptInputField[departureNr].value = as_deptDefaultText;
    	as_deptInputField[departureNr].style.color = as_defaultTextColor;
    }
    
    //Preload full departure airport list to improve response time
    if (as_deptList == null) {
    	loadAirportList(as_deptOrigin);
    }
}



//**********************************************************************
// Name			: initDestAirportSuggest
// Description	: Initializes global variables which will be used to
//				  manipulate the destination airport form fields and
//				  suggestion list.
// Input		: destinationNr - Unique numeric value indicating the
//								destination section within the page with
//								which the specified variables are
//								associated,as some pages will include 
//								multiple destination sections.
//				  inputField - Text box into which the user will enter
//				 			   destination airport input
//				  valueField - Hidden field which will be used to store
//							   the destination airport code if/when one
//							   is selected
//				  suggestDiv - Div section where the destination airport
//							   suggestion list should be placed
//				  suggestFrame - iFrame to be placed in the same location
//								 as the div section; this must be done
//								 to work around an IE bug which would
//								 otherwise allow select lists to appear
//								 on top of the div
// Output		: N/A
//**********************************************************************
function initDestAirportSuggest(destinationNr, inputField, valueField, suggestDiv, suggestFrame) {
    as_destInputField[destinationNr] = inputField;
    as_destValueField[destinationNr] = valueField;
    as_destSuggestDiv[destinationNr] = suggestDiv;
    as_destSuggestFrame[destinationNr] = suggestFrame;
    as_destCurrentInput[destinationNr] = "";
	as_destScrollIndex[destinationNr] = -1;
	
	if (as_destInputField[destinationNr].style.color) {
		as_customTextColor = as_destInputField[destinationNr].style.color;
	}
    
    //Form fields have been prepopulated with previous search data  
    if (as_destValueField[destinationNr].value.length > 0) {
    	var displayText = getAirportDisplayText(as_destValueField[destinationNr].value, true);
    	if (displayText == null) {
    		displayText = as_destValueField[destinationNr].value;
    	}
    	
    	as_destInputField[destinationNr].value = displayText;
    	as_destCurrentInput[destinationNr] = displayText;
    }
    else {
    	as_destInputField[destinationNr].value = as_destDefaultText;
    	as_destInputField[destinationNr].style.color = as_defaultTextColor;
    }
    
    //Preload full departure airport list to improve response time
    if (as_destList == null) {
    	loadAirportList(as_destOrigin);
    }
}



//**********************************************************************
// Name			: checkDeptDefaultText
// Description	: If the departure display field contains the default
//				  text value when it gains focus, clears this value.
// Input		: departureNr - Unique numeric value indicating the
//								departure section within the page which
//								has gained focus,as some pages will  
//								include multiple departure sections.
// Output		: N/A
//**********************************************************************
function checkDeptDefaultText(departureNr) {
	checkDefaultText(as_deptInputField[departureNr], as_deptDefaultText);	
}



//**********************************************************************
// Name			: checkDestDefaultText
// Description	: If the destination display field contains the default
//				  text value when it gains focus, clears this value.
// Input		: destinationNr - Unique numeric value indicating the
//								destination section within the page which
//								has gained focus,as some pages will  
//								include multiple destination sections.
// Output		: N/A
//**********************************************************************
function checkDestDefaultText(destinationNr) {
	checkDefaultText(as_destInputField[destinationNr], as_destDefaultText);
}



//**********************************************************************
// Name			: toggleDeptAirportList
// Description	: Displays or hides the full list of available departure
//				  airports, based on whether it is currently visible.
// Input		: departureNr - Unique numeric value indicating the
//								departure section within the page for 
//								which the full list is to be toggled,
//								as some pages will include multiple
//								departure sections.
//				  listIcon - Image element used to display the plus or
//							 minus icon based on the current state
//							 of the list.
// Output		: N/A
//**********************************************************************
function toggleDeptAirportList(departureNr, listIcon) {	
	if (listIcon.className == "tah_minus") {		
		toggleOffAirportList(as_deptInputField[departureNr], as_deptSuggestDiv[departureNr], as_deptSuggestFrame[departureNr], as_deptOrigin, listIcon, departureNr);		
	}
	else {
		if (as_deptList == null) {
			loadAirportList(as_deptOrigin);
		}
		toggleOnAirportList(as_deptInputField[departureNr], as_deptValueField[departureNr], as_deptSuggestDiv[departureNr], as_deptSuggestFrame[departureNr], as_deptOrigin, listIcon, departureNr);		
	}	
}



//**********************************************************************
// Name			: toggleDestAirportList
// Description	: Displays or hides the full list of available destination
//				  airports, based on whether it is currently visible.
// Input		: destinationNr - Unique numeric value indicating the
//								destination section within the page for 
//								which the full list is to be toggled,
//								as some pages will include multiple
//								destination sections.
//				  listIcon - Image element used to display the plus or
//							 minus icon based on the current state
//							 of the list.
// Output		: N/A
//**********************************************************************
function toggleDestAirportList(destinationNr, listIcon) {	
	if (listIcon.className == "tah_minus") {		
		toggleOffAirportList(as_destInputField[destinationNr], as_destSuggestDiv[destinationNr], as_destSuggestFrame[destinationNr], as_destOrigin, listIcon, destinationNr);		
	}
	else {
		if (as_destList == null) {			
			loadAirportList(as_destOrigin);
		}
		toggleOnAirportList(as_destInputField[destinationNr], as_destValueField[destinationNr], as_destSuggestDiv[destinationNr], as_destSuggestFrame[destinationNr], as_destOrigin, listIcon, destinationNr);		
	}
}



//**********************************************************************
// Name			: suggestDeptAirports
// Description	: Controls the display and population of the departure
//				  airport suggestion list based on the text the user
//				  enters into the associated input field.
// Input		: departureNr - Unique numeric value indicating the
//								departure section within the page for 
//								which suggestions are to be provided,
//								as some pages will include multiple
//								departure sections.
// Output		: N/A
//**********************************************************************
function suggestDeptAirports(departureNr) {   
    if (as_deptInputField[departureNr].value != as_deptCurrentInput[departureNr]) {
        as_deptCurrentInput[departureNr] = as_deptInputField[departureNr].value; 
        as_deptValueField[departureNr].value = "";
                
        if (as_deptInputField[departureNr].value.replace(/\s/g, "") != "" &&
				as_deptInputField[departureNr].value.length >= as_minInputText) {            
            suggestAirports(as_deptInputField[departureNr], as_deptValueField[departureNr], as_deptSuggestDiv[departureNr], as_deptSuggestFrame[departureNr], as_deptOrigin, departureNr);
        }
        else {
            clearAirportSuggestions(as_deptSuggestDiv[departureNr], as_deptSuggestFrame[departureNr], as_deptOrigin, departureNr);
        }
    }    
}



//**********************************************************************
// Name			: suggestDestAirports
// Description	: Controls the display and population of the destination
//				  airport suggestion list based on the text the user
//				  enters into the associated input field.
// Input		: destinationNr - Unique numeric value indicating the
//								  destination within the page for which
//								  suggestions are to be provided, as
//								  some pages will include multiple
//								  destination sections.
// Output		: N/A
//**********************************************************************
function suggestDestAirports(destinationNr) {
    if (as_destInputField[destinationNr].value != as_destCurrentInput[destinationNr]) {
        as_destCurrentInput[destinationNr] = as_destInputField[destinationNr].value; 
        as_destValueField[destinationNr].value = "";
        
        if (as_destInputField[destinationNr].value.replace(/\s/g, "") != "" &&
				as_destInputField[destinationNr].value.length >= as_minInputText) {
            suggestAirports(as_destInputField[destinationNr], as_destValueField[destinationNr], as_destSuggestDiv[destinationNr], as_destSuggestFrame[destinationNr], as_destOrigin, destinationNr);
        }
        else {
            clearAirportSuggestions(as_destSuggestDiv[destinationNr], as_destSuggestFrame[destinationNr], as_destOrigin, destinationNr);
        }
    }    
}



//**********************************************************************
// Name			: checkDeptAirportSelection
// Description	: When a user navigates away from the departure airport
//				  input field, determines whether an airport selection
//				  was made and performs any required clean-up tasks 
//				  related to the associated suggestion list.
// Input		: departureNr - Unique numeric value indicating the
//								departure section within the page whose
//								fields are to be checked, as some pages
//								will include multiple departure sections.
// Output		: N/A
//**********************************************************************
function checkDeptAirportSelection(departureNr) {    
    checkAirportSelection(as_deptInputField[departureNr], as_deptValueField[departureNr], as_deptSuggestDiv[departureNr], as_deptSuggestFrame[departureNr], as_deptOrigin, departureNr);
}



//**********************************************************************
// Name			: checkDestAirportSelection
// Description	: When a user navigates away from the destination airport
//				  input field, determines whether an airport selection
//				  was made and performs any required clean-up tasks 
//				  related to the associated suggestion list.
// Input		: destinationNr - Unique numeric value indicating the
//								  destination within the page whose
//								  fields are to be checked, as some
//								  pages will include multiple destination
//								  sections.
// Output		: N/A
//**********************************************************************
function checkDestAirportSelection(destinationNr) {   
    checkAirportSelection(as_destInputField[destinationNr], as_destValueField[destinationNr], as_destSuggestDiv[destinationNr], as_destSuggestFrame[destinationNr], as_destOrigin, destinationNr);
}



//**********************************************************************
// Name			: checkDeptScroll
// Description	: Checks to see if the user has pressed a key which
//				  should result in a scrolling or a selection action on
//				  the departure airport suggestion list, and takes 
//				  the appropriate action.
// Input		: event - Browser event (i.e., a key press) that was
//						  captured
//				  departureNr - Unique numeric value indicating the
//								departure section within the page on
//								which action is to be taken, as some pages
//								will include multiple departure sections.
// Output		: N/A
//**********************************************************************
function checkDeptAirportScroll(event, departureNr) {    
    checkAirportScroll(event, as_deptInputField[departureNr], as_deptValueField[departureNr], as_deptSuggestDiv[departureNr], as_deptSuggestFrame[departureNr], as_deptOrigin, departureNr);
}



//**********************************************************************
// Name			: checkDestScroll
// Description	: Checks to see if the user has pressed a key which
//				  should result in a scrolling or a selection action on
//				  the destination airport suggestion list, and takes 
//				  the appropriate action.
// Input		: event - Browser event (i.e., a key press) that was
//						  captured
//				  destinationNr - Unique numeric value indicating the
//								  destination within the page on which
//								  action is to be taken, as some pages
//								  will include multiple destination 
//								  sections.
// Output		: N/A
//**********************************************************************
function checkDestAirportScroll(event, destinationNr) {
    checkAirportScroll(event, as_destInputField[destinationNr], as_destValueField[destinationNr], as_destSuggestDiv[destinationNr], as_destSuggestFrame[destinationNr], as_destOrigin, destinationNr);
}



//**********************************************************************
// Name			: checkDefaultText
// Description	: If the specified display field contains the specified
//				  default text value when it gains focus, clears this
//				  value.
// Input		: inputField - Field containing the user's input
//				  defaultText - Default text value for this field
// Output		: N/A
//**********************************************************************
function checkDefaultText(inputField, defaultText) {
	if (inputField.value == defaultText) {
		inputField.value = "";
		if (as_customTextColor != null) {
			inputField.style.color = as_customTextColor;
		}
		else {
			inputField.style.color = "";
		}		
	}
}



//**********************************************************************
// Name			: getAirportDisplayText
// Description	: If the browser supports Ajax, sends a request to the
//				  server to obtain the display text associated with a
//				  particular airport code.
// Input		: airportCode - The airport code for which display text
//							    is to be retrieved
//				  detailed - true if detailed display text should be 
//							 returned or false if the basic display text
//							 should be returned
// Output		: The display text associated with the specified airport
//				  code, or null if it cannot be determined for any reason.
//**********************************************************************
function getAirportDisplayText(airportCode, detailed) {    
    var displayText = null;    
    var targetURL = "/aev/airport_suggest";  
    var reqParamsString = "target=displayText&airportCode=" + airportCode + "&detailed=" + detailed;        
    var xmlHttpRequest = createXMLHttpRequest();
    
    if (xmlHttpRequest != null) {    	
        xmlHttpRequest.open("POST", targetURL, false);
        xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttpRequest.send(reqParamsString);	
                      	
    	//This is a synchronous call, so onreadystatechange handler is needed
    	if (xmlHttpRequest.status == 200) {				
			var airport = xmlHttpRequest.responseXML.getElementsByTagName("airport")[0];
		    displayText = airport.getElementsByTagName("text")[0].childNodes[0].nodeValue;
        } 
    } 
    
    return displayText;
}



//**********************************************************************
// Name			: toggleOnAirportList
// Description	: Renders full list of available airports.
// Input		: inputField - Field containing the user's input
//				  valueField - Hidden field which is used to store
//							   the airport code if/when one is selected
//				  suggestDiv - Div section where the airport list is to
//							   be placed
//				  suggestFrame - iFrame associated with the suggestion
//								 div section; this must be done
//								 to work around an IE bug which would
//								 otherwise allow select lists to appear
//								 on top of the div
//				  origin - String indicating whether the departure or
//						   destination list is to be rendered
//				  airportSectionNr - Unique numeric value indicating the
//								     airport section within the page for
//								  	 which action is to be taken, as
//								     some pages will include multiple
//									 departure/destination sections
//				  listIcon - Image element used to display the plus or
//							 minus icon based on the current state
//							 of the list.
// Output		: N/A
//**********************************************************************
function toggleOnAirportList(inputField, valueField, suggestDiv, suggestFrame, origin, listIcon, airportSectionNr) {	
	if (origin == as_deptOrigin) {        
    	as_deptListIcon[airportSectionNr] = listIcon;
    	genAirportSuggestionList(as_deptList, inputField, valueField, suggestDiv, suggestFrame, origin, airportSectionNr, false);    
        checkDeptDefaultText(airportSectionNr);        
    }
    else if (origin == as_destOrigin) {
    	as_destListIcon[airportSectionNr] = listIcon;    	
        genRegionalAirportList(as_destList, inputField, valueField, suggestDiv, suggestFrame, origin, airportSectionNr);
        checkDestDefaultText(airportSectionNr);
    }
	
	listIcon.setAttribute("src", "/images/booking/aev_tah_minus.gif");
    listIcon.setAttribute("alt", "Close list");
    listIcon.className = "tah_minus";
}



//**********************************************************************
// Name			: toggleOffAirportList
// Description	: Hides full list of available airports.
// Input		: inputField - Field containing the user's input
//				  suggestDiv - Div section where the airport list resides
//				  suggestFrame - iFrame associated with the suggestion
//								 div section; this must be done
//								 to work around an IE bug which would
//								 otherwise allow select lists to appear
//								 on top of the div
//				  origin - String indicating whether the departure or
//						   destination list is to be hidden
//				  airportSectionNr - Unique numeric value indicating the
//								     airport section within the page for
//								  	 which action is to be taken, as
//								     some pages will include multiple
//									 departure/destination sections
//				  listIcon - Image element used to display the plus or
//							 minus icon based on the current state
//							 of the list.
// Output		: N/A
//**********************************************************************
function toggleOffAirportList(inputField, suggestDiv, suggestFrame, origin, listIcon, airportSectionNr) {
    clearAirportSuggestions(suggestDiv, suggestFrame, origin, airportSectionNr);
			    
    if (inputField.value == "") {
    	inputField.style.color = as_defaultTextColor;
                
        if (origin == as_deptOrigin) {	            
            inputField.value = as_deptDefaultText;
        }
        else if (origin == as_destOrigin) {	            
            inputField.value = as_destDefaultText;
        }	
    }
}



//**********************************************************************
// Name			: loadAirportList
// Description	: If the browser supports Ajax, sends a request to the
//				  server to obtain the full list of airports for the
//				  specified origin.
// Input		: origin - String indicating whether the departure or
//						   destination list is to be retrieved
// Output		: N/A
//**********************************************************************
function loadAirportList(origin) {
    var targetURL = "/aev/airport_suggest";  
    var reqParamsString = "target=list&origin=" + origin;   
    var xmlHttpRequest = createXMLHttpRequest();
    
    if (xmlHttpRequest != null) {
    	xmlHttpRequest.onreadystatechange = function() {
            if (xmlHttpRequest.readyState == 4) {
                if (xmlHttpRequest.status == 200) {                    
                    if (origin == as_deptOrigin) {        
				        as_deptList = xmlHttpRequest.responseXML;
				    }
				    else if (origin == as_destOrigin) {
				        as_destList = xmlHttpRequest.responseXML;
				    }
                }                              
            }
        };
	    
        xmlHttpRequest.open("POST", targetURL, true);
        xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttpRequest.send(reqParamsString);	    
    }
}



//**********************************************************************
// Name			: suggestAirports
// Description	: If the browser supports Ajax, sends a request to the
//				  server to obtain a list of suggested airports based on
//				  the text that the user has entered and renders the list. 
// Input		: inputField - Field containing the user's input
//				  valueField - Hidden field which will be used to store
//							   the airport code if/when one is selected
//				  suggestDiv - Div section where the airport suggestion
//							   list should be placed
//				  suggestFrame - iFrame to be placed in the same location
//								 as the div section; this must be done
//								 to work around an IE bug which would
//								 otherwise allow select lists to appear
//								 on top of the div
//				  origin - String indicating whether suggestions are to
//						   be retrieved for departure or destination 
//						   airports
//				  airportSectionNr - Unique numeric value indicating the
//								     airport section within the page for
//								  	 which action is to be taken, as
//								     some pages will include multiple
//									 departure/destination sections
// Output		: N/A
//**********************************************************************
function suggestAirports(inputField, valueField, suggestDiv, suggestFrame, origin, airportSectionNr) {    
    var targetURL = "/aev/airport_suggest";  
    var reqParamsString = "target=suggest&inputText=" + escape(inputField.value) + "&origin=" + origin;   
    var xmlHttpRequest = createXMLHttpRequest();
    
    if (xmlHttpRequest != null) {
    	xmlHttpRequest.onreadystatechange = function() {
            if (xmlHttpRequest.readyState == 4) {
                if (xmlHttpRequest.status == 200) {
                    genAirportSuggestionList(xmlHttpRequest.responseXML, inputField, valueField, suggestDiv, suggestFrame, origin, airportSectionNr, true);
                }
                else {                    
                    clearAirportSuggestions(suggestDiv, suggestFrame, origin, airportSectionNr);    
                }                
            }
        };
	    
        xmlHttpRequest.open("POST", targetURL, true);
        xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttpRequest.send(reqParamsString);	    
    } 
}



//**********************************************************************
// Name			: genAirportSuggestionList
// Description	: Parses the XML response from the server which contains
//				  the suggested airport data and renders the list.
// Input		: responseXML - XML response from the server which 
//							    contains the suggested airport data
//				  inputField - Field containing the user's input
//				  valueField - Hidden field which will be used to store
//							   the airport code if/when one is selected
//				  suggestDiv - Div section where the airport suggestion
//							   list should be placed
//				  origin - String indicating whether suggestions are
//						   for departure or destination airports
//				  airportSectionNr - Unique numeric value indicating the
//								     airport section within the page for
//								  	 which action is to be taken, as
//								     some pages will include multiple
//									 departure/destination sections
//				  autoSuggest - true if the list is being generated
//								via auto suggest; false if it is 
//								being generated via the full list
//								option
// Output		: N/A
//**********************************************************************
function genAirportSuggestionList(responseXML, inputField, valueField, suggestDiv, suggestFrame, origin, airportSectionNr, autoSuggest) {    
    clearAirportSuggestions(suggestDiv, suggestFrame, origin, airportSectionNr);    
                
    var airports = responseXML.getElementsByTagName("airport");
    
    for (var i=0; i<airports.length; i++) {        
        var airportCode = airports[i].getElementsByTagName("code")[0].childNodes[0].nodeValue;
        var displayText = airports[i].getElementsByTagName("text")[0].childNodes[0].nodeValue;
                
        div = document.createElement("div");
        div.style.width = "100%";        
        div.style.cursor = "default";
        div.style.paddingLeft = "4px";
        div.onmouseover = function() { focusOnAirportSuggestion(this, suggestDiv, origin, airportSectionNr); };
        div.onmouseout = function() { focusOffAirportSuggestion(this, origin, airportSectionNr); };
        div.onclick = function() { selectAirport(inputField, valueField, suggestDiv, suggestFrame, origin, airportSectionNr, autoSuggest); };
                
        suggValue = document.createElement("input");
        suggValue.setAttribute("type", "hidden");
        suggValue.setAttribute("name", "suggValue");
        suggValue.setAttribute("id", "suggValue");
        suggValue.setAttribute("value", airportCode);
        suggText = document.createTextNode(displayText);
                        
        div.appendChild(suggValue);
        div.appendChild(suggText);       
        suggestDiv.appendChild(div);       
    }
        
    setAirportListPosition(inputField, suggestDiv, suggestFrame, autoSuggest);    
}



//**********************************************************************
// Name			: genRegionalAirportList
// Description	: Parses the XML response from the server, which contains
//				  a regionalized airport list, and renders the list.
// Input		: responseXML - XML response from the server which 
//							    contains the airport list data
//				  inputField - Field containing the user's input
//				  valueField - Hidden field which will be used to store
//							   the airport code if/when one is selected
//				  suggestDiv - Div section where the airport suggestion
//							   list should be placed
//				  origin - String indicating whether suggestions are
//						   for departure or destination airports
//				  airportSectionNr - Unique numeric value indicating the
//								     airport section within the page for
//								  	 which action is to be taken, as
//								     some pages will include multiple
//									 departure/destination sections
// Output		: N/A
//**********************************************************************
function genRegionalAirportList(responseXML, inputField, valueField, suggestDiv, suggestFrame, origin, airportSectionNr) {    
    clearAirportSuggestions(suggestDiv, suggestFrame, origin, airportSectionNr);    
                   
    var regions = responseXML.getElementsByTagName("region");
   
    for (var r=0; r<regions.length; r++) {	    
	    var regionName = regions[r].attributes[0].value;
	    	    
	    div = document.createElement("div");
	    div.style.width = "100%";        
	    div.style.cursor = "default";
	    div.style.paddingLeft = "4px";
	    div.style.fontWeight = "bold";
	    div.style.fontStyle = "italic";	    
        div.appendChild(document.createTextNode(regionName));       
        suggestDiv.appendChild(div);    
	    
	    var airports = regions[r].getElementsByTagName("airport");
	    
	    for (var a=0; a<airports.length; a++) {        
	        var airportCode = airports[a].getElementsByTagName("code")[0].childNodes[0].nodeValue;
	        var displayText = airports[a].getElementsByTagName("text")[0].childNodes[0].nodeValue;
	                
	        div = document.createElement("div");
	        div.style.width = "100%";        
	        div.style.cursor = "default";
	        div.style.paddingLeft = "4px";
	        div.onmouseover = function() { focusOnAirportSuggestion(this, suggestDiv, origin, airportSectionNr); };
	        div.onmouseout = function() { focusOffAirportSuggestion(this, origin, airportSectionNr); };
	        div.onclick = function() { selectAirport(inputField, valueField, suggestDiv, suggestFrame, origin, airportSectionNr, false); };
	                
	        suggValue = document.createElement("input");
	        suggValue.setAttribute("type", "hidden");
	        suggValue.setAttribute("name", "suggValue");
	        suggValue.setAttribute("id", "suggValue");
	        suggValue.setAttribute("value", airportCode);
	        suggText = document.createTextNode(displayText);
	                        
	        div.appendChild(suggValue);
	        div.appendChild(suggText);       
	        suggestDiv.appendChild(div);       
	    }
	}
        
    setAirportListPosition(inputField, suggestDiv, suggestFrame, false);    
}



//**********************************************************************
// Name			: focusOnAirportSuggestion
// Description	: Highlights a particular airport suggestion within the
//				  list.
// Input		: div - Div section containing the suggestion which is
//						to receive focus
//				  suggestDiv - Parent div section containing the entire
//							   airport suggestion list
//				  origin - String indicating whether the suggestion is
//						   for a departure or destination airport
//				  airportSectionNr - Unique numeric value indicating the
//								     airport section within the page for
//								  	 which action is to be taken, as
//								     some pages will include multiple
//									 departure/destination sections
// Output		: N/A
//**********************************************************************
function focusOnAirportSuggestion(div, suggestDiv, origin, airportSectionNr) {    
    var scrollIndex;
    
    //Clear all existing highlights in case the user has switched b/t
    //mouseovers and using the up/down arrows
    for (var i=0; i<suggestDiv.childNodes.length; i++) {
        suggestDiv.childNodes[i].style.backgroundColor="#FFFFFF"; 
        
        if (suggestDiv.childNodes[i] == div) {
            scrollIndex = i;
        }
    }
    
    suggestDiv.childNodes[scrollIndex].style.backgroundColor="#FCE87D";   
    
    if (origin == as_deptOrigin) {        
        as_deptScrollIndex[airportSectionNr] = scrollIndex;        
    }
    else if (origin == as_destOrigin) {
        as_destScrollIndex[airportSectionNr] = scrollIndex;        
    }
}



//**********************************************************************
// Name			: focusOffAirportSuggestion
// Description	: Removes previously applied highlighting from a 
//				  particular airport suggestion within the list.
// Input		: div - Div section containing the suggestion from which
//						focus is to be removed
//				  origin - String indicating whether the suggestion is
//						   for a departure or destination airport
//				  airportSectionNr - Unique numeric value indicating the
//								     airport section within the page for
//								  	 which action is to be taken, as
//								     some pages will include multiple
//									 departure/destination sections
// Output		: N/A
//**********************************************************************
function focusOffAirportSuggestion(div, origin, airportSectionNr) {
    div.style.backgroundColor="#FFFFFF";
    
    if (origin == as_deptOrigin) {        
        as_deptScrollIndex[airportSectionNr] = -1;
    }
    else if (origin == as_destOrigin) {
        as_destScrollIndex[airportSectionNr] = -1;       
    }
}



//**********************************************************************
// Name			: selectAirport
// Description	: Populates the appropriate hidden value field with the
//				  airport code corresponding to the suggestion selected
//				  by the user and and performs any required clean-up 
//				  tasks related to the associated suggestion list.
// Input		: inputField - Field containing the user's input
//				  valueField - Hidden field to be used to store the
//							   airport code that has been selected
//				  suggestDiv - Div section containing the airport
//							   suggestion list
//				  suggestFrame - Associated iFrame
//				  origin - String indicating whether suggestions are
//						   for departure or destination airports
//				  airportSectionNr - Unique numeric value indicating the
//								     airport section within the page for
//								  	 which action is to be taken, as
//								     some pages will include multiple
//									 departure/destination sections
//				  autoSuggest - true if the list is being generated
//								via auto suggest; false if it is 
//								being generated via the full list
//								option
// Output		: N/A
//**********************************************************************
function selectAirport(inputField, valueField, suggestDiv, suggestFrame, origin, airportSectionNr, autoSuggest) {    
    var inputText = inputField.value;
	var inputValue = valueField.value;
    var div;
        
    if (origin == as_deptOrigin) {
        div = suggestDiv.childNodes[as_deptScrollIndex[airportSectionNr]];
    }
    else if (origin == as_destOrigin) {
        div = suggestDiv.childNodes[as_destScrollIndex[airportSectionNr]];
    }
    
    var airportCode = div.childNodes[0].value;
    var displayText = div.childNodes[1].nodeValue;
               
    if (autoSuggest && inputText.replace(/\s/g, "") != "") {
    	trackAirportMatch(origin, inputText, airportCode);
    }
               
    inputField.value = displayText;
    valueField.value = airportCode;
    clearAirportSuggestions(suggestDiv, suggestFrame, origin, airportSectionNr);    
    
    if (origin == as_deptOrigin) {        
        as_deptCurrentInput[airportSectionNr] = displayText;               
    }
    else if (origin == as_destOrigin) {  
		if(airportCode!=inputValue && document.ssSearchForm !=undefined && document.ssSearchForm.hotelChoice0!=undefined){
			toggleHotelList('N',(airportSectionNr-1),inputValue);
		}
        as_destCurrentInput[airportSectionNr] = displayText;        
    }    
}



//**********************************************************************
// Name			: clearAirportSuggestions
// Description	: Clears and hides the airport suggestion list.
// Input		: suggestDiv - Div section containing the airport
//							   suggestion list to be cleared
//				  suggestFrame - Associated iFrame to be cleared
//				  origin - String indicating whether suggestions are
//						   for departure or destination airports
//				  airportSectionNr - Unique numeric value indicating the
//								     airport section within the page for
//								  	 which action is to be taken, as
//								     some pages will include multiple
//									 departure/destination sections
// Output		: N/A
//**********************************************************************
function clearAirportSuggestions(suggestDiv, suggestFrame, origin, airportSectionNr) {
    var listIcon = null;
    var listLength = suggestDiv.childNodes.length;
    
    for (var i=listLength-1; i >=0; i--) {
        suggestDiv.removeChild(suggestDiv.childNodes[i]);
    }
    
    suggestDiv.style.border = "none";
    suggestDiv.style.display = "none";
    suggestFrame.style.display = "none";
    
    if (origin == as_deptOrigin) {
        as_deptScrollIndex[airportSectionNr] = -1;
        listIcon = as_deptListIcon[airportSectionNr];
    }
    else if (origin == as_destOrigin) {
        as_destScrollIndex[airportSectionNr] = -1;
        listIcon = as_destListIcon[airportSectionNr];
    }
    
    if (listIcon != undefined && listIcon != null) {
    	listIcon.setAttribute("src", "/images/booking/aev_tah_plus.gif");
		listIcon.setAttribute("alt", "View complete list");	
		listIcon.className = "tah_plus";	
    }
}



//**********************************************************************
// Name			: checkAirportSelection
// Description	: When a user navigates away from the airport input 
//				  field, determines whether an airport selection
//				  was made and performs any required clean-up tasks 
//				  related to the associated suggestion list.
// Input		: inputField - Field containing the user's input
//				  valueField - Hidden field containing the airport code
//							   if one has been selected
//				  suggestDiv - Div section containing the airport
//							   suggestion list
//				  suggestFrame - Associated iFrame
//				  origin - String indicating whether suggestions are
//						   for departure or destination airports
//				  airportSectionNr - Unique numeric value indicating the
//								     airport section within the page for
//								  	 which action is to be taken, as
//								     some pages will include multiple
//									 departure/destination sections
// Output		: N/A
//**********************************************************************
function checkAirportSelection(inputField, valueField, suggestDiv, suggestFrame, origin, airportSectionNr) {
    var scrollIndex;
    var displayWarning = false;
    
    if (origin == as_deptOrigin) {
        scrollIndex = as_deptScrollIndex[airportSectionNr];    
    }
    else if (origin == as_destOrigin) {
        scrollIndex = as_destScrollIndex[airportSectionNr];    
    }
    
    //User did NOT make a selection
    if (scrollIndex < 0 && valueField.value.length == 0) {        
        var inputText = inputField.value.replace(/\s/g, "");
                
        if (inputText != "" && inputText.length >= as_minInputText) {        	
	        if (suggestDiv.childNodes.length > 0) {	            
		        //If user entered enough text to trigger suggestions and suggestions
		        //were available but none were selected, track this
	        	trackAirportMatch(origin, inputField.value, valueField.value);
		        		        
		        //If the user entered a 3-digit airport code but then navigated
	        	//away from the field without selecting anything, auto-select
	        	//that airport.
		        if (inputText.length == 3) {
		        	var upperInput = inputText.toUpperCase();
		        	var optionDiv = null;
		        	
		        	for (var i=0; i<suggestDiv.childNodes.length; i++) {
		        		optionDiv = suggestDiv.childNodes[i];
		        		if (optionDiv.childNodes[0].value == upperInput) {
		        			//Select the airport and clean up
		        			var airportCode = optionDiv.childNodes[0].value;
						    var displayText = optionDiv.childNodes[1].nodeValue;
						             
						    inputField.value = displayText;
						    valueField.value = airportCode;
						    clearAirportSuggestions(suggestDiv, suggestFrame, origin, airportSectionNr);    
						    
						    if (origin == as_deptOrigin) {        
						        as_deptCurrentInput[airportSectionNr] = displayText;               
						    }
						    else if (origin == as_destOrigin) {        
						        as_destCurrentInput[airportSectionNr] = displayText;        
						    } 
		        			
		        			return;		        			
		        		}
		        	}
		        } //end airport auto-selection check		        
	        } 	        
	        //If user had entered enough text to trigger suggestions and no
	        //suggestions were available, display a warning
	        else if (suggestDiv.childNodes.length == 0) {
	        	displayWarning = true;	
	        }
        }
                        
        //Clear all fields        
        clearAirportSuggestions(suggestDiv, suggestFrame, origin, airportSectionNr);
        inputField.style.color = as_defaultTextColor;
                
        if (origin == as_deptOrigin) {
            as_deptCurrentInput[airportSectionNr] = "";
            inputField.value = as_deptDefaultText;
        }
        else if (origin == as_destOrigin) {
            as_destCurrentInput[airportSectionNr] = "";
            inputField.value = as_destDefaultText;
        }
    }    
    
    if (displayWarning) {
    	var originText = "";
    	
    	if (origin == as_deptOrigin) {
    		originText = "departure";	
    	}
    	else if (origin == as_destOrigin) {
    		originText = "destination";
    	}
    	
    	var warning = "Sorry, no suggestions are available for the " + 
    		originText + " value you entered. Please check your spelling " +    		
    		"or enter a different " + originText + " point.";
    	window.alert(warning);
    	
    	//focus() does not work in Firefox; code below works in both FF and IE
    	setTimeout("document.getElementById('" + inputField.id + "').focus();", 1);
    }
}



//**********************************************************************
// Name			: trackAirportMatch
// Description	: If the browser supports Ajax, sends data to the server
//				  that will be used to track a user input string for
//				  which suggested airports were found.
// Input		: origin - String indicating whether the input was for
//						   a departure or destination airport
//				  inputText - The user input string
//				  airportCode - if the user selected a suggestion from
//								from the list, the airport code
//								corresponding to that suggestion		  
// Output		: N/A
//**********************************************************************
function trackAirportMatch(origin, inputText, airportCode) {    
    var targetURL = "/aev/airport_suggest";  
    var reqParamsString = "target=match&inputText=" + escape(inputText) + 
        "&origin=" + origin + "&airportCode=" + airportCode;        
    var xmlHttpRequest = createXMLHttpRequest();
    
    if (xmlHttpRequest != null) {    	
        xmlHttpRequest.open("POST", targetURL, true);
        xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttpRequest.send(reqParamsString);
    }
}



//**********************************************************************
// Name			: setAirportListPosition
// Description	: Calculates the location on the page where the airport
//				  suggestion list should be rendered based on the
//				  placement of the associated input field.
// Input		: inputField - Associated field into which the user
//				 			   enters airport input
//				  suggestDiv - Div section where the airport suggestion
//							   list should be placed
//				  suggestFrame - Associated iFrame for the suggestion list
//				  autoSuggest - true if the list is being generated
//								via auto suggest; false if it is 
//								being generated via the full list
//								option
// Output		: N/A
//**********************************************************************
function setAirportListPosition(inputField, suggestDiv, suggestFrame, autoSuggest) {
    var width = inputField.offsetWidth;    
    var left = calculateOffset(inputField, "offsetLeft");
    var top = calculateOffset(inputField, "offsetTop") + inputField.offsetHeight;
                
    if (autoSuggest) {
    	if (width >= 225) {
	    	suggestDiv.style.width = width + "px";    	
	    }
	    else {
	    	suggestDiv.style.width = "225px";
	    }
	    suggestDiv.style.height = "auto";
		suggestDiv.style.overflow = "hidden";
    }
    else {
    	if (width >= 241) {
	    	suggestDiv.style.width = width + "px";    	
	    }
	    else {	    
			suggestDiv.style.width = "241px";
		}
		suggestDiv.style.height = "225px";
    	suggestDiv.style.overflow = "auto";
    }

    suggestDiv.style.top = top + "px";
    suggestDiv.style.left = left + "px";
    suggestDiv.style.border = "black 1px solid";    
    suggestDiv.style.display = "block";
        
    suggestFrame.style.left = suggestDiv.style.left;
	suggestFrame.style.top = suggestDiv.style.top;	
	suggestFrame.style.width = suggestDiv.offsetWidth;
	suggestFrame.style.height = suggestDiv.offsetHeight;	
	suggestFrame.style.display = "block";	
}



//**********************************************************************
// Name			: calculateOffset
// Description	: Calculates the specified offset for the specified field.
// Input		: field - Field for which an offset is to be calculated
//				  offsetType - Type of offset to be calculated
// Output		: The calculated offset value
//**********************************************************************
function calculateOffset(field, offsetType) {
    var offset = 0;
    var curField = field;
    
    while (curField != undefined && curField.className.indexOf(as_posRefClass) < 0) {
        offset += curField[offsetType];        
        curField = curField.offsetParent;        
    }
        
    return offset;
}



//**********************************************************************
// Name			: checkAirportScroll
// Description	: Checks to see if the user has pressed a key which
//				  should result in a scrolling or a selection action on
//				  the airport suggestion list, and takes the appropriate 
//				  action.
// Input		: event - Browser event (i.e., a key press) that was
//						  captured
//				  inputField - Field containing the user's input
//				  valueField - Hidden field which will be used to store
//							   the airport code if/when one is selected
//				  suggestDiv - Div section containing the airport
//							   suggestion list
//				  origin - String indicating whether suggestions are
//						   for departure or destination airports
//				  airportSectionNr - Unique numeric value indicating the
//								     airport section within the page for
//								  	 which action is to be taken, as
//								     some pages will include multiple
//									 departure/destination sections
// Output		: N/A
//**********************************************************************
function checkAirportScroll(event, inputField, valueField, suggestDiv, suggestFrame, origin, airportSectionNr) {        
    var keyCode;
    
    if (! event) {
        event = window.event;
        keyCode = event.keyCode;
    }
    else {
        if (event.which) {
            keyCode = event.which;            
        }
        else {
            keyCode = event.keyCode;           
        }
    }
        
    switch(keyCode) {
        //Up Arrow
        case 38:
            scrollPreviousAirport(suggestDiv, origin, airportSectionNr);
            break;
            
        //Down Arrow
        case 40:
            scrollNextAirport(suggestDiv, origin, airportSectionNr);
            break;
            
        //Tab or Enter
        case 9:
        case 13:
            if (origin == as_deptOrigin) {
                if (as_deptScrollIndex[airportSectionNr] > -1) {                    
                    selectAirport(inputField, valueField, suggestDiv, suggestFrame, origin, airportSectionNr, true);
                }
            }
            else if (origin == as_destOrigin) {
                if (as_destScrollIndex[airportSectionNr] > -1) {                   
                    selectAirport(inputField, valueField, suggestDiv, suggestFrame, origin, airportSectionNr, true);
                }
            }     
            break;
    }
}



//**********************************************************************
// Name			: scrollNextAirport
// Description	: Highlights the next suggestion in the airport 
//				  suggestion list and removes all other existing 
//				  highlighting.
// Input		: suggestDiv - Div section containing the airport
//							   suggestion list
//				  origin - String indicating whether suggestions are
//						   for departure or destination airports
//				  airportSectionNr - Unique numeric value indicating the
//								     airport section within the page for
//								  	 which action is to be taken, as
//								     some pages will include multiple
//									 departure/destination sections
// Output		: N/A
//**********************************************************************
function scrollNextAirport(suggestDiv, origin, airportSectionNr) {    
    var scrollIndex;
    
    if (origin == as_deptOrigin) {
        scrollIndex = as_deptScrollIndex[airportSectionNr];
    }
    else if (origin == as_destOrigin) {
        scrollIndex = as_destScrollIndex[airportSectionNr];
    }
    
    var listLength = suggestDiv.childNodes.length;
    
    if (listLength > 0 && scrollIndex < (listLength - 1)) {
        if (scrollIndex > -1) {            
            focusOffAirportSuggestion(suggestDiv.childNodes[scrollIndex], origin, airportSectionNr);
        }       
        
        focusOnAirportSuggestion(suggestDiv.childNodes[scrollIndex + 1], suggestDiv, origin, airportSectionNr);                  
    }
}



//**********************************************************************
// Name			: scrollPreviousAirport
// Description	: Highlights the previous suggestion in the airport 
//				  suggestion list and removes all other existing 
//				  highlighting.
// Input		: suggestDiv - Div section containing the airport
//							   suggestion list
//				  origin - String indicating whether suggestions are
//						   for departure or destination airports
//				  airportSectionNr - Unique numeric value indicating the
//								     airport section within the page for
//								  	 which action is to be taken, as
//								     some pages will include multiple
//									 departure/destination sections
// Output		: N/A
//**********************************************************************
function scrollPreviousAirport(suggestDiv, origin, airportSectionNr) {    
    var scrollIndex;
    
    if (origin == as_deptOrigin) {
        scrollIndex = as_deptScrollIndex[airportSectionNr];
    }
    else if (origin == as_destOrigin) {
        scrollIndex = as_destScrollIndex[airportSectionNr];
    }
    
    var listLength = suggestDiv.childNodes.length;
        
    if (listLength > 0 && scrollIndex >= 0) {
        focusOffAirportSuggestion(suggestDiv.childNodes[scrollIndex], origin, airportSectionNr);
                
        if (scrollIndex > 0) {            
            focusOnAirportSuggestion(suggestDiv.childNodes[scrollIndex - 1], suggestDiv, origin, airportSectionNr);
        }              
    }
}