/*
	Revision Date: 02/20/2006
	By: Keith Jaskiewicz
		Modify output string to include Airport City; Key event handling for Enter and Right-Arrow added; Modify element.select().		
*/
/*
	Revision Date: 04/08/2006
	By: Peter Femiani
		Smart logic event handling now ignores KEY_HOME, KEY_SHIFT, and KEY_TAB.
		The smart logic box should now behave more like a normal textbox.
		
	Revision Date: 04/21/2006
	By: Keith Jaskiewicz
		Remove search on airport name (Bug 3967).
		
	Revision Date: 04/23/2006
	By: Keith Jaskiewicz
		Add special handling for last item in drop down list
		
	Revision Date: 04/26/2006
	By: Nick Murphy
		More special handling for last item in drop down list
		
	Revision Date: 04/26/2006
	By: Nick Murphy
		Added "px" to the left and top positioning so it works in Firefox.
		
	Revision Date: 04/29/2006
	By: Keith Jaskiewicz
		Enhancement: Move exact airport code matches to the top of the list (Bug 3627)

	Revision Date: 06/12/2006
	By: Peter Femiani
		Added validation function for HotelSmartLogicTextBox
		
	Revision Date: 11/13/2006
	By: Nick Murphy
		Added clear text function
*/
<!--
// global variables
var SmartLogic_OptionsDIV;
var SmartLogic_Processing = false;
var KEY_SPECIAL = 0;
var KEY_TAB = 9;
var KEY_ENTER = 13;
var KEY_SHIFT = 16;
var KEY_HOME = 36;
var KEY_LEFT_ARROW = 37;
var KEY_UP_ARROW = 38;
var KEY_RIGHT_ARROW = 39;
var KEY_DOWN_ARROW = 40;

// finds and displays the matching airport options
function SmartLogic_FindMatchingAirports(elem, e, AirportList_Airports, displayCode)
{	
	// this makes sure that the onblur and onfocus dont step on each other
	while(SmartLogic_Processing)
	{
		// do nothing, just wait
	}

	// handle incomplete input
	var input = elem.value;
	var output = input;
	if(input.length < 2)
	{
		SmartLogic_HideOptions(elem);
		return;
	}

	// ok
	var matches = new Array();
	matches.selectedItem = 0;
	var setText = false;

	// first, handle any special key presses (shift, tab, alt, etc)
	e = (e) ? e : event;
	var key = e.keyCode;
	
	// alert(key);
	
	if(key == KEY_HOME || key == KEY_SHIFT || key == KEY_TAB)
	{
		return;
	}
	
	if(key == KEY_SPECIAL)
	{
		SmartLogic_Deselect(elem);
		return;
	}

	// handle arrow keys	
	if(key == KEY_UP_ARROW || key == KEY_DOWN_ARROW)
	{
		setText = true;
		if(key == KEY_UP_ARROW)
		{
			if(SmartLogic_OptionsDIV != null && SmartLogic_OptionsDIV.matches != null && SmartLogic_OptionsDIV.matches.selectedItem > 0)
			{
				SmartLogic_OptionsDIV.matches.selectedItem--;
			}
		}
		
		if(key == KEY_DOWN_ARROW)
		{
			if(SmartLogic_OptionsDIV != null && SmartLogic_OptionsDIV.matches != null && SmartLogic_OptionsDIV.matches.selectedItem < SmartLogic_OptionsDIV.matches.length - 1)
			{
				SmartLogic_OptionsDIV.matches.selectedItem++;
			}
		}

		if(SmartLogic_OptionsDIV)
			matches = SmartLogic_OptionsDIV.matches;
	}
	else if(key == KEY_ENTER || key == KEY_RIGHT_ARROW)
	{
		if(SmartLogic_OptionsDIV != null && SmartLogic_OptionsDIV.matches != null && SmartLogic_OptionsDIV.matches.selectedItem >= 0 && SmartLogic_OptionsDIV.matches.selectedItem <= SmartLogic_OptionsDIV.matches.length - 1)
		{
			SmartLogic_SetSelection(elem);
			return;
		}
	}
	else
	{
		// check the input for matches
		for(i=0;i<AirportList_Airports.length;i++)
		{
			var airport = AirportList_Airports[i];
			
			// Handle case where input.length == 3 and airport.airportCode is exact match
			if( input.length == 3 )
			{
				if(SmartLogic_StartsWith(airport.airportCode, input ))
				{
					// move any previously found matches down the list
					for( j=matches.length; j>0; j-- )
					{
						matches[j] = matches[j-1];
					}
					// add exact airport.airportCode match to the top
					matches[0] = airport;
				}
				else if(SmartLogic_StartsWith(airport.cityCode, input) ||
						SmartLogic_StartsWith(airport.airportDisplayName, input))
				{
					matches[matches.length] = airport;
				}
			}
			else
			{
				if(SmartLogic_StartsWith(airport.airportCode, input) ||
					SmartLogic_StartsWith(airport.cityCode, input) ||
					SmartLogic_StartsWith(airport.airportDisplayName, input))
				{
					// match
					matches[matches.length] = airport;
				}
			}
		}
	}

	SmartLogic_HideOptions(elem);

	var innerDIV;
	if(matches.length > 0)
	{
		SmartLogic_OptionsDIV = document.createElement("div");
		SmartLogic_OptionsDIV.style.position = "absolute";
		SmartLogic_OptionsDIV.style.display = "block";
		SmartLogic_OptionsDIV.style.top = (SmartLogic_FindPosY(elem) + elem.offsetHeight) + "px";
		SmartLogic_OptionsDIV.style.left = (SmartLogic_FindPosX(elem) + 20) + "px";
		SmartLogic_OptionsDIV.className = "airportSmartDropdown";
		SmartLogic_OptionsDIV.style.zIndex = "1000";
		SmartLogic_OptionsDIV.matches = matches;
		SmartLogic_OptionsDIV.elem = elem;
		
		
		for(j=0;j<SmartLogic_OptionsDIV.matches.length-1;j++)
		{
			var airport = matches[j];			
			var airportDisplayText = document.createTextNode( " " + airport.airportDisplayName + " " );
			var airportDisplaySpan = document.createElement("span");
			var airportDisplayDIV = document.createElement("div");
			var className = "airportSmartDropdownOption";
			if(SmartLogic_OptionsDIV.matches.selectedItem == j)
			{
				className = "airportSmartDropdownOptionSelected";
				var valueDisplayText = airport.airportCode;
				if (displayCode == false)
				{
					valueDisplayText = airport.airportDisplayName
				}
				if(setText) output = valueDisplayText;
			}
			
			airportDisplaySpan.className = className + "Text";
			airportDisplaySpan.appendChild(airportDisplayText);
			
			airportDisplayDIV.id = airport.airportCode;
			airportDisplayDIV.index = j;
			airportDisplayDIV.onmouseover = displaySelect;
			airportDisplayDIV.onclick = selectOption;
			airportDisplayDIV.className = className;
			airportDisplayDIV.appendChild(airportDisplaySpan);
			SmartLogic_OptionsDIV.appendChild(airportDisplayDIV);
		}
		
		//
		// Last item requires special DIV class
		//
		var airport = matches[SmartLogic_OptionsDIV.matches.length-1];			
		var airportDisplayText = document.createTextNode( " " + airport.airportDisplayName + " " );
		var airportDisplaySpan = document.createElement("span");
		var airportDisplayDIV = document.createElement("div");
		var className = "airportSmartDropdownOptionLast";
		if(SmartLogic_OptionsDIV.matches.selectedItem == SmartLogic_OptionsDIV.matches.length-1)
		{
			className = "airportSmartDropdownOptionLastSelected";
			var valueDisplayText = airport.airportCode;
			if (displayCode == false)
			{
				valueDisplayText = airport.airportDisplayName
			}
			if(setText) output = valueDisplayText;
		}
		
		airportDisplaySpan.className = className + "Text";
		airportDisplaySpan.appendChild(airportDisplayText);
		
		airportDisplayDIV.id = airport.airportCode;
		airportDisplayDIV.index = SmartLogic_OptionsDIV.matches.length-1;
		airportDisplayDIV.onmouseover = displaySelectLast;
		airportDisplayDIV.onclick = selectOption;
		airportDisplayDIV.className = className;
		airportDisplayDIV.appendChild(airportDisplaySpan);
		SmartLogic_OptionsDIV.appendChild(airportDisplayDIV);		
		
		
		document.body.appendChild(SmartLogic_OptionsDIV);

		var maxWidth = 0;
		for(j=0;j<SmartLogic_OptionsDIV.children.length;j++)
		{
			if (SmartLogic_OptionsDIV.children[j].clientWidth > maxWidth)
			{
				maxWidth = SmartLogic_OptionsDIV.children[j].clientWidth;
			}
		}
		
		SmartLogic_OptionsDIV.style.width = maxWidth + 6;

		for(j=0;j<SmartLogic_OptionsDIV.children.length;j++)
		{
			SmartLogic_OptionsDIV.children[j].style.width = "100%";
		}
		
		// handle the fact that IE cant show a dynamic DIV over windowed controls
		if(document.body.insertAdjacentHTML)
		{
		    var iframeSrcAttribute = "";
		    if (location.protocol=="https:") {
                iframeSrcAttribute = 'src="https:' + appName + '/resources/_javascript/blank.htm"';
            }
            else {
                iframeSrcAttribute = 'src="about:blank"';
            }

			var iframe = "<IFRAME " + iframeSrcAttribute + " id='_internal_iframe_id' style='POSITION:absolute;LEFT:" + SmartLogic_OptionsDIV.style.left + ";TOP:" + SmartLogic_OptionsDIV.style.top + ";WIDTH:" + SmartLogic_OptionsDIV.offsetWidth + "px;HEIGHT:" + SmartLogic_OptionsDIV.offsetHeight + "px;Z-INDEX:999;' src='about:blank' frameBorder='0' scrolling='no'></IFRAME>";
			SmartLogic_OptionsDIV.insertAdjacentHTML("afterEnd", iframe);
			SmartLogic_OptionsDIV.iframeID = "_internal_iframe_id";
		}

		elem.value = output;
		SmartLogic_Deselect(elem);

	}
}

function displaySelect()
{
	var prevSelect = this.parentNode.childNodes[this.parentNode.matches.selectedItem];
	//special case for the last option in the list
	if (prevSelect.index == this.parentNode.childNodes.length-1)
	{
		prevSelect.className = "airportSmartDropdownOptionLast";
	}
	else
	{
		prevSelect.className = "airportSmartDropdownOption";
	}
	this.className = "airportSmartDropdownOptionSelected";
	this.parentNode.matches.selectedItem = this.index;
}

function displaySelectLast()
{
	var prevSelect = this.parentNode.childNodes[this.parentNode.matches.selectedItem];
	prevSelect.className = "airportSmartDropdownOption";
	this.className = "airportSmartDropdownOptionLastSelected";
	this.parentNode.matches.selectedItem = this.index;
}

function selectOption()
{
	SmartLogic_SetSelection(this.parentNode.elem)
}

function SmartLogic_SetSelection2(elem)
{
	SmartLogic_SetSelection(this.elem);
}

/// <summary>
/// 
/// </summary>
/// <returns></returns>
/// <created>March 2, 2006</created>
/// <by>Keith Jaskiewicz</by>
/// <revision by="Sunil Gupta" dated="4/11/2006">Use airportDisplayName rendered by server. Client does not build the display name.</revision>
function SmartLogic_SetSelection(elem)
{  
	SmartLogic_Processing = true;	
	
	if(SmartLogic_OptionsDIV != null && SmartLogic_OptionsDIV.matches != null && SmartLogic_OptionsDIV.matches.selectedItem >= 0 && SmartLogic_OptionsDIV.matches.selectedItem <= SmartLogic_OptionsDIV.matches.length - 1)
	{
		var airport = SmartLogic_OptionsDIV.matches[SmartLogic_OptionsDIV.matches.selectedItem];		
		elem.value = airport.airportDisplayName;
		PopulateOriginAirport (elem);		
	}
	SmartLogic_HideOptions(elem);
	SmartLogic_Processing = false;
}

/// <summary>
/// Auto populate the next Origin Aiport control with the name of the previous Desitination Airpot control
/// </summary>
/// <returns></returns>
/// <created>6/27/2006</created>
/// <by>Gene Strickland</by>
function PopulateOriginAirport (elem)
{
	try
	{
		//Get the identifier for the destination box
		var str = "_multiCityFlightPairSelector_airportPairSelectorControl_destinationAirportSmartLogicTextBox";
		//If this is not a destination box, exit.
		var index = (elem.id).indexOf(str);
		if (index < 1)
			return;
			 
		//The numeric identifier is just before the substring index	 
		var num = (elem.id).substr(index-1, 1); 
		
		//The next origin box identifier number is +2
		var elnum  = new Number(num) + 2;
		var origin = "multiCityControl_multiCityRepeater__ctl" + elnum + "_multiCityFlightPairSelector_airportPairSelectorControl_originAirportSmartLogicTextBox";
		//Get the origin box
		var elem2 = document.getElementById(origin);
		
		//If the origin box was found, and it is empty, populate it with the destination airport
		if (elem2 != null && elem != null && (elem2.value.length==0))
		{
			elem2.value = elem.value;
		}
	}
	catch (e)
	{
	}	
}

function SmartLogic_OnClick(elem)
{
	var val = elem.value;
}

function SmartLogic_OnFocus(elem)
{
	var start = elem.value.length;
	var length = 0;

	if(elem.createTextRange)
	{
		var oRange = elem.createTextRange();
		oRange.moveStart('character', start);
		oRange.moveEnd('character', -1 * length);
		oRange.select();
		if(elem.value.length > 3)
			elem.select();
	}
	else if(elem.setSelectionRange)
	{
		elem.setSelectionRange(start, start + length);
	}

	elem.focus();
}

/// <summary>
/// This method clears the default text of the textbox
/// </summary>
/// <param name="sender">object control that is calling this method</param>
/// <created>10/09/2006</created>
/// <by>Nick Murphy</by>
function SmartLogic_ClearText(sender) {	
	
	if (sender.defaultValue == sender.value) 
	{
		sender.value = "";
	}
	
	sender.focus();
}

function SmartLogic_Deselect(elem)
{
	var start = elem.value.length;
	var length = 0;

	if(elem.createTextRange)
	{
		var oRange = elem.createTextRange();
		oRange.moveStart('character', start);
		oRange.moveEnd('character', -1 * length);
		oRange.select();
	}
	else if(elem.setSelectionRange)
	{
		elem.setSelectionRange(start, start + length);
	}

	elem.focus();
}

function SmartLogic_HideOptions(elem)
{
	if(SmartLogic_OptionsDIV != null)
	{
		if(SmartLogic_OptionsDIV.iframeID != null)
		{
			var iframe = document.getElementById(SmartLogic_OptionsDIV.iframeID);
			document.body.removeChild(iframe);
		}

		document.body.removeChild(SmartLogic_OptionsDIV);
		SmartLogic_OptionsDIV = null;
	}
}

function SmartLogic_StartsWith(string, phrase)
{
	var phraseLength = phrase.length;
	var result = string.substring(0, phraseLength);
	if(result.toUpperCase() == phrase.toUpperCase())
		return true;
	else
		return false;
}

function SmartLogic_FindPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function SmartLogic_FindPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function SmartLogic_DeriveAirportCode(displayText)
{	
    var airportCode = "";
	var tokenIndex = displayText.indexOf( '(' );
	
	if( tokenIndex != -1 && displayText.length > tokenIndex + 3 )
	{
		airportCode = displayText.substring( tokenIndex + 1, tokenIndex + 4 );
	}
	else if( displayText.length > 3 )
	{
		airportCode = displayText.substring( 0, 4 );
	}
	
	return airportCode;
}

/// <summary>
/// Forces Safari to use the Display Text instead of user input.
/// </summary>
/// <created>April 12, 2007</created>
/// <by>John Sheets</by>
function SafariFix(inputText)
{
    if(SmartLogic_OptionsDIV != null && SmartLogic_OptionsDIV.matches != null && SmartLogic_OptionsDIV.matches.selectedItem >= 0 && SmartLogic_OptionsDIV.matches.selectedItem <= SmartLogic_OptionsDIV.matches.length - 1)
    {
	    var airport = SmartLogic_OptionsDIV.matches[SmartLogic_OptionsDIV.matches.selectedItem];		
	    inputText = airport.airportDisplayName.toUpperCase();
    }

    return inputText;
}

/// validates that the input value is a valid airport code
/// <revision by="Sunil Gupta" dated="10/24/2006">For cars if user doesn't put drop off location and leave it as
/// "same as pick up" then should be a valid selection, which is what the change to this method deals with.</revision>
/// <revision by="John Sheets" date="04/12/2007">Safari tries to validate on what the user typed in instead of a selection.</revision>
function SmartLogic_Validate(sender, args, AirportList_Airports, allowNonAirportText)
{   
    var inputText = args.Value.toUpperCase(); 
    
    if (navigator.userAgent.toLowerCase().indexOf("safari") != -1)
    {        
        inputText = SafariFix(inputText);
    }
    
	args.IsValid = false;
	
	if(allowNonAirportText && typeof smartCityValidTextList != "unassigned")
	{
		for(var index = 0; index < smartCityValidTextList.length; index++)
		{
			if(inputText == smartCityValidTextList[index].toUpperCase())
			{
				args.IsValid = true;
				return;
			}
		}
	}	
	
	var val = SmartLogic_DeriveAirportCode(inputText);
	
	for(i=0;i<AirportList_Airports.length;i++)
	{
		var airport = AirportList_Airports[i];
		if(airport.airportCode == val)
		{
			// it is valid
			args.IsValid = true;
			return;
		}
	}
}

// validates that the input value is a valid airport code
function SmartLogic_ValidateHotel(sender, args, AirportList_Airports)
{
	var val = args.Value.toUpperCase();
	
	if (navigator.userAgent.toLowerCase().indexOf("safari") != -1)
    {
        val = SafariFix(val);
    }

	args.IsValid = false;
	for(i=0;i<AirportList_Airports.length;i++)
	{
		var airport = AirportList_Airports[i];
		if(airport.airportDisplayName.toUpperCase() == val)
		{
			// it is valid
			args.IsValid = true;
		}
	}
}

/// <summary>
/// Returns airport object.
/// </summary>
/// <returns></returns>
/// <created>March 2, 2006</created>
/// <by>Keith Jaskiewicz</by>
/// <revision by="Sunil Gupta" dated="3/29/2006">Moved this method from AirportList.aspx to this file.</revision>
/// <revision by="Sunil Gupta" dated="4/11/2006">Use airportDisplayName rendered by server. Client does not build the display name.</revision>
/// <revision by="Keith Jaskiewicz" date="09/02/2006">Reduce the airport object.</revision>
//function AirportList_Airport(airportCode, cityCode, airportDisplayName)
//{
//	this.airportCode = airportCode;
//	this.airportDisplayName = airportDisplayName;
//	this.cityCode = cityCode;
//}
function AirportList_Airport(airportCode, airportName, cityCode, cityName, airportDisplayName, stateCode, stateName, countryCode)
{
	this.airportCode = airportCode;
	this.airportName = airportName;
	this.airportDisplayName = airportDisplayName;
	this.cityCode = cityCode;
	this.cityName = cityName;	
	this.stateCode = stateCode;
	this.stateName = stateName;
	this.countryCode = countryCode;
}
//-->
