/**************************************************************************************************

NetPolls - The web poll engine
Copyright (c) 2007 Data Illusion (http://www.dataillusion.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

************************************************************************************************/
namespace DataIllusion.NetPolls.WebAdmin.UserControls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using DataIllusion.NetPolls.Data;
	using DataIllusion.NetPolls.DataAccess;

	/// <summary>
	///		Summary description for PollListControl.
	/// </summary>
	public partial class PollListControl : System.Web.UI.UserControl
	{

		public string SurveyTitle 
		{
			get { return PollListDropDownList.SelectedItem.Text; } 
		}


		/// <summary>
		/// Id of the selected survey 
		/// </summary>
		public int PollId
		{
			get { return (ViewState["PollID"]==null) ? -1 : int.Parse(ViewState["PollID"].ToString()); }
			set { ViewState["PollID"] = value; }
		}

		
		// Bind data to page controls
		public void BindDropDown()
		{
	
			Polls polls = new Polls();
			PollDataCollection pollDataCollection = polls.GetAllPolls();
			ListItem listItem = new ListItem();
			PollListDropDownList.Items.Clear();

			foreach(PollData pollData in pollDataCollection)
			{
				listItem = new ListItem(pollData.Title,pollData.Id.ToString());				
				if (pollData.Id == PollId)
					listItem.Selected = true;
				PollListDropDownList.Items.Add(listItem);
			}
		}


		protected void Page_Load(object sender, System.EventArgs e)
		{
			string query = '?' + Request.QueryString.ToString();
			int surveyParamIndex = query.IndexOf("pollid=");
			
			if (surveyParamIndex > 0)
			{
				query = query.Substring(0, query.IndexOf("pollid=")); 
			}
			else if (query.Length > 0)
			{
				query = query + '&';
			}
		
			PollListDropDownList.Attributes.Add("OnChange","document.location.href = '" +Context.Request.FilePath + query + "pollid='+this[this.selectedIndex].value;return false;");
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion
	}
}
