﻿//====================================================================================
var ForgotPasswordWindow = function() {
	// children go here

	ForgotPasswordWindow.superclass.constructor.call(this, {
		//config goes here
		title: 'Forgot Password',
	    width: 300,
	    height: 100,
	    closable: false,
	    bodyStyle: 'padding: 5px;',
	    layout: 'form',
	    labelWidth: 80,
	    items: [{
	        xtype: 'textfield',
	        fieldLabel: 'User Name',
	        name: 'user_name',
	        anchor: '97%',
	        allowBlank: false
	    }],
		buttons: [
		    {
		        text: 'Submit',
		        handler: this.submit,
		        scope: this
		    },
		    {
		        text: 'Cancel',
		        handler: function() {
		            this.close();
		        },
		        scope: this
		    }
		],
		buttonAlign: 'center',
		modal: true
	});

	// events go here
};

Ext.extend(ForgotPasswordWindow, Ext.Window, {
	// functions go here
	submit: function() {
	    var un = this.items.get(0).getValue();
	    
	    Ext.Ajax.request({
            url: 'user_edit.aspx',
            scope: this,
            success: this.submit_done,
            failure: this.submit_done,
            params: {
                cmd: 'forgot_password',
                user_name: un
            }
        });
	},
	
	submit_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 (data.processed) {
	            Ext.Msg.alert('Information', 'Your password recovery is being prepared. Check your email for instructions on how to complete this process.');
	        }
	        else {
	            Ext.Msg.alert('Information', 'iCenter was unable to automatically process your password recovery. Contact your system administrator to complete this process.');
	        }
	        
            this.close();
	    }
	    catch (ex) {
	        Ext.Msg.alert('Warning', 'There was a problem. ' + error_msg);
        }
	}
});
