var COLOR_MOUSEON="#eeeeee";
var COLOR_MOUSEOFF="#eeebbb";
var SHOW_IF_ONLY_ME=true;
var SRC_TEXT=null;
var ACCOUNT_RANGE="";

//找到当前对象所在的行对象
function getTR() {
    var obj=event.srcElement;
    while (obj.tagName != "TR") {
        obj = obj.parentElement;
    }
    return obj;
}

function fMouseOver(){
    var el=getTR();
    el.style.background=COLOR_MOUSEON;
}

function fMouseOut(el){
    var el=getTR();
    el.style.background=COLOR_MOUSEOFF;
}

//隐藏列表框
function HideSelect(){
  ACCOUNT_LIST.style.visibility = "hidden";
}


//得到某个控件在页面上的坐标数组
function GetXY(aTag){
  var oTmp = aTag;
  var pt = new aPoint(0,0);
  do {
  	pt.x += oTmp.offsetLeft;
  	pt.y += oTmp.offsetTop;
  	oTmp = oTmp.offsetParent;
  } while(oTmp.tagName!="BODY");
  return pt;
}

//定义坐标对象
function aPoint(iX, iY){
	this.x = iX;
	this.y = iY;
}


//为了拴牢当前下拉列表框，将页面原有的select框隐藏
function ToggleTags(){
  with (document.all.tags("SELECT")){
 	for (i=0; i<length; i++)
 		if (TagInBound(item(i))){
 			item(i).style.visibility = "hidden";
 			SeleTag[SeleTag.length] = item(i);
 		}
  }
}

function TagInBound(aTag){
  with (ACCOUNT_LIST.style){
  	var l = parseInt(left);
  	var t = parseInt(top);
  	var r = l+parseInt(width);
  	var b = t+parseInt(height);
	var ptLT = GetXY(aTag);
	return !((ptLT.x>r)||(ptLT.x+aTag.offsetWidth<l)||(ptLT.y>b)||(ptLT.y+aTag.offsetHeight<t));
  }
}

//##############################################################################################

//弹出列表框
function onPopDivClick(aRange){
  ACCOUNT_RANGE=aRange;
  
  onKeyUpEvent();
  
  SRC_TEXT=event.srcElement;
  
  event.cancelBubble=true;
  var SelectObj=event.srcElement;
  var point = GetXY(SelectObj);
  with (ACCOUNT_LIST.style) {
  		left = point.x-13;
  		top  = point.y+SelectObj.offsetHeight+2;
  		width = ACCOUNT_LIST.offsetWidth;
  		height = ACCOUNT_LIST.offsetHeight;
  		ToggleTags();
  		visibility = 'visible';
  }
  ACCOUNT_LIST.scrollTop=0;
}

//键盘按下
function onKeyDownEvent() {
    switch (event.keyCode) {
      case 13: // Enter
        HideSelect();
        break;
      case 8:  // Backspace
      case 9:  // Tab
      case 35: // End
      case 36: // Home
      case 37: // Left Arrow
      case 39: // Right Arrow
      case 46: // Del
      return true;
    }

    // 只处理 '0'～'9'，其它键忽略掉
    event.returnValue = false;
    if (!(event.keyCode >= 48 && event.keyCode <= 57 ) && !( event.keyCode>=96 && event.keyCode<=105 )) {
        return false;
    }
    event.returnValue = true;
}

//键盘弹起
function onKeyUpEvent(){
    if (event.keyCode==13 || event.keyCode==9) return;
    var account_code=event.srcElement.value;
    SHOW_IF_ONLY_ME=true;
    refresh(account_code);
    
}

//设置科目列表中应该显示的科目
function refresh(account_code){
  accTBODY.addBehavior("#default#download");
  var suburl="/adjustAction.do?action=downAccount&account_code="+account_code+"&range="+ACCOUNT_RANGE;
  accTBODY.startDownload(suburl,onDownloadAccountDone);
}

//下载科目完成执行的函数
function onDownloadAccountDone(s){
  
  var lineArray = s.split("\r\n");
  if (lineArray[0] == "ERROR") {
    alert("操作失败");
    return;
  }  
  
  var theAccountCode=lineArray[1];
  var theDownAccountCode="";
  
  //删除原来下载的科目
  var rowsLength=accTABLE.rows.length;
  for (var i=1;i<=rowsLength;i++){
    accTABLE.deleteRow(0);
  }
  
  var num=0;
  
  for (var i=2; i<lineArray.length-1; i++) {
    num++;
    txtArray = lineArray[i].split("`");
    theDownAccountCode=txtArray[0];
    
    
    var oNewTR=document.createElement("TR");
    accTBODY.insertAdjacentElement("beforeEnd", oNewTR);
    oNewTR.onclick=checkedOneAcc;
    oNewTR.style.cursor="hand";
    oNewTR.account_code=txtArray[0];
    oNewTR.account_name=txtArray[1];
    oNewTR.bgColor=COLOR_MOUSEOFF;
    oNewTR.onmouseover=fMouseOver;
    oNewTR.onmouseout=fMouseOut;
    
    var oNewTD=document.createElement("TD");
    oNewTR.insertAdjacentElement("beforeEnd", oNewTD);
    oNewTD.innerHTML="【"+txtArray[0]+"】";
    oNewTD.width="30%";
    oNewTD.noWrap=true;
    
    
    oNewTD=document.createElement("TD");
    oNewTR.insertAdjacentElement("beforeEnd", oNewTD);
    oNewTD.innerHTML=txtArray[1];   
    oNewTD.noWrap=true;
    
    if (txtArray[0]==theAccountCode){
      setValue(txtArray[0],txtArray[1]);
    } 
  }
  
  if (!SHOW_IF_ONLY_ME && num<=1 && theDownAccountCode==theAccountCode){
    HideSelect();
  }else if (num==0){
    HideSelect();
  }else{
    ACCOUNT_LIST.style.visibility = "visible";
  }
  
}

//选中列表框的值，填入文本框，并且隐藏列表框
function checkedOneAcc(){
  var oTR=getTR();
  var account_code=oTR.account_code;
  var account_name=oTR.account_name;
  setValue(account_code,account_name);
  SHOW_IF_ONLY_ME=false;
  refresh(account_code);
}

//将标准科目代码和名称写到页面上
function setValue(account_code,account_name){
  SRC_TEXT.value=account_code;
}

with (document) {
  write("<Div id=ACCOUNT_LIST onclick='event.cancelBubble=true' style='POSITION:absolute;visibility:hidden;OVERFLOW:auto;width:270;height:200;'>");
  write("<table border='1' cellspacing='1' cellpadding='2'  bgcolor='#eeeeee' width='100%' id='accTABLE'>");
  write("  <tbody id='accTBODY'>");
  write("  </tbody>");
  write("</tbody>");
  write("</table>");
  
  write("<table border='1' cellspacing='1' cellpadding='2'  bgcolor='#eeebbb' width='100%'>");
  write("  <tr onMouseOver='fMouseOver();'  onMouseOut='fMouseOut();' style='cursor:hand'><td align='center' onclick='HideSelect();'>关  闭</td></tr>");
  write("</table>");
  write("</Div>");
  
  write("<SCRIPT event=onclick() for=document>HideSelect()</SCRIPT>");
}