Ext.ns('Ext.pv.security');
Ext.pv.security.ForgotPasswordDialog = Ext.extend(Ext.Window, {
	id : 'forgotPasswordDialogId',
	frame : true,
	width : 400,
	autoHeight : true,
	modal : true,
	resizable : false,
	
	// Localized strings
	title : 'Forgot Password',
	instructionText : 'Enter your email address below and an email will be sent to you containing your new password.',
	usernameLabelText : 'Email',
	alertTitle : 'Message',
	failureMessage : 'Input is invalid.',
	successMessage : 'An email has been sent with your new password.',
	okButtonText : 'OK',
	cancelButtonText : 'Cancel',
	
	direction : 'ltr',
	personId : 0,
	contextPath : null,
	username : null,

	/**
	 * initComponent
	 * 
	 * @protected
	 */
	initComponent : function() {

		if (this.direction=='rtl') {
			this.style = 'direction:rtl';
		}
	
		this.items = [ this.buildInfoPanel(), this.buildForm() ];
	
		this.buttons = [ {
			text : this.okButtonText,
			handler : this.onOk,
			scope : this
		}, {
			text : this.cancelButtonText,
			handler : this.onCancel,
			scope : this
	    } ];
		
		this.on(
				'show',
				function() {
					var fld = Ext.getCmp("username");
					fld.focus('',true);
					fld.clearInvalid();
				},
				this
			);
		
		// super
		Ext.pv.security.ForgotPasswordDialog.superclass.initComponent.call(this);
	},
	
	/**
	 * buildLinksPanel
	 * 
	 * @private
	 */
	buildInfoPanel: function() {
	
		this.linksPanel = new Ext.Panel({
		    border: false,
			bodyStyle: 'background-color:#f0f0f0;padding: 20 20 0 20px;',
			html: this.instructionText
		});
	
		return this.linksPanel;
	},
	
	/**
	 * buildLoginForm
	 * 
	 * @private
	 */
	buildForm : function() {
	
		this.form = new Ext.FormPanel( {
			frame : false,
			border : false,
			bodyStyle: 'background-color:#f0f0f0;padding:20 20 0 20px;',
			labelWidth : 75,
			autoWidth : true,
			autoHeight : true,
			defaults: {width: 260},
			defaultType : 'textfield',
			items : [ {
				xtype : 'hidden',
				name : 'personId',
				value : this.personId
			}, {
				xtype : 'emailfield',
				id : 'username',
				fieldLabel : this.usernameLabelText,
				allowBlank : false,
				validateOnBlur : false,
				value : this.username,
				enableKeyEvents : true,
				listeners : {
					specialkey : function(f, o) {
			            if (o.getKey() == 13) {
			            	o.stopEvent(); // prevent event propogation in Chrome/Safari
			                this.onOk();
						}
					},
					scope : this
				},
				style: {marginBottom: '15px', marginTop: '0px'}
			} ]
		});
	
		return this.form;
	},
	
	/**
	 * doLogin
	 * 
	 * @private
	 */
	onOk : function(button, event) {
		
        if (!this.form.getForm().isValid()) {
        	Ext.Msg.alert(this.alertTitle,this.failureMessage); 
            return false;
        }
        
        var myThis = this;
        this.form.getForm().submit({
            clientValidation: true,
            url: this.contextPath + '/security/forgotPassword.do',
            success: function(form, action) {
        		Ext.Msg.alert(myThis.alertTitle,myThis.successMessage);
        		myThis.close();
            },
            failure: function(form, action) {
            	Ext.Msg.alert(myThis.alertTitle,action.result.error);
            }
        });
	},
	
	onCancel : function() {
		this.close();
	}

});
