//var mk_status ;

function mSift(cVarName, nMax) {
    this.oo = cVarName;
    this.Max = nMax;
}
mSift.prototype = {
    Target: Object,
    TgList: Object,
    Listeners: null,
    SelIndex: 0,
    Data: [],
    ReData: [],
    Create: function (str) {
        var _this = this;
        var oUL = document.createElement('ul');
        var oObj = document.getElementById(str);
        oUL.style.display = 'none';
        oUL.className = 'checkmenu'
        oObj.parentNode.insertBefore(oUL, oObj);
        _this.TgList = oUL;
        oObj.onkeyup = function (e) {//#txtattstock
            var val = $('#' + str).val();
            if (val.length > 0) {
                getSearchValue(val, _this);
            }
            _this.Listen(this, str);
        };

        oObj.onclick = function (e) {
            var val = $('#' + str).val();
            if (val.length > 0) {
                getSearchValue(val, _this);
            }
            _this.Listen(this, str);
        };

        oObj.onblur = function () {
            setTimeout(function () {
                _this.Clear();
            },
			25);
        };
    },
    Complete: function () { },
    Select: function (str) {
        var _this = this;
        if (_this.ReData.length > 0) { 
            //by 选取，显示到文本框
            var choose = _this.Data[_this.SelIndex];
            var pre = choose.split(",");
            var mk = "";
            if (pre[1] == 1) {
                mk = "SH";
            } else if (pre[1] == 2) {
                mk = "SZ";
            }
            $('#' + str).attr("mk", mk);
            _this.Target.value = mk + pre[2].replace(/\*/g, '*').replace(/\|/g, '|');
            _this.Clear();
            //数据添加到文本框
            if (str == "txtattstock") {
                insertstock();
            }
        }
    },
    Listen: function (oObj, str) {
        var _this = this;
        _this.Target = oObj;
        var e = arguments[arguments.length - 1];
        var ev = window.event || e;
        switch (ev.keyCode) {
            case 9:
                //TAB
                return;
            case 13:
                //ENTER
                _this.Target.blur();
                _this.Select(str);
                return;
            case 38:
                //UP
                _this.SelIndex = _this.SelIndex > 0 ? _this.SelIndex - 1 : _this.ReData.length - 1;
                break;
            case 40:
                //DOWN
                _this.SelIndex = _this.SelIndex < _this.ReData.length - 1 ? _this.SelIndex + 1 : 0;
                break;
            default:
                _this.SelIndex = 0;
        }
        if (_this.Listeners) {
            clearInterval(_this.Listeners);
        }
        _this.Listeners = setInterval(function () {
            _this.Get(str);
        },
		10);
    },
    Get: function (str) {
        var _this = this;
        if (_this.Target.value == '') {
            _this.Clear();
            return;
        }
        if (_this.Listeners) {
            clearInterval(_this.Listeners);
        };
        _this.ReData = [];
        var cResult = '';
        var len = _this.Data.length;
        for (var i = 0; i < len; i++) {
            if (_this.Data[i] == null || $.trim(_this.Data[i]) == '') {
                continue;
            }
            var pres = _this.Data[i].split(",");
            //用户手动输入
            var val = $('#' + str).val();
            var mk = "";
            if (pres[1] == 1) {
                mk = "SH";
            } else if (pres[1] == 2) {
                mk = "SZ";
            }
            if (len == 1 && $.trim(val) == pres[2]) {

                if (pres[1] == 1) {
                    mk = "SH";
                } else if (pres[1] == 2) {
                    mk = "SZ";
                }
                $('#' + str).attr("mk", mk);
            }
            _this.ReData.push(pres[3] + "(" + mk + pres[2] + ")");

            if (_this.ReData == _this.Max) {
                break;
            }
            //}
        }
        for (var i = 0; i < _this.ReData.length; i++) {
            cResult += '<li  style="width:100%; line-height:20px; border-top:1px solid #d7d4d4; cursor:default;" onmouseover=' +
			_this.oo + '.ChangeOn(this);' + _this.oo + '.SelIndex=' + i + '; onmousedown=' + _this.oo + '.Select("' + str + '");>&nbsp;'
			 + _this.ReData[i] + '</li>';
        }
        if (cResult == '') {
            _this.Clear();
        }
        else {
            _this.TgList.innerHTML = cResult;
            ////上下键移动
            _this.TgList.style.cssText = 'display:block; position:absolute; background:#fff; color:#333; border:1px solid #a9abaa; border-top:none; font-size:12px;';
        }
        var oLi = _this.TgList.getElementsByTagName('li');
        if (oLi.length > 0) {
            oLi[_this.SelIndex].style.cssText = 'width:100%; background:#64a8e7;color:#fff; line-height:20px; cursor:default; border-top:1px solid #d7d4d4;';
        }
    },
    ChangeOn: function (oObj) {
        var oLi = this.TgList.getElementsByTagName('li');
        for (var i = 0; i < oLi.length; i++) {
            oLi[i].style.cssText = 'width:100%; height:20px; line-height:20px; cursor:default; border-top:1px solid #d2d2d2;'; //鼠标移过变成白色
        }
        oObj.style.cssText = 'width:100%; background:#64a8e7; height:20px; line-height:20px; cursor:default; color:#fff; border-top:1px solid #d7d4d4;'; //鼠标移到变蓝色
    },
    Clear: function () {
        var _this = this;
        if (_this.TgList) {
            _this.TgList.style.cssText = '';
            _this.TgList.style.display = 'none';
            _this.ReData = [];
            _this.SelIndex = 0;
        }
    }
}

function getSearchValue(val, obj) {
    val = $.trim(val);
    if (val == null || val == "" || val.length < 1)
        return false;
    var data = GetSearchList('0\n3\n' + val + '\n6'); 
    if (data != null) {
        var last = data.substring(data.length - 1, data.length);
        if (last == "|") {
            data = data.substring(0, data.length - 1);
        }
        //黄金眼还是普通邮件
        obj.Data = data.split("|");
    }
}