var d, kw, oResult, oInput, currentRow,oldValue = '';
function $(id) {
    return document.getElementById(id);
}
function getOffset(obj) {
    var x = 0, y = 0;
    do{
        x += obj.offsetLeft;
        y += obj.offsetTop;

        obj = obj.offsetParent;
    } while (obj);
    return {x:x, y:y};
}
function initPosition() {
    oResult = $("divResult");
    oInput = $("b_destination");

    var point = getOffset(oInput);
    oResult.style.left = point.x + "px";
    oResult.style.top = point.y + oInput.offsetHeight + "px";
    oResult.style.width = oInput.offsetWidth - 3 + "px";
}
function hideResult() {
    oResult.style.display = "none";
}
function mover(row) {
    if (currentRow > 0) {
        var oRow = $("tbResult").rows[currentRow - 1];
        mout(oRow);
    }
    row.style.backgroundColor = "#3366CC";
    row.cells[0].style.color = "#ffffff";
}
function mout(row) {
    row.style.backgroundColor = "#ffffff";
    row.cells[0].style.color = "#111111";
}
function mclick(row) {
    // oInput.value = row.cells[0].innerHTML;
    oInput.value = row.all['k1'].value;
    oldValue = oInput.value;
    hideResult();
}
function moveRow(dir) {
    if (currentRow > 0) {
        var oRow = $("tbResult").rows[currentRow - 1];
        mout(oRow);
    }
    if (dir == "UP") {
        currentRow--;
        if (currentRow < 0) {
            currentRow = d.data.length;
        }
    } else {
        currentRow++;
        if (currentRow > d.data.length) {
            currentRow = 0;
        }
    }
    if (currentRow == 0) {
        oInput.value = kw;
    } else {
        var oRow = $("tbResult").rows[currentRow - 1];
        mover(oRow);

        oInput.value = oRow.cells[0].innerHTML;
    }
}
function SearchBoxSearch() {
    initPosition();
    var val = $("b_destination").value;
    if (val != oldValue) {
        oldValue = val;
        oResult = $("divResult");
        if (val == "") {
            hideResult();
            return;
        }
        oResult.innerHTML = "<div style='font-size: 12px;'>Load....</div>";
        oResult.style.display = "";
        var url = "/ajax/LoadCity.jsp?s=" + val;
        var ShowName = function(obj) {
            oResult.innerHTML = obj.responseText;
            var objdiv = oResult;
            var w = objdiv.offsetWidth;
            var h = objdiv.offsetHeight;
            var ifrm = document.createElement('iframe');
            ifrm.src = 'javascript:false';
            ifrm.style.cssText = 'position:absolute; visibility:inherit; top:0px; left:0px; width:' + w + 'px; height:' + h + 'px; z-index:-1; filter: \'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)\'';
            objdiv.appendChild(ifrm);
        }
        AjaxFunction.doAction(url, "", ShowName);
    }
}
window.onload = function() {
    initPosition();
}
