Ext.ns('Ext.pv.fandf');
Ext.pv.fandf.AddDialog = Ext.extend(Ext.Window, {
	frame : true,
	layout : 'fit',
	forceLayout : true,
	autoScroll : true,
	width : 420,
	height : 200,
	modal : true,
	resizable : true,
	
	// Localized strings
	title : 'Add Guest',
	firstnameLabel : 'First Name',
	lastnameLabel : 'Last Name',
	emailLabel : 'Email',
	relationshipLabel : 'Relationship/Comment',
	okButtonText : 'OK',
	cancelButtonText : 'Cancel',
	alertTitle : 'Message',
	invalidInputMessage : 'Form is invalid.',

	direction : 'ltr',
	personId : 0,
	contextPath : null,
	callback : null,
	

	/**
	 * initComponent
	 * 
	 * @protected
	 */
	initComponent : function() {
		
		if (this.direction=='rtl') {
			this.style = 'direction:rtl';
		}
	
		this.form = this.buildForm();
		this.items = [this.form];
		
		// Build the buttons
		this.buttons = [ {
			text : this.okButtonText,
			handler : this.onOk,
			scope : this
		},{
			text : this.cancelButtonText,
			handler : this.onCancel,
			scope : this
		} ];
		
		// super
		Ext.pv.fandf.AddDialog.superclass.initComponent.call(this);
		
	},
	
	buildForm : function() {
		return new Ext.form.FormPanel({
			autoScroll : true,
			labelAlign : 'left',
			labelWidth: 150,
			bodyStyle : 'padding: 10 10 10 10px',
			defaultType : 'textfield',
			defaults : {
				width : 200,
				maxLength : 256,
				allowBlank : false,
				style: {marginBottom: '0px', marginTop: '0px'}
			},
			items : [{
				xtype : 'hidden',
				name : 'personId',
				value : this.personId
			},{
				fieldLabel : this.firstnameLabel,
				name : 'firstName'
			},{
				fieldLabel : this.lastnameLabel,
				name : 'lastName'
			},{
				xtype : 'emailfield',
				fieldLabel : this.emailLabel,
				name : 'email'
			},{
				fieldLabel : this.relationshipLabel,
				name : 'relationship',
				allowBlank : true
			}]
		});
	},
	
	onOk : function() {
		if (!this.form.getForm().isValid()) {
			Ext.Msg.alert(this.alertTitle,this.invalidInputMessage);
			return;
		}
		
		var myThis = this;
		this.form.getForm().submit({
			clientValidation: true,
			url: this.contextPath + '/siteAdmin/family/addFamilyAndFriends.do',
			success: function(form, action) {
				if(action.result.message!=null) {
					Ext.Msg.alert(myThis.alertTitle, action.result.message,
						function() {
							myThis.callback();
							myThis.close();
						},
						this
					);
				} else {
					myThis.callback();
					myThis.close();
				}
			},
			failure: function(form, action) {
				Ext.Msg.alert(myThis.alertTitle, action.result.error);
			}
		});
	},
	
	onCancel : function() {
		this.close();
	}

});
