var editInputHasFocus = false; var selectInputHasFocus = false; var lastEditValue = ""; var alwaysVisible = false; var bIsTabPressed = false; var arrayOfAutoCompleteData = new Array(); var textArrayForCombo = null; var valueArrayForCombo = null; var CurrentComboDivId = null; var CurrentTextInputId = null; var CurrentUrlForUpdate = null; var bIsProxyRequest = false; function EditInputOnKeyDownWithIDs(textInputID, completeDivID, url, selectFirstItem, _event) { EditInputOnKeyDown(getElement(textInputID), getElement(completeDivID), url, _event); } function EditInputOnKeyDown(textInput, completeDiv, url, _event) { CurrentComboDivId = completeDiv.id; CurrentTextInputId = textInput.id; CurrentUrlForUpdate = url; nKeyPressed = GetKeyPressedValue(_event); switch(nKeyPressed) { case 9://TAB ToggleAutoCompleteAndListVisibility( completeDiv, false ); ResetCombo(); return; break; case 37://Left Arrow case 38://Up Arrow case 39://Right Arrow case 40://Down Arrow if( completeDiv.style.visibility=="visible") { setTimeout("SetFocusToComboDelayed(true);", 10); // If opening the Combo, skip first item var selects = completeDiv.getElementsByTagName("SELECT"); if ( selectInputHasFocus == false && nKeyPressed == 40 && selects[0].options.length > 1 ) { selects[0].selectedIndex = 1; } } return; break; } lastEditValue = textInput.value; setTimeout("updateAutoComplete();", 10); } function SetFocusToComboDelayed( show ) { var completeDiv = getElement(CurrentComboDivId); var selects = completeDiv.getElementsByTagName("SELECT"); selects[0].focus(); selectInputHasFocus = true; if ( show ) selects[0].style.visibility = 'visible'; } function updateAutoComplete() { var textInput = getElement(CurrentTextInputId); if( lastEditValue == textInput.value ) return; if( textInput.value=="" ) { var completeDiv = getElement(CurrentComboDivId); ToggleAutoCompleteAndListVisibility( completeDiv, false ); return; } editInputHasFocus = true; // Check if a function is used to fill the Combo var codesrc = arrayOfAutoCompleteData[CurrentUrlForUpdate]; if( codesrc ) { if( codesrc.toString().indexOf("function")==0 ) { codesrc(); } RefreshAutoCompleteCombo(); } else { updateAutoCompleteWithAjaxRequest(); } } function completedAutoCompleteAjaxCall(AjaxRequest) { eval( AjaxRequest.responseText ); RefreshAutoCompleteCombo(); } function failedAutoCompleteAjaxCall(AjaxRequest) { alert('There was a problem with the result of this AJAX request.'); alert( AjaxRequest.responseText ); } // Note: when using proxy url passed in query string has is own query string and // first value must start with ? // http://localhost/test.aspx?Path=http://localhost/AutoComplete.aspx?text=YUL function updateAutoCompleteWithAjaxRequest() { var currentTextInInput = getElement(CurrentTextInputId).value; var url = CurrentUrlForUpdate; var queryString = ""; if(url.indexOf("?text") == -1 ) queryString = "?text=" + encodeURI( currentTextInInput ); else queryString = "&text=" + encodeURI( currentTextInInput ); url += queryString; //alert(url); new Ajax.Request( url, {onComplete:completedAutoCompleteAjaxCall, onFailure:failedAutoCompleteAjaxCall} ); return true; } function RefreshAutoCompleteCombo() { var completeDiv = getElement(CurrentComboDivId); var select = completeDiv.getElementsByTagName("SELECT")[0]; var textInput = getElement(CurrentTextInputId); if( !select ) { alert("No Select to refresh!"); return; } if( !textArrayForCombo || !valueArrayForCombo ) { return; } ToggleAutoCompleteAndListVisibility( completeDiv, true ); while( select.length>0 ) { select.remove(select.length-1); } for(var i=0; i=0; i--) { var tOptionTxt = select.options[i].text.toLowerCase(); var tInputTxt = textInput.value.toLowerCase(); if( tOptionTxt.indexOf(tInputTxt)==0 && tInputTxt.value!="" ) { select.selectedIndex = i; } } if( select.options.length>1 ) { select.size = select.options.length; } else { select.size = 2; } } function ResetCombo() { textArrayForCombo = null; valueArrayForCombo = null; } function ToggleAutoCompleteAndListVisibility( completeDiv, bVisible ) { if(!alwaysVisible) { if ( completeDiv == null ) return; var selects = completeDiv.getElementsByTagName("SELECT"); if ( !bVisible ) { if( completeDiv.style.visibility!="hidden" ) { completeDiv.style.visibility = "hidden"; } if( selects[0].style.visibility!="hidden" ) { selects[0].style.visibility = "hidden"; } } else { if( completeDiv.style.visibility!="visible" ) { completeDiv.style.visibility = "visible"; } if( selects[0].style.visibility!="visible" ) { selects[0].style.visibility = "visible"; } } } } function HideAutoComplete(textInput, completeDiv) { var selects = completeDiv.getElementsByTagName("SELECT"); var idxSel = selects[0].selectedIndex; if( idxSel>=0 ) { textInput.value = selects[0].options[idxSel].text; } ToggleAutoCompleteAndListVisibility( completeDiv, false ); ResetCombo(); } //hotelLocationCodeId is used only for Silent search function HideAutoCompleteKeydownWithID(textInputID, completeDivID, _event, _selectFirstItem, hotelLocationCodeId) { HideAutoCompleteKeydown(getElement(textInputID), getElement(completeDivID), _event, _selectFirstItem, hotelLocationCodeId); } function HideAutoCompleteKeydown(textInput, completeDiv, _event, _selectFirstItem, hotelLocationCodeId) { CurrentComboDivId = completeDiv.id; CurrentTextInputId = textInput.id; if( GetKeyPressedValue(_event)==13 || GetKeyPressedValue(_event)==9)// ENTER { ToggleAutoCompleteAndListVisibility( completeDiv, false ); ResetCombo(); } else { setTimeout("SetInputTextWithCombo("+_selectFirstItem+",'"+hotelLocationCodeId+"');", 10); } } function SetInputTextWithCombo(_selectFirstItem, hotelLocationCodeId) { var textInput = getElement(CurrentTextInputId); var completeDiv = getElement(CurrentComboDivId); var hotelLocationCode = getElement(hotelLocationCodeId); if(completeDiv != null) { var selects = completeDiv.getElementsByTagName("SELECT"); var idxSel = selects[0].selectedIndex; } var selectFirstItem = true; if (_selectFirstItem != null) { selectFirstItem = _selectFirstItem; } if (selectFirstItem == true) { if( idxSel>=0) { textInput.value = selects[0].options[idxSel].text; if (hotelLocationCode != null) { hotelLocationCode.value = selects[0].options[idxSel].value; } } } else { if( idxSel>0) { textInput.value = selects[0].options[idxSel].text; if (hotelLocationCode != null) { hotelLocationCode.value = selects[0].options[idxSel].value; } } } } function HideComboOnBlur(completeDiv, _selectFirstItem, hotelLocationCodeId) { if( !editInputHasFocus && !selectInputHasFocus ) { if (typeof document.body.style.maxHeight != "undefined") { // IE 7, mozilla, safari, opera 9 } else { // IE6, older browsers ToggleAutoCompleteAndListVisibility( getElement( completeDiv ), false ); } ResetCombo(); SetInputTextWithCombo(_selectFirstItem, hotelLocationCodeId); } } function setAutoComplete(textInputId, divId, url) { var textInput = getElement(textInputId); var completeDiv = getElement(divId); if ( completeDiv == null || completeDiv == undefined ) return; var selects = completeDiv.getElementsByTagName("SELECT"); if( selects.length<=0 ) { alert('Pas de combo a editer'); return; } if( url=="" || url==null ) { alert('Pas de url ou de javascript pour updater le combo'); return; } if( !alwaysVisible ) { completeDiv.style.visibility = "hidden"; } //completeDiv.style.width = textInput.style.width; completeDiv.style.width = "300px"; //textInput.onkeydown = function(){ EditInputOnKeyDown(textInput, completeDiv, url); } //textInput.onblur = function(){ editInputHasFocus = false; HideComboOnBlur(completeDiv); } //completeDiv.onkeydown = function(){ HideAutoCompleteKeydown(textInput, completeDiv); } completeDiv.onclick = function(){ HideAutoComplete(textInput, completeDiv); } //selects[0].onblur = function(){ selectInputHasFocus = false; HideComboOnBlur(completeDiv); } } function setAutoCompleteExt(textInputId, divId, url, _selectFirstItem, hotelLocationCodeId) { var textInput = getElement(textInputId); var completeDiv = getElement(divId); var selects = completeDiv.getElementsByTagName("SELECT"); if( selects.length<=0 ) { alert('Pas de combo a editer'); return; } if( url=="" || url==null ) { alert('Pas de url ou de javascript pour updater le combo'); return; } if( !alwaysVisible ) completeDiv.style.visibility = "hidden"; completeDiv.style.width = textInput.style.width; completeDiv.style.width = "300px"; textInput.onkeydown = function(e){ EditInputOnKeyDown(textInput, completeDiv, url, e); } textInput.onblur = function(){ editInputHasFocus = false; HideComboOnBlur(completeDiv, _selectFirstItem, hotelLocationCodeId ); } completeDiv.onkeydown = function(e){ HideAutoCompleteKeydown(textInput, completeDiv, e, _selectFirstItem, hotelLocationCodeId ); } completeDiv.onclick = function(){ HideAutoComplete(textInput, completeDiv); } selects[0].onblur = function(){ selectInputHasFocus = false; HideComboOnBlur(completeDiv, _selectFirstItem , hotelLocationCodeId); } } function GetAutoCompleteFile( autoCompleteType ) { var autoCompleteFile = 'AutoCompleteCity.aspx'; if ( autoCompleteType == 'nocity' ) { autoCompleteFile = 'AutoCompleteNoCity.aspx'; } else if ( autoCompleteType == 'hotelDestination' ) { autoCompleteFile = 'AutoCompleteHotelDestination.aspx'; } return autoCompleteFile; } function setTSGAutoCompleteWithProxy( textInputId, divId, proxyHost, proxyType, autoCompleteType, _selectFirstItem, _hotelLocationCodeId ) { var fullHostName = ''; if ( proxyHost == '' ) proxyHost = 'localhost'; var proxyPath = GetTSGProxyBaseUrl( proxyType ); var proxyHostName = proxyHost + proxyPath; bIsProxyRequest = true; setTSGAutoComplete(textInputId, divId, proxyHostName, autoCompleteType,_selectFirstItem , _hotelLocationCodeId ); } function setTSGAutoComplete(textInputId, divId, ajaxHostName, autoCompleteType, _selectFirstItem , _hotelLocationCodeId) { var fullHostName = ''; if ( ajaxHostName != null && ajaxHostName != '' && ajaxHostName.search( 'http' ) == -1 ) fullHostName = "http://" + ajaxHostName; var autoCompleteFile = GetAutoCompleteFile( autoCompleteType ); setAutoCompleteExt(textInputId, divId, fullHostName + "/TSGUtilityApp/AutoComplete/" + autoCompleteFile, _selectFirstItem, _hotelLocationCodeId); }