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

using System;
using System.Text;
using System.Web.UI.WebControls;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Provides a simple way to specify a specific button to fire when "Enter" is clicked with in 
	/// the textbox
	/// </summary>
	public class DefaultButtonTextBox : TextBox
	{
		public DefaultButtonTextBox()
		{

		}

		#region DefaultButton
		private WebControl _defaultButton = null;

		/// <summary>
		/// Attempts to find a reference to the Control specified in the Button property
		/// </summary>
		protected WebControl DefaultButton
		{
			get
			{
				if(_defaultButton == null)
				{
					if(Button != null)
					{
						_defaultButton = FindControl(Button) as WebControl;
						if(_defaultButton != null)
						{
							if(!(_defaultButton is Button || _defaultButton is LinkButton || _defaultButton is ImageButton))
								throw new ArgumentException("DefautlButton Property value must be of Type Button, LinkButton, or ImageButton");
						}
					}
				}
				return _defaultButton;
			}
		}

		string _button;
		/// <summary>
		/// The ID of the button to wire up to this instance of TextBox
		/// </summary>
		public string Button
		{
			get{ return _button;}
			set {_button = value;}
		}
		#endregion

		#region PreRender
		/// <summary>
		/// Override OnPreRender and test to see if a Button is wired up to the TextBox. If so, render the javascript
		/// and set the textbox's attributes
		/// </summary>
		protected override void OnPreRender(EventArgs e)
		{
			if(DefaultButton != null)
			{
				ButtonScript();
				this.Attributes.Add("onkeydown", string.Format(OnKeyDownButtonAttribute,DefaultButton.ClientID));
			}
			base.OnPreRender (e);
		}
		#endregion

		#region JavaScript

		/// <summary>
		/// OnKeyDown Attribute. We use Button, Event ...since just button did not work in FireFox :)
		/// </summary>
		const string OnKeyDownButtonAttribute = "KeyDownHandler{0}(event);";

		/// <summary>
		/// Renders the javascript function to fire our postback
		/// </summary>
		protected void ButtonScript()
		{
			StringBuilder sb = new StringBuilder();
			sb.Append("<script language=\"javascript\" type=\"text/javascript\">\n");
			sb.Append("<!--\n");
			sb.Append("function KeyDownHandler");
			sb.Append(DefaultButton.ClientID);
			sb.Append("(event)\n");
			sb.Append("{\n");
			sb.Append("	if (event.keyCode == 13)\n");
			sb.Append("	{\n");
			sb.Append("		event.returnValue = false;\n");
			sb.Append("		event.cancel = true;\n");
			sb.Append("     ");
			sb.Append(Page.GetPostBackClientEvent(this.DefaultButton, ""));
			//sb.Append("     __doPostBack(btn)\n");
			sb.Append("	}\n");
			sb.Append("}\n");
			sb.Append("\n");
			sb.Append("//-->\n");
			sb.Append("</script>\n");
			Page.RegisterStartupScript(DefaultButton.ClientID, sb.ToString());
			Page.RegisterRequiresPostBack(this);
			Page.GetPostBackEventReference(this);
		}
		#endregion
	}
}
