Ext.BLANK_IMAGE_URL = 'resources/images/default/s.gif';
Ext.state.Manager.setProvider(new Ext.state.CookieProvider()); // 必须在使用之前定义
SR = {
	title : "易择分销DRP企业在线解决方案",
	公司编号 : "公司编号",
	用户名 : "用户名",
	密码 : "密码",
	帐套 : "帐套",
	登录 : '登录',
	取消 : '取消',
	系统信息 : '系统信息',
	请输入必填项 : '请输入必填项',
	无法弹出窗口 : '您的浏览器启用了弹出窗口阻止程序，请设置为允许本站点弹出窗口，以便正常使用本系统。谢谢！'
};

var ztStore = new Ext.data.Store({
			url : 'zt.xml',
			reader : new Ext.data.XmlReader({
						record : 'zt'
					}, ['ShowName'])
		});

var fPanel = new Ext.FormPanel({
	labelWidth : 75,
	url : 'Process.aspx',
	frame : true,
	defaultType : 'textfield',
	anchor : '0 -70', // 70:topPanel的高度
	bodyStyle : 'padding:5px 55px',
	items : [{
				fieldLabel : SR.公司编号,
				id : 'org',
				stateEvents : ['change'],
				getState : function() {
					return {
						value : this.getValue()
					};
				}
			}, {
				fieldLabel : SR.用户名,
				id : 'user',
				allowBlank : false,
				stateEvents : ['change'],
				getState : function() {
					return {
						value : this.getValue()
					};
				}
			}, {
				fieldLabel : SR.密码,
				id : 'pwd',
				inputType : 'password'
			}, {
				xtype : 'combo',
				fieldLabel : SR.帐套,
				id : 'zt',
				editable : false,
				triggerAction : 'all',
				selectOnFocus : true,
				allowBlank : false,

				store : ztStore,
				displayField : 'ShowName',
				mode : 'local',
				stateEvents : ['select'],
				getState : function() {
					return {
						value : this.getValue()
					};
				}
			}],

	buttonAlign : 'center',
	buttons : [{
		text : SR.登录,
		handler : function() {
			fPanel.getForm().submit({
				params : {
					action : 'login'
				},
				success : function(f, r) {
					if (r.result.msg)
						alert(r.result.msg);

					var s = 'main.htm?n=' + r.result.n;
					if (window.opener) { // 注销重登录
						document.location = s;
					} else {
						var win = window.open(s, null, 'left=0,top=0,height='
										+ new String(screen.availHeight - 28)
										+ ',width='
										+ new String(screen.availWidth - 10)
										+ ',location=no,menubar=no,toolbar=no');
						if (win) {
							winLogin.close(); // FF必须
							close();
						} else
							Ext.Msg.alert('', SR.无法弹出窗口);
					}
				},
				failure : function(form, action) {
					var fn = function() {
						Ext.get('user').focus();
					}
					if (action.failureType == "client")
						Ext.Msg.alert('', SR.请输入必填项, fn);
					else if (action.failureType == "connect")
						Ext.Msg.alert('', action.response.responseText, fn);
					else
						Ext.Msg.alert('', action.result.msg.replace('\n',
										'<br/>'), fn);
				}
			})
		}
	}, {
		text : SR.取消,
		handler : function() {
			winLogin.close(); // FF必须
			close(); // 关闭整个浏览器窗口
		}
	}]
});

Ext.onReady(function() {
	Ext.QuickTips.init(); // Form的错误提示依靠它
	Ext.getDoc().dom.title = SR.title;
	Ext.form.Field.prototype.msgTarget = 'side';
	Ext.Ajax.url = 'Process.aspx';

	ztStore.load();

	Mydb.doAjax({
		action : 'get_login_pic',
		fn : function(r) {
			if (r.msg == null)
				var topPanel = new Ext.Panel({
					layout : 'absolute',
					bodyStyle : 'background-image: url("img/Loginbg1.gif")',
					height : 70,

					items : [new Ext.form.Label({
						text : 'YizeSoft',
						x : 12,
						y : 9,
						style : 'color:white;font-size:20pt;font-family:Times;font-style:italic;font-weight:bold'
					}), new Ext.form.Label({
								text : SR.title,
								x : 12,
								y : 45,
								style : 'color:white'
							}), new Ext.form.Label({
						text : 'V3.0',
						x : 340,
						y : 45,
						style : 'color:white;font-size:16pt;font-family:Times;font-style:italic;font-weight:bold'
					})]
				});
			else
				var topPanel = new Ext.Panel({
							height : 72,
							bodyStyle : 'background: url("' + r.msg
									+ '") no-repeat'
						});
			// ★回车控制处理
			for (var i = 0; i < fPanel.items.length; i++) {
				var f = fPanel.get(i);
				f.on('specialkey', function(f, e) {
							if (e.getKey() == e.ENTER) {
								var nf = this.nextSibling();
								if (nf)
									nf.focus();
								else
									this.ownerCt.buttons[0].handler();
							}
						})
			}

			winLogin = new Ext.Window({
						layout : 'anchor',
						title : SR.登录,
						width : 401,
						height : 261,
						modal : true,
						resizable : false,
						toFront : Ext.emptyFn,// ★
						items : [topPanel, fPanel],
						listeners : {// ★
							show : function() {
								this.get(1).get(0).focus();
							}
						}
					});
			winLogin.show();
		}
	});
});

