/*****************************************输入框边输入内容边搜索功能方法*************************************************/
//级联诊断码
function getDiagCodeContents(id) {
    //获取同级的诊断名称是否输入
    var diagName = $("#"+id).parent().prev().find('input').val();
    if(diagName != ''){
        var ajax = new XMLHttpRequest();
        var url = path+"/commom/getDiagCodeContents?name=" + diagName;
        ajax.open("GET", url, true);
        ajax.send(null);
        ajax.onreadystatechange = function() {
            if (ajax.readyState == 4) {
                if (ajax.status == 200) {
                    var result = ajax.responseText;
                    insertContent(result,id);
                }
            }
        }
    }else{
        var keyword = document.getElementById(id).value;
        if (keyword.length > 2) {
            var ajax = new XMLHttpRequest();
            var url = path+"/commom/getDiagCodeContents?code=" + keyword;
            ajax.open("GET", url, true);
            ajax.send(null);
            ajax.onreadystatechange = function() {
                if (ajax.readyState == 4) {
                    if (ajax.status == 200) {
                        var result = ajax.responseText;
                        insertContent(result,id);
                    }
                }
            }
        } else {
            clearContent(id);
            return;
        }
    }
}
//级联诊断名称
function getDiagNameContents(id) {
    //获取同级的诊断码是否输入
    var diagCode = $("#"+id).parent().next().find('input').val();
    if(diagCode != ''){
        var ajax = new XMLHttpRequest();
        var url = path+"/commom/getDiagCodeContents?code=" + diagCode;
        ajax.open("GET", url, true);
        ajax.send(null);
        ajax.onreadystatechange = function() {
            if (ajax.readyState == 4) {
                if (ajax.status == 200) {
                    var result = ajax.responseText;
                    insertContent(result,id);
                }
            }
        }
    }else{
        var keyword = document.getElementById(id).value;
        if (keyword.length > 2) {
            var ajax = new XMLHttpRequest();
            var url = path+"/commom/getDiagCodeContents?code=" + diagCode+"&name="+keyword;
            ajax.open("GET", url, true);
            ajax.send(null);
            ajax.onreadystatechange = function() {
                if (ajax.readyState == 4) {
                    if (ajax.status == 200) {
                        var result = ajax.responseText;
                        insertContent(result,id);
                    }
                }
            }
        } else {
            clearContent(id);
            return;
        }
    }
}
//级联手术编码
function getOperateCodeContents(id) {
    //获取同级的手术名称是否输入
    var operaName = $("#"+id).parent().next().find('input').val();
    if(operaName != ''){
        var ajax = new XMLHttpRequest();
        var url = path+"/commom/getOperateCodeContents?name="+operaName;
        ajax.open("GET", url, true);
        ajax.send(null);
        ajax.onreadystatechange = function() {
            if (ajax.readyState == 4) {
                if (ajax.status == 200) {
                    var result = ajax.responseText;
                    insertContent(result,id);
                }
            }
        }
    }else{
        var keyword = document.getElementById(id).value;
        if (keyword.length > 2) {
            var ajax = new XMLHttpRequest();
            var url = path+"/commom/getOperateCodeContents?code=" + keyword;
            ajax.open("GET", url, true);
            ajax.send(null);
            ajax.onreadystatechange = function() {
                if (ajax.readyState == 4) {
                    if (ajax.status == 200) {
                        var result = ajax.responseText;
                        insertContent(result,id);
                    }
                }
            }
        } else {
            clearContent(id);
            return;
        }
    }
}

//级联手术名称
function getOperateNameContents(id) {
    //获取同级的手术编码是否输入
    var operaCode = $("#"+id).parent().prev().find('input').val();
    if(operaCode != ''){
        var ajax = new XMLHttpRequest();
        var url = path+"/commom/getOperateCodeContents?code=" + operaCode;
        ajax.open("GET", url, true);
        ajax.send(null);
        ajax.onreadystatechange = function() {
            if (ajax.readyState == 4) {
                if (ajax.status == 200) {
                    var result = ajax.responseText;
                    insertContent(result,id);
                }
            }
        }
    }else{
        var keyword = document.getElementById(id).value;
        if (keyword.length > 2) {
            var ajax = new XMLHttpRequest();
            var url = path+"/commom/getOperateCodeContents?name=" + keyword;
            ajax.open("GET", url, true);
            ajax.send(null);
            ajax.onreadystatechange = function() {
                if (ajax.readyState == 4) {
                    if (ajax.status == 200) {
                        var result = ajax.responseText;
                        insertContent(result,id);
                    }
                }
            }
        } else {
            clearContent(id);
            return;
        }
    }
}
//添加内容，tr td
function insertContent(content,id) {
    clearContent(id);
    setLocation(id);
    var json = JSON.parse(content);
    for (key in json) {
        var tr = document.createElement("tr");
        var td1 = document.createElement("td");
        var td2 = document.createElement("td");
        var td3 = document.createElement("td");
        var td4 = document.createElement("td");
        tr.setAttribute("bgcolor", "#FFFAFA");
        tr.setAttribute("border", "0");
        if(id == 'inpatient_no'){
            td1.innerHTML = "病案号:";
        }else if(id.indexOf('diag_code')!= -1 || id.indexOf('diag_name')!= -1){
            td1.innerHTML = "ICD码:";
        }else if(id.indexOf('operate_code')!= -1 || id.indexOf('operate_name')!= -1){
            td1.innerHTML = "手术编码:";
        }
        td2.innerHTML = key;
        if(id == 'inpatient_no'){
            td3.innerHTML = "姓名:";
        }else if(id.indexOf('diag_code')!= -1 || id.indexOf('diag_name')!= -1){
            td3.innerHTML = "诊断名称:";
        }else if(id.indexOf('operate_code')!= -1 || id.indexOf('operate_name')!= -1){
            td3.innerHTML = "手术名称:";
        }

        td4.innerHTML = json[key];
        tr.appendChild(td1);
        tr.appendChild(td2);
        tr.appendChild(td3);
        tr.appendChild(td4);
        document.getElementById(id+"Content").appendChild(tr);
        tr.onmouseover = function() {
            this.className = 'mouseOver';
            if(id.indexOf('diag_code') != -1 || id.indexOf('operate_code') != -1){
                document.getElementById(id).value=$(this).children("td").eq(1).html();
            }else{
                document.getElementById(id).value=$(this).children("td").eq(3).html();
            }
        };
        tr.onmouseout = function() {
            this.className = 'mouseOut';
        };
    }
}
//清楚内容方法
function clearContent(id) {
    var popNode = document.getElementById(id+"Div");
    popNode.style.border = "none";
    var contentNode = document.getElementById(id+"Content");
    var len = contentNode.childNodes.length;
    for (var i = len - 1; i >= 0; i--) {
        contentNode.removeChild(contentNode.childNodes[i]);
    }
}
//设置样式
function setLocation(id){
    var inputNode = document.getElementById(id);
    var width = inputNode.offsetWidth;
    var left = inputNode["offsetLeft"];
    var top = inputNode.offsetHeight+inputNode["offsetTop"];
    var popNode = document.getElementById(id+"Div");
    popNode.style.border = "gray 0.5px solid";
    popNode.style.width = width+"px";
    popNode.style.top = top+"px";
    popNode.style.left = left+"px";
    document.getElementById(id+"Content_table").style.width=width+"px";
}