Login = function() {
	var dialog, form, submitUrl = 'AjaxMVC.wws';

	return {
		Init	: function() {
			Ext.QuickTips.init();

			// get the path
			var path = window.location.pathname;
			path = path.substring(0, path.lastIndexOf('/') + 1);

			// clear the cookies
			set_cookie('acl_key', '', '', path, '', '');
			set_cookie('memberName', '', '', path, '', '');
			set_cookie('memberType', '', '', path, '', '');
			set_cookie('memberGroup', '', '', path, '', '');
			set_cookie(__storecookiename, __storecookieid, '', path, '', '');

			var logoPanel = new Ext.Panel({
				baseCls	: 'x-plain',
				id		: 'login-logo',
				height:80,
				region	: 'north'
			});
			


			var formPanel = new Ext.form.FormPanel({
				baseCls		: 'x-plain',
				baseParams	: {
					module	: 'ajaxlogin',
					action  :  'login',
					isAjax: 'true',
					cookieID: __storecookieid,
					clientid: __clientid
				},
				defaults	: {
					width		: 200,
					msgTarget	: 'side'
				},
				defaultType	: 'textfield',
				frame		: false,
				height		: 100,
				id			: 'login-form',
				items		: [{
					fieldLabel	: 'User ID',
					name		: 'user',
					allowBlank	: false,
					blankText	: 'User ID is required'
				}, {
					fieldLabel	: 'Password',
					inputType	: 'password',
					name		: 'pass',
					allowBlank	: false,
					blankText	: 'Password is required'
				}, {
					fieldLabel		: 'Interface',
					xtype			: 'combo',
					name			: 'type',
					allowBlank		: false,
					emptyText		: 'Select an interface...',
					blankText		: 'Interface is required',
					forceSelection	: true,
					editable		: false,
					displayField	: 'name',					
					triggerAction	: 'all',
					valueField		: 'value',
					store			: new Ext.data.Store({
						proxy		: new Ext.data.HttpProxy({
							url	: submitUrl
						}),
						baseParams	: {
							module		: "getLookupData",
							lookuptype	: 'userlevel',
							area	: 'admin',
							isAjax	: 'true',
							cookieid:__storecookieid,
							clientid: __clientid
						},
						reader		: new Ext.data.JsonReader({
							root	: 'data',
							id		: 'name'
						}, [{
							name	: 'value'
						}, {
							name	: 'name'
						}])
					})
				}],
				labelWidth	: 100,
				region		: 'center',
				url			: submitUrl				
			});
			
						var regPanel = new Ext.Panel({
				baseCls	: 'x-plain',
				id		: 'reg-link',
				region	: 'south',
				height: 60,
				html:'<p align="right"><a href="dealerregistration.wws">Registration</a></p>',
				bbar		: new Ext.StatusBar({
					id			: 'form-statusbar',
					defaultText	: 'Ready',
					plugins		: new Ext.ux.ValidationStatus({
						form	: 'login-form'
					})
				})
			});

			dialog = new Ext.Window({
				buttons		: [{
					handler	: function() {
						form.submit({
							waitMsg	: 'Please Wait...',
							reset	: true,
							success	: Login.Success,
							scope	: Login
						});
					},
					scope	: Login,
					text	: 'Login'
				}],
				buttonAlign	: 'right',
				closable	: false,
				draggable	: true,
				height		: 300,
				id			: 'login-win',
				layout		: 'border',
				minHeight	: 250,
				minWidth	: 310,
				plain		: false,
				resizable	: true,
				region: 'center',
				items		: [logoPanel, formPanel,regPanel],
				title		: 'Secured Area > Please Login!	',
				width		: 450
			});

			form = formPanel.getForm();

			dialog.show();
		},

		Success	: function(f, a) {
			if (a && a.result) {
				dialog.destroy(true);

				// get the path
				var path = window.location.pathname;
				path = path.substring(0, path.lastIndexOf('/') + 1);

				// set the cookie
				set_cookie('acl_key', a.result.key, '', path, '', '');
				set_cookie('memberName', a.result.name, '', path, '', '');
				set_cookie('memberType', a.result.type, '', path, '', '');
				set_cookie('memberGroup', a.result.group, '', path, '', '');

				// redirect the window
				//window.location = path + 'adminDesktop.wws';
				//window.location = window.location ;
				// redirect the window back to http
				window.location = window.location.protocol+"//"+window.location.hostname+ "/afterlogin.wws?clientid="+__clientid+"&"+"cookieID="+ __storecookieid+"&"+"afterlogin="+__afterLogin;
			}
		}
	};
}();

Ext.BasicForm.prototype.afterAction = function(action, success) {
	this.activeAction = null;
	var o = action.options;
	if (o.waitMsg) {
		Ext.MessageBox.updateProgress(1);
		Ext.MessageBox.hide();
	}
	if (success) {
		if (o.reset) {
			this.reset();
		}
		Ext.callback(o.success, o.scope, [this, action]);
		this.fireEvent('actioncompleted', this, action);
	} else {
		Ext.callback(o.failure, o.scope, [this, action]);
		this.fireEvent('actionfailed', this, action);
	}
}

Ext.onReady(Login.Init, Login, true);
