﻿var AllCities = new Array();
function QueryString(key) {
    hu = window.location.search.substring(1);
    hu = hu.replace('amp;', '');
    items = hu.split("&");
    for (i = 0; i < items.length; i++) {
        ft = items[i].split("=");
        if (ft[0] == key) {
            return ft[1];
        }
    }
}
function cAutocomplete(sInputId) {
    this.init(sInputId);
}
var LastActiveElement = null;
cAutocomplete.CS_NAME = 'Autocomplete component';
cAutocomplete.CS_OBJ_NAME = 'AC_COMPONENT';
cAutocomplete.CS_LIST_PREFIX = 'ACL_';
cAutocomplete.CS_BUTTON_PREFIX = 'ACB_';
cAutocomplete.CS_INPUT_PREFIX = 'AC_';
cAutocomplete.CS_HIDDEN_INPUT_PREFIX = 'ACH_';
cAutocomplete.CS_INPUT_CLASSNAME = 'dropdown';
cAutocomplete.CB_AUTOINIT = true;
cAutocomplete.CB_AUTOCOMPLETE = false;
cAutocomplete.CB_FORCECORRECT = false;
cAutocomplete.CB_MATCHSUBSTRING = false;
cAutocomplete.CS_SEPARATOR = '|';
cAutocomplete.CS_ARRAY_SEPARATOR = '|';
cAutocomplete.CB_MATCHSTRINGBEGIN = true;
cAutocomplete.CN_OFFSET_TOP = 2;
cAutocomplete.CN_OFFSET_LEFT = -1;
cAutocomplete.CN_LINE_HEIGHT = 17;
cAutocomplete.CN_NUMBER_OF_LINES = 10;
cAutocomplete.CN_HEIGHT_FIX = 8;
cAutocomplete.CN_CLEAR_TIMEOUT = 300;
cAutocomplete.CN_SHOW_TIMEOUT = 400;
cAutocomplete.CN_REMOTE_SHOW_TIMEOUT = 1000;
cAutocomplete.CN_MARK_TIMEOUT = 1;
cAutocomplete.CN_ATTRIBUTE_SEPARATOR = '%';
cAutocomplete.hListDisplayed = null;
cAutocomplete.nCount = 0;
cAutocomplete.FillItems = true;
cAutocomplete.LANGID = JSMAN_LANGID; 
cAutocomplete.PartialCities = true;
cAutocomplete.CS_MIN_CHAR = 3;
cAutocomplete.CS_TEXTBOXCHANGES = false;
cAutocomplete.autoInit = function() {
    var nI = 0;
    var hACE = null;
    var sLangAtt;
    for (nI = 0; nI < document.getElementsByTagName('INPUT').length; nI++) {
        if (document.getElementsByTagName('INPUT')[nI].type.toLowerCase() == 'text') {
            sLangAtt = document.getElementsByTagName('INPUT')[nI].getAttribute('acdropdown');
            if (sLangAtt != null && sLangAtt.length > 0) {
                if (document.getElementsByTagName('INPUT')[nI].id == null || document.getElementsByTagName('INPUT')[nI].id.length == 0) {
                    document.getElementsByTagName('INPUT')[nI].id = cAutocomplete.CS_OBJ_NAME + cAutocomplete.nCount;
                }
                hACE = new cAutocomplete(document.getElementsByTagName('INPUT')[nI].id);
            }
        }
    }
    var nTALength = document.getElementsByTagName('TEXTAREA').length;
    for (nI = 0; nI < nTALength; nI++) {
        sLangAtt = document.getElementsByTagName('TEXTAREA')[nI].getAttribute('acdropdown');
        if (sLangAtt != null && sLangAtt.length > 0) {
            if (document.getElementsByTagName('TEXTAREA')[nI].id == null || document.getElementsByTagName('TEXTAREA')[nI].id.length == 0) {
                document.getElementsByTagName('TEXTAREA')[nI].id = cAutocomplete.CS_OBJ_NAME + cAutocomplete.nCount;
            }
            hACE = new cAutocomplete(document.getElementsByTagName('TEXTAREA')[nI].id);
        }
    }
    var nSelectsLength = document.getElementsByTagName('SELECT').length;
    var aSelect = null;
    for (nI = 0; nI < nSelectsLength; nI++) {

        aSelect = document.getElementsByTagName('SELECT')[nI];
        try {
            if (document.getElementById('slidac') == null) {
                var hidLid = document.createElement('input');
                hidLid.setAttribute('type', 'hidden');
                hidLid.setAttribute('name', 'slid');
                hidLid.setAttribute('id', 'slidac');
                hidLid.setAttribute('value', cAutocomplete.LANGID)
                aSelect.parentNode.appendChild(hidLid);
            }
        } catch (errInp) { }
        sLangAtt = aSelect.getAttribute('acdropdown');

        if (sLangAtt != null && sLangAtt.length > 0) {
            if (aSelect.id == null || aSelect.id.length == 0) {
                aSelect.id = cAutocomplete.CS_OBJ_NAME + cAutocomplete.nCount;
            }
            hACE = new cAutocomplete(aSelect.id);
            nSelectsLength--;
            nI--;
        }
    }
};

if (cAutocomplete.CB_AUTOINIT) {
    if (window.attachEvent) {
        window.attachEvent('onload', cAutocomplete.autoInit);
    }
    else if (window.addEventListener) {
        window.addEventListener('load', cAutocomplete.autoInit, false);
    }
}
cAutocomplete.prototype.FillPartial = function(word) {
    if (word.indexOf(",") == -1) {
        this.sListURL = "/Cities.aspx?q=" + encodeURIComponent(word) + "&l=" + JSMAN_LANGID;
        this.hXMLHttp = XmlHttp.create();
        this.loadListArray();
    }



};
cAutocomplete.prototype.init = function(sInputId) {
    this.sInputId = sInputId;
    this.sListId = cAutocomplete.CS_LIST_PREFIX + sInputId;
    var AutoDropDown = document.getElementById(sInputId);
    AutoDropDown.style.visibility = 'hidden';
    var selectedValue = AutoDropDown.options[AutoDropDown.selectedIndex].text.toString();
    var selectedID = AutoDropDown.options[AutoDropDown.selectedIndex].value;
    AutoDropDown.length = 0;
    var wasSelected = false;
    for (var i = 0; i < AllCities.length; i++) {
        var tmp = AllCities[i];
        var op = new Option(tmp.split(';')[0], tmp.split(';')[1]);
        if (tmp.split(';')[0].split(',')[0] == selectedValue) {
            op.selected = true;
            LastActiveElement = tmp.split(';')[0].split('|')[0];
            wasSelected = true;

        }
        AutoDropDown.options[i] = op;
    }
    if(wasSelected==false)
    {
        var defaultOption = new Option(selectedValue, selectedID);
        defaultOption.selected = true;
        AutoDropDown.options[AllCities.length] = defaultOption;
    }
    this.sObjName = cAutocomplete.CS_OBJ_NAME + '_obj_' + (cAutocomplete.nCount++);
    this.hObj = this.sObjName;
    this.hActiveSelection = null;
    this.nSelectedItemIdx = -1;
    this.sSelectedItemValue = '';
    this.sLastActiveValue = '';
    this.sActiveValue = '';
    this.bListDisplayed = false;
    this.nItemsDisplayed = 0;
    this.bAssociative = false;
    this.sHiddenInputId = null;
    this.bHasButton = false;
    this.aData = null;
    this.aSearchData = new Array();
    this.bSorted = true;
    this.nLastMatchLength = 0;
    this.bForceCorrect = cAutocomplete.CB_FORCECORRECT;
    var sForceCorrect = document.getElementById(this.sInputId).getAttribute('autocomplete_forcecorrect');
    if (sForceCorrect != null && sForceCorrect.length > 0) {
        this.bForceCorrect = eval(sForceCorrect + ";");
    }
    this.bMatchBegin = cAutocomplete.CB_MATCHSTRINGBEGIN;
    var sMatchBegin = document.getElementById(this.sInputId).getAttribute('autocomplete_matchbegin');
    if (sMatchBegin != null && sMatchBegin.length > 0) {
        this.bMatchBegin = eval(sMatchBegin + ";");
    }
    this.bMatchSubstring = cAutocomplete.CB_MATCHSUBSTRING;
    var sMatchSubstring = document.getElementById(this.sInputId).getAttribute('autocomplete_matchsubstring');
    if (sMatchSubstring != null && sMatchSubstring.length > 0) {
        this.bMatchSubstring = true;
    }
    this.bAutoComplete = cAutocomplete.CB_AUTOCOMPLETE;
    this.bAutocompleted = false;
    var sAutoComplete = document.getElementById(this.sInputId).getAttribute('autocomplete_complete');
    if (sAutoComplete != null && sAutoComplete.length > 0) {
        this.bAutoComplete = eval(sAutoComplete + ";");
    }
    this.formatOptions = null;
    var sFormatFunction = document.getElementById(this.sInputId).getAttribute('autocomplete_format');
    if (sFormatFunction != null && sFormatFunction.length > 0) {
        this.formatOptions = eval(sFormatFunction + ";");
    }
    this.onSelect = null;
    var sOnSelectFunction = document.getElementById(this.sInputId).getAttribute('autocomplete_onselect');
    if (sOnSelectFunction != null && sOnSelectFunction.length > 0) {
        this.onSelect = eval(sOnSelectFunction + ";");
    }
    this.bAssociative = true;
    var sAssociative = document.getElementById(this.sInputId).getAttribute('autocomplete_assoc');
    if (sAssociative != null && sAssociative.length > 0) {
        if (sAssociative == 'false') {
            this.bAssociative = false;
        }
    }
    if (this.getListArrayType() != 'url') {
        this.bRemoteList = false;
    }
    else {
        this.bRemoteList = true;
        this.sListURL = this.getListURL();
        this.hXMLHttp = XmlHttp.create();
    }
    this.initListArray();
    this.initListContainer();
    this.initInput();
    eval(this.hObj + '= this' + ";");
    AutoDropDown.style.visibility = 'visible';
};
cAutocomplete.prototype.initInput = function() {
    var hInput = document.getElementById(this.sInputId);
    hInput.hAutocomplete = this;
    var hContainer = document.getElementById(this.sListId);
    hContainer.hAutocomplete = this;
    var nWidth = hInput.offsetWidth;
    if (!nWidth || nWidth == 0) {
        var hOWInput = hInput.cloneNode(true);
        hOWInput.style.position = 'absolute';
        hOWInput.style.top = '-1000px';
        document.body.appendChild(hOWInput);
        var nWidth = hOWInput.offsetWidth;
        document.body.removeChild(hOWInput);
    }
    var sInputName = hInput.name;
    var hForm = hInput.form;
    var bHasButton = false;
    var sHiddenValue = hInput.value;
    var sValue = hInput.type.toLowerCase() == 'text' ? hInput.value : '';
    var sHasButton = hInput.getAttribute('autocomplete_button');
    if (sHasButton != null && sHasButton.length > 0) {
        bHasButton = true;
    }
    if (hInput.type.toLowerCase() == 'select-one') {
        bHasButton = true;
        if (hInput.selectedIndex >= 0) {
            sHiddenValue = hInput.options[hInput.selectedIndex].value;
            sValue = hInput.options[hInput.selectedIndex].text.split('|')[0];
        }
    }
    if (hForm) {
        var hHiddenInput = document.createElement('INPUT');
        hHiddenInput.id = cAutocomplete.CS_HIDDEN_INPUT_PREFIX + this.sInputId;
        hHiddenInput.type = 'hidden';
        hForm.appendChild(hHiddenInput);
        if (this.bAssociative) {
            hHiddenInput.name = sInputName;
            hInput.name = cAutocomplete.CS_INPUT_PREFIX + sInputName;
        }
        else {
            hHiddenInput.name = cAutocomplete.CS_INPUT_PREFIX + sInputName;
        }
        hHiddenInput.value = sHiddenValue;
        this.sHiddenInputId = hHiddenInput.id;
    }
    if (bHasButton) {
        this.bHasButton = true;
        var hInputContainer = document.createElement('DIV');
        hInputContainer.className = 'acinputContainer';
        hInputContainer.style.width = nWidth;
        var hInputButton = document.createElement('INPUT');
        hInputButton.id = cAutocomplete.CS_BUTTON_PREFIX + this.sInputId;
        hInputButton.type = 'button';
        hInputButton.className = 'buttonDD';
        hInputButton.tabIndex = hInput.tabIndex + 1;
        hInputButton.hAutocomplete = this;
        var hNewInput = document.createElement('INPUT');
        if (this.bAssociative) {
            hNewInput.name = cAutocomplete.CS_INPUT_PREFIX + sInputName;
        }
        else {
            hNewInput.name = sInputName;
        }
        hNewInput.type = 'text';
        hNewInput.value = sValue;
        hNewInput.style.width = nWidth - 22;
        hNewInput.className = cAutocomplete.CS_INPUT_CLASSNAME;
        hNewInput.tabIndex = hInput.tabIndex;
        hNewInput.hAutocomplete = this;
        hInputContainer.appendChild(hNewInput);
        hInputContainer.appendChild(hInputButton);
        hInput.parentNode.replaceChild(hInputContainer, hInput);
        hNewInput.id = this.sInputId;
        hInput = hNewInput;
    }
    if (hInput.attachEvent) {
        hInput.attachEvent('onkeyup', cAutocomplete.onInputKeyUp);
        hInput.attachEvent('onkeyup', cAutocomplete.saveCaretPosition);
        hInput.attachEvent('onkeydown', cAutocomplete.onInputKeyDown);
        hInput.attachEvent('onblur', cAutocomplete.onInputBlur);
        hInput.attachEvent('onclick', cAutocomplete.onInputClick);
        hInput.attachEvent('onfocus', cAutocomplete.onInputFocus);
        if (hInputButton) {
            hInputButton.attachEvent('onclick', cAutocomplete.onButtonClick);
        }
    }
    else if (hInput.addEventListener) {
        hInput.addEventListener('keyup', cAutocomplete.onInputKeyUp, false);
        hInput.addEventListener('keyup', cAutocomplete.saveCaretPosition, false);
        hInput.addEventListener('keydown', cAutocomplete.onInputKeyDown, false);
        hInput.addEventListener('keypress', cAutocomplete.onInputKeyPress, false);
        hInput.addEventListener('blur', cAutocomplete.onInputBlur, false);
        hInput.addEventListener('click', cAutocomplete.onInputClick, false);
        hInput.addEventListener('focus', cAutocomplete.onInputFocus, false);
        if (hInputButton) {
            hInputButton.addEventListener('click', cAutocomplete.onButtonClick, false);
        }
    }
    hInput.setAttribute('autocomplete', 'OFF');
    if (hForm) {
        if (hForm.attachEvent) {
            hForm.attachEvent('onsubmit', cAutocomplete.onFormSubmit);
        }
        else if (hForm.addEventListener) {
            hForm.addEventListener('submit', cAutocomplete.onFormSubmit, false);
        }
    }
};
cAutocomplete.prototype.initListContainer = function() {
    var hInput = document.getElementById(this.sInputId);
    var hContainer = document.createElement('DIV');
    hContainer.className = 'autocomplete_holder';
    hContainer.id = this.sListId;
    hContainer.style.zIndex = 10000 + cAutocomplete.nCount;
    hContainer.hAutocomplete = this;
    var hFirstBorder = document.createElement('DIV');
    hFirstBorder.className = 'autocomplete_firstborder';
    var hSecondBorder = document.createElement('DIV');
    hSecondBorder.className = 'autocomplete_secondborder';
    var hList = document.createElement('UL');
    hList.className = 'autocomplete';
    hSecondBorder.appendChild(hList);
    hFirstBorder.appendChild(hSecondBorder);
    hContainer.appendChild(hFirstBorder);
    document.body.appendChild(hContainer);
    if (hContainer.attachEvent) {
        hContainer.attachEvent('onblur', cAutocomplete.onListBlur);
        hContainer.attachEvent('onfocus', cAutocomplete.onListFocus);
    }
    else if (hInput.addEventListener) {
        hContainer.addEventListener('blur', cAutocomplete.onListBlur, false);
        hContainer.addEventListener('focus', cAutocomplete.onListFocus, false);
    }
    if (hContainer.attachEvent) {
        hContainer.attachEvent('onclick', cAutocomplete.onItemClick);
    }
    else if (hContainer.addEventListener) {
        hContainer.addEventListener('click', cAutocomplete.onItemClick, false);
    }
};
cAutocomplete.prototype.createList = function() {
    this.aSearchData = new Array();
    var hInput = document.getElementById(this.sInputId);
    var hContainer = document.getElementById(this.sListId);
    var hList = hContainer.getElementsByTagName('UL')[0];
    if (hList) {
        hList = hList.parentNode.removeChild(hList);
        while (hList.hasChildNodes()) {
            hList.removeChild(hList.childNodes[0]);
        }
    }
    var hListItem = null;
    var hListItemLink = null;
    var hArrKey = null;
    var sArrEl = null;
    var hArr = this.aData;
    var nI = 0;
    var sRealText;
    for (hArrKey in hArr) {
        sArrEl = hArrKey;
        hListItem = document.createElement('LI');
        hListItemLink = document.createElement('A');
        hListItemLink.setAttribute('itemvalue', hArr[hArrKey]);
        var sArrData = sArrEl.split(cAutocomplete.CS_ARRAY_SEPARATOR);
        if (sArrData.length > 1) {
            this.aData[hArrKey] = sArrData[0];
            hListItemLink.setAttribute('itemdata', sArrEl.substring(sArrEl.indexOf(cAutocomplete.CS_ARRAY_SEPARATOR) + 1));
            sRealText = sArrData[0];
        }
        else {
            sRealText = sArrEl;
        };
        hListItemLink.href = '#';
        hListItemLink.appendChild(document.createTextNode(sRealText));
        hListItemLink.realText = sRealText;
        if (nI == this.nSelectedItemIdx) {
            this.hActiveSelection = hListItemLink;
            this.hActiveSelection.className = 'selected';
        }
        hListItem.appendChild(hListItemLink);
        hList.appendChild(hListItem);
        this.aSearchData[nI++] = sRealText.toLowerCase();
    }
    var hSecondBorder = hContainer.firstChild.firstChild;
    hSecondBorder.appendChild(hList);
    this.bListUpdated = false;
};
cAutocomplete.prototype.initListArray = function() {
    var hInput = document.getElementById(this.sInputId);
    var hArr = null;
    if (hInput.type.toLowerCase() == 'select-one') {
        hArr = new Object();
        for (var nI = 0; nI < hInput.options.length; nI++) {
            hArrKey = hInput.options.item(nI).text;
            sArrEl = hInput.options.item(nI).value;
            hArr[hArrKey] = sArrEl;
            if (hInput.options.item(nI).selected) {
                this.nSelectedItemIdx = nI;
                this.sSelectedItemValue = hInput.options.item(nI).text;
            }
        }
    }
    else {
        var sAA = hInput.getAttribute('autocomplete_list');
        var sAAS = hInput.getAttribute('autocomplete_list_sort');
        var sArrayType = this.getListArrayType();
        switch (sArrayType) {
            case 'array': hArr = eval(sAA.substring(6) + ";");
                break;
            case 'list': hArr = new Array();
                var hTmpArray = sAA.substring(5).split('|');
                var aValueArr;
                for (hKey in hTmpArray) {
                    aValueArr = hTmpArray[hKey].split(cAutocomplete.CS_ARRAY_SEPARATOR);
                    if (aValueArr.length == 1) {
                        hArr[hKey] = hTmpArray[hKey];
                        this.bAssociative = false;
                    }
                    else {
                        hArr[aValueArr[0]] = aValueArr[1];
                    }
                }
                break;
        }
        if (sAAS != null && eval(sAAS + ";")) {
            this.bSorted = true;
            this.aData = hArr.sort();
            hArr = hArr.sort();
        }
    }
    this.setArray(hArr);
};
cAutocomplete.prototype.setArray = function(sArray) {
    if (typeof sArray == 'string') {
        this.aData = eval(sArray + ";");
    }
    else {
        this.aData = sArray;
    }
    this.bListUpdated = true;
};
cAutocomplete.prototype.setListArray = function(sArray) {
    this.setArray(sArray);
    this.updateAndShowList();
};
cAutocomplete.prototype.getListArrayType = function() {
    var hInput = document.getElementById(this.sInputId);
    var sAA = hInput.getAttribute('autocomplete_list');
    if (sAA != null && sAA.length > 0) {
        if (sAA.indexOf('array:') >= 0) {
            return 'array';
        }
        else if (sAA.indexOf('list:') >= 0) {
            return 'list';
        }
        else if (sAA.indexOf('url:') >= 0) {
            return 'url';
        }
    }
};
cAutocomplete.prototype.getListURL = function() {
    var hInput = document.getElementById(this.sInputId);
    var sAA = hInput.getAttribute('autocomplete_list');
    if (sAA != null && sAA.length > 0) {
        if (sAA.indexOf('url:') >= 0) {
            return sAA.substring(4);
        }
    }
};
cAutocomplete.prototype.setListURL = function(sURL) {
    this.sListURL = sURL;
};
cAutocomplete.prototype.onXmlHttpLoad = function() {
    if (this.hXMLHttp.readyState == 4) {
        var hError = this.hXMLHttp.parseError;
        if (hError && hError.errorCode != 0) {
            alert(hError.reason);
        }
        else {
            this.afterRemoteLoad();
        }
    }
};
cAutocomplete.prototype.loadListArray = function() {
    var sURL = this.sListURL;
    if (!cAutocomplete.PartialCities) {
        var sStartWith = this.getStringForAutocompletion(this.sActiveValue, this.nInsertPoint);
        sStartWith = sStartWith.replace(/^\s/, '');
        sStartWith = sStartWith.replace(/\s$/, '');
        if (sURL.indexOf('[S]') >= 0) {
            sURL = sURL.replace('[S]', sStartWith);
        }
        else {
            sURL += this.sActiveValue;
        }
    }
    this.hXMLHttp.open('GET', sURL, true);
    var hAC = this;
    this.hXMLHttp.onreadystatechange = function() {
        hAC.onXmlHttpLoad();
    };
    this.hXMLHttp.send(null);
};
cAutocomplete.prototype.afterRemoteLoad = function() {
    var hInput = document.getElementById(this.sInputId);
    var hArr = new Array();
    if (this.hXMLHttp.responseText.indexOf("DOCTYPE") > -1) {
        //empty list returned
        return;
    }
    var hTmpArray = this.hXMLHttp.responseText.split('#');
    if (cAutocomplete.PartialCities) {
        AllCities = hTmpArray;
        var hiddenInpput = document.getElementById(cAutocomplete.CS_HIDDEN_INPUT_PREFIX + this.sInputId);
        var selectedValue = this.sSelectedItemValue;
        var selectedID = hiddenInpput.value;
        AllCities.sort();
        var options = new Array();
        for (var i = 0; i < AllCities.length; i++) {
            var tmp = AllCities[i];
            var op = new Option(tmp.split(';')[0], tmp.split(';')[1]);
            options[i] = op;
        }

        var hArr = null;
        hArr = new Object();
        for (var nI = 0; nI < options.length; nI++) {
            hArrKey = options[nI].text;
            sArrEl = options[nI].value;
            hArr[hArrKey] = sArrEl;
        }

        this.setArray(hArr);
        this.prepareList();

    } else {
        var aValueArr;
        for (hKey in hTmpArray) {


            aValueArr = hTmpArray[hKey].split(cAutocomplete.CS_ARRAY_SEPARATOR);
            if (aValueArr.length == 1) {
                hArr[hKey] = hTmpArray[hKey];
            }
            else {
                hArr[aValueArr[0]] = hTmpArray[hKey].substr(hTmpArray[hKey].indexOf(cAutocomplete.CS_ARRAY_SEPARATOR) + 1);
            }
        }

        hInput.className = '';
        hInput.readonly = false;
        hInput.value = this.sActiveValue;
        this.setListArray(hArr);
    }
};
cAutocomplete.prototype.prepareList = function(bFullList) {
    var hInput = document.getElementById(this.sInputId);
    if (hInput.value == '') {
        this.clearList();
        return;
    }
    
    this.sActiveValue = hInput.value;
    if (this.bRemoteList) {
        hInput.readonly = true;
    }
    var sST = this.getStringForAutocompletion(this.sActiveValue, this.nInsertPoint);
    var sLST = this.getStringForAutocompletion(this.sLastActiveValue, this.nInsertPoint);
    if (sLST != sST || bFullList || !this.bListDisplayed || this.bMatchSubstring) {
        if (this.bRemoteList) {
            hInput.className = 'search';
            hInput.value = 'please wait...';
            this.loadListArray();
            return;
        }
        this.updateAndShowList(bFullList);
    }
};
cAutocomplete.prototype.updateAndShowList = function(bFullList) {
    var hContainer = document.getElementById(this.sListId);
    var hList = hContainer.getElementsByTagName('UL')[0];
    var hInput = document.getElementById(this.sInputId);
    if (this.bListUpdated) {
        this.createList();
    }
    var sST = this.bMatchSubstring ? this.getStringForAutocompletion(this.sActiveValue, this.nInsertPoint) : this.sActiveValue;
    var sLST = this.bMatchSubstring ? this.getStringForAutocompletion(this.sLastActiveValue, this.nInsertPoint) : this.sLastActiveValue;
    if (sST == sLST) {
        if (!this.bMatchSubstring) {
            bFullList = true;
        }
    }
    this.filterOptions(bFullList);
    if (this.nItemsDisplayed == 0) {
        if (this.bForceCorrect) {
            var aPos = this.getInsertPos(this.sActiveValue, this.nInsertPoint, '');
            cAutocomplete.markInputRange(hInput, this.nLastMatchLength, aPos[0]);
        }
    }
    /*this.sLastActiveValue = this.sActiveValue;    */
    if (this.nItemsDisplayed > 0) {
        if (!bFullList || this.bMatchSubstring) {
            this.deselectOption();
        }
        if (this.bAutoComplete && this.nItemsDisplayed == 1) {
            var sStartWith = this.getStringForAutocompletion(this.sActiveValue, this.nInsertPoint);
            var sItemText = hList.getElementsByTagName('LI')[this.nFirstDisplayed].getElementsByTagName('A')[0].realText;
            if (sStartWith.toLowerCase() == sItemText.toLowerCase()) {
                this.selectOption(hList.getElementsByTagName('LI')[this.nFirstDisplayed].getElementsByTagName('A')[0]);
                this.hideOptions();
                return;
            }
        }
        if (this.bAutoComplete && !bFullList) {
            this.selectOption(hList.getElementsByTagName('LI')[this.nFirstDisplayed].getElementsByTagName('A')[0]);
        }
        this.showList();
    }
    else {
        this.clearList();
    }
};
cAutocomplete.prototype.showList = function() {
    if (cAutocomplete.hListDisplayed) {
        cAutocomplete.hListDisplayed.clearList();
    }
    var hInput = document.getElementById(this.sInputId);
    var nTop = cDomObject.getOffsetParam(hInput, 'offsetTop');
    var nLeft = cDomObject.getOffsetParam(hInput, 'offsetLeft');
    var hContainer = document.getElementById(this.sListId);
    var hList = hContainer.getElementsByTagName('UL')[0];
    if (this.bHasButton) {
        hContainer.style.width = document.getElementById(this.sInputId).parentNode.offsetWidth;
    }
    else {
        hContainer.style.width = document.getElementById(this.sInputId).offsetWidth;
    }
    var nNumLines = (this.nItemsDisplayed < cAutocomplete.CN_NUMBER_OF_LINES) ? this.nItemsDisplayed : cAutocomplete.CN_NUMBER_OF_LINES;
    hList.style.height = nNumLines * cAutocomplete.CN_LINE_HEIGHT + cAutocomplete.CN_HEIGHT_FIX + 'px';
    hContainer.style.top = nTop + hInput.offsetHeight + cAutocomplete.CN_OFFSET_TOP + 'px';
    hContainer.style.left = nLeft + cAutocomplete.CN_OFFSET_LEFT + 'px';
    hContainer.style.display = 'none';
    hContainer.style.visibility = 'visible';
    hContainer.style.display = 'block';
    cAutocomplete.hListDisplayed = this;
    this.bListDisplayed = true;
};
cAutocomplete.prototype.binarySearch = function(sFilter) {
    var nLow = 0;
    var nHigh = this.aSearchData.length - 1;
    var nMid;
    var nTry, nLastTry;
    var sData;
    var nLen = sFilter.length;
    var lastTry;
    while (nLow <= nHigh) {
        nMid = (nLow + nHigh) / 2;
        nTry = (nMid < 1) ? 0 : parseInt(nMid);
        sData = this.aSearchData[nTry].substr(0, nLen);
        if (sData < sFilter) {
            nLow = nTry + 1;
            continue;
        }
        if (sData > sFilter) {
            nHigh = nTry - 1;
            continue;
        }
        if (sData == sFilter) {
            nHigh = nTry - 1;
            nLastTry = nTry;
            continue;
        }
        return nTry;
    }
    if (typeof (nLastTry) != "undefined") {
        return nLastTry;
    }
    else {
        return null;
    }
};
cAutocomplete.prototype.getStringForAutocompletion = function(sString, nPos) {
    if (sString == null || sString.length == 0) {
        return '';
    }
    if (this.bMatchSubstring) {
        var nStartPos = sString.lastIndexOf(cAutocomplete.CS_SEPARATOR, nPos - 1);
        nStartPos = nStartPos < 0 ? 0 : nStartPos;
        var nEndPos = sString.indexOf(cAutocomplete.CS_SEPARATOR, nPos);
        nEndPos = nEndPos < 0 ? sString.length : nEndPos;
        var sStr = sString.substr(nStartPos, nEndPos - nStartPos);
        sStr = sStr.replace(/^(\,?)(\s*)(\S*)(\s*)(\,?)$/g, '$3');
        return sStr;
    }
    else {
        return sString;
    }
};
cAutocomplete.prototype.insertString = function(sString, nPos, sInsert) {
    if (this.bMatchSubstring) {
        var nStartPos = sString.lastIndexOf(cAutocomplete.CS_SEPARATOR, nPos - 1);
        nStartPos = nStartPos < 0 ? 0 : nStartPos;
        var nEndPos = sString.indexOf(cAutocomplete.CS_SEPARATOR, nPos);
        nEndPos = nEndPos < 0 ? sString.length : nEndPos;
        var sStr = sString.substr(nStartPos, nEndPos - nStartPos);
        sStr = sStr.replace(/^(\,?)(\s*)(\S?[\S\s]*\S?)(\s*)(\,?)$/g, '$1$2' + sInsert + '$4$5');
        sStr = sString.substr(0, nStartPos) + sStr + sString.substr(nEndPos);
        return sStr;
    }
    else {
        return sInsert;
    }
};
cAutocomplete.prototype.getInsertPos = function(sString, nPos, sInsert) {
    nPos = nPos == null ? 0 : nPos;
    var nStartPos = sString.lastIndexOf(cAutocomplete.CS_SEPARATOR, nPos - 1);
    nStartPos = nStartPos < 0 ? 0 : nStartPos;
    var nEndPos = sString.indexOf(cAutocomplete.CS_SEPARATOR, nPos);
    nEndPos = nEndPos < 0 ? sString.length : nEndPos;
    var sStr = sString.substr(nStartPos, nEndPos - nStartPos);
    sStr = sStr.replace(/^(\,?)(\s*)(\S?[\S\s]*\S?)(\s*)(\,?)$/g, '$1$2' + sInsert);
    return [nPos, nStartPos + sStr.length];
};
cAutocomplete.prototype.filterOptions = function(bShowAll) {
    if (this.hActiveSelection && !bShowAll) {
        this.hActiveSelection.className = '';
    }
    if (typeof bShowAll == 'undefined') {
        bShowAll = false;
    }
    var hInput = document.getElementById(this.sInputId);
    var sStartWith = this.getStringForAutocompletion(this.sActiveValue, this.nInsertPoint);
    if (bShowAll) {
        sStartWith = '';
    }
    var hContainer = document.getElementById(this.sListId);
    var hList = hContainer.getElementsByTagName('UL')[0];
    var nItemsLength = hList.childNodes.length;
    var hLinkItem = null;
    var nCount = 0;
    var hParent = hList.parentNode;
    var hList = hList.parentNode.removeChild(hList);
    var hTItems = hList.childNodes;
    var CityNameInOtherLang = 0;
    this.nItemsDisplayed = 0;
    if (sStartWith.length == 0) {
        for (var nI = 0; nI < nItemsLength; nI++) {
            if (this.formatOptions) {
                hTItems[nI].childNodes[0].innerHTML = this.formatOptions(hTItems[nI].childNodes[0].realText, nI);
            }
            if (hTItems[nI].firstChild.getAttribute('itemdata') == cAutocomplete.LANGID) {
                hTItems[nI].style.display = 'block';
            } else {
                hTItems[nI].style.display = 'none';
            }

        }
        nCount = nItemsLength;
        if (nItemsLength > 0) {
            this.nFirstDisplayed = 0;
            this.nLastDisplayed = nItemsLength - 1;
        }
        else {
            this.nFirstDisplayed = this.nLastDisplayed = -1;
        }
        var aPos = this.getInsertPos(this.sActiveValue, this.nInsertPoint, sStartWith);
        this.nLastMatchLength = aPos[0];
    }
    else {
        this.nFirstDisplayed = this.nLastDisplayed = -1;
        sStartWith = sStartWith.toLowerCase();
        var bEnd = false;
        if (this.bSorted && this.bMatchBegin) {
            var nStartAt = this.binarySearch(sStartWith);
            for (var nI = 0; nI < nItemsLength; nI++) {
                hTItems[nI].style.display = 'none';
                if (nI >= nStartAt && !bEnd) {
                    if (!bEnd && this.aSearchData[nI].indexOf(sStartWith) != 0) {
                        bEnd = true;
                        continue;
                        /*if (hTItems[nI].firstChild.getAttribute('itemdata') == cAutocomplete.LANGID) {
                        CityNameInOtherLang = hTItems[nI].firstChild.getAttribute('itemdata');
                        bEnd = true;
                        continue;
                        } else {
                        CityNameInOtherLang = hTItems[nI].getAttribute('itemdata');
                        }*/
                    }

                    if ((CityNameInOtherLang == 0 && hTItems[nI].firstChild.getAttribute('itemdata') == cAutocomplete.LANGID) || CityNameInOtherLang > 0) {
                        //CityNameInOtherLang = hTItems[nI].firstChild.getAttribute('itemdata');
                        nCount++;
                        if (this.formatOptions) {
                            hTItems[nI].childNodes[0].innerHTML = this.formatOptions(hTItems[nI].childNodes[0].realText, nI);
                        }
                        hTItems[nI].style.display = 'block';
                        if (this.nFirstDisplayed < 0) {
                            this.nFirstDisplayed = nI;
                        }
                        this.nLastDisplayed = nI;
                    } else {
                        if (CityNameInOtherLang == 0)
                            CityNameInOtherLang = hTItems[nI].firstChild.getAttribute('itemdata');

                    }

                }
            }
            /*region show names in other language*/
            var tmpLid = cAutocomplete.LANGID;
            if (CityNameInOtherLang != cAutocomplete.LANGID && CityNameInOtherLang > 0) {
                var NEWLangID = CityNameInOtherLang;
                cAutocomplete.LANGID = NEWLangID;
                nStartAt = this.binarySearch(sStartWith);
                bEnd = false;
                for (var nI = 0; nI < nItemsLength; nI++) {
                    hTItems[nI].style.display = 'none';
                    if (nI >= nStartAt && !bEnd) {
                        if (!bEnd && this.aSearchData[nI].indexOf(sStartWith) != 0) {
                            if (hTItems[nI].firstChild.getAttribute('itemdata') == NEWLangID) {
                                CityNameInOtherLang = hTItems[nI].firstChild.getAttribute('itemdata');
                                bEnd = true;
                                continue;
                            } else {
                                CityNameInOtherLang = hTItems[nI].firstChild.getAttribute('itemdata');
                            }
                        }
                        //if (hTItems[nI].firstChild.getAttribute('itemdata') == NEWLangID) {
                        if (hTItems[nI].firstChild.getAttribute('itemdata') != NEWLangID && hTItems[nI].firstChild.getAttribute('itemdata') != tmpLid) {
                            nCount++;
                        }
                        if (this.formatOptions) {
                            hTItems[nI].childNodes[0].innerHTML = this.formatOptions(hTItems[nI].childNodes[0].realText, nI);
                        }
                        hTItems[nI].style.display = 'block';
                        if (this.nFirstDisplayed < 0) {
                            this.nFirstDisplayed = nI;
                        }
                        this.nLastDisplayed = nI;
                        // }

                    }
                }

            }
            /*endregion show names in other language*/
        }
        else {
            for (var nI = 0; nI < nItemsLength; nI++) {
                hTItems[nI].style.display = 'none';
                if ((this.bMatchBegin && this.aSearchData[nI].indexOf(sStartWith) == 0) || (!this.bMatchBegin && this.aSearchData[nI].indexOf(sStartWith) >= 0)) {
                    if (this.formatOptions) {
                        hTItems[nI].childNodes[0].innerHTML = this.formatOptions(hTItems[nI].childNodes[0].realText, nI);
                    }
                    hTItems[nI].style.display = 'block';
                    nCount++;
                    if (this.nFirstDisplayed < 0) {
                        this.nFirstDisplayed = nI;
                    }
                    this.nLastDisplayed = nI;
                }
            }
        }
        if (nCount > 0) {
            var aPos = this.getInsertPos(this.sActiveValue, this.nInsertPoint, sStartWith);
            this.nLastMatchLength = aPos[0];
        }
    }
    var tmpCount = 0;
    for (var k = 0; k < hList.length; k++) {
        if (hList[k].style.display = 'block') {
            tmpCount++;
        }
    }
    hParent.appendChild(hList);
    try {
        this.nItemsDisplayed = hList.children.length;
    } catch (exc) {
        this.nItemsDisplayed = hList.childNodes.length;
    }
};
cAutocomplete.prototype.hideOptions = function() {
    var hContainer = document.getElementById(this.sListId);
    hContainer.style.visibility = 'hidden';
    hContainer.style.display = 'none';
    cAutocomplete.hListDisplayed = null;
};
cAutocomplete.prototype.markAutocompletedValue = function() {
    var hInput = document.getElementById(this.sInputId);
    var sValue = this.hActiveSelection.realText;
    if (this.bMatchSubstring) {
        var aPos = this.getInsertPos(this.sLastActiveValue, this.nInsertPoint, sValue);
        var nStartPos = aPos[0];
        var nEndPos = aPos[1];
    }
    else {
        var nStartPos = this.nInsertPoint;
        var nEndPos = sValue.length;
    }
    this.nStartAC = nStartPos;
    this.nEndAC = nEndPos;
    if (this.hMarkRangeTimeout != null) {
        clearTimeout(this.hMarkRangeTimeout);
    }
    this.hMarkRangeTimeout = setTimeout(function() {
        cAutocomplete.markInputRange2(hInput.id);
    }
   , cAutocomplete.CN_MARK_TIMEOUT);
};
cAutocomplete.prototype.selectOptionByIndex = function(nOptionIndex) {
    if (this.bListUpdated) {
        this.createList();
    }
    var hContainer = document.getElementById(this.sListId);
    var hList = hContainer.getElementsByTagName('UL')[0];
    var nItemsLength = hList.childNodes.length;
    if (nOptionIndex >= 0 && nOptionIndex < nItemsLength) {
        this.selectOption(hList.childNodes[nOptionIndex].getElementsByTagName('A')[0]);
    }
};
cAutocomplete.prototype.selectOptionByValue = function(sValue) {
    if (this.bListUpdated) {
        this.createList();
    }
    sValue = sValue.toLowerCase();
    var hContainer = document.getElementById(this.sListId);
    var hList = hContainer.getElementsByTagName('UL')[0];
    var nItemsLength = hList.childNodes.length;
    var nSelectedIndex = -1;
    for (var nI = 0; nI < nItemsLength; nI++) {
        if (this.aSearchData[nI].indexOf(sValue) == 0) {
            nSelectedIndex = nI;
        }
    }
    if (nSelectedIndex >= 0) {
        this.selectOption(hList.childNodes[nSelectedIndex].getElementsByTagName('A')[0]);
    }
};
cAutocomplete.prototype.selectOption = function(hNewOption, FillTextBox) {
    if (hNewOption == null)
        return;
    cAutocomplete.LANGID = hNewOption.getAttribute('itemdata');
    try {
        var LindInput = document.getElementById('slidac');
        LindInput.value = cAutocomplete.LANGID;

    } catch (errInp) { }
    if (this.hActiveSelection) {
        if (this.hActiveSelection == hNewOption) {
            return;
        }
        else {
            this.hActiveSelection.className = '';
        }
    }
    this.hActiveSelection = hNewOption;
    LastActiveElement = this.sSelectedItemValue = hNewOption.realText;
    var hInput = document.getElementById(this.sInputId);
    if (this.hActiveSelection != null) {
        if (this.sHiddenInputId != null) {
            if (this.bMatchSubstring) {
                document.getElementById(this.sHiddenInputId).value = this.hActiveSelection.getAttribute('itemvalue');
            }
            else {
                document.getElementById(this.sHiddenInputId).value = this.hActiveSelection.getAttribute('itemvalue');
            }
        }
        this.hActiveSelection.className = 'selected';
        if (this.bAutoComplete) {
            hInput.value = this.insertString(this.sLastActiveValue, this.nInsertPoint, this.hActiveSelection.realText);
            this.bAutocompleted = true;
            this.markAutocompletedValue();
        }
        else {
            if (FillTextBox == true || FillTextBox == null) {
                var aPos = this.getInsertPos(this.sLastActiveValue, this.nInsertPoint, this.hActiveSelection.realText);
                hInput.value = this.insertString(this.sActiveValue, this.nInsertPoint, this.hActiveSelection.realText);
                cAutocomplete.setInputCaretPosition(hInput, aPos[1]);
            }

        }
        this.sActiveValue = hInput.value;
        if (this.onSelect) {
            this.onSelect();
        }
    }
    else {
        hInput.value = this.sActiveValue;
        cAutocomplete.setInputCaretPosition(hInput, this.nInsertPoint);
    }
};
cAutocomplete.prototype.deselectOption = function() {
    if (this.hActiveSelection != null) {
        this.hActiveSelection.className = '';
        this.hActiveSelection = null;
    }
};
cAutocomplete.prototype.clearList = function() {
    this.hideOptions();
    this.bListDisplayed = false;
};
cAutocomplete.prototype.getPrevDisplayedItem = function(hItem) {
    if (hItem == null) {
        var hContainer = document.getElementById(this.sListId);
        hItem = hContainer.getElementsByTagName('UL')[0].childNodes.item(hContainer.getElementsByTagName('UL')[0].childNodes.length - 1);
    }
    else {
        hItem = getPrevNodeSibling(hItem.parentNode);
    }
    while (hItem != null) {
        if (hItem.style.display == 'block') {
            return hItem;
        }
        hItem = hItem.previousSibling;
    }
    return null;
};
cAutocomplete.prototype.getNextDisplayedItem = function(hItem) {
    if (hItem == null) {
        var hContainer = document.getElementById(this.sListId);
        hItem = hContainer.getElementsByTagName('UL')[0].childNodes.item(0);
    }
    else {
        hItem = getNextNodeSibling(hItem.parentNode);
    }
    while (hItem != null) {
        if (hItem.style.display == 'block') {
            return hItem;
        }
        hItem = hItem.nextSibling;
    }
    return null;
};
cAutocomplete.onInputKeyDown = function(hEvent) {
    if (hEvent == null) {
        hEvent = window.event;
    }
    var hElement = (hEvent.srcElement) ? hEvent.srcElement : hEvent.originalTarget;
    var hAC = hElement.hAutocomplete;
    var hContainer = document.getElementById(hAC.sListId);
    var hInput = document.getElementById(hAC.sInputId);
    var hList = hContainer.getElementsByTagName('UL')[0];
    var hEl = getParentByTagName(hElement, 'A');
    var bItemSelected = false;
    if (hContainer != null && hAC.bListDisplayed) {
        var hLI = null;
        var hLINext = null;
        if ((hEvent.keyCode == 13) || (hEvent.keyCode == 27)) {
            bItemSelected = hEvent.keyCode == 13 ? true : false;
            hAC.clearList();
        }
        if (bItemSelected && !cAutocomplete.CS_TEXTBOXCHANGES) {
            var aPos = hAC.getInsertPos(hAC.sLastActiveValue, hAC.nInsertPoint, hAC.hActiveSelection.realText);
            hInput.value = hAC.insertString(hAC.sActiveValue, hAC.nInsertPoint, hAC.hActiveSelection.realText);
            cAutocomplete.setInputCaretPosition(hInput, aPos[1]);
        }
        if (hEvent.keyCode == 38) {
            hLINext = hAC.getPrevDisplayedItem(hAC.hActiveSelection);
            if (hLINext != null) {
                hAC.selectOption(hLINext.childNodes.item(0), cAutocomplete.CS_TEXTBOXCHANGES);
                if (hAC.nItemsDisplayed > cAutocomplete.CN_NUMBER_OF_LINES) {
                    if (hList.scrollTop < 5 && hLINext.offsetTop > hList.offsetHeight) {
                        hList.scrollTop = hList.scrollHeight - hList.offsetHeight;
                    }
                    if (hLINext.offsetTop - hList.scrollTop < 0) {
                        hList.scrollTop -= hLINext.offsetHeight;
                    }
                }
            }
            else {
                hAC.selectOption(null);
            }
        }
        else if (hEvent.keyCode == 40) {
            hLINext = hAC.getNextDisplayedItem(hAC.hActiveSelection);
            if (hLINext != null) {
                hAC.selectOption(hLINext.childNodes.item(0), cAutocomplete.CS_TEXTBOXCHANGES);
                if (hAC.nItemsDisplayed > cAutocomplete.CN_NUMBER_OF_LINES) {
                    if (hList.scrollTop > 0 && hList.scrollTop > hLINext.offsetTop) {
                        hList.scrollTop = 0;
                    }
                    if (Math.abs(hLINext.offsetTop - hList.scrollTop - hList.offsetHeight) < 5) {
                        hList.scrollTop += hLINext.offsetHeight;
                    }
                }
            }
            else {
                hAC.selectOption(null);
            }
        }
    }
    if (hInput.form) {
        hInput.form.bLocked = true;
    }
    if (hEvent.keyCode == 13 || hEvent.keyCode == 27 || hEvent.keyCode == 38 || hEvent.keyCode == 40) {
        if (hEvent.preventDefault) {
            hEvent.preventDefault();
        }
        hEvent.cancelBubble = true;
        hEvent.returnValue = false;
        return false;
    }
};
cAutocomplete.onInputKeyPress = function(hEvent) {
    if (hEvent.keyCode == 13 || hEvent.keyCode == 38 || hEvent.keyCode == 40) {
        if (hEvent.preventDefault) {
            hEvent.preventDefault();
        }
        hEvent.cancelBubble = true;
        hEvent.returnValue = false;
        return false;
    }
};
cAutocomplete.onInputKeyUp = function(hEvent) {
    if (hEvent == null) {
        hEvent = window.event;
    }
    var hElement = (hEvent.srcElement) ? hEvent.srcElement : hEvent.originalTarget;
    if (hEvent.keyCode!=8 && hElement.value.length != 0 && hElement.value.length < cAutocomplete.CS_MIN_CHAR) {
        return;
    }
    var hAC = hElement.hAutocomplete;
    var hInput = document.getElementById(hAC.sInputId);
    switch (hEvent.keyCode) {
        case 8: if (hAC.bAutoComplete && hAC.bAutocompleted) {
                hAC.bAutocompleted = false;
                return false;
            }
            break;
        case 38: ;
        case 40: if (hAC.bListDisplayed) {
                if (hEvent.preventDefault) {
                    hEvent.preventDefault();
                }
                hEvent.cancelBubble = true;
                hEvent.returnValue = false;
                return false;
            }
            break;
        case 13: ;
        case 32: ;
        case 46: ;
        case 35: ;
        case 36: break;
        default: if (hEvent.keyCode < 48) {
                if (hEvent.preventDefault) {
                    hEvent.preventDefault();
                }
                hEvent.cancelBubble = true;
                hEvent.returnValue = false;
                return false;
            }
            break;
    }
    if (hAC.hMarkRangeTimeout != null) {
        clearTimeout(hAC.hMarkRangeTimeout);
    }
    if (hAC.hShowTimeout) {
        clearTimeout(hAC.hShowTimeout);
        hAC.hShowTimeout = null;
    }
    if (cAutocomplete.PartialCities) {
        if (hElement.value == '') {
            hAC.clearList();

        } else {
            hAC.FillPartial(hElement.value);
        }

    } else {
        var nTimeout = hAC.bRemoteList ? cAutocomplete.CN_REMOTE_SHOW_TIMEOUT : cAutocomplete.CN_SHOW_TIMEOUT;
        hAC.hShowTimeout = setTimeout(function() {
            hAC.prepareList();
        }
   , nTimeout);
    }
};
cAutocomplete.onInputClick = function(hEvent) {
    if (hEvent == null) {
        hEvent = window.event;
    }
    var hElement = (hEvent.srcElement) ? hEvent.srcElement : hEvent.currentTarget;
    hElement.select(); 

};
cAutocomplete.onInputBlur = function(hEvent) {
    if (hEvent == null) {
        hEvent = window.event;
    }
    var hElement = (hEvent.srcElement) ? hEvent.srcElement : hEvent.originalTarget;
    if (hElement.form) {
        hElement.form.bLocked = false;
    }
    if (hElement.hAutocomplete.hActiveSelection != null && hElement.hAutocomplete.hActiveSelection.realText) {
        hElement.value = hElement.hAutocomplete.hActiveSelection.realText;

    } else if (LastActiveElement != null) {
        hElement.value = LastActiveElement;
    }
    if (!hElement.hAutocomplete.hClearTimeout) {
        hElement.hAutocomplete.hClearTimeout = setTimeout(function() {
            hElement.hAutocomplete.clearList();
        }
      , cAutocomplete.CN_CLEAR_TIMEOUT);
    }    
};
cAutocomplete.onInputFocus = function(hEvent) {
    if (hEvent == null) {
        hEvent = window.event;
    }
    var hElement = (hEvent.srcElement) ? hEvent.srcElement : hEvent.originalTarget;    
    var hAC = hElement.hAutocomplete;
    if (hAC.hClearTimeout) {
        clearTimeout(hAC.hClearTimeout);
        hAC.hClearTimeout = null;
    }
};
cAutocomplete.saveCaretPosition = function(hEvent) {
    if (hEvent == null) {
        hEvent = window.event;
    }
    var hElement = (hEvent.srcElement) ? hEvent.srcElement : hEvent.originalTarget;
    var hAC = hElement.hAutocomplete;
    var hInput = document.getElementById(hAC.sInputId);
    if (hEvent.keyCode != 38 && hEvent.keyCode != 40) {
        hAC.nInsertPoint = cAutocomplete.getInputCaretPosition(hInput);
    }
};
cAutocomplete.getInputCaretPosition = function(hInput) {
    if (typeof hInput.selectionStart != 'undefined') {
        if (hInput.selectionStart == hInput.selectionEnd) {
            return hInput.selectionStart;
        }
        else {
            return hInput.selectionStart;
        }
    }
    else if (hInput.createTextRange) {
        var hSelRange = document.selection.createRange();
        if (hInput.tagName.toLowerCase() == 'textarea') {
            var hSelBefore = hSelRange.duplicate();
            var hSelAfter = hSelRange.duplicate();
            hSelRange.moveToElementText(hInput);
            hSelBefore.setEndPoint('StartToStart', hSelRange);
            return hSelBefore.text.length;
        }
        else {
            hSelRange.moveStart('character', -1 * hInput.value.length);
            var nLen = hSelRange.text.length;
            return nLen;
        }
    }
    return null;
};
cAutocomplete.setInputCaretPosition = function(hInput, nPosition) {
    if (hInput.setSelectionRange) {
        hInput.setSelectionRange(nPosition, nPosition);
    }
    else if (hInput.createTextRange) {
        var hRange = hInput.createTextRange();
        hRange.moveStart('character', nPosition);
        hRange.moveEnd('character', nPosition);
        hRange.collapse(true);
        hRange.select();
    }
};
cAutocomplete.markInputRange = function(hInput, nStartPos, nEndPos) {
    if (hInput.setSelectionRange) {
        hInput.focus();
        hInput.setSelectionRange(nStartPos, nEndPos);
    }
    else if (hInput.createTextRange) {
        var hRange = hInput.createTextRange();
        hRange.collapse(true);
        hRange.moveStart('character', nStartPos);
        hRange.moveEnd('character', nEndPos - nStartPos);
        hRange.select();
    }
};
cAutocomplete.markInputRange2 = function(sInputId) {
    var hInput = document.getElementById(sInputId);
    var nStartPos = hInput.hAutocomplete.nStartAC;
    var nEndPos = hInput.hAutocomplete.nEndAC;
    cAutocomplete.markInputRange(hInput, nStartPos, nEndPos);
};
cAutocomplete.onListBlur = function(hEvent) {
    if (hEvent == null) {
        hEvent = window.event;
    }
    var hElement = (hEvent.srcElement) ? hEvent.srcElement : hEvent.originalTarget;
    hElement = getParentByProperty(hElement, 'className', 'autocomplete_holder');
    var hAC = hElement.hAutocomplete;
    if (!hAC.hClearTimeout) {
        hAC.hClearTimeout = setTimeout(function() {
            hAC.clearList();
        }
      , cAutocomplete.CN_CLEAR_TIMEOUT);
    }
};
cAutocomplete.onListFocus = function(hEvent) {
    if (hEvent == null) {
        hEvent = window.event;
    }
    var hElement = (hEvent.srcElement) ? hEvent.srcElement : hEvent.originalTarget;
    hElement = getParentByProperty(hElement, 'className', 'autocomplete_holder');
    var hAC = hElement.hAutocomplete;
    if (hAC.hClearTimeout) {
        clearTimeout(hAC.hClearTimeout);
        hAC.hClearTimeout = null;
    }
};
cAutocomplete.onItemClick = function(hEvent) {
    if (hEvent == null) {
        hEvent = window.event;
    }
    var hElement = (hEvent.srcElement) ? hEvent.srcElement : hEvent.originalTarget;
    var hContainer = getParentByProperty(hElement, 'className', 'autocomplete_holder');
    var hEl = getParentByTagName(hElement, 'A');
    if (hContainer != null) {
        var hAC = hContainer.hAutocomplete;
        hAC.selectOption(hEl);
        document.getElementById(hAC.sInputId).focus();
        hAC.clearList();
    }
    if (hEvent.preventDefault) {
        hEvent.preventDefault();
    }
    hEvent.cancelBubble = true;
    hEvent.returnValue = false;
    return false;
};
cAutocomplete.onButtonClick = function(hEvent) {
    if (hEvent == null) {
        hEvent = window.event;
    }
    var hElement = (hEvent.srcElement) ? hEvent.srcElement : hEvent.originalTarget;
    var hAC = hElement.hAutocomplete;
    var hInput = document.getElementById(hAC.sInputId);
    if (hInput.disabled) {
        return;
    }
    hAC.prepareList(true);
    var hInput = document.getElementById(hAC.sInputId);
    hInput.focus();
};
cAutocomplete.onFormSubmit = function(hEvent) {
    if (hEvent == null) {
        hEvent = window.event;
    }
    
    
    var hElement = (hEvent.srcElement) ? hEvent.srcElement : hEvent.originalTarget;
    if (hElement.bLocked) {
        hElement.bLocked = false;
        hEvent.returnValue = false;
        if (hEvent.preventDefault) {
            hEvent.preventDefault();
        }
        return false;
    }
};
function cDomObject(sId) {
    if (bw.dom || bw.ie) {
        this.hElement = document.getElementById(sId);
        this.hStyle = this.hElement.style;
    }
}
cDomObject.prototype.getWidth = function() {
    return cDomObject.getWidth(this.hElement);
};
cDomObject.getWidth = function(hElement) {
    if (hElement.currentStyle) {
        var nWidth = parseInt(hElement.currentStyle.width);
        if (isNaN(nWidth)) {
            return parseInt(hElement.offsetWidth);
        }
        else {
            return nWidth;
        }
    }
    else {
        return parseInt(hElement.offsetWidth);
    }
};
cDomObject.prototype.getHeight = function() {
    return cDomObject.getHeight(this.hElement);
};
cDomObject.getHeight = function(hElement) {
    if (hElement.currentStyle) {
        var nHeight = parseInt(hElement.currentStyle.height);
        if (isNaN(nHeight)) {
            return parseInt(hElement.offsetHeight);
        }
        else {
            return nHeight;
        }
    }
    else {
        return parseInt(hElement.offsetHeight);
    }
};
cDomObject.prototype.getLeft = function() {
    return cDomObject.getLeft(this.hElement);
};
cDomObject.getLeft = function(hElement) {
    return parseInt(hElement.offsetLeft);
};
cDomObject.prototype.getTop = function() {
    return cDomObject.getTop(this.hElement);
};
cDomObject.getTop = function(hElement) {
    return parseInt(hElement.offsetTop);
};
cDomObject.getOffsetParam = function(hElement, sParam, hLimitParent) {
    var nRes = 0;
    if (hLimitParent == null) {
        hLimitParent = document.body.parentElement;
    }
    while (hElement != hLimitParent) {
        nRes += eval('hElement.' + sParam + ";");
        if (!hElement.offsetParent) {
            break;
        }
        hElement = hElement.offsetParent;
    }
    return nRes;
};
cDomObject.getScrollOffset = function(hElement, sParam, hLimitParent) {
    nRes = 0;
    if (hLimitParent == null) {
        hLimitParent = document.body.parentElement;
    }
    while (hElement != hLimitParent) {
        nRes += eval('hElement.scroll' + sParam + ";");
        if (!hElement.offsetParent) {
            break;
        }
        hElement = hElement.parentNode;
    }
    return nRes;
};
if (document.ELEMENT_NODE == null) {
    document.ELEMENT_NODE = 1;
    document.TEXT_NODE = 3;
}
function getSubNodeByName(hNode, sNodeName) {
    if (hNode != null) {
        var nNc = 0;
        var nC = 0;
        var hNodeChildren = hNode.childNodes;
        var hCNode = null;
        while (nC < hNodeChildren.length) {
            hCNode = hNodeChildren.item(nC++);
            if ((hCNode.nodeType == 1) && (hCNode.nodeName.toLowerCase() == sNodeName)) {
                return hCNode;
            }
        }
    }
    return null;
}
function getPrevNodeSibling(hNode) {
    if (hNode != null) {
        do {
            hNode = hNode.previousSibling;
        }
        while (hNode != null && hNode.nodeType != 1);
        return hNode;
    }
}
function getNextNodeSibling(hNode) {
    if (hNode != null) {
        do {
            hNode = hNode.nextSibling;
        }
        while (hNode != null && hNode.nodeType != 1);
        return hNode;
    }
}
function getLastSubNodeByName(hNode, sNodeName) {
    if (hNode != null) {
        var nNc = 0;
        var nC = 0;
        var hNodeChildren = hNode.childNodes;
        var hCNode = null;
        var nLength = hNodeChildren.length - 1;
        while (nLength >= 0) {
            hCNode = hNodeChildren.item(nLength);
            if ((hCNode.nodeType == 1) && (hCNode.nodeName.toLowerCase() == sNodeName)) {
                return hCNode;
            }
            nLength--;
        }
    }
    return null;
}
function getSubNodeByProperty(hNode, sProperty, sPropValue) {
    if (hNode != null) {
        var nNc = 0;
        var nC = 0;
        var hNodeChildren = hNode.childNodes;
        var hCNode = null;
        var sAttribute;
        var hProp;
        sPropValue = sPropValue.toLowerCase();
        while (nC < hNodeChildren.length) {
            hCNode = hNodeChildren.item(nC++);
            if (hCNode.nodeType == document.ELEMENT_NODE) {
                hProp = eval('hCNode.' + sProperty + ";");
                if (typeof (sPropValue) != 'undefined') {
                    if (hProp.toLowerCase() == sPropValue) {
                        return hCNode;
                    }
                }
                else {
                    return hCNode;
                }
            }
            nNc++;
        }
    }
    return null;
}
function findAttribute(hNode, sAtt) {
    sAtt = sAtt.toLowerCase();
    for (var nI = 0; nI < hNode.attributes.length; nI++) {
        if (hNode.attributes.item(nI).nodeName.toLowerCase() == sAtt) {
            return hNode.attributes.item(nI).nodeValue;
        }
    }
    return null;
}
function getSubNodeByAttribute(hNode, sAtt, sAttValue) {
    if (hNode != null) {
        var nNc = 0;
        var nC = 0;
        var hNodeChildren = hNode.childNodes;
        var hCNode = null;
        var sAttribute;
        sAttValue = sAttValue.toLowerCase();
        while (nC < hNodeChildren.length) {
            hCNode = hNodeChildren.item(nC++);
            if (hCNode.nodeType == document.ELEMENT_NODE) {
                sAttribute = hCNode.getAttribute(sAtt);
                if (sAttribute && sAttribute.toLowerCase() == sAttValue);
                return hCNode;
            }
            nNc++;
        }
    }
    return null;
}
function getLastSubNodeByAttribute(hNode, sAtt, sAttValue) {
    if (hNode != null) {
        var nNc = 0;
        var nC = 0;
        var hNodeChildren = hNode.childNodes;
        var hCNode = null;
        var nLength = hNodeChildren.length - 1;
        while (nLength >= 0) {
            hCNode = hNodeChildren.item(nLength);
            if (hCNode.nodeType == document.ELEMENT_NODE) {
                sAttribute = hCNode.getAttribute(sAtt);
                if (sAttribute && sAttribute.toLowerCase() == sAttValue);
                return hCNode;
            }
            nLength--;
        }
    }
    return null;
}
function getParentByTagName(hNode, sParentTagName) {
    while ((hNode.tagName) && !(/(body|html)/i.test(hNode.tagName))) {
        if (hNode.tagName == sParentTagName) {
            return hNode;
        }
        hNode = hNode.parentNode;
    }
    return null;
}
function getParentByAttribute(hNode, sAtt, sAttValue) {
    while ((hNode.tagName) && !(/(body|html)/i.test(hNode.tagName))) {
        var sAttr = hNode.getAttribute(sAtt);
        if (sAttr != null && sAttr.toString().length > 0) {
            if (sAttValue !== null) {
                if (sAttr == sAttValue) {
                    return hNode;
                }
            }
            else {
                return hNode;
            }
        }
        hNode = hNode.parentNode;
    }
    return null;
}
function getParentByProperty(hNode, sProperty, sPropValue) {
    while ((hNode.tagName) && !(/(body|html)/i.test(hNode.tagName))) {
        var hProp = eval('hNode.' + sProperty + ";");
        if (hProp != null && hProp.toString().length > 0) {
            if (sPropValue !== null) {
                if (hProp == sPropValue) {
                    return hNode;
                }
            }
            else {
                return hNode;
            }
        }
        hNode = hNode.parentNode;
    }
    return null;
}
function getNodeText(hNode) {
    if (hNode == null) {
        return '';
    }
    var sRes;
    if (hNode.hasChildNodes()) {
        sRes = hNode.childNodes.item(0).nodeValue;
    }
    else {
        sRes = hNode.text;
    }
    return sRes;
}
// used to find the Automation server name
function getDomDocumentPrefix() {
    if (getDomDocumentPrefix.prefix)
        return getDomDocumentPrefix.prefix;

    var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
    var o;
    for (var i = 0; i < prefixes.length; i++) {
        try {
            // try to create the objects
            o = new ActiveXObject(prefixes[i] + ".DomDocument");
            return getDomDocumentPrefix.prefix = prefixes[i];
        }
        catch (ex) { };
    }

    throw new Error("Could not find an installed XML parser");
}

function getXmlHttpPrefix() {
    if (getXmlHttpPrefix.prefix)
        return getXmlHttpPrefix.prefix;

    var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
    var o;
    for (var i = 0; i < prefixes.length; i++) {
        try {
            // try to create the objects
            o = new ActiveXObject(prefixes[i] + ".XmlHttp");
            return getXmlHttpPrefix.prefix = prefixes[i];
        }
        catch (ex) { };
    }

    throw new Error("Could not find an installed XML parser");
}


//////////////////////////


// XmlHttp factory
function XmlHttp() { }

XmlHttp.create = function() {
    try {
        if (window.XMLHttpRequest) {
            var req = new XMLHttpRequest();

            // some versions of Moz do not support the readyState property
            // and the onreadystate event so we patch it!
            if (req.readyState == null) {
                req.readyState = 1;
                req.addEventListener("load", function() {
                    req.readyState = 4;
                    if (typeof req.onreadystatechange == "function")
                        req.onreadystatechange();
                }, false);
            }

            return req;
        }
        if (window.ActiveXObject) {
            return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
        }
    }
    catch (ex) { }
    // fell through
    throw new Error("Your browser does not support XmlHttp objects");
};

// XmlDocument factory
function XmlDocument() { }

XmlDocument.create = function() {
    try {
        // DOM2
        if (document.implementation && document.implementation.createDocument) {
            var doc = document.implementation.createDocument("", "", null);

            // some versions of Moz do not support the readyState property
            // and the onreadystate event so we patch it!
            if (doc.readyState == null) {
                doc.readyState = 1;
                doc.addEventListener("load", function() {
                    doc.readyState = 4;
                    if (typeof doc.onreadystatechange == "function")
                        doc.onreadystatechange();
                }, false);
            }

            return doc;
        }
        if (window.ActiveXObject)
            return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument");
    }
    catch (ex) { }
    throw new Error("Your browser does not support XmlDocument objects");
};

// Create the loadXML method and xml getter for Mozilla
if (window.DOMParser &&
	window.XMLSerializer &&
	window.Node && Node.prototype && Node.prototype.__defineGetter__) {

    // XMLDocument did not extend the Document interface in some versions
    // of Mozilla. Extend both!
    //XMLDocument.prototype.loadXML = 
    Document.prototype.loadXML = function(s) {

        // parse the string to a new doc	
        var doc2 = (new DOMParser()).parseFromString(s, "text/xml");

        // remove all initial children
        while (this.hasChildNodes())
            this.removeChild(this.lastChild);

        // insert and import nodes
        for (var i = 0; i < doc2.childNodes.length; i++) {
            this.appendChild(this.importNode(doc2.childNodes[i], true));
        }
    };


    Document.prototype.__defineGetter__("xml", function() {
        return (new XMLSerializer()).serializeToString(this);
    });
}