Ext.namespace("dlr.cmp.workSpace");
dlr.cmp.workSpace.settingsWindow = function(cfg){
	// ------------------------------------------------
	// private properties
	// ------------------------------------------------
	var that = this;
	var windowCfg = {
		layout: "border",
		width: 500,
		title: "Settings",
		height: 300,
		closeAction: "hide",
		items:[{ // ct
			xtype: "tabpanel",
			region: "center",
			frame: false,
			border: false,
			activeTab: 0,
			enableTabScroll: true,
			anchor: "100% 0",
			items:[{ 
					xtype: "panel",
					title: "Account",
					frame: false,
					border: false,
					autoScroll: true,
					anchor: "100%",
					bodyStyle: "padding:20px;",
					items:[{
						xtype: "form",
						layout: "form",
						id: "regForm",
						labelWidth: 130,
						defaultType: 'textfield',
						frame: false,
						border: false,
						defaults: {
							width: "100%"
						},
						autoScroll: true,
						items:[{
								fieldLabel: 'User Name',
								name: 'userName',
								allowBlank: false,
								disabled: true
							},{
								fieldLabel: 'Old Password',
								name: 'oldPassword',
								allowBlank: true,
								inputType: "password",
								validator: basicVal
							},{
								fieldLabel: 'New Password',
								id: "settingsWinNewPassword",
								name: 'password',
								allowBlank: true,
								inputType: "password",
								validator: basicVal
							},{
								fieldLabel: 'Confirm New Password',
								name: 'confirmPassword',
								allowBlank: true,
								inputType: "password",
								validator: function(val){
									if (getCmps().frmAccount.findById("settingsWinNewPassword").getValue() != val)
										return "Confirmation Passwords do not Match.";
									else return true;
								}
							},{
								fieldLabel: 'First Name',
								name: 'firstName',
								allowBlank: false
							},{
								fieldLabel: 'Last Name',
								name: 'lastName',
								allowBlank: false
							},{
								fieldLabel: 'Email',
								name: 'email',
								allowBlank: false,
								vtype: "email",
								vtypeText: "Please Enter a Valid Email Address."
							}]
					}]
				}/*,{ 
					xtype: "panel",
					title: "Preferences",
					frame: false,
					border: false,
					autoScroll: true,
					anchor: "100%"
				}*/]
		}],
		buttons:[{
				text: "Save",
				handler: (function(){
					var params = {method: "saveUserAccountSettings"};
					params.data = Ext.encode(getCmps().frmAccount.getForm().getValues());
					Ext.Ajax.request({
						url: 'loginHelper.cfc',
						params: params,
						success: (function(response, options){
							var response = Ext.decode(response.responseText);
							var cmps = getCmps();
							if (!response.status){
								cmps.frmop.markInvalid(response.message);
							}
							else{
								cmps.frmop.setValue("");
								cmps.frmnp.setValue("");
								cmps.frmcp.setValue("");
								cmps.frmAccount.getForm().clearInvalid();
								Ext.Msg.alert("","User Account Settings Saved.");
							}
						}),
						failure: (function(r){_d(r.responseText)})
					});
				})
			},{
				text: "Cancel",
				handler: (function(){that.hide();})
		}],
		listeners: {
			"show": function(cmp){
				Ext.Ajax.request({
					url: 'loginHelper.cfc',
					params: {method: "userAccountSettings"},
					success: (function(response, options){
						var response = Ext.decode(response.responseText);
						//_d(response);
						var cmps = getCmps();
						cmps.frmAccount.getForm().reset();
						cmps.frmUserName.setValue(response.user.username);
						cmps.frmFirstName.setValue(response.user.firstname);
						cmps.frmLastName.setValue(response.user.lastname);
						cmps.frmEmail.setValue(response.user.email);
					}),
					failure: (function(r){_d(r.responseText)})
				});
			}
		} // end listeners
	};
	var ldMsk = null;
	// ------------------------------------------------
	// private functions
	// ------------------------------------------------
	function getCmps(){
		return {
			frmAccount: that.items.get(0).items.get(0).items.get(0),
			frmUserName: that.items.get(0).items.get(0).items.get(0).items.get(0),
			frmop: that.items.get(0).items.get(0).items.get(0).items.get(1),
			frmnp: that.items.get(0).items.get(0).items.get(0).items.get(2),
			frmcp: that.items.get(0).items.get(0).items.get(0).items.get(3),
			frmFirstName: that.items.get(0).items.get(0).items.get(0).items.get(4),
			frmLastName: that.items.get(0).items.get(0).items.get(0).items.get(5),
			frmEmail: that.items.get(0).items.get(0).items.get(0).items.get(6)
		};
	}; // end getCmps
	function basicVal(val){
		var myRegxp = /^([a-zA-Z0-9_-]+)$/;
		var isVal = true;
		if(myRegxp.test(val) == false){
			isVal = "Only Letters and Numbers are Allowed.";
		}
		if (val.length < 3){
			isVal = "Must be More Than 3 Characters.";
		}
		return isVal;
	}; // end basicVal
	// ------------------------------------------------
	// public properties
	// ------------------------------------------------
	// ------------------------------------------------
	// public methods
	// ------------------------------------------------
	// ------------------------------------------------
	// constructor code
	// ------------------------------------------------
	Ext.apply(this, cfg);
	// apply private cfgs
	Ext.apply(this, windowCfg);
	dlr.cmp.workSpace.settingsWindow.superclass.constructor.call(this, cfg);
	// events
};
Ext.extend(dlr.cmp.workSpace.settingsWindow, Ext.Window);
Ext.reg("settingsWindow", dlr.cmp.workSpace.settingsWindow);