protected void Page_Init(object sender, EventArgs e)
        {
            SamesAsCheckBox3.Checked = false;
            SamesAsCheckBox32.Checked = false;
            SameAsLabel4CheckBox.Checked = false;
            SameAsBillingLabel4CheckBox.Checked = false;
            SameAsShippingLabel4CheckBox.Checked = false;

               StaticFieldsPanel1.Visible = false;
            StaticFieldsPanel2.Visible = false;
            StaticFieldsPanel3.Visible = false;
            StaticFieldsPanel4.Visible = false;
            StaticFieldsPanel5.Visible = false;
            StaticFieldsPanel6.Visible = false;
            StaticFieldsPanel7.Visible = false;
            StaticFieldsPanel8.Visible = false;
            StaticFieldsPanel9.Visible = false;

            if(Session["AccountCreateType"] == null)
                Session["AccountCreateType"] = "Create";

            bool isCreate = false;

            if (Session["AccountCreateType"] == "Create")
                isCreate = true;

            string message = "";

            try
            {
                if (Session["formpage"] == null)
                {
                    Session["formpage"] = "1";
                }

                bool drawForm = false;
                if (Session["RedrawForm"] == null)
                {
                    drawForm = true;
                }
                else
                {
                    if (bool.Parse(Session["RedrawForm"].ToString()))
                        drawForm = true;
                    else
                        drawForm = false;
                }

                if (!IsPostBack)
                {
                    drawForm = true;
                }
                //ErrorLabel.Text = "got here2";
                DataSet dsAll = doQuery("SELECT * FROM FormStaticPageFields");
                if (drawForm)
                {

                    string formID = Session["formpage"].ToString();

                    if (int.Parse(formID) < 7)
                    {

                        if (!isCreate)
                        {
                            Image theImage = (Image)FindControl("Image" + formID);
                            theImage.Visible = true;
                        }

                        DataSet ds = doQuery("SELECT NavTitle FROM FormPages WHERE ID=" + formID);
                        DataSet dsForm = doQuery("SELECT * FROM FormStaticPageFields WHERE FormPageID=" + formID);

                        DataSet dsCount = doQuery("SELECT DISTINCT FormPageID FROM FormStaticPageFields");

                        if (int.Parse(formID) < 6)
                        {
                            //ErrorLabel.Text = "got here";
                            ProductName.Text = ds.Tables[0].Rows[0]["NavTitle"].ToString();

                            if (formID == "2")
                            {
                                AllMoneyPanel.DefaultButton = "Button1";

                            }
                            else if(formID == "5")
                            {
                                AllMoneyPanel.DefaultButton = "Button5";
                                StaticFieldsPanel2.DefaultButton = "Button5";

                                BillingHidePanel.DefaultButton = "Button5";
                            }
                        }
                        else if(formID == "6")
                        {
                            ProductName.Text = "Review Order Information";
                        }
                        else if (formID == "7")
                        {
                            ProductName.Visible = false;

                            //Get the invoice
                            Explore.NetSuite.DataAccess.NDAL client = new Explore.NetSuite.DataAccess.NDAL();
                            Array theTransactions = (Array)client.GetTransactions(Session["UserID"].ToString(), DateTime.Now.AddMinutes(double.Parse("-10.00")), DateTime.Now);

                            //code recommended by john ogle. refer to bug 1688 in OA bug tracking system.
                            Dictionary<string, IEnumerable<Transaction>> invoiceMap = new Dictionary<string, IEnumerable<Transaction>>();
                            List<Transaction> invoices;

                            foreach (Transaction t in theTransactions)
                            {
                                if (t.Type != "SalesOrd")
                                {
                                    invoices = new List<Transaction>();

                                    if (invoiceMap.ContainsKey(t.CreatedFrom))
                                    {
                                        invoices = (List<Transaction>)invoiceMap[t.CreatedFrom];
                                    }
                                    else
                                    {
                                        invoiceMap.Add(t.CreatedFrom, invoices);
                                    }

                                    invoices.Add(t);
                                }
                            }

                            foreach (Transaction oneTrans in theTransactions)
                            {
                                if (oneTrans.Type == "SalesOrd" && oneTrans.OrderNumber == Session["OrderNumber"].ToString())
                                {
                                    SalesOrderHyperLink.NavigateUrl = oneTrans.PrintoutURL;
                                }
                            }
                        }
                        else if (formID == "8")
                        {
                            ProductName.Visible = false;
                        }

                        if (formID == "6")
                        {
                            Explore.NetSuite.DataAccess.NDAL client = new Explore.NetSuite.DataAccess.NDAL();

                            BuildIt(client, client.GetCustomer(Session["UserID"].ToString()));

                        }

                        Panel staticPanel = (Panel)FindControl("StaticFieldsPanel" + formID);
                        staticPanel.Visible = true;

                        if (dsAll.Tables.Count > 0)
                        {
                            for (int i = 0; i < dsAll.Tables[0].Rows.Count; i++)
                            {
                                Label theLabel = (Label)FindControl(dsAll.Tables[0].Rows[i]["LabelFormName"].ToString());
                                message += dsAll.Tables[0].Rows[i]["LabelFormName"].ToString();
                                theLabel.Text = dsAll.Tables[0].Rows[i]["LabelName"].ToString();
                            }
                        }

                        RadComboBoxItem item;

                        RadComboBox countryDrop = new RadComboBox();
                        RadComboBox stateDrop = new RadComboBox();
                        TextBox stateTextB = new TextBox();

                        for (int i = 0; i < dsCount.Tables[0].Rows.Count; i++)
                        {

                            countryDrop = (RadComboBox)FindControl("CountryLabel" + (i + 1).ToString() + "RadComboBox");
                            stateTextB = (TextBox)FindControl("StateLabel" + (i + 1).ToString() + "TextBox");
                            stateDrop = (RadComboBox)FindControl("StateLabel" + (i + 1).ToString() + "RadComboBox");

                            if (countryDrop != null)
                            {
                                message += "fin got here";
                                DataSet dsCountry = doQuery("SELECT * FROM Countries ORDER BY country_2_code asc");
                                countryDrop.Items.Clear();
                                countryDrop.DataSource = dsCountry;
                                countryDrop.DataTextField = "country_name";
                                countryDrop.DataValueField = "country_id";
                                countryDrop.DataBind();
                                //for (int n = 0; n < dsCountry.Tables["table"].Rows.Count; n++)
                                //{
                                //    item = new RadComboBoxItem(dsCountry.Tables["table"].Rows[n]["country_2_code"].ToString(),
                                //        dsCountry.Tables["table"].Rows[n]["country_id"].ToString());
                                //    countryDrop.Items.Add(item);
                                //}
                                dsCountry = doQuery("SELECT * FROM countries where country_name='" + ConfigurationManager.AppSettings.Get("defaultcountry") + "'");
                                countryDrop.SelectedValue = dsCountry.Tables["table"].Rows[0]["country_id"].ToString();

                                DataSet dsState = doQuery("SELECT * FROM states WHERE country_id=" + countryDrop.SelectedValue + " ORDER BY state_name ASC");

                                if (dsState.Tables["table"].Rows.Count > 0)
                                {
                                    stateDrop.Items.Clear();
                                    for (int n = 0; n < dsState.Tables["table"].Rows.Count; n++)
                                    {
                                        item = new RadComboBoxItem(dsState.Tables["table"].Rows[n]["state_name"].ToString(), dsState.Tables["table"].Rows[n]["state_code"].ToString());
                                        stateDrop.Items.Add(item);
                                    }
                                    stateTextB.Visible = false;
                                    stateDrop.Visible = true;
                                    stateDrop.Items.Insert(0, new RadComboBoxItem("Please Select...", "-1"));

                                }
                                else
                                {
                                    stateTextB.Visible = true;
                                    stateDrop.Visible = false;
                                }
                            }
                        }

                    }
                    else
                    {
                        GoToForm(int.Parse(formID), 1);
                    }

                }

                DataView dvHelp = new DataView(dsAll.Tables[0], "hasHelp='True'", "", DataViewRowState.CurrentRows);

                for (int j = 0; j < dvHelp.Count; j++)
                {
                    Panel helpPanel = (Panel)FindControl(dvHelp[j]["LabelFormName"].ToString() + "HelpPanel");
                    CreateHelp(dvHelp[j]["HelpText"].ToString(), j.ToString(), ref helpPanel);
                }

                SetBoxes();
                AddFields();

                if (drawForm)
                {
                    switch (Session["formpage"].ToString())
                    {
                        //case "1":
                        //    FillAccount();
                        //    break;
                        case "1":
                            RoleDropDown.Items.Clear();
                            DataSet ds = doQuery("SELECT * FROM ROLES ORDER BY ID ASC");
                            RadComboBoxItem item = new RadComboBoxItem();
                            item.Text = "Please Select...";
                            item.Value = "-1";
                            RoleDropDown.Items.Add(item);
                            for (int i = 0; i < ds.Tables["table"].Rows.Count; i++)
                            {
                                item = new RadComboBoxItem(ds.Tables["table"].Rows[i]["NAME"].ToString(),
                                    ds.Tables["table"].Rows[i]["NetSuiteID"].ToString());
                                RoleDropDown.Items.Add(item);
                            }

                            ds = new DataSet();
                            AccountDropDown.Items.Clear();
                            ds = doQuery("SELECT * FROM AccountType");
                            item = new RadComboBoxItem();
                            item.Text = "Please Select...";
                            item.Value = "-1";
                            AccountDropDown.Items.Add(item);
                            AccountDropDown.Width = 240;
                            for (int i = 0; i < ds.Tables["table"].Rows.Count; i++)
                            {
                                item = new RadComboBoxItem(ds.Tables["table"].Rows[i]["AccountType"].ToString(),
                                    ds.Tables["table"].Rows[i]["NetSuiteID"].ToString());
                                AccountDropDown.Items.Add(item);
                            }
                            break;
                        case "2":
                            StaticFieldsPanel2.DefaultButton = "Button1";
                            BillingHidePanel.DefaultButton = "Button1";
                            FillBilling();
                            break;
                        case "3":
                            FillShipping();
                            break;
                        case "4":
                            FillDelivery();
                            break;
                        case "5":
                            StaticFieldsPanel2.DefaultButton = "Button5";
                            BillingHidePanel.DefaultButton = "Button5";
                            GoToForm(5, 4);
                            //SetBoxes();
                            //BuildIt();
                            FillPayment();
                            break;
                        case "6":
                            GoToForm(6, 5);
                            Explore.NetSuite.DataAccess.NDAL client = new Explore.NetSuite.DataAccess.NDAL();

                            BuildIt(client, client.GetCustomer(Session["UserID"].ToString()));
                            FillPayment();
                            SetBoxes();
                            break;
                        default: break;
                    }
                }

            }
            catch (Exception ex)
            {
                ErrorLabel.Text = ex.ToString() + message;
            }

            if (Session["formpage"] == "2")
            {
                if (Session["InShopping"] != null)
                {
                    Button9.Visible = false;
                }
            }
        }
        protected void SubmitOrder(object sender, EventArgs e)
        {
            if (Session["HasOrderBeenClicked"] == null)
            {
                try
                {
                    if (TermsCheckBox.Checked)
                    {

                        HttpContext.Current.Trace.Warn("FormPage_SubmitOrder()", string.Format("{0},{1},{2},{3}",
                              DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                              "info",
                              "SubmitLogin()",
                              "---SubmitOrder Begin------"));
                        Explore.NetSuite.DataAccess.NDAL client = new Explore.NetSuite.DataAccess.NDAL();
                        client.MediatorLoggingLevel = Explore.NetSuite.JSONservice.MediatorLogLevels.Debug;
                        HttpContext.Current.Trace.Warn("FormPage_SubmitOrder()", string.Format("{0},{1},{2},{3}",
                              DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                              "info",
                              "SubmitLogin()",
                              "---SubmitOrder End------"));
                        //if (Session["UserID"] == null)
                        //    CreateUser();

                        HttpContext.Current.Trace.Warn("FormPage_SubmitOrder()", string.Format("{0},{1},{2},{3}",
                              DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                              "info",
                              "SubmitLogin()",
                              "---CreateOrder() Begin------"));
                        CreateOrder();
                        HttpContext.Current.Trace.Warn("FormPage_SubmitOrder()", string.Format("{0},{1},{2},{3}",
                              DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                              "info",
                              "SubmitLogin()",
                              "---CreateOrder() End------"));
                        if (Session["OrderStatus"].ToString() == "A")
                        {

                            OrderLabel.Text = "Your order number is #" + Session["OrderNumber"].ToString();
                        }
                        else
                        {
                            OrderLabel.Text = "Your order was not submited!";
                        }
                        string thecartID = Session["yourcart"].ToString();
                        doQuery("DELETE FROM Cart WHERE CartSessionID='" + thecartID+"'");

                        Session["formpage"] = "6";

                        //Clear cart after submitting to NetSuite
                        Session["yourcart"] = null;
                        Session.Remove("yourcart");

                        Session["HasOrderBeenClicked"] = "true";

                        //Update Top Header
                        UpdateTopHeader();

                        if (Session["PaymentCookie"].ToString() == "CreditCardPanel")
                        {
                            GoToForm(8, 6);
                            Session["PaymentCookie"] = null;
                            Session["CreditCardLabel"] = null;
                            Session["CreditCSVLabel"] = null;
                            Session["CreditExpirationLabel"] = null;
                            Session["CreditCardType"] = null;
                            Session["CreditName"] = null;
                            Session["POExpirationLabel"] = null;

                            Session.Remove("PaymentCookie");
                            Session.Remove("CreditCardLabel");
                            Session.Remove("CreditCSVLabel");
                            Session.Remove("CreditExpirationLabel");
                            Session.Remove("CreditCardType");
                            Session.Remove("CreditName");
                            Session.Remove("POExpirationLabel");
                            Session["formpage"] = "8";
                            Response.Redirect("default.aspx?a=form&id=1");
                        }
                        else
                        {
                            GoToForm(7, 6);
                            Session["PaymentCookie"] = null;
                            Session["CreditCardLabel"] = null;
                            Session["CreditCSVLabel"] = null;
                            Session["CreditExpirationLabel"] = null;
                            Session["CreditCardType"] = null;
                            Session["CreditName"] = null;
                            Session["POExpirationLabel"] = null;

                            Session.Remove("PaymentCookie");
                            Session.Remove("CreditCardLabel");
                            Session.Remove("CreditCSVLabel");
                            Session.Remove("CreditExpirationLabel");
                            Session.Remove("CreditCardType");
                            Session.Remove("CreditName");
                            Session.Remove("POExpirationLabel");
                            Session["formpage"] = "7";

                            //Get the invoice
                            Array theTransactions = (Array)client.GetTransactions(Session["UserID"].ToString(), DateTime.Now.AddMinutes(double.Parse("-10.00")), DateTime.Now);

                            //code recommended by john ogle. refer to bug 1688 in OA bug tracking system.
                            Dictionary<string, IEnumerable<Transaction>> invoiceMap = new Dictionary<string, IEnumerable<Transaction>>();
                            List<Transaction> invoices;

                            foreach (Transaction t in theTransactions)
                            {
                                if (t.Type != "SalesOrd")
                                {
                                    invoices = new List<Transaction>();

                                    if (invoiceMap.ContainsKey(t.CreatedFrom))
                                    {
                                        invoices = (List<Transaction>)invoiceMap[t.CreatedFrom];
                                    }
                                    else
                                    {
                                        invoiceMap.Add(t.CreatedFrom, invoices);
                                    }

                                    invoices.Add(t);
                                }
                            }

                            foreach (Transaction oneTrans in theTransactions)
                            {
                                if (oneTrans.Type == "SalesOrd" && oneTrans.OrderNumber == Session["OrderNumber"].ToString())
                                {
                                    SalesOrderHyperLink.NavigateUrl = oneTrans.PrintoutURL;
                                }
                            }

                            Response.Redirect("default.aspx?a=form&id=1");

                        }

                    }
                    else
                    {
                        Static6Messages.Text = "Must agree to terms and conditions.";
                    }
                }
                catch (CommandErrorException ex)
                {
                    if (ex.ToString().Contains("An error occurred while processing your credit card"))
                    {
                        Static6Messages.Text = "There was an error processing your credit card. Please contact the merchant for further assistance.";
                    }
                    else
                    {
                        Static6Messages.Text = ex.ToString();
                    }
                }
                catch (Exception ex)
                {
                    ErrorLabel.Text += ex.ToString();
                }

            }
            else
            {
                //Clear cart after submitting to NetSuite

                UpdateTopHeader();

                if (Session["PaymentCookie"].ToString() == "CreditCardPanel")
                {
                    GoToForm(8, 6);
                    Session["formpage"] = "8";
                    Response.Redirect("default.aspx?a=form&id=1");
                }
                else
                {
                    GoToForm(7, 6);
                    Session["formpage"] = "7";
                    Response.Redirect("default.aspx?a=form&id=1");
                }
            }
        }
        protected void GoToForm(int formID, int disappearID)
        {
            SamesAsCheckBox32.Checked = false;
            SameAsLabel4CheckBox.Checked = false;
            SameAsBillingLabel4CheckBox.Checked = false;
            SameAsShippingLabel4CheckBox.Checked = false;

            SamesAsCheckBox3.Checked = false;
            SamesAsCheckBox32.Checked = false;
            SameAsLabel4CheckBox.Checked = false;
            SameAsBillingLabel4CheckBox.Checked = false;
            SameAsShippingLabel4CheckBox.Checked = false;

            if (Session["UserID"] != null)
            {
                if (IsUserInternational())
                {
                    formID = 9;
                }
                else
                {
                    Panel staticPanel3 = (Panel)FindControl("StaticFieldsPanel9");
                    staticPanel3.Visible = false;
                }

                    Panel staticPanel = (Panel)FindControl("StaticFieldsPanel" + disappearID.ToString());
                    staticPanel.Visible = false;

                    Panel staticPanel2 = (Panel)FindControl("StaticFieldsPanel" + (formID).ToString());
                    staticPanel2.Visible = true;

                    Image1.Visible = false;
                    Image2.Visible = false;
                    Image3.Visible = false;
                    Image4.Visible = false;
                    Image5.Visible = false;
                    Image6.Visible = false;

                    if (formID < 7)
                    {
                        Image theImage = (Image)FindControl("Image" + formID);
                        theImage.Visible = true;

                    }

                    if (formID == 5)
                    {
                        StaticFieldsPanel2.Visible = true;
                        BillingForPaymentPanel.Visible = true;
                        BillingHidePanel.Visible = false;
                        BillingHidePanel2.Visible = false;

                        PaymentButtonsPanel.Visible = true;

                        AllMoneyPanel.DefaultButton = "Button5";

                    }
                    else if (formID == 1)
                    {
                        RoleDropDown.Items.Clear();
                        DataSet ds3 = doQuery("SELECT * FROM ROLES ORDER BY ID ASC");
                        RadComboBoxItem item = new RadComboBoxItem();
                        item.Text = "Please Select...";
                        item.Value = "-1";
                        RoleDropDown.Items.Add(item);
                        for (int i = 0; i < ds3.Tables["table"].Rows.Count; i++)
                        {
                            item = new RadComboBoxItem(ds3.Tables["table"].Rows[i]["NAME"].ToString(),
                                ds3.Tables["table"].Rows[i]["NetSuiteID"].ToString());
                            RoleDropDown.Items.Add(item);
                        }

                        ds3 = new DataSet();
                        AccountDropDown.Items.Clear();
                        ds3 = doQuery("SELECT * FROM AccountType");
                        item = new RadComboBoxItem();
                        item.Text = "Please Select...";
                        item.Value = "-1";
                        AccountDropDown.Items.Add(item);
                        AccountDropDown.Width = 240;
                        for (int i = 0; i < ds3.Tables["table"].Rows.Count; i++)
                        {
                            item = new RadComboBoxItem(ds3.Tables["table"].Rows[i]["AccountType"].ToString(),
                                ds3.Tables["table"].Rows[i]["NetSuiteID"].ToString());
                            AccountDropDown.Items.Add(item);
                        }
                    }
                    else if (formID == 7)
                    {
                        ProductName.Visible = false;

                        HttpContext.Current.Trace.Warn("FormPage_DrawForm()", string.Format("{0},{1},{2}",
                              DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                              "info",
                              "---Draw Review get transactions for invoice Begin------"));
                        //Get the invoice
                        Explore.NetSuite.DataAccess.NDAL client = new Explore.NetSuite.DataAccess.NDAL();
                        Array theTransactions = (Array)client.GetTransactions(Session["UserID"].ToString(), DateTime.Now.AddMinutes(double.Parse("-10.00")), DateTime.Now);

                        //code recommended by john ogle. refer to bug 1688 in OA bug tracking system.
                        Dictionary<string, IEnumerable<Transaction>> invoiceMap = new Dictionary<string, IEnumerable<Transaction>>();
                        List<Transaction> invoices;

                        foreach (Transaction t in theTransactions)
                        {
                            if (t.Type != "SalesOrd")
                            {
                                invoices = new List<Transaction>();

                                if (invoiceMap.ContainsKey(t.CreatedFrom))
                                {
                                    invoices = (List<Transaction>)invoiceMap[t.CreatedFrom];
                                }
                                else
                                {
                                    invoiceMap.Add(t.CreatedFrom, invoices);
                                }

                                invoices.Add(t);
                            }
                        }

                        foreach (Transaction oneTrans in theTransactions)
                        {
                            if (oneTrans.Type == "SalesOrd" && oneTrans.OrderNumber == Session["OrderNumber"].ToString())
                            {
                                SalesOrderHyperLink.NavigateUrl = oneTrans.PrintoutURL;
                            }
                        }

                        HttpContext.Current.Trace.Warn("FormPage_DrawForm()", string.Format("{0},{1},{2}",
                              DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                              "info",
                              "---Draw Review get transactions for invoice End------"));
                    }
                    else if (formID == 8)
                    {
                        ProductName.Visible = false;
                    }
                    else if (formID == 2)
                    {
                        StaticFieldsPanel2.Visible = true;
                        BillingForPaymentPanel.Visible = true;
                        BillingHidePanel.Visible = true;
                        BillingHidePanel2.Visible = true;

                        PaymentButtonsPanel.Visible = false;

                        AllMoneyPanel.DefaultButton = "Button1";

                    }
                    else
                    {
                        StaticFieldsPanel2.Visible = false;
                        BillingForPaymentPanel.Visible = false;
                        BillingHidePanel.Visible = true;
                        BillingHidePanel2.Visible = true;

                        PaymentButtonsPanel.Visible = false;
                    }

                    DataSet ds = doQuery("SELECT NavTitle FROM FormPages WHERE ID=" + formID.ToString());

                    Session["formpage"] = formID.ToString();

                    if (ds.Tables.Count > 0)
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            ProductName.Text = ds.Tables[0].Rows[0]["NavTitle"].ToString();

                        }
                        else
                        {
                            if (formID == 6)
                                ProductName.Text = "Review Order Information";
                            else if (formID == 7)
                                ProductName.Visible = false;
                            else if (formID == 8)
                            {
                                ProductName.Visible = false;
                            }
                        }
                    else
                    {
                        if (formID == 6)
                            ProductName.Text = "Review Order Information";
                        else if (formID == 7)
                            ProductName.Visible = false;
                        else if (formID == 8)
                        {
                            ProductName.Visible = false;
                        }
                    }

            }
            else
            {
                Response.Redirect("~/UserLogin.aspx");
            }
        }