void signUpButton_Click(object sender, EventArgs e)
        {
            String emailAddress        = emailBox.Text.Trim();
            String confirmEmailAddress = confirmEmailBox.Text.Trim();

            if (emailAddress == String.Empty)
            {
                ErrorMessage = "E-mail is required";
            }

            if (emailAddress != confirmEmailAddress)
            {
                ErrorMessage = "E-mail addresses do not match";
            }

            if (!Regex.IsMatch(emailAddress, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
            {
                ErrorMessage = "E-mail address is not valid";
            }

            CustomerContactTableAdapter customerContactAdapter = new CustomerContactTableAdapter();

            customerContactAdapter.Insert(StationId, DateTime.Now, emailAddress, null, null);

            InfoMessage = "E-mail address " + emailAddress + " has been added to the mailing list. Thank you!";
            RedirectToHomePage();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Order == null || Order.StationId != StationId || Order.OrderStatusId != (int)OrderStatus.New)
            {
                OrderId = 0;
                Response.Redirect("~/Cart.aspx");
            }

            com.paypal.sdk.services.NVPCallerServices caller = PayPalAPI.PayPalAPIInitialize(IsDev);
            NVPCodec encoder = new NVPCodec();

            encoder["METHOD"] = "GetExpressCheckoutDetails";
            encoder["TOKEN"]  = Session["TOKEN"].ToString();

            string paypalRequest  = encoder.Encode();
            string paypalResponse = caller.Call(paypalRequest);

            NVPCodec decoder = new NVPCodec();

            decoder.Decode(paypalResponse);

            String billingEmailAddress = String.Empty;
            String firstName           = String.Empty;
            String lastName            = String.Empty;
            String phoneNumber         = String.Empty;

            /*
             * String address1 = String.Empty;
             * String address2 = String.Empty;
             * String city = String.Empty;
             * String stateCode = String.Empty;
             * String zipCode = String.Empty;
             */

            string strAck = decoder["ACK"];

            if (strAck != null && (strAck == "Success" || strAck == "SuccessWithWarning"))
            {
                Session["PAYERID"] = decoder["PAYERID"];

                billingEmailAddress = decoder["EMAIL"];
                firstName           = decoder["FIRSTNAME"];
                lastName            = decoder["LASTNAME"];
                phoneNumber         = decoder["PHONENUM"];
            }
            else
            {
                /*
                 * string pStrError =
                 *  "ErrorCode=" + decoder["L_ERRORCODE0"] + "&" +
                 *  "Desc=" + decoder["L_SHORTMESSAGE0"] + "&" +
                 *  "Desc2=" + decoder["L_LONGMESSAGE0"];
                 *
                 * Response.Redirect("APIError.aspx?" + pStrError);
                 * */

                ErrorMessage = decoder["L_LONGMESSAGE0"];
                //Response.Redirect("~/Cart.aspx");
                ResetAndRedirect();
            }


            OrderLineItemTableAdapter orderLineItemAdapter = new OrderLineItemTableAdapter();

            OrderTableAdapter             orderAdapter  = new OrderTableAdapter();
            CertificateNumberTableAdapter numberAdapter = new CertificateNumberTableAdapter();

            decimal subtotal = 0.0m;

            foreach (DollarSaverDB.OrderLineItemRow lineItem in Order.LineItems)
            {
                int numberAssigned = Convert.ToInt32(numberAdapter.Assign(lineItem.OrderLineItemId));

                if (numberAssigned != lineItem.Quantity)
                {
                    if (numberAssigned == 0)
                    {
                        ErrorMessage = "We're sorry, " + lineItem.Certificate.AdvertiserName + " is no longer available";
                        orderLineItemAdapter.Delete(lineItem.OrderLineItemId);
                    }
                    else
                    {
                        lineItem.Quantity = numberAssigned;
                        orderLineItemAdapter.Update(lineItem);

                        ErrorMessage = "We're sorry, " + lineItem.Certificate.AdvertiserName + " is no longer available in the quantity you requested. Please review your updated order and click on the chechout button if you would like to purchase the new quantity";
                    }

                    ResetAndRedirect();
                }


                subtotal += lineItem.Total;
            }

            // Check max purchase qty for Deal of the Week
            if (Station.StationSiteType == SiteType.DealOfTheWeek)
            {
                CertificateTableAdapter            certificateAdapter = new CertificateTableAdapter();
                DollarSaverDB.CertificateDataTable certificateTable   = certificateAdapter.GetCurrentDeal(StationId);

                if (certificateTable.Count == 1)
                {
                    DollarSaverDB.CertificateRow deal = certificateTable[0];

                    if (deal.MaxPurchaseQty > 0)
                    {
                        foreach (DollarSaverDB.OrderLineItemRow lineItem in Order.LineItems)
                        {
                            if (lineItem.CertificateId == deal.CertificateId)
                            {
                                int pastQty = Convert.ToInt32(orderLineItemAdapter.GetQtyByConsumer(firstName, lastName, billingEmailAddress,
                                                                                                    null, null, null, Order.ShippingEmail, deal.CertificateId));

                                if (pastQty + lineItem.Quantity > deal.MaxPurchaseQty)
                                {
                                    ErrorMessage = "Sorry, the maximum purchase quantity per person for the Deal of the Week is " + deal.MaxPurchaseQty + ".";

                                    if (pastQty >= deal.MaxPurchaseQty)
                                    {
                                        ErrorMessage += "<BR>You have already purchased the maximum allowed.";
                                    }
                                    else
                                    {
                                        int allowedAmount = deal.MaxPurchaseQty - pastQty;
                                        ErrorMessage += "<BR>You may only purchase " + allowedAmount + " more.";
                                    }

                                    ResetAndRedirect();
                                }
                            }
                        }
                    }
                }
            }

            //charge order here


            Order.SubTotal         = subtotal;
            Order.GrandTotal       = subtotal;
            Order.BillingFirstName = firstName;
            Order.BillingLastName  = lastName;
            Order.BillingEmail     = billingEmailAddress;

            /*
             * Order.BillingAddress1 = address1;
             * if (address2 != String.Empty) {
             *  Order.BillingAddress2 = address2;
             * } else {
             *  Order.SetBillingAddress2Null();
             * }
             * Order.BillingCity = city;
             * Order.BillingStateCode = stateCode;
             * Order.BillingZipCode = zipCode;
             */

            Order.BillingPhone = phoneNumber;

            Order.PaymentMethodId = (int)PaymentMethod.PayPal;

            orderAdapter.Update(Order);


            if (Order.CheckoutStartDate < Order.LineItemModifiedDate)
            {
                ErrorMessage = "Your cart has been updated while checking out, please verify your items and continue the checkout process.";

                ResetAndRedirect();
            }

            Order.OrderStatusId = (int)OrderStatus.Processing;
            orderAdapter.Update(Order);


            encoder["METHOD"]        = "DoExpressCheckoutPayment";
            encoder["TOKEN"]         = Session["TOKEN"].ToString();
            encoder["PAYERID"]       = Session["PAYERID"].ToString();
            encoder["AMT"]           = subtotal.ToString("0.00");
            encoder["PAYMENTACTION"] = "Sale";
            encoder["CURRENCYCODE"]  = "USD";

            encoder["INVNUM"] = Order.OrderId.ToString();

            encoder["ITEMAMT"] = Order.LineItems.SubTotal.ToString("0.00");
            foreach (DollarSaverDB.OrderLineItemRow lineItem in Order.LineItems.Rows)
            {
                int itemNumber = lineItem.SeqNo - 1;

                encoder["L_NAME" + itemNumber]   = lineItem.ShortName;
                encoder["L_NUMBER" + itemNumber] = lineItem.CertificateId.ToString();
                encoder["L_QTY" + itemNumber]    = lineItem.Quantity.ToString();
                encoder["L_AMT" + itemNumber]    = lineItem.DiscountValue.ToString("0.00");
            }

            paypalRequest  = encoder.Encode();
            paypalResponse = String.Empty;

            try {
                paypalResponse = caller.Call(paypalRequest);
            } catch {
                ErrorMessage = "An error occurred while processing your order, please try submitting it again.";
                ResetAndRedirect();
            }

            decoder.Decode(paypalResponse);

            strAck = decoder["ACK"];
            if (strAck != null && (strAck == "Success" || strAck == "SuccessWithWarning"))
            {
                /*
                 * string pStrResQue = "TRANSACTIONID=" + decoder["TRANSACTIONID"] + "&" +
                 *  "CURRENCYCODE=" + decoder["CURRENCYCODE"] + "&" +
                 *  "AMT=" + decoder["AMT"];
                 *
                 * Response.Redirect("DoExpressCheckoutPayment.aspx?" + pStrResQue);
                 * */


                string transactionId = decoder["TRANSACTIONID"];
                Order.TransactionId = transactionId;

                Order.OrderDate     = DateTime.Now;
                Order.OrderStatusId = (int)OrderStatus.Complete;
                orderAdapter.Update(Order);

                InfoMessage = "Successfully processed order";

                if (SendReceipt())
                {
                    InfoMessage += "<BR />Receipt sent to " + Order.ShippingEmail;
                }

                if (Order.AddToMailingList)
                {
                    CustomerContactTableAdapter customerContactAdapter = new CustomerContactTableAdapter();
                    customerContactAdapter.Insert(StationId, DateTime.Now, Order.ShippingEmail, Order.BillingFirstName, Order.BillingLastName);
                }

                Response.Redirect("~/Confirmation.aspx");
            }
            else
            {
                /*
                 * string pStrError =
                 *  "ErrorCode=" + decoder["L_ERRORCODE0"] + "&" +
                 *  "Desc=" + decoder["L_SHORTMESSAGE0"] + "&" +
                 *  "Desc2=" + decoder["L_LONGMESSAGE0"];
                 *
                 * Response.Redirect("APIError.aspx?" + pStrError);
                 * */

                ErrorMessage = "An error has occurred while processing your order: " + decoder["L_LONGMESSAGE0"] + " (" + decoder["L_ERRORCODE0"] + ")";
                ResetAndRedirect();
            }
        }
        void placeOrderButton_Click(object sender, EventArgs e)
        {
            // Validate fields

            /*
             * string patternLenient = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
             *
             * string patternStrict = @"^(([^<>()[\]\\.,;:\s@\""]+"
             + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
             + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
             + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
             + @"[a-zA-Z]{2,}))$";
             +
             */

            String creditCardType   = creditCardList.SelectedValue;
            String creditCardNumber = creditCardNumberBox.Text.Trim();

            creditCardNumber = Regex.Replace(creditCardNumber, @"\D", "");
            String verificationNumber = verificationNumberBox.Text.Trim();

            String firstName   = firstNameBox.Text.Trim();
            String lastName    = lastNameBox.Text.Trim();
            String address1    = address1Box.Text.Trim();
            String address2    = address2Box.Text.Trim();
            String city        = cityBox.Text.Trim();
            String stateCode   = stateList.SelectedValue;
            String zipCode     = zipCodeBox.Text.Trim();
            String phoneNumber = phoneNumberBox.Text.Trim();

            if (firstName == String.Empty)
            {
                ErrorMessage = "First Name is required";
                return;
            }
            if (lastName == String.Empty)
            {
                ErrorMessage = "Last Name is required";
                return;
            }
            if (address1 == String.Empty)
            {
                ErrorMessage = "Address is required";
                return;
            }
            if (city == String.Empty)
            {
                ErrorMessage = "City is required";
                return;
            }
            if (stateCode == String.Empty)
            {
                ErrorMessage = "State is required";
                return;
            }
            if (zipCode == String.Empty)
            {
                ErrorMessage = "Zip Code is required";
                return;
            }
            if (phoneNumber == String.Empty)
            {
                ErrorMessage = "Phone Number is required";
                return;
            }
            if (phoneNumber.Length < 10)
            {
                ErrorMessage = "Phone Number must include area code plus 7 digit phone number";
                return;
            }

            if (DsCookie["_c"] == "1")
            {
                DsCookie["_c_ba"] = Cipher.Encrypt2(firstName);
                DsCookie["_c_bb"] = Cipher.Encrypt2(lastName);
                DsCookie["_c_bc"] = Cipher.Encrypt2(address1);
                DsCookie["_c_bd"] = Cipher.Encrypt2(address2);
                DsCookie["_c_be"] = Cipher.Encrypt2(city);
                DsCookie["_c_bf"] = Cipher.Encrypt2(stateCode);
                DsCookie["_c_bg"] = Cipher.Encrypt2(zipCode);
                DsCookie["_c_bh"] = Cipher.Encrypt2(phoneNumber);
            }

            if (creditCardNumber == String.Empty)
            {
                ErrorMessage = "Credit Card Number is required";
                return;
            }
            if (verificationNumber == String.Empty)
            {
                ErrorMessage = "Card Verification Number is required";
                return;
            }

            OrderTableAdapter             orderAdapter         = new OrderTableAdapter();
            CertificateNumberTableAdapter numberAdapter        = new CertificateNumberTableAdapter();
            OrderLineItemTableAdapter     orderLineItemAdapter = new OrderLineItemTableAdapter();


            decimal subtotal = 0.0m;

            foreach (DollarSaverDB.OrderLineItemRow lineItem in Order.LineItems)
            {
                int numberAssigned = Convert.ToInt32(numberAdapter.Assign(lineItem.OrderLineItemId));

                if (numberAssigned != lineItem.Quantity)
                {
                    if (numberAssigned == 0)
                    {
                        ErrorMessage = "We're sorry, " + lineItem.Certificate.AdvertiserName + " is no longer available";
                        orderLineItemAdapter.Delete(lineItem.OrderLineItemId);
                    }
                    else
                    {
                        lineItem.Quantity = numberAssigned;
                        orderLineItemAdapter.Update(lineItem);

                        ErrorMessage = "We're sorry, " + lineItem.Certificate.AdvertiserName + " is no longer available in the quantity you requested. Please review your updated order and click on the checkout button if you would like to purchase the new quantity";
                    }

                    Order.LineItemModifiedDate = DateTime.Now;
                    orderAdapter.Update(Order);

                    ResetOrder();
                    Response.Redirect("~/Cart.aspx");
                }


                subtotal += lineItem.Total;
            }


            switch (creditCardType)
            {
            case "Visa":
                Order.PaymentMethodId = (int)PaymentMethod.Visa;
                break;

            case "MasterCard":
                Order.PaymentMethodId = (int)PaymentMethod.MasterCard;
                break;

            case "Discover":
                Order.PaymentMethodId = (int)PaymentMethod.Discover;
                break;

            case "Amex":
                Order.PaymentMethodId = (int)PaymentMethod.Amex;
                break;

            default:

                break;
            }


            Order.SubTotal         = subtotal;
            Order.GrandTotal       = subtotal;
            Order.BillingFirstName = firstName;
            Order.BillingLastName  = lastName;
            Order.BillingAddress1  = address1;
            if (address2 != String.Empty)
            {
                Order.BillingAddress2 = address2;
            }
            else
            {
                Order.SetBillingAddress2Null();
            }
            Order.BillingCity      = city;
            Order.BillingStateCode = stateCode;
            Order.BillingZipCode   = zipCode;
            Order.BillingPhone     = phoneNumber;


            orderAdapter.Update(Order);


            // Check max purchase qty for Deal of the Week
            if (Station.StationSiteType == SiteType.DealOfTheWeek)
            {
                CertificateTableAdapter            certificateAdapter = new CertificateTableAdapter();
                DollarSaverDB.CertificateDataTable certificateTable   = certificateAdapter.GetCurrentDeal(StationId);

                if (certificateTable.Count == 1)
                {
                    DollarSaverDB.CertificateRow deal = certificateTable[0];
                    if (deal.MaxPurchaseQty > 0)
                    {
                        foreach (DollarSaverDB.OrderLineItemRow lineItem in Order.LineItems)
                        {
                            if (lineItem.CertificateId == deal.CertificateId)
                            {
                                int pastQty = Convert.ToInt32(orderLineItemAdapter.GetQtyByConsumer(firstName, lastName, null,
                                                                                                    address1, city, stateCode, Order.ShippingEmail, deal.CertificateId));

                                if (pastQty + lineItem.Quantity > deal.MaxPurchaseQty)
                                {
                                    ErrorMessage = "Sorry, the maximum purchase quantity per person for the Deal of the Week is " + deal.MaxPurchaseQty + ".";

                                    if (pastQty >= deal.MaxPurchaseQty)
                                    {
                                        ErrorMessage += "<BR>You have already purchased the maximum allowed.";
                                    }
                                    else
                                    {
                                        int allowedAmount = deal.MaxPurchaseQty - pastQty;
                                        ErrorMessage += "<BR>You may only purchase " + allowedAmount + " more.";
                                    }

                                    ResetOrder();
                                    Response.Redirect("~/Cart.aspx");
                                }
                            }
                        }
                    }
                }
            }

            if (Order.CheckoutStartDate < Order.LineItemModifiedDate)
            {
                ResetOrder();

                ErrorMessage = "Your cart has been updated while checking out, please verify you items and continue the checkout process.";

                Response.Redirect("~/Cart.aspx");
            }

            Order.OrderStatusId = (int)OrderStatus.Processing;
            orderAdapter.Update(Order);


            // charge order...
            NVPCallerServices caller  = PayPalAPI.PayPalAPIInitialize(IsDev);
            NVPCodec          encoder = new NVPCodec();

            encoder["VERSION"]        = "50.0";
            encoder["METHOD"]         = "DoDirectPayment";
            encoder["PAYMENTACTION"]  = "Sale";
            encoder["AMT"]            = subtotal.ToString("0.00");
            encoder["CREDITCARDTYPE"] = creditCardType;
            encoder["ACCT"]           = creditCardNumber;
            encoder["EXPDATE"]        = expirationMonthList.SelectedValue + expirationYearList.SelectedValue;
            encoder["CVV2"]           = verificationNumber;
            encoder["FIRSTNAME"]      = firstName;
            encoder["LASTNAME"]       = lastName;
            encoder["STREET"]         = address1;
            encoder["CITY"]           = city;
            encoder["STATE"]          = stateCode;
            encoder["ZIP"]            = zipCode;
            encoder["COUNTRYCODE"]    = "US";
            encoder["CURRENCYCODE"]   = "USD";

            /*
             * encoder["INVNUM"] = Order.OrderId.ToString();
             * encoder["ITEMAMT"] = Order.LineItems.SubTotal.ToString("0.00");
             * foreach (DollarSaverDB.OrderLineItemRow lineItem in Order.LineItems.Rows) {
             *  int itemNumber = lineItem.SeqNo - 1;
             *
             *  encoder["L_NAME" + itemNumber] = lineItem.ShortName;
             *  encoder["L_NUMBER" + itemNumber] = lineItem.CertificateId.ToString();
             *  encoder["L_QTY" + itemNumber] = lineItem.Quantity.ToString();
             *  encoder["L_AMT" + itemNumber] = lineItem.DiscountValue.ToString("0.00");
             * }
             */

            string paypalRequest  = encoder.Encode();
            string paypalResponse = String.Empty;


            try {
                paypalResponse = caller.Call(paypalRequest);
            } catch {
                ResetOrder();
                ErrorMessage = "An error occurred while processing your order, please try submitting it again.";
                return;
            }


            NVPCodec decoder = new NVPCodec();

            decoder.Decode(paypalResponse);

            string strAck = decoder["ACK"];

            if (strAck != null && (strAck == "Success" || strAck == "SuccessWithWarning"))
            {
                string transactionId = decoder["TRANSACTIONID"];
                Order.TransactionId = transactionId;

                Order.OrderDate     = DateTime.Now;
                Order.OrderStatusId = (int)OrderStatus.Complete;
                orderAdapter.Update(Order);

                InfoMessage = "Successfully processed order";

                if (SendReceipt())
                {
                    InfoMessage += "<BR />Receipt sent to " + Order.ShippingEmail;
                }

                if (Order.AddToMailingList)
                {
                    CustomerContactTableAdapter customerContactAdapter = new CustomerContactTableAdapter();
                    customerContactAdapter.Insert(StationId, DateTime.Now, Order.ShippingEmail, Order.BillingFirstName, Order.BillingLastName);
                }

                Response.Redirect("~/Confirmation.aspx", true);
                return;
            }
            else
            {
                ResetOrder();

                ErrorMessage = "Error! " + decoder["L_LONGMESSAGE0"] + " (" + decoder["L_ERRORCODE0"] + ")";

                return;
            }
        }