using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Configuration;

namespace VoServices.Footprint.Web
{
	/// <summary>
	///		Summary description for Layout.
	/// </summary>
	public class BasePage : System.Web.UI.Page
	{
		public string Title;
		public string Keywords;
		public string Description;
		public string Section;
		public string Language;

		protected string _relativePath;
		protected SqlConnection _databaseConnection;
		//protected Smtp _smtp;

		public BasePage():base()
		{
			_databaseConnection = null;
			Title = "";
			Keywords = "";
			Description = "";
			Section = "general";
			Language = "en";
		}

		public SqlConnection DatabaseConnection
		{
			get
			{
//				try
//				{
					if (_databaseConnection == null)
					{
						_databaseConnection = new SqlConnection(ConfigurationSettings.AppSettings["SqlConnectionString"]);
						_databaseConnection.Open();
					}

					return _databaseConnection;
//				}
//				catch
//				{
//					return null;
//				}
				
			}
		}

		//		public Smtp Smtp
		//		{
		//			get
		//			{
		//				try
		//				{
		//					if (_smtp == null)
		//					{
		//						_smtp = new Smtp((string) Application["SmtpConnectionString"]);
		//					}
		//
		//					return _smtp;
		//				}
		//				catch
		//				{
		//					return null;
		//				}
		//				
		//			}
		//		}

		public string RelativePath
		{
			get
			{
				return _relativePath;
			}
		}

		public string CurrentWebServiceUrl
		{
			get
			{
				return ConfigurationSettings.AppSettings["CurrentWebServiceUrl"];
			}
		}

		protected void CheckTimeout()
		{
			if ((string) Session["UserWebServiceId"] == "") Response.Redirect("timeout.aspx");
		}

		protected bool IsUserLoggedIn
		{
			get
			{
				return ((string) Session["UserWebServiceId"] != "");
			}
		}

		protected string LoggedInUserGuid
		{
			get
			{
				return (string) Session["UserWebServiceId"];
			}
		}

		protected string OriginalReferer
		{
			get
			{
				return (string) ViewState["OriginalReferer"];
			}
		}

		override protected void OnInit(EventArgs e)
		{
			_relativePath = Request.ApplicationPath;
			if (_relativePath == "/") _relativePath = "";

			if (Title == "") Title = (string) Application["DefaultPageTitle"];

			Response.Expires = -1;
			//Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");

			this.Load += new System.EventHandler(this.Page_BeforeLoad);

			base.OnInit(e);
		}

		private void Page_BeforeLoad(object sender, System.EventArgs e)
		{
			if (!IsPostBack)
			{
				ViewState["OriginalReferer"] = Request.ServerVariables["HTTP_REFERER"];
			}
		}

		override protected void OnUnload(EventArgs e)
		{
			if (_databaseConnection != null)
			{
				_databaseConnection.Close();
				_databaseConnection.Dispose();
			}

			//			if (_smtp != null)
			//			{
			//				_smtp.Disconnect();
			//			}
		}

		public DataSet ExecuteCommand(SqlCommand cmd)
		{
			SqlDataAdapter da = new SqlDataAdapter(cmd);
			DataSet ds = new DataSet();

			da.Fill(ds);

			da.Dispose();
			cmd.Dispose();

			return ds;
		}

		public void ExecuteCommandNonquery(SqlCommand cmd)
		{
			cmd.ExecuteNonQuery();
		}
	}
}
