using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SK.MemberWeb.Controls
{
	/**//// <summary>
	/// An enhanced radiobutton,especially for use in datagrid,and add commandName and commandArgument property
	/// </summary>
    
	[ToolboxDataAttribute("<{0}:RadioButtonPlus runat=server></{0}:RadioButtonPlus>")] 
	public class RadioButtonPlus : CheckBox, IPostBackDataHandler,IPostBackEventHandler
	{
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			if (((this.Page != null) && !this.Checked) && this.Enabled)
			{
				this.Page.RegisterRequiresPostBack(this);
			}
			if (this.GroupName.Length == 0)
			{
				this.GroupName = this.UniqueID;
			}
		}

		protected override void Render(HtmlTextWriter writer)
		{
			this.RenderInputTag(writer,this.ClientID,this.Attributes["onclick"]);
		}

		private void RenderInputTag(HtmlTextWriter writer, string clientID, string onClick)
		{
			writer.AddAttribute(HtmlTextWriterAttribute.Id, clientID);
			writer.AddAttribute(HtmlTextWriterAttribute.Type, "radio");
			writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueGroupName);
			writer.AddAttribute(HtmlTextWriterAttribute.Value, this.ValueAttribute);
			if (this.Checked)
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
			}
			if (!this.Enabled)
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
			}
			if (this.AutoPostBack)
			{
				if (onClick != null)
				{
					onClick = onClick + this.Page.GetPostBackClientEvent(this, "");
				}
				else
				{
					onClick = this.Page.GetPostBackClientEvent(this, "");
				}
				writer.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
				writer.AddAttribute("language", "javascript");
			}
			else if (onClick != null)
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
			}
			string accessKey = this.AccessKey;
			if (accessKey.Length > 0)
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, accessKey);
			}
			int tabIndex = this.TabIndex;
			if (tabIndex != 0)
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, tabIndex.ToString(NumberFormatInfo.InvariantInfo));
			}
			writer.RenderBeginTag(HtmlTextWriterTag.Input);
			writer.RenderEndTag();
		}

		bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
		{
			string strValue = postCollection[this.UniqueGroupName];
			bool bFound = false;
			if ((strValue != null) && strValue.Equals(this.ValueAttribute))
			{
				if (!this.Checked)
				{
					this.Checked = true;
					bFound = true;
				}
				return bFound;
			}
			if (this.Checked)
			{
				this.Checked = false;
			}
			return bFound;
		}


		void IPostBackDataHandler.RaisePostDataChangedEvent()
		{
			this.OnCheckedChanged(EventArgs.Empty);
		}

		public virtual string GroupName
		{
			get
			{
				string strGroupName = (string) this.ViewState["GroupName"];
				if (strGroupName != null)
				{
					return strGroupName;
				}
				return string.Empty;
			}
			set
			{
				this.ViewState["GroupName"] = value;
			}
		}

		private string UniqueGroupName
		{
			get
			{
				return this.GroupName;
			}
		}

		private string ValueAttribute
		{
			get
			{
				return this.UniqueID;
			}
		}

		[DefaultValue("")]
		public string CommandArgument
		{
			get
			{
				string strCommandArgument = (string) this.ViewState["CommandArgument"];
				if (strCommandArgument != null)
				{
					return strCommandArgument;
				}
				return string.Empty;
			}
			set
			{
				this.ViewState["CommandArgument"] = value;
			}
		}

		[DefaultValue("")]
		public string CommandName
		{
			get
			{
				string strCommandName = (string) this.ViewState["CommandName"];
				if (strCommandName != null)
				{
					return strCommandName;
				}
				return string.Empty;
			}
			set
			{
				this.ViewState["CommandName"] = value;
			}
		}

		#region IPostBackEventHandler Members

		public void RaisePostBackEvent(string eventArgument)
		{
			this.RaiseBubbleEvent(this,new CommandEventArgs(this.CommandName, this.CommandArgument));
		}

		#endregion
	}
}