﻿// JScript File

// JScript File
// 包含XML提示对象操作函数，在body onload中设置 InitHintControl()，TextBox.url指定xml来源url， TextBox.moreKey指定附加条件，使用内容检测功能必须让checkValue=yes，如果不使用前缀必须设置onlyMe=yes
// 2008-09-26

var colorDown='#8592B5';
var colorOver='#b5bdd6';

// XML读取对象
var XMLLoader = new ActiveXObject("MSXML2.DOMDocument.3.0");
XMLLoader.async=false;
// 提示框中项目的数量
var hintListCount=0;
// 提示框中最后一次被选中的项目
var hintListOldRow=null;
// 输入框中上次的值
var hintInputOldValue="";

// Cell_SetColor
  function Cell_SetColor(cell, backgroundColor, borderColor){
    if(backgroundColor!=null){
      cell.bgColor=backgroundColor;
      cell.borderColor=backgroundColor;
    }
    if(borderColor!=null){
      cell.style.borderLeft=borderColor;
      cell.style.borderRight=borderColor;
      cell.style.borderTop=borderColor;
      cell.style.borderBottom=borderColor;
    }
  }

// Row_SetColor2 XML Hint 专用
  function Row_SetColor2(row, backgroundColor, borderColor, start, len){
    if(hintListOldRow!=null) Row_SetColor(hintListOldRow, '', borderColor, start, len);
    Row_SetColor(row, backgroundColor, borderColor, start, len);
    hintListOldRow=row;
  }
//

// Row_SetColor
  function Row_SetColor(row, backgroundColor, borderColor, start, len){
    var i=0;
    var ocell=null;
    for(i=start;i<len;i++){
      ocell=row.cells(i);
      if(backgroundColor!=null){
        ocell.bgColor=backgroundColor;
        ocell.borderColor=backgroundColor;
      }
      if(borderColor!=null){
        if(i==start) ocell.style.borderLeft=borderColor;
        if(i+1==len) ocell.style.borderRight=borderColor;
        ocell.style.borderTop=borderColor;
        ocell.style.borderBottom=borderColor;
      }
    }
  }
//

  function LTrim(str){
    if(str=="") return "";
    var left=0;
    for(; left<str.length; left++) if(str.charAt(left)!=" ") break;
    return str.substring(left);
  }
  
  function RTrim(str){
    if(str=="") return "";
    var right=str.length-1;
    for(; right>=0; right--) if(str.charAt(right)!=" ") break;
    return str.substring(0, right+1);
  }

  function Trim(str){
    return RTrim(LTrim(str));
  }

  function Chk_Value(oObjects, oLabels){
    for(var i=0; i<oObjects.length; i++){
      if(Trim(oObjects[i].value)==""){
        alert(oLabels[i]);
        oObjects[i].focus();
        return false;
      }
    }
    return true;
  }

  function doDetail(url){
    document.location=url;
    return false;
  }

  // 将字符串限制在制定宽度内（s 字符串， maxWidth 最大宽度（数字）， fontSize 字体）
  function ConvertViewString(s, maxWidth, fontSize){
    if(typeof fontSize=="undefined") fontSize="9pt";
    var obj=document.getElementById("__testViewString");
    if(document.getElementById("__testViewString")==null){
      obj=document.createElement("<div>");
      obj.id="__testViewString";
      obj.style.overflowX="scroll";
      obj.style.overflowY="hidden";
      obj.style.height="1px";
      obj.style.width="1px";
      obj.style.fontSize=fontSize;
      obj.style.fontFamily="宋体";
      obj.style.top="-1px";
      obj.style.left="0px";
      obj.style.position="absolute";
      obj.style.zIndex="-1";
      obj.noWrap=true;
      document.body.insertBefore(obj);
    }
    obj.innerText=s;
    if(obj.scrollWidth>maxWidth){
      var len=Math.floor(s.length*maxWidth/obj.scrollWidth);
      obj.innerText=s.substring(0, len-2)+"...";
      while(maxWidth>obj.scrollWidth){
        len=len+1;
        obj.innerText=s.substring(0, len-2)+"...";
      }
      while(obj.scrollWidth>maxWidth){
        len=len-1;
        obj.innerText=s.substring(0, len-2)+"...";
      }
    }
    return obj.innerText;
  }

  // 将字符串转换成数字
  function ToNumber(s){
    var result="";
    var cs="";
    var pos=-1;
    var doted=false;
    if(isNaN(s)){
      for(var i=0; i<s.length; i++){
        cs=s.charAt(i);
        pos="+-.01234567890".indexOf(cs);
        if(pos>=0){
          if(pos<2&&result=="") result=cs;
          else{
            if(pos==2&&!doted){
              result=result+cs;
              doted=true;
            }else if(pos>2) result=result+cs;
          }
        }
      }
    }else result=s;
    var nResult=0;
    try{
      nResult=result*1;
    }catch(e){
    }
    return nResult;
  }

  // 获得Hint对象名（不含前缀）
  function GetHintName(obj){
    if(typeof obj.onlyMe != "undefined" && obj.onlyMe.toLowerCase()!="no") return obj.id;
    else return obj.id.substring(10);
  }

  // XML Hint 对象操作段    
  // 隐藏提示框
  function doHintListHidden(name){
    if(typeof name == "object") name=GetHintName(name);
    document.getElementById("hintList_"+name).style.display="none";
    hintListOldRow=null;
    hintListCount=0;
    hintInputOldValue="";
  }
    
  // 输入动作
  function doHintListShow(objHintInput){
    // 相关对象的名称（ID）
    var bindName=GetHintName(objHintInput);
    var objHintList=document.getElementById("hintList_"+bindName);
    
    // 按方向键（37左，39右）
    if(event.keyCode==37 || event.keyCode==39) return;
      
    // 按ESC键
    if(event.keyCode==27){
      doHintListHidden(bindName);
      return;
    }
      
    // 按回车键
    if(event.keyCode==13 && hintListOldRow!=null){
      doHintSelected(objHintInput, hintListOldRow);
      return;
    }
      
    // 按方向键（40向下，38向上）
    if(event.keyCode==40 || event.keyCode==38){
      if(hintListCount>0 && objHintList.style.display==""){
        var currentRow=null;
        if(event.keyCode==40){
          if(hintListOldRow!=null) currentRow=hintListOldRow.nextSibling;
          if(currentRow==null) currentRow=document.getElementById("hintTable_"+bindName).rows(0);
        }else{
          if(hintListOldRow!=null) currentRow=hintListOldRow.previousSibling;
          if(currentRow==null) currentRow=document.getElementById("hintTable_"+bindName).rows(hintListCount-1);
        }
        Row_SetColor2(currentRow, colorOver, null, 0, 1);
      }
    }else{  // 正常输入
      // 内容无变化直接跳出
      if(hintInputOldValue==objHintInput.value) return;
      
      // 无内容关闭跳出
      if(objHintInput.value == ""){
        doHintListHidden(bindName);
        return;
      }

      // 获得最大字符串长度
      var maxWidth=ToNumber(objHintInput.style.width);

      // 提示窗口处理
      hintListCount=0;
      XMLLoader.abort();

      // 获得url
      var url="";
      if(typeof objHintInput.url != "undefined") url=objHintInput.url;
      else url="Hint_"+bindName+".aspx";
      url=url+"?key="+escape(objHintInput.value);

      // 附加参数
      if(typeof objHintInput.moreKey != "undefined") url=url+"&moreKey="+objHintInput.moreKey;

      try{
        XMLLoader.load(url);
        var items=XMLLoader.selectNodes("/Items/Item");
        var s="<table width=\"100%\" id=\"hintTable_"+bindName+"\" cellpadding=0 cellspacing=0 style=\"cursor: hand\">";
        for(var i=0; i<items.length; i++){
          var id=GetXMLNode("/Items/Item["+i+"]/ID");
          var name=GetXMLNode("/Items/Item["+i+"]/Name");
          var name2=ConvertViewString(name, maxWidth, "9pt");
          name2="&nbsp;"+name2;
          s=s+"<tr title=\""+name+"\" itemID=\""+id+"\" onclick=\"doHintSelected(document.getElementById('"+objHintInput.id+"'), this);\" onmouseout=\"hintListOldRow=this;\" onmouseover=\"Row_SetColor2(this, colorOver, null, 0, 1);\"><td align=\"left\"><span>"+name2+"</span></td></tr>";
        }
        s=s+"</table>";
        hintListCount=items.length;
        if(items.length==0){
          doHintListHidden(bindName);
          return;
        }
        hintListOldRow=null;
        hintInputOldValue=objHintInput.value;
        objHintList.innerHTML=s;
        objHintList.style.height=hintListCount*16;
        objHintList.style.display="";
      }catch(e){
        doHintListHidden(bindName);
      }
    }
  }
    
  // 获得XML接点
  function GetXMLNode(xpath) {
    var result = "";
    var value = XMLLoader.documentElement.selectSingleNode(xpath);
    if (value) result = value.text;
    return result;
  }
    
  // 初始化对象（只要设置TextBox，但ID必须以hintInput_开头）
  function InitHintControl(){
    for(var i=0; i<document.all.length; i++){
      var obj=document.all(i);
      if(obj.id.substring(0, 10)=="hintInput_" || typeof obj.onlyMe != "undefined" && obj.onlyMe.toLowerCase()!="no"){
        obj.onblur=function(){doHintInputBlur(this)};
        obj.onkeyup=function(){doHintListShow(this)};
        obj.onkeydown=function(){if(event.keyCode==9) doHintListHidden(this)};
        
        var objList=document.getElementById("hintList_"+GetHintName(obj));
        if(objList==null){
          objList=document.createElement("<div>");
          objList.id="hintList_"+GetHintName(obj);
          objList.style.display="none";
          objList.style.overflowX="hidden";
          objList.style.overflowY="hidden";
          objList.style.position="absolute";
          objList.style.zIndex="1";
          objList.style.fontSize="9pt";
          objList.style.border="1px solid #000000";
          objList.style.backgroundColor="White";
          objList.noWrap=true;
          objList.style.left=GetLeft(obj);
          objList.style.top=GetTop(obj)+obj.offsetHeight;
          InsertAfter(objList, obj);
        }
        objList.style.width=ToNumber(obj.style.width)+4;
      }
    }
  }
    
  // 内容被选中
  function doHintSelected(obj, tr){
    var objID=document.getElementById("txt"+GetHintName(obj));
    if(objID!=null) objID.value=tr.itemID;
    obj.value=tr.title;
    doHintListHidden(obj);
  }
    
  // 当输入框失去焦点， 使用内容检测功能必须让checkValue=yes
  function doHintInputBlur(obj){
    var objHintList=document.getElementById("hintList_"+GetHintName(obj));
    if(!objHintList.contains(document.activeElement)){
      doHintListHidden(obj);
      
      if(typeof obj.checkValue != "undefined" && obj.checkValue.toLowerCase()!="no"){
        var url="";
        if(typeof obj.url != "undefined") url=obj.url;
        else url="Hint_"+bindName+".aspx";
        url=url+"?key="+escape(obj.value);

        if(typeof obj.moreKey != "undefined") url=url+"&moreKey="+obj.moreKey;

        var txtObj=document.getElementById("txt"+GetHintName(obj));
        try{
          XMLLoader.load(url);
          var items=XMLLoader.selectNodes("/Items/Item");
          if(items.length>0){
            if(txtObj!=null) txtObj.value=GetXMLNode("/Items/Item[0]/ID");
            obj.value=GetXMLNode("/Items/Item[0]/Name");
          }else if(txtObj!=null) txtObj.value="0";
        }catch(e){
          if(txtObj!=null) txtObj.value="0";
        }
      }
    }
  }

  // 在某个对象前插入新对象
  function InsertAfter(newElement,targetElement) {
    var parent=targetElement.parentNode;
    if (parent.lastChild==targetElement) parent.appendChild(newElement);
    else parent.insertBefore(newElement,targetElement.nextSibling);
  }

  // 获得对象在窗口中的位置Left
  function GetLeft(obj, endTag){
    if(typeof endTag=="undefined") endTag=="";
    var result=obj.offsetLeft;
    while(obj.parentElement!=null){
      if(endTag==obj.tagName) break;
      obj=obj.parentElement;
      result=result+obj.offsetLeft;
    }
    return result;
  }

  // 获得对象在窗口中的位置Top
  function GetTop(obj, endTag){
    if(typeof endTag=="undefined") endTag=="";
    var result=obj.offsetTop;
    while(obj.parentElement!=null){
      if(endTag==obj.tagName) break;
      obj=obj.parentElement;
      if(obj.tagName=="TD" && endTag!=obj.tagName) continue;
      result=result+obj.offsetTop;
    }
    return result;
  }

