using System;
using System.IO;
using System.Text;
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 System.Xml;
using System.Xml.XPath;

namespace baoge.tongxun
{
	/// <summary>
	/// Sign 的摘要说明。
	/// </summary>
	public class Sign : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.TextBox TextBoxName;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator4;
		protected System.Web.UI.WebControls.Label Label2;
		protected System.Web.UI.WebControls.TextBox TextBoxEMail;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator3;
		protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
		protected System.Web.UI.WebControls.Label Label3;
		protected System.Web.UI.WebControls.TextBox TextBoxHomepageTitle;
		protected System.Web.UI.WebControls.Label Label4;
		protected System.Web.UI.WebControls.TextBox TextBoxHomepageURL;
		protected System.Web.UI.WebControls.Label Label5;
		protected System.Web.UI.WebControls.TextBox TextBoxLocation;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
		protected System.Web.UI.WebControls.Label Label6;
		protected System.Web.UI.WebControls.TextBox TextBoxComments;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
		protected System.Web.UI.WebControls.CheckBox CheckBoxPrivate;
		protected System.Web.UI.WebControls.Button ButtonContinue;
		protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
		protected System.Web.UI.HtmlControls.HtmlForm Form1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
		}


		private void addTextElement( XmlDocument doc, XmlElement nodeParent, string strTag, string strValue )
		{
			XmlElement nodeElem = doc.CreateElement( strTag );
			XmlText nodeText = doc.CreateTextNode( strValue );
			nodeParent.AppendChild( nodeElem );
			nodeElem.AppendChild( nodeText );
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.ButtonContinue.Click += new System.EventHandler(this.ButtonContinue_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void ButtonContinue_Click(object sender, System.EventArgs e)
		{
			//加载通讯录数据库
			XmlDocument xmldoc = new XmlDocument();
			xmldoc.Load( Server.MapPath("guestbook.xml") );

			//获取保密状态
			string strPrivate;
			if ( CheckBoxPrivate.Checked )
				strPrivate = "yes";
			else
				strPrivate = "no";

			//创建一个新XML元素
			XmlElement elem = xmldoc.CreateElement("guest");
			elem.SetAttribute("private", strPrivate);

			//将新的用户信息加在开始
			xmldoc.DocumentElement.PrependChild(elem);

			addTextElement( xmldoc, elem, "name", TextBoxName.Text );
			addTextElement( xmldoc, elem, "email", TextBoxEMail.Text );
			addTextElement( xmldoc, elem, "homepage", TextBoxHomepageTitle.Text );

			XmlAttribute newAttr = xmldoc.CreateAttribute("url");
			newAttr.Value = TextBoxHomepageURL.Text;

			elem.LastChild.Attributes.Append( newAttr );
			
			addTextElement( xmldoc, elem, "location", TextBoxLocation.Text );
			addTextElement( xmldoc, elem, "comment", TextBoxComments.Text );

			//记录时间
			string strDate = DateTime.Now.ToLongDateString() + 
				" - " + 
				DateTime.Now.ToLongTimeString(); 

			addTextElement( xmldoc, elem, "date", strDate );

			xmldoc.Save( Server.MapPath("guestbook.xml") );

			Response.Redirect("view.aspx");
		}
	}
}
