Ext.ns('Ext.pv.security');
Ext.pv.security.ChangePasswordDialog = Ext.extend(Ext.Window, {
	id : 'ChangePasswordDialogId',
	frame : true,
	width : 460,
	autoHeight : true,
	modal : true,
	resizable : false,
	
	// Localized strings
	title : 'Change Password',
	usernameLabelText : 'Email',
	oldPasswordLabelText : 'Old Password',
	newPasswordLabelText : 'New Password',
	confirmPasswordLabelText : 'Confirm Password',
	alertTitle : 'Message',
	failureMessage : 'Input is invalid.',
	successMessage : 'Your password has been changed.',
	okButtonText : 'OK',
	cancelButtonText : 'Cancel',
	
	direction : 'ltr',
	vendorId : 0,
	personId : 0,
	contextPath : null,
	username : null,
	successfullLoginCallback : null,
	doAjaxLogin : true,

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

		if (this.direction=='rtl') {
			this.style = 'direction:rtl';
		}
	
		this.items = [ 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.ChangePasswordDialog.superclass.initComponent.call(this);
	},
	
	/**
	 * buildLoginForm
	 * 
	 * @private
	 */
	buildForm : function() {
	
		this.form = new Ext.FormPanel( {
			frame : false,
			border : false,
			bodyStyle: 'background-color:#f0f0f0;padding:20 20 0 20px;',
			labelWidth : 150,
			autoWidth : true,
			autoHeight : true,
			defaults: {width: 230},
			defaultType : 'textfield',
			items : [ {
				xtype : 'hidden',
				name : 'personId',
				value : this.personId
			}, {
				xtype : 'emailfield',
				id : 'username',
				fieldLabel : this.usernameLabelText,
				allowBlank : false,
				validateOnBlur : false,
				value : this.username,
				style: {marginBottom: '0px', marginTop: '0px'}
			}, {
				id : 'old_password',
				fieldLabel : this.oldPasswordLabelText,
				inputType : 'password',
				allowBlank : false,
				style: {marginBottom: '15px', marginTop: '0px'}
			}, {
				id : 'new_password1',
				fieldLabel : this.newPasswordLabelText,
				inputType : 'password',
				allowBlank : false,
				style: {marginBottom: '0px', marginTop: '0px'}
			}, {
				id : 'new_password2',
				fieldLabel : this.confirmPasswordLabelText,
				inputType : 'password',
				allowBlank : false,
				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;
        }
        
        // Trim leading and trailing whitespace from password
        this.form.getForm().findField('old_password').setValue(
				Ext.util.Format.trim(this.form.getForm().findField('old_password').getValue()));
        this.form.getForm().findField('new_password1').setValue(
				Ext.util.Format.trim(this.form.getForm().findField('new_password1').getValue()));
        this.form.getForm().findField('new_password2').setValue(
				Ext.util.Format.trim(this.form.getForm().findField('new_password2').getValue()));
        
        var myThis = this;
        this.form.getForm().submit({
            clientValidation: true,
            url: this.contextPath + '/security/changePassword.do',
            success: function(form, action) {
	        	
            	// Poke the server to log a page-hit against this
    			Ext.Ajax.request({
    				url: myThis.contextPath + '/security/changePasswordSuccess.json?personId=' + myThis.personId,
    				method: 'POST'
    			});
            	
            	var username = myThis.form.getForm().findField('username').getValue();
	    		var password = myThis.form.getForm().findField('new_password1').getValue();
	    		var dlg = new Ext.pv.security.LoginDialog({
	    			vendorId : myThis.vendorId,
	    			contextPath : myThis.contextPath,
	    			successfullLoginCallback : myThis.successfullLoginCallback,
	    			y : -1000 // Show the Login Dialog off screen
	    		});
	        	
	        	Ext.Msg.alert(myThis.alertTitle,myThis.successMessage);
        		myThis.close();
        		
        		dlg.show();
        		dlg.autoLogin(username,password,myThis.doAjaxLogin);
            },
            failure: function(form, action) {
            	Ext.Msg.alert(myThis.alertTitle,action.result.error);
            }
        });
	},
	
	onCancel : function() {
		this.close();
	}

});
