﻿/// <reference path="Ext/vswd-ext_2.0.2.js" />
Ext.QuickTips.init();
Ext.BLANK_IMAGE_URL = "Ext/resources/images/vista/s.gif";
Ext.form.Field.prototype.msgTarget = "title";
LoginWindow = Ext.extend(Ext.Window,
{ title: "系统登录", width: 280, height: 210, frame: true, plain: true, resizable: false,
    minimizable: false, maximizable: false, collapsible: true, border: false, closable: false,
    defaults: { border: false },
    buttonAlign: "center",
    createFormPanel: function() {
        return new Ext.form.FormPanel({
            bodyStyle: "padding-top:6px ",
            defaultType: "textfield",
            labelAlign: "right",
            labelWidth: 60,
            labelPad: 0,
            frame: true,
            defaults: {
                allowBlank: false,
                width: 158
            },
            items: [{
                id: "businessname",
                name: "businessname",
                fieldLabel: "帐&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号",
                blankText: "帐号不能为空"
            },
            {
                id: "username",
                name: "username",
                fieldLabel: "工&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号",
                blankText: "工号不能为空"
            },
            {
                id: "password",
                name: "password",
                fieldLabel: "密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码",
                blankText: "密码不能为空",
                inputType: "password"
            },
            {
                name: "randcode",
                id: "randcode",
                fieldLabel: "验&nbsp;证&nbsp;&nbsp;码",
                width: 100,
                blankText: "验证码不能为空"
            },
            {
                xtype: "checkbox", hidden: true,
                id: "rememberpassword",
                name: "rememberpassword",
                inputValue: "1",
                boxLabel: "记住密码"
}]
            });
        },
        login: function() {
            var This = this;
            if (this.fp.form.isValid()) {
                this.fp.form.submit({
                    waitMsg: "正在登录......",
                    clientValidation: true,
                    url: "LoginHandler.ashx",
                    params: { action: "UserLogin", LoginType: "other" },
                    success: function(form, action) {
                        if (Number((action.result.status)) == 1) {//强制登录
                            KM.setDisabled(true);
                            Ext.Ajax.request({
                                url: "LoginHandler.ashx",
                                method: "POST",
                                disableCaching: false,
                                params: Ext.apply(This.fp.form.getValues(false), {
                                    action: "ForceLogin"
                                }),
                                success: function(response, opts) {
                                    window.location.href = "application.html";
                                }
                            });
                        } else {
                            window.location.href = "application.html";
                        }
                    },
                    failure: function(form, action) {
                        Ext.Msg.show({ title: "登录失败", msg: decodeURIComponent(action.result.message),
                            fn: function(btn) {
                                if (btn == "ok") {
                                    document.getElementById("checkImg").src = "Common/Codeimg.aspx?" + new Date().getTime();
                                    Ext.getCmp("randcode").focus();
                                    if (action.result.status == 0) {
                                        (function() { Ext.getCmp("randcode").selectText(); })();
                                    }
                                }
                            },
                            width: 400, buttons: Ext.MessageBox.OK
                        });
                    }
                });
            }
        },
        listeners: {
            render: function(win) {
                win.fp.form.waitMsgTarget = win.getEl();
                //读取Cookie并给企业账户，用户账户赋值(如果勾选了“记住密码”,需要给密码框赋值)
                var strtemp = unescape(document.cookie.replace(/\\/g, "%")).split("; ");
                for (var i = 0; i < strtemp.length; i++) {
                    var strtemp2 = strtemp[i].split("=");
                    if (strtemp2.length == 3 && strtemp2[1] == "UserName") {
                        Ext.getCmp("username").setValue(strtemp2[2].split('@')[0]);
                        Ext.getCmp("businessname").setValue(strtemp2[2].split('@')[1]);
                        break;
                    }
                    if (strtemp2.length == 4 && strtemp2[1] == "UserName") {
                        Ext.getCmp("username").setValue(strtemp2[2].split('@')[0]);
                        Ext.getCmp("password").setValue(strtemp2[3]);
                        Ext.getCmp("businessname").setValue(strtemp2[2].split('@')[1].split('&')[0]);
                        Ext.getCmp("rememberpassword").setValue("1");
                        break;
                    }
                }
            }
        },
        //初始化窗口
        initComponent: function() {
            LoginWindow.superclass.initComponent.call(this);
            this.fp = this.createFormPanel();
            this.add(this.fp); //将form表单加入到window中
            this.addButton({ id: "btnLogin", name: "btnLogin", text: "登录" }, this.login, this);
            this.addButton("重置", function() { this.fp.form.reset(); }, this);
        }
    });

    Ext.onReady(function() {
        var win = new LoginWindow();
        win.show();
        var bd = Ext.getDom("randcode");
        var bd2 = Ext.get(bd.parentNode);
        bd2.createChild({
            tag: "input",
            type: "image",
            name: "checkImg",
            id: "checkImg",
            src: "Common/Codeimg.aspx?" + new Date().getTime(),
            onclick: 'this.src="Common/Codeimg.aspx?"+new Date().getTime()',
            style: "cursor:hand",
            align: "absbottom"
        });
        KM = new Ext.KeyMap(Ext.getBody(), { key: Ext.EventObject.ENTER, fn: function() { Ext.getCmp("randcode").getEl().blur(); win.login(); } });
    });
