//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Components;

namespace CommunityServer.Controls
{
	public class InviteUser : TemplatedWebControl
	{
		#region Child Controls

		TextBox Email;
		TextBox Message;
		IButton Send;
		StatusMessage FormStatus;

		CSContext csContext = CSContext.Current;

		#endregion

		protected override void OnInit(EventArgs e) 
		{
			if (SkinName == null)                
				ExternalSkinFileName = "Skin-InviteUser.ascx";
			else 
				ExternalSkinFileName = SkinName;
			
			base.OnInit(e);
		}
		
		protected override void OnLoad(EventArgs e) 
		{
			base.OnLoad(e);

			UsersOnline.SetLocation("Inviting Users",null);
		}

		#region Skin
                
		protected override void AttachChildControls() 
		{
			Email = FindControl("Email") as TextBox;
			Message = FindControl("Message") as TextBox;
			Send = FindButton("Send");
			FormStatus = FindControl("FormStatus") as StatusMessage;

			if (Send != null)
			{
				Send.Click += new EventHandler(Send_Click);
				Send.Text = ResourceManager.GetString("Send");
			}
		}

		#endregion

		#region Event Handlers

		public void Send_Click(Object sender, EventArgs e) 
		{
			if (!Page.IsValid)
				return;

			String[] email = Email.Text.Split(';');
			
			for (int i = 0; i < email.Length; i++)
			{
				email[i] = email[i].Trim();

				if (!Globals.IsNullorEmpty(email[i]))
				{
					UserInvitation invite = new UserInvitation();
					invite.UserID = csContext.User.UserID;
					invite.Email = email[i];
					invite.Message = Message != null ? Message.Text : string.Empty;
					invite.DateInvited = DateTime.Now;

					UserInvitations.Create(invite);
				}
			}

			Email.Text = "";

			if (FormStatus != null)
			{
				FormStatus.Success = true;
				FormStatus.Visible = true;
				FormStatus.ResourceName = "InviteUser_SendSuccess";
			}
		}

		#endregion


	
	}
}
