using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Orciid.Core;

namespace Orciid
{
	public class flashcheck : PageTemplate.OrciidPage
	{
		protected System.Web.UI.WebControls.Panel CheckFailedPanel;
		protected System.Web.UI.WebControls.Panel CheckingPanel;
//		protected System.Web.UI.WebControls.Literal SuccessUrlLiteral;
//		protected System.Web.UI.WebControls.Literal KeyValuePairsLiteral;
		protected System.Web.UI.WebControls.HyperLink ContinueHyperLink;
		protected System.Web.UI.HtmlControls.HtmlGenericControl Heading;
		protected System.Web.UI.HtmlControls.HtmlGenericControl PleaseWaitSpan;
		protected System.Web.UI.HtmlControls.HtmlGenericControl Subheading;
		protected System.Web.UI.HtmlControls.HtmlGenericControl FlashNeededSpan;
		protected System.Web.UI.HtmlControls.HtmlGenericControl TrademarksSpan;
//		protected System.Web.UI.WebControls.Literal QueryStringLiteral;
		protected System.Web.UI.WebControls.Literal ScriptVariablesLiteral;

		private void Page_Load(object sender, System.EventArgs e)
		{
			string successurl = Request.QueryString["success"];
/*			if (successurl == null || successurl.Length == 0)
				// don't redirect to a real page by default, as it could have a page menu,
				// which could screw up framesets
				successurl = "about:blank"; 
			if ((string)Session["FlashAvailable"] == "yes")
				Response.Redirect(successurl);				
*/			if (Request.QueryString["flash"] == "no")
			{
				CheckFailedPanel.Visible = true;
				ContinueHyperLink.Visible = (Request.QueryString["maycontinue"] == "yes");
				Session["FlashAvailable"] = "no";
			}
			else 
			{
				CheckingPanel.Visible = true;
				CheckFailedPanel.Visible = false;
				ContinueHyperLink.Visible = false;
				Session["FlashAvailable"] = "yes";
			}
			//SuccessUrlLiteral.Text = successurl;
			string keyvaluepairs = "";
			string oldquerystring = "";
			int count = 1;
			if (successurl != null && successurl.IndexOf("%") >= 0)
				throw new Exception("Error: SuccessUrl contains query string " +
					"- the Flash version checker cannot handle this!");
			foreach (string key in Request.QueryString.Keys)
			{
				string val = Request.QueryString[key];
				oldquerystring += String.Format("&{0}={1}", Server.UrlEncode(key),
					Server.UrlEncode(val));
				if (key != "success" && count < 10)
					keyvaluepairs += String.Format("&key{0}={1}&value{0}={2}", count++,
						Server.UrlEncode(key), Server.UrlEncode(val));
			}
			//KeyValuePairsLiteral.Text = keyvaluepairs;
			//QueryStringLiteral.Text = "&" + Request.QueryString.ToString();
			ScriptVariablesLiteral.Text = String.Format(@"
<script language='JavaScript'>
	var successurl = '{0}';
	var keyvaluepairs = '{1}';
	var oldquerystring = '{2}';
</script>
", successurl, keyvaluepairs, oldquerystring);
			string querystring = "";
			foreach (string key in Request.QueryString.Keys)
				if (key != "success" && key != "flash")
					querystring += String.Format("{0}{1}={2}", (querystring == "" ? "?" : "&"),
						Server.UrlEncode(key), Server.UrlEncode(Request.QueryString[key]));
			ContinueHyperLink.NavigateUrl = Request.QueryString["success"] + querystring;
		}

		protected override object LoadPageStateFromPersistenceMedium()
		{
			// overridden because of exceptions in the base method caused 
			// by certain versions of IE on the Mac, and since we don't
			// need a view state anyway
			return null;
		}

		public override string GetHighlightedMenuItem()
		{
			return "";
		}

		public override bool HasLeftMenu()
		{
			return false;
		}

		protected override bool HasPageHeader()
		{
			return (Request.QueryString["header"] == "yes");
		}

		protected override bool HasSelectionStatus()
		{
			return false;
		}

		protected override bool CheckUserPrivileges(Orciid.Core.User user)
		{
			return true;
		}
 
		#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()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

	}
}
