﻿
document.write("<script src=/js/ajax.js></script>");
document.onclick = function(){
if($("listdiv")){
    $("listdiv").style.display="none";}
};
function Set(showid,fname,setsort)
{
    if(document.readyState!="complete"){return ""};
    var showobj = $(showid);
    var cvalue = showobj.value.trim();
    if(cvalue=="")return null;
    var w=showobj.offsetWidth;
    var h = showobj.offsetHeight;
    if(!$("listdiv")){CreateDIV(w);}
    var pos = getAbsolutePosition(showobj);
    
    var textfiald="";
    var valuefiald ="id";
    if(setsort==3)
    {
        textfiald="hospitalname";
    }
    else if(setsort==4)
    {
        textfiald="doctorname";
    }
    else if(setsort==5)
    {
        textfiald="ItemName";
    }
    else
    {
        textfiald="Name";
        valuefiald ="ID";
    }
//    alert(setsort);
    
    ajaxRead("/ajaxpage/gethospitallist/?key="+encodeURIComponent(cvalue)+"&dt="+setsort+"", "BindList(obj,'"+fname+"','"+showid+"','"+textfiald+"','"+valuefiald+"');");
    
    $("listdiv").style.left = pos.x+"px";
    $("listdiv").style.top = pos.y+h-1+"px";
}
function BindList(obj,fname,robjid,textfiald,valuefiald)
{
//    var bobj = $("mallselect");
//    bobj.length=1;
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false" ;
    xmlDoc.load(obj); //装载xml文件 
    $("listdiv").innerHTML="";
    var liArray = xmlDoc.getElementsByTagName('ds');
    if(liArray.length>0){
        var MyUL = document.createElement("ul");
        
        for(var i=0;i<liArray.length;i++)
        {
            var MyLi = document.createElement("li");
            
            MyLi.id = "myli"+i;
            MyLi.innerHTML=liArray[i].getElementsByTagName(textfiald)[0].firstChild.data;
            MyLi.title = liArray[i].getElementsByTagName(valuefiald)[0].firstChild.data;
            MyUL.appendChild(MyLi);
        }
        $("listdiv").appendChild(MyUL);
        SetListOn(fname,robjid);
        $("listdiv").style.display="";
    }
    else{
        $("listdiv").style.display="none";
    }
}
function SetListOn(fname,robjid)
{
    var list_obj = $("listdiv").getElementsByTagName("li"); //获取list所有li对象数组
    for (var i = 0; i < list_obj.length; i++)
    {
            list_obj[i].onmouseover = function(){this.style.backgroundColor='blue';this.style.color='#fff';}
            list_obj[i].onmouseout = function(){this.style.backgroundColor='';this.style.color='';}
            list_obj[i].onclick = function(){eval(""+fname+"(this.title,this.innerHTML,'"+robjid+"')")}
    }      
}
function fc(v,s,robjid)
{
    $(robjid).value=s;
}
function CreateDIV(w)
{
    var div=document.createElement("div");//创建一个DIV
    div.id = "listdiv";
    div.style.position = 'absolute';
    div.style.border="1px solid #ccc";
    div.style.backgroundColor="#FFF";
    div.style.width = w-2+"px";
    div.className = "listinputdiv";
    div.style.display="none";
    document.body.appendChild(div); //主要加上这句，把新建的DIV加到页面上。
}
function getAbsolutePosition(obj){
  position = new Object();
  position.x = 0;
  position.y = 0;
  var tempobj = obj;
   while(tempobj!=null && tempobj!=document.body)
   {
    if(window.navigator.userAgent.indexOf("MSIE")!=-1)
    {
      position.x += tempobj.offsetLeft;
      position.y += tempobj.offsetTop;
     }
    else if(window.navigator.userAgent.indexOf("Firefox")!=-1)
    {
      position.x += tempobj.offsetLeft;
      position.y += tempobj.offsetTop;
     }
     tempobj = tempobj.offsetParent
    }
  return position;
 } 
 

/**
* 删除左右两端的空格
*/
String.prototype.trim=function()
{
     return this.replace(/(^\s*)|(\s*$)/g, '');
}
/**
* 删除左边的空格
*/
String.prototype.ltrim=function()
{
     return this.replace(/(^\s*)/g,'');
}
/**
* 删除右边的空格
*/
String.prototype.rtrim=function()
{
     return this.replace(/(\s*$)/g,'');
}
