﻿//====================================================================================
var RecoverPasswordWindow = function(token, user_id, is_new) {
	// children go here
	this.token = token;
	this.user_id = user_id;
    this.is_new = is_new;
    
	RecoverPasswordWindow.superclass.constructor.call(this, {
		//config goes here
		title: 'Set Password',
	    width: 500,
	    height: 120,
	    closable: false,
	    bodyStyle: 'padding: 5px;',
	    layout: 'form',
	    labelWidth: 150,
	    items: [{
	        xtype: 'textfield',
	        inputType: 'password',
	        fieldLabel: 'New Password',
	        name: 'new_password',
	        anchor: '97%',
	        allowBlank: false
	    },{
	        xtype: 'textfield',
	        inputType: 'password',
	        fieldLabel: 'Confirm New Password',
	        name: 'confirm_new_password',
	        anchor: '97%',
	        allowBlank: false
	    }],
		buttons: [
		    {
		        text: 'Submit',
		        handler: this.change,
		        scope: this
		    },
		    {
		        text: 'Cancel',
		        handler: function() {
		            this.close();
		        },
		        scope: this
		    }
		],
		buttonAlign: 'center',
		modal: true
	});

	// events go here
};

Ext.extend(RecoverPasswordWindow, Ext.Window, {
	// functions go here
	change: function() {
	    var np = this.items.get(0).getValue();
	    var cnp = this.items.get(1).getValue();
	    
	    Ext.Ajax.request({
            url: 'user_edit.aspx',
            scope: this,
            success: this.change_done,
            failure: this.change_done,
            params: {
                cmd: 'recover_password',
                new_password: np,
                confirm_new_password: cnp,
                recovery_token: this.token,
                user_id: this.user_id
            }
        });
	},
	
	change_done: function(response) {
	    var error_msg = 'Error contacting server.';
	    
	    try {
	        var data = Ext.util.JSON.decode(response.responseText);
	        
	        if (!data.success) {
	            error_msg = data.error;
	            throw(0);
	        }
	        
	        Ext.Msg.alert('Information', 'Your password has been set.');
	        
            this.close();
	    }
	    catch (ex) {
	        Ext.Msg.alert('Warning', 'There was a problem setting your password. ' + error_msg);
        }
	}
});
