﻿/**
 * 會員相關處理
 * @include "Util.js"
 * @class
 */
var Member = {

    ow: null,
    lock: false,
    form: null,

    /**
    * 初始化登入頁面
    */
    InitLoginForm: function () {
        Ext.onReady(function () {
            Ext.BLANK_IMAGE_URL = "../include/ext/resources/images/default/s.gif";
            Ext.QuickTips.init();

            new Ext.form.TextField({
                applyTo: "account",
                id: "act",
                hideLabel: true,
                emptyText: "請輸入您的帳號",
                allowBlank: false,
                maxLength: 15,
                width: 180
            });

            new Ext.form.TextField({
                applyTo: "password",
                id: "pwd",
                allowBlank: false,
                hideLabel: true,
                style: "margin:5px 0",
                inputType: "password",
                maxLength: 20,
                width: 180
            });

            new Ext.form.TextField({
                applyTo: "vcode",
                id: "vcode",
                hideLabel: true,
                allowBlank: false,
                maxLength: 5,
                width: 75
            });

            Member.ReloadVCode("vc");
            Ext.get("reload").addListener("click", function () { Member.ReloadVCode("vc") });

            new Ext.KeyNav("password", {
                "enter": function () {
                    Member.DoLogin();
                }
            });

            new Ext.KeyNav("vcode", {
                "enter": function () {
                    Member.DoLogin();
                }
            });

            new Ext.KeyNav("toLogin", {
                "enter": function () {
                    Member.DoLogin();
                }
            });
        });
    },

    /**
    *重新整理驗證碼
    */
    ReloadVCode: function (id) {
        Ext.get(id)
        .update("<img src=\"ValidCode.ashx?nocache=" + new Date() + "\" style=\"vertical-align:middle; width:85px; height:35px; margin-left:8px;\" alt=\"驗證碼\" />")
        .fadeIn({ endOpacity: 1, duration: .5, remove: false, concurrent: true });
    },

    /**
    * 登入系統
    */
    DoLogin: function () {
        if (this.lock) return;

        var act = Ext.ComponentMgr.get("act");
        var pwd = Ext.ComponentMgr.get("pwd");
        var vcode = Ext.ComponentMgr.get("vcode");

        try {
            if ((act == undefined || act.getValue() == '') || (pwd == undefined || pwd.getValue() == '')) throw Error("帳號或密碼未填寫");
            Ext.Ajax.request({
                url: "Handler.aspx?fn=Login",
                params: { act: act.getValue(), pwd: pwd.getValue(), vcode: vcode.getValue() },
                mode: "POST",
                scope: this,
                success: function (response, options) {
                    var o = Ext.decode(response.responseText);
                    if (o.success == true) {
                        act.setDisabled(true);
                        pwd.setDisabled(true);
                        vcode.setDisabled(true);
                        Util.MsgBox(false, "登入成功，歡迎回來！", function () {
                            location.href = "Member.aspx";
                            this.lock = true;
                        });
                    } else {
                        Util.MsgBox(true, o.msg);
                        Member.ReloadVCode("vc");
                        Ext.ComponentMgr.get("pwd").reset();
                        Ext.ComponentMgr.get("vcode").reset();
                    }
                },
                failure: function () {
                    Util.MsgBox(true, "非常抱歉!系統忙碌中，請稍後再試。");
                    Member.ReloadVCode("vc");
                    Ext.ComponentMgr.get("pwd").reset();
                    Ext.ComponentMgr.get("vcode").reset();
                }
            });
        }
        catch (e) {
            Util.MsgBox(true, e.message);
        }
    },

    /**
    * 會員登出
    */
    Logout: function () {
        Ext.Ajax.request({
            url: "Handler.aspx?fn=Logoff",
            mode: "GET",
            scope: this,
            success: function (response, options) {
                location.href = "Default.aspx"
            },
            failure: function () {
                Util.MsgBox(true, "非常抱歉!系統忙碌中，請稍後再試。");
            }
        });
    },

    /**
    * 初始化加入表單
    */
    InitJoinForm: function () {
        var year, store, storeCounty, storeArea;

        Ext.onReady(function () {
            Ext.QuickTips.init();
            Ext.BLANK_IMAGE_URL = "../include/ext/resources/images/default/s.gif";

            //註冊客戶端密碼驗證
            Ext.apply(Ext.form.VTypes, {
                checkpwd: function (val, field) {
                    if (field.initialPassField) {
                        var checkfield = Ext.getCmp(field.initialPassField);
                        return (val == checkfield.getValue());
                    }
                    else {
                        return true;
                    }
                },
                checkpwdText: "登入密碼與確認密碼不相符。"
            });

            //註冊客戶端帳號驗證
            Ext.apply(Ext.form.VTypes, {
                onlyEng: function (v) {
                    var reg = /[\W]/g;
                    return !(reg.test(v));
                },
                onlyEngText: "帳號不可為中文字。"
            });

            //建立縣市資料集
            storeCounty = new Ext.data.JsonStore({
                url: "Handler.aspx?fn=GetCounty",
                totalProperty: "total",
                root: "data",
                fields: ["value", "text"]
            });

            //建立地區資料集
            storeArea = new Ext.data.JsonStore({
                url: "Handler.aspx?fn=GetArea",
                totalProperty: "total",
                root: "data",
                fields: ["value", "text"]
            });


            //建立生日年份資料集
            year = new Array;
            for (var i = 0; i <= parseInt(new Date().getFullYear()) - 1930; i++) {
                year.push(new Array(1930 + i, 1930 + i));
            }

            store = new Ext.data.SimpleStore({
                fields: ['value', 'text'],
                data: year
            });


            Member.form = new Ext.BasicForm("JoinForm");
            Member.form.add(
				new Ext.form.TextField({
				    id: "act",
				    applyTo: "act",
				    hideLabel: true,
				    emptyText: "輸入您的帳號",
				    maxLength: 15,
				    minLength: 4,
				    allowBlank: false,
				    anchor: "100%",
				    vtype: "onlyEng"
				}),
				new Ext.form.TextField({
				    id: "name",
				    applyTo: "name",
				    hideLabel: true,
				    emptyText: "輸入您的姓名",
				    maxLength: 15,
				    minLength: 2,
				    allowBlank: false,
				    anchor: "100%"
				}),
				new Ext.form.TextField({
				    id: "nick",
				    applyTo: "nick",
				    hideLabel: true,
				    emptyText: "輸入您的暱稱",
				    maxLength: 30,
				    minLength: 2,
				    allowBlank: true,
				    anchor: "100%"
				}),
				new Ext.form.TextField({
				    id: "pwd",
				    applyTo: "pwd",
				    hideLabel: true,
				    inputType: "password",
				    maxLength: 20,
				    minLength: 6,
				    allowBlank: false,
				    anchor: "100%"
				}),
				new Ext.form.TextField({
				    id: "pwdChk",
				    applyTo: "pwdChk",
				    hideLabel: true,
				    inputType: 'password',
				    maxLength: 20,
				    minLength: 6,
				    allowBlank: false,
				    initialPassField: 'pwd',
				    vtype: 'checkpwd',
				    anchor: '100%'
				}),
				new Ext.form.TextField({
				    id: "email",
				    applyTo: "email",
				    hideLabel: true,
				    emptyText: "輸入您的電子信箱",
				    maxLength: 128,
				    vtype: "email",
				    allowBlank: false,
				    width: 300
				}),
				new Ext.form.ComboBox({
				    id: "_birthYear",
				    applyTo: "_birthYear",
				    hiddenName: "birthYear",
				    hideLabel: true,
				    triggerAction: "all",
				    mode: "local",
				    displayField: "text",
				    valueField: "value",
				    readOnly: true,
				    style: "margin-top:0px;",
				    width: 60,
				    store: store,
				    value: new Date().getFullYear().toString()
				}),
				new Ext.form.ComboBox({
				    id: "_birthMonth",
				    applyTo: "_birthMonth",
				    hiddenName: "birthMonth",
				    hideLabel: true,
				    triggerAction: "all",
				    mode: "local",
				    displayField: "text",
				    valueField: "value",
				    readOnly: true,
				    style: "margin-top:0px;",
				    width: 60,
				    value: 1,
				    store: new Ext.data.SimpleStore({
				        fields: ["value", "text"],
				        data: [["1", "01"], ["2", "02"], ["3", "03"], ["4", "04"], ["5", "05"], ["6", "06"],
	                    	["7", "07"], ["8", "08"], ["9", "09"], ["10", "10"], ["11", "11"], ["12", "12"]]
				    })
				}),
				new Ext.form.ComboBox({
				    id: "_birthDay",
				    applyTo: "_birthDay",
				    hiddenName: "birthDay",
				    hideLabel: true,
				    triggerAction: "all",
				    mode: "local",
				    displayField: "text",
				    valueField: "value",
				    readOnly: true,
				    style: "margin-top:0px;",
				    width: 60,
				    value: 1,
				    store: new Ext.data.SimpleStore({
				        fields: ["value", "text"],
				        data: [["1", "01"], ["2", "02"], ["3", "03"], ["4", "04"], ["5", "05"], ["6", "06"],
						   ["7", "07"], ["8", "08"], ["9", "09"], ["10", "10"], ["11", "11"], ["12", "12"],
						   ["13", "13"], ["14", "14"], ["15", "15"], ["16", "16"], ["17", "17"], ["18", "18"],
						   ["19", "19"], ["20", "20"], ["21", "21"], ["22", "22"], ["23", "23"], ["24", "24"],
						   ["25", "25"], ["26", "26"], ["27", "27"], ["28", "28"], ["29", "29"], ["30", "30"],
						   ["31", "31"]]
				    })
				}),
				new Ext.form.ComboBox({
				    id: "_sex",
				    applyTo: "_sex",
				    hiddenName: "sex",
				    hideLabel: true,
				    triggerAction: "all",
				    mode: "local",
				    displayField: "text",
				    valueField: "value",
				    readOnly: true,
				    style: "margin-top:0px;",
				    width: 60,
				    value: 0,
				    store: new Ext.data.SimpleStore({
				        fields: ["value", "text"],
				        data: [["0", "男"], ["1", "女"]]
				    })
				}),
				new Ext.form.TextField({
				    id: "tel",
				    applyTo: "tel",
				    hideLabel: true,
				    maxLength: 20,
				    minLength: 6,
				    allowBlank: false,
				    invalidText: "聯絡電話必須至少填寫一支。",
				    anchor: "100%"
				}),
				new Ext.form.TextField({
				    id: "tel2",
				    applyTo: "tel2",
				    hideLabel: true,
				    maxLength: 20,
				    minLength: 6,
				    anchor: "100%"
				}),
				new Ext.form.ComboBox({
				    id: "_county",
				    applyTo: "_county",
				    hiddenName: "county",
				    hideLabel: true,
				    triggerAction: "all",
				    emptyText: "選擇縣市..",
				    mode: "remote",
				    displayField: "text",
				    valueField: "value",
				    readOnly: true,
				    allowBlank: false,
				    style: "margin-top:0px;",
				    width: 90,
				    store: storeCounty,
				    listeners: {
				        "select": function (combo) {
				            var cmp = Ext.ComponentMgr.get("_area");
				            var dom = Ext.getDom("zip");

				            storeArea.load({
				                params: { cid: combo.getValue() },
				                callback: function (r, options, success) {
				                    if (success == true) {
				                        if (storeArea.getCount() > 0) {
				                            cmp.setValue(r[0].get('value'));
				                            dom.value = r[0].get('value').split(':')[1];
				                            cmp.setDisabled(false);
				                        }
				                    }
				                    else {
				                        Util.MsgBox(true, "鄉鎮區域資料載入失敗，系統忙碌請稍候再試！");
				                    }
				                }
				            });
				        }
				    }
				}),
				new Ext.form.ComboBox({
				    id: "_area",
				    applyTo: "_area",
				    hiddenName: "area",
				    hideLabel: true,
				    triggerAction: "all",
				    emptyText: "鄉鎮區域..",
				    mode: "local",
				    displayField: "text",
				    valueField: "value",
				    readOnly: true,
				    disabled: true,
				    allowBlank: false,
				    style: "margin-top:0px;",
				    width: 90,
				    store: storeArea,
				    listeners: {
				        "select": function (combo) {
				            var dom = Ext.getDom("zip");
				            if (dom != undefined) {
				                dom.value = combo.getValue().split(':')[1];
				            }
				        }
				    }
				}),
				new Ext.form.TextField({
				    id: "zip",
				    applyTo: "zip",
				    allowBlank: false,
				    readOnly: true,
				    width: 50,
				    fieldClass: "field_null",
				    value: ''
				}),
				new Ext.form.TextField({
				    id: "address",
				    applyTo: "address",
				    style: "margin-top:5px",
				    hideLabel: true,
				    emptyText: "輸入您的通訊地址",
				    maxLength: 128,
				    allowBlank: false,
				    width: 400
				}),
				new Ext.form.ComboBox({
				    id: "_edm",
				    applyTo: "_edm",
				    hiddenName: "edm",
				    hideLabel: true,
				    triggerAction: "all",
				    mode: "local",
				    displayField: "text",
				    valueField: "value",
				    readOnly: true,
				    style: "margin-top:0px;",
				    width: 80,
				    value: "1",
				    store: new Ext.data.SimpleStore({
				        fields: ["value", "text"],
				        data: [["0", "不訂閱"], ["1", "訂閱"]]
				    })
				}),
                new Ext.form.TextField({
                    applyTo: "vcode",
                    id: "vcode",
                    hideLabel: true,
                    allowBlank: false,
                    maxLength: 5,
                    width: 75
                })
			);

            Member.ReloadVCode("vc");
            Ext.get("reload").addListener("click", function () { Member.ReloadVCode("vc") });
        });
    },

    /**
    * 送出加入會員資料
    */
    DoJoin: function () {
        if (Member.form != null && Member.form.isValid()) {
            Member.form.submit({
                method: "POST",
                url: "Handler.aspx?fn=AddMember",
                waitTitle: "系統訊息",
                waitMsg: "申請資料傳送中，請稍候...",
                reset: false,
                success: function (form, action) {
                    var o, myMask;
                    try {
                        o = Ext.util.JSON.decode(action.response.responseText);
                        Util.MsgBox(false, o.msg, function () {
                            var myMask = new Ext.LoadMask(Ext.getBody(), { msg: "資料處理中，請稍候..." });
                            myMask.show();
                            location.href = "Member.aspx?u=Join3"
                        });
                    } catch (e) {
                        Util.MsgBox(true, e.message);
                    }
                },
                failure: function (form, action) {
                    var o;
                    try {
                        o = Ext.util.JSON.decode(action.response.responseText);
                        throw Error(o.msg);
                    } catch (e) {
                        Util.MsgBox(true, e.message);
                    }
                }
            });
        }
    },

    /**
    * 離開加入會員
    */
    ExitJoinForm: function () {
        Util.ConfirmBox("您確定要離開返回首頁", function (request) {
            if (request != "yes") return;
            location.href = "Default.aspx";
        });
    },

    /**
    * 初始化更新表單資料
    */
    InitUpdateData: function () {
        Ext.Ajax.request({
            url: "Handler.aspx?fn=GetMemberData",
            mode: "GET",
            scope: this,
            success: function (response, options) {
                var o;
                try {
                    o = Ext.util.JSON.decode(response.responseText);
                    if (!o.success) throw Error(o.msg);
                    this.InitUpdateForm(o.data);
                }
                catch (e) {
                    Util.MsgBox(true, e.message, function () {
                        location.href = "Member.aspx";
                    });
                }
            },
            failure: function () {
                Util.MsgBox(true, "非常抱歉!系統忙碌中，請稍後再試。", function () {
                    location.href = "Member.aspx";
                });
            }
        });
    },

    /**
    * 初始化更新表單
    */
    InitUpdateForm: function (o) {
        var year, store, storeCounty, storeArea;

        Ext.onReady(function () {
            Ext.QuickTips.init();
            Ext.BLANK_IMAGE_URL = "../include/ext/resources/images/default/s.gif";


            //建立縣市資料集
            storeCounty = new Ext.data.JsonStore({
                url: "Handler.aspx?fn=GetCounty",
                totalProperty: "total",
                root: "data",
                fields: ["value", "text"]
            });

            //建立地區資料集
            storeArea = new Ext.data.JsonStore({
                url: "Handler.aspx?fn=GetArea",
                totalProperty: "total",
                root: "data",
                fields: ["value", "text"]
            });


            //建立生日年份資料集
            year = new Array;
            for (var i = 0; i <= parseInt(new Date().getFullYear()) - 1930; i++) {
                year.push(new Array(1930 + i, 1930 + i));
            }

            store = new Ext.data.SimpleStore({
                fields: ['value', 'text'],
                data: year
            });


            //開始建立表單
            Ext.getDom("titleName").value = o.name;

            Member.form = new Ext.BasicForm("UpdateForm");
            Member.form.add(
				new Ext.form.TextField({
				    id: "act",
				    applyTo: "act",
				    hideLabel: true,
				    allowBlank: false,
				    readOnly: true,
				    anchor: "100%",
				    fieldClass: "field_null",
				    value: o.account || ''
				}),
				new Ext.form.TextField({
				    id: "name",
				    applyTo: "name",
				    hideLabel: true,
				    emptyText: "輸入您的姓名",
				    maxLength: 15,
				    minLength: 2,
				    allowBlank: false,
				    anchor: "100%",
				    value: o.name || ''
				}),
				new Ext.form.TextField({
				    id: "nick",
				    applyTo: "nick",
				    hideLabel: true,
				    emptyText: "輸入您的暱稱",
				    maxLength: 30,
				    minLength: 2,
				    allowBlank: true,
				    anchor: "100%",
				    value: o.nick || ''
				}),
				new Ext.form.TextField({
				    id: "email",
				    applyTo: "email",
				    hideLabel: true,
				    emptyText: "輸入您的電子信箱",
				    maxLength: 128,
				    vtype: "email",
				    allowBlank: false,
				    width: 300,
				    value: o.email || ''
				}),
				new Ext.form.ComboBox({
				    id: "_birthYear",
				    applyTo: "_birthYear",
				    hiddenName: "birthYear",
				    hideLabel: true,
				    triggerAction: "all",
				    mode: "local",
				    displayField: "text",
				    valueField: "value",
				    readOnly: true,
				    style: "margin-top:0px;",
				    width: 60,
				    store: store,
				    value: o.birthYear || new Date().getFullYear().toString()
				}),
				new Ext.form.ComboBox({
				    id: "_birthMonth",
				    applyTo: "_birthMonth",
				    hiddenName: "birthMonth",
				    hideLabel: true,
				    triggerAction: "all",
				    mode: "local",
				    displayField: "text",
				    valueField: "value",
				    readOnly: true,
				    style: "margin-top:0px;",
				    width: 60,
				    value: o.birthMonth || 1,
				    store: new Ext.data.SimpleStore({
				        fields: ["value", "text"],
				        data: [["1", "01"], ["2", "02"], ["3", "03"], ["4", "04"], ["5", "05"], ["6", "06"],
	                    	["7", "07"], ["8", "08"], ["9", "09"], ["10", "10"], ["11", "11"], ["12", "12"]]
				    })
				}),
				new Ext.form.ComboBox({
				    id: "_birthDay",
				    applyTo: "_birthDay",
				    hiddenName: "birthDay",
				    hideLabel: true,
				    triggerAction: "all",
				    mode: "local",
				    displayField: "text",
				    valueField: "value",
				    readOnly: true,
				    style: "margin-top:0px;",
				    width: 60,
				    value: o.birthDay || 1,
				    store: new Ext.data.SimpleStore({
				        fields: ["value", "text"],
				        data: [["1", "01"], ["2", "02"], ["3", "03"], ["4", "04"], ["5", "05"], ["6", "06"],
						   ["7", "07"], ["8", "08"], ["9", "09"], ["10", "10"], ["11", "11"], ["12", "12"],
						   ["13", "13"], ["14", "14"], ["15", "15"], ["16", "16"], ["17", "17"], ["18", "18"],
						   ["19", "19"], ["20", "20"], ["21", "21"], ["22", "22"], ["23", "23"], ["24", "24"],
						   ["25", "25"], ["26", "26"], ["27", "27"], ["28", "28"], ["29", "29"], ["30", "30"],
						   ["31", "31"]]
				    })
				}),
				new Ext.form.ComboBox({
				    id: "_sex",
				    applyTo: "_sex",
				    hiddenName: "sex",
				    hideLabel: true,
				    triggerAction: "all",
				    mode: "local",
				    displayField: "text",
				    valueField: "value",
				    readOnly: true,
				    style: "margin-top:0px;",
				    width: 60,
				    value: o.sex || 0,
				    store: new Ext.data.SimpleStore({
				        fields: ["value", "text"],
				        data: [["0", "男"], ["1", "女"]]
				    })
				}),
				new Ext.form.TextField({
				    id: "tel",
				    applyTo: "tel",
				    hideLabel: true,
				    maxLength: 20,
				    minLength: 6,
				    allowBlank: false,
				    invalidText: "聯絡電話必須至少填寫一支。",
				    anchor: "100%",
				    value: o.tel || ''
				}),
				new Ext.form.TextField({
				    id: "tel2",
				    applyTo: "tel2",
				    hideLabel: true,
				    maxLength: 20,
				    minLength: 6,
				    anchor: "100%",
				    value: o.tel2 || ''
				}),
				new Ext.form.ComboBox({
				    id: "_county",
				    applyTo: "_county",
				    hiddenName: "county",
				    hideLabel: true,
				    triggerAction: "all",
				    emptyText: "選擇縣市..",
				    mode: "local",
				    displayField: "text",
				    valueField: "value",
				    readOnly: true,
				    allowBlank: false,
				    style: "margin-top:0px;",
				    width: 90,
				    store: storeCounty,
				    listeners: {
				        "select": function (combo) {
				            var cmp = Ext.ComponentMgr.get("_area");
				            var dom = Ext.getDom("zip");

				            storeArea.load({
				                params: { cid: combo.getValue() },
				                callback: function (r, options, success) {
				                    if (success == true) {
				                        if (storeArea.getCount() > 0) {
				                            cmp.setValue(r[0].get('value'));
				                            dom.value = r[0].get('value').split(':')[1];
				                            cmp.setDisabled(false);
				                        }
				                    }
				                    else {
				                        Util.MsgBox(true, "鄉鎮區域資料載入失敗，系統忙碌請稍候再試！");
				                    }
				                }
				            });
				        }
				    }
				}),
				new Ext.form.ComboBox({
				    id: "_area",
				    applyTo: "_area",
				    hiddenName: "area",
				    hideLabel: true,
				    triggerAction: "all",
				    emptyText: "鄉鎮區域..",
				    mode: "local",
				    displayField: "text",
				    valueField: "value",
				    readOnly: true,
				    disabled: true,
				    allowBlank: false,
				    style: "margin-top:0px;",
				    width: 90,
				    store: storeArea,
				    listeners: {
				        "select": function (combo) {
				            var dom = Ext.getDom("zip");
				            if (dom != undefined) {
				                dom.value = combo.getValue().split(':')[1];
				            }
				        }
				    }
				}),
				new Ext.form.TextField({
				    id: "zip",
				    applyTo: "zip",
				    allowBlank: false,
				    readOnly: true,
				    width: 50,
				    fieldClass: "field_null",
				    listeners: {
				        "render": function (field) {
				            var m = o;
				            var c = Ext.ComponentMgr.get("_county");
				            var c1 = Ext.ComponentMgr.get("_area");
				            if (m.county == null) return;
				            storeCounty.load({
				                callback: function (r, o, success) {
				                    if (success && r.length > 0) {
				                        c.setValue(m.county);
				                        c1.setDisabled(false);

				                        //讀取鄉鎮區域
				                        storeArea.load({
				                            params: { cid: m.county },
				                            callback: function (r, options, success) {
				                                if (success && r.length > 0) {
				                                    c1.setValue(m.area);
				                                    field.setValue(m.zip);
				                                }
				                            }
				                        });
				                    }
				                }
				            });
				        }
				    }
				}),
				new Ext.form.TextField({
				    id: "address",
				    applyTo: "address",
				    style: "margin-top:5px",
				    hideLabel: true,
				    emptyText: "輸入您的通訊地址",
				    maxLength: 128,
				    allowBlank: false,
				    width: 400,
				    value: o.address || ''
				}),
				new Ext.form.ComboBox({
				    id: "_edm",
				    applyTo: "_edm",
				    hiddenName: "edm",
				    hideLabel: true,
				    triggerAction: "all",
				    mode: "local",
				    displayField: "text",
				    valueField: "value",
				    readOnly: true,
				    style: "margin-top:0px;",
				    width: 80,
				    value: o.order || "1",
				    store: new Ext.data.SimpleStore({
				        fields: ["value", "text"],
				        data: [["0", "不訂閱"], ["1", "訂閱"]]
				    })
				})/*,
                new Ext.form.TextField({
                    applyTo: "vcode",
                    id: "vcode",
                    hideLabel: true,
                    allowBlank: false,
                    maxLength: 5,
                    width: 75
                })*/
			);

            //Member.ReloadVCode("vc");
            //Ext.get("reload").addListener("click", function () { Member.ReloadVCode("vc") });
        })
    },

    /**
    * 送出更新會員資料
    */
    DoUpdate: function () {
        if (Member.form != null && Member.form.isValid()) {
            Member.form.submit({
                method: "POST",
                url: "Handler.aspx?fn=UpdateMember",
                waitTitle: "系統訊息",
                waitMsg: "資料傳送中，請稍候...",
                reset: false,
                success: function (form, action) {
                    var o, myMask;
                    try {
                        o = Ext.util.JSON.decode(action.response.responseText);
                        Util.MsgBox(false, o.msg, function () {
                            var myMask = new Ext.LoadMask(Ext.getBody(), { msg: "資料處理中，請稍候..." });
                            myMask.show();
                            location.href = "Member.aspx?u=MyProfile"
                        });
                    } catch (e) {
                        Util.MsgBox(true, e.message);
                    }
                },
                failure: function (form, action) {
                    var o;
                    try {
                        o = Ext.util.JSON.decode(action.response.responseText);
                        throw Error(o.msg);
                    } catch (e) {
                        Util.MsgBox(true, e.message);
                    }
                }
            });
        }
    },

    /**
    * 初始化忘記密碼頁面
    */
    InitForgotForm: function () {
        Ext.onReady(function () {
            Ext.BLANK_IMAGE_URL = "../include/ext/resources/images/default/s.gif";
            Ext.QuickTips.init();

            new Ext.form.TextField({
                applyTo: "account",
                id: "act",
                hideLable: true,
                emptyText: "請輸入您的帳號",
                allowBlank: false,
                maxLength: 15,
                width: 235
            });

            new Ext.form.TextField({
                applyTo: "email",
                id: "email",
                allowBlank: false,
                hideLabel: true,
                emptyText: "請輸入您的電子郵件",
                maxLength: 80,
                width: 235,
                vtype: "email"
            });

            new Ext.form.TextField({
                applyTo: "vcode",
                id: "vcode",
                hideLabel: true,
                allowBlank: false,
                maxLength: 5,
                width: 75
            });

            Member.ReloadVCode("vc");
            Ext.get("reload").addListener("click", function () { Member.ReloadVCode("vc") });

            new Ext.KeyNav("email", {
                "enter": function () {
                    Member.DoQueryForgot();
                }
            });

            new Ext.KeyNav("vcode", {
                "enter": function () {
                    Member.DoQueryForgot();
                }
            });

            new Ext.KeyNav("Query", {
                "enter": function () {
                    Member.DoQueryForgot();
                }
            });
        });
    },

    /**
    * 送出忘記密碼
    */
    DoQueryForgot: function () {
        if (this.lock) return;

        var act = Ext.ComponentMgr.get("act");
        var email = Ext.ComponentMgr.get("email");
        var vcode = Ext.ComponentMgr.get("vcode");

        try {
            if (!act.isValid(true) || !email.isValid(true) || !vcode.isValid(true)) throw Error("資料欄位請確實填寫。");
            Ext.Ajax.request({
                url: "Handler.aspx?fn=ForgotPwd",
                params: { act: act.getValue(), email: email.getValue(), vcode: vcode.getValue() },
                mode: "POST",
                scope: this,
                success: function (response, options) {
                    var o = Ext.decode(response.responseText);
                    if (o.success == true) {
                        act.reset();
                        email.reset();
                        vcode.reset();
                        Member.ReloadVCode("vc");
                        Util.MsgBox(false, "查詢密碼信件已寄出至您的信箱！", function () {
                            this.lock = true;
                        });
                    } else {
                        Util.MsgBox(true, o.msg);
                        Member.ReloadVCode("vc");
                    }
                },
                failure: function () {
                    Util.MsgBox(true, "非常抱歉!系統忙碌中，請稍後再試。");
                }
            });
        } catch (e) {
            Util.MsgBox(true, e.message);
        }
    },

    /**
    * 初始化變更密碼頁面
    */
    InitChangePwdForm: function () {
        Ext.onReady(function () {
            Ext.BLANK_IMAGE_URL = "../include/ext/resources/images/default/s.gif";
            Ext.QuickTips.init();

            //註冊客戶端密碼驗證
            Ext.apply(Ext.form.VTypes, {
                checkpwd: function (val, field) {
                    if (field.initialPassField) {
                        var checkfield = Ext.getCmp(field.initialPassField);
                        return (val == checkfield.getValue());
                    }
                    else {
                        return true;
                    }
                },
                checkpwdText: "新密碼與確認密碼不相符。"
            });

            new Ext.form.TextField({
                applyTo: "oldPwd",
                id: "oldPwd",
                hideLable: true,
                allowBlank: false,
                minLength: 6,
                maxLength: 20,
                width: 235
            });

            new Ext.form.TextField({
                applyTo: "pwd",
                id: "pwd",
                allowBlank: false,
                hideLabel: true,
                inputType: "password",
                minLength: 6,
                maxLength: 20,
                width: 235
            });

            new Ext.form.TextField({
                applyTo: "pwdChk",
                id: "pwdChk",
                allowBlank: false,
                hideLabel: true,
                inputType: "password",
                initialPassField: "pwd",
                vtype: "checkpwd",
                minLength: 6,
                maxLength: 20,
                width: 235
            });

            /* new Ext.form.TextField({
            applyTo: "vcode",
            id: "vcode",
            hideLabel: true,
            allowBlank: false,
            maxLength: 5,
            width: 75
            });*/

            // Member.ReloadVCode("vc");
            // Ext.get("reload").addListener("click", function () { Member.ReloadVCode("vc") });

            new Ext.KeyNav("Submit", {
                "enter": function () {
                    Member.DoChangePwd();
                }
            });
        });
    },

    /**
    * 進行密碼變更
    */
    DoChangePwd: function () {
        if (this.lock) return;

        var oldPwd = Ext.ComponentMgr.get("oldPwd");
        var pwd = Ext.ComponentMgr.get("pwd");
        var pwdChk = Ext.ComponentMgr.get("pwdChk");
        // var vcode = Ext.ComponentMgr.get("vcode");

        try {
            if (!oldPwd.isValid(true)) throw Error("原密碼欄位不正確。");
            if (!pwd.isValid(true) || !pwdChk.isValid(true)) throw Error("新密碼或確認密碼欄位不正確。");
            //  if (!vcode.isValid(true)) throw Error("驗證碼未填寫。");

            Ext.Ajax.request({
                url: "Handler.aspx?fn=ChangePwd",
                params: { oldPwd: oldPwd.getValue(), pwd: pwd.getValue(), pwdChk: pwdChk.getValue()/*, vcode: vcode.getValue()*/ },
                mode: "POST",
                scope: this,
                success: function (response, options) {
                    var o = Ext.decode(response.responseText);
                    if (o.success == true) {
                        oldPwd.reset();
                        pwd.reset();
                        pwdChk.reset();
                        Util.MsgBox(false, "成功變更密碼設定！", function () {
                            this.lock = true;
                            location.href = '/Member.aspx';
                        });
                    } else {
                        Util.MsgBox(true, o.msg);
                    }
                },
                failure: function () {
                    Util.MsgBox(true, "非常抱歉!系統忙碌中，請稍後再試。");
                }
            });
        } catch (e) {
            Util.MsgBox(true, e.message);
        }
    },

    /**
    * 紅利積點
    */
    InitBonusPage: function () {
        var i, ol = Ext.DomQuery.select("tr[name=order]");
        try {
            if (ol == null) throw Error();
            for (i = 0; i < ol.length; i++) {
                Ext.get(ol[i]).on("click", function () {
                    if (this.id != undefined && this.id != null)
                        Member.OrderDetail(this.id.substring(2, this.id.length));
                });
            }
        }
        catch (e) {
            return;
        }
        finally {
            i = null;
            ol = null;
        }
    },


    queryType: null,

    /**
    * 訂單查詢
    */
    InitOrderQueryPage: function () {
        Ext.onReady(function () {
            Ext.BLANK_IMAGE_URL = "../include/ext/resources/images/default/s.gif";
            Ext.QuickTips.init();

            //編號查詢
            new Ext.form.TextField({
                id: "orderNum",
                hideLabel: true,
                maxLength: 50,
                disabled: true,
                style: "margin-left:12px;",
                renderTo: "OrderNumBox",
                width: 250
            });

            //日期查詢
            new Ext.Container({
                autoEl: {},
                renderTo: "DateQuery",
                layout: "column",
                style: "margin:4px 0 0 0",
                items: [{
                    xtype: "box",
                    autoEl: {
                        tag: "div",
                        html: "自",
                        style: "line-height:180%; margin:0 5px;"
                    }
                }, {
                    xtype: "datefield",
                    id: "sdate",
                    hideLabel: true,
                    readOnly: true,
                    disabled: true,
                    width: 100,
                    minValue: "2009/06/01",
                    listeners: {
                        "change": function () {
                            Ext.ComponentMgr.get("edate").setMinValue(this.getValue());
                            Ext.ComponentMgr.get("edate").setValue(this.getValue());
                        },
                        "blur": function () {
                            if (Ext.ComponentMgr.get("Category2").checked && this.getValue() == '') {
                                this.markInvalid("請選擇啟始日期");
                            }
                            else {
                                this.clearInvalid();
                            }
                        }
                    }
                }, {
                    xtype: "box",
                    autoEl: {
                        tag: "div",
                        html: "起至",
                        style: "line-height:180%; margin:0 5px;"
                    }
                }, {
                    xtype: "datefield",
                    id: "edate",
                    hideLabel: true,
                    readOnly: true,
                    disabled: true,
                    width: 100,
                    maxValue: new Date(),
                    listeners: {
                        "change": function () {
                            Ext.ComponentMgr.get("sdate").setMaxValue(this.getValue());
                        },
                        'blur': function () {
                            if (Ext.ComponentMgr.get("Category2").checked && this.getValue() == '') {
                                this.markInvalid("請選擇結束日期");
                            }
                            else {
                                this.clearInvalid();
                            }
                        }
                    }
                }]
            });

            //類型
            new Ext.form.RadioGroup({
                id: "QueryCategory",
                renderTo: "Categories",
                hideLabel: true,
                allowBlank: false,
                width: "100%",
                columns: 3,
                items: [{
                    name: "Category",
                    id: "Category0",
                    inputValue: '0',
                    boxLabel: "所有訂單",
                    autoWidth: true
                }, {
                    name: "Category",
                    id: "Category1",
                    inputValue: '1',
                    boxLabel: "依訂單編號查詢",
                    autoWidth: true
                }, {
                    name: "Category",
                    id: "Category2",
                    inputValue: '2',
                    boxLabel: "依日期區間查詢",
                    autoWidth: true
                }],
                listeners: {
                    "change": function (r, chk) {
                        Member.queryType = chk.inputValue;
                        switch (chk.inputValue) {
                            case '0':
                                Ext.ComponentMgr.get("orderNum").setDisabled(true);
                                Ext.ComponentMgr.get("edate").setDisabled(true);
                                Ext.ComponentMgr.get("sdate").setDisabled(true);
                                Ext.ComponentMgr.get("orderNum").clearInvalid();
                                Ext.ComponentMgr.get("edate").clearInvalid();
                                Ext.ComponentMgr.get("sdate").clearInvalid();
                                Ext.ComponentMgr.get("edate").setValue();
                                Ext.ComponentMgr.get("sdate").setValue();
                                Ext.ComponentMgr.get("orderNum").setValue();
                                break;

                            case '1':
                                Ext.ComponentMgr.get("orderNum").setDisabled(false);
                                Ext.ComponentMgr.get("edate").setDisabled(true);
                                Ext.ComponentMgr.get("sdate").setDisabled(true);
                                Ext.ComponentMgr.get("edate").clearInvalid();
                                Ext.ComponentMgr.get("sdate").clearInvalid();
                                Ext.ComponentMgr.get("edate").setValue();
                                Ext.ComponentMgr.get("sdate").setValue();
                                break;

                            case '2':
                                Ext.ComponentMgr.get("edate").setDisabled(false);
                                Ext.ComponentMgr.get("sdate").setDisabled(false);
                                Ext.ComponentMgr.get("orderNum").setDisabled(true);
                                Ext.ComponentMgr.get("orderNum").clearInvalid();
                                Ext.ComponentMgr.get("orderNum").setValue();
                                break;
                        }
                    },
                    "render": function () {
                        if (Member.queryType == null || (Member.queryType != '0' && Member.queryType != '1' && Member.queryType != '2'))
                            Member.queryType = '0';

                        if (Member.queryType == '2') {
                            Ext.ComponentMgr.get("Category2").setValue(true);
                            Ext.ComponentMgr.get("edate").setDisabled(false);
                            Ext.ComponentMgr.get("sdate").setDisabled(false);
                        }
                        else if (Member.queryType == '1') {
                            Ext.ComponentMgr.get("Category1").setValue(true);
                            Ext.ComponentMgr.get("orderNum").setDisabled(false);
                        }
                        else {
                            Ext.ComponentMgr.get("Category0").setValue(true);
                        }
                    }
                }
            })
        });
    },

    /**
    * 送出訂單查詢
    */
    SubmitOrderQuery: function () {
        var sel = Ext.ComponentMgr.get("Category1").getGroupValue();

        if (sel == '1' && Ext.ComponentMgr.get("orderNum").getValue() == '') {
            Ext.ComponentMgr.get("orderNum").markInvalid("使用訂單編號查詢,訂單編號為必填欄位!");
            return;
        }
        else if (sel == '2') {
            if (Ext.ComponentMgr.get("sdate").getValue() == '') {
                Ext.ComponentMgr.get("sdate").markInvalid("請選擇啟始日期!");
                return;
            }

            if (Ext.ComponentMgr.get("edate").getValue() == '') {
                Ext.ComponentMgr.get("edate").markInvalid("請選擇結束日期!");
                return;
            }
        }

        Ext.Ajax.request({
            url: "Handler.aspx?fn=OrderQuery",
            params: {
                category: sel,
                num: Ext.ComponentMgr.get("orderNum").getValue(),
                startDate: Ext.ComponentMgr.get("sdate").getValue(),
                endDate: Ext.ComponentMgr.get("edate").getValue()
            },
            mode: "post",
            scope: this,
            success: function (response, options) {
                var o = '';
                try {
                    o = Ext.decode(response.responseText);
                    if (!o.success) throw Error(o.msg);

                    var text = new Array();
                    text.push("<table cellpadding=\"0\" cellspacing=\"0\" class=\"BonusMyOrder\" summary=\"我的訂單紀錄\">");
                    text.push("<tr>");
                    text.push("<th style=\"width: 15%;\">狀態</th>");
                    text.push("<th style=\"width: 30%;\">訂單編號</th>");
                    text.push("<th style=\"width: 20%;\">下單日期</th>");
                    text.push("<th style=\"width: 20%;\">處理日期</th>");
                    text.push("<th style=\"width: 20%; border-right: 1px solid #9CBF3B;\">出貨日期</th>");
                    text.push("</tr>");
                    text.push(o.data);
                    text.push("</table>")

                    Ext.get("QueryDetail").update(text.join('')).fadeIn({
                        endOpacity: 1,
                        duration: .5,
                        useDisplay: true,
                        remove: false,
                        concurrent: true
                    });
                }
                catch (e) {
                    Ext.get("QueryDetail").fadeOut({ endOpacity: 0, duration: .5, useDisplay: true, remove: false, concurrent: true });
                    Util.MsgBox(true, e.message);
                }
            },
            failure: function () {
                Util.MsgBox(true, "非常抱歉!系統忙碌中，請稍後再試。");
            }
        });
    },

    /**
    * 訂單列表
    */
    OrderDetail: function (id) {

        if (id == null || Member.ow != null) return;

        Member.ow = new Ext.Window({
            id: "orderWindows",
            title: "訂單明細",
            width: 740,
            height: 550,
            modal: true,
            border: false,
            resizable: false,
            animCollapse: true,
            plain: true,
            constrain: true,
            bodyStyle: "border:none;",
            html: "<iframe id=\"newfram\" src=\"Handler.aspx?fn=GetOrderDetail&oid=" + id + "\" frameborder=\"0\" marginheight=\"0\"" +
    			 " marginwidth=\"0\" style=\"width:100%; height:100%; border:none; overflow-x:hidden; background-color:#fff;\"/>",
            listeners: {
                "close": function () {
                    this.destroy();
                },
                "destory": function () {
                    Member.ow = null;
                }
            }
        }).show();
    },

    /**
    * 檢查郵件唯一性
    */
    UniquenessEmail: function (isUpdate) {
        var email = Ext.ComponentMgr.get("email");

        try {
            if ((email == undefined || email == null) || !email.isValid())
                throw Error("電子郵件格式錯誤!");

            Ext.Ajax.request({
                url: "Handler.aspx?fn=CheckEmail",
                params: { email: email.getValue(), isUpdate: isUpdate },
                mode: "POST",
                scope: this,
                success: function (response, options) {
                    var o = Ext.decode(response.responseText);
                    if (o.success == true) {
                        Util.MsgBox(false, o.msg);
                    } else {
                        Util.MsgBox(true, o.msg);
                        email.markInvalid("已有相同的電子郵件存在。", true);
                    }
                },
                failure: function () {
                    Util.MsgBox(true, "非常抱歉!系統忙碌中，請稍後再試。");
                }
            });
        }
        catch (e) {
            Util.MsgBox(true, e.message);
        }
    },

    /**
    * 檢查帳號唯一性
    */
    UniquenessAccount: function () {
        var act = Ext.ComponentMgr.get("act");

        try {
            if ((act == undefined || act == null) || !act.isValid())
                throw Error("錯誤的帳號格式!");

            Ext.Ajax.request({
                url: "Handler.aspx?fn=CheckAccount",
                params: { account: act.getValue() },
                mode: "POST",
                scope: this,
                success: function (response, options) {
                    var o = Ext.decode(response.responseText);
                    if (o.success == true) {
                        Util.MsgBox(false, o.msg);
                    } else {
                        Util.MsgBox(true, o.msg);
                        act.markInvalid("已有相同的帳號存在。", true);
                    }
                },
                failure: function () {
                    Util.MsgBox(true, "非常抱歉!系統忙碌中，請稍後再試。");
                }
            });
        }
        catch (e) {
            Util.MsgBox(true, e.message);
        }
    }
};
