﻿//库存搜索 智能提示
var num = 0;
//键盘事件记录原始输入型号
var oldstr;
//自动扫描记录原始型号
var oldpn;

function MouseOv(obj) {
    num = parseInt($(obj).attr("id").substring(1, 2));
    $(".sys_div").css({ color: "black", background: "" });
    $(obj).css({ color: "#ffffff", background: "#3366CC" });
}

function MouseOu(obj) {
    $(obj).css({ color: "black", background: "" });
}

$(function() {
    $(window).resize(function() {
        myresize();
    });
    $("#FloatDIV").hide();
    $(document).click(function() {
        $("#FloatDIV").hide();
        $("#FloatDIV").html("");
    });
    $("#txtStore").keydown(function(event) {
        if (event.which == 38) {
            num = num - 1;
            if (num == 0) {
                $("#txtStore").val(oldstr);
                $(".sys_div").css({ color: "black", background: "" });
                return;
            } else if (num == -1) {
                num = $("#FloatDIV a").size();
            }
            if ($("#FloatDIV").html() != "") {
                $("#txtStore").val($("#d" + num).text());
                $(".sys_div").css({ color: "black", background: "" });
                $("#d" + num).css({ color: "#ffffff", background: "#3366CC" });

            }
        } else if (event.which == 40) {
            num = num + 1;
            if (num > $("#FloatDIV a").size()) {
                $("#txtStore").val(oldstr);
                num = 0;
                $(".sys_div").css({ color: "black", background: "" });
                return;
            }

            if ($("#FloatDIV").html() != "") {
                $("#txtStore").val($("#d" + num).text());
                $(".sys_div").css({ color: "black", background: "" });
                $("#d" + num).css({ color: "#ffffff", background: "#3366CC" });
            }
        }
    });
    $("#txtStore").keyup(function(event) {

        // 38上箭头 40下箭头 8退格
        if (event.which == 37 || event.which == 38 || event.which == 39 || event.which == 40) {
            return;
        }
        if ($.trim($(this).val()) == "") {
            $("#FloatDIV").html("");
            $("#FloatDIV").hide();
            return;
        }
        if ((event.which >= 48 && event.which <= 57) || (event.which >= 96 && event.which <= 105) || (event.which >= 65 && event.which <= 90) || (event.which >= 97 && event.which <= 122) || event.which == 8 || event.which == 46 || event.which == 13) 
        {
            num = 0;
            oldstr = $(this).val();
            BeginAjax();
        }
        else {
            $("#FloatDIV").html("");
            $("#FloatDIV").hide();
        }

    });
});

//补救floatDiv绝对定位，浏览器大小变化
function myresize() {
    $("#FloatDIV").css("left", $("#txtStore").offset().left);
    $("#FloatDIV").css("top", $("#txtStore").offset().top + $("#txtStore").height() + 4);
}
function BeginAjax() {


    var URL;
    try {
        URL = LinkURL;

    }
    catch (er1) {
        URL = "";
    }
    $.ajax({
        type: "POST",
        url: "/handler/PnMatching.ashx",
        data: "Pn=" + $.trim($("#txtStore").val()),
        success: function(data) {
            eval("pns=" + data);
            var pn = "";
            $.each(pns, function(i, n) {
                var idnum = i + 1;
                try {
                    if (Tgt == "") {
                        Tgt = "_self";
                    }
                }
                catch (er2) {
                    Tgt = "_self";
                }
                try {

                    if (URL != "") {
                        //地址前半部分
                        URLfront = URL.substring(0, URL.indexOf('?'));
                        //地址后半部分
                        URLafter = URL.substring(URL.indexOf('?') + 1, URL.length);

                        var param = URLafter.split('&');
                        for (i = 0; i < param.length; i++) {
                            if (i == 0) {
                                URLafter = param[i] + "=" + escape(n);
                            } else {
                                URLafter += "&" + param[i] + "=" + escape(n);
                            }
                        }
                        LinkURL = URLfront + "?" + URLafter;
                    } else {
                        LinkURL = "/ic/" + escape(n) + ".shtml";
                    }
                }
                catch (er3) {
                    LinkURL = "/ic/" + escape(n) + ".shtml";
                }
                pn += "<a id='d" + idnum + "' class='sys_div' target=" + Tgt + " href='" + LinkURL + "' onmouseover='MouseOv(this)' onmouseout='MouseOu(this)'>" + n + "</a>";

            });
            if (pn == "") {
                $("#FloatDIV").html("");
                $("#FloatDIV").hide();
                return;
            }
            //清空本次链接
            LinkURL = "";
            $("#FloatDIV").html(pn);
            $(".sys_div").css("color", "black");
            $("#FloatDIV").show();
            $("#FloatDIV").width($("#txtStore").width() + 3);
            $("#FloatDIV").height($("#FloatDIV a").height() * $("#FloatDIV a").size());
            $("#FloatDIV").css("left", $("#txtStore").offset().left);
            $("#FloatDIV").css("top", $("#txtStore").offset().top + $("#txtStore").height() + 6);
        }
    });

}