﻿ // 11/30/2017 - code removed and replace by simple data binding to fields returned in view InvRecord
 // From form1 , method populate_invscreen, the below code populated the checkboxes of in facility, under power, etc based on the string held in field OtherInfo
 
 //// first uncheck all the boxes in the OtherInfo panel
            //foreach (Control c in this.invOtherPanel.Controls)
            //{
            //    if (c is CheckBox)
            //    {
            //        CheckBox b = new CheckBox();
            //        b = (CheckBox)c;
            //        b.Checked = false;
            //    }
            //}

            //string temp = dr.OtherInfo.Trim();
            //while (temp.Length > 5)
            //{
            //    int npos = 0;
            //    npos = temp.IndexOf(';');
            //    if (npos > 0)
            //    {
            //        string s1 = temp.Substring(0, npos);
            //        foreach (Control c in this.invOtherPanel.Controls)
            //        {
            //            if (c is CheckBox)
            //            {
            //                CheckBox b = new CheckBox();
            //                b = (CheckBox)c;
            //                if (b.Text.ToUpper().Trim() == s1.ToUpper().Trim())
            //                {
            //                    b.Checked = true;
            //                }

            //            }
            //        }
            //        temp = temp.Substring(npos + 1);
            //    }
            //    else
            //    {
            //        break;
            //    }
            //}

			// 12/6/2017 - shipping data entry code that was in form1.cs now in frmShip
			 private void tpShipping_Enter(object sender, EventArgs e)
        {
            if (!_shAddMode)
            {
                if (_currentSHKey == -1)
                {
                    this.SH_LoadLast();
                }
                else
                {
                    this.SH_Loadid(_currentSHKey);
                }
            }
        }

        private void SH_LoadLast()
        {
            dtSH.Clear();
            int nrows = this.taSH.FillLoadLast(dtSH);

            if (nrows > 0)
            {
                SH_populate();
            }
        }

        private void SH_Loadid(int idToLoad)
        {
            dtSH.Clear();
            int nrows = taSH.FillById(dtSH, idToLoad);

            if (nrows > 0)
            {
                SH_populate();
            }
        }

        private void SH_LoadInbno(string inbnoToLoad)
        {
            dtSH.Clear();
            int nrows = taSH.FillByShipNo(dtSH, inbnoToLoad);

            if (nrows > 0)
            {
                SH_populate();
                _currentSHKey = dtSH[0].id;
            }
        }

        private void SH_populate()
        {
            _shAddMode = false;
            shNewbtn.Enabled = true;
            shPrintPoBtn.Enabled = true;

            shShipNo.Text = dtSH[0].INBNO.Trim();

            // rigger fields populated
            shRiggertb.Text = dtSH[0].RiggerName.Trim();
            _shOrigRig = dtSH[0].RiggerName.Trim();    // original rigger on shipping record

            shRigAmounttb.Text = dtSH[0].RG_AMT.ToString("c2");
            shRigLastmtb.Text = objGen.formatDateAsMMDDYYYY(dtSH[0].RG_CNTCTDT);
            shContainertb.Text = dtSH[0].CONTAINER.Trim();
            shriggerkeylb.Text = dtSH[0].rigcstno;

            // trucker controls populated
            shTruckertb.Text = dtSH[0].TruckerName.Trim();
            _shOrigTrk = dtSH[0].TruckerName.Trim();
            shTrkAmounttb.Text = dtSH[0].TRK_AMT.ToString("c2");
            shTruckLastmtb.Text = objGen.formatDateAsMMDDYYYY(dtSH[0].TR_CNTCTDT);
            shtruckerkeylb.Text = dtSH[0].trkcstno;

            shTruckertb2.Text = dtSH[0].TruckerName2.Trim();
            _shOrigTrk2 = dtSH[0].TruckerName2.Trim();
            shTrkAmounttb2.Text = dtSH[0].trk_amt2.ToString("c2");
            shTruckLastmtb2.Text = objGen.formatDateAsMMDDYYYY(dtSH[0].tr_cntctdt2);
            shtruckerkeylb2.Text = dtSH[0].trkcstno2;

            shTruckertb3.Text = dtSH[0].TruckerName3.Trim();
            _shOrigTrk3 = dtSH[0].TruckerName3.Trim();
            shTrkAmounttb3.Text = dtSH[0].trk_amt3.ToString("c2");
            shTruckLastmtb3.Text = objGen.formatDateAsMMDDYYYY(dtSH[0].tr_cntctdt3);
            shtruckerkeylb3.Text = dtSH[0].trkcstno3;

            shShipTypecb.Text = "Inbound";
            if (dtSH[0].SHPTYPE == "O")
                shShipTypecb.Text = "Outbound";

            if (dtSH[0].SHPTYPE == "T")
                shShipTypecb.Text = "Transfer";

            //populate data grid of items shipped
            this.dtSHI.Clear();
            int nrows = this.taSHI.FillByInbno(this.dtSHI, dtSH[0].INBNO);
            shipItemSelectByShipNoBindingSource.DataSource = this.dtSHI;

            this.shRigTotalLb.Text = this.shCalcRigTotal().ToString("c2");
            this.shTruckTotalLb.Text = this.shCalcTruckTotal().ToString("c2");
            this.shTruckTotalLb2.Text = this.shCalcTruckTotal2().ToString("c2");
            this.shTruckTotalLb3.Text = this.shCalcTruckTotal3().ToString("c2");

            //Notes section
            shComments.Text = dtSH[0].COMMENTS.Trim();
        }

        private float shCalcRigTotal()
        {
            //calc total of items on grid
            float totalAmt = 0;
            for (int i = 0; i < this.shdgv.Rows.Count; i++)
            {
                if (shdgv.Rows[i].Cells["shdgvrigAmount"].Value != System.DBNull.Value &&
                    shdgv.Rows[i].Cells["shdgvrigAmount"].Value != null)
                {
                    totalAmt += Convert.ToSingle(this.shdgv.Rows[i].Cells["shdgvrigAmount"].Value);
                }
            }
            return totalAmt;
        }

        private float shCalcTruckTotal()
        {
            //calc total of items on grid
            float totalAmt = 0;
            for (int i = 0; i < this.shdgv.Rows.Count; i++)
            {
                if (shdgv.Rows[i].Cells["shdgvtrkAmount"].Value != System.DBNull.Value &&
                    shdgv.Rows[i].Cells["shdgvtrkAmount"].Value != null)
                {
                    totalAmt += Convert.ToSingle(this.shdgv.Rows[i].Cells["shdgvtrkAmount"].Value);
                }
            }
            return totalAmt;
        }

        private float shCalcTruckTotal2()
        {
            //calc total of items on grid
            float totalAmt = 0;
            for (int i = 0; i < this.shdgv.Rows.Count; i++)
            {
                if (shdgv.Rows[i].Cells["shdgvtrkAmount2"].Value != System.DBNull.Value &&
                    shdgv.Rows[i].Cells["shdgvtrkAmount2"].Value != null)
                {
                    totalAmt += Convert.ToSingle(this.shdgv.Rows[i].Cells["shdgvtrkAmount2"].Value);
                }
            }
            return totalAmt;
        }

        private float shCalcTruckTotal3()
        {
            //calc total of items on grid
            float totalAmt = 0;
            for (int i = 0; i < this.shdgv.Rows.Count; i++)
            {
                if (shdgv.Rows[i].Cells["shdgvtrkAmount3"].Value != System.DBNull.Value &&
                    shdgv.Rows[i].Cells["shdgvtrkAmount3"].Value != null)
                {
                    totalAmt += Convert.ToSingle(this.shdgv.Rows[i].Cells["shdgvtrkAmount3"].Value);
                }
            }
            return totalAmt;
        }

        private void shCancelbtn_Click(object sender, EventArgs e)
        {
            DialogResult result;
            result = MessageBox.Show("Cancel ?", "Cancel", MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                _shAddMode = false;
                this.tpShipping_Enter(sender, e);
            }
        }

        private void shNewbtn_Click(object sender, EventArgs e)
        {
            _currentSHKey = -1;
            this.shNewbtn.Enabled = false;
            this.shPrintPoBtn.Enabled = false;

            _shAddMode = true;

            shtruckerkeylb.Text = "";
            shriggerkeylb.Text = "";

            shShipNo.Text = "Determined after Saving";
            shRiggertb.Text = "";
            shRigLastmtb.Text = objGen.formatDateAsMMDDYYYY(DateTime.Today);
            shRigAmounttb.Text = "0";
            shContainertb.Text = "";

            shTruckertb.Text = "";
            shTruckLastmtb.Text = objGen.formatDateAsMMDDYYYY(DateTime.Today);
            shTrkAmounttb.Text = "0";
            shShipTypecb.Text = "Inbound";

            this.dtSHI.Clear();
            shTruckTotalLb.Text = "0";
            shRigTotalLb.Text = "0";

            shComments.Text = "";

            shTruckertb.Focus();

        }

        private void shRiggertb_Leave(object sender, EventArgs e)
        {
            if (shRiggertb.Text.Trim().Length == 0)
            {
                shriggerkeylb.Text = "";
                this.LostFocusBackColor(sender, e);
                return;
            }

            if (shRiggertb.Text.Trim() != _shOrigRig)
            {
                // if no match on rigger found then perform lookup screen
                this.shRiggerLookUpbtn_Click(sender, e);
            }
            this.LostFocusBackColor(sender, e);
        }

        private void shRiggerLookUpbtn_Click(object sender, EventArgs e)
        {
            Form1.sFormPass = "";
            Form1.sFormPass2 = "";

            frmCustomerSelection frm = new frmCustomerSelection(shRiggertb.Text, dtState, false);
            frm.ShowDialog();
            if (Form1.sFormPass.Trim().Length > 0)
            {
                // store the customer# of the rigger selected
                shriggerkeylb.Text = Form1.sFormPass;
                dsCustomerMaster.CUSTOMERRow dr;
                dr = objGen.GetCustomer(shriggerkeylb.Text);
                if (dr.cstno == "xxxxxx")
                {
                    this.shRiggertb.Text = "Not Found";
                }
                else
                {
                    this.shRiggertb.Text = dr.custname.Trim();
                }
            }
            else
            {
                this.shRiggertb.Text = "";
                shriggerkeylb.Text = "";
            }
            this.Cursor = Cursors.Default;
            this.LostFocusBackColor(sender, e);
        }

        private void shRiggertb_Enter(object sender, EventArgs e)
        {
            this.SetFocusBackColor(sender, e);
        }

        #region shipping trucker 1

        private void shTrkAmounttb_Enter(object sender, EventArgs e)
        {
            this.SetFocusBackColor(sender, e);
            try
            {
                if (Convert.ToSingle(objGen.CleanNumber(shTrkAmounttb.Text)) == 0)
                    shTrkAmounttb.Text = "";
            }
            catch
            {
                shTrkAmounttb.Focus();
            }
        }

        private void shTruckertb_Leave(object sender, EventArgs e)
        {
            if (shTruckertb.Text.Trim().Length == 0)
            {
                this.shtruckerkeylb.Text = "";
                this.LostFocusBackColor(sender, e);
                return;
            }

            if (this.shTruckertb.Text.Trim() != _shOrigTrk)
            {
                // if no match on trucker found then perform lookup screen
                this.shtruckerLookupbtn_Click(sender, e);
            }
            this.LostFocusBackColor(sender, e);
        }

        private void shtruckerLookupbtn_Click(object sender, EventArgs e)
        {
            Form1.sFormPass = "";
            Form1.sFormPass2 = "";

            frmCustomerSelection frm = new frmCustomerSelection(shTruckertb.Text, dtState, false);
            frm.ShowDialog();
            if (Form1.sFormPass.Trim().Length > 0)
            {
                // store the customer# of the trucker selected
                shtruckerkeylb.Text = Form1.sFormPass;
                dsCustomerMaster.CUSTOMERRow dr;
                dr = objGen.GetCustomer(shtruckerkeylb.Text);
                if (dr.cstno == "xxxxxx")
                {
                    this.shTruckertb.Text = "Not Found";
                }
                else
                {
                    this.shTruckertb.Text = dr.custname.Trim();
                }
            }
            else
            {
                this.shTruckertb.Text = "";
                shtruckerkeylb.Text = "";
            }
            this.Cursor = Cursors.Default;
            this.LostFocusBackColor(sender, e);

        }

        private void shTrkAmounttb_Leave(object sender, EventArgs e)
        {
            try
            {
                shTrkAmounttb.Text = objGen.CleanNumber(shTrkAmounttb.Text);
                shTrkAmounttb.Text = Convert.ToSingle(shTrkAmounttb.Text.Trim()).ToString("c2");
                this.LostFocusBackColor(sender, e);
            }
            catch (Exception e2)
            {
                MessageBox.Show("There is a problem with the trucking amount you entered.  Enter only numbers.  If still a problem call I.T.  Message " + e2.Message);
                shTrkAmounttb.Focus();
            }
        }
        private void shipTrkLabel_Click(object sender, EventArgs e)
        {
            JumpToCustomer(shtruckerkeylb.Text);
        }
        #endregion

        #region Trucker 2
        private void shTruckertb2_Leave(object sender, EventArgs e)
        {
            if (shTruckertb2.Text.Trim().Length == 0)
            {
                this.shtruckerkeylb2.Text = "";
                this.shTrkAmounttb2.Text = "0";
                this.shTruckLastmtb2.Text = "12/30/1899";
                this.LostFocusBackColor(sender, e);
                return;
            }

            if (this.shTruckertb2.Text.Trim() != _shOrigTrk2)
            {
                // if no match on trucker found then perform lookup screen
                this.shtruckerLookupbtn2_Click(sender, e);
            }
            this.LostFocusBackColor(sender, e);
        }

        private void shtruckerLookupbtn2_Click(object sender, EventArgs e)
        {
            Form1.sFormPass = "";
            Form1.sFormPass2 = "";

            frmCustomerSelection frm = new frmCustomerSelection(shTruckertb2.Text, dtState, false);
            frm.ShowDialog();
            if (Form1.sFormPass.Trim().Length > 0)
            {
                // store the customer# of the trucker selected
                shtruckerkeylb2.Text = Form1.sFormPass;
                dsCustomerMaster.CUSTOMERRow dr;
                dr = objGen.GetCustomer(shtruckerkeylb2.Text);
                if (dr.cstno == "xxxxxx")
                {
                    this.shTruckertb2.Text = "Not Found";
                }
                else
                {
                    this.shTruckertb2.Text = dr.custname.Trim();
                    if (shTruckLastmtb2.Text == "12/30/1899")
                        shTruckLastmtb2.Text = DateTime.Today.ToShortDateString();
                }
            }
            else
            {
                this.shTruckertb2.Text = "";
                shtruckerkeylb2.Text = "";
                this.shTrkAmounttb2.Text = "0";
                this.shTruckLastmtb2.Text = "12/30/1899";

            }
            this.Cursor = Cursors.Default;
            this.LostFocusBackColor(sender, e);

        }

        private void shTrkAmounttb2_Enter(object sender, EventArgs e)
        {
            this.SetFocusBackColor(sender, e);
            try
            {
                if (Convert.ToSingle(objGen.CleanNumber(shTrkAmounttb2.Text)) == 0)
                    shTrkAmounttb2.Text = "";
            }
            catch
            {
                shTrkAmounttb2.Focus();
            }
        }

        private void shTrkAmounttb2_Leave(object sender, EventArgs e)
        {
            try
            {
                shTrkAmounttb2.Text = objGen.CleanNumber(shTrkAmounttb2.Text);
                shTrkAmounttb2.Text = Convert.ToSingle(shTrkAmounttb2.Text.Trim()).ToString("c2");
                this.LostFocusBackColor(sender, e);
            }
            catch (Exception e2)
            {
                MessageBox.Show("There is a problem with the trucking amount you entered.  Enter only numbers.  If still a problem call I.T.  Message " + e2.Message);
                shTrkAmounttb2.Focus();
            }
        }
        private void shipTrkLabel2_Click(object sender, EventArgs e)
        {
            JumpToCustomer(shtruckerkeylb2.Text);
        }

        #endregion

        #region Trucker 3

        private void shTrkAmounttb3_Enter(object sender, EventArgs e)
        {
            this.SetFocusBackColor(sender, e);
            try
            {
                if (Convert.ToSingle(objGen.CleanNumber(shTrkAmounttb3.Text)) == 0)
                    shTrkAmounttb3.Text = "";
            }
            catch
            {
                shTrkAmounttb3.Focus();
            }
        }

        private void shTruckertb3_Leave(object sender, EventArgs e)
        {
            if (shTruckertb3.Text.Trim().Length == 0)
            {
                this.shtruckerkeylb3.Text = "";
                this.shTrkAmounttb3.Text = "0";
                this.shTruckLastmtb3.Text = "12/30/1899";
                this.LostFocusBackColor(sender, e);

                return;
            }

            if (this.shTruckertb3.Text.Trim() != _shOrigTrk3)
            {
                // if no match on trucker found then perform lookup screen
                this.shtruckerLookupbtn3_Click(sender, e);
            }
            this.LostFocusBackColor(sender, e);
        }

        private void shtruckerLookupbtn3_Click(object sender, EventArgs e)
        {
            Form1.sFormPass = "";
            Form1.sFormPass2 = "";

            frmCustomerSelection frm = new frmCustomerSelection(shTruckertb3.Text, dtState, false);
            frm.ShowDialog();
            if (Form1.sFormPass.Trim().Length > 0)
            {
                // store the customer# of the trucker selected
                shtruckerkeylb3.Text = Form1.sFormPass;
                dsCustomerMaster.CUSTOMERRow dr;
                dr = objGen.GetCustomer(shtruckerkeylb3.Text);
                if (dr.cstno == "xxxxxx")
                {
                    this.shTruckertb3.Text = "Not Found";
                }
                else
                {
                    this.shTruckertb3.Text = dr.custname.Trim();
                    if (shTruckLastmtb3.Text == "12/30/1899")
                        shTruckLastmtb3.Text = DateTime.Today.ToShortDateString();
                }
            }
            else
            {
                this.shTruckertb3.Text = "";
                shtruckerkeylb3.Text = "";
                this.shTrkAmounttb2.Text = "0";
                this.shTruckLastmtb2.Text = "12/30/1899";

            }
            this.Cursor = Cursors.Default;
            this.LostFocusBackColor(sender, e);

        }

        private void shTrkAmounttb3_Leave(object sender, EventArgs e)
        {
            try
            {
                shTrkAmounttb3.Text = objGen.CleanNumber(shTrkAmounttb3.Text);
                shTrkAmounttb3.Text = Convert.ToSingle(shTrkAmounttb3.Text.Trim()).ToString("c2");
                this.LostFocusBackColor(sender, e);
            }
            catch (Exception e2)
            {
                MessageBox.Show("There is a problem with the trucking amount you entered.  Enter only numbers.  If still a problem call I.T.  Message " + e2.Message);
                shTrkAmounttb3.Focus();
            }
        }

        private void shipTrkLabel3_Click(object sender, EventArgs e)
        {
            JumpToCustomer(shtruckerkeylb3.Text);
        }

        #endregion


        private void shRigAmounttb_Leave(object sender, EventArgs e)
        {
            try
            {
                shRigAmounttb.Text = objGen.CleanNumber(shRigAmounttb.Text);
                shRigAmounttb.Text = Convert.ToSingle(shRigAmounttb.Text.Trim()).ToString("c2");
                this.LostFocusBackColor(sender, e);
            }
            catch (Exception e2)
            {
                MessageBox.Show("There is a problem with the trucking amount you entered.  Enter only numbers.  If still a problem call I.T.  Message " + e2.Message);
                this.shRigAmounttb.Focus();
            }
        }

        private void shdgv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            this.shRigTotalLb.Text = this.shCalcRigTotal().ToString("c2");
            this.shTruckTotalLb.Text = this.shCalcTruckTotal().ToString("c2");
            this.shTruckTotalLb2.Text = this.shCalcTruckTotal2().ToString("c2");
            this.shTruckTotalLb3.Text = this.shCalcTruckTotal3().ToString("c2");

            // validate inventory number entered and populate cash, model, mfg, and location columns
            if (e.ColumnIndex == 0)
            {
                string sinvno = shdgv.CurrentRow.Cells["shdgvinvno"].Value.ToString();
                if (sinvno.Trim().Length > 3 && sinvno.Substring(0, 4).ToUpper() == "MISC")
                {
                    return;
                }

                TestSql.DAL.dsInventory.PERFECDataTable dt = new dsInventory.PERFECDataTable();
                int nrows = taInv.FillByInvno(dt, sinvno);

                if (nrows == 0)
                {
                    MessageBox.Show("Inventory number not found");
                    return;
                }


                // check if item already on file with the same shipping type
                TestSql.DAL.dsShipItem.ShipItemSelectByShipNoDataTable dtI = new dsShipItem.ShipItemSelectByShipNoDataTable();
                TestSql.DAL.dsShip.ShipParentDataTable dtP = new dsShip.ShipParentDataTable();
                int n = this.taSHI.FillByInvno(dtI, sinvno);

                int i = 0;
                while (i < n)
                {
                    if (shShipNo.Text.Trim() != dtI[i].inbno)
                    {
                        dtP.Clear();
                        int p = this.taSH.FillByShipNo(dtP, dtI[i].inbno);
                        if (p > 0)
                        {
                            if (dtP[0].SHPTYPE == shShipTypecb.Text.Substring(0, 1))
                            {
                                string msg = sinvno + " already on file with another shipment. " + Environment.NewLine;
                                msg += "Pickup: " + dtI[i].pickup + Environment.NewLine;
                                msg += "Destination: " + dtI[i].dest + Environment.NewLine;
                                msg += "Ship Date: " + dtI[i].shipdate + Environment.NewLine;
                                msg += "Delivery Date: " + dtI[i].deldate + Environment.NewLine;
                                msg += "Destination: " + dtI[i].destcity.Trim() + " " + dtI[i].deststate + Environment.NewLine;
                                MessageBox.Show(msg);
                                break;
                            }
                        }
                    }
                    i++;
                }

                // fill in the model 
                shdgv.CurrentRow.Cells["shdgvmodel"].Value = dt[0].model;

                // fill in the cash column
                if (dt[0].PIFStatus.Length > 1)
                {
                    shdgv.CurrentRow.Cells["shdgvCash"].Value = dt[0].PIFStatus.Substring(1);
                }
                else
                {
                    shdgv.CurrentRow.Cells["shdgvCash"].Value = dt[0].PIFStatus;
                }

                // fill in the mfg and the current location of the machine
                shdgv.CurrentRow.Cells["shdgvmfg_short"].Value = dt[0].mfg_short;
                shdgv.CurrentRow.Cells["shdgvlocation"].Value = dt[0].location;

                // if the machine is part of an outbound shipment then default the pickup spot to be the current location
                if (shShipTypecb.Text.Trim().ToUpper() == "OUTBOUND")
                {
                    if (shdgv.CurrentRow.Cells["shdgvpickup"].Value.ToString().Trim().Length == 0)
                    {
                        shdgv.CurrentRow.Cells["shdgvpickup"].Value = dt[0].location;
                    }

                    // default the destination as CUST ( therefore customer )
                    if (shdgv.CurrentRow.Cells["shdgvdest"].Value.ToString().Trim().Length == 0)
                    {
                        shdgv.CurrentRow.Cells["shdgvdest"].Value = "CUST";

                        // when defaulted to customer look if there are any other customer CUST records on the grid, if so then
                        // default the address fields of the current record to what those are.
                        int i1 = 0;
                        while (i1 < shdgv.Rows.Count)
                        {
                            if (shdgv.Rows[i1].Cells["shdgvinvno"].Value != null && shdgv.Rows[i1].Cells["shdgvinvno"].Value.ToString() != sinvno &&
                                shdgv.Rows[i1].Cells["shdgvdest"].Value.ToString() == "CUST")
                            {
                                shdgv.CurrentRow.Cells["shdgvdestcust"].Value = shdgv.Rows[i1].Cells["shdgvdestcust"].Value;
                                shdgv.CurrentRow.Cells["shdgvdestcstno"].Value = shdgv.Rows[i1].Cells["shdgvdestcstno"].Value;
                                shdgv.CurrentRow.Cells["shdgvdestaddr1"].Value = shdgv.Rows[i1].Cells["shdgvdestaddr1"].Value;
                                shdgv.CurrentRow.Cells["shdgvdestaddr2"].Value = shdgv.Rows[i1].Cells["shdgvdestaddr2"].Value;
                                shdgv.CurrentRow.Cells["shdgvdestcity"].Value = shdgv.Rows[i1].Cells["shdgvdestcity"].Value;
                                shdgv.CurrentRow.Cells["shdgvdeststate"].Value = shdgv.Rows[i1].Cells["shdgvdeststate"].Value;
                                shdgv.CurrentRow.Cells["shdgvdestzip"].Value = shdgv.Rows[i1].Cells["shdgvdestzip"].Value;
                                shdgv.CurrentRow.Cells["shdgvdestforn"].Value = shdgv.Rows[i1].Cells["shdgvdestforn"].Value;
                                break;
                            }
                            i1++;
                        }
                    }
                }

            }


            // validate the shipping date entered
            if (e.ColumnIndex == 15)
            {
                string s = shdgv.CurrentRow.Cells["shdgvshipped"].Value.ToString().Trim();
                string msg = objGen.isValidDate(s);
                if (msg != "ok")
                {
                    MessageBox.Show(msg);
                    return;
                }
            }

            // validate the delivered date entered
            if (e.ColumnIndex == 16)
            {
                string s = shdgv.CurrentRow.Cells["shdgvdelivered"].Value.ToString().Trim();
                string msg = objGen.isValidDate(s);
                if (msg != "ok")
                {
                    MessageBox.Show(msg);
                    return;
                }
            }

            // validate pickup location
            if (e.ColumnIndex == 7)
            {
                // check if not empty, then validate
                if (shdgv.CurrentRow.Cells["shdgvpickup"].Value.ToString().Trim().Length > 0)
                {
                    if (!objGen.ValidLocationCode(shdgv.CurrentRow.Cells["shdgvpickup"].Value.ToString()))
                    {
                        Form1.sFormPass = "";
                        frmLocationLookUp frm = new frmLocationLookUp(shdgv.CurrentRow.Cells["shdgvpickup"].Value.ToString(), true);
                        frm.ShowDialog();

                        shdgv.CurrentRow.Cells["shdgvpickup"].Value = Form1.sFormPass;
                    }
                }
            }

            //validate destination location
            if (e.ColumnIndex == 8)
            {
                shdgv.CurrentRow.Cells["shdgvdest"].Value = shdgv.CurrentRow.Cells["shdgvdest"].Value.ToString().ToUpper();

                // if the user blanks out the destination field then blank out the address fields
                if (shdgv.CurrentRow.Cells["shdgvdest"].Value.ToString().Trim().Length == 0)
                {
                    shdgv.CurrentRow.Cells["shdgvdestcstno"].Value = "";
                    shdgv.CurrentRow.Cells["shdgvdestcust"].Value = "";
                    shdgv.CurrentRow.Cells["shdgvdestaddr1"].Value = "";
                    shdgv.CurrentRow.Cells["shdgvdestaddr2"].Value = "";
                    shdgv.CurrentRow.Cells["shdgvdestcity"].Value = "";
                    shdgv.CurrentRow.Cells["shdgvdeststate"].Value = "";
                    shdgv.CurrentRow.Cells["shdgvdestzip"].Value = "";
                    shdgv.CurrentRow.Cells["shdgvdestforn"].Value = "";
                    return;
                }

                if (this.shShipTypecb.Text.Substring(0, 1).ToUpper() == "O" && shdgv.CurrentRow.Cells["shdgvdest"].Value.ToString().ToUpper() != "CUST")
                {
                    MessageBox.Show("Destination of outbound shipment must be CUST");
                    return;
                }

                if (this.shShipTypecb.Text.Substring(0, 1).ToUpper() == "O" && shdgv.CurrentRow.Cells["shdgvdest"].Value.ToString().ToUpper() == "CUST")
                {
                    // if the destination customer information is not filled in yet, then populate it for the user by looking
                    // up the sales order for the item and grabbing the customer information
                    if (shdgv.CurrentRow.Cells["shdgvdestcust"].Value.ToString().Trim().Length == 0)
                    {

                        // first lookup inventory record and grab the customer number from the tag
                        string sinvno = shdgv.CurrentRow.Cells["shdgvinvno"].Value.ToString();
                        TestSql.DAL.dsInventory.PERFECDataTable dt = new dsInventory.PERFECDataTable();
                        int nrows = taInv.FillByInvno(dt, sinvno);

                        if (nrows == 1 && dt[0].hld_cstno.Trim().Length > 0)
                        {
                            TestSql.DAL.dsCustomerMaster.CUSTOMERDataTable dtCstM = new dsCustomerMaster.CUSTOMERDataTable();
                            TestSql.DAL.dsCustomerMasterTableAdapters.CUSTOMERTableAdapter taCstM = new TestSql.DAL.dsCustomerMasterTableAdapters.CUSTOMERTableAdapter();
                            taCstM.Connection = this.WhichConnection;

                            int ncst = taCstM.FillbyCstno(dtCstM, dt[0].hld_cstno, 0);

                            if (ncst == 1)
                            {
                                shdgv.CurrentRow.Cells["shdgvdestcust"].Value = dtCstM[0].custname.Trim();
                                shdgv.CurrentRow.Cells["shdgvdestcstno"].Value = dtCstM[0].cstno.Trim();
                                shdgv.CurrentRow.Cells["shdgvdestaddr1"].Value = dtCstM[0].address1.Trim();
                                shdgv.CurrentRow.Cells["shdgvdestaddr2"].Value = dtCstM[0].address2.Trim();
                                shdgv.CurrentRow.Cells["shdgvdestcity"].Value = dtCstM[0].city.Trim();
                                shdgv.CurrentRow.Cells["shdgvdeststate"].Value = dtCstM[0].state.Trim();
                                shdgv.CurrentRow.Cells["shdgvdestzip"].Value = dtCstM[0].zip.Trim();
                                shdgv.CurrentRow.Cells["shdgvdestforn"].Value = dtCstM[0].forgncode.Trim();
                            }
                            else
                            {
                                shdgv.CurrentRow.Cells["shdgvdestcstno"].Value = "";
                                shdgv.CurrentRow.Cells["shdgvdestcust"].Value = "";
                                shdgv.CurrentRow.Cells["shdgvdestaddr1"].Value = "";
                                shdgv.CurrentRow.Cells["shdgvdestaddr2"].Value = "";
                                shdgv.CurrentRow.Cells["shdgvdestcity"].Value = "";
                                shdgv.CurrentRow.Cells["shdgvdeststate"].Value = "";
                                shdgv.CurrentRow.Cells["shdgvdestzip"].Value = "";
                                shdgv.CurrentRow.Cells["shdgvdestforn"].Value = "";
                            }


                        }
                    }


                }
                if (this.shShipTypecb.Text.Substring(0, 1).ToUpper() != "O")
                {
                    // check if not empty, then validate
                    if (shdgv.CurrentRow.Cells["shdgvdest"].Value.ToString().Trim().Length > 0)
                    {
                        if (!objGen.ValidLocationCode(shdgv.CurrentRow.Cells["shdgvdest"].Value.ToString()))
                        {
                            // the 'true' parameter in the below will cause the stored proc to be called that does a LIKE comparison of the
                            // location code that was entered.
                            Form1.sFormPass = "";
                            frmLocationLookUp frm = new frmLocationLookUp(shdgv.CurrentRow.Cells["shdgvdest"].Value.ToString(), true);
                            frm.ShowDialog();

                            shdgv.CurrentRow.Cells["shdgvdest"].Value = Form1.sFormPass;
                        }
                    }
                }
            }
        }

        private void shSavebtn_Click(object sender, EventArgs e)
        {
            // before saving make sure there are items for the shipment
            bool bOk = true;
            bool bAtLeastOneItemEntered = false;
            string sinvno = "";
            int nrows = 0;
            string msg = "";
            int i = 0;

            TestSql.DAL.dsInventory.PERFECDataTable dt = new dsInventory.PERFECDataTable();

            while (i < shdgv.Rows.Count - 1 && bOk)
            {
                sinvno = shdgv.Rows[i].Cells["shdgvinvno"].Value.ToString();
                nrows = taInv.FillByInvno(dt, sinvno);
                if (nrows == 0)
                {
                    msg += sinvno + " ";
                    bOk = false;
                }
                else
                {
                    bAtLeastOneItemEntered = true;
                }
                i++;
            }

            if (!bOk)
            {
                MessageBox.Show("Cannot save the shipping information because inventory items " + msg + " are invalid.");
                return;
            }

            if (!bAtLeastOneItemEntered)
            {
                MessageBox.Show("Cannot save a shipment without inventory items");
                return;
            }

            /* validate that all items have a delivery date and destination for outbound shipments */
            if (shShipTypecb.Text.Substring(0, 1) == "O")
            {
                bool b = false;
                foreach (dsShipItem.ShipItemSelectByShipNoRow r in dtSHI)
                {
                    if (r.RowState == DataRowState.Deleted)
                    {
                        continue;
                    }

                    if (r.deldate == DateTime.Parse("12/30/1899") || r.dest.Trim().Length == 0)
                    {
                        b = true;
                        break;
                    }
                }

                if (b)
                {
                    MessageBox.Show("All items on an outbound shipping record must be given a delivery date and a destination.  If the destination is the customer then you can just enter 'CUST' ");
                    return;
                }
            }

            blShipping obj = new blShipping(pUserId, pconnectionString);

            if (_shAddMode)
            {
                obj.inbno = "";   // the shipping number will be determined in the insert stored proc
            }
            else
            {
                obj.inbno = shShipNo.Text.Trim();
            }
            obj.rigcstno = shriggerkeylb.Text.Trim();
            obj.rg_amt = Convert.ToSingle(objGen.CleanNumber(shRigAmounttb.Text));
            obj.rig_last_contacted = objGen.blankDate(shRigLastmtb.Text);

            obj.trkcstno = shtruckerkeylb.Text.Trim();
            obj.trk_amt = Convert.ToDecimal(objGen.CleanNumber(this.shTrkAmounttb.Text));
            obj.trk_last_contacted = objGen.blankDate(shTruckLastmtb.Text);

            obj.trkcstno2 = shtruckerkeylb2.Text.Trim();
            obj.trk_amt2 = Convert.ToDecimal(objGen.CleanNumber(this.shTrkAmounttb2.Text));
            obj.trk_last_contacted2 = objGen.blankDate(shTruckLastmtb2.Text);

            obj.trkcstno3 = shtruckerkeylb3.Text.Trim();
            obj.trk_amt3 = Convert.ToDecimal(objGen.CleanNumber(this.shTrkAmounttb3.Text));
            obj.trk_last_contacted3 = objGen.blankDate(shTruckLastmtb3.Text);

            obj.container = shContainertb.Text.Trim();
            obj.comments = shComments.Text.Trim();
            obj.shipmentType = shShipTypecb.Text.Substring(0, 1);

            obj.addbyid = Form1.pUserId;

            if (obj.Save(obj, this.dtSHI))
            {
                shShipNo.Text = obj.inbno;
                MessageBox.Show(obj.message);

                // in add mode obj.id will be set in the saving of the parent
                // shipping record.  Set the current  shipping key equal to that value
                // so we can re-populate the shipping screen with the new values
                // of that record.
                if (_shAddMode)
                {
                    this.SH_LoadInbno(obj.inbno);
                }
                else
                {
                    this.SH_Loadid(_currentSHKey);
                }
                _shAddMode = false;
                shNewbtn.Enabled = true;
                shPrintPoBtn.Enabled = true;

            }
            else
            {
                MessageBox.Show(obj.message);
            }
        }

		 private void shPrintPoBtn_Click(object sender, EventArgs e)
        {
            this.shSavebtn_Click(sender, e);

            if (dtSH[0].trkcstno.Trim().Length == 0 &&
               dtSH[0].rigcstno.Trim().Length == 0)
            {
                MessageBox.Show("Cannot print a PO until their is a rigger or trucker associated with this shipping record.");
                return;
            }

            this.Cursor = Cursors.Default;

            if (dtSH[0].trkcstno.Trim().Length > 0 &&
                dtSH[0].rigcstno.Trim() == dtSH[0].trkcstno.Trim())
            {
                foreach (TestSql.DAL.dsShipItem.ShipItemSelectByShipNoRow i in dtSHI)
                {
                    i.rpt_amt = i.trk_amt + Convert.ToDecimal(i.rg_amt);
                }

                TestSql.Reports.rpShipPOBothRF r1 = new TestSql.Reports.rpShipPOBothRF(dtSH[0], dtSHI,
                    dtSH[0].trkcstno, 1, dtSH[0].TRK_AMT, Convert.ToDecimal(dtSH[0].RG_AMT));
                Application.DoEvents();
                r1.DataSource = dtSHI;
                r1.PrintDialog();
                Application.DoEvents();
                //r1.ShowPreview();

                if (dtSH[0].trkcstno2.Trim().Length > 0)
                {
                    foreach (TestSql.DAL.dsShipItem.ShipItemSelectByShipNoRow i in dtSHI)
                    {
                        i.rpt_amt = i.trk_amt2;
                    }
                    TestSql.Reports.rptShipPO r2 = new TestSql.Reports.rptShipPO(dtSH[0], dtSHI,
                        "Freight", dtSH[0].trkcstno2, 2, dtSH[0].trk_amt2, Convert.ToDecimal(dtSH[0].RG_AMT));
                    Application.DoEvents();
                    r2.DataSource = dtSHI;
                    r2.PrintDialog();
                    Application.DoEvents();

                }

                if (dtSH[0].trkcstno3.Trim().Length > 0)
                {
                    foreach (TestSql.DAL.dsShipItem.ShipItemSelectByShipNoRow i in dtSHI)
                    {
                        i.rpt_amt = i.trk_amt3;
                    }

                    TestSql.Reports.rptShipPO r3 = new TestSql.Reports.rptShipPO(dtSH[0], dtSHI,
                        "Freight", dtSH[0].trkcstno3, 3, dtSH[0].trk_amt3, Convert.ToDecimal(dtSH[0].RG_AMT));
                    Application.DoEvents();
                    r3.DataSource = dtSHI;
                    r3.PrintDialog();
                    Application.DoEvents();
                    //r3.ShowPreview();
                }

            }
            else
            {
                if (dtSH[0].trkcstno.Trim().Length > 0)
                {
                    foreach (TestSql.DAL.dsShipItem.ShipItemSelectByShipNoRow i in dtSHI)
                    {
                        i.rpt_amt = i.trk_amt;
                    }
                    //dtSHI.AcceptChanges();

                    TestSql.Reports.rptShipPO r9 = new TestSql.Reports.rptShipPO(dtSH[0], dtSHI, "Freight",
                         dtSH[0].trkcstno, 1, dtSH[0].TRK_AMT, Convert.ToDecimal(dtSH[0].RG_AMT));
                    Application.DoEvents();
                    r9.DataSource = dtSHI;
                    r9.PrintDialog();
                    Application.DoEvents();
                    //r9.ShowPreview();
                }

                if (dtSH[0].trkcstno2.Trim().Length > 0)
                {
                    foreach (TestSql.DAL.dsShipItem.ShipItemSelectByShipNoRow i in dtSHI)
                    {
                        i.rpt_amt = i.trk_amt2;
                    }

                    TestSql.Reports.rptShipPO r2 = new TestSql.Reports.rptShipPO(dtSH[0], dtSHI, "Freight",
                         dtSH[0].trkcstno2, 2, dtSH[0].trk_amt2, Convert.ToDecimal(dtSH[0].RG_AMT));
                    Application.DoEvents();
                    r2.DataSource = dtSHI;
                    r2.PrintDialog();
                    Application.DoEvents();
                    //r2.ShowPreview();
                }

                if (dtSH[0].trkcstno3.Trim().Length > 0)
                {
                    foreach (TestSql.DAL.dsShipItem.ShipItemSelectByShipNoRow i in dtSHI)
                    {
                        i.rpt_amt = i.trk_amt3;
                    }

                    TestSql.Reports.rptShipPO r3 = new TestSql.Reports.rptShipPO(dtSH[0], dtSHI, "Freight",
                         dtSH[0].trkcstno3, 3, dtSH[0].trk_amt3, Convert.ToDecimal(dtSH[0].RG_AMT));
                    Application.DoEvents();
                    r3.DataSource = dtSHI;
                    r3.PrintDialog();
                    Application.DoEvents();
                    //r3.ShowPreview();
                }

                if (dtSH[0].rigcstno.Trim().Length > 0)
                {
                    foreach (TestSql.DAL.dsShipItem.ShipItemSelectByShipNoRow i in dtSHI)
                    {
                        i.rpt_amt = Convert.ToDecimal(i.rg_amt);
                    }
                    //dtSHI.AcceptChanges();

                    TestSql.Reports.rptShipPO rig = new TestSql.Reports.rptShipPO(dtSH[0], dtSHI, "Rigging",
                         dtSH[0].trkcstno, 1, dtSH[0].TRK_AMT, Convert.ToDecimal(dtSH[0].RG_AMT));
                    Application.DoEvents();
                    rig.DataSource = dtSHI;
                    rig.PrintDialog();
                    Application.DoEvents();
                    //rig.ShowPreview();
                }
            }
            this.Cursor = Cursors.Default;
        }

		private void shBillOfLadingbtn_Click(object sender, EventArgs e)
        {
            TestSql.Forms.BillLading.frmBOL frm = new TestSql.Forms.BillLading.frmBOL();
            frm.shipno = shShipNo.Text;
            frm.Show();

            /*
            if (frmBOL == null)
            {
                frmBOL = new TestSql.Forms.BillLading.frmBOL();
                frmBOL.TopLevel = false;
                frmBOL.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                frmBOL.Parent = tpDeals;
                //frmBOL.userid = Form1.pUserId;
                //frmBOL.id = -1;
                frmBOL.Show();
            }

            frmBOL.BringToFront();
             * */

        }

		private void shPrintPoBtn_Click(object sender, EventArgs e)
        {
            this.shSavebtn_Click(sender, e);

            if (dtSH[0].trkcstno.Trim().Length == 0 &&
               dtSH[0].rigcstno.Trim().Length == 0)
            {
                MessageBox.Show("Cannot print a PO until their is a rigger or trucker associated with this shipping record.");
                return;
            }

            this.Cursor = Cursors.Default;

            if (dtSH[0].trkcstno.Trim().Length > 0 &&
                dtSH[0].rigcstno.Trim() == dtSH[0].trkcstno.Trim())
            {
                foreach (TestSql.DAL.dsShipItem.ShipItemSelectByShipNoRow i in dtSHI)
                {
                    i.rpt_amt = i.trk_amt + Convert.ToDecimal(i.rg_amt);
                }

                TestSql.Reports.rpShipPOBothRF r1 = new TestSql.Reports.rpShipPOBothRF(dtSH[0], dtSHI,
                    dtSH[0].trkcstno, 1, dtSH[0].TRK_AMT, Convert.ToDecimal(dtSH[0].RG_AMT));
                Application.DoEvents();
                r1.DataSource = dtSHI;
                r1.PrintDialog();
                Application.DoEvents();
                //r1.ShowPreview();

                if (dtSH[0].trkcstno2.Trim().Length > 0)
                {
                    foreach (TestSql.DAL.dsShipItem.ShipItemSelectByShipNoRow i in dtSHI)
                    {
                        i.rpt_amt = i.trk_amt2;
                    }
                    TestSql.Reports.rptShipPO r2 = new TestSql.Reports.rptShipPO(dtSH[0], dtSHI,
                        "Freight", dtSH[0].trkcstno2, 2, dtSH[0].trk_amt2, Convert.ToDecimal(dtSH[0].RG_AMT));
                    Application.DoEvents();
                    r2.DataSource = dtSHI;
                    r2.PrintDialog();
                    Application.DoEvents();

                }

                if (dtSH[0].trkcstno3.Trim().Length > 0)
                {
                    foreach (TestSql.DAL.dsShipItem.ShipItemSelectByShipNoRow i in dtSHI)
                    {
                        i.rpt_amt = i.trk_amt3;
                    }

                    TestSql.Reports.rptShipPO r3 = new TestSql.Reports.rptShipPO(dtSH[0], dtSHI,
                        "Freight", dtSH[0].trkcstno3, 3, dtSH[0].trk_amt3, Convert.ToDecimal(dtSH[0].RG_AMT));
                    Application.DoEvents();
                    r3.DataSource = dtSHI;
                    r3.PrintDialog();
                    Application.DoEvents();
                    //r3.ShowPreview();
                }

            }
            else
            {
                if (dtSH[0].trkcstno.Trim().Length > 0)
                {
                    foreach (TestSql.DAL.dsShipItem.ShipItemSelectByShipNoRow i in dtSHI)
                    {
                        i.rpt_amt = i.trk_amt;
                    }
                    //dtSHI.AcceptChanges();

                    TestSql.Reports.rptShipPO r9 = new TestSql.Reports.rptShipPO(dtSH[0], dtSHI, "Freight",
                         dtSH[0].trkcstno, 1, dtSH[0].TRK_AMT, Convert.ToDecimal(dtSH[0].RG_AMT));
                    Application.DoEvents();
                    r9.DataSource = dtSHI;
                    r9.PrintDialog();
                    Application.DoEvents();
                    //r9.ShowPreview();
                }

                if (dtSH[0].trkcstno2.Trim().Length > 0)
                {
                    foreach (TestSql.DAL.dsShipItem.ShipItemSelectByShipNoRow i in dtSHI)
                    {
                        i.rpt_amt = i.trk_amt2;
                    }

                    TestSql.Reports.rptShipPO r2 = new TestSql.Reports.rptShipPO(dtSH[0], dtSHI, "Freight",
                         dtSH[0].trkcstno2, 2, dtSH[0].trk_amt2, Convert.ToDecimal(dtSH[0].RG_AMT));
                    Application.DoEvents();
                    r2.DataSource = dtSHI;
                    r2.PrintDialog();
                    Application.DoEvents();
                    //r2.ShowPreview();
                }

                if (dtSH[0].trkcstno3.Trim().Length > 0)
                {
                    foreach (TestSql.DAL.dsShipItem.ShipItemSelectByShipNoRow i in dtSHI)
                    {
                        i.rpt_amt = i.trk_amt3;
                    }

                    TestSql.Reports.rptShipPO r3 = new TestSql.Reports.rptShipPO(dtSH[0], dtSHI, "Freight",
                         dtSH[0].trkcstno3, 3, dtSH[0].trk_amt3, Convert.ToDecimal(dtSH[0].RG_AMT));
                    Application.DoEvents();
                    r3.DataSource = dtSHI;
                    r3.PrintDialog();
                    Application.DoEvents();
                    //r3.ShowPreview();
                }

                if (dtSH[0].rigcstno.Trim().Length > 0)
                {
                    foreach (TestSql.DAL.dsShipItem.ShipItemSelectByShipNoRow i in dtSHI)
                    {
                        i.rpt_amt = Convert.ToDecimal(i.rg_amt);
                    }
                    //dtSHI.AcceptChanges();

                    TestSql.Reports.rptShipPO rig = new TestSql.Reports.rptShipPO(dtSH[0], dtSHI, "Rigging",
                         dtSH[0].trkcstno, 1, dtSH[0].TRK_AMT, Convert.ToDecimal(dtSH[0].RG_AMT));
                    Application.DoEvents();
                    rig.DataSource = dtSHI;
                    rig.PrintDialog();
                    Application.DoEvents();
                    //rig.ShowPreview();
                }
            }
            this.Cursor = Cursors.Default;
        }

        private void shipRigLabel_Click(object sender, EventArgs e)
        {
            JumpToCustomer(shriggerkeylb.Text);
        }
        private void shipRigLabel_Click(object sender, EventArgs e)
        {
            JumpToCustomer(shriggerkeylb.Text);
        }
        private void shSearchtb_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!_shAddMode)
            {
                if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Tab)
                {
                    this.Cursor = Cursors.WaitCursor;
                    Application.DoEvents();

                    // first check if we find an exact match and only one record
                    this.dsShip.ShipSearch.Clear();
                    TestSql.DAL.dsShipTableAdapters.ShipSearchTableAdapter ta = new TestSql.DAL.dsShipTableAdapters.ShipSearchTableAdapter();
                    ta.Connection = this.WhichConnection;

                    int nrows = ta.FillBySearchKey(this.dsShip.ShipSearch, this.shSearchtb.Text);

                    this.Cursor = Cursors.Default;
                    Application.DoEvents();

                    if (nrows != 1)
                    {
                        Form1.sFormPass = "";
                        frmShipSearch frm = new frmShipSearch(this.shSearchtb.Text);

                        frm.ShowDialog();

                        if (Form1.sFormPass.Trim().Length > 0)
                        {
                            EventArgs e1 = new EventArgs();
                            this.LostFocusBackColor(sender, e1);
                            this._currentSHKey = Convert.ToInt32(Form1.sFormPass.Trim());
                            this.SH_Loadid(_currentSHKey);
                        }
                    }
                    else
                    {
                        EventArgs e1 = new EventArgs();
                        this.LostFocusBackColor(sender, e1);
                        this._currentSHKey = this.dsShip.ShipSearch[0].id;
                        this.SH_Loadid(_currentSHKey);
                    }
                }
            }
        }

        private void shListll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Form1.sFormPass = "";
            frmShipSearch frm = new frmShipSearch("", "list");
            frm.ShowDialog();
            if (Form1.sFormPass != "")
            {
                _currentSHKey = Convert.ToInt32(Form1.sFormPass);
                this.SH_Loadid(_currentSHKey);
            }
        }

        private void shSearchll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Form1.sFormPass = "";
            frmShipSearch frm = new frmShipSearch(this.shSearchtb.Text);
            frm.ShowDialog();

            if (Form1.sFormPass.Trim().Length > 0)
            {
                this._currentSHKey = Convert.ToInt32(Form1.sFormPass.Trim());
                this.SH_Loadid(_currentSHKey);
            }
        }

		#region

			// 12/6/2017 - old code from form1.cs , this code sent an email to a customer on someone else's behalf:

			//private void SendEmailCustomerOnBehalfOfSomeone()
        //{
        //    bool bEmailSent = false;
        //    this.Cursor = Cursors.WaitCursor;

        //    try
        //    {
        //        if (tbEmailFrom.Text.Trim().Length < 5)
        //        {
        //            MessageBox.Show("Please enter a valid FROM email address", "Invalid FROM", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        //            return;
        //        }

        //        MailMessage msg = new MailMessage();

        //        if (tbEmailTo.Text.Trim().Length > 5)
        //        {
        //            string s = tbEmailTo.Text.Trim();
        //            while (s.Length > 5)
        //            {
        //                int n = s.IndexOf(';');
        //                if (n > 0)
        //                {
        //                    msg.To.Add(s.Substring(0, n));
        //                    s = s.Substring(n + 1);
        //                }
        //                else
        //                {
        //                    msg.To.Add(s);
        //                    break;
        //                }
        //            }
        //        }

        //        if (tbEmailCC.Text.Trim().Length > 5)
        //        {
        //            string s = tbEmailCC.Text.Trim();
        //            while (s.Length > 5)
        //            {
        //                int n = s.IndexOf(';');
        //                if (n > 0)
        //                {
        //                    msg.CC.Add(s.Substring(0, n));
        //                    s = s.Substring(n + 1);
        //                }
        //                else
        //                {
        //                    msg.CC.Add(s);
        //                    break;
        //                }
        //            }
        //        }

        //        if (tbEmailBcc.Text.Trim().Length > 5)
        //        {
        //            string s = tbEmailBcc.Text.Trim();
        //            while (s.Length > 5)
        //            {
        //                int n = s.IndexOf(';');
        //                if (n > 0)
        //                {
        //                    msg.Bcc.Add(s.Substring(0, n));
        //                    s = s.Substring(n + 1);
        //                }
        //                else
        //                {
        //                    msg.Bcc.Add(s);
        //                    break;
        //                }
        //            }
        //        }

        //        msg.From = new MailAddress(tbEmailFrom.Text.Trim());
        //        msg.Subject = tbEmailSubject.Text.Trim();
        //        msg.IsBodyHtml = true;

        //        if (SendAsPIS.Checked)
        //        {
        //            msg.Body = "<div><img src=http://pmsql01.perfectionmachinery.com/images/PIS_Header.JPG><br><br></div>" +
        //                "<div style='font-size:10.0pt;width=500px' align=" + '"' + "Left" + '"' + ">" +
        //                objGen.parsetext(tbEmailMessage.Text.Trim(), true) + "</div>";
        //        }
        //        else
        //        {
        //            msg.Body = "<div><img src=http://pmsql01.perfectionmachinery.com/images/Banner_4BarMDNA.jpg></div>" +
        //                     "<div style='font-size:10.0pt;width=300px' align=" + '"' + "Left" + '"' + ">" +
        //                     objGen.parsetext2(tbEmailMessage.Text.Trim(), true) + "</div>";
        //        }

        //        /* Determine which email signature to use.  If the email is being sent
        //         * for someone else, example Kathy sending the email for Joe, then use
        //         * Joe's signature
        //         * */
        //        int emailAddedFor = Form1.pUserId;

        //        string _emailSignature = pUserEmailSignature;
        //        if (msg.From.ToString() != pUserEmail)
        //        {
        //            TestSql.DAL.dsEmployee.EmployeeDataTable dtE = new dsEmployee.EmployeeDataTable();
        //            dtE = objGen.GetEmployeeRecordUsingEmailAddress(msg.From.ToString());
        //            if (dtE.Rows.Count > 0)
        //            {
        //                _emailSignature = dtE[0].EmailSignature;
        //                emailAddedFor = dtE[0].employeeid;

        //            }
        //            else
        //            {
        //                _emailSignature = pUserEmailSignature;
        //            }
        //        }

        //        if (SendAsPIS.Checked)
        //        {
        //            msg.Body += "<div><img src=http://pmsql01.perfectionmachinery.com/images/PISletterheadbottom.jpg></div>";
        //        }

        //        string sfilename = "";

        //        if (tbEmailAttachments.Text.Trim().Length > 5)
        //        {
        //            string s = tbEmailAttachments.Text.Replace(Environment.NewLine, "");
        //            s = s.Replace(@"\n", "");
        //            s = s.Trim();
        //            s = s.Replace("\n", "");
        //            s = s.Replace('\n', ' ');

        //            while (s.Length > 5)
        //            {
        //                int n = s.IndexOf(';');
        //                if (n > 0)
        //                {
        //                    sfilename = s.Substring(0, n);
        //                    Attachment att = new Attachment(sfilename);
        //                    msg.Attachments.Add(att);
        //                    s = s.Substring(n + 1);
        //                }
        //                else
        //                {
        //                    Attachment att = new Attachment(s);
        //                    msg.Attachments.Add(att);
        //                    break;
        //                }
        //            }
        //        }

        //        string emailSend = objGen.SendEmail(msg);
        //        if (emailSend.Trim().Length > 1)
        //        {
        //            MessageBox.Show(emailSend);
        //            bEmailSent = false;
        //            return;
        //        }

        //        bEmailSent = true;

        //        SqlConnection cn = new SqlConnection();
        //        cn.ConnectionString = _connectionString;

        //        SqlCommand cmd = new SqlCommand();
        //        cmd.Connection = cn;
        //        cmd.CommandType = CommandType.StoredProcedure;
        //        cmd.CommandText = "EmailInsert";

        //        cmd.Parameters.Add(new SqlParameter("@cstno", this.CustomerCstnolb.Text));
        //        cmd.Parameters.Add(new SqlParameter("@contactid", Convert.ToInt32(cbEmailContact.SelectedValue)));
        //        if (tbEmailAttachments.Text.Trim().Length > 5)
        //        {
        //            cmd.Parameters.Add(new SqlParameter("@note", "Attachments sent were " + this.tbEmailAttachments.Text + Environment.NewLine + tbEmailMessage.Text.Trim()));
        //        }
        //        else
        //        {
        //            cmd.Parameters.Add(new SqlParameter("@note", tbEmailMessage.Text.Trim()));
        //        }
        //        cmd.Parameters.Add(new SqlParameter("@addbyid", emailAddedFor));
        //        cmd.Parameters.Add(new SqlParameter("@emailaddress", tbEmailTo.Text.Trim()));
        //        cmd.Parameters.Add(new SqlParameter("@emailsubject", tbEmailSubject.Text.Trim()));
        //        cmd.Parameters.Add(new SqlParameter("@emailCC", tbEmailCC.Text.Trim()));
        //        cmd.Parameters.Add(new SqlParameter("@emailBCC", tbEmailBcc.Text.Trim()));
        //        cmd.Parameters.Add(new SqlParameter("@emailFrom", tbEmailFrom.Text.Trim()));

        //        cn.Open();

        //        int nrows = Convert.ToInt32(cmd.ExecuteScalar());

        //        this.Cursor = Cursors.Default;

        //        if (nrows == 1)
        //        {
        //            MessageBox.Show("Email Sent");
        //            _EmailAddMode = false;

        //            btnEmailLast.Enabled = true;
        //            btnEmailNew.Enabled = true;
        //            btnEmailCancel.Enabled = false;
        //            btnEmailSendCustomer.Enabled = false;

        //            // call the load last method to repaint screen 
        //            this.Email_LoadLast(CustomerCstnolb.Text);
        //        }
        //        else
        //        {
        //            MessageBox.Show("Error -  Email Sent, but the system did not save the fact that you sent the email");
        //        }
        //        cn.Close();
        //        cn.Dispose();
        //        cmd.Dispose();

        //    }
        //    catch (Exception e2)
        //    {
        //        this.Cursor = Cursors.Default;

        //        if (bEmailSent)
        //        {
        //            objGen.error_record(e2, Form1.pUserId);
        //            MessageBox.Show("Error - The email was sent, but the system did not save the fact that you sent the email.  Call I.T. with the following: " + e2.Message);
        //        }
        //        else
        //        {
        //            MessageBox.Show("Error sending email " + e2.Message);
        //        }
        //    }

        //}
		#endregion