﻿//====================================================================================
var ChangePasswordWindow = function(expired, user_name) {
	// children go here
	this.expired = expired;
	this.user_name = user_name;

	ChangePasswordWindow.superclass.constructor.call(this, {
		//config goes here
		title: (this.expired ? 'Your password has expired - ' : '') + 'Change Password',
	    width: 500,
	    height: 150,
	    closable: false,
	    bodyStyle: 'padding: 5px;',
	    layout: 'form',
	    labelWidth: 150,
	    items: [{
	        xtype: 'textfield',
	        inputType: 'password',
	        fieldLabel: 'Old Password',
	        name: 'old_password',
	        anchor: '97%',
	        allowBlank: false
	    },{
	        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: 'Change',
		        handler: this.change,
		        scope: this
		    },
		    {
		        text: 'Cancel',
		        handler: function() {
		            this.close();
		        },
		        scope: this
		    }
		],
		buttonAlign: 'center',
		modal: true
	});

	// events go here
	this.addEvents('password_changed');
};

Ext.extend(ChangePasswordWindow, Ext.Window, {
	// functions go here
	change: function() {
	    var op = this.items.get(0).getValue();
	    var np = this.items.get(1).getValue();
	    var cnp = this.items.get(2).getValue();
	    
	    Ext.Ajax.request({
            url: 'user_edit.aspx',
            scope: this,
            success: this.change_done,
            failure: this.change_done,
            params: {
                cmd: 'change_password',
                old_password: op,
                new_password: np,
                confirm_new_password: cnp,
                expired: this.expired,
                user_name: this.user_name
            }
        });
	},
	
	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);
	        }
	        
	        if (!this.expired) {
	            Ext.Msg.alert('Information', 'Password changed.');
	        }
	        
	        this.fireEvent('password_changed');
	        
            this.close();
	    }
	    catch (ex) {
	        Ext.Msg.alert('Warning', 'There was a problem changing your password. ' + error_msg);
        }
	}
});
