private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                OrgId = _functions.GetUserOrgId(HttpContext.Current.User.Identity.Name, false);
                if (Request.QueryString["repairid"] != null && !string.IsNullOrEmpty(Request.QueryString["repairid"]))
                {
                    RepairId = Convert.ToInt32(Request.QueryString["repairid"]);
                }
                if (Request.QueryString["mode"] == null || Request.QueryString["id"] == null || Request.QueryString["repairid"] == null)
                {
                    Session["lastpage"] = "main.aspx";
                    Session["error"]    = _functions.ErrorMessage(104);
                    Response.Redirect("error.aspx", false);
                    return;
                }
                try
                {
                    if (Request.QueryString["mode"] == "Add")
                    {
                        Mode = true;
                    }
                    else
                    {
                        Mode = false;
                    }
                    OrderId = Convert.ToInt32(Request.QueryString["id"]);

                    if (Request.QueryString["inspectid"] != null)
                    {
                        InspectId = Convert.ToInt32(Request.QueryString["inspectid"]);
                    }
                    else
                    {
                        InspectId = 0;
                    }
                    if (Request.QueryString["itemid"] != null)
                    {
                        ItemId = Convert.ToInt32(Request.QueryString["itemid"]);
                    }
                    else
                    {
                        ItemId = 0;
                    }
                    if (Request.QueryString["type"] != null)
                    {
                        sType = Request.QueryString["type"]; //RI, PMI, II, FREE
                    }
                    else
                    {
                        sType = "";
                    }
                    if (Request.QueryString["op"] != null)
                    {
                        ServiceResult = Request.QueryString["op"]; //Repair, Replace
                    }
                    else
                    {
                        ServiceResult = "";
                    }
                    sCurrentPage = "wo_updateRepair.aspx?mode=" + (Mode ? "Add" : "Edit") + "&id=" + OrderId.ToString() + "&repairid=" + RepairId.ToString() + "&inspectid=" + InspectId.ToString() + "&itemid=" + ItemId.ToString() + "&type=" + sType + "&op=" + ServiceResult;
                }
                catch (FormatException fex)
                {
                    Session["lastpage"] = "main.aspx";
                    Session["error"]    = _functions.ErrorMessage(105);
                    Response.Redirect("error.aspx", false);
                    return;
                }
                // getting the Bread Crumbs from Cookie and add last item
                if (Request.Cookies.Count > 0 && Request.Cookies["bfp_navigate"] != null)
                {
                    Header.BrdCrumbsSerialization = Request.Cookies["bfp_navigate"].Value;
                }
                Header.AddBreadCrumb("Work Order Detail", "/wo_viewWorkOrder.aspx?id=" + OrderId.ToString() + "&#Repairs");

                if (sType == "II") // if came from Inspetion screen
                {
                    Header.AddBreadCrumb("Inspection Detail", "/wo_viewCompleteInspection.aspx?id=" + OrderId.ToString() + "&inspectid=" + InspectId.ToString());
                    sLastPage = "wo_viewCompleteInspection.aspx?id=" + OrderId.ToString() + "&inspectid=" + InspectId.ToString();
                }
                else
                {
                    sLastPage = "wo_viewWorkOrder.aspx?id=" + OrderId.ToString() + "&#Repairs";
                }

                if (Mode)
                {
                    this.PageTitle = "Update Repair";
                }
                else if (sType == "FREE")
                {
                    this.PageTitle = "Add Repair";
                }
                else
                {
                    this.PageTitle = "Edit Repair";
                }
                Header.AddJavaScriptFile("/wo_updateRepair.js");
                Header.BodyOnloadScript = "javascript:CheckEditMode();";

                SaveCancelControl.CausesValidation = false;
                SaveCancelControl.ButtonText       = " Save & Back ";
                SaveCancelControl.IsCancel         = false;

                ddlRepairCats.Attributes.Add("onchange", "javascript:ValidRepairItems();");
                ddlRepairItems.Attributes.Add("onchange", "javascript:ValidRepairItems();");

                clsWorkOrders orders = new clsWorkOrders();
                string        key    = orders.GetIntegrationKey(this.OrgId);

                clsUsers user = new clsUsers();
                user.cAction = "S";
                user.iOrgId  = OrgId;
                user.iId     = _functions.GetUserOrgId(Context.User.Identity.Name, true);
                user.UserDetails();

                if (RepairId == 0)
                {
                    preRepairId = orders.GetRepairPreId();
                    preRepairId++;
                }

                WarehousePanel.Visible = false;

                //if (string.IsNullOrEmpty(key))
                //    WarehousePanel.Visible = false;
                //else
                //{
                //    string url = String.Format(_functions.GetValueFromConfig("WarehouseListnerPage") + "?action=create&key={0}&app=fleet&roid={1}&riid={2}&rufn={3}&ruln={4}&rue={5}&ron={6}&rofn={7}&shipto={8}&shiploc={9}", key, RepairId > 0 ? RepairId.ToString() : preRepairId.ToString(), string.Empty, user.sFirstName.Value, user.sLastName.Value, user.sEmail.Value, "Work Order", OrderId.ToString(), user.sFirstName.Value + " " + user.sLastName.Value, string.Empty);
                //    string windowOpenScript = "function openWindow() { window.open('" + url + "','Warehouse','height=600,width=900,toolbar=0,status=1,location=0,menubar=0,scrollbars=1,resizable=1'); }";

                //    if (!Page.ClientScript.IsClientScriptBlockRegistered("openWindow"))
                //        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "openWindow", windowOpenScript, true);
                //}

                if (!IsPostBack)
                {
                    order           = new clsWorkOrders();
                    order.cAction   = "S";
                    order.iOrgId    = OrgId;
                    order.iId       = OrderId;
                    order.iRepairId = RepairId;

                    dsRepairItemsCats = order.GetRepairItemsAndCats();

                    dsRepairItemsCats.Relations.Add(new DataRelation("Cats_Items", dsRepairItemsCats.Tables["Table"].Columns["Id"], dsRepairItemsCats.Tables["Table1"].Columns["CatId"]));

                    ddlRepairCats.DataSource = dsRepairItemsCats;
                    ddlRepairCats.DataBind();

                    ddlRepairItems.DataSource = dsRepairItemsCats;
                    ddlRepairItems.DataBind();

                    ListLink.DataBind();

                    if (Mode) // Add Mode
                    {
                        order.sTypeService = sType;
                        order.iItemId      = ItemId;

                        if (sType == "II")
                        {
                            lblTableTitle.Text = "Please check the following to update any reported issues associated with this Inspection item";
                            DataTable dtReportedIssues = order.GetReportedIssuesListUpdate();
                            if (dtReportedIssues.Rows.Count > 0)
                            {
                                repReportedIssues.DataSource = new DataView(dtReportedIssues);
                                repReportedIssues.DataBind();
                            }
                            else
                            {
                                lblTableTitle.Text        = "Serviced Repairs";
                                repReportedIssues.Visible = false;
                            }
                        }
                        else
                        {
                            lblTableTitle.Text        = "Serviced Repairs";
                            repReportedIssues.Visible = false;
                        }

                        // showing all repaires with associations
                        repRepairs.DataSource = new DataView(order.GetRepairsListUpdate());
                        repRepairs.DataBind();

                        order.GetIssueInfo();

                        lblSourceType.Text     = order.sTypeService.Value;
                        lblRepairCategory.Text = order.sRepairCatName.Value;
                        lblIssue.Text          = order.sServiceName.Value;
                    }
                    else // Edit Mode
                    {
                        // hidding the controls for Edit mode
                        tblSource.Visible            = false;
                        lblTableTitle.Text           = "Repair";
                        repRepairs.Visible           = false;
                        html_chNewRepair.Visible     = false;
                        lblTableFooter.Visible       = false;
                        RepairLogsLinksPanel.Visible = false;

                        // getting repair's info
                        order.RepairCommonDetails();
                        ddlRepairCats.SelectedValue  = order.iRepairCatId.Value.ToString();
                        ddlRepairItems.SelectedValue = order.iRepairItemId.Value.ToString();
                        tbNotes.Text = order.sRepairDesc.Value;
                    }
                    dtParts = order.GetPartsUsedList();
                    ViewState["PartsUsed"] = dtParts;
                    dwParts             = new DataView(dtParts);
                    dwParts.RowFilter   = "IsDeleted=0";
                    repParts.DataSource = dwParts;
                    repParts.DataBind();
                }
            }
            catch (Exception ex)
            {
                _functions.Log(ex, HttpContext.Current.User.Identity.Name, SourcePageName);
                Session["lastpage"]     = sLastPage;
                Session["error"]        = ex.Message;
                Session["error_report"] = ex.ToString();
                Response.Redirect("error.aspx", false);
            }
            finally
            {
                if (order != null)
                {
                    order.Dispose();
                }
            }
        }