void bw2_RunWorkerCompleted(object sender,
                           RunWorkerCompletedEventArgs e)
        {
            bool dontoutputflag=false;
            try
            {
                DirectionArgs args = (DirectionArgs)e.Result;
                if (args.counter == counter && args.counter != -1)
                {
                    MapWriter mapwriter = new MapWriter();
                    int numberdisplayed = 0;
                    for (int i = 0; i < args.display.Count;i++ )
                    {
                        if (args.display[i] == true)
                        {
                            numberdisplayed++;
                        }
                    }

                    List<float>[] points_shape = new List<float>[numberdisplayed];
                    List<int> color = new List<int>();
                    List<double>[] points_latitude = new List<double>[numberdisplayed];
                    List<int>[] sequence = new List<int>[numberdisplayed];
                    List<double>[] points_longitude = new List<double>[numberdisplayed];
                    int index = 0;

                        for (int i = 0; i < args.latitude.Count; i++)
                        {

                            if (args.display[i] == true)
                            {
                                color.Add(-1);
                                points_shape[index] = args.directiondata[i].routeshape;
                                points_latitude[index] = args.directiondata[i].latitude;
                                points_longitude[index] = args.directiondata[i].longitude;
                                sequence[index] = args.directiondata[i].locationSequence;
                                color[index]=args.color[i];
                                index++;
                            }

                        }
                    mapwriter.WriteMap(numberdisplayed, color, null, points_shape, points_latitude, points_longitude, sequence, 420, 668);
                    string currentdirectory = Directory.GetCurrentDirectory();
                    currentdirectory = currentdirectory.Replace("\\", "/");
                    currentdirectory = Uri.EscapeUriString(currentdirectory);
                    string tovisit = "file:///" + currentdirectory + "/" + "map.html";
                    if (this.IsDisposed == false)
                    {
                        webkitBrowser2.Navigate(tovisit);
                    }
                    int totalmiles = 0;
                    long totaltime = 0;
                    totaltimeindividual = new int[args.directiondata.Count];
                    totalmilesindividual = new int[args.directiondata.Count];
                    for (int l = 0; l < args.directiondata.Count; l++)
                    {
                        for (int i = 0; i < args.directiondata[l].distance.Count; i++)
                        {
                            totalmilesindividual[l] += (int)args.directiondata[l].distance[i];
                            totalmiles += (int)args.directiondata[l].distance[i];

                            totaltimeindividual[l] += (int)args.directiondata[l].time[i];
                            totaltime += (int)args.directiondata[l].time[i];
                        }
                    }
                    textTotalMiles.Text = totalmiles + " Mi";
                    //weekly miles
                    drivercost = new List<double>();
                    milecost = new List<double>();
                    maitenancecost = new List<double>();
                    double estimatedcost = 0 ;
                    driverestimatedcost=new int[routes.Count];
                    individualstopcounter = new int[routes.Count];
                    try
                    {
                        for (int l = 0; l < routes.Count; l++)
                        {
                            if (routes[l].invoicerouteinfo.Count > 0)
                            {
                                int index2 = 0;
                                for (int k = 0; k < scheduleddrivers.Count; k++)
                                {
                                    if (scheduleddrivers[k].number == routes[l].drivernumber)
                                    {
                                        index2 = k;
                                    }
                                }
                                DayOfWeek startOfWeek = DayOfWeek.Sunday;

                                int diff = viewdate.DayOfWeek - startOfWeek;
                                if (diff < 0)
                                {
                                    diff += 7;
                                }
                                DateTime aday = new DateTime();
                                aday = viewdate.AddDays(-1 * diff).Date;
                                int weeklymiles = 0;
                                for (int i = 0; i <= diff; i++)
                                {
                                    List<RouteInfo> routesforday = mysql_routes.GetRouteInfo(aday);
                                    for (int j = 0; j < routesforday.Count; j++)
                                    {
                                        if (routes[index2].drivernumber == routesforday[j].drivernumber)
                                        {
                                            weeklymiles += routesforday[j].miles;
                                        }
                                    }
                                    aday = aday.AddDays(1);
                                }
                                int stopcounter = 0;
                                for (int k = 0; k < args.directiondata[l].time.Count; k++)
                                {
                                    if (args.directiondata[l].time[k] != 0)
                                    {
                                        stopcounter++;
                                    }
                                }
                                stopcounter--;
                                totaltimeindividual[l] += stopcounter * 1800;
                                individualstopcounter[l] = stopcounter;
                                drivercost.Add((totaltimeindividual[l]/3600 )* scheduleddrivers[l].hour_rate);
                                milecost.Add(totalmilesindividual[l] * scheduleddrivers[l].fuel_surch);
                                maitenancecost.Add(0);
                                if (totalmilesindividual[l] > 100)
                                {
                                    maitenancecost[l] = (totalmilesindividual[l] - 100) * scheduleddrivers[l].maint_surch;
                                }
                                estimatedcost += drivercost[l] + milecost[l] + maitenancecost[l];
                                driverestimatedcost[l] = (int)drivercost[l] + (int)milecost[l] + (int)maitenancecost[l];
                            }
                        }
                    }
                    catch { dontoutputflag = true; }
                    try
                    {
                        if (dontoutputflag == false)
                        {
                            textEstimatedCost.Text = DataFormat.FormatMoneyToString((int)estimatedcost);
                            double percent = estimatedcost / (double)totalvalue;
                            textCostPercentage.Text = percent.ToString("0.00%");
                        }
                    }
                    catch { }
                }

            }

            catch
            {
                string currentdirectory = Directory.GetCurrentDirectory();
                currentdirectory = currentdirectory.Replace("\\", "/");
                currentdirectory = Uri.EscapeUriString(currentdirectory);
                string tovisit = "file:///" + currentdirectory + "/" + "error.html";
                try
                {
                    if (this.IsDisposed == false)
                    {
                        webkitBrowser2.Navigate(tovisit);
                    }

                }
                catch { }
            }
        }
        private void RefreshCompanyDrivers()
        {
            try
            {
                companydrivers = new List<DriverInfo>();
                scheduleddrivers.Clear();
                companydrivers = mysql_drivers.GetDrivers();//change removed routes local var
                //List<RouteInfo> routes = mysql_routes.GetRouteInfo(viewdate);
                listCompanyDrivers.Items.Clear();
                listScheduledDrivers.Items.Clear();
                int colorindex = 0;
                int index = 0;
                for (int i = 0; i < companydrivers.Count; i++)
                {
                    string toinsert = companydrivers[i].name;
                    if (companydrivers[i].flat == true)
                    {
                        toinsert = toinsert + " - Flat";
                    }
                    if (companydrivers[i].trailer == true)
                    {
                        toinsert = toinsert + " - Trailer";
                    }
                    bool flag = false;
                    for (int j = 0; j < routes.Count; j++)
                    {

                        if (routes[j].drivernumber == companydrivers[i].number)
                        {
                            MapWriter mapwriter = new MapWriter();
                            if (routes[j].invoicerouteinfo.Count == 0)
                            {
                                displayroute[index] = false;
                            }
                            if (displayroute[index] == true)
                            {
                                if (mapwriter.colors[colorindex][0] != '#')
                                {
                                    toinsert = toinsert + " - " + mapwriter.colors[colorindex];
                                }
                                else
                                {
                                    if (mapwriter.colors[colorindex] == "#000000")
                                    {
                                        toinsert = toinsert + " - " + "black";
                                    }
                                    if (mapwriter.colors[colorindex] == "#A0522D")
                                    {
                                        toinsert = toinsert + " - " + "brown";
                                    }
                                    if (mapwriter.colors[colorindex] == "#8B0000")
                                    {
                                        toinsert = toinsert + " - " + "red";
                                    }
                                    if (mapwriter.colors[colorindex] == "#00FF00")
                                    {
                                        toinsert = toinsert + " - " + "lime";
                                    }
                                    if (mapwriter.colors[colorindex] == "#191970")
                                    {
                                        toinsert = toinsert + " - " + "dark blue";
                                    }

                                }
                                colorindex++;
                            }
                            index++;
                            listScheduledDrivers.Items.Add(toinsert);
                            j = routes.Count;
                            flag = true;
                            scheduleddrivers.Add(companydrivers[i]);
                            companydrivers.RemoveAt(i);
                            i = i - 1;
                        }

                    }
                    if (flag == false)
                    {
                        listCompanyDrivers.Items.Add(toinsert);
                    }

                }

                listCompanyDrivers.SelectedIndex = 0;
            }
            catch { }
        }
 void bw_RunWorkerCompleted(object sender,
                            RunWorkerCompletedEventArgs e)
 {
     GeoData currentgeoddata=(GeoData)e.Result;
     toolStripStatusLabel1.Text = "Search completed.";
     FindLocation.Enabled = true;
     FindLocation.Text = "Search for Addrress";
     RefreshLocations();
     Map.MapWriter mapwriter = new Map.MapWriter();
     List<Double>[] latitude=new List<Double>[1];
     List<Double>[] longitude=new List<Double>[1];
     latitude[0] = new List<Double>();
     longitude[0] = new List<Double>();
     for(int i=0;i<currentgeodata.latitude.Count;i++)
     {
         latitude[0].Add(currentgeodata.latitude[i]);
     longitude[0].Add(currentgeodata.longitude[i]);
     }
     mapwriter.WriteMap(1, null, null, null, latitude, longitude, null, 420, 668);
                 string currentdirectory = Directory.GetCurrentDirectory();
     currentdirectory = currentdirectory.Replace("\\", "/");
     currentdirectory = Uri.EscapeUriString(currentdirectory);
     string tovisit = "file:///" + currentdirectory + "/" + "map.html";
     webkitBrowser1.Navigate(tovisit);
 }
        void bw2_RunWorkerCompleted(object sender,
                                   RunWorkerCompletedEventArgs e)
        {
            try
            {
                DirectionArgs args = (DirectionArgs)e.Result;
                if (args.counter == counter&&args.counter!=-1)
                {
                    MapWriter mapwriter = new MapWriter();
                    List<float>[] points_shape = new List<float>[1];
                    points_shape[0] = args.directiondata.routeshape;
                    List<double>[] points_latitude = new List<double>[1];
                    points_latitude[0] = args.directiondata.latitude;
                    List<double>[] points_longitude = new List<double>[1];
                    points_longitude[0] = args.directiondata.longitude;
                    List<int>[] sequence = new List<int>[1];
                    sequence[0] = args.directiondata.locationSequence;
                    mapwriter.WriteMap(1, null, null, points_shape, points_latitude, points_longitude, sequence, 420, 668);
                    string currentdirectory = Directory.GetCurrentDirectory();
                    currentdirectory = currentdirectory.Replace("\\", "/");
                    currentdirectory = Uri.EscapeUriString(currentdirectory);
                    string tovisit = "file:///" + currentdirectory + "/" + "map.html";
                    if (this.IsDisposed == false)
                    {
                        webkitBrowser.Navigate(tovisit);
                    }
                    //update miles

                    int totalmiles=0;
                    for(int i=0;i<args.directiondata.distance.Count;i++)
                    {
                        totalmiles+=(int)args.directiondata.distance[i];
                    }
                    viewroute.miles=totalmiles;
                    mysql_routes.updateMileage(viewroute);
                    //weekly miles
                    DayOfWeek startOfWeek = DayOfWeek.Sunday;

                    int diff = viewdate.DayOfWeek - startOfWeek;
                    if (diff < 0)
                    {
                        diff += 7;
                    }
                    DateTime aday = new DateTime();
                    aday=viewdate.AddDays(-1 * diff).Date;
                    int weeklymiles = 0;
                    for (int i = 0; i <= diff; i++)
                    {
                        List <RouteInfo> routesforday=mysql_routes.GetRouteInfo(aday);
                        for (int j = 0; j < routesforday.Count; j++)
                        {
                            if (routesforday[j].drivernumber == viewroute.drivernumber)
                            {
                                weeklymiles += routesforday[j].miles;
                            }
                        }
                            aday = aday.AddDays(1);
                    }

                    long totaltime = 0;
                    for(int i=0;i<args.directiondata.time.Count;i++)
                    {
                        totaltime+=(int)args.directiondata.time[i];
                    }
                    totaltime += stopcounter * 1800;
                    textTotalMiles.Text = totalmiles + " Mi";
                    textTotalTime.Text=DataFormat.FormatTimeToString(totaltime);
                    double drivercost=(totaltime/3600)*driver.hour_rate;
                    double milecost = totalmiles * driver.fuel_surch;
                    double maitenancecost=0;
                    if (totalmiles > 100)
                    {
                        maitenancecost = (totalmiles - 100) * driver.maint_surch;
                    }
                    double estimatedcost = drivercost + milecost + maitenancecost;
                    textEstimatedCost.Text = DataFormat.FormatMoneyToString((int)estimatedcost);
                    double percent=estimatedcost/(double)totalvalue;
                    textCostPercentage.Text = percent.ToString("0.00%");
                }
                else
                {
                    string currentdirectory = Directory.GetCurrentDirectory();
                    currentdirectory = currentdirectory.Replace("\\", "/");
                    currentdirectory = Uri.EscapeUriString(currentdirectory);
                    string tovisit = "file:///" + currentdirectory + "/" + "error.html";
                    if (this.IsDisposed == false)
                    {
                        webkitBrowser.Navigate(tovisit);
                    }
                        textTotalMiles.Text = "";
                    textTotalTime.Text = "";
                }
            }
            catch
            {
                string currentdirectory = Directory.GetCurrentDirectory();
                currentdirectory = currentdirectory.Replace("\\", "/");
                currentdirectory = Uri.EscapeUriString(currentdirectory);
                string tovisit = "file:///" + currentdirectory + "/" + "error.html";
                try
                {
                    if (this.IsDisposed == false)
                    {
                        webkitBrowser.Navigate(tovisit);
                    }
                }
                catch { }
            }
        }
        void bw2_RunWorkerCompleted(object sender,
                           RunWorkerCompletedEventArgs e)
        {
            try
            {
                DirectionArgs args = (DirectionArgs)e.Result;
                if (args.counter == counter && args.counter != -1)
                {
                    MapWriter mapwriter = new MapWriter();
                    List<float>[] points_shape = new List<float>[1];
                    points_shape[0] = args.directiondata.routeshape;
                    List<double>[] points_latitude = new List<double>[1];
                    points_latitude[0] = args.directiondata.latitude;
                    List<double>[] points_longitude = new List<double>[1];
                    points_longitude[0] = args.directiondata.longitude;
                    List<int>[] sequence = new List<int>[1];
                    sequence[0] = args.directiondata.locationSequence;
                    mapwriter.WriteMap(1, null, null, points_shape, points_latitude, points_longitude, sequence, 403, 560);
                    string currentdirectory = Directory.GetCurrentDirectory();
                    currentdirectory = currentdirectory.Replace("\\", "/");
                    currentdirectory = Uri.EscapeUriString(currentdirectory);
                    string tovisit = "file:///" + currentdirectory + "/" + "map.html";
                    if (this.IsDisposed == false)
                    {
                        webKitBrowser1.Navigate(tovisit);
                    }
                    //update miles

                    int totalmiles = 0;
                    for (int i = 0; i < args.directiondata.distance.Count; i++)
                    {
                        totalmiles += (int)args.directiondata.distance[i];
                    }

                    long totaltime = 0;
                    for (int i = 0; i < args.directiondata.time.Count; i++)
                    {
                        totaltime += (int)args.directiondata.time[i];
                    }

                    textDistance1.Text = totalmiles + " Mi";
                    textTime1.Text = DataFormat.FormatTimeToString(totaltime);
                    textInvoices.Text = optimizedroutes[listRoutes.SelectedIndex].Count.ToString();

                }
                else
                {
                    string currentdirectory = Directory.GetCurrentDirectory();
                    currentdirectory = currentdirectory.Replace("\\", "/");
                    currentdirectory = Uri.EscapeUriString(currentdirectory);
                    string tovisit = "file:///" + currentdirectory + "/" + "blank.html";
                    if (this.IsDisposed == false)
                    {
                        webKitBrowser1.Navigate(tovisit);
                    }
                    textDistance1.Text = "";
                    textTime1.Text = "";
                    textInvoices.Text = "";
                }
            }
            catch
            {
                string currentdirectory = Directory.GetCurrentDirectory();
                currentdirectory = currentdirectory.Replace("\\", "/");
                currentdirectory = Uri.EscapeUriString(currentdirectory);
                string tovisit = "file:///" + currentdirectory + "/" + "blank.html";
                try
                {
                    if (this.IsDisposed == false)
                    {
                        webKitBrowser1.Navigate(tovisit);
                    }
                }
                catch { }
            }
        }