using System;
using System.Xml;
using System.IO;
using System.Globalization;
using System.Data.SqlClient;

/* =====================================================================

  File:      Demographics.aspx.cs for Adventure Works Cycles Storefront Sample
  Summary:   Collects and saves demographics information about users.
  Date:	     November 3, 2003

---------------------------------------------------------------------

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= */

namespace Microsoft.Samples.SqlServer
{
	public partial class Demographics_aspx : System.Web.UI.Page
	{
        protected void BirthdayCustomValidator_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
		{
			try
			{
				if (!args.Value.Equals(String.Empty))
				{
					DateTime time = DateTime.Parse(args.Value, CultureInfo.CurrentUICulture);
				}

				args.IsValid = true;
				return;
			}
			catch (System.Exception)
			{
			}
			args.IsValid = false;
		}

        protected void SubmitButton_Click(object sender,
								System.Web.UI.ImageClickEventArgs e)
		{
			String customerID = User.Identity.Name;

			//Convert the information entered into the web form to an XML string.
			using (StringWriter sw = new StringWriter())
			{
				using (XmlTextWriter xtw = new XmlTextWriter(sw))
				{
					xtw.WriteStartElement("IndividualSurvey",
										  "http://schemas.adventure-works.com/Individual/Survey");
					xtw.WriteAttributeString("xmlns", "is", null,
											 "http://schemas.adventure-works.com/Individual/Survey");
					xtw.WriteElementString("TotalPurchaseYTD",
										   "http://schemas.adventure-works.com/Individual/Survey",
										   "0.00");
					MaybeWriteSurveyElement(xtw, "BirthDate",
											BirthdayTextBox.Text);
					MaybeWriteSurveyElement(xtw, "MaritalStatus",
											MaritalStatusTextBox.Text);
					MaybeWriteSurveyElement(xtw, "YearlyIncome",
											AnnualIncomeDropDownList.
											SelectedItem.Value.ToString());
					MaybeWriteSurveyElement(xtw, "Gender",
											GenderDropDownList.SelectedItem.
											Value.ToString());
					MaybeWriteSurveyElement(xtw, "NoOfChildren",
											NumberOfChildrenTextBox.Text);
					MaybeWriteSurveyElement(xtw, "NoOfChildrenAtHome",
											NumberOfChildrenAtHomeTextBox.Text);
					MaybeWriteSurveyElement(xtw, "Education",
											EducationTextBox.Text);
					MaybeWriteSurveyElement(xtw, "Occupation",
											OccupationTextBox.Text);
					MaybeWriteSurveyElement(xtw, "HomeOwnerFlag",
											XmlConvert.ToString(HomeOwnerCheckBox.
																Checked));
					MaybeWriteSurveyElement(xtw, "NoOfCarsOwned",
											NumberOfCarsOwnedTextBox.Text);

					string[] hobbies = HobbiesTextBox.Text.Split(new Char[] {
						','
					}, true);

					foreach (string hobby in hobbies)
					{
						MaybeWriteSurveyElement(xtw, "Hobby", hobby.Trim());
					}

					MaybeWriteSurveyElement(xtw, "Comments",
											CommentsTextBox.Text);
					xtw.Flush();
					xtw.Close();
				}

				CustomersDB customer = new CustomersDB();

				try
				{
					customer.AddCustomerSurvey(Int32.Parse(customerID,
														   CultureInfo.
														   InvariantCulture),
											   sw.ToString());
				}
				catch (SqlException se)
				{
					MyError.Text =
					"An exception occurred during the processing of your information: " +
								   se.ToString();
				}
				Response.Redirect("ShoppingCart.aspx");
			}
		}

		private void MaybeWriteSurveyElement(XmlWriter xw, String Element,
											 String elementValue)
		{
			if (!elementValue.Equals(String.Empty))
			{
				xw.WriteElementString(Element,
									  "http://schemas.adventure-works.com/Individual/Survey",
									  elementValue);
			}
		}
		

	}
}