﻿Search = function (id) {
	this.fieldID = id;
	this.keyField = null;
	this.Init();
};

Search.prototype = {
    fieldID: null,
    keyField: null,

    Init: function () {
        var func = this;

        this.keyField = new Ext.form.TextField({
            applyTo: this.fieldID,
            id: this.fieldID,
            style: "margin-top:4px; float:left;",
            hideLabel: true,
            height: 22,
            allowBlank: false,
            width: 110
        });

        new Ext.KeyNav(this.fieldID, {
            "enter": function () {
                func.Submit();
            }
        });

        Ext.get(Ext.getDom(this.fieldID).parentNode)
			.createChild([{
			    tag: "img",
			    id: "SearchClick",
			    style: "vertical-align:middle; margin-top:4px; cursor:pointer",
			    alt: "開始搜尋",
			    title: "開始搜尋",
			    src: "images/normal/head-tbar-go.png"
			}]);

        Ext.EventManager.addListener("SearchClick", "click", function (event, obj) { func.Submit(); });
    },

    Submit: function () {
        var link = new Array();

        try {
            if (!this.keyField.isValid()) throw Error("搜尋關鍵字必須填寫。");
            link.push("Default.aspx?u=Products&fn=Search&kwd=");
            link.push(encodeURIComponent(Base64.encode(this.keyField.getValue())));
            location.href = link.join('');
        }
        catch (e) {
            Util.MsgBox(true, e.message);
        }
    }
};
