﻿var _commDHintDivHTML = "<div id=\"commDHintDiv\" class=\"comm_dhint_div\" style=\"display:none;z-index:100;\">" +
                        "</div>"+
                        "<iframe id=\"commDHintFrame\" src=\"\" scrolling=\"no\" frameborder=\"0\" "+
                        "style=\"position:absolute; top:0px; left:0px; display:none; background-color2:#f00;\"></iframe>";
document.write(_commDHintDivHTML);

var _Const_Cur_Char_Min=2;
var _Const_Cur_DataName="";
var _Const_Cur_Mode="";
var _Const_Cur_UseFrame=false;
var _Const_Cur_eTarget;
var _Const_Cur_DHint_Show=false;
var _Const_ClassName_Item_Over="comm_dhint_div_item_over";
var _Const_ClassName_Item_Out="comm_dhint_div_item_out";
var _Const_AppPath='/';

var commDHintDiv,commDHintFrame;
function initCommDHint(_dataname,_charmin,_useframe) {
    var href=location.href||'';
    if(href.indexOf("/shmcweb/")>0)_Const_AppPath='/shmcweb/';
    if(_dataname!=null) _Const_Cur_DataName=_dataname;
    if(_charmin!=null) _Const_Cur_Char_Min=_charmin;
    if(_useframe!=null) _Const_Cur_UseFrame=_useframe;
    commDHintDiv = document.getElementById("commDHintDiv");
    commDHintFrame=document.getElementById("commDHintFrame");
    if(_isIE)addEvent(document.body,"click",doBodyClick);
    else document.body.onclick=doBodyClick;
}

function addEventToDHint(objidstr) {
    var _arr = objidstr.split(",");
    for(var i = 0; i < _arr.length; i++) {
        var _o = document.getElementById(_arr[i]);
        if(_o != null) {
            if(_isIE) {
                addEvent(_o, "keydown", doCommDHintKeyEvent);
                addEvent(_o, "keyup", doDHintEvent);
            } else {
                _o.onkeydown = doCommDHintKeyEvent;
                _o.onkeyup = doDHintEvent;
            }
        }
    }
}

function doDHintEvent() {
    var e = EventUtil.getEvent();
    var obj = e.target;
    if(obj == null) return false;
    _Const_Cur_eTarget=obj;
    //doCommDHintKeyUpEvent(e);
    var objvalue=obj.value||'';
    var objvaluepre=obj.getAttribute("prevalue")||'';
    var _char_min=(obj.getAttribute("charmin")||'')==''?_Const_Cur_Char_Min:
                  parseInt(obj.getAttribute("charmin"));
    if((obj.getAttribute("dataname")||'')=='idcard'){ //member idcardnolist
        var loadMemberIDCardListAjax = new LoadMemberIDCardListAjax(obj, obj.getAttribute("dataname"),
                obj.getAttribute("mode"));
        loadMemberIDCardListAjax.execute();
    }else{
        if(objvalue.length >= _char_min) {
            if(objvalue != objvaluepre) {
                obj.setAttribute("prevalue", objvalue);
                obj.removeAttribute("cindex");
                obj.removeAttribute("cvalue");
                var loadDHintDataAjax = new LoadDHintDataAjax(obj, obj.value, obj.getAttribute("dataname"),
                        obj.getAttribute("mode"));
                loadDHintDataAjax.execute();
            //showCommDHintDiv(obj);
            }
        } else {
            obj.setAttribute("prevalue", objvalue);
            fadeDHintDiv();
        }
    }
}

var LoadDHintDataAjax = Class.create();
LoadDHintDataAjax.prototype = {
    initialize: function(_e, _key, _dataname, _mode, _method) {
        this.e=_e;
        this.key = _key==null?"":_key;
        this.dataname = _dataname==null?_Const_Cur_DataName:_dataname;
        this.mode = _mode==null?_Const_Cur_Mode:_mode;
        this.method = _method == null ? "get" : _method;
    },
    execute: function() {
        var params=_Const_Cur_eTarget.getAttribute("params")||'';
        var url = _Const_AppPath+"XML_CommDHintData.jsp";
        var pars = "key=" + encodeURIComponent(base64encode(utf16to8(this.key))) + "&dataname=" + this.dataname +
                   "&mode="+this.mode+
                   "&params="+params+
                   ((this.e.getAttribute("from")||'')!=''?'&from='+this.e.getAttribute("from"):'')+
                   "&t=" + getRandomNum();
        //alert(url+"?"+pars);
        var _ajax = new Ajax.Request(url, {method: this.method, parameters: pars, onComplete: this.executeCompleted.bind(this)});
    },
    executeCompleted: function(res) {
        var resText = strtrim(res.responseText);
        //alert(resText);
        if(resText=='') fadeDHintDiv();
        else {
            showCommDHintDiv(this.e);
            commDHintDiv.innerHTML=resText;
            if(this.e.getAttribute("selfirst")!=null) doCommDHintItemMoveDown();
        }
    }
};

var LoadMemberIDCardListAjax = Class.create();
LoadMemberIDCardListAjax.prototype = {
    initialize: function(_e, _dataname, _mode, _method) {
        this.e=_e;
        this.dataname = _dataname==null?_Const_Cur_DataName:_dataname;
        this.mode = _mode==null?_Const_Cur_Mode:_mode;
        this.method = _method == null ? "get" : _method;
    },
    execute: function() {
        var memberid=this.e.getAttribute("memberid")||'';
        var params=_Const_Cur_eTarget.getAttribute("params")||'';
        var url = _Const_AppPath+"XML_MemberIDCardList.jsp";
        var pars = "dataname=" + this.dataname +
                   "&mode="+this.mode+
                   "&memberid="+memberid+
                   "&params="+params+
                   ((this.e.getAttribute("from")||'')!=''?'&from='+this.e.getAttribute("from"):'')+
                   "&t=" + getRandomNum();
        //alert(url+"?"+pars);
        var _ajax = new Ajax.Request(url, {method: this.method, parameters: pars, onComplete: this.executeCompleted.bind(this)});
    },
    executeCompleted: function(res) {
        var resText = strtrim(res.responseText);
        //alert(resText);
        if(resText=='') fadeDHintDiv();
        else {
            showCommDHintDiv(this.e);
            commDHintDiv.innerHTML=resText;
            if(this.e.getAttribute("selfirst")!=null) doCommDHintItemMoveDown();
        }
    }
};

function clickCommDHintItem(_dataname, _did, _dname) {
    if(_dataname=='sysuser'){
        doCommDHintItemClick(_did,_dname);
    }else{
        doCommDHintItemClick(_did,_dname);
    }
}

function doCommDHintItemClick(_did,_dname){
    if(_Const_Cur_eTarget==null) return;
    var obj=_Const_Cur_eTarget;
    obj.value=_dname;
    obj.focus();
    obj.setAttribute("dataid",_did);
    fadeDHintDiv();
    var func=obj.getAttribute('func')||'';
    if(func!='') setTimeout(func,0);
}

function doCommDHintKeyEvent(e){
    if(_isIE) e = EventUtil.getEvent();    
    var keycode = _isIE ? e.keyCode : e.which;
    if(!_Const_Cur_DHint_Show) return;
    if(keycode==40) doCommDHintItemMoveDown(e);
    if(keycode==38) doCommDHintItemMoveUp(e);
    if(keycode==13){
        //var obj = e==null? null: e.target;
        //if(obj==null) obj=_Const_Cur_eTarget;
        //if(obj.getAttribute("cindex")!=null) return false;
        return doCommDHintItemEnter(e);
    }
}

function doCommDHintKeyUpEvent(e){
    if(_isIE) e = EventUtil.getEvent();
    var keycode = _isIE ? e.keyCode : e.which;
    if(!_Const_Cur_DHint_Show) return;
    if(!_isIE){
        if(keycode==40) doCommDHintItemMoveDown(e);
        if(keycode==38) doCommDHintItemMoveUp(e);
    }
    if(keycode==13) doCommDHintItemEnter(e);
}

function doCommDHintMouseOver(objitem,cindex){
    var obj=_Const_Cur_eTarget;
    if(obj==null) return;
    var cindexpre=obj.getAttribute("cindex");
    if(cindexpre!=null){
        if(cindexpre==cindex) return;
        var objitempre=document.getElementById("div_dhint_item_"+cindexpre);
        if(objitempre!=null) objitempre.className=_Const_ClassName_Item_Out;
    }
    objitem.className=_Const_ClassName_Item_Over;
    obj.setAttribute("cindex",cindex);
    obj.setAttribute("cvalue",objitem.getAttribute("itemvalue")||'');
}

function doCommDHintMouseOut(obj,cindex){
}

function doBodyClick(){
    var e = EventUtil.getEvent();
    var target=e.target;
    if(_Const_Cur_DHint_Show && target!=null){
        if(target!=_Const_Cur_eTarget) fadeDHintDiv();
    }
}

function doCommDHintItemMoveDown(e){
    var obj = e==null? null: e.target;
    if(obj==null) obj=_Const_Cur_eTarget;
    var cindex=obj.getAttribute("cindex");
    if(cindex==null||isNaN(cindex)) cindex=-1;
    var cvalue=obj.getAttribute("cvalue")||'';
    var objitem=document.getElementById("div_dhint_item_"+cindex);
    cindex++;
    var objitemnext=document.getElementById("div_dhint_item_"+cindex);
    if(objitemnext!=null){
        if(objitem!=null) objitem.className=_Const_ClassName_Item_Out;
        objitemnext.className=_Const_ClassName_Item_Over;
        /*var divZone=document.getElementById("div_dhint_zone");
        if(divZone!=null){
            if(cindex>=10) {
                var _top=(divZone.style.top||'').replace(/(px)/g,'');
                divZone.style.top=parseInt(_top)-15;
            }
        }*/
        obj.setAttribute("cindex",cindex);
        obj.setAttribute("cvalue",objitemnext.getAttribute("itemvalue")||'');
    }
}

function doCommDHintItemMoveUp(e){
    var obj = e==null? null: e.target;
    if(obj==null) obj=_Const_Cur_eTarget;
    var cindex=obj.getAttribute("cindex");
    if(cindex==null||isNaN(cindex)) cindex=-1;
    var cvalue=obj.getAttribute("cvalue")||'';
    var objitem=document.getElementById("div_dhint_item_"+cindex);
    cindex--;
    var objitemnext=document.getElementById("div_dhint_item_"+cindex);
    if(objitemnext!=null){
        if(objitem!=null) objitem.className=_Const_ClassName_Item_Out;
        objitemnext.className=_Const_ClassName_Item_Over;
        /*var divZone=document.getElementById("div_dhint_zone");
        if(divZone!=null){
            var count=divZone.getAttribute("count");
            if(cindex<count-10) {
                var _top=(divZone.style.top||'').replace(/(px)/g,'');
                divZone.style.top=parseInt(_top)+15;
            }
        }*/
        obj.setAttribute("cindex",cindex);
        obj.setAttribute("cvalue",objitemnext.getAttribute("itemvalue")||'');
    }
}

function doCommDHintItemEnter(e){
    var obj = e==null? null: e.target;
    if(obj==null) obj=_Const_Cur_eTarget;
    if(obj.getAttribute("cvalue")!=null){
        obj.value=obj.getAttribute("cvalue");
        obj.setAttribute("prevalue",obj.value);
        fadeDHintDiv();
        return false;
    }
}

function showCommDHintDiv(e) {
    var e_divw = 0,e_divh = 0;
    try {
        e_divw = parseInt(e.getAttribute("divw"));
        e_divh = parseInt(e.getAttribute("divh"));
        if(isNaN(e_divw)) e_divw=0;
        if(isNaN(e_divh)) e_divh=0;
    } catch(ex) {
    }
    var t = e.offsetTop,  h = e.clientHeight, w = e.clientWidth, l = e.offsetLeft, p = e.type;
    while(e = e.offsetParent) {
        t += e.offsetTop;
        l += e.offsetLeft;
    }
    if(e_divw>0) commDHintDiv.style.width = e_divw;
    else commDHintDiv.style.width=w+(_isIE?4:-2);
    if(e_divh>0) commDHintDiv.style.height = e_divh;
    commDHintDiv.style.zIndex = 100;
    //commDHintDiv.style.visibility = "visible";
    commDHintDiv.style.left = l;
    commDHintDiv.style.top = t + h + 3;
    if(!_isIE&&_Const_Cur_UseFrame) {
        //commDHintDiv.style.width='auto';
    }
    appearDHintDiv();
}

function appearDHintDiv(){
    if(commDHintDiv.style.display!='none') return;
    Effect.Appear(commDHintDiv.getAttribute("id"),{duration:0.5, from:0.0, to:1.0});
    _Const_Cur_DHint_Show=true;
    //alert(commDHintDiv.clientWidth);
    if(_Const_Cur_UseFrame){
        _Const_ShowCover_Count=0;
        _Const_ShowCover_Eqal_count=0;
        _Const_DHintDiv_H=commDHintDiv.clientHeight;
        //$('key_hos').value=_Const_DHintDiv_H+',';
        showCoverFrameProxy();
    }
    //if(_Const_Cur_UseFrame) setTimeout("showCoverFrame();",500);
}
function fadeDHintDiv(){
    if(commDHintDiv.style.display=='none') return;
    Effect.Fade(commDHintDiv.getAttribute("id"),{duration:0.1, from:1.0, to:0.0});
    _Const_Cur_DHint_Show=false;
    if(_Const_Cur_eTarget!=null){
        var obj=_Const_Cur_eTarget;
        obj.removeAttribute("cindex");
        obj.removeAttribute("cvalue");
    }
    if(_Const_Cur_UseFrame) hideCoverFrame();
}

var _Const_ShowCover_Count=0;
var _Const_DHintDiv_H=0;
var _Const_ShowCover_Eqal_count=0;
function showCoverFrameProxy(){
    var dhintdiv_h=commDHintDiv.clientHeight;
    if(dhintdiv_h==null) dhintdiv_h=0;
    if(dhintdiv_h==_Const_DHintDiv_H||_Const_ShowCover_Count==0){
        if(dhintdiv_h==_Const_DHintDiv_H) _Const_ShowCover_Eqal_count++;
        if(_Const_ShowCover_Eqal_count>5){
            showCoverFrame();
        }else{
            setTimeout("showCoverFrameProxy();",10);
            _Const_ShowCover_Count++;
            //$('key_hos').value=$('key_hos').value+_Const_DHintDiv_H+',';
        }
    }else{
        showCoverFrame();
    }
}

function showCoverFrame(){
    commDHintFrame.style.width = commDHintDiv.clientWidth;
    commDHintFrame.style.height = commDHintDiv.clientHeight+2;
    commDHintFrame.style.top = commDHintDiv.style.top;
    commDHintFrame.style.left = commDHintDiv.style.left;
    commDHintFrame.style.zIndex = commDHintDiv.style.zIndex - 1;
    commDHintFrame.style.display = "block";
    //$('key_hos').value=$('key_hos').value+commDHintDiv.clientHeight;
}
function hideCoverFrame(){
    commDHintFrame.style.display = "none";
}