using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class PreCheckout : WebBlockPage { private OrderInfo OrderInfo { get { if (Session["OrderInfo"] == null) Session["OrderInfo"] = new OrderInfo(); return (OrderInfo)Session["OrderInfo"]; } } protected override void OnInit(EventArgs e) { if (ShoppingCart.GrandTotal == 0) Response.Redirect("Cart.aspx"); SecurePage = true; SingleLineLogin1.SuccessfulLogin += new EventHandler(Login1_SuccessfulLogin); base.OnInit(e); } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Session["ShippingAddressID"] = null; Utilities.PopulateStateList(ddlShipState); Utilities.PopulateCountryList(ddlShipCountry); ddlShipCountry.SelectedValue = "USA"; if (CustomerInfo != null) { LoadCustomerData(); } } } private void LoadCustomerData() { // LOAD Shipping ddlUseExistingShipping.Items.Clear(); DataTable dt = Database.Addresses_GetByCustomerID(CustomerInfo.CustomerID, false); if (dt.Rows.Count > 0) { ddlUseExistingShipping.Items.Add(new ListItem("Use a New Shipping Address", "")); foreach(DataRow dr in dt.Rows) { string AddressSnippet = (string)dr["Address"] + ", " + (string)dr["City"] + ", " + (string)dr["State"] + ", " + (string)dr["ZipCode"]; AddressSnippet = AddressSnippet.Substring(0, Math.Min(40, AddressSnippet.Length)) + "..."; ddlUseExistingShipping.Items.Add(new ListItem(AddressSnippet, dr["AddressID"].ToString())); } } txtEmailAddress.Text = CustomerInfo.EmailAddress; txtEmailAddress2.Text = CustomerInfo.EmailAddress; txtEmailAddress.Enabled = false; txtEmailAddress2.Enabled = false; // load in shipping data if its there if (OrderInfo != null && OrderInfo.ShippingAddressID > 0) { DataRow dr = Database.Addresses_GetByAddressID(OrderInfo.ShippingAddressID); txtShipFirstName.Text = (string)dr["FirstName"]; txtShipLastName.Text = (string)dr["LastName"]; txtShipAddress.Text = (string)dr["Address"]; txtShipCity.Text = (string)dr["City"]; ddlShipState.SelectedValue = (string)dr["State"]; ddlShipCountry.SelectedValue = (string)dr["Country"]; txtShipZipCode.Text = (string)dr["ZipCode"]; } else { // pre-load if there is only one existing address if (ddlUseExistingShipping.Items.Count == 2) { ddlUseExistingShipping.Items[1].Selected = true; ddlUseExistingShipping_SelectedIndexChanged(null, null); } } } protected void ddlUseExistingShipping_SelectedIndexChanged(object sender, EventArgs e) { txtEmailAddress.Text = CustomerInfo.EmailAddress; txtEmailAddress2.Text = CustomerInfo.EmailAddress; if (ddlUseExistingShipping.SelectedValue == "") { txtShipFirstName.Text = ""; txtShipLastName.Text = ""; txtShipAddress.Text = ""; txtShipCity.Text = ""; ddlShipState.SelectedValue = ""; ddlShipCountry.SelectedValue = "USA"; txtShipZipCode.Text = ""; txtShipFirstName.Enabled = true; txtShipLastName.Enabled = true; txtShipAddress.Enabled = true; txtShipCity.Enabled = true; ddlShipState.Enabled = true; ddlShipCountry.Enabled = true; txtShipZipCode.Enabled = true; txtEmailAddress.Enabled = false; txtEmailAddress2.Enabled = false; } else { DataRow dr = Database.Addresses_GetByAddressID(Int32.Parse(ddlUseExistingShipping.SelectedValue)); txtShipFirstName.Text = (string)dr["FirstName"]; txtShipLastName.Text = (string)dr["LastName"]; txtShipAddress.Text = (string)dr["Address"]; txtShipCity.Text = (string)dr["City"]; ddlShipState.SelectedValue = (string)dr["State"]; ddlShipCountry.SelectedValue = (string)dr["Country"]; txtShipZipCode.Text = (string)dr["ZipCode"]; txtShipFirstName.Enabled = false; txtShipLastName.Enabled = false; txtShipAddress.Enabled = false; txtShipCity.Enabled = false; ddlShipState.Enabled = false; ddlShipCountry.Enabled = false; txtShipZipCode.Enabled = false; txtEmailAddress.Enabled = false; txtEmailAddress2.Enabled = false; } } void Login1_SuccessfulLogin(object sender, EventArgs e) { LoadCustomerData(); } protected void btnSubmit_Click(object sender, ImageClickEventArgs e) { Page.Validate("valCheckout"); if (Page.IsValid) { // ensure that a customer is active or has been made if (CustomerInfo == null) { CustomerInfo = new CustomerInfo(); // if the user is creating an account, do that now if (txtPassword.Text.Length > 0 && txtPasswordConfirm.Text.Length > 0) { // create a re-usable account CustomerInfo.CustomerID = Database.Customers_Add(txtEmailAddress.Text,txtPassword.Text,false,true); CustomerInfo.OneTimeCustomer = false; } else { // create a one-time account CustomerInfo.CustomerID = Database.Customers_Add(txtEmailAddress.Text, Utilities.GenerateRandomString(10), true, false); CustomerInfo.OneTimeCustomer = true; } CustomerInfo.EmailAddress = txtEmailAddress.Text; } if (ddlUseExistingShipping.SelectedValue != "") OrderInfo.ShippingAddressID = Int32.Parse(ddlUseExistingShipping.SelectedValue); else if (OrderInfo.ShippingAddressID == 0) { // doesnt exist, go ahead and add it OrderInfo.ShippingAddressID = Database.Addresses_Add(CustomerInfo.CustomerID, false, txtShipFirstName.Text, txtShipLastName.Text, txtShipAddress.Text, txtShipCity.Text, ddlShipState.SelectedValue, ddlShipCountry.SelectedValue, txtShipZipCode.Text, "", txtEmailAddress.Text); } else { Database.Addresses_Update(OrderInfo.ShippingAddressID, CustomerInfo.CustomerID, false, txtShipFirstName.Text, txtShipLastName.Text, txtShipAddress.Text, txtShipCity.Text, ddlShipState.SelectedValue, ddlShipCountry.SelectedValue, txtShipZipCode.Text, "", txtEmailAddress.Text); } // also store the zipcode and state in the shopping cart ShoppingCart.ShippingState = ddlShipState.SelectedValue; ShoppingCart.Zipcode = txtShipZipCode.Text; // since we MAY have changed shipping info here, we need // to reset the shipping carrier options (they can be set on the next page) ShoppingCart.ShippingName = ""; ShoppingCart.ShippingCost = 0; OrderInfo.Comments = txtComments.Text; // all set to go to the next page Response.Redirect("ShippingOptions.aspx"); } } protected void custEmailAddress_ServerValidate(object source, ServerValidateEventArgs args) { if (txtPassword.Text.Length > 0) { DataRow dr = Database.Customers_GetNonOneTimeByEmailAddress(txtEmailAddress.Text, null); args.IsValid = (dr == null); } else args.IsValid = true; } protected void btnEnter_Click(object sender, EventArgs e) { SingleDiscount sd = ShoppingCart.MatchDiscount(txtDiscountCode.Text); if (sd != null) { if (ShoppingCart.DiscountList == null) ShoppingCart.DiscountList = new DiscountList(); sd.RunDiscount(); ShoppingCart.DiscountList.Add(sd); ShoppingCart.RunDiscountIfPresent(); CartControl1.ForceRebind(); txtDiscountCode.Text = ""; if (!sd.DiscountApplied) Page.ClientScript.RegisterStartupScript(typeof(string), "err", "alert('Please check that you have met the minimum requirements of your discount code.');", true); } else Page.ClientScript.RegisterStartupScript(typeof(string), "nosuch", "alert('Invalid discount code.');", true); } }