Class used to perform very basic validation against any XML file
        /// <summary> Constructor for a new instance of the New_TEI_MySobekViewer class </summary>
        /// <param name="RequestSpecificValues"> All the necessary, non-global data specific to the current request </param>
        public New_TEI_MySobekViewer(RequestCache RequestSpecificValues)
            : base(RequestSpecificValues)
        {
            RequestSpecificValues.Tracer.Add_Trace("New_TEI_MySobekViewer.Constructor", String.Empty);

            // If the RequestSpecificValues.Current_User cannot submit items, go back
            if (!RequestSpecificValues.Current_User.Can_Submit)
            {
                RequestSpecificValues.Current_Mode.My_Sobek_Type = My_Sobek_Type_Enum.Home;
                UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                return;
            }

            // Ensure the TEI plug-in is enabled
            if ((UI_ApplicationCache_Gateway.Configuration.Extensions == null) ||
                (UI_ApplicationCache_Gateway.Configuration.Extensions.Get_Extension("TEI") == null) ||
                (!UI_ApplicationCache_Gateway.Configuration.Extensions.Get_Extension("TEI").Enabled))
            {
                RequestSpecificValues.Current_Mode.My_Sobek_Type = My_Sobek_Type_Enum.Home;
                UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                return;
            }

            // Ensure this user is enabled to add TEI
            string user_tei_enabled = RequestSpecificValues.Current_User.Get_Setting("TEI.Enabled", "false");
            if (String.Compare(user_tei_enabled, "true", StringComparison.OrdinalIgnoreCase) != 0)
            {
                RequestSpecificValues.Current_Mode.My_Sobek_Type = My_Sobek_Type_Enum.Home;
                UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                return;
            }

            // Determine the in process directory for this
            if (RequestSpecificValues.Current_User.ShibbID.Trim().Length > 0)
                userInProcessDirectory = Path.Combine(UI_ApplicationCache_Gateway.Settings.Servers.In_Process_Submission_Location, RequestSpecificValues.Current_User.ShibbID + "\\tei");
            else
                userInProcessDirectory = Path.Combine(UI_ApplicationCache_Gateway.Settings.Servers.In_Process_Submission_Location, RequestSpecificValues.Current_User.UserName.Replace(".", "").Replace("@", "") + "\\tei");

            // Load the CompleteTemplate
            completeTemplate = Template_MemoryMgmt_Utility.Retrieve_Template("tei", RequestSpecificValues.Tracer);
            if (completeTemplate != null)
            {
                RequestSpecificValues.Tracer.Add_Trace("New_TEI_MySobekViewer.Constructor", "Found template in cache");
            }
            else
            {
                RequestSpecificValues.Tracer.Add_Trace("New_TEI_MySobekViewer.Constructor", "Reading template");

                string user_template = Path.Combine(UI_ApplicationCache_Gateway.Settings.Servers.Application_Server_Network, "plugins\\tei\\templates\\user\\template.xml");
                if (!File.Exists(user_template))
                    user_template = Path.Combine(UI_ApplicationCache_Gateway.Settings.Servers.Application_Server_Network, "plugins\\tei\\templates\\default\\template.xml");

                // Read this CompleteTemplate
                Template_XML_Reader reader = new Template_XML_Reader();
                completeTemplate = new CompleteTemplate();
                reader.Read_XML(user_template, completeTemplate, true);

                // Save this into the cache
                Template_MemoryMgmt_Utility.Store_Template("tei", completeTemplate, RequestSpecificValues.Tracer);
            }

            // Determine the number of total CompleteTemplate pages
            totalTemplatePages = completeTemplate.InputPages_Count + 3;
            if (completeTemplate.Permissions_Agreement.Length > 0)
                totalTemplatePages++;
            if (completeTemplate.Upload_Types != CompleteTemplate.Template_Upload_Types.None)
                totalTemplatePages++;

            // Determine the title for this CompleteTemplate, or use a default
            toolTitle = completeTemplate.Title;
            if (toolTitle.Length == 0)
                toolTitle = "TEI Submission Tool";

            // Determine the current phase
            currentProcessStep = 1;
            if ((RequestSpecificValues.Current_Mode.My_Sobek_SubMode.Length > 0) && (Char.IsNumber(RequestSpecificValues.Current_Mode.My_Sobek_SubMode[0])))
            {
                Int32.TryParse(RequestSpecificValues.Current_Mode.My_Sobek_SubMode.Substring(0), out currentProcessStep);
            }

            // Load some information from the session
            if ( HttpContext.Current.Session["New_TEI_mySobekViewer.Mapping_File"] != null )
                mapping_file = HttpContext.Current.Session["New_TEI_mySobekViewer.Mapping_File"] as string;
            if ( HttpContext.Current.Session["New_TEI_mySobekViewer.XSLT_File"] != null )
                xslt_file = HttpContext.Current.Session["New_TEI_mySobekViewer.XSLT_File"] as string;
            if ( HttpContext.Current.Session["New_TEI_mySobekViewer.CSS_File"] != null )
                css_file = HttpContext.Current.Session["New_TEI_mySobekViewer.CSS_File"] as string;

            // If this is process step 1 and there is no permissions statement in the CompleteTemplate,
            // just go to step 2
            if ((currentProcessStep == 1) && (completeTemplate.Permissions_Agreement.Length == 0))
            {
                // Delete any pre-existing agreement from an earlier aborted submission process
                if (File.Exists(userInProcessDirectory + "\\agreement.txt"))
                    File.Delete(userInProcessDirectory + "\\agreement.txt");

                // Skip the permissions step
                currentProcessStep = 2;
            }

            // If there is a boundary infraction here, go back to step 2
            if (currentProcessStep < 0)
                currentProcessStep = 2;
            if ((currentProcessStep > completeTemplate.InputPages.Count + 4) && (currentProcessStep != 8) && (currentProcessStep != 9))
                currentProcessStep = 2;

            // Look for the item in the session, then directory, then just create a new one
            if (HttpContext.Current.Session["TEI_Item"] == null)
            {
                // Clear any old files (older than 24 hours) that are in the directory
                if (!Directory.Exists(userInProcessDirectory))
                    Directory.CreateDirectory(userInProcessDirectory);
                else
                {
                    // Anything older than a day should be deleted
                    string[] files = Directory.GetFiles(userInProcessDirectory);
                    foreach (string thisFile in files)
                    {
                        DateTime modifiedDate = ((new FileInfo(thisFile)).LastWriteTime);
                        if (DateTime.Now.Subtract(modifiedDate).TotalHours > (24 * 7))
                        {
                            try
                            {
                                File.Delete(thisFile);
                            }
                            catch (Exception)
                            {
                                // Unable to delete existing file in the RequestSpecificValues.Current_User's folder.
                                // This is an error, but how to report it?
                            }
                        }
                    }
                }

                // First, look for an existing METS file
                string[] existing_mets = Directory.GetFiles(userInProcessDirectory, "*.mets*");
                if (existing_mets.Length > 0)
                {
                    RequestSpecificValues.Tracer.Add_Trace("New_TEI_MySobekViewer.Constructor", "Reading existing METS file<br />(" + existing_mets[0] + ")");
                    item = SobekCM_Item.Read_METS(existing_mets[0]);

                    // Set the visibility information from the CompleteTemplate
                    item.Behaviors.IP_Restriction_Membership = completeTemplate.Default_Visibility;
                }

                // If there is still no item, just create a new one
                if (item == null)
                {
                    // Build a new empty METS file
                    new_item(RequestSpecificValues.Tracer);
                }

                // Save this to the session state now
                HttpContext.Current.Session["Item"] = item;
            }
            else
            {
                RequestSpecificValues.Tracer.Add_Trace("New_TEI_MySobekViewer.Constructor", "Item found in session cache");
                item = (SobekCM_Item)HttpContext.Current.Session["Item"];
            }

            // Find the TEI file
            if (Directory.Exists(userInProcessDirectory))
            {
                string[] tei_files = Directory.GetFiles(userInProcessDirectory, "*.xml");
                if (tei_files.Length > 1)
                {
                    // Two XML files, so delete all but the latest
                    string latest_tei_file = String.Empty;
                    DateTime latest_timestamp = DateTime.MinValue;

                    // Find the latest TEI file
                    foreach (string thisTeiFile in tei_files)
                    {
                        // If this is marc.xml, skip it
                        if ((Path.GetFileName(thisTeiFile).ToLower().IndexOf("marc.xml") >= 0) || (Path.GetFileName(thisTeiFile).ToLower().IndexOf("mets.xml") >= 0))
                            continue;

                        DateTime file_timestamp = File.GetLastWriteTime(thisTeiFile);

                        if (DateTime.Compare(latest_timestamp, file_timestamp) < 0)
                        {
                            latest_tei_file = thisTeiFile;
                            latest_timestamp = file_timestamp;
                        }
                    }

                    // If a latest file as found, delete the others
                    if (!String.IsNullOrEmpty(latest_tei_file))
                    {
                        foreach (string thisTeiFile in tei_files)
                        {
                            // If this is marc.xml, skip it
                            if ((Path.GetFileName(thisTeiFile).ToLower().IndexOf("marc.xml") >= 0) || (Path.GetFileName(thisTeiFile).ToLower().IndexOf("mets.xml") >= 0))
                                continue;

                            // Was this the latest file?
                            if (String.Compare(thisTeiFile, latest_tei_file, StringComparison.OrdinalIgnoreCase) == 0)
                                continue;

                            try
                            {
                                File.Delete(thisTeiFile);
                            }
                            catch { }

                        }
                    }

                    tei_file = latest_tei_file;
                }
                else if (tei_files.Length == 1)
                {
                    tei_file = Path.GetFileName(tei_files[0]);
                }
            }

            #region Special code to handle any uploaded files

            // Any post-processing to do?
            if ((currentProcessStep == 8) && (Directory.Exists(userInProcessDirectory)))
            {
                string[] processFiles = Directory.GetFiles(userInProcessDirectory);
                foreach (string thisFile in processFiles)
                {
                    FileInfo thisFileInfo = new FileInfo(thisFile);
                    if ((thisFileInfo.Extension.ToUpper() == ".TIF") || (thisFileInfo.Extension.ToUpper() == ".TIFF"))
                    {
                        // Is there a JPEG and/or thumbnail?
                        string jpeg = userInProcessDirectory + "\\" + thisFileInfo.Name.Replace(thisFileInfo.Extension, "") + ".jpg";
                        string jpeg_thumbnail = userInProcessDirectory + "\\" + thisFileInfo.Name.Replace(thisFileInfo.Extension, "") + "thm.jpg";

                        // Is one missing?
                        if ((!File.Exists(jpeg)) || (!File.Exists(jpeg_thumbnail)))
                        {
                            using (Image tiffImg = Image.FromFile(thisFile))
                            {
                                try
                                {
                                    var mainImg = ScaleImage(tiffImg, UI_ApplicationCache_Gateway.Settings.Resources.JPEG_Width, UI_ApplicationCache_Gateway.Settings.Resources.JPEG_Height);
                                    mainImg.Save(jpeg, ImageFormat.Jpeg);
                                    mainImg.Dispose();
                                    var thumbnailImg = ScaleImage(tiffImg, 150, 400);
                                    thumbnailImg.Save(jpeg_thumbnail, ImageFormat.Jpeg);
                                    thumbnailImg.Dispose();
                                }
                                catch
                                {

                                }
                                finally
                                {
                                    if (tiffImg != null)
                                        tiffImg.Dispose();
                                }
                            }

                        }
                    }
                }
            }

            #endregion

            #region Handle any other post back requests

            // If this is post-back, handle it
            if (RequestSpecificValues.Current_Mode.isPostBack)
            {
                // If this is a request from stage 8, save the new labels and url first
                if (currentProcessStep == 8)
                {
                    string[] getKeys = HttpContext.Current.Request.Form.AllKeys;
                    string file_name_from_keys = String.Empty;
                    string label_from_keys = String.Empty;
                    foreach (string thisKey in getKeys)
                    {
                        if (thisKey.IndexOf("upload_file") == 0)
                        {
                            file_name_from_keys = HttpContext.Current.Request.Form[thisKey];
                        }
                        if (thisKey.IndexOf("upload_label") == 0)
                        {
                            label_from_keys = HttpContext.Current.Request.Form[thisKey];
                        }
                        if ((file_name_from_keys.Length > 0) && (label_from_keys.Length > 0))
                        {
                            HttpContext.Current.Session["file_" + file_name_from_keys.Trim()] = label_from_keys.Trim();
                            file_name_from_keys = String.Empty;
                            label_from_keys = String.Empty;
                        }

                        if (thisKey == "url_input")
                        {
                            item.Bib_Info.Location.Other_URL = HttpContext.Current.Request.Form[thisKey];
                        }
                    }
                }

                // Was this where the mapping, xslt, and css is set?
                if (currentProcessStep == 3)
                {
                    string[] getKeys = HttpContext.Current.Request.Form.AllKeys;
                    string file_name_from_keys = String.Empty;
                    string label_from_keys = String.Empty;
                    foreach (string thisKey in getKeys)
                    {
                        if (thisKey.IndexOf("mapping_select") == 0)
                        {
                            mapping_file = HttpContext.Current.Request.Form[thisKey];
                            HttpContext.Current.Session["New_TEI_mySobekViewer.Mapping_File"] = mapping_file;
                        }
                        if (thisKey.IndexOf("xslt_select") == 0)
                        {
                            xslt_file = HttpContext.Current.Request.Form[thisKey];
                            HttpContext.Current.Session["New_TEI_mySobekViewer.XSLT_File"] = xslt_file;
                        }
                        if (thisKey.IndexOf("css_select") == 0)
                        {
                            css_file = HttpContext.Current.Request.Form[thisKey];
                            HttpContext.Current.Session["New_TEI_mySobekViewer.CSS_File"] = css_file;
                        }
                    }
                }

                string action = HttpContext.Current.Request.Form["action"];
                if (action == "cancel")
                {
                    // Clear all files in the RequestSpecificValues.Current_User process folder
                    try
                    {
                        string[] all_files = Directory.GetFiles(userInProcessDirectory);
                        foreach (string thisFile in all_files)
                            File.Delete(thisFile);
                        Directory.Delete(userInProcessDirectory);
                    }
                    catch (Exception ee)
                    {
                        tei_file = ee.Message;
                        // Unable to delete existing file in the RequestSpecificValues.Current_User's folder.
                        // This is an error, but how to report it?
                    }

                    // Clear all the information in memory
                    HttpContext.Current.Session["agreement_date"] = null;
                    HttpContext.Current.Session["item"] = null;

                    HttpContext.Current.Session["New_TEI_mySobekViewer.Mapping_File"] = null;
                    HttpContext.Current.Session["New_TEI_mySobekViewer.XSLT_File"] = null;
                    HttpContext.Current.Session["New_TEI_mySobekViewer.CSS_File"] = null;

                    // Clear any temporarily assigned current project and CompleteTemplate
                    RequestSpecificValues.Current_User.Current_Default_Metadata = null;
                    RequestSpecificValues.Current_User.Current_Template = null;

                    // Forward back to my Sobek home
                    RequestSpecificValues.Current_Mode.My_Sobek_Type = My_Sobek_Type_Enum.Home;
                    UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                }

                if (action == "delete")
                {
                    string filename = HttpContext.Current.Request.Form["phase"];
                    try
                    {
                        if (File.Exists(userInProcessDirectory + "\\" + filename))
                            File.Delete(userInProcessDirectory + "\\" + filename);

                        // Forward
                        UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                        return;
                    }
                    catch (Exception)
                    {
                        // Unable to delete existing file in the RequestSpecificValues.Current_User's folder.
                        // This is an error, but how to report it?
                    }
                }

                if (action == "clear")
                {
                    // If there is an old METS file, delete it
                    if (File.Exists(userInProcessDirectory + "\\TEMP000001_00001.mets"))
                        File.Delete(userInProcessDirectory + "\\TEMP000001_00001.mets");

                    // Create the new METS file and add to the session
                    new_item(null);
                    HttpContext.Current.Session["Item"] = item;

                    // Forward back to the same URL
                    RequestSpecificValues.Current_Mode.My_Sobek_SubMode = "2";
                    UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                    return;
                }

                if (action == "next_phase")
                {

                    string next_phase = HttpContext.Current.Request.Form["phase"];

                    // If this goes from step 1 to step 2, write the permissions first
                    if ((currentProcessStep == 1) && (next_phase == "2") && (completeTemplate.Permissions_Agreement.Length > 0))
                    {
                        // Store this agreement in the session state
                        DateTime agreement_date = DateTime.Now;
                        HttpContext.Current.Session["agreement_date"] = agreement_date;

                        // Also, save this as a text file
                        string agreement_file = userInProcessDirectory + "\\agreement.txt";
                        StreamWriter writer = new StreamWriter(agreement_file, false);
                        writer.WriteLine("Permissions Agreement");
                        writer.WriteLine();
                        if (!String.IsNullOrWhiteSpace(RequestSpecificValues.Current_User.ShibbID))
                            writer.WriteLine("User: "******" ( " + RequestSpecificValues.Current_User.ShibbID + " )");
                        else
                            writer.WriteLine("User: "******"Date: " + agreement_date.ToString());
                        writer.WriteLine("IP Address: " + HttpContext.Current.Request.UserHostAddress);
                        writer.WriteLine();
                        writer.WriteLine(completeTemplate.Permissions_Agreement);
                        writer.Flush();
                        writer.Close();

                        if (HttpContext.Current.Request.Form["setNewDefaultCheckBox"] != null)
                        {
                            // Set the default metadata preference first
                            string prefProject = HttpContext.Current.Request.Form["prefProject"];
                            if (!String.IsNullOrEmpty(prefProject))
                                RequestSpecificValues.Current_User.Set_Current_Default_Metadata(prefProject.Trim());

                            // Set the template code next
                            string prefTemplate = HttpContext.Current.Request.Form["prefTemplate"];
                            if (!String.IsNullOrEmpty(prefTemplate))
                                RequestSpecificValues.Current_User.Set_Default_Template(prefTemplate.Trim());

                            // Save the user preferences
                            SobekCM_Database.Save_User(RequestSpecificValues.Current_User, String.Empty, RequestSpecificValues.Current_User.Authentication_Type, RequestSpecificValues.Tracer);
                        }
                    }

                    // If this goes from step 2 (upload TEI) to step 3, validate the TEI XML file
                    if ((currentProcessStep == 2) && (next_phase == "3"))
                    {
                        // Should be a TEI file to continue
                        if (!String.IsNullOrEmpty(tei_file))
                        {
                            XmlValidator validator = new XmlValidator();
                            string tei_filepath = Path.Combine(userInProcessDirectory, tei_file);
                            bool isValid = validator.IsValid(tei_filepath);
                            if (!isValid)
                            {
                                string validatorErrors = validator.Errors.Replace("\n", "<br />\n");
                                error_message = "Uploaded TEI file is not a valid XML source file.<br /><br />\n" + validatorErrors + "<br />";
                                next_phase = "2";
                            }
                        }
                    }

                    // If this is going from a step that includes the metadata entry portion, save this to the item
                    if ((currentProcessStep > 4) && (currentProcessStep < 8))
                    {
                        // Save to the item
                        completeTemplate.Save_To_Bib(item, RequestSpecificValues.Current_User, currentProcessStep - 4);
                        item.Save_METS();
                        HttpContext.Current.Session["Item"] = item;

                        // Save the pertinent data to the METS file package
                        item.METS_Header.Create_Date = DateTime.Now;
                        if ((HttpContext.Current.Session["agreement_date"] != null) && (HttpContext.Current.Session["agreement_date"].ToString().Length > 0))
                        {
                            DateTime asDateTime;
                            if (DateTime.TryParse(HttpContext.Current.Session["agreement_date"].ToString(), out asDateTime))
                                item.METS_Header.Create_Date = asDateTime;
                        }
                        HttpContext.Current.Session["Item"] = item;

                        // Save this item, just in case it gets lost somehow
                        item.Source_Directory = userInProcessDirectory;
                        string acquisition_append = "Submitted by " + RequestSpecificValues.Current_User.Full_Name + ".";
                        if (item.Bib_Info.Notes_Count > 0)
                        {
                            foreach (Note_Info thisNote in item.Bib_Info.Notes.Where(ThisNote => ThisNote.Note_Type == Note_Type_Enum.Acquisition))
                            {
                                if (thisNote.Note.IndexOf(acquisition_append) < 0)
                                    thisNote.Note = thisNote.Note.Trim() + "  " + acquisition_append;
                                break;
                            }
                        }

                        // Also, check all the authors to add the current users attribution information
                        if (RequestSpecificValues.Current_User.Organization.Length > 0)
                        {
                            if ((item.Bib_Info.Main_Entity_Name.Full_Name.IndexOf(RequestSpecificValues.Current_User.Family_Name) >= 0) && ((item.Bib_Info.Main_Entity_Name.Full_Name.IndexOf(RequestSpecificValues.Current_User.Given_Name) >= 0) || ((RequestSpecificValues.Current_User.Nickname.Length > 2) && (item.Bib_Info.Main_Entity_Name.Full_Name.IndexOf(RequestSpecificValues.Current_User.Nickname) > 0))))
                            {
                                item.Bib_Info.Main_Entity_Name.Affiliation = RequestSpecificValues.Current_User.Organization;
                                if (RequestSpecificValues.Current_User.College.Length > 0)
                                    item.Bib_Info.Main_Entity_Name.Affiliation = item.Bib_Info.Main_Entity_Name.Affiliation + " -- " + RequestSpecificValues.Current_User.College;
                                if (RequestSpecificValues.Current_User.Department.Length > 0)
                                    item.Bib_Info.Main_Entity_Name.Affiliation = item.Bib_Info.Main_Entity_Name.Affiliation + " -- " + RequestSpecificValues.Current_User.Department;
                                if (RequestSpecificValues.Current_User.Unit.Length > 0)
                                    item.Bib_Info.Main_Entity_Name.Affiliation = item.Bib_Info.Main_Entity_Name.Affiliation + " -- " + RequestSpecificValues.Current_User.Unit;
                            }
                            if (item.Bib_Info.Names_Count > 0)
                            {
                                foreach (Name_Info thisName in item.Bib_Info.Names)
                                {
                                    if ((thisName.Full_Name.IndexOf(RequestSpecificValues.Current_User.Family_Name) >= 0) && ((thisName.Full_Name.IndexOf(RequestSpecificValues.Current_User.Given_Name) >= 0) || ((RequestSpecificValues.Current_User.Nickname.Length > 2) && (thisName.Full_Name.IndexOf(RequestSpecificValues.Current_User.Nickname) > 0))))
                                    {
                                        thisName.Affiliation = RequestSpecificValues.Current_User.Organization;
                                        if (RequestSpecificValues.Current_User.College.Length > 0)
                                            thisName.Affiliation = thisName.Affiliation + " -- " + RequestSpecificValues.Current_User.College;
                                        if (RequestSpecificValues.Current_User.Department.Length > 0)
                                            thisName.Affiliation = thisName.Affiliation + " -- " + RequestSpecificValues.Current_User.Department;
                                        if (RequestSpecificValues.Current_User.Unit.Length > 0)
                                            thisName.Affiliation = thisName.Affiliation + " -- " + RequestSpecificValues.Current_User.Unit;

                                    }
                                }
                            }
                        }
                        item.Save_METS();
                        HttpContext.Current.Session["Item"] = item;
                    }

                    // For now, just forward to the next phase
                    if (currentProcessStep.ToString() != next_phase)
                    {
                        RequestSpecificValues.Current_Mode.My_Sobek_SubMode = next_phase;
                        UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                        return;
                    }
                }
            }

            #endregion

            #region Perform some validation to determine if the RequestSpecificValues.Current_User should be at this step

            // If this is past the agreement phase, check that an agreement exists
            if (currentProcessStep > 1)
            {
                // Validate that an agreement.txt file exists, if the CompleteTemplate has permissions
                if ((completeTemplate.Permissions_Agreement.Length > 0) && (!File.Exists(userInProcessDirectory + "\\agreement.txt")))
                {
                    RequestSpecificValues.Current_Mode.My_Sobek_SubMode = "1";
                    UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                    return;
                }

                // Get the validation errors
                validationErrors = new List<string>();
                SobekCM_Item_Validator.Validate_SobekCM_Item(item, validationErrors);
            }

            // If this is past the step to upload a TEI file, ensure a TEI file exists
            if ((currentProcessStep > 2) && (String.IsNullOrEmpty(tei_file)))
            {
                RequestSpecificValues.Current_Mode.My_Sobek_SubMode = "2";
                UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                return;
            }

            // If this is past the step to upload a TEI file, ensure a TEI file exists
            if ((currentProcessStep > 3) && ((String.IsNullOrEmpty(mapping_file)) || (String.IsNullOrEmpty(xslt_file))))
            {
                RequestSpecificValues.Current_Mode.My_Sobek_SubMode = "3";
                UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                return;
            }

            // If this is to put up items or complete the item, validate the METS
            if (currentProcessStep >= 8)
            {
                // Validate that a METS file exists
                if (Directory.GetFiles(userInProcessDirectory, "*.mets*").Length == 0)
                {
                    RequestSpecificValues.Current_Mode.My_Sobek_SubMode = "2";
                    UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                    return;
                }

                // Get the validation errors
                if (validationErrors.Count == 0)
                    item.Save_METS();
                else
                {
                    item.Web.Show_Validation_Errors = true;
                    RequestSpecificValues.Current_Mode.My_Sobek_SubMode = "2";
                    UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                    return;
                }
            }

            // If this is for step 8, ensure that this even takes this information, or go to step 9
            if ((currentProcessStep == 8) && (completeTemplate.Upload_Types == CompleteTemplate.Template_Upload_Types.None))
            {
                RequestSpecificValues.Current_Mode.My_Sobek_SubMode = "9";
                UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                return;
            }

            // If this is going into the last process step, check that any mandatory info (file, url, .. )
            // from the last step is present
            if (currentProcessStep == 9)
            {
                // Complete the item submission
                complete_item_submission(item, RequestSpecificValues.Tracer);
            }

            #endregion
        }
        /// <summary> Constructor for a new instance of the Edit_TEI_Item_MySobekViewer class </summary>
        /// <param name="RequestSpecificValues"> All the necessary, non-global data specific to the current request </param>
        public Edit_TEI_Item_MySobekViewer(RequestCache RequestSpecificValues)
            : base(RequestSpecificValues)
        {
            RequestSpecificValues.Tracer.Add_Trace("Edit_TEI_Item_MySobekViewer.Constructor", String.Empty);

            // If the RequestSpecificValues.Current_User cannot submit items, go back
            if (!RequestSpecificValues.Current_User.Can_Submit)
            {
                RequestSpecificValues.Current_Mode.My_Sobek_Type = My_Sobek_Type_Enum.Home;
                UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                return;
            }

            // Ensure the TEI plug-in is enabled
            if ((UI_ApplicationCache_Gateway.Configuration.Extensions == null) ||
                (UI_ApplicationCache_Gateway.Configuration.Extensions.Get_Extension("TEI") == null) ||
                (!UI_ApplicationCache_Gateway.Configuration.Extensions.Get_Extension("TEI").Enabled))
            {
                RequestSpecificValues.Current_Mode.My_Sobek_Type = My_Sobek_Type_Enum.Home;
                UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                return;
            }

            // Ensure BibID and VID provided
            RequestSpecificValues.Tracer.Add_Trace("Edit_Item_Metadata_MySobekViewer.Constructor", "Validate provided bibid / vid");
            if ((String.IsNullOrEmpty(RequestSpecificValues.Current_Mode.BibID)) || (String.IsNullOrEmpty(RequestSpecificValues.Current_Mode.VID)))
            {
                RequestSpecificValues.Tracer.Add_Trace("Edit_Item_Metadata_MySobekViewer.Constructor", "BibID or VID was not provided!");
                RequestSpecificValues.Current_Mode.Mode = Display_Mode_Enum.Error;
                RequestSpecificValues.Current_Mode.Error_Message = "Invalid Request : BibID/VID missing in item metadata edit request";
                return;
            }

            bibid = RequestSpecificValues.Current_Mode.BibID;
            vid = RequestSpecificValues.Current_Mode.VID;

            // Ensure the item is valid
            RequestSpecificValues.Tracer.Add_Trace("Edit_Item_Metadata_MySobekViewer.Constructor", "Validate bibid/vid exists");
            if (!UI_ApplicationCache_Gateway.Items.Contains_BibID_VID(bibid, vid))
            {
                RequestSpecificValues.Tracer.Add_Trace("Edit_Item_Metadata_MySobekViewer.Constructor", "BibID/VID indicated is not valid", Custom_Trace_Type_Enum.Error);
                RequestSpecificValues.Current_Mode.Mode = Display_Mode_Enum.Error;
                RequestSpecificValues.Current_Mode.Error_Message = "Invalid Request : BibID/VID indicated is not valid";
                return;
            }

            // Determine the in process directory for this
            if (RequestSpecificValues.Current_User.ShibbID.Trim().Length > 0)
                userInProcessDirectory = Path.Combine(UI_ApplicationCache_Gateway.Settings.Servers.In_Process_Submission_Location, RequestSpecificValues.Current_User.ShibbID, "teiedit", bibid + "_" + vid);
            else
                userInProcessDirectory = Path.Combine(UI_ApplicationCache_Gateway.Settings.Servers.In_Process_Submission_Location, RequestSpecificValues.Current_User.UserName.Replace(".", "").Replace("@", ""), "teiedit", bibid + "_" + vid);

            // Clear any old files (older than 24 hours) that are in the directory
            if (!Directory.Exists(userInProcessDirectory))
                Directory.CreateDirectory(userInProcessDirectory);
            else
            {
                // Anything older than a day should be deleted
                string[] files = Directory.GetFiles(userInProcessDirectory);
                foreach (string thisFile in files)
                {
                    DateTime modifiedDate = ((new FileInfo(thisFile)).LastWriteTime);
                    if (DateTime.Now.Subtract(modifiedDate).TotalHours > (24*7))
                    {
                        try
                        {
                            File.Delete(thisFile);
                        }
                        catch (Exception)
                        {
                            // Unable to delete existing file in the RequestSpecificValues.Current_User's folder.
                            // This is an error, but how to report it?
                        }
                    }
                }
            }

            // Load the CompleteTemplate
            completeTemplate = Template_MemoryMgmt_Utility.Retrieve_Template("tei_edit", RequestSpecificValues.Tracer);
            if (completeTemplate != null)
            {
                RequestSpecificValues.Tracer.Add_Trace("Edit_TEI_Item_MySobekViewer.Constructor", "Found template in cache");
            }
            else
            {
                RequestSpecificValues.Tracer.Add_Trace("Edit_TEI_Item_MySobekViewer.Constructor", "Reading template");

                string user_template = Path.Combine(UI_ApplicationCache_Gateway.Settings.Servers.Application_Server_Network, "plugins\\tei\\templates\\user\\edit.xml");
                if (!File.Exists(user_template))
                    user_template = Path.Combine(UI_ApplicationCache_Gateway.Settings.Servers.Application_Server_Network, "plugins\\tei\\templates\\default\\edit.xml");

                // Read this CompleteTemplate
                Template_XML_Reader reader = new Template_XML_Reader();
                completeTemplate = new CompleteTemplate();
                reader.Read_XML(user_template, completeTemplate, true);

                // Save this into the cache
                Template_MemoryMgmt_Utility.Store_Template("tei_edit", completeTemplate, RequestSpecificValues.Tracer);
            }

            // Determine the number of total CompleteTemplate pages
            totalTemplatePages = completeTemplate.InputPages_Count + 3;

            // Determine the title for this CompleteTemplate, or use a default
            toolTitle = completeTemplate.Title;
            if (toolTitle.Length == 0)
                toolTitle = "TEI Editing Tool";

            // Determine the current phase
            currentProcessStep = 1;
            if ((RequestSpecificValues.Current_Mode.My_Sobek_SubMode.Length > 0) && (Char.IsNumber(RequestSpecificValues.Current_Mode.My_Sobek_SubMode[0])))
            {
                Int32.TryParse(RequestSpecificValues.Current_Mode.My_Sobek_SubMode.Substring(0), out currentProcessStep);
            }

            // Load some information from the session
            if (HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".Mapping_File"] != null)
                mapping_file = HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".Mapping_File"] as string;
            if (HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".XSLT_File"] != null)
                xslt_file = HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".XSLT_File"] as string;
            if (HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".CSS_File"] != null)
                css_file = HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".CSS_File"] as string;
            if (HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".Original_TEI_File"] != null)
                original_tei_file = HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".Original_TEI_File"] as string;

            // Pull the current item beig edited
            editingItem = null;
            if (HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".New_Item"] != null)
                editingItem = HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".New_Item"] as SobekCM_Item;

            // If any are null or blank, pull from the item
            if ((String.IsNullOrEmpty(mapping_file)) || (String.IsNullOrEmpty(xslt_file)) || (String.IsNullOrEmpty(original_tei_file)) || (editingItem == null))
            {
                // Pull the original item
                RequestSpecificValues.Tracer.Add_Trace("Edit_Item_Metadata_MySobekViewer.Constructor", "Try to pull this sobek complete item");
                SobekCM_Item currentItem = SobekEngineClient.Items.Get_Sobek_Item(bibid, vid, RequestSpecificValues.Current_User.UserID, RequestSpecificValues.Tracer);
                if (currentItem == null)
                {
                    RequestSpecificValues.Tracer.Add_Trace("Edit_Item_Metadata_MySobekViewer.Constructor", "Unable to build complete item");
                    RequestSpecificValues.Current_Mode.Mode = Display_Mode_Enum.Error;
                    RequestSpecificValues.Current_Mode.Error_Message = "Invalid Request : Unable to build complete item";
                    return;
                }

                // If the editing item was NULL, assign it
                if (editingItem == null)
                {
                    editingItem = currentItem;
                    editingItem.Source_Directory = userInProcessDirectory;
                    HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".New_Item"] = editingItem;
                }

                // Settings SHOULD not be null
                if (currentItem.Behaviors.Settings != null)
                {
                    // Build the setting dictionary
                    Dictionary<string, string> settingDictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
                    foreach (Tuple<string, string> setting in currentItem.Behaviors.Settings)
                    {
                        settingDictionary[setting.Item1] = setting.Item2;
                    }

                    // Now, use to assign to each value
                    if ((String.IsNullOrEmpty(mapping_file)) && (settingDictionary.ContainsKey("TEI.Mapping")))
                        mapping_file = settingDictionary["TEI.Mapping"];
                    if ((String.IsNullOrEmpty(css_file)) && (settingDictionary.ContainsKey("TEI.CSS")))
                        css_file = settingDictionary["TEI.CSS"];
                    if ((String.IsNullOrEmpty(xslt_file)) && (settingDictionary.ContainsKey("TEI.XSLT")))
                        xslt_file = settingDictionary["TEI.XSLT"];
                    if ((String.IsNullOrEmpty(original_tei_file)) && (settingDictionary.ContainsKey("TEI.Source_File")))
                        original_tei_file = settingDictionary["TEI.Source_File"];

                    // Now, save all these
                    HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".Mapping_File"] = mapping_file;
                    HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".XSLT_File"] = xslt_file;
                    HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".CSS_File"] = css_file;
                    HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".Original_TEI_File"] = original_tei_file;
                }
            }

            // If there is a boundary infraction here, go back to step 1
            if (currentProcessStep < 0)
                currentProcessStep = 1;
            if ((currentProcessStep > completeTemplate.InputPages.Count + 4) && (currentProcessStep != 8) && (currentProcessStep != 9))
                currentProcessStep = 1;

            // Find the TEI file
            if (Directory.Exists(userInProcessDirectory))
            {
                string[] tei_files = Directory.GetFiles(userInProcessDirectory, "*.xml");
                if (tei_files.Length > 1)
                {
                    // Two XML files, so delete all but the latest
                    string latest_tei_file = String.Empty;
                    DateTime latest_timestamp = DateTime.MinValue;

                    // Find the latest TEI file
                    foreach (string thisTeiFile in tei_files)
                    {
                        // If this is marc.xml, skip it
                        if ((Path.GetFileName(thisTeiFile).ToLower().IndexOf("marc.xml") >= 0) || (Path.GetFileName(thisTeiFile).ToLower().IndexOf("mets.xml") >= 0))
                            continue;

                        DateTime file_timestamp = File.GetLastWriteTime(thisTeiFile);

                        if (DateTime.Compare(latest_timestamp, file_timestamp) < 0)
                        {
                            latest_tei_file = thisTeiFile;
                            latest_timestamp = file_timestamp;
                        }
                    }

                    // If a latest file as found, delete the others
                    if (!String.IsNullOrEmpty(latest_tei_file))
                    {
                        foreach (string thisTeiFile in tei_files)
                        {
                            // If this is marc.xml, skip it
                            if ((Path.GetFileName(thisTeiFile).ToLower().IndexOf("marc.xml") >= 0) || (Path.GetFileName(thisTeiFile).ToLower().IndexOf("mets.xml") >= 0))
                                continue;

                            // Was this the latest file?
                            if (String.Compare(thisTeiFile, latest_tei_file, StringComparison.OrdinalIgnoreCase) == 0)
                                continue;

                            try
                            {
                                File.Delete(thisTeiFile);
                            }
                            catch
                            {
                            }

                        }
                    }

                    new_tei_file = latest_tei_file;
                }
                else if (tei_files.Length == 1)
                {
                    new_tei_file = Path.GetFileName(tei_files[0]);
                }
            }

            #region Handle any other post back requests

            // If this is post-back, handle it
            if (RequestSpecificValues.Current_Mode.isPostBack)
            {
                // Was this where the mapping, xslt, and css is set?
                if (currentProcessStep == 1)
                {
                    string[] getKeys = HttpContext.Current.Request.Form.AllKeys;
                    foreach (string thisKey in getKeys)
                    {
                        if (thisKey.IndexOf("mapping_select") == 0)
                        {
                            mapping_file = HttpContext.Current.Request.Form[thisKey];
                            HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".Mapping_File"] = mapping_file;
                        }
                        if (thisKey.IndexOf("xslt_select") == 0)
                        {
                            xslt_file = HttpContext.Current.Request.Form[thisKey];
                            HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".XSLT_File"] = xslt_file;
                        }
                        if (thisKey.IndexOf("css_select") == 0)
                        {
                            css_file = HttpContext.Current.Request.Form[thisKey];
                            HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".CSS_File"] = css_file;
                        }
                    }
                }

                // If this is a post back from editing the XML, save this
                if (currentProcessStep == 2)
                {
                    // Get the content of the edited source
                    string new_output = HttpContext.Current.Request.Form["tei_source_content"];

                    // If the user is editing the current TEI file, make a new version under the
                    // user process folder now
                    string save_tei_file = Path.Combine(userInProcessDirectory, original_tei_file);
                    if (!String.IsNullOrEmpty(new_tei_file))
                    {
                        // Determine new filename
                        save_tei_file = Path.Combine(userInProcessDirectory, new_tei_file);
                    }
                    else
                    {
                        new_tei_file = original_tei_file;
                    }

                    // Save new source file
                    try
                    {
                        StreamWriter writer = new StreamWriter(save_tei_file);
                        writer.Write(new_output);
                        writer.Flush();
                        writer.Close();

                        success_message = "Saved TEI changes";
                    }
                    catch (Exception ee)
                    {
                        error_message = "Error saving the new XML source.<br /><br />" + ee.Message + "<br /><br />";

                    }

                }

                string action = HttpContext.Current.Request.Form["action"];
                if (action == "cancel")
                {
                    // Clear all files in the RequestSpecificValues.Current_User process folder
                    try
                    {
                        string[] all_files = Directory.GetFiles(userInProcessDirectory);
                        foreach (string thisFile in all_files)
                            File.Delete(thisFile);
                        Directory.Delete(userInProcessDirectory);
                    }
                    catch (Exception ee)
                    {
                        new_tei_file = ee.Message;
                        // Unable to delete existing file in the RequestSpecificValues.Current_User's folder.
                        // This is an error, but how to report it?
                    }

                    // Clear all the information in memory
                    HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".New_Item"] = null;
                    HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".Mapping_File"] = null;
                    HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".XSLT_File"] = null;
                    HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".CSS_File"] = null;
                    HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".Original_TEI_File"] = null;

                    // Clear any temporarily assigned current project and CompleteTemplate
                    RequestSpecificValues.Current_User.Current_Default_Metadata = null;
                    RequestSpecificValues.Current_User.Current_Template = null;

                    // Forward back to item page
                    RequestSpecificValues.Current_Mode.Mode = Display_Mode_Enum.Item_Display;
                    RequestSpecificValues.Current_Mode.BibID = bibid;
                    RequestSpecificValues.Current_Mode.VID = vid;
                    UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                }

                if (action == "next_phase")
                {
                    string next_phase = HttpContext.Current.Request.Form["phase"];

                    if (currentProcessStep == 1)
                    {
                        // Should be a TEI file to continue
                        if (!String.IsNullOrEmpty(new_tei_file))
                        {
                            XmlValidator validator = new XmlValidator();
                            string tei_filepath = Path.Combine(userInProcessDirectory, new_tei_file);
                            bool isValid = validator.IsValid(tei_filepath);
                            if (!isValid)
                            {
                                string validatorErrors = validator.Errors.Replace("\n", "<br />\n");
                                error_message = "Uploaded TEI file is not a valid XML source file.<br /><br />\n" + validatorErrors;
                                next_phase = "1";
                            }
                        }
                    }

                    // Was this from editing the XMl file?
                    if (currentProcessStep == 2)
                    {
                        // Was there an error?
                        if (!String.IsNullOrEmpty(error_message))
                            next_phase = "2";
                        else if (!String.IsNullOrEmpty(new_tei_file))
                        {
                            XmlValidator validator = new XmlValidator();
                            string tei_filepath = Path.Combine(userInProcessDirectory, new_tei_file);
                            bool isValid = validator.IsValid(tei_filepath);
                            if (!isValid)
                            {
                                string validatorErrors = validator.Errors.Replace("\n", "<br />\n");
                                error_message = "TEI is not valid XML.<br /><br />\n" + validatorErrors;
                                next_phase = "2";
                            }
                        }
                    }

                    // If this is going from a step that includes the metadata entry portion, save this to the item
                    if ((currentProcessStep >= 4) && (currentProcessStep < 8))
                    {
                        // Save to the item
                        completeTemplate.Save_To_Bib(editingItem, RequestSpecificValues.Current_User, currentProcessStep - 4);
                        editingItem.Save_METS();
                        HttpContext.Current.Session["Edit_TEI_mySobekViewer." + bibid + "_" + vid + ".New_Item"] = editingItem;
                    }

                    // For now, just forward to the next phase
                    if (currentProcessStep.ToString() != next_phase)
                    {
                        RequestSpecificValues.Current_Mode.My_Sobek_SubMode = next_phase;
                        UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                        return;
                    }
                }
            }

            #endregion

            #region Perform some validation to determine if the RequestSpecificValues.Current_User should be at this step

            // If this is past the step to upload a TEI file, ensure a TEI file exists
            if ((currentProcessStep > 1) && ((String.IsNullOrEmpty(mapping_file)) || (String.IsNullOrEmpty(xslt_file))))
            {
                RequestSpecificValues.Current_Mode.My_Sobek_SubMode = "1";
                UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                return;
            }

            // If this is to put up items or complete the item, validate the METS
            if (currentProcessStep >= 8)
            {
                // Validate that a METS file exists
                if (Directory.GetFiles(userInProcessDirectory, "*.mets*").Length == 0)
                {
                    RequestSpecificValues.Current_Mode.My_Sobek_SubMode = "2";
                    UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                    return;
                }

                // Get the validation errors
                validationErrors = new List<string>();
                SobekCM_Item_Validator.Validate_SobekCM_Item(editingItem, validationErrors);

                // Get the validation errors
                if ((validationErrors == null ) || ( validationErrors.Count == 0))
                    editingItem.Save_METS();
                else
                {
                    editingItem.Web.Show_Validation_Errors = true;

                    RequestSpecificValues.Current_Mode.My_Sobek_SubMode = "4";
                    UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                    return;
                }
            }

            // If this is going into the last process step, check that any mandatory info (file, url, .. )
            // from the last step is present
            if (currentProcessStep == 9)
            {
                // Complete the item submission
                complete_item_submission(editingItem, RequestSpecificValues.Tracer);
            }

            #endregion
        }