public override void Add_Controls(PlaceHolder MainPlaceHolder, Custom_Tracer Tracer)
        {
            Tracer.Add_Trace("Track_Item_MySobekViewer.Add_Controls", "");
            //   base.Add_Controls(MainPlaceHolder, Tracer);

            string barcode_row_style = String.Empty;
            string manual_row_style = String.Empty;

            StringBuilder builder = new StringBuilder(2000);
            builder.AppendLine("<!-- Track_Item_MySobekViewer.Add_Controls -->");
            builder.AppendLine("  <link rel=\"stylesheet\" type=\"text/css\" href=\"" + currentMode.Base_URL + "default/SobekCM_MySobek.css\" /> ");
            builder.AppendLine("  <link rel=\"stylesheet\" type=\"text/css\" href=\"" + currentMode.Base_URL + "default/SobekCM_Admin.css\" /> ");

            builder.AppendLine("<script type=\"text/javascript\" src=\"" + currentMode.Base_URL + "default/scripts/jquery/jquery-ui-1.10.3.custom.min.js\"></script>");
            builder.AppendLine("<script type=\"text/javascript\" src=\"" + currentMode.Base_URL + "default/scripts/jquery/jquery.color-2.1.1.js\"></script>");

            //Set the JavaScript global page value based on the currently selected tab
            builder.AppendLine("<script type=\"text/javascript\">");
            builder.AppendLine("setCurrentTab(" + page + ");");
            builder.AppendLine("</script>");

            //Add the main title
            builder.AppendLine("<div class=\"SobekHomeText\">");
            builder.AppendLine("    <br />");
            builder.AppendLine("    <h1>Item Tracking</h1>");
            builder.AppendLine("  </div>");
            builder.AppendLine();

            //Add the hidden variables
            builder.AppendLine("<!-- Hidden field is used for postbacks to add new form elements (i.e., new page, etc..) -->");
            builder.AppendLine("<input type=\"hidden\" id=\"Track_Item_behaviors_request\" name=\"Track_Item_behaviors_request\" value=\"\"/>");
            builder.AppendLine("<input type=\"hidden\" id=\"Track_Item_hidden_value\" name=\"Track_Item_hidden_value\" value=\"\"/>");
            builder.AppendLine("<input type=\"hidden\" id=\"TI_entry_type\" name=\"TI_entry_type\" value=\"\"/>");
            builder.AppendLine("<input type=\"hidden\" id=\"hidden_BibID\" name=\"hidden_BibID\" value=\"\"/>");
            builder.AppendLine("<input type=\"hidden\" id=\"hidden_VID\" name=\"hidden_VID\" value=\"\" />");
            builder.AppendLine("<input type=\"hidden\" id=\"hidden_event_num\" name=\"hidden_event_num\" value=\"\" />");
            builder.AppendLine("<input type=\"hidden\" id=\"hidden_equipment\" name=\"hidden_equipment\" value=\"\"/>");
            builder.AppendLine("<input type=\"hidden\" id=\"hidden_selected_username\" name=\"hidden_selected_username\" value=\"\"/>");
            builder.AppendLine("<input type=\"hidden\" id=\"tracking_new_page\" name=\"tracking_new_page\" value=\"\"/>");
            builder.AppendLine("<input type=\"hidden\" id=\"hidden_itemID\" name=\"hidden_itemID\" value=\"\"/>");

            //Start the User, Equipment info table
            builder.AppendLine("<span class=\"sbkTi_HomeText\"><h2>User and Equipment</h2></span>");
            builder.AppendLine("<table class=\"sbkTi_table\">");
            builder.AppendLine("<tr>");
            builder.AppendLine("          <td>Scanned/Processed by:</td>");
            builder.AppendLine("          <td><select id=\"ddlUserStart\" name=\"ddlUserStart\" onchange=\"ddlUser_Changed(this.id);\">");

            //Add the list of users to the dropdown list
            foreach (KeyValuePair<string, User_Object> thisUser in user_list)
            {
                if (thisUser.Key == current_selected_user.UserName)
                    builder.AppendLine("<option value=\"" + thisUser.Key + "\" selected>" + thisUser.Value.Full_Name + "</option>");
                else
                {
                    builder.AppendLine("<option value=\"" + thisUser.Key + "\">" + thisUser.Value.Full_Name + "</option>");
                }
            }
            builder.AppendLine("</td>");
            builder.AppendLine("           <td>Equipment used:</td>");
            builder.AppendLine("           <td><select name=\"ddlEquipmentStart\" id=\"ddlEquipmentStart\" onchange=\"ddlEquipment_Changed(this.id);\">");

            //Add the list of scanners to the dropdown list
            foreach (string thisScanner in scanners_list)
            {
                if (thisScanner == equipment)
                    builder.AppendLine("<option value=\"" + thisScanner + "\" selected>" + thisScanner + "</option>");
                else
                    builder.AppendLine("<option value=\"" + thisScanner + "\">" + thisScanner + "</option>");
            }
            builder.AppendLine("</select></td>");
            builder.AppendLine("</tr>");
            builder.AppendLine("</table>");

            //Start the Item Information Table
            string bibid = (String.IsNullOrEmpty(BibID)) ? String.Empty : BibID;
            string vid = (String.IsNullOrEmpty(VID)) ? String.Empty : VID;

            // Start the outer tab container
            builder.AppendLine("  <div id=\"tabContainer\" class=\"fulltabs\">");

            builder.AppendLine("    <div class=\"tabs\">");
            builder.AppendLine("      <ul>");

            const string DURATION = "Track with Duration";
            const string SINGLE_POINT = "Track without Duration";
            const string EDIT_SCREEN = "Edit Previous Entries";

            //Draw all the page tabs for this form

            builder.AppendLine("    <li id=\"tabHeader_1\" onclick=\"save_item_tracking('1');\">" + DURATION + "</li>");
            builder.AppendLine("     <li id=\"tabHeader_2\" onclick=\"save_item_tracking('2');\">" + SINGLE_POINT + "</li>");
            //builder.AppendLine("     <li id=\"tabHeader_3\" onclick=\"save_item_tracking('3');\">" + EDIT_SCREEN + "</li>");

            builder.AppendLine("</ul>");
            builder.AppendLine("</div>");

            //Start the divs for the content of the tabs
            builder.AppendLine("    <div class=\"tabscontent\">");

            #region First tab - Tracking with duration

            builder.AppendLine("            <div class=\"tabpage\" id=\"tabpage_1\">");
            //Start the item information table

            //Display errors if any
            if (error_message.Length > 0 && page == 1)
            {
                builder.AppendLine("<span style=\"color:red; float:left;\">" + error_message + "</span><br/>");
            }
            builder.AppendLine("<span class=\"sbkTi_HomeText\"><h2>Item Information</h2></span>");
            builder.AppendLine("<table class=\"sbkTi_table\">");

            //Add the item info section
            if (hidden_request == "read_manual_entry")
            {
                //Add the option for barcode entry
                builder.AppendLine("<tr><td colspan=\"100%\"><input type=\"radio\" name=\"rbEntryType\" id=\"rb_barcode\" value=0 onclick=\"rbEntryTypeChanged(this.value);\">Barcode Entry</td></tr>");
                barcode_row_style = "style=\"margin-left:200px;\"";

                builder.AppendLine("<tr id=\"tblrow_Barcode\" " + barcode_row_style + "><td></td><td>Scan barcode here:</td>");
                builder.AppendLine("         <td colspan=\"3\"><input type=\"text\" id=\"txtScannedString\" name=\"txtScannedString\" autofocus onchange=\"BarcodeStringTextbox_Changed(this.value);\"/></td>");
                builder.AppendLine("<td>");
                builder.AppendLine("<div id=\"divAddButton_barcode\" style=\"float:right;\">");
                builder.AppendLine("    <button title=\"Add new tracking entry\" class=\"sbkMySobek_RoundButton\" onclick=\"Add_new_entry_barcode(); return false;\">ADD</button>");
                builder.AppendLine("</div></td></tr>");

                //Add the option for manual entry
                builder.AppendLine("<td colspan=\"100%\"><input type=\"radio\" name=\"rbEntryType\" id=\"rb_manual\" value=1 checked onclick=\"rbEntryTypeChanged(this.value);\">Manual Entry</td></tr>");
                if (page == 1)
                {
                    builder.AppendLine("<tr id=\"tblrow_Manual1\" " + manual_row_style + "><td></td><td>BibID:</td><td><input type=\"text\" id=\"txtBibID\" value=\"" + bibid + "\" /></td>");
                    builder.AppendLine("         <td>VID:</td><td><input type=\"text\" id=\"txtVID\" value=\"" + vid + "\" /></td>");
                }
                else
                {
                    builder.AppendLine("<tr id=\"tblrow_Manual1\" " + manual_row_style + "><td></td><td>BibID:</td><td><input type=\"text\" id=\"txtBibID\" value=\"\" /></td>");
                    builder.AppendLine("         <td>VID:</td><td><input type=\"text\" id=\"txtVID\" value=\"\" /></td>");
                }
                builder.AppendLine("</tr>");
                builder.AppendLine("<tr id=\"tblrow_Manual2\" " + manual_row_style + ">");
                builder.AppendLine("<td></td><td>Event:</td><td><select id=\"ddlManualEvent\" name=\"ddlManualEvent\">");
                builder.AppendLine("                                       <option value=\"1\" selected>Start Scanning</option>");
                builder.AppendLine("                                        <option value=\"2\">End Scanning</option>");
                builder.AppendLine("                                        <option value=\"3\">Start Processing</option>");
                builder.AppendLine("                                        <option value=\"4\">End Processing</option></select>");
                builder.AppendLine("</td>");

                //Call the JavaScript functions to apply the appropriate CSS class for the disabled row(s)
                builder.AppendLine("<script type=\"text/javascript\">DisableRow_SetCSSClass('tblrow_Barcode');</script>");
                builder.AppendLine("<script type=\"text/javascript\">DisableRow_RemoveCSSClass('tblrow_Manual1');</script>");
                builder.AppendLine("<script type=\"text/javascript\">DisableRow_RemoveCSSClass('tblrow_Manual2');</script>");

            }
            else
            {
                builder.AppendLine("<tr><td colspan=\"100%\"><input type=\"radio\" name=\"rbEntryType\" id=\"rb_barcode\" value=0 checked onclick=\"rbEntryTypeChanged(this.value);\">Barcode Entry</td></tr>");
                manual_row_style = "style=\"margin-left:200px;\"";

                builder.AppendLine("<tr id=\"tblrow_Barcode\" " + barcode_row_style + "><td></td><td>Scan barcode here:</td>");
                builder.AppendLine("         <td colspan=\"3\"><input type=\"text\" id=\"txtScannedString\" name=\"txtScannedString\" autofocus onchange=\"BarcodeStringTextbox_Changed(this.value);\"/></td>");
                builder.AppendLine("<td>");
                builder.AppendLine("<div id=\"divAddButton_barcode\" style=\"float:right;\">");
                builder.AppendLine("    <button title=\"Add new tracking entry\" class=\"sbkMySobek_RoundButton\" onclick=\"Add_new_entry_barcode(); return false;\">ADD</button>");
                builder.AppendLine("</div></td></tr>");

                if (page == 1)
                {
                    builder.AppendLine("<tr><td colspan=\"100%\"><input type=\"radio\" name=\"rbEntryType\" id=\"rb_manual\" value=1 onclick=\"rbEntryTypeChanged(this.value);\">Manual Entry</td></tr>");
                    builder.AppendLine("<tr id=\"tblrow_Manual1\" " + manual_row_style + "><td></td><td>BibID:</td><td><input type=\"text\" id=\"txtBibID\" value=\"" + bibid + "\" /></td>");
                    builder.AppendLine("         <td>VID:</td><td><input type=\"text\" id=\"txtVID\" value=\"" + vid + "\" /></td>");
                }
                else
                {
                    builder.AppendLine("<tr><td colspan=\"100%\"><input type=\"radio\" name=\"rbEntryType\" id=\"rb_manual\" value=1 onclick=\"rbEntryTypeChanged(this.value);\">Manual Entry</td></tr>");
                    builder.AppendLine("<tr id=\"tblrow_Manual1\" " + manual_row_style + "><td></td><td>BibID:</td><td><input type=\"text\" id=\"txtBibID\" value=\"\" /></td>");
                    builder.AppendLine("         <td>VID:</td><td><input type=\"text\" id=\"txtVID\" value=\"\" /></td>");
                }
                builder.AppendLine("</tr>");
                builder.AppendLine("<tr id=\"tblrow_Manual2\" " + manual_row_style + ">");
                builder.AppendLine("<td></td><td>Event:</td><td><select id=\"ddlManualEvent\" name=\"ddlManualEvent\">");
                builder.AppendLine("                                       <option value=\"1\" selected>Start Scanning</option>");
                builder.AppendLine("                                        <option value=\"2\">End Scanning</option>");
                builder.AppendLine("                                        <option value=\"3\">Start Processing</option>");
                builder.AppendLine("                                        <option value=\"4\">End Processing</option></select>");
                builder.AppendLine("</td>");

                //Call the JavaScript functions to apply the appropriate CSS class for the disabled row(s)
                builder.AppendLine("<script type=\"text/javascript\">DisableRow_SetCSSClass('tblrow_Manual1');</script>");
                builder.AppendLine("<script type=\"text/javascript\">DisableRow_SetCSSClass('tblrow_Manual2');</script>");

                builder.AppendLine("<script type=\"text/javascript\">DisableRow_RemoveCSSClass('tblrow_Barcode');</script>");

            }

            //Set the appropriate value in the workflow dropdown
            if (stage >= 1)
            {
                builder.AppendLine("<script type=\"text/javascript\">SetDropdown_Selected(" + stage + ");</script>");
            }

            builder.AppendLine("<td>");
            builder.AppendLine("<div id=\"divAddButton\" style=\"float:right;\">");
            builder.AppendLine("    <button title=\"Add new tracking entry\" class=\"sbkMySobek_RoundButton\" onclick=\"Add_new_entry(); return false;\">ADD</button>");
            builder.AppendLine("</div></td></tr>");

            builder.AppendLine("</table>");

            //End the item information table

            //If a new event has been scanned/entered, then display this table
            if (!String.IsNullOrEmpty(bibid) && !String.IsNullOrEmpty(vid) && !(error_message.Length > 0))
            {

                string selected_text_scanning = String.Empty;
                string selected_text_processing = String.Empty;
                if (stage == 1 || stage == 2)
                    selected_text_scanning = "selected";
                else
                    selected_text_processing = "selected";
                string currentTime = DateTime.Now.ToString("");

                if (page == 1)
                {
                    string currentDate = DateTime.Now.Date.ToString("yyyy-MM-dd");
                    //If this workflow has already been saved, get the saved information to display
                    if (current_workflow_id > 0)
                    {
                        this_workflow = current_workflows[current_workflow_id.ToString()];
                        //Tracking_Workflow thisSavedWorkflow = new Tracking_Workflow();
                        bibid = this_workflow.BibID;
                        vid = this_workflow.VID;
                        title = this_workflow.Title;
                        start_Time = this_workflow.StartTime;
                        end_Time = this_workflow.EndTime;
                        currentDate = this_workflow.Date;
                        if (this_workflow.Workflow_type == 1 || this_workflow.Workflow_type == 2)
                        {
                            selected_text_scanning = "selected";
                            selected_text_processing = String.Empty;
                        }
                        else
                        {
                            selected_text_scanning = String.Empty;
                            selected_text_processing = "selected";
                        }
                    }

                    //Start the Tracking Info section
                    builder.AppendLine("<span class=\"sbkTi_HomeText\"><h2>Add Tracking Information</h2></span>");
                    builder.AppendLine("<span id = \"TI_NewEntrySpan\" class=\"sbkTi_TrackingEntrySpanMouseOut\" onmouseover=\"return entry_span_mouseover(this.id);\" onmouseout=\"return entry_span_mouseout(this.id);\">");
                    builder.AppendLine("<table class=\"sbkTi_table\">");

                    //If the user is trying to close an unopened entry, display an error message
                    if (close_error == true)
                    {
                        builder.AppendLine("<tr><td colspan=\"4\">");
                        builder.AppendLine("<span style=\"color:red;\">There is no matching open workflow to close!</span>");
                        builder.AppendLine("</td></tr>");
                    }

                    builder.AppendLine("<tr >");
                    builder.AppendLine("<td>Item: </td><td>" + bibid + ":" + vid + "</td>");

                    builder.AppendLine("<td>Title: </td><td>" + title + "</td>");
                    builder.AppendLine("</tr>");

                    builder.AppendLine("<tr><td>Workflow:</td>");
                    builder.AppendLine("         <td><select id=\"ddlEvent" + current_workflow_id + "\" name=\"ddlEvent" + current_workflow_id + "\"> disabled");
                    builder.AppendLine("                  <option value=\"1\" " + selected_text_scanning + ">Scanning</option>");
                    builder.AppendLine("                  <option value=\"2\"" + selected_text_processing + ">Processing</option></select>");
                    builder.AppendLine("         </td>");
                    builder.AppendLine("         <td>Date:</td>");

                    //Set this as a JQuery datepicker field
                    //builder.AppendLine("<script type=\"text/javascript\">$(function(){$(\"#txtStartDate" + current_workflow_id + "\").datepicker();});</script>");
                    builder.AppendLine("         <td><input type=\"text\"  name=\"txtStartDate" + current_workflow_id + "\" id=\"txtStartDate" + current_workflow_id + "\" value=\"" + Convert.ToDateTime(currentDate).ToString("MM/dd/yyyy") + "\" /> </td>");
                    builder.AppendLine("<script type=\"text/javascript\">setDatePicker(\"txtStartDate" + current_workflow_id + "\");</script>");
                    builder.AppendLine("</tr>");

                    //Add the Start and End Times
                    builder.AppendLine("<tr>");
                    builder.AppendLine("<td>Start Time:</td>");
                    if (close_error == true)
                    {
                        // builder.AppendLine("<td><input type=\"time\" name=\"txtStartTime" + current_workflow_id + "\" class=\"sbkTi_ErrorBox\" id=\"txtStartTime" + current_workflow_id + "\" value = \"" + start_Time + "\"/></td>");
                        builder.AppendLine("<td><input type=\"text\" name=\"txtStartTime" + current_workflow_id + "\" class=\"sbkTi_ErrorBox\" id=\"txtStartTime" + current_workflow_id + "\" value = \"" + start_Time + "\"/></td>");
                        builder.AppendLine("<script type=\"text/javascript\">setTimePicker(\"txtStartTime" + current_workflow_id + "\");</script>");

                    }
                    else
                    {
                        // builder.AppendLine("<td><input type=\"time\" name=\"txtStartTime" + current_workflow_id + "\" id=\"txtStartTime" + current_workflow_id + "\" value = \"" + start_Time + "\"/></td>");
                        builder.AppendLine("<td><input type=\"text\" name=\"txtStartTime" + current_workflow_id + "\" id=\"txtStartTime" + current_workflow_id + "\" value = \"" + start_Time + "\"/></td>");
                        builder.AppendLine("<script type=\"text/javascript\">setTimePicker(\"txtStartTime" + current_workflow_id + "\");</script>");
                    }
                    builder.AppendLine("<td>End Time:</td>");
                    builder.AppendLine("<td><input type=\"text\" name=\"txtEndTime" + current_workflow_id + "\" id=\"txtEndTime" + current_workflow_id + "\" value = \"" + end_Time + "\"/></td></tr>");
                    builder.AppendLine("<script type=\"text/javascript\">setTimePicker(\"txtEndTime" + current_workflow_id + "\");</script>");

                    builder.AppendLine("<tr><td colspan=\"4\"><span style=\"float:right;\">");
                    builder.AppendLine("    <button title=\"Save changes\" class=\"sbkMySobek_RoundButton\" onclick=\"save_workflow('" + current_workflow_id + "',' " + itemID + "'); return false;\">SAVE</button>");
                    builder.AppendLine("    <button title=\"Delete this workflow\" class=\"sbkMySobek_RoundButton\" onclick=\"delete_workflow(" + current_workflow_id + "); \">DELETE</button>");
                    builder.AppendLine("</span></td></tr>");

                    //End this table
                    builder.AppendLine("</table>");

                    builder.AppendLine("</span>");
                }

                //If there are any previously opened and unclosed workflows for this item
                if (open_workflows_from_DB != null && open_workflows_from_DB.Rows.Count > 0)
                {

                    //builder.AppendLine("<tr><th>Item</th><th>Workflow</th><th>Date</th><th>Start Time</th><th>End Time</th><th>User</th><th>Equipment</th></tr>");
                    foreach (DataRow row in open_workflows_from_DB.Rows)
                    {
                        string thisWorkflowID = row["ProgressID"].ToString();
                        //If this is a "close" workflow event, display all open and unclosed workflows from before today
                        if (stage == 2 || stage == 4)
                        {
                            if (row["DateStarted"] != null && Convert.ToDateTime(row["DateStarted"]).ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd"))
                                continue;
                        }

                        string this_date_started = String.Empty;
                        if (row["DateStarted"] != null)
                        {
                            object thisRowDateStarted = (object)(row["DateStarted"]);
                            this_date_started = (thisRowDateStarted == DBNull.Value) ? String.Empty : Convert.ToDateTime(row["DateStarted"]).ToString("MM/dd/yyyy");
                        }
                        builder.AppendLine("<span id=\"TI_NewEntry_duplicate_Span" + row["ProgressID"] + "\"  class=\"sbkTi_TrackingEntrySpanMouseOut\"  onmouseover=\"return entry_span_mouseover(this.id);\" onmouseout=\"return entry_span_mouseout(this.id);\">");
                        builder.AppendLine("<table class=\"sbkTi_table\" >");

                        builder.AppendLine("<tr><td colspan=\"4\">");
                        builder.AppendLine("<span style=\"color:red;\">You have a previously opened workflow from " + this_date_started + "! </span>");

                        builder.AppendLine("</td></tr>");

                        //Start the table
                        builder.AppendLine("<tr >");
                        builder.AppendLine("<td>Item: </td><td>" + BibID + ":" + VID + "</td>");

                        builder.AppendLine("<td>Title: </td><td>" + title + "</td>");
                        builder.AppendLine("</tr>");

                        string this_workflow = row["WorkFlowName"].ToString();
                        if (this_workflow == "Scanning")
                        {
                            selected_text_scanning = "selected";
                            selected_text_processing = "";
                        }
                        else if (this_workflow == "Processing")
                        {
                            selected_text_scanning = "";
                            selected_text_processing = "selected";
                        }
                        builder.AppendLine("<tr><td>Workflow:</td>");
                        builder.AppendLine("         <td><select id=\"ddlEvent" + thisWorkflowID + "\" name=\"ddlEvent" + thisWorkflowID + "\"> disabled");
                        builder.AppendLine("                  <option value=\"1\" " + selected_text_scanning + ">Scanning</option>");
                        builder.AppendLine("                  <option value=\"2\"" + selected_text_processing + ">Processing</option></select>");
                        builder.AppendLine("         </td>");
                        builder.AppendLine("         <td>Date:</td>");

                        DateTime startDateToDisplay;
                        string startDateDisplayString = String.Empty;
                        //DateTime.TryParse(this_date_started, out startDateToDisplay);
                        if (!String.IsNullOrEmpty(row["DateStarted"].ToString()))
                            startDateDisplayString = Convert.ToDateTime(row["DateStarted"].ToString()).ToString();
                        builder.AppendLine("         <td><input type=\"text\" name=\"txtStartDate" + thisWorkflowID + "\" id=\"txtStartDate" + thisWorkflowID + "\" value=\"" + (this_date_started == String.Empty ? String.Empty : Convert.ToDateTime(startDateDisplayString).ToString("MM/dd/yyyy")) + "\" /> </td>");
                        builder.AppendLine("<script type=\"text/javascript\">setDatePicker(\"txtStartDate" + thisWorkflowID + "\");</script>");
                        builder.AppendLine("</tr>");

                        //Add the Start and End Times
                        builder.AppendLine("<tr>");
                        builder.AppendLine("<td>Start Time:</td>");
                        builder.AppendLine("<td><input type=\"text\" name=\"txtStartTime" + thisWorkflowID + "\" id=\"txtStartTime" + thisWorkflowID + "\" value = \"" + (this_date_started == String.Empty ? String.Empty : Convert.ToDateTime(startDateDisplayString).ToString("hh:mmtt")) + "\"/></td>");
                        builder.AppendLine("<script type=\"text/javascript\">setTimePicker(\"txtStartTime" + thisWorkflowID + "\");</script>");

                        builder.AppendLine("<td>End Time:</td>");
                        builder.AppendLine("<td><input type=\"text\" name=\"txtEndTime" + thisWorkflowID + "\" id=\"txtEndTime" + thisWorkflowID + "\" class=\"sbkTi_ErrorBox\" value = \"\"/></td></tr>");
                        builder.AppendLine("<script type=\"text/javascript\">setTimePicker(\"txtEndTime" + thisWorkflowID + "\");</script>");

                        builder.AppendLine("<tr><td colspan=\"4\"><span style=\"float:right;\">");
                        builder.AppendLine("    <button title=\"Save changes\" class=\"sbkMySobek_RoundButton\" onclick=\"save_workflow('" + thisWorkflowID + "','" + row["ItemID"] + "'); return false;\">SAVE</button>");
                        builder.AppendLine("    <button title=\"Delete this workflow\" class=\"sbkMySobek_RoundButton\" onclick=\"delete_workflow(" + thisWorkflowID + "); \">DELETE</button>");
                        builder.AppendLine("</span></td></tr>");

                        //End this table
                        builder.AppendLine("</table>");
                        builder.AppendLine("</span>");

                    }

                }
            }
            //Add the current History table
            if (current_workflows != null && current_workflows.Count > 0)
            {
                builder.AppendLine("<span class=\"sbkTi_HomeText\"><h2>Current Work History</h2></span>");
                builder.AppendLine("<table id=\"sbkTi_tblCurrentTracking\" class=\"sbkSaav_Table\">");
                builder.AppendLine("<tr><th>Item</th><th>Workflow</th><th>Date</th><th>Start Time</th><th>End Time</th><th>User</th><th>Equipment</th></tr>");
                foreach (KeyValuePair<string, Tracking_Workflow> thisPair in current_workflows)
                {
                    Tracking_Workflow this_workflow = thisPair.Value;
                    string workflow_text = String.Empty;
                    if (this_workflow.Workflow_type == 1 || this_workflow.Workflow_type == 2)
                        workflow_text = "Scanning";
                    else
                    {
                        workflow_text = "Processing";
                    }
                    builder.AppendLine("<tr>");
                    builder.AppendLine("<td title=\"" + this_workflow.Title + "\">" + this_workflow.BibID + ":" + this_workflow.VID + "</td>");
                    builder.AppendLine("<td>" + workflow_text + "</td>");
                    builder.AppendLine("<td>" + Convert.ToDateTime(this_workflow.Date).ToString("MM/dd/yyyy") + "</td>");
                    builder.AppendLine("<td>" + (String.IsNullOrEmpty(this_workflow.StartTime) ? "-" : Convert.ToDateTime(this_workflow.StartTime).ToString("hh:mm tt")) + "</td>");
                    builder.AppendLine("<td>" + (String.IsNullOrEmpty(this_workflow.EndTime) ? "-" : Convert.ToDateTime(this_workflow.EndTime).ToString("hh:mm tt")) + "</td>");
                    builder.AppendLine("<td>" + this_workflow.thisUser.UserName + "</td>");
                    builder.AppendLine("<td>" + this_workflow.Equipment + "</td>");
                    builder.AppendLine("</tr>");
                }

                builder.AppendLine("</table>");

            }

            //Add the Save and Done buttons
            builder.AppendLine("<div id=\"divButtons\" style=\"float:right;\">");
            //               builder.AppendLine("    <button title=\"Save changes\" class=\"sbkMySobek_RoundButton\" onclick=\"save(); return false;\">SAVE</button>");
            builder.AppendLine("    <button title=\"Save all changes and exit\" class=\"sbkMySobek_RoundButton\" onclick=\"save(); return false;\">DONE</button>");
            builder.AppendLine("</div>");
            builder.AppendLine("<br/><br/>");

            //             }

            //Close the inner tab div
            builder.AppendLine("</div>");

            #endregion

            #region Second tab - Tracking without duration

            builder.AppendLine("            <div class=\"tabpage\" id=\"tabpage_2\">");

            //Start the item information table
            //Display errors if any
            if (error_message.Length > 0 && page == 2)
            {
                builder.AppendLine("<span style=\"color:red; float:left;\";>" + error_message + "</span><br/>");
            }

            builder.AppendLine("<span class=\"sbkTi_HomeText\"><h2>Item Information</h2></span>");
            builder.AppendLine("<table class=\"sbkTi_table\">");

            //Add the item info section
            if (hidden_request == "read_manual_entry")
            {
                //Add the option for barcode entry
                builder.AppendLine("<tr><td colspan=\"100%\"><input type=\"radio\" name=\"rbEntryType2\" id=\"rb_barcode\" value=0  onclick=\"rbEntryType2Changed(this.value);\">Barcode Entry</td></tr>");
                barcode_row_style = "style=\"margin-left:200px\";";

                builder.AppendLine("<tr id=\"tblrow2_Barcode\" " + barcode_row_style + "><td></td><td>Scan barcode here:</td>");
                builder.AppendLine("         <td colspan=\"3\"><input type=\"text\" id=\"txtScannedString2\" name=\"txtScannedString2\" autofocus onchange=\"BarcodeStringTextbox2_Changed(this.value);\"/></td>");
                builder.AppendLine("<td>");
                builder.AppendLine("<div id=\"divAddButton2_barcode\" style=\"float:right;\">");
                builder.AppendLine("    <button title=\"Add new tracking entry\" class=\"sbkMySobek_RoundButton\" onclick=\"Add_new_entry_barcode2(); return false;\">ADD</button>");
                builder.AppendLine("</div></td></tr>");

                //Add the option for manual entry
                builder.AppendLine("<td colspan=\"100%\"><input type=\"radio\" name=\"rbEntryType2\" id=\"rb_manual\" value=1 checked onclick=\"rbEntryType2Changed(this.value);\">Manual Entry</td></tr>");

                if (page == 2)
                {
                    builder.AppendLine("<tr id=\"tblrow2_Manual1\" " + manual_row_style + "><td></td><td>BibID:</td><td><input type=\"text\" id=\"txtBibID2\"  value=\"" + bibid + "\" /></td>");
                    builder.AppendLine("         <td>VID:</td><td><input type=\"text\" id=\"txtVID2\" value=\"" + vid + "\" /></td>");
                }
                else
                {
                    builder.AppendLine("<tr id=\"tblrow2_Manual1\" " + manual_row_style + "><td></td><td>BibID:</td><td><input type=\"text\" id=\"txtBibID2\"  value=\"\" /></td>");
                    builder.AppendLine("         <td>VID:</td><td><input type=\"text\" id=\"txtVID2\" value=\"\" /></td>");
                }
                builder.AppendLine("         <td>VID:</td><td><input type=\"text\" id=\"txtVID2\" value=\"" + vid + "\" /></td>");
                builder.AppendLine("</tr>");
                builder.AppendLine("<tr id=\"tblrow2_Manual2\" " + manual_row_style + ">");
                builder.AppendLine("<td></td><td>Event:</td><td><select id=\"ddlManualEvent2\" name=\"ddlManualEvent2\">");
                builder.AppendLine("                                       <option value=\"1\" selected>Start Scanning</option>");
                builder.AppendLine("                                        <option value=\"2\">End Scanning</option>");
                builder.AppendLine("                                        <option value=\"3\">Start Processing</option>");
                builder.AppendLine("                                        <option value=\"4\">End Processing</option></select>");
                builder.AppendLine("</td>");

                //Call the JavaScript functions to apply the appropriate CSS class for the disabled row(s)
                builder.AppendLine("<script type=\"text/javascript\">DisableRow_SetCSSClass('tblrow2_Barcode');</script>");
                builder.AppendLine("<script type=\"text/javascript\">DisableRow_RemoveCSSClass('tblrow2_Manual1');</script>");
                builder.AppendLine("<script type=\"text/javascript\">DisableRow_RemoveCSSClass('tblrow2_Manual2');</script>");

            }
            else
            {
                builder.AppendLine("<tr><td colspan=\"100%\"><input type=\"radio\" name=\"rbEntryType2\" id=\"rb_barcode\" value=0 checked onclick=\"rbEntryType2Changed(this.value);\"/>Barcode Entry</td></tr>");
                manual_row_style = "style=\"margin-left:200px\";";

                builder.AppendLine("<tr id=\"tblrow2_Barcode\" " + barcode_row_style + "><td></td><td>Scan barcode here:</td>");
                builder.AppendLine("         <td colspan=\"3\"><input type=\"text\" id=\"txtScannedString2\" name=\"txtScannedString2\" autofocus onchange=\"BarcodeStringTextbox_Changed(this.value);\"/></td>");
                builder.AppendLine("<td>");
                builder.AppendLine("<div id=\"divAddButton_barcode\" style=\"float:right;\">");
                builder.AppendLine("    <button title=\"Add new tracking entry\" class=\"sbkMySobek_RoundButton\" onclick=\"Add_new_entry_barcode2(); return false;\">ADD</button>");
                builder.AppendLine("</div></td></tr>");

                builder.AppendLine("<tr><td colspan=\"100%\"><input type=\"radio\" name=\"rbEntryType2\" id=\"rb_manual\" value=1 onclick=\"rbEntryType2Changed(this.value);\"/>Manual Entry</td></tr>");
                if (page == 2)
                {
                    builder.AppendLine("<tr id=\"tblrow2_Manual1\" " + manual_row_style + "><td></td><td>BibID:</td><td><input type=\"text\" id=\"txtBibID2\" value=\"" + bibid + "\" /></td>");
                    builder.AppendLine("         <td>VID:</td><td><input type=\"text\" id=\"txtVID2\" value=\"" + vid + "\" /></td>");
                }
                else
                {
                    builder.AppendLine("<tr id=\"tblrow2_Manual1\" " + manual_row_style + "><td></td><td>BibID:</td><td><input type=\"text\" id=\"txtBibID2\" value=\"\" /></td>");
                    builder.AppendLine("         <td>VID:</td><td><input type=\"text\" id=\"txtVID2\" value=\"\" /></td>");
                }
                builder.AppendLine("</tr>");
                builder.AppendLine("<tr id=\"tblrow2_Manual2\" " + manual_row_style + ">");
                builder.AppendLine("<td></td><td>Event:</td><td><select id=\"ddlManualEvent2\" name=\"ddlManualEvent2\">");
                builder.AppendLine("                                       <option value=\"1\" selected>Start Scanning</option>");
                builder.AppendLine("                                        <option value=\"2\">End Scanning</option>");
                builder.AppendLine("                                        <option value=\"3\">Start Processing</option>");
                builder.AppendLine("                                        <option value=\"4\">End Processing</option></select>");
                builder.AppendLine("</td>");

                //Call the JavaScript functions to apply the appropriate CSS class for the disabled row(s)
                builder.AppendLine("<script type=\"text/javascript\">DisableRow_SetCSSClass('tblrow2_Manual1');</script>");
                builder.AppendLine("<script type=\"text/javascript\">DisableRow_SetCSSClass('tblrow2_Manual2');</script>");

                builder.AppendLine("<script type=\"text/javascript\">DisableRow_RemoveCSSClass('tblrow2_Barcode');</script>");

            }

            //Set the appropriate value in the workflow dropdown
            if (stage >= 1)
            {
                builder.AppendLine("<script type=\"text/javascript\">SetDropdown_Selected(" + stage + ");</script>");
            }

            builder.AppendLine("<td>");
            builder.AppendLine("<div id=\"divAddButton\" style=\"float:right;\">");
            builder.AppendLine("    <button title=\"Add new tracking entry\" class=\"sbkMySobek_RoundButton\" onclick=\"Add_new_entry(); return false;\">ADD</button>");
            builder.AppendLine("</div></td></tr>");

            builder.AppendLine("</table>");

            //End the item information table

            //If a new event has been scanned/entered, then display this table
            if (!String.IsNullOrEmpty(bibid) && !String.IsNullOrEmpty(vid) && !(error_message.Length > 0))
            {

                string selected_text_scanning = String.Empty;
                string selected_text_processing = String.Empty;
                string currentTime = DateTime.Now.ToString("");
                string currentDate = DateTime.Now.Date.ToString("yyyy-MM-dd");

                //Start the Tracking Info section
                if (page == 2)
                {
                    if (current_workflow_id > 0)
                    {
                        this_workflow = current_workflows_no_durations[current_workflow_id.ToString()];
                        //Tracking_Workflow thisSavedWorkflow = new Tracking_Workflow();
                        bibid = this_workflow.BibID;
                        vid = this_workflow.VID;
                        title = this_workflow.Title;
                        currentDate = this_workflow.Date;
                        if (this_workflow.Workflow_type == 1 || this_workflow.Workflow_type == 2)
                        {
                            selected_text_scanning = "selected";
                            selected_text_processing = String.Empty;
                        }
                        else
                        {
                            selected_text_scanning = String.Empty;
                            selected_text_processing = "selected";
                        }
                    }

                    builder.AppendLine("<span class=\"sbkTi_HomeText\"><h2>Add Tracking Information</h2></span>");
                    builder.AppendLine("<span id = \"TI_NewEntrySpan\" class=\"sbkTi_TrackingEntrySpanMouseOut\" onmouseover=\"return entry_span_mouseover(this.id);\" onmouseout=\"return entry_span_mouseout(this.id);\">");
                    builder.AppendLine("<table class=\"sbkTi_table\">");
                    builder.AppendLine("<tr >");
                    builder.AppendLine("<td>Item: </td><td>" + bibid + ":" + vid + "</td>");

                    builder.AppendLine("<td>Title: </td><td>" + title + "</td>");
                    builder.AppendLine("</tr>");

                    builder.AppendLine("<tr><td>Workflow:</td>");
                    builder.AppendLine("         <td><select id=\"ddlEvent2\" name=\"ddlEvent2\"> disabled");
                    builder.AppendLine("                  <option value=\"1\" " + selected_text_scanning + ">Scanning</option>");
                    builder.AppendLine("                  <option value=\"2\"" + selected_text_processing + ">Processing</option></select>");
                    builder.AppendLine("         </td>");
                    builder.AppendLine("         <td>Date:</td>");

                    builder.AppendLine("         <td><input type=\"text\" name=\"txtStartDate2\" id=\"txtStartDate2\" value=\"" + Convert.ToDateTime(currentDate).ToString("MM/dd/yyyy") + "\" /> </td>");
                    builder.AppendLine("<script type=\"text/javascript\">setDatePicker(\"txtStartDate2\");</script>");
                    builder.AppendLine("</tr>");

                    builder.AppendLine("<tr><td colspan=\"4\"><span style=\"float:right;\">");
                    builder.AppendLine("    <button title=\"Save changes\" class=\"sbkMySobek_RoundButton\" onclick=\"save_workflow('" + current_workflow_id + "','" + itemID + "'); return false;\">SAVE</button>");
                    builder.AppendLine("    <button title=\"Delete this workflow\" class=\"sbkMySobek_RoundButton\" onclick=\"delete_workflow(); return false;\">DELETE</button>");
                    builder.AppendLine("</span></td></tr>");

                    //End this table
                    builder.AppendLine("</table>");

                    builder.AppendLine("</span>");
                }

            }
            //Add the current History table
            if (current_workflows_no_durations != null && current_workflows_no_durations.Count > 0)
            {
                builder.AppendLine("<span class=\"sbkTi_HomeText\"><h2>Current Work History</h2></span>");
                builder.AppendLine("<table id=\"sbkTi_tblCurrentTracking2\" class=\"sbkSaav_Table\">");
                builder.AppendLine("<tr><th>Item</th><th>Workflow</th><th>Date</th><th>User</th><th>Equipment</th></tr>");
                foreach (KeyValuePair<string, Tracking_Workflow> thisPair in current_workflows_no_durations)
                {
                    Tracking_Workflow this_workflow = thisPair.Value;
                    string workflow_text = String.Empty;
                    if (this_workflow.Workflow_type == 1 || this_workflow.Workflow_type == 2)
                        workflow_text = "Scanning";
                    else
                    {
                        workflow_text = "Processing";
                    }
                    builder.AppendLine("<tr>");
                    builder.AppendLine("<td title=\"" + this_workflow.Title + "\">" + this_workflow.BibID + ":" + this_workflow.VID + "</td>");
                    builder.AppendLine("<td>" + workflow_text + "</td>");
                    builder.AppendLine("<td>" + Convert.ToDateTime(this_workflow.Date).ToString("MM/dd/yyyy") + "</td>");
                    //           builder.AppendLine("<td>" + (String.IsNullOrEmpty(this_workflow.EndTime) ? "-" : Convert.ToDateTime(this_workflow.EndTime).ToString("hh:mm tt")) + "</td>");
                    builder.AppendLine("<td>" + this_workflow.thisUser.UserName + "</td>");
                    builder.AppendLine("<td>" + this_workflow.Equipment + "</td>");
                    builder.AppendLine("</tr>");
                }

                builder.AppendLine("</table>");

            }

            //Add the Save and Done buttons
            builder.AppendLine("<div id=\"divButtons\" style=\"float:right;\">");
            //         builder.AppendLine("    <button title=\"Save changes\" class=\"sbkMySobek_RoundButton\" onclick=\"save(); return false;\">SAVE</button>");
            builder.AppendLine("    <button title=\"Save all changes and exit\" class=\"sbkMySobek_RoundButton\" onclick=\"save(); return false;\">DONE</button>");
            builder.AppendLine("</div>");
            builder.AppendLine("<br/><br/>");

            //         }

            //Close the inner tab div
            builder.AppendLine("</div>");

            //builder.AppendLine("</div>");

            #endregion

            //#region Third tab - Editing previous entries
            //builder.AppendLine("            <div class=\"tabpage\" id=\"tabpage_3\">");
            //if (previous_workflows_this_user.Rows.Count > 0)
            //{
            //    //Start the table for displaying the workflow entries
            //    builder.AppendLine("<table id=\"sbkTracking_Workflows_Table\">");

            //    //Add the table headers
            //    builder.AppendLine("<tr><th>Workflow name</th>");
            //    builder.AppendLine("<th>Date</th>");
            //    builder.AppendLine("<th>Start time</th>");
            //    builder.AppendLine("<th>End Time</th>");
            //    builder.AppendLine("<th>Equipment</th>");
            //    builder.AppendLine("<th>User</th></tr>");

            //    foreach (DataRow thisRow in previous_workflows_this_user.Rows)
            //    {
            //        builder.AppendLine("<tr>");
            //      //  builder.AppendLine("<td>" + thisRow["ItemID"] + "</td>");
            //        builder.AppendLine("<td>" + thisRow["WorkFlowName"] + "</td>");
            //        builder.AppendLine("<td>" + thisRow["DateStarted"] + "</td>");
            //        builder.AppendLine("<td>" + thisRow["DateCompleted"] + "</td>");
            //        builder.AppendLine("<td>" + thisRow["RelatedEquipment"] + "</td>");
            //        builder.AppendLine("<td>" + thisRow["WorkPerformedBy"] + "</td>");
            //        builder.AppendLine("</tr>");
            //    }
            //    builder.AppendLine("</table>");

            //}
            //builder.AppendLine("</div>");
            //#endregion

            builder.AppendLine("</div>");

            //Close the outer tab container
            builder.AppendLine("</div>");

            builder.AppendLine("</div>");

            //Close the main div
            builder.AppendLine("</div>");

            builder.AppendLine("<br/><br/>");

            //Add the script to get the tab functionality
            builder.AppendLine("  <script>");
            builder.AppendLine("    $(document).ready(function(){");
            builder.AppendLine("      $(\"#tabContainer\").acidTabs();");
            builder.AppendLine("    });");
            builder.AppendLine("  </script>");

            LiteralControl control1 = new LiteralControl(builder.ToString());

            MainPlaceHolder.Controls.Add(control1);
        }
        /// <summary> Save or Update Workflow when a 'Save' button for a tracking entry is clicked </summary>
        /// <param name="thisWorkflowId"></param>
        /// <param name="thisItemID"></param>
        private void Add_or_Update_Workflow(int thisWorkflowId, int thisItemID)
        {
            string thisWorkflowId_string = thisWorkflowId.ToString();
            string new_start_time = "";
            string new_end_time = "";
            DateTime new_date;
            int selected_ddl_workflow;
            int this_event = -1;
            int start_event_num = -1;
            int end_event_num = -1;
            int start_end_event_num = -1;

            if (thisWorkflowId == -1)
            {
                thisWorkflowId_string = "-1";
            }
            if (page == 1)
            {
                //    DateTime.TryParse(HttpContext.Current.Request.Form["txtStartDate" + thisWorkflowId_string], out new_date);
                new_date = Convert.ToDateTime(HttpContext.Current.Request.Form["txtStartDate" + thisWorkflowId_string]);
                selected_ddl_workflow = Convert.ToInt32(HttpContext.Current.Request.Form["ddlEvent" + thisWorkflowId_string]);
            }
            else
            {
                //       DateTime.TryParse(HttpContext.Current.Request.Form["txtStartDate2"], out new_date);
                new_date = Convert.ToDateTime(HttpContext.Current.Request.Form["txtStartDate2"]);
                selected_ddl_workflow = Convert.ToInt32(HttpContext.Current.Request.Form["ddlEvent2"]);
            }

            if (page == 1)
            {
                new_start_time = String.IsNullOrEmpty(HttpContext.Current.Request.Form["txtStartTime" + thisWorkflowId_string]) ? "" : Convert.ToDateTime(HttpContext.Current.Request.Form["txtStartTime" + thisWorkflowId_string]).ToString("hh:mm tt");
                new_end_time = String.IsNullOrEmpty(HttpContext.Current.Request.Form["txtEndTime" + thisWorkflowId_string]) ? "" : Convert.ToDateTime(HttpContext.Current.Request.Form["txtEndTime" + thisWorkflowId_string]).ToString("hh:mm tt");
                if (selected_ddl_workflow == 1)
                {
                    this_event = 1;
                    start_event_num = 1;
                    if (!String.IsNullOrEmpty(new_end_time))
                    {
                        end_event_num = 2;
                        this_event = 2;
                    }
                }
                else
                {
                    this_event = 3;
                    start_event_num = 3;
                    if (!String.IsNullOrEmpty(new_end_time))
                    {
                        end_event_num = 4;
                        this_event = 4;
                    }
                }
            }

                 //If this is from the second tab, set the appropriate start_end_event number
            else
            {
                new_start_time = new_date.ToString("hh:mm tt");
                new_end_time = new_date.ToString("hh:mm tt");
                if (selected_ddl_workflow == 1)
                {
                    this_event = 2;
                    start_end_event_num = 2;
                }
                else
                {
                    this_event = 4;
                    start_end_event_num = 4;
                }
            }

            //Create a new workflow object for this workflow
            Tracking_Workflow this_workflow = new Tracking_Workflow();
            SqlDateTime start_time_to_save = DateTime.Parse(new_date.ToShortDateString() + " " + new_start_time);
            SqlDateTime end_time_to_save = String.IsNullOrEmpty(new_end_time) ? SqlDateTime.Null : DateTime.Parse(new_date.ToShortDateString() + " " + new_end_time);
            if (page == 2)
                end_time_to_save = start_time_to_save;

            this_workflow.BibID = BibID;
            this_workflow.VID = VID;
            if (selected_ddl_workflow == 1)
                this_workflow.Equipment = equipment;
            else
            {
                this_workflow.Equipment = "";
            }
            this_workflow.Date = new_date.ToString("yyyy-MM-dd");
            this_workflow.Saved = true;
            this_workflow.Title = title;
            this_workflow.StartTime = Convert.ToDateTime(new_start_time).ToString("hh:mm tt");
            if (page == 1)
            {
                this_workflow.EndTime = String.IsNullOrEmpty(new_end_time) ? DateTime.MinValue.ToShortTimeString() : Convert.ToDateTime(new_end_time).ToString("hh:mm tt");
            }
            else
            {
                this_workflow.EndTime = this_workflow.StartTime;
            }
            this_workflow.Workflow_type = this_event;
            this_workflow.thisUser = current_selected_user;

            //Fetch the workflow dictionaries from the session if present
            current_workflows = (HttpContext.Current.Session["Tracking_Current_Workflows"]) as Dictionary<string, Tracking_Workflow>;
            current_workflows_no_durations = (HttpContext.Current.Session["Tracking_Current_Workflows_No_Duration"]) as Dictionary<string, Tracking_Workflow>;

            //else create new ones
            if (current_workflows == null && page == 1)
            {
                current_workflows = new Dictionary<string, Tracking_Workflow>();
            }
            else if (current_workflows_no_durations == null && page == 2)
            {
                current_workflows_no_durations = new Dictionary<string, Tracking_Workflow>();
            }

            //If this has not been previously saved, create a new entry in the DB
            if (thisWorkflowId == -1)
            {
                //If this is a 'Start' event, use the event number, since there can be at max one event start  per item per user per day
                //else use the unique Guid. This ensures there can be any number of 'close' events for an item per user per day
                string guidObj = Guid.NewGuid().ToString();

                string stage_from_event = (page == 1) ? ((this_event == 1 || this_event == 3) ? this_event.ToString() : guidObj) : guidObj;

                //    string key = page + thisItemID + this_workflow.Date+stage_from_event + current_selected_user.UserName;
                string key = current_workflow_id.ToString();

                if (page == 1)
                {
                    //Save this to the DB
                    this_workflow.WorkflowID = Database.SobekCM_Database.Tracking_Save_New_Workflow(thisItemID, current_selected_user.UserName, equipment, start_time_to_save, end_time_to_save, this_event, start_event_num, end_event_num, start_end_event_num);

                    current_workflow_id = this_workflow.WorkflowID;
                    key = current_workflow_id.ToString();

                    if (current_workflows.ContainsKey("-1"))
                        current_workflows.Remove("-1");

                    //Add this to the dictionary
                    current_workflows.Add(key, this_workflow);
                }

                else if (page == 2)
                {

                    //Save this workflow to the database
                    this_workflow.WorkflowID = Database.SobekCM_Database.Tracking_Save_New_Workflow(thisItemID, current_selected_user.UserName, equipment, start_time_to_save, end_time_to_save, stage, start_event_num, end_event_num, start_end_event_num);

                    current_workflow_id = this_workflow.WorkflowID;
                    key = current_workflow_id.ToString();

                    if (current_workflows_no_durations.ContainsKey("-1"))
                        current_workflows_no_durations.Remove("-1");

                    //Add this to the dictionary
                    current_workflows_no_durations.Add(key, this_workflow);

                }

            }
            //Else update this workflow in the database
            else
            {
                string guidObj = Guid.NewGuid().ToString();
                string stage_from_event = (page == 1) ? ((this_event == 1 || this_event == 3) ? this_event.ToString() : guidObj) : guidObj;
                //                            string stage_from_event = (this_event == 1 || this_event == 3) ? this_event.ToString() : thisWorkflowId.ToString();

                //string key = page + thisItemID + this_workflow.Date + stage_from_event + current_selected_user.UserName;
                string key = thisWorkflowId.ToString();
                current_workflow_id = thisWorkflowId;

                Database.SobekCM_Database.Tracking_Update_Workflow(thisWorkflowId, thisItemID, current_selected_user.UserName, start_time_to_save, end_time_to_save, equipment, this_event, start_event_num, end_event_num);

                if (page == 1)
                {
                    if (this_event == 2 || this_event == 4)
                    {
                        string opened_key_string = this_event == 2 ? "1" : "3";
                        //         string opened_key = page + thisItemID + this_workflow.Date + opened_key_string + current_selected_user.UserName;
                        string opened_key = key;

                        //Remove the old value from the dictionary if present
                        if (current_workflows.ContainsKey(opened_key))
                            current_workflows.Remove(opened_key);
                    }

                    if (current_workflows.ContainsKey(key))
                        current_workflows.Remove(key);
                    //Add this to the dictionary
                    current_workflows.Add(key, this_workflow);
                }

                else if (page == 2)
                {

                    //Remove the old value from the dictionary if present
                    if (current_workflows_no_durations.ContainsKey(key))
                        current_workflows_no_durations.Remove(key);

                    //Add this to the dictionary
                    current_workflows_no_durations.Add(key, this_workflow);

                }

            }

            //Save the dictionaries back to the session
            HttpContext.Current.Session["Tracking_Current_Workflows"] = current_workflows;
            HttpContext.Current.Session["Tracking_Current_Workflows_No_Duration"] = current_workflows_no_durations;
        }