using System;
using System.Web;

namespace interfacesproject
{
	/// <summary>
	/// Summary description for custommodule.
	/// </summary>
	public class custommodule : IHttpModule
	{
		public custommodule()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		#region Implementation of IHttpModule
		public void Init(System.Web.HttpApplication context)
		{
			context.BeginRequest +=
				new EventHandler(BeginRequest);
			string html = @"
<html>
Loading...
<script language=vbscript>
Sub GetDimensions()
Dim clientWidth
Dim clientHeight
clientWidth = document.body.ClientWidth
clientHeight = document.body.ClientHeight
document.myform.txtDimensions.Value = clientWidth & "";"" & clientHeight & "";""
document.myform.submit
End Sub
</script>
<body onLoad=""GetDimensions"">
<form name=""myform"" method=""POST"" action=""{0}"">
<p><input type=""hidden"" name=""txtDimensions"" size=""20""></p>
</form>
</body>
</html>";
			context.Application["__DiscoverHTML"] = html;
		}

		public void Dispose()
		{
		
		}
		#endregion

		void BeginRequest(object sender, EventArgs args)
		{
			Global g = (Global)sender;
			string dimensions = g.Request.Form["txtDimensions"];
			if (dimensions == null || dimensions == "" )
			{
				string html = (string)g.Context.Application["__DiscoverHTML"];
				string newUrl = string.Format(html,g.Request.Url.AbsolutePath);
				g.Response.Write(newUrl);
				g.CompleteRequest();
			}
			else if	(dimensions.IndexOf("ClientWidth") == -1)
			{
				string[ ] sizes = dimensions.Split(';');
				int clientWidth = System.Convert.ToInt32(sizes[0]);
				int clientHeight = System.Convert.ToInt32(sizes[1]);
				g.Context.Items["__ClientWidth"] = clientWidth;
				g.Context.Items["__ClientHeight"] = clientHeight;
			}
		}
	}
}
