function usermgmtForm(myform) {
	var userStore = new Ext.data.JsonStore({
		url: 'getUserInfo.php',
		fields: ['users_id', 'users_name', 'users_userid', 'users_passwd', 'users_usertype', 'users_email', 'users_organization', 'shortname'],
		sortInfo: {field: 'users_name', direction: "ASC"}
	});
	
	var orgStore = new Ext.data.JsonStore({
		url: 'getOrgs.php',
		fields: ['organization'],
		sortInfo: {field: "organization", direction: "ASC"}
	});

	var usertypeStore = new Ext.data.JsonStore({
		url: 'getUserTypes.php',
		fields: ['id', 'usertype'],
		sortInfo: {field: "usertype", direction: "ASC"}
	});

	var dataChanged = false;

	var newUserForm = function(r) {
		var savebtn = new Ext.Button({
			id: 'saveuserdata',
			text: 'Save User Data',
			scope: this,
			handler: saveUserData,
			disabled: true
		});
		var changebtn = new Ext.Button({
			id: 'changepassword',
			text: 'Change Password',
			scope: this,
			handler: saveUserPassword,
			disabled: true
		});
		var deletebtn = new Ext.Button({
			id: 'deleteuser',
			text: 'Delete User',
			scope: Ext.getCmp('users_userid'),
			handler: delUserData,
			disabled: true
		});
		var clearbtn = new Ext.Button({
			id: 'clearuser',
			text: 'Reset Form',
			scope: this,
			handler: formReset
		});
		var closebtn = new Ext.Button({
			id: 'closeuserpanel',
			text: 'Close',
			scope: this,
			handler: formDestroy
		});
		
		var passwdevents = {blur: function() {
			var p = Ext.getCmp('dup_passwd');
			if(Ext.getCmp('users_passwd').getValue() != ""){
				p.enable();
				p.focus(true, 50);
			} else {
				p.disable();
			}
		}, scope: this.ownerCt};

		if(typeof r != "undefined") {
			var buttons = [savebtn, changebtn, deletebtn, closebtn];
			var afterlayoutevents = {afterlayout: function() {
				deletebtn.enable();
			}};
			var pwevents = {
				blur: function() {
					var v1 = Ext.getCmp('users_passwd').getValue();
					var v2 = Ext.getCmp('dup_passwd').getValue();
					if(v1 != v2){
						Ext.MessageBox.show({
							title: "Password Mismatch", 
							msg: "The passwords you have entered do not match each other.  Please try again.",
							buttons: Ext.MessageBox.OK,
							fn: function() {
								Ext.getCmp('dup_passwd').setValue('');
								Ext.getCmp('users_passwd').focus(true, 50);
							}
						});
					} else {
						if(v1 != "" && v2 != ""){
							changebtn.enable();
							changebtn.focus(false, 50);
						}
					}
				},
				focus: function() {
					if(Ext.getCmp('addnew').getValue() === false){
						savebtn.disable();
					}
				}
			}
		} else {
			var buttons = [savebtn, clearbtn]
			var afterlayoutevents = "";
			var pwevents = {blur: function() {
				var p = Ext.getCmp('users_passwd');
				var pw = p.getValue();
				var cp = Ext.getCmp('dup_passwd').getValue();
				if(pw != cp) {
					Ext.MessageBox.show({
						title: 'Password Mismatch',
						msg: 'The passwords you entered do not match each other.  Try again.',
						buttons: Ext.MessageBox.OK,
						icon: Ext.MessageBox.INFO
					});
					p.focus(true);
				}
			}};
		}
		
		// var changeevents = {'keyup': {fn: enableSaveBtn, scope: this}};
		var comboevents = {'change': {fn: enableSaveBtn, scope: this}, select: {fn: enableSaveBtn, scope: this}};
		
		function delUserData() {
			var curUser = Ext.getCmp('users_id').getValue();
			if(curUser > 1) {	// superuser cannot be removed....
				var parms = {};
				Ext.MessageBox.show({
					title: 'Confirmation required',
					msg: 'You have requested that the currently selected user be permanently removed from the system.  Proceed?',
					buttons: Ext.MessageBox.YESNO,
					icon: Ext.MessageBox.INFO,
					fn: function(btn) {
						parms.del_id = curUser;
						parms.deleteUser = true;
						if(btn == "yes") {
							Ext.Ajax.request({
								url: 'postuser.php',
								params: parms,
								success: function(resp) {
									var json = Ext.util.JSON.decode(resp.responseText);
									if(json.success) {
										userStore.removeAt(json.id);
										formDestroy();
										Ext.MessageBox.show({
											title: 'Request Completed',
											msg: 'The selected user has been removed from the system.',
											buttons: Ext.MessageBox.OK,
											icon: Ext.MessageBox.INFO
										});
									} else {
										Ext.MessageBox.show({
											title: 'Transaction Error',
											msg: json.msg,
											buttons: Ext.MessageBox.OK,
											icon: Ext.MessageBox.ERROR
										});
									}
								},
								failure: function(resp) {
									Ext.MessageBox.show({
										title: 'Error',
										msg: 'Error communicating with remote server.  Check your internet connection and/or server status.',
										buttons: Ext.MessageBox.OK,
										icon: Ext.MessageBox.ERROR
									});
								}
							});
						}
					}
				});
			} else {
				Ext.MessageBox.show({
					title: 'Procedural Error',
					msg: 'You must select a valid user prior to deletion.',
					buttons: Ext.MessageBox.OK,
					icon: Ext.MessageBox.ERROR
				});
				deletebtn.disable();
			}
		}
		
		function saveUserPassword() {
			var parms = {users_passwd: Ext.getCmp('users_passwd').getValue(), 
				users_userid: Ext.getCmp('users_userid').getValue()};
			Ext.Ajax.request({
				url: 'setPasswd.php',
				params: parms,
				success: function(resp) {
					var json = Ext.util.JSON.decode(resp.responseText);
					changebtn.disable();
					if(json.success) {
						Ext.MessageBox.show({
							title: 'Operation Complete',
							msg: json.msg,
							buttons: Ext.MessageBox.OK,
							icon: Ext.MessageBox.INFO
						});
					} else {
						Ext.MessageBox.show({
							title: 'Error',
							msg: json.msg,
							buttons: Ext.MessageBox.OK,
							icon: Ext.MessageBox.ERROR
						});
					}
				}
			});
		}
		
		function saveUserData() {
			var j = Ext.getCmp('userpanel').getForm();
			var parms = j.getValues();
			
			Ext.Ajax.request({
				url: 'postuser.php',
				params: parms,
				success: function(resp) {
					var json = Ext.util.JSON.decode(resp.responseText);
					if(json.success) {
						Ext.MessageBox.show({
							title: 'Database Update Successful',
							msg: json.msg,
							buttons: Ext.MessageBox.OK,
							icon: Ext.MessageBox.INFO
						});
						var urec = Ext.data.Record.create([ 
						     {name: 'users_id', type: 'int'},
						     {name: 'users_name', type: 'string'},
						     {name: 'users_userid', type: 'string'},
						     {name: 'users_passwd', type: 'string'},
						     {name: 'users_usertype', type: 'string'},
						     {name: 'users_email', type: 'string'},
						     {name: 'users_organization', type: 'string'},
						     {name: 'shortname', type: 'string'}
						]);
						userStore.addSorted( new urec({
							'users_id': json.data.users_id,
							'users_name': json.data.users_name,
							'users_userid': json.data.users_userid,
							'users_usertype': json.data.users_usertype,
							'users_email': json.data.users_email,
							'users_organization': json.data.users_organization,
							'shortname': json.data.shortname
						}));
						// userStore.reload();
						usertypeStore.reload();
						orgStore.reload();
						formDestroy();
					} else {
						Ext.MessageBox.show({
							title: 'Database Update Failed',
							msg: json.msg,
							buttons: Ext.MessageBox.OK,
							icon: Ext.MessageBox.ERROR,
							fn: function() {
								Ext.getCmp('users_userid').focus(true, 50);
							}
						});
					}
				},
				failure: function(resp) {
					Ext.MessageBox.show({
						title: 'Error',
						msg: 'Unable to communicate with remote server.  Check you internet connection and/or server status.',
						buttons: Ext.MessageBox.OK,
						icons: Ext.MessageBox.ERROR
					})
				}
			})
		}
		
		function enableSaveBtn() {
			dataChanged = true;
			/* Ext.each(this.ownerCt.items.keys, function(t, i, a) {
				Ext.getCmp(t).un('change', enableSaveBtn);
			}) */
			var f = Ext.getCmp('userpanel').getForm();
			if(f.isValid()){
				savebtn.enable();
			}
			// check if we are ICCI personel
			var utype = Ext.getCmp('users_usertype').getValue();
			var isICCI =  utype == "Staff" || utype == "Event";
			var sn = Ext.getCmp('shortname');
			if(isICCI){
				sn.enable();
			} else {
				sn.setValue('');
				sn.disable();
			}
			if(typeof r != "undefined"){
				changebtn.disable();
				Ext.getCmp('users_passwd').disable();
				Ext.getCmp('dup_passwd').disable();
			}
		}
		
		function enableAddOrgBtn() {
			Ext.getCmp('addorgbtn').enable();
		}
		
		function enableAddUtypeBtn() {
			Ext.getCmp('addutypebtn').enable();	
		}
		
		var formset =  new Ext.form.FieldSet({
			id: 'newuserform',
			layout: 'form',
			buttonAlign: 'right',
			collapsible: false,
			width: 375,
			height: 275,
			listeners: afterlayoutevents,
			defaults: {
				layout: 'form'
			} ,
			items: [{
				xtype: 'hidden',
				id: 'users_id',
				value: typeof r != "undefined" ? r.data.users_id:0
			},{
				xtype: 'combo',
				id: 'users_organization',
				mode: 'local',
				fieldLabel: 'Organization',
				width: 235,
				maxLength: 100,
				selectOnFocus: true,
				triggerAction: 'all',
				editable: true,
				forceSelection: false,
				displayField: 'organization',
				store: orgStore,
				listeners: comboevents,
				value: typeof r != "undefined" ? r.data.users_organization:''
			},{
				xtype: 'combo',
				id: 'users_usertype',
				mode: 'local',
				fieldLabel: 'User-type',
				width: 235,
				maxLength: 25,
				selectOnFocus: true,
				triggerAction: 'all',
				editable: true,
				forceSelection: true,
				displayField: 'usertype',
				store: usertypeStore,
				listeners: comboevents,
				value: typeof r != "undefined" ? r.data.users_usertype:''
			},{
				xtype: 'textfield',
				fieldLabel: 'Name',
				id: 'users_name',
				width: 235,
				maxLength: 49,
				enableKeyEvents: true,
				listeners: {keyup: enableSaveBtn},
				value: typeof r != "undefined" ? r.data.users_name:''
			},{
				xtype: 'textfield',
				fieldLabel: "Calendar Name",
				id: 'shortname',
				disabled: typeof r != "undefined" && r.data.shortname != "" ? false:true,
				width: 75,
				allowBlank: false,
				maxLength: 10,
				value: typeof r != "undefined" ? r.data.shortname:''
			},{
				xtype: 'textfield',
				fieldLabel: 'User ID',
				id: 'users_userid',
				maxLength: 20,
				enableKeyEvents: true,
				listeners: {keyup: enableSaveBtn},
				value: typeof r != "undefined" ? r.data.users_userid:''
			},{
				xtype: 'textfield',
				fieldLabel: 'Email Address',
				id: 'users_email',
				width: 235,
				maxLength: 100,
				enableKeyEvents: true,
				listeners: {keyup: enableSaveBtn},
				value: typeof r != "undefined" ? r.data.users_email:''
			},{
				xtype: 'textfield',
				fieldLabel: 'Password',
				id: 'users_passwd',
				maxLength: 10,
				allowBlank: true,
				inputType: 'password',
				listeners: passwdevents
			},{
				xtype: 'textfield',
				fieldLabel: 'Re-enter Password',
				id: 'dup_passwd',
				maxLength: 10,
				allowBlank: true,
				disabled: true,
				inputType: 'password',
				listeners: pwevents
			},{
				xtype: 'hidden',
				id: 'addnew',
				value: false
			}],
			bbar: buttons
		});
		return formset;
	}
	
	function showDataGrid(panel, cont, cls) {
		var j = Ext.getCmp(cont);
		if(j.items.length > 0) j.remove(j.items.items[0]);	// .destroy();
		j.add(panel);
		j.container.addClass(cls);
		j.doLayout();
		var f = Ext.getCmp('users_name').focus(true, 50);
	}
	
	function addUser() {
		var fieldset = new newUserForm();
		fieldset.setTitle('Create New User Profile');
		Ext.getCmp('addnew').setValue(true);
		showDataGrid(fieldset, 'dataviewgrid', 'newuserform');
	}

	function loadUserForm(t, r) {
		var fieldset = new newUserForm(r);
		fieldset.setTitle('Modify User Profile');
		Ext.getCmp('addnew').setValue(false);
		var els = ['nameselectioncombo', 'useridselectioncombo', 'usertypeselectioncombo'];
		Ext.each(els, function(el, i, a){
			Ext.getCmp(el).setValue("");
		})
		showDataGrid(fieldset, 'dataviewgrid', 'newuserform');
	};
	
	this.fieldset = new Ext.form.FieldSet({
		title: 'View Management',
		id: 'viewselector',
		layout: 'form',
		cls: 'viewselector',
		header: false,
		collapsible: false,
		width: 350,
		height: 160,
		items: [{
			xtype: 'combo',
			id: 'nameselectioncombo',
			mode: 'local',
			fieldLabel: 'Select by Name',
			displayField: 'users_name',
			editable: true,
			maxLength: 49,
			width: 220,
			forceSelection: true,
			selectOnFocus: true,
			triggerAction: 'all',
			emptyText: 'Select User by Name',
			// tpl: '<tpl for="."><div class="x-combo-list-item">{users_name} of {users_organization}</div></tpl>',
			store: userStore,
			listeners: {
				beforerender: function(){
					userStore.sort({field: 'users_name', direction: 'ASC'});
				},
				select: function(t, r, i) {
					loadUserForm(t, r);
				},
				scope: this
			}
		},{
			xtype: 'combo',
			id: 'useridselectioncombo',
			mode: 'local',
			fieldLabel: 'Select by ID',
			maxLength: 20,
			width: 220,
			forceSelection: true,
			selectOnFocus: true,
			triggerAction: 'all',
			emptyText: 'Select User by ID',
			displayField: 'users_userid',
			store: userStore,
			listeners: {
				beforerender: function(){
					userStore.sort({field: 'users_userid', direction: "ASC"});	
				},
				select: function(t, r, i) {
					loadUserForm(t, r);
				},
				scope: this
			}
		},{
			xtype: 'combo',
			id: 'usertypeselectioncombo',
			mode: 'local',
			fieldLabel: 'User Type',
			maxLength: 9,
			width: 220,
			forceSelection: true,
			selectOnFocus: true,
			triggerAction: 'all',
			displayField: 'usertype',
			emptyText: 'Select View by User Type',
			store: usertypeStore,
			listeners: {
				select: function(t, r, i) {
					alert("Grid view selected.  Value: "+r.data.usertype+"\n\nThis feature will be implemented in future development.");
				},
				scope: this
			}
		}],
		bbar: [{
			xtype: 'button',
			text: 'Create New User',
			handler: addUser
		},{
			xtype: 'button',
			text: 'Add Organization',
			handler: addOrganization
		},{
			xtype: 'button',
			text: 'Add User Type',
			handler: addUserType
		}]
	});
	
	function addUserType() {
		function addNewUType(v) {
			if(typeof v == "string") {
				var parms = {usertype: v};
			} else {
				var parms = {usertype: Ext.getCmp('users_usertype').getValue()};
			}
			Ext.Ajax.request({
				url: 'addUtype.php',
				params: parms,
				success: function(resp) {
					var json = Ext.util.JSON.decode(resp.responseText);
					if(json.success) {
						usertypeStore.reload();
						if(typeof v == "undefined") {
							Ext.getCmp('addutypebtn').disable();
							dataChanged = true;
							savebtn.enable();
						}
						Ext.MessageBox.show({
							title: 'Operation Complete',
							msg: json.msg,
							buttons: Ext.MessageBox.OK,
							icon: Ext.MessageBox.INFO
						});
						var usertyperec = Ext.data.Record.create([
							{name: 'id', type: 'int'},
							{name: 'usertype', type: 'string'}
						]);
						var rec = new usertyperec({
							id: json.id,
							usertype: json.type
						});
						usertypeStore.addSorted(rec);
					} else {
						Ext.MessageBox.show({
							title: 'Data Error',
							msg: json.msg,
							buttons: Ext.MessageBox.OK,
							icon: Ext.MessageBox.ERROR
						});
					}
				}
			});
			
		}
		
		Ext.Msg.prompt(
			'Add new UserType',
			'Please enter the desired type',
			function(btn, text) {
				if(btn == "ok") {
					addNewUType(text);
				}
			}
		)
	}
	
	function addOrganization() {
		Ext.Msg.prompt(
			'Add new Organization',
			'Please enter the desired organization\'s name',
			function(btn, text) {
				if(btn == "ok") {
					addNewOrganization(text);
				}
			}
		)
	}

	function addNewOrganization(v) {
		if(typeof v == "string") {
			var parms = {organization: v};
		} else {
			var parms = {organization: Ext.getCmp('users_organization').getValue()};
		}
		Ext.Ajax.request({
			url: 'addOrganization.php',
			params: parms,
			success: function(resp) {
				var json = Ext.util.JSON.decode(resp.responseText);
				if(json.success) {
					orgStore.reload();
					if(typeof v == "undefined") {
						Ext.getCmp('addorgbtn').disable();
						dataChanged = true;
						savebtn.enable();
					}
					Ext.MessageBox.show({
						title: 'Operation Complete',
						msg: json.msg,
						buttons: Ext.MessageBox.OK,
						icon: Ext.MessageBox.INFO
					});
				} else {
					Ext.MessageBox.show({
						title: 'Data Error',
						msg: json.msg,
						buttons: Ext.MessageBox.OK,
						icon: Ext.MessageBox.ERROR
					});
				}
			}
		});
	}
	
	var panel = new Ext.form.FormPanel({
		title: 'User Management',
		header: false,
		id: 'userpanel',
		layout: 'form',
		width: '50%',
		style: 'margin-top: 175px;',
		bodyStyle: 'padding: 15px; padding-left: 40px;',
		deferredRender: true,
		formId: 'useradmin',
		items: [{
			xtype: 'panel',
			layout: 'column',
			border: false,
			frame: false,
			defaults: {
				frame: false,
				border: false
			},
			items: [{
				columnWidth: .5,
				id: 'userfieldset',
				height: 515,
				defaults: {
					layout: 'form'
				},
				items: [this.fieldset]
			},{
				columnWidth: .5,
				height: 515,
				id: 'dataviewwrapper',
				defaults: {
					layout: 'form',
					border: false,
					frame: false,
					header: false/*,
					width: '95%',
					height: '100%'*/
				},
				items: [{
					xtype: 'panel',
					id: 'dataviewgrid',
					autoHeight: true
				}]
			}]
		}]
	});

	function formDestroy() {
		var j = Ext.getCmp('dataviewgrid');
		if(j.items.length > 0) j.remove(j.items.items[0]);	//.destroy();
	}
	
	function formReset() {		
		addUser();
	}
	
	panel.on('activate', function(){
		// alert("Tab panel activated.");
		if(userStore.getCount() == 0) userStore.load();
		if(orgStore.getCount() == 0) orgStore.load();
		if(usertypeStore.getCount() == 0) usertypeStore.load();
	});
	return panel;
}