protected void ValidateFieldNames_Click(object sender, EventArgs e)
    {
        Util util = new Util();
        Hashtable State = (Hashtable)HttpRuntime.Cache[Session.SessionID];
        if (util.CheckSessionTimeout(State, Response, "Default.aspx")) return;

        if (SavedCanvasHtml.Text.Length > 0)
            SavePage();
        XmlUtil x_util = new XmlUtil();
        x_util.ValidateFieldNames(State, Message);
    }
    /* protected void GoToWebAppDesign_Click(object sender, EventArgs e)
    {
        Util util = new Util();
        Hashtable State = (Hashtable)HttpRuntime.Cache[Session.SessionID];
        if (util.CheckSessionTimeout(State, Response, "Default.aspx")) return;

        if (SavedCanvasHtml.Text.Length > 0)
            SavePage();
        if (State["SelectedApp"] != null &&
            !State["SelectedApp"].ToString().Contains("->") &&
            SaveBeforeChangingAppType.Text.Length > 0)
        {
            SaveBeforeChangingAppType.Text = "";
           // util.SetAppType(State, Constants.WEB_APP_TYPE);
        }
        else
        {
            InitCurrentApp("->");
        }
        Response.Redirect("TabDesignWeb.aspx", false);
    }*/
    protected void DisplayMode_Click(object sender, EventArgs e)
    {
        Util util = new Util();
        Hashtable State = (Hashtable)HttpRuntime.Cache[Session.SessionID];
        if (util.CheckSessionTimeout(State, Response, "Default.aspx")) return;

        if (State["SelectedApp"] == null || State["SelectedApp"].ToString().Contains("->"))
            return;

        int index = DisplayModeButton.SelectedToggleStateIndex;

        if (index == 0)//design mode
        {
             State["WebAppMode"] = null;//design mode is the default
            canvas.Attributes["src"] = "canvas.aspx";
            DesignMessage.Text = "";
            CurrentAppContainer.Style.Value = CurrentAppContainerStyle;
            PreviewNote.Text = "";
            ShowAppControls();
        }
        else //preview mode
        {
            //save Page first
            if (SavedCanvasHtml.Text.Length > 0)
                SavePage();

            HideAppControls();
            DisplayModeButton.Style.Value = "";//don't hide the toggle button
            CurrentAppContainer.Style.Value = "display:none";
            State["WebAppMode"] = "preview";

            //validate fields next
            XmlUtil x_util = new XmlUtil();
            Message.Text = "";
            x_util.ValidateFieldNames(State, Message);
            if (Message.Text != "All fields have been validated")
            {
                PreviewNote.Text = "There were errors in generating your app. Return to design mode to fix the errors.";
                DisplayModeValue.Text = "";
                SavedCanvasHtml.Text = "";
                return;
            }
            Message.Text = "";
            WebAppsUtil web_util = new WebAppsUtil();
            AmazonS3 s3 = new AmazonS3();

            State["IsProduction"] = false;
            string file_name =  State["SelectedApp"].ToString().Replace(" ", "_") + Constants.WEB_APP_TEST_SUFFIX +"/index.html";
            string save_file_path =  HttpRuntime.Cache["TempFilesPath"].ToString() + State["Username"].ToString() + "." + file_name.Replace(Constants.WEB_APP_TEST_SUFFIX + "/index.html", ".html");

            if (State["UrlAccountIdentifier"] == null)
            {
                Message.Text = "The Account Identifier has not been set.";
                return;
            }

            string html = web_util.GetWebApp(State,util.GetStagingAppXml(State),1.0D, 1.0D);
            if (html.StartsWith("Error:"))
            {
                Message.Text = html;
                DisplayMode_Click(null, null);
                return;
            }
            File.WriteAllText(save_file_path, html);

            string key =  State["UrlAccountIdentifier"].ToString() + "/" + file_name;
            s3.UploadFileWithKey(State, file_name, save_file_path,key);
            string url = "http://viziapps.s3-website-us-east-1.amazonaws.com/" +  State["UrlAccountIdentifier"].ToString() + "/" +  State["SelectedApp"].ToString().Replace(" ", "_") + Constants.WEB_APP_TEST_SUFFIX;
            canvas.Attributes["src"] = url;

            if (File.Exists(save_file_path))
                File.Delete(save_file_path);

            PreviewNote.Text = "This preview cannot show actions with native device resources. To test those actions, go to design mode and select this app for testing on your device.";
            DisplayModeValue.Text = "";
            SavedCanvasHtml.Text = "";
        }
    }