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

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

namespace CommunityServer.Components
{
	/// <summary>
	/// Summary description for ConfirmationPage.
	/// </summary>
	public class CSConfirmationPage : CSFormPage
	{
		public CSConfirmationPage()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		private bool _IsInEdit = false;
		public bool IsInEdit
		{
			get {return this._IsInEdit;}
			set {this._IsInEdit = value;}
		}

		private string _message = "You will lose any non-saved text";
		public string Message
		{
			get{return _message;}
			set{_message = value;}
		}

		protected override void OnPreRender(EventArgs e)
		{
			//If we are in edit mode, register the script
			if(IsInEdit)
			{
				Page.RegisterClientScriptBlock("ConfirmationBeforeLeaving",string.Format("{0}{1}{2}",scriptStart,Message,scriptEnd));
			}
			base.OnPreRender (e);
		}



		const string scriptStart = "<script language=\"javascript\">g_blnCheckUnload = true;function RunOnBeforeUnload() { if (g_blnCheckUnload) {window.event.returnValue = '";
		const string scriptEnd = "';} }  function bypassCheck() {    g_blnCheckUnload  = false;   }</script>";

		public static readonly string ByPassFuncationName = "bypassCheck();";

		public static void RegisterControlForByPass(WebControl cntrl)
		{
            string script = ByPassFuncationName;
            string currentScript = cntrl.Attributes["onclick"];
            if(!Globals.IsNullorEmpty(script))
            {
                script = currentScript + script;
            }
            
			cntrl.Attributes.Add("onclick",script);
		}


		protected override void Render(HtmlTextWriter writer)
		{

			//If we are in edit mode, wire up the onbeforeunload event
			if(IsInEdit)
			{
				TextWriter tempWriter = new StringWriter();
				base.Render(new HtmlTextWriter(tempWriter));
				writer.Write(Regex.Replace(tempWriter.ToString(),"<body","<body onbeforeunload=\"RunOnBeforeUnload()\"",RegexOptions.IgnoreCase));
			}
			else
			{
				base.Render(writer);
			}
		}
	}
}
