/*
    功能用途：实现可编辑的下拉选择框
    使用方法：

        1. 在页面的 head 中包含本程序
            <script language="JavaScript" src="/js/combobox.js"></script>

        2. 在页面的 body 中定义用于做下拉选择框的选择器 SELECT
            <select id="theSelector" style="position:absolute;visibility:hidden;" tabIndex="-1">
              <option>selection 1</option>
              <option>selection 2</option>
              <option>selection 3</option>
            </select>

        3. 在页面的 body 中定义用于普通编辑的文本框 INPUT，并通过 onfocus 将其与选择器相关联
            <INPUT name="theName" value="some text" onfocus="_cb_ComeHere('theSelector')">
*/

var selectObj;

function _cb_ComeHere(sName) {
    var el = window.event.srcElement;
    var combo = document.all(sName);
    combo.style.clip = "rect(auto auto auto " + (el.clientWidth+2) + ")";
    combo.style.posLeft = _cb_findLeftOffset(el);
    combo.style.posTop = _cb_findTopOffset(el);
    combo.style.posWidth = el.clientWidth + 20;
    combo.style.posHeight = el.clientHeight;
    combo.style.visibility = "visible";
    combo.setAttribute("_cb_tid", el.uniqueID);
    combo.onchange = _cb_onSelectorChange;
    combo.on
    selectObj=combo;
}

function _cb_onSelectorChange() {
    var combo = window.event.srcElement;
    var idx = combo.selectedIndex;
    if (idx != null && idx >= 0) {
        document.all(combo.getAttribute("_cb_tid")).value = combo.options[idx].text;
    }

    selectObj.style.visibility = "hidden";
}

function _cb_findLeftOffset(el) {
	var left = 0;
	while (el.tagName != "BODY") {
		left += el.offsetLeft;
		el = el.offsetParent;
	}
	return left;
}
function _cb_findTopOffset(el) {
	var top = 0;
	while (el.tagName != "BODY") {
		top += el.offsetTop;
		el = el.offsetParent;
	}
	return top;
}
