private void Save()
        {
            try
            {
                int      defaultAllocationLimit = AppVars.objPolicyConfiguration.AllocateDrvPreExistJobLimit.ToInt();
                int?     driverId          = ddl_Driver.SelectedValue.ToIntorNull();
                int?     oldDriverId       = null;
                string   oldDriverNo       = string.Empty;
                long     jobId             = 0;
                bool     cancelJob         = false;
                DateTime?pickupDateAndTime = ObjMaster.Current.PickupDateTime.ToDateTimeorNull();


                //if (driverId != null && defaultAllocationLimit>0 &&
                //    General.GetQueryable<Booking>(null).Count(c => c.DriverId == driverId && c.Id!=ObjMaster.Current.Id
                //         && (c.BookingStatusId==Enums.BOOKINGSTATUS.WAITING || c.BookingStatusId==Enums.BOOKINGSTATUS.PENDING)
                //         &&

                //         (
                //         (c.PickupDateTime.Value<=pickupDateAndTime
                //         && (c.PickupDateTime.Value >= pickupDateAndTime.Value.AddMinutes(-defaultAllocationLimit)) )

                //              || (c.PickupDateTime.Value >= pickupDateAndTime
                //         && (c.PickupDateTime.Value <= pickupDateAndTime.Value.AddMinutes(defaultAllocationLimit)))
                //         )


                //         ) > 0)
                //{

                //    if (DialogResult.No == MessageBox.Show("This driver already have a Job Allocated at this time "+
                //         Environment.NewLine + "Do you still want to Allocate it ? ", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                //    {
                //        return;

                //    }

                //}


                if (driverId != null)
                {
                    var ObjDriver = General.GetObject <Fleet_Driver>(c => c.Id == driverId);

                    if (ObjDriver != null)
                    {
                        allocateDrvNo = ObjDriver.DriverNo.ToStr().Trim();
                        if (ObjDriver.VehicleTypeId != null)
                        {
                            if (ObjMaster.Current.AttributeValues.ToStr().Trim().Length > 0)
                            {
                                string[] bookingAttrs  = ObjMaster.Current.AttributeValues.ToStr().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                string   drvAttributes = ObjDriver.AttributeValues.ToStr() + "," + ObjDriver.Fleet_VehicleType.AttributeValues;

                                int      totalAttr          = bookingAttrs.Count();
                                int      matchCnt           = 0;
                                string   unmatchedAttrValue = string.Empty;
                                string[] drvAttrsArr        = drvAttributes.ToStr().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                                foreach (var item in bookingAttrs)
                                {
                                    if (drvAttrsArr.Count(c => c.ToLower() == item.ToLower()) > 0)
                                    {
                                        matchCnt++;
                                    }
                                    else
                                    {
                                        unmatchedAttrValue += item + ",";
                                    }
                                }

                                if (matchCnt != totalAttr)
                                {
                                    if (unmatchedAttrValue.EndsWith(","))
                                    {
                                        unmatchedAttrValue = unmatchedAttrValue.Substring(0, unmatchedAttrValue.LastIndexOf(","));
                                    }

                                    MessageBox.Show(("Driver : " + ObjDriver.DriverNo + " doesn't have attributes (" + unmatchedAttrValue + ")"), "Warning");
                                    return;
                                }
                            }

                            if (AppVars.listUserRights.Count(c => c.functionId == "RESTRICT ON DESPATCH JOB TO INVALID VEHICLE DRIVER") > 0)
                            {
                                string vehAttributes = ObjMaster.Current.Fleet_VehicleType.DefaultIfEmpty().AttributeValues.ToStr().Trim();

                                if (vehAttributes.Length > 0)
                                {
                                    bool MatchedAttr = false;
                                    foreach (var item in vehAttributes.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                                    {
                                        if (ObjDriver.VehicleTypeId.ToInt() == item.ToInt())
                                        {
                                            MatchedAttr = true;
                                            break;
                                        }
                                    }



                                    if (MatchedAttr == false)
                                    {
                                        MessageBox.Show("This Job is for " + ObjMaster.Current.Fleet_VehicleType.VehicleType.ToStr() + " Vehicle" + Environment.NewLine +
                                                        "and Driver no " + ObjDriver.DriverNo + " have " + ObjDriver.Fleet_VehicleType.VehicleType + ".");

                                        return;
                                    }
                                }
                                else
                                {
                                    if (ObjDriver.Fleet_VehicleType.NoofPassengers.ToInt() < ObjMaster.Current.Fleet_VehicleType.NoofPassengers.ToInt())
                                    {
                                        MessageBox.Show("This Job is for " + ObjMaster.Current.Fleet_VehicleType.VehicleType.ToStr() + " Vehicle" + Environment.NewLine +
                                                        "and Driver no " + ObjDriver.DriverNo + " have " + ObjDriver.Fleet_VehicleType.VehicleType + ".");


                                        return;
                                    }
                                }
                            }
                            else
                            {
                                if (ObjDriver.Fleet_VehicleType.NoofPassengers.ToInt() < ObjMaster.Current.Fleet_VehicleType.NoofPassengers.ToInt())
                                {
                                    if (DialogResult.No == MessageBox.Show("This Job is for " + ObjMaster.Current.Fleet_VehicleType.VehicleType.ToStr() + " Vehicle" + Environment.NewLine +
                                                                           "and Driver no " + ObjDriver.DriverNo + " have " + ObjDriver.Fleet_VehicleType.VehicleType + "." + Environment.NewLine
                                                                           + "Do you still want to Allocate this Job to that Driver " + ObjDriver.DriverNo + " ?", "Despatch", MessageBoxButtons.YesNo))
                                    {
                                        return;
                                    }
                                }
                            }
                        }
                    }

                    try
                    {
                        if ((driverId != null && ObjMaster.Current.DriverId == null) || (driverId != null && ObjMaster.Current.DriverId != null && driverId != ObjMaster.Current.DriverId))
                        {
                            if (IsDriverDocumentExpired(driverId.ToInt(), ObjDriver))
                            {
                                return;
                            }
                        }
                    }
                    catch
                    {
                    }
                }



                if (ObjMaster.Current != null)
                {
                    if (driverId != null || (ObjMaster.Current.DriverId != null && ObjMaster.Current.BookingStatusId.ToInt() == Enums.BOOKINGSTATUS.WAITING))
                    {
                        //if ((driverId != null && ObjMaster.Current.DriverId == null) || (driverId!=null && ObjMaster.Current.DriverId!=null && driverId!=ObjMaster.Current.DriverId))
                        //{

                        //   if  (IsDriverDocumentExpired(driverId.ToInt()))
                        //       return;

                        //}


                        if (driverId == null && ObjMaster.Current.DriverId != null)
                        {
                            oldDriverNo = ObjMaster.Current.Fleet_Driver.DriverNo.ToStr();
                        }


                        if (ObjMaster.Current.DriverId != null)
                        {
                            oldDriverId = ObjMaster.Current.DriverId;
                        }


                        ObjMaster.CheckDataValidation = false;

                        ObjMaster.Edit();

                        ObjMaster.Current.DriverId = driverId;

                        ObjMaster.Current.IsConfirmedDriver = driverId != null ? chkConfirmed.Checked:false;



                        if (ObjMaster.Current.BookingStatusId.ToInt() == Enums.BOOKINGSTATUS.NOTACCEPTED)
                        {
                            ObjMaster.Current.BookingStatusId = Enums.BOOKINGSTATUS.WAITING;
                        }



                        if (driverId == null || (oldDriverId != null && oldDriverId != driverId && ObjMaster.Current.BookingStatusId != Enums.BOOKINGSTATUS.WAITING))
                        {
                            ObjMaster.Current.BookingStatusId = Enums.BOOKINGSTATUS.WAITING;
                            cancelJob = true;
                        }


                        jobId          = ObjMaster.Current.Id;
                        allocatedJobId = jobId;

                        ObjMaster.CheckCustomerValidation = false;
                        ObjMaster.DisableUpdateReturnJob  = true;

                        ObjMaster.Save();


                        if (ObjMaster.Current.BookingTypeId.ToInt() == Enums.BOOKING_TYPES.THIRDPARTY && ObjMaster.Current.OnlineBookingId != null)
                        {
                            General.UpdateThirdPartyJobStatus(null, jobId, "allocated");
                        }

                        this.Close();


                        if (IsOpenFrom == 1)
                        {
                            RefreshTodayBookingsDashboard();
                        }
                        else
                        {
                            RefreshTodayAndPreBookingsDashboard();
                            //  AppVars.frmMDI.RefreshTodayAnPreDashboard();
                        }

                        string Msg = string.Empty;


                        if (driverId != null)
                        {
                            if (chkConfirmed.Checked)
                            {
                                Msg = "Job is Allocated and confirmed to Driver (" + ObjMaster.Current.Fleet_Driver.DriverNo.ToStr() + ")";
                            }
                            else
                            {
                                Msg = "Job is Allocated  to Driver (" + ObjMaster.Current.Fleet_Driver.DriverNo.ToStr() + ")";
                            }
                        }
                        else if (driverId == null && !string.IsNullOrEmpty(oldDriverNo))
                        {
                            Msg = "Job is De-Allocated from Driver (" + oldDriverNo + ")";
                        }

                        using (TaxiDataContext db = new TaxiDataContext())
                        {
                            db.stp_BookingLog(ObjMaster.Current.Id, AppVars.LoginObj.UserName.ToStr(), Msg);


                            if (cancelJob)
                            {
                                if (AppVars.objPolicyConfiguration.DespatchOfflineJobs.ToBool())
                                {
                                    db.stp_DeleteDrvOfflineJob(ObjMaster.Current.Id, oldDriverId);
                                }
                            }
                        }



                        if (cancelJob)
                        {
                            //For TCP Connection
                            if (AppVars.objPolicyConfiguration.IsListenAll.ToBool())
                            {
                                new Thread(delegate()
                                {
                                    General.SendMessageToPDA("request pda=" + oldDriverId + "=" + jobId + "=Cancelled Pre Job>>" + jobId + "=2");
                                }).Start();
                            }
                        }
                    }
                    else
                    {
                        ENUtils.ShowMessage("Required: Driver");
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }