Ext.ns('Ext.pv.security');
Ext.pv.security.RequestFriendshipDialog = Ext.extend(Ext.Window, {
	frame : true,
	layout : 'border',
	autoScroll : true,
    width : 620,
    height : 380,
    modal : true,
    resizable : false,
    
    direction : 'ltr',
    personId : 0,
    contextPath : null,
	showRequestAccessMsg : false,
    
	// Localized strings
	title : 'Join the Friends and Family',
	firstnameLabel : 'First Name',
	lastnameLabel : 'Last Name',
	emailLabel : 'Email',
	commentLabel : 'Add a personal note',
	requestAccessText : 'Access to this page is limited to family and friends. Use the following form to request access.',
	instructionText : 'Your request will be sent to the administrator for approval. Once approved, you will be sent an email with the information you need to access the site.',
	okButtonLabel : 'Ok',
	cancelButtonLabel : 'Cancel',
	alertTitle : 'Message',
	invalidInputMessage : 'Form is invalid.',
	successMessage : 'Sending request ... ',

	/**
	 * initComponent
	 * 
	 * @protected
	 */
	initComponent : function() {

		if (this.direction=='rtl') {
			this.style = 'direction:rtl';
		}
	
		var infoText = '';
		if (this.showRequestAccessMsg) {
			infoText += '<p style="color: #FF0000;">' + this.requestAccessText + '</p><br />';
		}
		infoText += '<p>' + this.instructionText + '</p>';
	
		this.form = this.buildForm();
		this.items = [{
				layout : 'fit',
				region : 'north',
				bodyStyle: 'background-color:#f0f0f0; padding: 10px;',
				html : infoText
			},this.form
		];
	
		this.buttons = [ {
			text : this.okButtonLabel,
			handler : this.onOk,
			scope : this
	    },{
			text : this.cancelButtonLabel,
			handler : this.onCancel,
			scope : this
	    }];
		
		// super
		Ext.pv.security.RequestFriendshipDialog.superclass.initComponent.call(this);
	},
	
	buildForm : function() {
		return new Ext.form.FormPanel({
			region : 'center',
			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 : 'emailAddress'
			},{
				xtype : 'textarea',
				width : 400,
				height : 100,
				fieldLabel : this.commentLabel,
				name : 'requestText'
			}]
		});
	},
	
	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 + '/siteContent/requestFriendship.do',
			success: function(form, action) {
				if(action.result.error!=null) {
					Ext.Msg.alert(myThis.alertTitle, action.result.error);
				} else if(action.result.message!=null) {
					myThis.close();
					Ext.Msg.alert(myThis.alertTitle, action.result.message);
				} else {
					myThis.close();
				}
			},
			failure: function(form, action) {
				Ext.Msg.alert(myThis.alertTitle, action.result.error);
			}
		});	
	}, 
	
	onCancel : function() {
		this.close();
	}
    
});
Ext.reg('RequestFriendshipDialog',Ext.pv.security.RequestFriendshipDialog);
