//<script>
//dropDownLossFocus(this.id); liveSearchEvents(event,this.id,'DepartureCity',this.form.name,200,''); liveSearchStart(event,this.id,'DepartureCity',this.form.name,200,'');
var liveSearchReq = false;
var isIE = false;
var DropDownID = "";
var DataSource = "";
var iRowCount=0;
var iRecordCount =0;
var FormName="";
var DropDownMinHeight=21;
var DropDownMaxHeight=200;
var MethodToFire="";
var LossFocusTimerID;
var iKeyUpFireCount;
var iKeyDownFireCount;


sniffer = new CJL_BrowserSniffer();
//sniffer.isOpera(),sniffer.isIEMac() ,sniffer.isIEWin(),sniffer.isGecko()
//sniffer.getVersion()

// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
	liveSearchReq = new XMLHttpRequest();
}
function getNewDropDownId(inStr)
{
    if(inStr!='')
    {
        return inStr.substring(0,inStr.lastIndexOf('_'));
    }
}
function liveSearchStart(e,sDropDownID, sDataSource, sFormName, iDropDownMaxHeight,sMethodToFire) {
	//Set Global Variables
	sDropDownID=getNewDropDownId(sDropDownID);
	FormName=sFormName;
	DropDownID=sDropDownID;
	DataSource=sDataSource;
	DropDownMaxHeight=iDropDownMaxHeight;
	MethodToFire=sMethodToFire;
	//End Global Variables

	var sText=liveSearchGetDropDownText(sDropDownID);
	
	if (!e) var e = window.event;
	
	if ((e.keyCode == 13) || (e.keyCode == 9) || (e.keyCode == 27) || (e.keyCode == 8) || (e.keyCode == 38) ||(e.keyCode == 40)) {
		return false;
	}
		//document.getElementById("debug100").value = document.getElementById("debug100").value + "|";
	
	if (alphanumeric(sText.slice(sText.length-1,sText.length))==false)
	{
		
		if (RemoveInvalid(sText)=="")
		{
				//alert("Invalid Character2");
				var InputBox = document.getElementById(sDropDownID + "_InputBox");
				InputBox.value="";
	
				//liveSearchSetDropDownText(DropDownID, sText.slice(0,sText.length-1));
				return;
		}
		
	}
	else
	{
		if (sText.length > 2)
		{
			liveSearchDoSearch(sText);
		}
	}
}

function liveSearchEvents(e,sDropDownID, sDataSource, sFormName, iDropDownMaxHeight,sMethodToFire) {
	//Set Global Variables
	sDropDownID=getNewDropDownId(sDropDownID);
	FormName=sFormName;
	DropDownID=sDropDownID;
	DataSource=sDataSource;
	DropDownMaxHeight=iDropDownMaxHeight;
	MethodToFire=sMethodToFire;
	//End Global Variables
	
	var sText=liveSearchGetDropDownText(sDropDownID);
	
	// e gives access to the event in all browsers
	if (!e) var e = window.event;
	
	//Enter or Tab Key Pressed
	if ((e.keyCode == 13) || (e.keyCode == 9)) {
		
		if (sText != ''){
			if( sniffer.isSafari() ){
				//Mac Safari
				var DropDownFrame = document.getElementById(sDropDownID +"DropDownFrame");
				var IFrameDoc = DropDownFrame.contentWindow.document;
			}
			else{
				var DropDownFrame = document.getElementById(sDropDownID +"DropDownFrame");
				var IFrameDoc = DropDownFrame.contentWindow.document;
			}
			
			var MenuItems=IFrameDoc.getElementsByTagName("A");
			
			var oInputBox=document.getElementById(sDropDownID + '_InputBox');
			var oHiddenField=document.getElementById(sDropDownID + 'HiddenField');
			
			var iSelectedItem=0;
			var bSelectedItemFound='false';
				
			for(var j=0; j < MenuItems.length; j++){
				Item=MenuItems[j];
					
				if (Item.className == 'MenuItemHover'){
					bSelectedItemFound='true';
					break;
				}
			}
		
			if (bSelectedItemFound=='true'){
				oInputBox.value=Item.getAttribute("SelectText");
				oHiddenField.value=Item.getAttribute("SelectValue");
				sMethodToFire=Item.getAttribute("MethodToFire")
				
				liveSearchToggle(sDropDownID,'none');
				
				if (sMethodToFire != ''){
					if (eval('window.' + sMethodToFire) != null){
						eval('window.' + sMethodToFire + '(Item)');
					}
				
					else if (eval('window.parent.' + sMethodToFire) != null){
						eval('window.parent.' + sMethodToFire + '(Item)');
					}
					
				}
			}

			liveSearchToggle(sDropDownID,'none');
			oInputBox.focus(); 
		}
		
		return;
	}
	
	//ESC
	else if (e.keyCode == 27) {
		liveSearchSetDropDown(sDropDownID, '', '')
		liveSearchToggle(sDropDownID,'none');
		return;
	}
	
	//BackSpace
	else if (e.keyCode == 8) {
		liveSearchSetDropDownValue(sDropDownID, '')
		
		if (sText.length < 2) {			
			liveSearchToggle(sDropDownID,'none');
		}
	}
	else if (e.keyCode == 40 ){
		//KEY DOWN
		
		if( sniffer.isSafari() ){
			if (iKeyDownFireCount==1){
				iKeyDownFireCount=0;
				return;
			}else{
				iKeyDownFireCount=1;
			}
		}
		
		if (iRecordCount > 0){
			if( sniffer.isSafari() )
			{
				var DropDownFrame = document.getElementById(sDropDownID +"DropDownFrame");
				var IFrameDoc = DropDownFrame.contentWindow.document;
			}
			else{
				var DropDownFrame = document.getElementById(sDropDownID +"DropDownFrame");
				var IFrameDoc = DropDownFrame.contentWindow.document;
			}
			
			var MenuItems=IFrameDoc.getElementsByTagName("A");
						
			var iSelectedItem=0;
			var bSelectedItemFound='false';
			
			for(var j=0; j<MenuItems.length; j++){
				Item=MenuItems[j];
				
				if (Item.className == 'MenuItemHover'){
					//Deselect the Item
					iSelectedItem = j;
					bSelectedItemFound = 'true';
					Item.className = 'MenuItem';	
					break;
				}
			}
			//document.getElementById("debug100").value = document.getElementById("debug100").value + "," + iSelectedItem;
			
			if (bSelectedItemFound=='false'){
				//At the Begining of the list
				MenuItems[0].className='MenuItemHover';
				
				if (MenuItems[0].nextSibling){
					MenuItems[0].nextSibling.scrollIntoView(false);
				}else if (MenuItems[0].previousSibling){
					MenuItems[0].previousSibling.scrollIntoView(false);
				}else{
					MenuItems[0].scrollIntoView(false);
				}
			} 
			else if (iSelectedItem==MenuItems.length-1){
				//Reached End of list
				Item.className='MenuItemHover';

				if (Item.nextSibling){
					Item.nextSibling.scrollIntoView(false);
				}else if (Item.previousSibling){
					Item.previousSibling.scrollIntoView(false);
				}else{
					Item.scrollIntoView(false);
				}
			}
			else{
				//Highlight the next item.
				MenuItems[iSelectedItem + 1].className='MenuItemHover';

				if (MenuItems[iSelectedItem + 1].nextSibling){
					MenuItems[iSelectedItem + 1].nextSibling.scrollIntoView(false);
				}else if (MenuItems[iSelectedItem + 1].previousSibling){
					MenuItems[iSelectedItem + 1].previousSibling.scrollIntoView(false);
				}else{
					MenuItems[iSelectedItem + 1].scrollIntoView(false);
				}
			}	
		}
		
		return;	
	} 
	
	else if (e.keyCode == 38 ) {
		//KEY UP
		//document.getElementById("debug100").value = document.getElementById("debug100").value + ",KeyUp";

		if( sniffer.isSafari() ){
			if (iKeyUpFireCount==1){
				iKeyUpFireCount=0;
				return;
			}else{
				iKeyUpFireCount=1;
			}
		}
		
		if (iRecordCount > 0){
			if( sniffer.isSafari() ){
				var DropDownFrame = document.getElementById(sDropDownID +"DropDownFrame");
				var IFrameDoc = DropDownFrame.contentWindow.document;
			}
			else{
				var DropDownFrame = document.getElementById(sDropDownID +"DropDownFrame");
				var IFrameDoc = DropDownFrame.contentWindow.document;
			}

			var MenuItems=IFrameDoc.getElementsByTagName("A");
			
			var iSelectedItem=0;
			var bSelectedItemFound='false';
			
			for(var j=0; j<MenuItems.length; j++){
				Item=MenuItems[j];
				
				if (Item.className == 'MenuItemHover'){
					//Deselect the Item
					iSelectedItem = j;
					bSelectedItemFound = 'true';
					Item.className = 'MenuItem';	
					break;
				}
			}

			if (bSelectedItemFound == 'false'){
				//At the Begining of the list
				MenuItems[0].className='MenuItemHover';
				
				if (MenuItems[0].nextSibling){
					MenuItems[0].nextSibling.scrollIntoView(false);
				}else if (MenuItems[0].previousSibling){
					MenuItems[0].previousSibling.scrollIntoView(false);
				}else{
					MenuItems[0].scrollIntoView(false);
				}
			}
			else if ((iSelectedItem - 1) < 0){
				//At the Begining of the list
				MenuItems[0].className='MenuItemHover';
				
				if (MenuItems[0].nextSibling){
					MenuItems[0].nextSibling.scrollIntoView(false);
				}else if (MenuItems[0].previousSibling){
					MenuItems[0].previousSibling.scrollIntoView(false);
				}else{
					MenuItems[0].scrollIntoView(false);
				}
			}
			else{
				//Highlight the next item.
				MenuItems[iSelectedItem - 1].className='MenuItemHover';
				MenuItems[iSelectedItem - 1].scrollIntoView(false);

				if (MenuItems[iSelectedItem - 1].nextSibling){
					MenuItems[iSelectedItem - 1].nextSibling.scrollIntoView(false);
				}else if (MenuItems[iSelectedItem - 1].previousSibling){
					MenuItems[iSelectedItem - 1].previousSibling.scrollIntoView(false);
				}else{
					MenuItems[iSelectedItem - 1].scrollIntoView(false);
				}
			}	
		}
		
		return;		
	} 
}

function liveSearchDoSearch(sText) {

	liveSearchSetDropDownValue(DropDownID,'');
	
	if ( eval('document.forms.' + FormName + '.' + DropDownID + '_InputBox.value') == "") {
		liveSearchToggle(DropDownID,'none');
		return false;
	}
	if(DataSource=='DepartureCity')
	    LibTrv.Web.WS.AutoCompleteService.GetDepartureCityList(sText,"20",FillCities,ErrorHandler,TimeOutHandler);
	else
	    LibTrv.Web.WS.AutoCompleteService.GetDestinationCityList(sText,"20",FillCities,ErrorHandler,TimeOutHandler);
}

function FillCities(result)
{
    var str='<div id="LSResult">';
    for(var i=0;i<result.length;i++)
    {
        if(i==0)
            str+="<a href='javascript:void(0);'  class='MenuItem' DropDownID='" +DropDownID + "'  SelectValue='LAS' SelectText=\"" + result[i] + "\" 		ParentDestCode='' MethodToFire='' onMouseOver='javascript: menuItemMouseOver(this);' onmouseout='javascript: menuItemMouseOut(this);'  onClick='javascript: selectDropDownItem(this);' style='border-bottom: 1px dotted #000;'><Div style='width:100%;'>" + result[i] + "</Div></a>";
        else
            str+="<a href='javascript:void(0);'  class='MenuItem' DropDownID='" + DropDownID + "'  SelectValue='DFW' SelectText=\"" + result[i] + "\" 		ParentDestCode='' MethodToFire='' onMouseOver='javascript: menuItemMouseOver(this);' onmouseout='javascript: menuItemMouseOut(this);'  onClick='javascript: selectDropDownItem(this);'><Div style='width:100%;'>" + result[i] + "</Div></a>";
    }
    str+="<input type='hidden' id='" + DropDownID + "RecordCount' name='" + DropDownID + "RecordCount' value='" + result.length + "'>";
    str+="</div>";
    liveSearchProcessReqChange(str);
}
    
function liveSearchProcessReqChange(sResponseText) 
{
    if (sResponseText.length > 0)
    {
        if( sniffer.isSafari() )
        {
            //Mac Safari
            var  DropDownFrame = document.getElementById(DropDownID +"DropDownFrame");
            var IFrameDoc = DropDownFrame.contentWindow.document;
        }
        else
        {
            var  DropDownFrame = document.getElementById(DropDownID +"DropDownFrame");
            var IFrameDoc = DropDownFrame.contentWindow.document;
        }
        
        IFrameDoc.body.innerHTML=sResponseText;		
   
        //Lets get the RecordCount
        if (IFrameDoc.getElementById(DropDownID +"RecordCount"))
        {
            iRecordCount = IFrameDoc.getElementById(DropDownID +"RecordCount").value;
            if (iRecordCount > 0)
            {
                if (iRecordCount == 1)
                {
                    var iHeight=DropDownMinHeight;
                }
                else
                {
                    var iHeight=(iRecordCount*21);
                }
                if (iHeight > DropDownMaxHeight)
                {
                    iHeight=DropDownMaxHeight;
                }
                liveSearchSelectFirstItem(DropDownID);
                liveSearchResizeDropDown(DropDownID,iHeight);
                liveSearchToggle(DropDownID,'block');
            }
            else
            {
                liveSearchToggle(DropDownID,'none');
            }
        }
    }
    else
    {
        liveSearchToggle(DropDownID,'none');
    }
}


function liveSearchSelectFirstItem(sDropDownID){
	if( sniffer.isSafari() ){
		var  DropDownFrame = document.getElementById(sDropDownID +"DropDownFrame");
		var IFrameDoc = DropDownFrame.contentWindow.document;
	}
	else{
		var  DropDownFrame = document.getElementById(sDropDownID +"DropDownFrame");
		var IFrameDoc = DropDownFrame.contentWindow.document;
	}
	
	var MenuItems=IFrameDoc.getElementsByTagName("A");
	
	MenuItem=MenuItems[0];		
	MenuItem.className = 'MenuItemHover';
}


function liveSearchToggle(sDropDownID,sState){
	if (sState=='none') {
		iRecordCount=0;	
	}

	if (document.all) {
		if (eval( "document.all." + sDropDownID + "DropDownFrame")){
			if (sState=="none"){
				eval( "document.all." + sDropDownID + "DropDownFrame").style.width = "0px";
				eval( "document.all." + sDropDownID + "DropDownFrame").style.height = "0px";
			}else{
				eval( "document.all." + sDropDownID + "DropDownFrame").style.borderRight = "1px solid #000000";
				eval( "document.all." + sDropDownID + "DropDownFrame").style.borderBottom = "1px solid #000000";
				eval( "document.all." + sDropDownID + "DropDownFrame").style.width = eval("document.all." + sDropDownID + "_InputBox").style.width;
			}			
		}
	}

	if (document.getElementById &&!document.all) {
		if (document.getElementById(sDropDownID + "DropDownFrame")){
			obj = document.getElementById(sDropDownID + "DropDownFrame");
			if (sState=="none"){
				obj.style.width = "0px";
				obj.style.height = "0px";
			}else{
				obj.style.borderRight = "1px solid #000000;";
				obj.style.borderBottom = "1px solid #000000;";
				obj.style.width = document.getElementById(sDropDownID + "_InputBox").style.width;
			}
		}
	}
}

function liveSearchSetDropDown(sDropDownID, sDescription, sValue){
	var InputBox = document.getElementById(sDropDownID + "_InputBox");
	var HiddenField = document.getElementById(sDropDownID + "HiddenField");
	
	InputBox.value=sDescription;
	HiddenField.value=sValue;
}

function liveSearchSetDropDownValue(sDropDownID, sValue){
	var HiddenField = document.getElementById(sDropDownID + "HiddenField");
	HiddenField.value=sValue;
}

function liveSearchSetDropDownText(sDropDownID, sValue){
	var InputBox = document.getElementById(sDropDownID + "_InputBox");
	InputBox.value=sValue;
}

function liveSearchGetDropDownValue(sDropDownID){
	var HiddenField = document.getElementById(sDropDownID + "HiddenField");
	return HiddenField.value;
}

function liveSearchGetDropDownText(sDropDownID){
	var InputBox = document.getElementById(sDropDownID + "_InputBox");
	
	//original:
	return InputBox.value;
	//new:	
//	var newText=RemoveInvalid(InputBox.value);
//	if(newText=="") 
//	    InputBox.value="";	
//	return 	newText;
}
/*----------------------------------------------------*/
function RemoveInvalid(sText) {	
	var BadChars = "\[]{}$#&*+=|!><~%^;:"
	var i = 0
	var newText=""	
	if (sText=="") {		
		return sText
	}
	for (i =0; i <= sText.length -1; i++) {
		if (BadChars.indexOf(sText.charAt(i)) != -1) {
            //alert(sText.charAt(i) + " is no good; by RemoveInvalid")
	        alert(BadChars + "   are invalid characters.");
	        return newText	
		} 
//		else
//		{
//		    newText=newText+sText.charAt(i)
//		}
	} // End for loop
	//alert(sText);
	return sText;	
}
/*----------------------------------------------------*/


function liveSearchResizeDropDown(sDropDownID,iHeight){
	
	if (document.all) {
		if (eval( "document.all." + sDropDownID + "DropDownFrame")){
			eval( "document.all." + sDropDownID + "DropDownFrame").style.height = iHeight;
		}
	}

	if (document.getElementById &&!document.all) {
		if (document.getElementById(sDropDownID + "DropDownFrame")){
			obj = document.getElementById(sDropDownID + "DropDownFrame");
			obj.style.height = iHeight+'px';
		}
	}
}

function dropDownLossFocus(sDropDownID){
sDropDownID=getNewDropDownId(sDropDownID);
	LossFocusTimerID = setTimeout("dropDownLossFocusCloseDropDown('" + sDropDownID + "')", 1000);
}

function dropDownLossFocusCloseDropDown(sDropDownID){
	clearTimeout(LossFocusTimerID);
	
	if( sniffer.isSafari() ){
		var  DropDownFrame = document.getElementById(sDropDownID +"DropDownFrame");
		var IFrame = DropDownFrame.contentWindow;
		var IFrameDoc = DropDownFrame.contentWindow.document;
	}
	else{
		var  DropDownFrame = document.getElementById(sDropDownID +"DropDownFrame");
		var IFrame = DropDownFrame.contentWindow;
		var IFrameDoc = DropDownFrame.contentWindow.document;
	}
	
	
	var MenuItems=IFrameDoc.getElementsByTagName("A");

	if (MenuItems.length > 0){
		if (liveSearchGetDropDownValue(sDropDownID) == ''){
			MenuItem=MenuItems[0];
			IFrame.selectDropDownItem(MenuItem);				
		}
	}else{
//		if (liveSearchGetDropDownValue(sDropDownID) == ''){
//			liveSearchSetDropDown(sDropDownID, '', '');
//			liveSearchToggle(sDropDownID,'none');
//	
//			if (document.getElementById(sDropDownID + "aCityParent")){
//				document.getElementById(sDropDownID + "aCityParent").value = '';
//			}
//		}
	}
}

function alphanumeric(alphane){
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
		{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  
		  //alert(hh);
		  
		if(((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123)) || (hh == 39) || (hh == 44) || (hh == 45) || (hh == 32) || (hh == 46)){
		}
		else{
			return false;
		}
	}
	return true;
}

function TimeOutHandler(result)
{
    alert("Timeout :" + result);
}

function ErrorHandler(result)
{
    ;
}
function CJL_BrowserSniffer()
{
   var ua = navigator.userAgent;

   this.isOpera = function()
   {
      return /Opera/.test(ua);
   }

   this.isSafari = function()
   {
      return /Safari/.test(ua);
   }

   this.isGecko = function()
   {
      return navigator.product == "Gecko" &&
	     ! ( this.isOpera() || this.isSafari() );
   }

   this.isIEWin = function()
   {
      return window.external && /Win/.test(ua);
   }

   this.isIEMac = function()
   {
      return window.external && /Mac/.test(ua);
   }

   this.getVersion = function()
   {
      if( this.isIEWin() || this.isIEMac() )
      {
         return Number(ua.match(/MSIE ([0-9.]+)/)[1]);
      }
      else if( this.isSafari() )
      {
         return Number(ua.match(/[0-9.]+$/));
      }
      else if( this.isGecko() )
      {
         var n = ua.match(/rv:([0-9.]+)/)[1];

         var ar = n.split(".");

         var s = ar[0] + ".";

         for(var i = 1; i < ar.length; ++i)
         {
            s += ("0" + ar[i]).match(/.{2}$/)[0];
         }

         return Number(s);
      }
      else if( this.isOpera() )
      {
         return Number(ua.match(/Opera.([0-9.]+)/)[1]);
      }
      else
      {
         return null;
      }
   }
}

  