
// 采用控件替换的办法，解决select控件不能输入的问题。 
// IE 5.5才支持onfocusout事件！ 
// 使用class selecteditctrl 控制替换select的input text控件的版式 
// 如果该select为可编辑的，并且值不在选项中，则使用title="xxxxx"来赋值 

if (!SelDropdownPicturePath) {
  var SelDropdownPicturePath = "../images/";
}

var oPopup = window.createPopup();
var WBSelectArray = new Array();

function WBSelect(innerhtml,xl,yl,edit,select)
{
  this.innerhtml = innerhtml;
  this.xl = xl;
  this.yl = yl;
  this.edit = edit;
  this.select = select;
}

// 弹出下拉框 
function DropDown(index) {
  with (WBSelectArray[index]) {
    oPopup.document.body.innerHTML = innerhtml; 
    oPopup.show(0, edit.offsetHeight, xl, yl,edit);
    
    // 截断过长的，以hint形式显示 
    var obj = oPopup.document.body.children(0).children(0);
    while (obj) {
      if (obj.offsetWidth > xl) {
        obj.style.textOverflow = "ellipsis";
        obj.style.overflow = "hidden";
        obj.title = obj.innerText;
        obj.style.pixelWidth = xl - 18;
      }
      obj = obj.nextSibling;
    }
  }
}

// 选中下拉框中的选项 
function DoSelect(i,ii)
{
  oPopup.hide();
  with (WBSelectArray[ii]) {
    edit.value = select.options(i).text;
    edit.select();
    edit.focus();
    if (select.selectedIndex != i) {
      select.selectedIndex = i;
      select.fireEvent("onchange");
    }
  }
}

// submit时取值 
function DoSubmit()
{
  var frm = window.event.srcElement;
  var obj,sel,edit;
  var oOption;
  for (var j=0;j<frm.elements.length;j++) {
    obj = frm.elements(j);
    if ((obj.tagName=="SELECT")&&(obj.className=="selectedit")) {
      edit = frm.elements("E_"+obj.id);
      if (obj.selectedIndex>=0) {
        oOption = obj.options(obj.selectedIndex);
      } else { // 无选项 
        var oOption = document.createElement("OPTION");
        oOption.selected = true;
        obj.options.add(oOption);
      }
      if (edit.value != oOption.text)
        oOption.value = "^"+edit.value;
        
      // 控件如果没有指定Name，不会被Post 
//      edit.removeNode();
//      j--; 
    }
  }
}

// 替换原Select控件 
function ReplaceSelect(select) {
  select.style.display="none";

  var parent=select.parentElement;
  
  var container=document.createElement("span");
  var containerPixelWidth=select.style.pixelWidth; //为了防止打印时（没有打印箭头图片） DIV内出现折行，这里就不设其样式的宽度，其后引用此值时改引用新设变量 
  if (containerPixelWidth==0) containerPixelWidth=select.clientWidth;//containerPixelWidth“FinanceCalculator\script”目录下有同一文件，由于打印不需要没有修改   谢仕文 2003-05-07修改
  with (container.style) {
    //pixelWidth = select.style.pixelWidth;
    //if (pixelWidth==0) pixelWidth=select.clientWidth;
    pixelLeft = select.style.pixelLeft;
    pixelTop = select.style.pixelTop;
    position = select.style.position;
  }
  parent.insertBefore(container,select);
  
  // 创建edit控件 
  var edit=document.createElement("input");
  edit.style.pixelWidth = containerPixelWidth - 16;
  edit.className = "selecteditctrl";
  edit.tabIndex=select.tabIndex;
  if (select.selectedIndex>=0)
    edit.value = select.options(select.selectedIndex).text;
  if (select.className!="selectedit")
    edit.readOnly = true;
  else if (select.title!="") edit.value = select.title;
    
  edit.disabled=select.disabled;  
  edit.id = "E_"+select.id;//penggang 2002-7-3
  container.appendChild(edit);

  // 创建下拉框
  var ColorOver="#97D6DB";
  var ColorOut="#DFF0F4";
  var len=select.options.length;
  var ii=WBSelectArray.length;

  var pixelheight = 20;     // 下拉框高度 
  var overflowy="hidden";

  if (len!=0) {
    if (len<=6) pixelheight = len * 20 + 2;
    else {
      pixelheight = 6 * 20 + 2;
      overflowy="";
    }
  }

  // 下拉框宽度 
  var xl = select.getAttribute('DropDownWidth');
  if (xl == null) xl = containerPixelWidth;
  else xl = Math.max(parseInt(xl),container.style.pixelWidth);

  var html='<DIV ' +
    'style="position:absolute;font-family:宋体; font-size:9pt;'+
    'overflow:scroll; overflow-x:hidden; scrollbar-base-color:#5598B9; '+
    'width:' + xl + ';height:' + pixelheight + ';overflow-y:' + overflowy + ';' +
    'color:#000080; border:1px solid #1B5977; cursor:hand; '+
    'SCROLLBAR-HIGHLIGHT-COLOR: #B4D9EC; SCROLLBAR-ARROW-COLOR: white">';
  // 创建下拉框的内容 
  for (var i=0;i<len;i++) {
    html += '<DIV onmouseover="this.style.background='
          + "'" + ColorOver + "'" + '" '
          + 'onmouseout="this.style.background='
          + "'" + ColorOut + "'" + '" '
          + 'NOWRAP STYLE="height:20px; background:' + ColorOut 
          + '; border-bottom:1px solid #1B5977; padding-top:4px; padding-left:2px" '
          + 'onclick="javascript:parent.DoSelect(' + i + ',' + ii + ');">'
          + select.options(i).text + '</DIV>';
  }
  html += '</DIV>';

  WBSelectArray[ii] = new WBSelect(html,xl,pixelheight,edit,select);

  // 创建下拉按钮 
  var down=document.createElement(
    '<IMG SRC="'+SelDropdownPicturePath+'SelDropdown.gif" border=0 ' +
    'style="margin-bottom:1px;vertical-align:bottom;cursor:hand" onclick="DropDown(' + ii + ')">');
    
  down.disabled=select.disabled;  
  down.id = "D_"+select.id;    
  container.appendChild(down);
}

function onloadinit() 
{
  var obj,frm,haveSelect;
  for (var i=0;i<document.forms.length;i++) {
    frm = document.forms(i);
    haveSelect = false;
    for (var j=0;j<frm.elements.length;j++) {
      obj = frm.elements(j);
      if ((obj.tagName=="SELECT")&&
//          ((obj.className=="selectonly")||(obj.className=="selectedit"))&&
          (obj.size==0)) {
        ReplaceSelect(obj);
        j++;  // 插入了一个Text 
        haveSelect = true;
      }
    }
    if (haveSelect)
      frm.attachEvent("onsubmit",DoSubmit);
  }
}
//清除替换样式 by aiai for test
function ClearReplace(ctl)
{
	while(ctl.tagName != "SPAN")
	{
		ctl = ctl.parentNode;
	}
	ctl.removeNode(true);
}

//实现disable
function SetDropDisabled(ctl)
{
	document.all('E_'+ctl.id).disabled = true;
	document.all('D_'+ctl.id).disabled = true;
}
//实现enable
function SetDropEnabled(ctl)
{
	document.all('E_'+ctl.id).disabled = false;
	document.all('D_'+ctl.id).disabled = false;
}
window.attachEvent("onload",onloadinit);
