Application.login.RetrievePasswordPanel = Ext.extend(Ext.Panel, {
	
	cn: null
	
	,getData: function () {
		return {
			email: this.getEmail()
		}
	}
	
	,getEmail: function () {
		return this.getEmailCmp().getData();
	}
	
	,getEmailCmp: function () {
		return this.findByType('ux.form.emailaddress')[0];
	}
	
	,getFormPanel: function () {
		return this.findByType('form')[0];
	}
	
	,mask: function (doMask) {
		if (doMask)
			this.getEl().mask('Retrieving...','x-mask-loading');
		else 
			this.getEl().unmask();
	}
	
	,isValid: function () {
		var fp = this.getFormPanel();
		return fp.getForm().isValid();	
	}
	
	,busy: function (isBusy) {
		this.mask(isBusy);
	}
	
	,onRetrieve: function () {
		if (this.isValid()) {
			if (this.cn) {
				this.busy(true);
				this.cn.request({
					params: {
						method: 'retrievePassword'
						,data: Ext.encode(this.getData())
					}
					,success: this.onSuccess
					,failure: this.onFailure
					,scope: this
				});
			}	
		} else {
			Ext.Msg.showAppError('Application.Validation');
		}
	}
	
	,onSuccess: function (response, options) {
		var r = Ext.decode(response.responseText);
		if (!r.isAuthenticated){
			this.busy(false);
			if (r.errors.length)
				Ext.Msg.showAppError(r.errors[0].type);
			else
				Ext.Msg.show({
				   title:'Status',
				   msg: 'Your password has been sent.',
				   buttons: Ext.Msg.OK,
				   fn: Ext.emptyFn,
				   icon: Ext.MessageBox.INFO,
				   maxWidth: 300	
				});
		}
	}
	
	,onFailure: function (response, options) {
		this.busy(false);
		Ext.Msg.showResponseError(response.status);
	}
	
	,onRetryLogin: function () {
		this.fireEvent('retryLogin', this);	
	}
	
	,onRetryLoginRender: function (cmp) {
		cmp.el.on({
			'click': this.onRetryLogin
			,scope: this
		});
	}
	
	,onRender: function () {
		Application.login.RetrievePasswordPanel.superclass.onRender.apply(this, arguments);
		this.el.addKeyListener(
			Ext.EventObject.ENTER, this.onKeyPress, this 
		);
	}
	
	,onKeyPress: function (keyCode, e) {
		if (keyCode == Ext.EventObject.ENTER)
			this.onRetrieve();
	}
	
	,initComponent:function() {
		
		Ext.apply(this, {
			border: false
			,items: [
				{
					title: 'Retrieve Password'
					,border: true
					,bodyBorder: true
					,cls: 'panel-bg'
					,items: [
						{
							xtype: 'form'
							//,frame: true
							,border: false
							,monitorValid: true
							,labelAlign: 'right'
							,autoHeight: true
							,border: false
							,bodyStyle: 'padding: 5px;'
							,items: [
								{
									xtype: 'ux.textpanel'
									,hidden: true
								},{
									xtype: 'ux.form.emailaddress'
									,fieldCfg: {
										anchor: '0'
										,allowBlank: false
										,tabIndex: 1
									}
									,deferredRender: false	
								}
							]
							,buttons: [{
						        text: 'Retrieve'
								,handler: this.onRetrieve
								,scope: this
								,tabIndex: 2
							}]		
						}
					]
				},{
					xtype: 'ux.linktextpanel'
					,cfg: {
						 txt: 'Retry logging in?'
						,style:	'width:150px; padding:5px;'
					}
					,listeners: {
						'render': this.onRetryLoginRender	
						,scope: this
					}
				}	
			]
		});
		
		this.addEvents({
			retryLogin : true
		});
				
		Application.login.RetrievePasswordPanel.superclass.initComponent.apply(this, arguments);
	}	
});	

Ext.reg('login.retrievepasswordpanel', Application.login.RetrievePasswordPanel);
