protected void btnSendToRoute_Click(object sender, EventArgs e)
        {
            int fullcounter = 0;

            //move the job card routes into the meter reading routes tables and then redirect to Management console
            //first we need to check if all the instructions were filled in
            foreach (GridDataItem row in gvSearchResults.Items)
            {
                CheckBox chk = (CheckBox)row["MarkCHK"].Controls[0];
                //CheckBox tempbox = (CheckBox)row.FindControl("CheckBox1");
                Classes.IDBHandler             db   = new Classes.DBHandler();
                Common.ReadingsDetailDBHandler rddb = new Common.ReadingsDetailDBHandler();

                if (chk.Checked)
                {
                    //string statusdescr = gvSearchResults.SelectedItems[row.RowIndex].OwnerTableView.DataKeyValues[3].ToString();
                    int jobHeaderID = Convert.ToInt32(row.GetDataKeyValue("JobCardHeaderID").ToString());

                    //check instructions
                    int counter = 0;
                    counter = rddb.CheckInstructions(jobHeaderID);
                    if (counter == 0)
                    {
                        //all instructions have been inserted. move the routes.
                        Boolean correct = rddb.MoveJobCardsToRoutes(jobHeaderID);

                        //change contractID
                        int ContractID = rddb.GetWorkOrderContract();
                        Users_UserDetail                   uud   = (Users_UserDetail)Session["userDetails"];
                        Common.ICommonDBHandler            cdb   = new Common.CommonDBHandler();
                        UserContract                       uclst = cdb.GetUserContractList(uud).Where(x => x.EnableContract == true && x.ClientContract.ContractActive == true && x.ContractID == ContractID).FirstOrDefault();
                        Common.CommonTasks.UserContractDet ucd   = cdb.LoadUserContractDetByUserContractID(uclst.UserContractID);
                        Session["UserContractDet"] = ucd;

                        //Redirect to Management Console
                        Response.Redirect("RouteManagementConsole.aspx");
                    }
                    else
                    {
                        //there are missing instructions - dont allow.
                        lblError.Text    = "Cannot transfer routes, please ensure you have added instructions to each meter.";
                        lblError.Visible = true;
                    }
                    fullcounter++;
                }
            }

            if (fullcounter > 0)
            {
                gvSearchResults.Rebind();
            }
            else
            {
                lblError.Text      = "Please select at least one item to send.";
                lblError.ForeColor = System.Drawing.Color.Red;
                lblError.Visible   = true;
            }

            //Response.Redirect("RouteManagementConsole.aspx");
        }
        protected void btnAddSameInstruction_Click(object sender, EventArgs e)
        {
            string listIDs = "";
            int    counter = 0;

            foreach (GridDataItem row in gvSearchResults.Items)
            {
                CheckBox chk = (CheckBox)row["MarkCHK"].Controls[0];
                //CheckBox tempbox = (CheckBox)row.FindControl("CheckBox1");
                Classes.IDBHandler             db   = new Classes.DBHandler();
                Common.ReadingsDetailDBHandler rddb = new Common.ReadingsDetailDBHandler();

                if (chk.Checked)
                {
                    //string statusdescr = gvSearchResults.SelectedItems[row.RowIndex].OwnerTableView.DataKeyValues[3].ToString();
                    int jobHeaderID = Convert.ToInt32(row.GetDataKeyValue("JobCardHeaderID").ToString());

                    if (counter == 0)
                    {
                        listIDs = jobHeaderID.ToString();
                    }
                    else
                    {
                        listIDs = listIDs + "," + jobHeaderID.ToString();
                    }
                    counter++;
                }
            }
            if (Session["jobIDs"] == null)
            {
                Session.Add("jobIDs", listIDs);
            }
            else
            {
                if (Session["jobIDs"].ToString() != listIDs)
                {
                    Session["jobIDs"]    = listIDs;
                    Session["openPopUp"] = "n";
                }
                else
                {
                    Session["jobIDs"]    = listIDs;
                    Session["openPopUp"] = "y";
                }
            }

            if (counter > 0)//Session["openPopUp"] = "n";
            {
                if ((Session["openPopUp"] == "n") || (Session["openPopUp"] == null))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "setInstruction", "<script>window.open('SetComment.aspx?all=y&JobCardHeaderID=" + listIDs + "', 'setInstruction', 'toolbar=0,location=0,status=1,menubar=0,scrollbars=1,top=50,left=50,width=550,height=270,resizable=1');</script>");
                }
            }
            else
            {
                lblError.Visible = true;
                lblError.Text    = "Please select at least one route to edit the instructions of.";
            }
        }
        protected void btnDeleteRoutes_Click(object sender, EventArgs e)
        {
            int fullcounter = 0;

            //move the job card routes into the meter reading routes tables and then redirect to Management console
            //first we need to check if all the instructions were filled in
            foreach (GridDataItem row in gvSearchResults.Items)
            {
                CheckBox chk = (CheckBox)row["MarkCHK"].Controls[0];
                //CheckBox tempbox = (CheckBox)row.FindControl("CheckBox1");
                Classes.IDBHandler             db   = new Classes.DBHandler();
                Common.ReadingsDetailDBHandler rddb = new Common.ReadingsDetailDBHandler();

                if (chk.Checked)
                {
                    //string statusdescr = gvSearchResults.SelectedItems[row.RowIndex].OwnerTableView.DataKeyValues[3].ToString();
                    int jobHeaderID = Convert.ToInt32(row.GetDataKeyValue("JobCardHeaderID").ToString());

                    if (rddb.DeleteJobCards(jobHeaderID))
                    {
                        lblError.Text      = "Route(s) successfully deleted.";
                        lblError.ForeColor = System.Drawing.Color.Black;
                        lblError.Visible   = true;
                    }
                    else
                    {
                        lblError.Text      = "A problem has occured.  Please try again.";
                        lblError.ForeColor = System.Drawing.Color.Red;
                        lblError.Visible   = true;
                    }
                    fullcounter++;
                }
            }

            if (fullcounter > 0)
            {
                gvSearchResults.Rebind();
            }
            else
            {
                lblError.Text      = "Please select at least one item to delete.";
                lblError.ForeColor = System.Drawing.Color.Red;
                lblError.Visible   = true;
            }
        }
        protected void btnAddItems_Click(object sender, EventArgs e)
        {
            Common.ReadingsDetailDBHandler rddb = new Common.ReadingsDetailDBHandler();
            int    counter         = 0;
            int    jobCardHeaderID = 0;
            string additionalParms = "";

            //check if hidden field has value, if not you need to first create the header record
            if (hidJobCardID.Value == "")
            {
                jobCardHeaderID    = rddb.InsertJobCardHeader(0);
                hidJobCardID.Value = jobCardHeaderID.ToString();
                additionalParms    = "&create=y";
            }
            else
            {
                jobCardHeaderID = Convert.ToInt32(hidJobCardID.Value);
            }
            //loop through gridview to find all marked for repairs
            foreach (GridDataItem row in gvAddingItems.Items)
            {
                if (row.ItemType == GridItemType.Item)
                {
                    CheckBox chkRow = (row.Cells[1].FindControl("chkMarkRepairs") as CheckBox);
                    if (chkRow.Checked)
                    {
                        //update mrd table with repairs
                        int mrdID = Convert.ToInt32(row.Cells[0].Text);

                        bool updated = rddb.UpdateMarkRepairs(mrdID, jobCardHeaderID);

                        //insert job card route
                        counter = counter + 1;
                    }
                }
            }
            gvSearchResults.Rebind();
            AddItem.Attributes["style"] = "display: none";
            Response.Redirect("ViewWorkOrders.aspx?new=y&JobCardHeaderID=" + jobCardHeaderID.ToString() + additionalParms);
        }