TaxTool.TellFriend = function()
{
	var win = null;
	var button;
	var form;

	return {
		sendForm: function()
		{
			if (!form.getForm().isValid()) { return; }

			win.body.mask();

			Ext.Ajax.request({
				url: 'tell-a-friend.php',
				method: 'POST',
				form: form.getForm().getEl().dom,
				timeout: 250000,

				success: function (result, request)
				{
					if (result.responseText.indexOf('succ:') == 0)
					{
						var wnd = Ext.MessageBox.alert('Success', result.responseText.substring(5));
						var delayedHide = new Ext.util.DelayedTask(wnd.hide, wnd).delay(5000); 
					}
					else Ext.MessageBox.alert('Failed', result.responseText);

					win.body.unmask();
					win.hide();
				},

				failure: function (result, request)
				{
					Ext.MessageBox.alert('Failed', result.responseText);

					win.body.unmask();
					win.hide();
				}
			});
		},

		createParts: function()
		{
			send_button = new Ext.Button({
				text: 'Send e-mail'
			});
			send_button.on('click', function() { TaxTool.TellFriend.sendForm();});
			
			form = new Ext.FormPanel({
				width: 310,
				height: 300,
				frame: true,
				labelWidth: 75,
				labelAlign: 'top',
				bodyStyle: 'padding:5px;',
				monitorValid: true,

				items: [
					new Ext.form.TextField({
							fieldLabel: '<b>Enter your email address</b>',
							vtype: 'email',
							name: 'email',
							allowBlank: false,
							anchor: '100%'
					}),
					new Ext.form.TextField({
							fieldLabel: '<b>Enter your first friend\'s email address</b>',
							vtype: 'email',
							name: 'friend_email1',
							allowBlank: false,
							anchor: '100%'
					}),
					new Ext.form.TextField({
							fieldLabel: 'Enter your second friend\'s email address (optional)',
							vtype: 'email',
							name: 'friend_email2',
							allowBlank: true,
							anchor: '100%'
					}),
					new Ext.form.TextField({
							fieldLabel: 'Enter your third friend\'s email address (optional)',
							vtype: 'email',
							name: 'friend_email3',
							allowBlank: true,
							anchor: '100%'
					}),
					new Ext.form.TextField({
							fieldLabel: 'Enter your fourth friend\'s email address (optional)',
							vtype: 'email',
							name: 'friend_email4',
							allowBlank: true,
							anchor: '100%'
					}),
					new Ext.form.TextField({
							fieldLabel: 'Enter your fifth friend\'s email address (optional)',
							vtype: 'email',
							name: 'friend_email5',
							allowBlank: true,
							anchor: '100%'
					}),
					new Ext.form.TextArea({
							fieldLabel: '<b>Send a personalized message</b>',
							name: 'message',
							allowBlank: false,
							anchor: '100%',
							height: '100'
					}), {
						items: [send_button],
						bodyStyle: 'margin: 8px 0px 13px 100px;'
					}, {
						bodyStyle: 'font-size: 10px;',
						html: 'Please note: Your personal tax information will not be shared with your friends.'
					}
				]
			});

			win = new Ext.Window({
				layout: 'fit',
				width: 320,
				height: 540,
				resizable: false,
				closeAction: 'hide',
				title: 'Tell a friend about this tool',
				items: [form],
				shadow: true,
				modal: false,
				border: false

				/*
				buttons: [{
					text: 'Send e-mail',
					handler: function() { TaxTool.TellFriend.sendForm(); }
				}]
				*/
			});
		},

		buttonClickedHandler: function(th, e)
		{
			if (!win) { this.createParts(); }
			win.show(th);
		},

		init: function()
		{
			button = Ext.get('tell_a_friend');
			button.on('click', function(e){ TaxTool.TellFriend.buttonClickedHandler(this, e); } );
		}
	};
}();
