COMP.Login=
{
	KeyHandler : function(f,e)
	{
		switch (e.getKey())
		{
			case e.ENTER:
				COMP.Login.HandleRWSLogin();
				break;
			
			case e.ESC:
				if (!COMP.Config.GetDefault('requireID_Password'))
					COMP.Login.CloseDialog();
				break;
		}
	}
	
	,CloseDialog : function()
	{
		if (!COMP.Login.dialog) return;
		COMP.Login.dialog.close();
		COMP.Login.dialog = null;
		ResetFocus();
	}
	
	,ForgotPassword : function()
	{
		var login = Ext.get('user').dom.value;
		if (!login.length)
		{
			alert(L(25954)); //'Enter your barcode, username, or email address, and we will use that information to send you your password.'
			return;
		}
		
		COMP.SendAjaxCommand('all_patron_forgot_password',{user:login},
			function(o)
			{
				alert(o.alert);
			});
	}
	,ResetPassword:function()
	{

		var resetDlg = new COMP.Controls.ResetPassword(); 
		resetDlg.show();
	}
	,handler : function(sender) { COMP.Login.HandleShowLogin("RWS.Patron.Show('loginWindow');", null, null, sender); }
	,HandleShowLogin : function(successFunc, o, loginName, sender) // successFunc must be a string
	{
		COMP.Login.successFunc = successFunc;
		COMP.Login.extraParams = o;
		
        var v1 = {
				xtype:'field'
				,fieldLabel: L(19429)
				,labelSeparator: ''
				,id: 'user'
				,name: 'user'
				,value: loginName
//				,allowBlank: false
				,listeners: {
					'render' : function() {
						if(!loginName)
							this.focus(false,1000);
					}
					,'specialkey': {
						fn: COMP.Login.KeyHandler
						,scope: this
					}
				}	
			};

		var v2 = {
				xtype:'field'
				,fieldLabel: L(4134)
				,id: 'pwmd'
				,name: 'pwmd'
				,inputType: 'password'
				,listeners: {
				'specialkey': COMP.Login.KeyHandler
				,'render' : function() {
					if(loginName)
						this.focus(false,1000);
					}
				}
			};
			
		var v3 = {
			xtype:'COMPpopupMenu'
			,fieldLabel: L(25449)//ls_Site
			,id: 'site'
			,name: 'site'
			,menuItems:[]

		};
			
		var itms=[v1,v2];
		if (COMP.Config.GetDefault('allowPatronToRequestPassword'))
			itms.add({
				xtype:'textLink'
				,cls:'centerText'
				,text:L(26110) // 'Reset Password'
				,handlerStr:"COMP.Login.ResetPassword();"
			});
		
		function getButtons()
		{
			var buttons=[];
			
			buttons.add('->');
			if (!COMP.Config.GetDefault('requireID_Password'))
				buttons.add({
					text: L(5108)
					,formBind: false
					,id: 'close_login_btn'
					,handler: COMP.Login.CloseDialog
				});
			buttons.add({	
				text: L(5275)
				,formBind: false
				,id: 'login_btn'
				,handler: function(){
					COMP.Login.HandleRWSLogin();
				}
			});

			return buttons;
		};
		
		var login = new Ext.FormPanel({
			labelWidth: 80
			,url: '/login/'
			,frame: false
			,border: false
			,defaultType: 'textfield'
			,cls:'rws_login'
			,monitorValid: true
			,defaultButton: COMP.Config.GetDefault('requireID_Password') ? 1 : 2
			,items: itms
		});

		COMP.Login.dialog = new Ext.Window({
			layout: 'fit'
			,width: 270
			,cls: 'dialog'
			,height: 135
			,closable: false
			,resizable: false
			,plain: true
			,id: 'loginWindow'
			,border: false
			,modal: true
			,buttonAlign: 'center'
			,items: [login]
			,bbar:getButtons()
		//	,buttons: getButtons()
		});
		
		COMP.Login.dialog.show(sender);
		//COMP.Login.dialog.findById('user').focus(true);		
	}
	,LoginRWSSuccessful : function(o)
	{
		var patName = o.patName;
//		HideWaitDialog();
		

		var logoutBtn = Ext.getCmp('north_login');
		logoutBtn.addClass('red');
		logoutBtn.setText(L(12193));
				
		var accountBtn = Ext.getCmp('north_patron');
		accountBtn.setText(patName);
		accountBtn.show();	
		
		RWS.Layout.NorthBar.doLayout();		 

		RWS.Patron.current = o;
		RWS.Patron.current.loggedIn = true;
		COMP.Config.SetLanguage();
		RWS.Layout.SimpleSearch.doLayout();
		COMP.Config.VerifyLocalCollection();
		// PreFilters.lexile.resetDefaults();
		RWS.SavedLists.ShowLists(o);

		if (o.successFunc.length > 0)
			eval(o.successFunc);
		COMP.Login.CloseDialog();
        if(COMP.Config.GetDefault('allowSaveBasket')) RWS.Layout.SavedLists.showButton();

		c = Ext.getCmp('prefLibrary');
		if(c){
			 c.replaceMenu(COMP.Prefs.collectionsList[COMP.Config.GetDefault('searchAddressBook')]);
			 setDefaultState(c.id, c.getValue());			
		}
	}
	,LoginLibSuccessful : function(o)
	{
//		HideWaitDialog();
		COMP.SetWindowLocation('/',{jli:true,sessionid:COMP.V.sessionid,refresh_loc:'/'});
	}
	,LoginFailed : function ()
	{
//		HideWaitDialog();
		var dlg = new RWS.Dialog({buttons:Ext.Msg.OKAY, message:L(11652), width: 300})
		dlg.show();
	}
	,LoginNotAllowed : function ()
	{
//		HideWaitDialog();
		var dlg = new RWS.Dialog({buttons:Ext.Msg.OKAY, message:L(25955), width: 300})//'You are not registered for Web Librarian. Admin login prohibited.'
		dlg.show();
	}	
	,HandleRWSLogout: function(o)
	{
		window.location = '/';
		return;
		
		RWS.Patron.current={data:{}};
		RWS.Patron.current.loggedIn = false;
		COMP.Config.SetLanguage();
		
		var logoutBtn = Ext.getCmp('rwsLogoutBtn');
		
		var accountBtn = Ext.getCmp('patronAccountBtn');
		accountBtn.hide();
		logoutBtn.getEl().setStyle('color', '#324A62');
		logoutBtn.setText(L(5275));
		
		var headerPanel = Ext.getCmp('headerPanel');
		headerPanel.doLayout();
		
    	Ext.getCmp('savedLists').hideButton();
	
		var savedListsPanel = Ext.getCmp('savedLists');
		var lists = savedListsPanel.find('isSavedList', true);
		for(var i=0; i<lists.length; i++)
			savedListsPanel.remove(lists[i]);
		    
        PreFilters.library.clear();
        PreFilters.medium.clear();
        PreFilters.awards.clear();
        PreFilters.pubyear.clear();
        PreFilters.lexile.clear();
        PreFilters.studyprogram.clear();
				
		if (COMP.Config.GetDefault('requireID_Password'))
			COMP.Login.HandleShowLogin();
			
		c = Ext.getCmp('prefLibrary');
		c.replaceMenu(COMP.Prefs.collectionsList[COMP.Config.GetDefault('searchAddressBook')]);
		setDefaultState(c.id, c.getValue());
	}
	,HandleRWSLogin:function()
	{
		COMP.LoadLibrariesSerial(['encode'], function(){
			
			var successFunc = COMP.Login.successFunc;
			var o = COMP.Login.extraParams;

			var pass = Ext.get('pwmd').dom.value;
			if (!o)	o = {};
			o = COMP.CombineObjects(o,{pwmd:myUrlEncode(pass,pass), un:myUrlEncode(Ext.get('user').dom.value, Ext.get('user').dom.value)});
			if (successFunc!=undefined) o.successFunc = successFunc;

//			ShowWaitDialog(L(25779) + '...'); // 'Logging in'
			COMP.SendAjaxCommand('all_login_request',o,COMP.Login.HandleLoginResponse, this);
			
		});
	}
	,HandleLoginResponse:function(result){
		if (!result)
		{
			COMP.Login.LoginFailed();
			return;
		}
		
		if (result.admin)
		{
			if (result.config)
			{
				if (result.security)
					COMP.Config.AdminLogin();
				else
					COMP.Config.SecurityFail();
			}
			else
			{
				COMP.Login.LoginLibSuccessful();
			}
		}
		else if (result.patron)
		{
			COMP.Login.LoginRWSSuccessful(result.patron);
		}
		else if (result.loginNotAllowed)
		{
			COMP.Login.LoginLibSuccessful();
		}
		else
		{
			COMP.Login.LoginFailed();
		}
	}
	,HandleLogout:function()
	{
		var o = {web_researcher: true, ticks:COMP.GetCurrentTicks()};
		COMP.SendAjaxCommand('all_logout_request',o,COMP.Login.HandleRWSLogout);
	}
};

if (window.COMP) COMP.Broadcast('scriptLoaded_rws_login');