﻿//<asp:TextBox ID="tb_ddl_Nature" runat="server"  onfocus="SetInputField('Nature')" onblur="SetInputFieldOff('Nature')" Width="329px"></asp:TextBox>
//<asp:TextBox ID="tb_ddl_Nature_Id" runat="server" style="display:none" Text="0"></asp:TextBox>
//                
//<div id="Tb_Ddl_Content" onmouseover="_isOverList=true" onmouseout="_isOverList=false"></div>


// Gestion de l'autocomplétion dans un div (non modifiables)

var _inputField = null;
var _oldInputFieldValue = "@";
var _currentInputFieldValue = "";
var _partialId = ""
var _resultCache = new Array;
var _resultCacheValue = new Array;
var _isOverList = false
var _eventKeycode = null;
var _localPrefix = "ctl00_Content_" // à modifier
var _localPrefixURL = "../inc/async/Tb_Ddl_List.aspx" // à modifier
var AC_AsyncXmlContent = null

function SetInputField(partialId, obj) {
    _inputField = obj
    _resultCache = new Array
    _resultCacheValue = new Array
    oldInputFieldValue = "@"
    _partialId = partialId
    _inputField.onkeyup = onKeyUpHandler;
    checkTbDdlValue()

    //var ScrollH = document.getElementById("ScrollContainer").scrollTop   
    document.getElementById("Tb_Ddl_Content").style.top = (calculateOffset(_inputField, "offsetTop") + 15) + "px"
    document.getElementById("Tb_Ddl_Content").style.left = (calculateOffset(_inputField, "offsetLeft")) + "px"



    document.getElementById("Tb_Ddl_Content").style.display = "block"
    document.getElementById("Tb_Ddl_Content").style.width = (Number(_inputField.style.width.replace("px", "")) + 2) + "px"
    //alert((Number(_inputField.style.width.replace("px","")) +2) + "px")
}


function calculateOffset(r, attr) {
    var kb = 0;
    while (r) {
        kb += r[attr];
        r = r.offsetParent
    }
    return kb
}

function SetInputFieldOff(partialId) {
    if (!_isOverList) {
        document.getElementById("Tb_Ddl_Content").style.display = "none";
        VerifyItem();
        _inputField.onkeyup = null;
        _inputField = null;
    }
}
var onKeyUpHandler = function (event) {
    if (_inputField == null)
    { return false }
    checkTbDdlValue()
}


function checkTbDdlValue() {
    if (_inputField == null)
    { return false }
    _currentInputFieldValue = _inputField.value;
    var tmpInputValue = _inputField.value;
    if (document.getElementById(_localPrefix + "tb_ddl_" + _partialId + "_Id").value != "0")
    { tmpInputValue = "" }
    getAsyncXmlDocument(_localPrefixURL + "?Type=" + _partialId + "&value=" + tmpInputValue, "buildCache()")
    //window.open(_localPrefixURL + "?Type=" + _partialId + "&value=" + tmpInputValue, "_blank")
}

function buildCache() {//document.getElementById("debug").innerHTML +=curUpdatedXmlDocument.childNodes.length + "<br>"
    if (curUpdatedXmlDocument.childNodes.length != 0) {
        _resultCache = new Array
        _resultCacheValue = new Array
        for (i = 0; i < curUpdatedXmlDocument.childNodes.length; i++) {
            var oNode = curUpdatedXmlDocument.childNodes[i];
            _resultCache.push(oNode.attributes[1].value)
            _resultCacheValue.push(oNode.attributes[0].value)
        }
        ShowResults()
        _oldInputFieldValue = _currentInputFieldValue;
    }
    else {
        _currentInputFieldValue = _oldInputFieldValue
        _inputField.value = _currentInputFieldValue
    }
}


function ShowResults() {
    document.getElementById("Tb_Ddl_Content").style.display = "block"
    document.getElementById("Tb_Ddl_Content").innerHTML = ""
    var a = document.createElement("a")
    a.className = "Tb_Ddl_Item"
    a.href = "javascript:SetItem(0,'')"
    a.tag = 0;
    a.innerHTML = "&nbsp;"
    document.getElementById("Tb_Ddl_Content").appendChild(a)
    var selId = document.getElementById(_localPrefix + "tb_ddl_" + _partialId + "_Id").value

    for (var i = 0; i < _resultCache.length; ++i) {
        if (_resultCache[i] != "") {
            var d = document.createElement("a")
            d.className = "Tb_Ddl_Item"
            d.tag = _resultCacheValue[i];
            d.href = "javascript:SetItem(" + _resultCacheValue[i] + ",\"" + _resultCache[i] + "\")"
            if (_resultCacheValue[i] == selId) {
                d.className = "Tb_Ddl_ItemHover"
            }
            d.innerHTML = _resultCache[i]
            document.getElementById("Tb_Ddl_Content").appendChild(d)
        }

    }
}

function SetItem(cId, cValue) {
    _inputField.value = cValue
    document.getElementById("Tb_Ddl_Content").style.display = "none"
    _oldInputFieldValue = "@"
    document.getElementById(_localPrefix + "tb_ddl_" + _partialId + "_Id").value = cId

    if (_inputField.id.indexOf("Nature") != -1)
    { CheckNature(cId) }
}

function VerifyItem() {
    var value = String(_inputField.value).toLowerCase
    for (var i = 0; i < document.getElementById("Tb_Ddl_Content").childNodes.length; ++i) {
        if (value == String(document.getElementById("Tb_Ddl_Content").childNodes[i].innerHTML).toLowerCase) {
            document.getElementById(_localPrefix + "tb_ddl_" + _partialId + "_Id").value = document.getElementById("Tb_Ddl_Content").childNodes[i].tag
            _inputField.value = document.getElementById("Tb_Ddl_Content").childNodes[i].innerHTML;
        }
    }
}
    