public WindowAssignClientsToDeliverer(Deliverer selectedDeliverer)
        {
            //Initialize variables
            currentDeliverer = new List<Deliverer>();
            zipcodesList = new List<string>();

            InitializeComponent();

            //Update ZipcodeListBox
            UpdateZipcodeListBox();

            //Update Data Grid
            UpdateDelivererInfoDataGrid(selectedDeliverer);

            //Generate Deliverer Assignment Map
            GenerateDelivererAssignmentMap(googleMapControlHtmFile, selectedDeliverer, "None");

            //Initialize Map Browser - Jerk
            InitializeMapBrowser(googleMapControlHtmFile);
        }
        private void PrepareMapGenerationArguments(ref string[] clients, ref string[] addresses, ref string[] clientsAssigned, ref string[] clientIDs, ref string showMarkerMode, ref Deliverer currentDeliverer, ref string clientZipcode)
        {
            if (Main.mSelectedYear == "NONE")
            {
                System.Windows.MessageBox.Show("Open a database and select a year.");
                return;
            }

            //Get showMarkerMode
            showMarkerMode = RefreshMapListBox.SelectedIndex.ToString();

            //Define local variables
            int clientCountByShowMarkerModeAndZipcode = GetClientCountByShowMarkerModeAndZipcode(showMarkerMode, clientZipcode, currentDeliverer);

            //Create the select selectByClient_IDQueryText
            string selectByClient_IDQueryText = "SELECT * FROM " + Main.mSelectedYear;

            //Get the list of clients

            //Create the clientIDList
            DataSet clientIDList = Main.mChristmasBasketsAccessDatabase.PerformSelectQuery(selectByClient_IDQueryText, Main.mSelectedYear);

            int index = 0;

            if (clientZipcode == "None")
            {
                //Return as we do not want any clients as the zipcode is None
                //Create new arrays
                return;
            }

            //Create new arrays
            clients = new string[clientCountByShowMarkerModeAndZipcode];
            addresses = new string[clientCountByShowMarkerModeAndZipcode];
            clientsAssigned = new string[clientCountByShowMarkerModeAndZipcode];
            clientIDs = new string[clientCountByShowMarkerModeAndZipcode];

            //Process each record in the clientIDList table
            foreach (DataRow dataRow in clientIDList.Tables[0].Rows)
            {
                //Create the selectByClientIDQueryText
                string selectByClientIDQueryText = "";

                if (clientZipcode == "All")
                {
                    //All zipcodes
                    selectByClientIDQueryText = "SELECT Client_ID, Last_Name, First_Name, Address_Number, Street_Address, City, Zipcode FROM Clients WHERE Client_ID = " + dataRow["Client_ID"].ToString();
                }
                else
                {
                    //Specific zipcode
                    selectByClientIDQueryText = "SELECT Client_ID, Last_Name, First_Name, Address_Number, Street_Address, City, Zipcode FROM Clients WHERE Client_ID = " + dataRow["Client_ID"].ToString() + " AND Zipcode = \"" + clientZipcode + "\"";
                }

                //Perform the selectByClientIDAndOrganizationQuery and store the results in a Data Table
                System.Data.DataSet selectByClientIDDataSet = Main.mChristmasBasketsAccessDatabase.PerformSelectQuery(selectByClientIDQueryText, "Clients");

                //Check to see we got results back
                if (selectByClientIDDataSet != null)
                {
                    //Format the strings properly
                    string id = selectByClientIDDataSet.Tables[0].Rows[0]["Client_ID"].ToString();
                    string firstName = selectByClientIDDataSet.Tables[0].Rows[0]["First_Name"].ToString();
                    string lastName = selectByClientIDDataSet.Tables[0].Rows[0]["Last_Name"].ToString();
                    string addressNumber = selectByClientIDDataSet.Tables[0].Rows[0]["Address_Number"].ToString();
                    string streetAddress = selectByClientIDDataSet.Tables[0].Rows[0]["Street_Address"].ToString();
                    string city = selectByClientIDDataSet.Tables[0].Rows[0]["City"].ToString();
                    string zipcode = selectByClientIDDataSet.Tables[0].Rows[0]["Zipcode"].ToString();
                    string assignedStatus = "";

                    string[] clientList = currentDeliverer.Clients.Split(',');
                    bool clientAlreadyAssigned = false;

                    foreach (string client in clientList)
                    {
                        if (client != "")
                        {
                            if (client == id)
                            {
                                clientAlreadyAssigned = true;
                                break;
                            }
                        }
                    }

                    //Determine if client is assigned to the current deliverer which is the target of the .htm file being investigatedv
                    if (clientAlreadyAssigned)
                    {
                        //Deliverer is not assigned to this deliverer
                        assignedStatus = "1";
                    }
                    else
                    {
                        if (dataRow["Assigned_Status"].ToString() == "true")
                        {
                            //Client is assigned to another deliverer other than the current deliverer
                            assignedStatus = "2";
                        }
                        else
                        {
                            //Client is not assigned to any deliverer
                            assignedStatus = "0";
                        }
                    }

                    bool addClient = false;

                    //Determine if we add the client

                    //Show Clients Assigned to Current Deliverer, Unassigned, and Assigned to Other Deliverers
                    if (showMarkerMode == "0")
                    {
                        addClient = true;
                    }
                    //Show Clients Assigned to Current Deliverer and Other Deliverers
                    else if (showMarkerMode == "1" && (assignedStatus == "1" || assignedStatus == "2"))
                    {
                        addClient = true;
                    }
                    //Show Unassigned Clients
                    else if (showMarkerMode == "2" && (assignedStatus == "0"))
                    {
                        addClient = true;
                    }
                    //Show Clients Assigned to Current Deliverer and Unassigned Clients
                    else if (showMarkerMode == "3" && (assignedStatus == "1" || assignedStatus == "0"))
                    {
                        addClient = true;
                    }
                    //Show Clients only assigned to Current Deliverer
                    else if (showMarkerMode == "4" && assignedStatus == "1")
                    {
                        addClient = true;
                    }

                    //Determine if we add the client
                    if (addClient)
                    {
                        clients[index] = id + " - " + lastName + ", " + firstName;
                        addresses[index] = addressNumber + " " + streetAddress + " " + city + ", VA  " + zipcode;
                        clientsAssigned[index] = assignedStatus;
                        clientIDs[index] = id;

                        index++;
                    }
                }

                if (index == clientCountByShowMarkerModeAndZipcode)
                {
                    break;
                }
            }
        }
        private void UpdateDelivererInfoDataGrid(Deliverer selectedDeliverer)
        {
            //Initialize variables
            string delivererIDQuery = "SELECT * FROM Deliverers WHERE Deliverer_ID = " + selectedDeliverer.DelivererID;

            //Clear currentDeliverer
            currentDeliverer.Clear();

            //Get all the selectedDelivererInfo
            DataSet selectedDelivererInfo = Main.mChristmasBasketsAccessDatabase.PerformSelectQuery(delivererIDQuery, Main.mSelectedYear + "_Deliverers");

            //See if we got a hit in the database
            if(selectedDelivererInfo.Tables[0].Rows.Count == 1)
            {
                //Single record found in the database
                DataRow delivererInfoFromDatabase = selectedDelivererInfo.Tables[0].Rows[0];

                Deliverer delivererToAdd = new Deliverer();

                delivererToAdd.DelivererID = Convert.ToInt32(delivererInfoFromDatabase["Deliverer_ID"]);
                delivererToAdd.FirstName = delivererInfoFromDatabase["First_Name"].ToString();
                delivererToAdd.LastName = delivererInfoFromDatabase["Last_Name"].ToString();
                delivererToAdd.Capacity = Convert.ToInt32(delivererInfoFromDatabase["Capacity"]);
                delivererToAdd.HelpStatus = delivererInfoFromDatabase["Help_Status"].ToString();
                delivererToAdd.Room = delivererInfoFromDatabase["Room"].ToString();
                delivererToAdd.WorkPhone = delivererInfoFromDatabase["Work_Phone"].ToString();
                delivererToAdd.HomePhone = delivererInfoFromDatabase["Home_Phone"].ToString();
                delivererToAdd.OccupationStatus = delivererInfoFromDatabase["Occupation_Status"].ToString();
                delivererToAdd.Comments = delivererInfoFromDatabase["Comments"].ToString();
                delivererToAdd.ClientHistory = delivererInfoFromDatabase["Client_History"].ToString();
                delivererToAdd.YearLastDelivered = delivererInfoFromDatabase["Year_Last_Delivered"].ToString();

                if (delivererInfoFromDatabase["Assigned"].ToString() == "")
                {
                    delivererInfoFromDatabase["Assigned"] = 0;
                }
                else
                {
                    delivererToAdd.Assigned = Convert.ToInt32(delivererInfoFromDatabase["Assigned"]);
                }

                //Get Clients from Year_XXXX_Clients Table
                string getDelivererClientsQuery = "SELECT Clients FROM " + Main.mSelectedYear + "_Deliverers WHERE Deliverer_ID = " + delivererInfoFromDatabase["Deliverer_ID"];

                DataSet delivererClientsBeingProcessed = Main.mChristmasBasketsAccessDatabase.PerformSelectQuery(getDelivererClientsQuery, Main.mSelectedYear + "_Deliverers");

                DataRow delivererClientsFromDatabase = delivererClientsBeingProcessed.Tables[0].Rows[0];

                delivererToAdd.Clients = delivererClientsFromDatabase["Clients"].ToString();

                currentDeliverer.Add(delivererToAdd);
            }
            else
            {
                MessageBox.Show("Deliverer_ID " + selectedDeliverer.DelivererID + " not found in the database!");
            }

            DataContext = this;

            currentDelivererCollectionView = CollectionViewSource.GetDefaultView(currentDeliverer);
        }
        private void GenerateDelivererAssignmentMap(string filename, Deliverer currentDeliverer, string clientZipcode)
        {
            //Define Map Generation Arguments
            string[] clients = null;
            string[] addresses = null;
            string[] clientsAssigned = null;
            string[] clientIDs = null;
            string showMarkerMode = "0";

            string currentDirectory = Directory.GetCurrentDirectory();
            string fullFilePath = currentDirectory + filename;

            //PrepareMapGenerationArguments
            PrepareMapGenerationArguments(ref clients, ref addresses, ref clientsAssigned, ref clientIDs, ref showMarkerMode, ref currentDeliverer, ref clientZipcode);


            //Update TotalClientsToDisplayValueLabel
            if (clients != null)
            {
                TotalClientsToDisplayValueLabel.Content = clients.Length.ToString();
            }
            else 
            {
                TotalClientsToDisplayValueLabel.Content = "0";
            }

            //Generate the Deliverer Assignment Map
            //Define local variables
            TextWriter textWriter = new StreamWriter(fullFilePath);

            //Write the header part of the .htm page
            textWriter.WriteLine("<!DOCTYPE html>");
            textWriter.WriteLine("<html>");
            textWriter.WriteLine("   <head>");
            textWriter.WriteLine("      <meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\"/>");
            textWriter.WriteLine("      <style type=\"text/css\">");
            textWriter.WriteLine("         html { height: 100% }");
            textWriter.WriteLine("         body { height: 100%; margin: 0; padding: 0 }");
            textWriter.WriteLine("         #map_canvas { height: 100% }");
            textWriter.WriteLine("      </style>");

            //Write the java script part of the .htm page
            textWriter.WriteLine("      <!--[if IE]> <script type=\"text/javascript\" src=\"ie-set_timeout.js\"></script> <![endif]-->");
            textWriter.WriteLine("      <script type=\"text/javascript\" src=\"http://maps.google.com/maps/api/js?sensor=false\"></script>");
            textWriter.WriteLine("      <script type=\"text/javascript\">");
            textWriter.WriteLine("");
            textWriter.WriteLine("      var geocoder;");
            textWriter.WriteLine("      var map;");
            textWriter.WriteLine("      var markers = [];");
            textWriter.WriteLine("      var showMarkerMode = " + showMarkerMode + ";	//0 = All Clients, 1 = Only Assigned Clients, 2 = Only Unassigned Clients, 3 = Current Deliverer Assigned and Unassigned Clients, 4 = Only Current Deliverer Assigned Clients");
            textWriter.WriteLine("      var assignedClientToCurrentDelivererImage = \"AssignedClientToCurrentDeliverer.png\";");
            textWriter.WriteLine("      var assignedClientToOtherDelivererImage = \"AssignedClientToOtherDeliverer.png\";");
            textWriter.WriteLine("      var unassignedClientImage = \"UnassignedClient.png\";");
            textWriter.WriteLine("      var mapCenterAddress = \"2147 Dale Avenue Southeast Roanoke, VA 24013\";");

            //Generate addresses and clients arrays
            string javaClients =            "      var clients = [";
            string javaAddresses =          "      var addresses = [";
            string javaClientsAssigned =    "      var clientsAssigned = [";
            string javaClientIDs =          "      var clientIDs = [";

            if (clientZipcode == "None")
            {
                //No clients wanted to be shown
                javaClients += "];";
                javaAddresses += "];";
                javaClientsAssigned += "];";  //0 - Client unassigned, 1 - client assigned to current deliverer the htm file represents, 2 - client assigned to another deliverer other than current deliverer htm file represents";
                javaClientIDs += "];";
            }
            else
            {
                //See if we have 1 or more than 1 client to process
                if (clients.Count() == 1)
                {
                    //One client
                    javaClients += "\"" + clients[0] + "\"];";
                    javaAddresses += "\"" + addresses[0] + "\"];";
                    javaClientsAssigned += "\"" + clientsAssigned[0] + "\"];";
                    javaClientIDs += "\"" + clientIDs[0] + "\"];";
                }
                else
                {
                    //More than one client
                    for (int i = 0; i < clients.Count(); i++)
                    {
                        //Insert clients
                        if (i == (clients.Count() - 1))
                        {
                            //Last Record
                            javaClients += "\"" + clients[i] + "\"];";
                            javaAddresses += "\"" + addresses[i] + "\"];";
                            javaClientsAssigned += "\"" + clientsAssigned[i] + "\"];  //0 - Client unassigned, 1 - client assigned to current deliverer the htm file represents, 2 - client assigned to another deliverer other than current deliverer htm file represents";
                            javaClientIDs += "\"" + clientIDs[i] + "\"];";
                        }
                        else
                        {
                            //Not the Last Record
                            javaClients += "\"" + clients[i] + "\",";
                            javaAddresses += "\"" + addresses[i] + "\",";
                            javaClientsAssigned += "\"" + clientsAssigned[i] + "\",";
                            javaClientIDs += "\"" + clientIDs[i] + "\",";
                        }
                    }
                }
            }
            //Insert addresses array
            textWriter.WriteLine(javaAddresses);

            //Insert clients array
            textWriter.WriteLine(javaClients);

            //Insert clientsAssigned array
            textWriter.WriteLine(javaClientsAssigned);

            //Insert clientIDs array
            textWriter.WriteLine(javaClientIDs);
            textWriter.WriteLine("");

            //initialize function
            textWriter.WriteLine("      function initialize()");
            textWriter.WriteLine("      {");
            textWriter.WriteLine("         //Create map options");
            textWriter.WriteLine("         var myOptions = {mapTypeId: google.maps.MapTypeId.ROADMAP};");
            textWriter.WriteLine("");
            textWriter.WriteLine("         //Create map and geocoder");
            textWriter.WriteLine("         map = new google.maps.Map(document.getElementById(\"map_canvas\"),myOptions);");
            textWriter.WriteLine("         geocoder = new google.maps.Geocoder();");
            textWriter.WriteLine("         //Show all addresses");
            textWriter.WriteLine("         for (i in clients)");
            textWriter.WriteLine("         {");
            textWriter.WriteLine("            address = addresses[i];");
            textWriter.WriteLine("            client = clients[i];");
            textWriter.WriteLine("            clientAssigned = clientsAssigned[i];");
            textWriter.WriteLine("            delay = i * 1000;");
            textWriter.WriteLine("            setTimeout(showAddress, delay, address, client, clientAssigned);");
            textWriter.WriteLine("         }");
            textWriter.WriteLine("");
            textWriter.WriteLine("         //Center the Map");
            textWriter.WriteLine("         centerMap(mapCenterAddress, 12);");
            textWriter.WriteLine("      }");
            textWriter.WriteLine("");

            //showAddress function
            textWriter.WriteLine("      //Show a single address with client info");
            textWriter.WriteLine("      function showAddress(address, client, assigned)");
            textWriter.WriteLine("      {");
            textWriter.WriteLine("          geocoder.geocode( { 'address': address},");
            textWriter.WriteLine("                              function(results, status)");
            textWriter.WriteLine("                              {");
            textWriter.WriteLine("                                 //Make sure we got a good result");
            textWriter.WriteLine("                                 if (status == google.maps.GeocoderStatus.OK)");
            textWriter.WriteLine("                                 {");
            textWriter.WriteLine("                                    //Temporary working marker");
            textWriter.WriteLine("                                    var marker;");
            textWriter.WriteLine("");
            textWriter.WriteLine("                                    //Create and display marker based on assigned status");
            textWriter.WriteLine("                                    if(assigned == \"0\")");
            textWriter.WriteLine("                                    {");
            textWriter.WriteLine("                                       //Client is not assigned to any deliverer");
            textWriter.WriteLine("                                       marker = new google.maps.Marker({map: map, position: results[0].geometry.location, title: client + \"\\n\" + address, icon: unassignedClientImage});");
            textWriter.WriteLine("                                    }");
            textWriter.WriteLine("                                    else if(assigned == \"1\")");
            textWriter.WriteLine("                                    {");
            textWriter.WriteLine("                                       //Client is assigned to current deliverer");
            textWriter.WriteLine("                                       marker = new google.maps.Marker({map: map, position: results[0].geometry.location, title: client + \"\\n\" + address, icon: assignedClientToCurrentDelivererImage});");
            textWriter.WriteLine("                                    }");
            textWriter.WriteLine("                                    else if(assigned == \"2\")");
            textWriter.WriteLine("                                    {");
            textWriter.WriteLine("                                       //Client is assigned to another deliverer other than current");
            textWriter.WriteLine("                                       marker = new google.maps.Marker({map: map, position: results[0].geometry.location, title: client + \"\\n\" + address, icon: assignedClientToOtherDelivererImage});");
            textWriter.WriteLine("                                    }");
            textWriter.WriteLine("");
            textWriter.WriteLine("                                    //Determine if we should show the Marker based on showMarkerMode");
            textWriter.WriteLine("");
            textWriter.WriteLine("                                    //Show All Clients");
            textWriter.WriteLine("                                    if(showMarkerMode == 0)");
            textWriter.WriteLine("                                    {");
            textWriter.WriteLine("                                       marker.setMap(map);");
            textWriter.WriteLine("                                    }");
            textWriter.WriteLine("                                    //Show Only Assigned Clients");
            textWriter.WriteLine("                                    else if(showMarkerMode == 1 || showMarkerMode == 4)");
            textWriter.WriteLine("                                    {");
            textWriter.WriteLine("                                       //If the client is not assigned - do not show on the map");
            textWriter.WriteLine("                                       if(assigned == \"0\")");
            textWriter.WriteLine("                                       {");
            textWriter.WriteLine("                                          marker.setMap(null);");
            textWriter.WriteLine("                                       }");
            textWriter.WriteLine("                                       else");
            textWriter.WriteLine("                                       {");
            textWriter.WriteLine("                                          marker.setMap(map);");
            textWriter.WriteLine("                                       }");
            textWriter.WriteLine("                                    }");
            textWriter.WriteLine("                                    //Show Only Unassigned Clients");
            textWriter.WriteLine("                                    else if(showMarkerMode == 2)");
            textWriter.WriteLine("                                    {");
            textWriter.WriteLine("                                       //If the client is assigned - do not show on the map");
            textWriter.WriteLine("                                       if(assigned != \"0\")");
            textWriter.WriteLine("                                       {");
            textWriter.WriteLine("                                          marker.setMap(null);");
            textWriter.WriteLine("                                       }");
            textWriter.WriteLine("                                       else");
            textWriter.WriteLine("                                       {");
            textWriter.WriteLine("                                          marker.setMap(map);");
            textWriter.WriteLine("                                       }");
            textWriter.WriteLine("                                    }");
            textWriter.WriteLine("                                    //Show Only Unassigned Clients and Clients that below to current Deliverer");
            textWriter.WriteLine("                                    else if(showMarkerMode == 3)");
            textWriter.WriteLine("                                    {");
            textWriter.WriteLine("                                       //If the client is assigned to the current deliverer or Unassigned - show on the map");
            textWriter.WriteLine("                                       if(assigned == \"0\"  || assigned == \"1\")");
            textWriter.WriteLine("                                       {");
            textWriter.WriteLine("                                          marker.setMap(map);");
            textWriter.WriteLine("                                       }");
            textWriter.WriteLine("                                       else");
            textWriter.WriteLine("                                       {");
            textWriter.WriteLine("                                          marker.setMap(null);");
            textWriter.WriteLine("                                       }");
            textWriter.WriteLine("                                    }");
            textWriter.WriteLine("");
            textWriter.WriteLine("                                    //Add marker to marker array");
            textWriter.WriteLine("                                    markers.push(marker);");
            textWriter.WriteLine("                                 }");
            textWriter.WriteLine("                                 else");
            textWriter.WriteLine("                                 {");
            textWriter.WriteLine("                                    //Alert - Geocode not successful");
            textWriter.WriteLine("                                    alert(\"showAddress - Geocode of (\" + address + \") was not successful for the following reason: (\" + status + \")\");");
            textWriter.WriteLine("                                 }");
            textWriter.WriteLine("                              });");
            textWriter.WriteLine("      }");
            textWriter.WriteLine("");
              
            //centerMap function
            textWriter.WriteLine("      //Show a single address with client info");
            textWriter.WriteLine("      function centerMap(address, zoom)");
            textWriter.WriteLine("      {");
            textWriter.WriteLine("         geocoder.geocode( { 'address': address},");
            textWriter.WriteLine("                           function(results, status)");
            textWriter.WriteLine("                           {");
            textWriter.WriteLine("                              //Make sure we got a good result");
            textWriter.WriteLine("                              if (status == google.maps.GeocoderStatus.OK)");
            textWriter.WriteLine("                              {");
            textWriter.WriteLine("                                 //Center Map");
            textWriter.WriteLine("                                 map.setCenter(results[0].geometry.location);");
            textWriter.WriteLine("");
            textWriter.WriteLine("                                 //Set Map Zoom");
            textWriter.WriteLine("                                 map.setZoom(zoom);");
            textWriter.WriteLine("                              }");
            textWriter.WriteLine("                              else");
            textWriter.WriteLine("                              {");
            textWriter.WriteLine("                                 //Alert - Geocode not successful");
            textWriter.WriteLine("                                 alert(\"centerMap - Geocode of (\" + address + \") was not successful for the following reason: (\" + status + \")\");");
            textWriter.WriteLine("                              }");
            textWriter.WriteLine("                           });");
            textWriter.WriteLine("      }");
            textWriter.WriteLine("");

            //assignClient function
            textWriter.WriteLine("      //Assign Client");
            textWriter.WriteLine("      function assignClient(clientID)");
            textWriter.WriteLine("      {");
            textWriter.WriteLine("         var clientIndex = -1;");
            textWriter.WriteLine("");
            textWriter.WriteLine("         //Find index for Client in the ClientIDs array");
            textWriter.WriteLine("         for(i in clientIDs)");
            textWriter.WriteLine("         {");
            textWriter.WriteLine("            if(clientIDs[i] == clientID)");
            textWriter.WriteLine("            {");
            textWriter.WriteLine("               clientIndex = i;");
            textWriter.WriteLine("               break;");
            textWriter.WriteLine("            }");
            textWriter.WriteLine("         }");
            textWriter.WriteLine("         //See if clientIndex was found");
            textWriter.WriteLine("         if(clientIndex > -1)");
            textWriter.WriteLine("         {");
            textWriter.WriteLine("            if(markers)");
            textWriter.WriteLine("            {");
            textWriter.WriteLine("               //Alter Marker");
            textWriter.WriteLine("               markers[clientIndex].setIcon(assignedClientToCurrentDelivererImage);");
            textWriter.WriteLine("");
            textWriter.WriteLine("               //Determine if we should show the Marker based on showMarkerMode");
            textWriter.WriteLine("");
            textWriter.WriteLine("               //Show All Clients");
            textWriter.WriteLine("               if(showMarkerMode == 0)");
            textWriter.WriteLine("               {");
            textWriter.WriteLine("               //We are assigning a client and we should display All Clients - show clinet on the map");
            textWriter.WriteLine("               markers[clientIndex].setMap(map);");
            textWriter.WriteLine("               }");
            textWriter.WriteLine("               //Show Only Assigned Clients");
            textWriter.WriteLine("               else if(showMarkerMode == 1  || showMarkerMode == 4)");
            textWriter.WriteLine("               {");
            textWriter.WriteLine("                  //We are assigning a client and we should display only Assigned Clients - show client on the map");
            textWriter.WriteLine("                  markers[clientIndex].setMap(map);");
            textWriter.WriteLine("               }");
            textWriter.WriteLine("               //Show Only Unassigned Clients");
            textWriter.WriteLine("               else if(showMarkerMode == 2)");
            textWriter.WriteLine("               {");
            textWriter.WriteLine("                  //We are assigning a client and we should display only UnassignedClients - do not show client on the map");
            textWriter.WriteLine("                  markers[clientIndex].setMap(null);");
            textWriter.WriteLine("               }");
            textWriter.WriteLine("");
            textWriter.WriteLine("               //Update clients Assigned");
            textWriter.WriteLine("               clientsAssigned[clientIndex] = \"1\";");
            textWriter.WriteLine("            }");
            textWriter.WriteLine("            else");
            textWriter.WriteLine("            {");
            textWriter.WriteLine("               //Alert - markers[] is null");
            textWriter.WriteLine("               alert(\"assignClient - markers[] is null\");");
            textWriter.WriteLine("            }");
            textWriter.WriteLine("         }");
            textWriter.WriteLine("         else");
            textWriter.WriteLine("         {");
            textWriter.WriteLine("         //Alert - clientID does not exits");
            textWriter.WriteLine("         alert(\"assignClient - ClientID (\" + clientID + \") not found in ClientIDs array\");");
            textWriter.WriteLine("         }");
            textWriter.WriteLine("      }");
            textWriter.WriteLine("");

            //unassignClient function            
            textWriter.WriteLine("      //Unassign Client");
            textWriter.WriteLine("      function unassignClient(clientID)");
            textWriter.WriteLine("      {");
            textWriter.WriteLine("         var clientIndex = -1;");
            textWriter.WriteLine("");
            textWriter.WriteLine("         //Find index for Client in the ClientIDs array");
            textWriter.WriteLine("         for(i in clientIDs)");
            textWriter.WriteLine("         {");
            textWriter.WriteLine("            if(clientIDs[i] == clientID)");
            textWriter.WriteLine("            {");
            textWriter.WriteLine("               clientIndex = i;");
            textWriter.WriteLine("               break;");
            textWriter.WriteLine("            }");
            textWriter.WriteLine("         }");
            textWriter.WriteLine("         //See if clientIndex was found");
            textWriter.WriteLine("         if(clientIndex > -1)");
            textWriter.WriteLine("         {");
            textWriter.WriteLine("            if(markers)");
            textWriter.WriteLine("            {");
            textWriter.WriteLine("               //Alter Marker");
            textWriter.WriteLine("               markers[clientIndex].setIcon(unassignedClientImage);");
            textWriter.WriteLine("");
            textWriter.WriteLine("               //Determine if we should show the Marker based on showMarkerMode");
            textWriter.WriteLine("");
            textWriter.WriteLine("               //Show All Clients");
            textWriter.WriteLine("               if(showMarkerMode == 0)");
            textWriter.WriteLine("               {");
            textWriter.WriteLine("               //We are unassigning a client and we should display All Clients - show clinet on the map);");
            textWriter.WriteLine("               markers[clientIndex].setMap(map);");
            textWriter.WriteLine("               }");
            textWriter.WriteLine("               //Show Only Assigned Clients");
            textWriter.WriteLine("               else if(showMarkerMode == 1  || showMarkerMode == 4)");
            textWriter.WriteLine("               {");
            textWriter.WriteLine("                  //We are unassigning a client and we should display only Assigned Clients - do not show client on the map");
            textWriter.WriteLine("                  markers[clientIndex].setMap(null);");
            textWriter.WriteLine("               }");
            textWriter.WriteLine("               //Show Only Unassigned Clients");
            textWriter.WriteLine("               else if(showMarkerMode == 2)");
            textWriter.WriteLine("               {");
            textWriter.WriteLine("                  //We are unassigning a client and we should display only UnassignedClients - show client on the map");
            textWriter.WriteLine("                  markers[clientIndex].setMap(map);");
            textWriter.WriteLine("               }");
            textWriter.WriteLine("");
            textWriter.WriteLine("               //Update clients Assigned");
            textWriter.WriteLine("               clientsAssigned[clientIndex] = \"0\";");
            textWriter.WriteLine("            }");
            textWriter.WriteLine("            else");
            textWriter.WriteLine("            {");
            textWriter.WriteLine("               //Alert - markers[] is null");
            textWriter.WriteLine("               alert(\"unassignClient - markers[] is null\");");
            textWriter.WriteLine("            }");
            textWriter.WriteLine("         }");
            textWriter.WriteLine("         else");
            textWriter.WriteLine("         {");
            textWriter.WriteLine("         //Alert - clientID does not exits");
            textWriter.WriteLine("         alert(\"unassignClient - ClientID (\" + clientID + \") not found in ClientIDs array\");");
            textWriter.WriteLine("         }");
            textWriter.WriteLine("      }");
            textWriter.WriteLine("      </script>");
            textWriter.WriteLine("   </head>");
            textWriter.WriteLine("");

            //Write the body part of the .htm page
            textWriter.WriteLine("   <body onload=\"initialize()\">");
            textWriter.WriteLine("      <div id=\"map_canvas\" style=\"height:90%;top:30px\"></div>");
            textWriter.WriteLine("   </body>");
            textWriter.WriteLine("</html>");

            //Close the file stream
            textWriter.Close();
        }
        int GetClientCountByShowMarkerModeAndZipcode(string showMarkerMode, string zipcode, Deliverer deliverer)
        {
            //Define local variables
            int numberOfClientsWithModeAndZipcode = 0;

            if (zipcode == "None")
            {
                numberOfClientsWithModeAndZipcode = 0;
            }
            else
            {
                //Create the select selectByClient_IDQueryText
                string selectByClient_IDQueryText = "SELECT * FROM " + Main.mSelectedYear;

                //Get the list of clients

                //Create the clientIDList
                DataSet clientIDList = Main.mChristmasBasketsAccessDatabase.PerformSelectQuery(selectByClient_IDQueryText, Main.mSelectedYear);

                //Process each record in the clientIDList table and count clients with specific zipcode
                foreach (DataRow dataRow in clientIDList.Tables[0].Rows)
                {
                    string query = "";

                    if (zipcode == "All")
                    {
                        //All zipcodes
                        query = "SELECT * FROM Clients WHERE Client_ID = " + dataRow["Client_ID"].ToString();
                    }
                    else
                    {
                        //Single zipcode
                        query = "SELECT * FROM Clients WHERE Client_ID = " + dataRow["Client_ID"].ToString() + " AND Zipcode = \"" + zipcode + "\"";
                    }
                    
                    
                    DataSet client = Main.mChristmasBasketsAccessDatabase.PerformSelectQuery(query, "Clients");

                    if(client != null)
                    {
                        string id = client.Tables[0].Rows[0]["Client_ID"].ToString();
                        string assignedStatus = "";

                        string[] delivererClientList = deliverer.Clients.Split(',');
                        bool clientAssignedToCurrentDeliverer = false;

                        foreach (string currentClient in delivererClientList)
                        {
                            if (currentClient != "")
                            {
                                if (id == currentClient)
                                {
                                    clientAssignedToCurrentDeliverer = true;
                                    break;
                                }
                            }
                        }

                        //Determine if client is assigned to the current deliverer which is the target of the .htm file being investigatedv
                        if (clientAssignedToCurrentDeliverer)
                        {
                            //Deliverer is assigned to this deliverer
                            assignedStatus = "1";
                        }
                        else
                        {
                            if (dataRow["Assigned_Status"].ToString() == "true")
                            {
                                //Client is assigned to another deliverer other than the current deliverer
                                assignedStatus = "2";
                            }
                            else
                            {
                                //Client is not assigned to any deliverer
                                assignedStatus = "0";
                            }
                        }

                        //Determine if we add the client

                        //Show Clients Assigned to Current Deliverer, Unassigned, and Assigned to Other Deliverers
                        if (showMarkerMode == "0")
                        {
                            numberOfClientsWithModeAndZipcode++;
                        }
                        //Show Clients Assigned to Current Deliverer and Other Deliverers
                        else if (showMarkerMode == "1" && (assignedStatus == "1" || assignedStatus == "2"))
                        {
                            numberOfClientsWithModeAndZipcode++;
                        }
                        //Show Unassigned Clients
                        else if (showMarkerMode == "2" && (assignedStatus == "0"))
                        {
                            numberOfClientsWithModeAndZipcode++;
                        }
                        //Show Clients Assigned to Current Deliverer and Unassigned Clients
                        else if (showMarkerMode == "3" && (assignedStatus == "1" || assignedStatus == "0"))
                        {
                            numberOfClientsWithModeAndZipcode++;
                        }
                        //Show Only Clients assigned to the Current Deliverer
                        else if (showMarkerMode == "4" && assignedStatus == "1")
                        {
                            numberOfClientsWithModeAndZipcode++;
                        }
                    }
                    
                }
            }

            return numberOfClientsWithModeAndZipcode;
        }
        private void GenerateUnassignedClientPacketsButton_Click(object sender, RoutedEventArgs e)
        {
            if (mSelectedYear == "NONE")
            {
                System.Windows.MessageBox.Show("Open a database and select a year.");
                return;
            }

            List<string> generatedFiles = new List<string>();
            string getUnassignedClientsQuery = "SELECT * FROM " + Main.mSelectedYear + " WHERE Assigned_Status = 'false'";

            DataSet unassignedClients = Main.mChristmasBasketsAccessDatabase.PerformSelectQuery(getUnassignedClientsQuery, Main.mSelectedYear);

            if (unassignedClients != null)
            {
                if (unassignedClients.Tables[0].Rows.Count != 0)
                {
                    foreach (DataRow unassignedClientRow in unassignedClients.Tables[0].Rows)
                    {
                        Deliverer deliverer = new Deliverer();

                        deliverer.LastName = "Unassigned";
                        deliverer.FirstName = "Client";
                        deliverer.DelivererID = 0;
                        deliverer.Assigned = 1;
                        deliverer.Clients = unassignedClientRow["Client_ID"].ToString();

                        //Create the fileName
                        string[] fileNames = new string[1];
                        string[] fullAddresses = null;
                        string[] clients = null;
                        string[] names = null;
                        string[] streetAddresses = null;
                        string[] cities = null;
                        string[] zipCodes = null;
                        string[] phoneNumbers = null;
                        string[] organizations = null;
                        string[] comments = null;

                        PrepareDelivererPacketArguments(deliverer, ref fullAddresses, ref clients, ref names, ref streetAddresses, ref cities, ref zipCodes, ref phoneNumbers, ref organizations, ref comments);

                        //Define FileName
                        fileNames[0] = "Deliverer(" + deliverer.DelivererID + ")_" + deliverer.LastName + "_" + deliverer.FirstName + "_Client_ID(" + unassignedClientRow["Client_ID"].ToString() + ").htm";

                        GenerateDelivererPacket(deliverer, ref fileNames, ref fullAddresses, ref clients, ref names, ref streetAddresses, ref cities, ref zipCodes, ref phoneNumbers, ref organizations, ref comments);

                        //Add the file to the list of files that are generated

                        generatedFiles.Add(fileNames[0]);


                        //Since we've created the .htm client white card files with maps
                        //open them in firefox to print them

                        //Get the total number of deliverers to display in firefox
                        int deliverersToDisplayInFirefox = generatedFiles.Count;

                        //Create a variable for the client to start from
                        int startingDeliverer = 0;

                        //Have a variable to use for number to print in a single session
                        int sessionDelivererLimit = 100;

                        while (deliverersToDisplayInFirefox != 0)
                        {
                            //Check for the case where deliverersToDisplayInFirefox < sessionDelivererLimit
                            if (deliverersToDisplayInFirefox < sessionDelivererLimit)
                            {
                                sessionDelivererLimit = deliverersToDisplayInFirefox;
                            }

                            //Create a new process
                            System.Diagnostics.Process process = new System.Diagnostics.Process();

                            //The process will be firefox
                            process.StartInfo.FileName = "firefox.exe";

                            //Create firefox's command argument line
                            //Open all of the files we created in seperate tabs
                            for (int i = startingDeliverer; i < (startingDeliverer + sessionDelivererLimit); i++)
                            {
                                process.StartInfo.Arguments += "\"" + generatedFiles[i] + "\" ";
                            }

                            //Start Firefox
                            process.Start();

                            //Close our handle to Firefox
                            process.Close();

                            //Update deliverersToDisplayInFirefox
                            deliverersToDisplayInFirefox -= sessionDelivererLimit;

                            //Update startingDeliverer
                            startingDeliverer += sessionDelivererLimit;
                        }
                    }
                }
                else
                {
                    System.Windows.MessageBox.Show("All clients are assigned to deliverers");
                }
            }
        }
        private void GenerateDelivererPacket(Deliverer deliverer, ref string  [] fileNames, ref string [] fullAddresses, ref string []  clients, ref string []  names, ref string []  streetAddresses, ref string []  cities, ref string []  zipCodes, ref string [] phoneNumbers, ref string []  organizations, ref string []  comments)
        {
            //See if there is just one file to generate
            if (fileNames.Length == 0)
            {
                //Nothing to Do
                return;
            }
            else
            {
                //Initialize variables for previous end addresses
                string previousEndAddress = "";
                int remainingNumberOfClients = clients.Length;
                int numberClientsInTable = 0;
                int index = 0;

                for(int i = 0; i < fileNames.Length; i++)
                {
                    //Define local variables
                    TextWriter textWriter = new StreamWriter(fileNames[i]);

                    //////////////////////////////////////////////////////////////////////////////////////////
                    //////////////////////////////////////////////////////////////////////////////////////////
                    ////////////////////////        COME BACK HERE JERK!!!!      /////////////////////////////
                    //////////////////////////////////////////////////////////////////////////////////////////
                    //////////////////////////////////////////////////////////////////////////////////////////

                    //Write the header part of the .htm page
                    textWriter.WriteLine("<!DOCTYPE html>");
                    textWriter.WriteLine("<html>");
                    textWriter.WriteLine("   <head>");
                    textWriter.WriteLine("      <meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\"/>");
                    textWriter.WriteLine("      <title>Deliverer(" + deliverer.DelivererID + "):  " + deliverer.FirstName + " " + deliverer.LastName + "</title>");

                    //Write the java script part of the .htm page
                    textWriter.WriteLine("      <script type=\"text/javascript\" src=\"http://maps.google.com/maps/api/js?sensor=false\"></script>");
                    textWriter.WriteLine("      <script type=\"text/javascript\">");
                    textWriter.WriteLine("");
                    textWriter.WriteLine("      var map;");
                    textWriter.WriteLine("      var directionsDisplay = new google.maps.DirectionsRenderer();");
                    textWriter.WriteLine("      var directionsService = new google.maps.DirectionsService();");

                    //If this is the first file we are generating start the route at GE in Salem
                    if(i == 0)
                    {
                        textWriter.WriteLine("      var start = \"1501 Roanoke Blvd Salem, VA 24153\";");
                    }
                    else
                    {
                        //This is not the first file we are generating so start the route at the previousEndAddress
                        textWriter.WriteLine("      var start = \""+ previousEndAddress + "\";");
                    }

                    //Generate addresses arrays
                    string javaAddresses = "      var addresses = [";

                    //See if we are generating a single file, if so send them back to GE in Salem as the end destination
                    if (fileNames.Length == 1)
                    {
                        textWriter.WriteLine("      var end = \"1501 Roanoke Blvd Salem, VA 24153\";");

                        //See if we have 1 or more than 1 client to process
                        if (clients.Count() == 1)
                        {
                            //One client
                            javaAddresses += "\"" + fullAddresses[0] + "\"];";
                        }
                        else
                        {
                            //More than one client
                            for (int j = 0; j < clients.Count(); j++)
                            {
                                //Insert clients
                                if (j == (clients.Count() - 1))
                                {
                                    //Last Record
                                    javaAddresses += "\"" + fullAddresses[j] + "\"];";
                                }
                                else
                                {
                                    //Not the Last Record
                                    javaAddresses += "\"" + fullAddresses[j] + "\",";
                                }
                            }

                        }

                        //Set numberClientsInTable
                        numberClientsInTable = clients.Count();
                    }
                    else
                    {
                        //More than one file to produce so choose an end destination from the clients list
                        if (remainingNumberOfClients <= mMaxNumberGoogleMapsWaypoints)
                        {
                            if (remainingNumberOfClients == 0)
                            {
                                //Last Record
                                textWriter.WriteLine("      var end = \"1501 Roanoke Blvd Salem, VA 24153\";");
                                javaAddresses += "];";
                                numberClientsInTable = 0;
                            }
                            else
                            {
                                //Write remainingNumberOfClients
                                for (int k = 0; k < remainingNumberOfClients; k++)
                                {
                                    index = (i * mMaxNumberGoogleMapsWaypoints) + k + 1;

                                    //Insert clients
                                    if ((k == (mMaxNumberGoogleMapsWaypoints - 2)) || (k == (remainingNumberOfClients - 1)))
                                    {
                                        //Last Record
                                        javaAddresses += "\"" + fullAddresses[index] + "\"];";
                                    }
                                    else
                                    {
                                        //Not the Last Record
                                        javaAddresses += "\"" + fullAddresses[index] + "\",";
                                    }
                                }

                                //Set var end as GE in Salem
                                textWriter.WriteLine("      var end = \"1501 Roanoke Blvd Salem, VA 24153\";");

                                //Cache away number of Clients to print in Deliverer Table
                                numberClientsInTable = remainingNumberOfClients;

                                //Subtract remainingNumberOfClients from remainingNumberOfClients to arrive at remainingNumberOfClients = 0
                                remainingNumberOfClients -= remainingNumberOfClients;
                            }
                        }
                        else
                        {
                            //Write 8 clients as waypoints + 1 client as the var end address
                            
                            //Write remainingNumberOfClients
                            for (int k = 0; k < mMaxNumberGoogleMapsWaypoints; k++)
                            {
                                index = (i * mMaxNumberGoogleMapsWaypoints) + k;

                                //Insert clients
                                if (k == (mMaxNumberGoogleMapsWaypoints - 1))
                                {
                                    //Last Record
                                    javaAddresses += "\"" + fullAddresses[index] + "\"];";
                                }
                                else
                                {
                                    //Not the Last Record
                                    javaAddresses += "\"" + fullAddresses[index] + "\",";
                                }
                            }

                            //Update previousEndAddress
                            previousEndAddress = fullAddresses[index + 1];

                            //Cache away number of Clients to print in Deliverer Table
                            numberClientsInTable = mMaxNumberGoogleMapsWaypoints + 1;

                            //Update var end
                            textWriter.WriteLine("      var end = \"" + previousEndAddress + "\";");

                            //Subtract 8 clients (waypoints) + 1 client (var end) from remaining
                            remainingNumberOfClients -= (mMaxNumberGoogleMapsWaypoints + 1);
                        }
                    }

                    //Insert addresses array
                    textWriter.WriteLine(javaAddresses);
                    textWriter.WriteLine("");

                    textWriter.WriteLine("      var unassignedClientImage = \"UnassignedClient.png\";");
                    textWriter.WriteLine("      var mapCenterAddress = \"2147 Dale Avenue Southeast Roanoke, VA 24013\";");

                    //initialize function
                    textWriter.WriteLine("      function initialize()");
                    textWriter.WriteLine("      {");
                    textWriter.WriteLine("         //Create map options");
                    textWriter.WriteLine("         var myOptions = {mapTypeId: google.maps.MapTypeId.ROADMAP};");
                    textWriter.WriteLine("");
                    textWriter.WriteLine("         //Create map and direction display");
                    textWriter.WriteLine("         map = new google.maps.Map(document.getElementById(\"map_canvas\"),myOptions);");
                    textWriter.WriteLine("         directionsDisplay.setMap(map);");
                    textWriter.WriteLine("         directionsDisplay.setPanel(document.getElementById(\"directions_panel\"));");
                    textWriter.WriteLine("");
                    textWriter.WriteLine("         //Calculate the proper route");
                    textWriter.WriteLine("         calcRoute();");
                    textWriter.WriteLine("      }");
                    textWriter.WriteLine("");

                    //calcRoute function
                    textWriter.WriteLine("      //Calculate Client Route");
                    textWriter.WriteLine("      function calcRoute()");
                    textWriter.WriteLine("      {");
                    textWriter.WriteLine("         //Build waypoints object");
                    textWriter.WriteLine("         var waypts = [];");
                    textWriter.WriteLine("");
                    textWriter.WriteLine("         for (var i = 0; i < addresses.length; i++)");
                    textWriter.WriteLine("         {");
                    textWriter.WriteLine("            waypts.push({location:addresses[i],stopover:true});");
                    textWriter.WriteLine("         }");
                    textWriter.WriteLine("");
                    textWriter.WriteLine("         //Build Request");
                    textWriter.WriteLine("         var request = {");
                    textWriter.WriteLine("         origin: start,");
                    textWriter.WriteLine("         destination: end,");
                    textWriter.WriteLine("         waypoints: waypts,");
                    textWriter.WriteLine("         optimizeWaypoints: true,");
                    textWriter.WriteLine("         travelMode: google.maps.DirectionsTravelMode.DRIVING};");
                    textWriter.WriteLine("");
                    textWriter.WriteLine("         //Show directions");
                    textWriter.WriteLine("         directionsService.route(request,");
                    textWriter.WriteLine("         function(response, status)");
                    textWriter.WriteLine("         {");
                    textWriter.WriteLine("            if (status == google.maps.DirectionsStatus.OK)");
                    textWriter.WriteLine("            {");
                    textWriter.WriteLine("               directionsDisplay.setDirections(response);");
                    textWriter.WriteLine("            }");
                    textWriter.WriteLine("            else");
                    textWriter.WriteLine("            {");
                    textWriter.WriteLine("               //Alert - Route Generation not successful");
                    textWriter.WriteLine("               alert(\"calcRoute - Route Generation was not successful for the following reason: (\" + status + \")\");");
                    textWriter.WriteLine("            }");
                    textWriter.WriteLine("         });");
                    textWriter.WriteLine("      }");
                    textWriter.WriteLine("");

                    //Final script tags
                    textWriter.WriteLine("      </script>");
                    textWriter.WriteLine("   </head>");
                    textWriter.WriteLine("");

                    //Write the body part of the .htm page
                    textWriter.WriteLine("   <body onload=\"initialize()\">");
                    textWriter.WriteLine("      <!-- General Instructions -->");
                    textWriter.WriteLine("      <h2>General Instructions:</h2>");
                    textWriter.WriteLine("      <div style=\"width:700px;height:85px;border:6px outset red;\">");
                    textWriter.WriteLine("      <font size = 3> Each delivery consists of two boxes; a red and a green.");
                    textWriter.WriteLine("      The recipient needs to sign a USDA form upon delivery.");
                    textWriter.WriteLine("      If a box cannot be delivered, call Dick Stanfield at 540-353-7977 for assistance.");
                    textWriter.WriteLine("      Undelivered boxes should be returned to the west gate of GE at 1501 Roanoke Blvd Salem, VA.");
                    textWriter.WriteLine("      If returning a box, be sure to include the corresponding USDA form.</font></div>");
                    textWriter.WriteLine("      <!-- Deliverer -->");
                    textWriter.WriteLine("      <h2>Deliverer:   " + deliverer.LastName + ", " + deliverer.FirstName + " - Number of Clients (" + clients.Length + ") - Number Of Boxes (" + clients.Length * 2 + ")" + "</h2>");
                    
                    textWriter.WriteLine("");
                    textWriter.WriteLine("      <!-- Google Map -->");
                    textWriter.WriteLine("      <h2>Suggested Route:</h2>");
                    textWriter.WriteLine("      <div id=\"map_canvas\" style=\"width:800px; height:800px;\"></div>");
                    textWriter.WriteLine("      <div style=\"page-break-before:always;\"></div>");
                    textWriter.WriteLine("");
                    textWriter.WriteLine("      <!-- Google Directions -->");
                    textWriter.WriteLine("      <h2>Directions:  Leaving From GE</h2>");
                    textWriter.WriteLine("      <div id=\"directions_panel\"></div>");
                    textWriter.WriteLine("      <div style=\"page-break-before:always;\"></div>");
                    textWriter.WriteLine("");
                    textWriter.WriteLine("      <!-- Client Information Table-->");
                    textWriter.WriteLine("      <h2 align = \"left\">Client Information</h2>");
                    textWriter.WriteLine("      <table align = \"left\" width=\"100%\" border=\"1\">");
                    textWriter.WriteLine("      <!-- Table Headings -->");
                    textWriter.WriteLine("      <tr>");
                    textWriter.WriteLine("         <th width = \"10%\">Client ID</th>");
                    textWriter.WriteLine("         <th width = \"20%\">Client Name</th>");
                    textWriter.WriteLine("         <th width = \"30%\">Street Address</th>");
                    textWriter.WriteLine("         <th width = \"10%\">City</th>");
                    textWriter.WriteLine("         <th width = \"10%\">Zip</th>");
                    textWriter.WriteLine("         <th width = \"10%\">Phone</th>");
                    textWriter.WriteLine("         <th width = \"10%\">Organization</th>");
                    textWriter.WriteLine("      </tr>");

                    //Put each deliverer's client in the table
                    for (int m = 0; m < numberClientsInTable; m++)
                    {
                        if (remainingNumberOfClients == 0 && numberClientsInTable != 9)
                        {
                           index = (i * mMaxNumberGoogleMapsWaypoints) + m + 1;
                        }
                        else
                        {
                           index = (i * mMaxNumberGoogleMapsWaypoints) + m;
                        }
                        

                        textWriter.WriteLine("      <!-- Client Data -->");
                        textWriter.WriteLine("      <tr>");
                        textWriter.WriteLine("         <td>" + clients[index] + "</td>");
                        textWriter.WriteLine("         <td>" + names[index] + "</td>");
                        textWriter.WriteLine("         <td>" + streetAddresses[index] + "</td>");
                        textWriter.WriteLine("         <td>" + cities[index] + "</td>");
                        textWriter.WriteLine("         <td>" + zipCodes[index] + "</td>");
                        textWriter.WriteLine("         <td>" + phoneNumbers[index] + "</td>");
                        textWriter.WriteLine("         <td>" + organizations[index] + "</td>");
                        textWriter.WriteLine("      </tr>");
                    }

                    textWriter.WriteLine("");
                    textWriter.WriteLine("      </table>");
                    textWriter.WriteLine("      <!-- Client Specific Notes -->");
                    textWriter.WriteLine("      <h2 align = \"left\">Client Specific Notes</h2>");
                    textWriter.WriteLine("      <table align = \"left\" width=\"100%\" border=\"1\">");
                    textWriter.WriteLine("      <!-- Table Headings -->");
                    textWriter.WriteLine("      <tr>");
                    textWriter.WriteLine("         <th width = \"10%\">Client ID</th>");
                    textWriter.WriteLine("         <th width = \"20%\">Client Name</th>");
                    textWriter.WriteLine("         <th width = \"70%\">Comments</th>");
                    textWriter.WriteLine("      </tr>");

                    //Put each deliverer's client comments in the table
                    for (int n = 0; n < numberClientsInTable; n++)
                    {
                        if (remainingNumberOfClients == 0 && numberClientsInTable != 9)
                        {
                            index = (i * mMaxNumberGoogleMapsWaypoints) + n + 1;
                        }
                        else
                        {
                            index = (i * mMaxNumberGoogleMapsWaypoints) + n;
                        }

                        textWriter.WriteLine("      <!-- Client Data -->");
                        textWriter.WriteLine("      <tr>");
                        textWriter.WriteLine("         <td>" + clients[index] + "</td>");
                        textWriter.WriteLine("         <td>" + names[index] + "</td>");
                        textWriter.WriteLine("         <td>" + comments[index] + "</td>");
                        textWriter.WriteLine("      </tr>");
                    }

                    textWriter.WriteLine("      </table>");
                    textWriter.WriteLine("   </body>");
                    textWriter.WriteLine("</html>");

                    //Close the file stream
                    textWriter.Close();
                }
            }
        }
        private void PrepareDelivererPacketArguments(Deliverer deliverer, ref string[] fullAddresses, ref string[] clients, ref string[] names, ref string[] streetAddresses, ref string[] cities, ref string[] zipCodes, ref string[] phoneNumbers, ref string[] organizations, ref string[] comments)
        {
            //Get the list of clients

            //Create the clientIDList
            string [] clientIDList = deliverer.Clients.Split(',');

            int index = 0;

            //Create new arrays
            fullAddresses = new string[deliverer.Assigned];
            clients = new string[deliverer.Assigned];
            names = new string[deliverer.Assigned];
            streetAddresses = new string[deliverer.Assigned];
            cities = new string[deliverer.Assigned];
            zipCodes = new string[deliverer.Assigned];
            phoneNumbers = new string[deliverer.Assigned];
            organizations = new string[deliverer.Assigned];
            comments = new string[deliverer.Assigned];

            //Process each record in the clientIDList table
            foreach (string clientIDToProcess in clientIDList)
            {
                if (clientIDToProcess != "")
                {
                    //Create the selectByClientIDQueryText
                    string selectByClientIDQueryText = "SELECT * FROM Clients WHERE Client_ID = " + clientIDToProcess;

                    //Perform the selectByClientIDDataSet and store the results in a Data Table
                    System.Data.DataSet selectByClientIDDataSet = Main.mChristmasBasketsAccessDatabase.PerformSelectQuery(selectByClientIDQueryText, "Clients");

                    //Check to see we got results back
                    if (selectByClientIDDataSet != null)
                    {
                        //Format the strings properly
                        string id = selectByClientIDDataSet.Tables[0].Rows[0]["Client_ID"].ToString();
                        string firstName = selectByClientIDDataSet.Tables[0].Rows[0]["First_Name"].ToString();
                        string lastName = selectByClientIDDataSet.Tables[0].Rows[0]["Last_Name"].ToString();
                        string addressNumber = selectByClientIDDataSet.Tables[0].Rows[0]["Address_Number"].ToString();
                        string streetAddress = selectByClientIDDataSet.Tables[0].Rows[0]["Street_Address"].ToString();
                        string city = selectByClientIDDataSet.Tables[0].Rows[0]["City"].ToString();
                        string phone = selectByClientIDDataSet.Tables[0].Rows[0]["Phone"].ToString();
                        string zipcode = selectByClientIDDataSet.Tables[0].Rows[0]["Zipcode"].ToString();
                        string organization = selectByClientIDDataSet.Tables[0].Rows[0]["Organization"].ToString();
                        string comment = selectByClientIDDataSet.Tables[0].Rows[0]["Directions"].ToString();

                        fullAddresses[index] = addressNumber + " " + streetAddress + " " + city + ", VA  " + zipcode;
                        clients[index] = id;
                        names[index] = firstName + " " + lastName;
                        streetAddresses[index] = addressNumber + " " + streetAddress;
                        cities[index] = city;
                        zipCodes[index] = zipcode;
                        phoneNumbers[index] = phone;
                        organizations[index] = organization;
                        comments[index] = comment;

                        index++;
                    }

                    if (index == deliverer.Assigned)
                    {
                        break;
                    }
                }
            }
        }
        private void GenerateDelivererPacketsButton_Click(object sender, RoutedEventArgs e)
        {
            if (mSelectedYear == "NONE")
            {
                System.Windows.MessageBox.Show("Open a database and select a year.");
                return;
            }

            //Initialize variables
            string delivererIDQuery = "SELECT * FROM " + Main.mSelectedYear + "_Deliverers";
            List<Deliverer> deliverers = new List<Deliverer>();
            List<string> generatedFiles = new List<string>();

            //Get all the deliverers assigned to the year
            DataSet delivererIDs = Main.mChristmasBasketsAccessDatabase.PerformSelectQuery(delivererIDQuery, Main.mSelectedYear + "_Deliverers");

            //Process each record in the imported deliverers table
            foreach (DataRow dataRow in delivererIDs.Tables[0].Rows)
            {
                //Get the detailed information from the master Deliverer's Table in the database
                string getDelivererInfoQuery = "SELECT * FROM Deliverers WHERE Deliverer_ID = " + dataRow["Deliverer_ID"];

                DataSet delivererBeingProcessed = Main.mChristmasBasketsAccessDatabase.PerformSelectQuery(getDelivererInfoQuery, "Deliverers");

                DataRow delivererInfoFromDatabase = delivererBeingProcessed.Tables[0].Rows[0];

                Deliverer delivererToAdd = new Deliverer();

                delivererToAdd.DelivererID = Convert.ToInt32(delivererInfoFromDatabase["Deliverer_ID"]);
                delivererToAdd.FirstName = delivererInfoFromDatabase["First_Name"].ToString();
                delivererToAdd.LastName = delivererInfoFromDatabase["Last_Name"].ToString();
                delivererToAdd.Capacity = Convert.ToInt32(delivererInfoFromDatabase["Capacity"]);
                delivererToAdd.HelpStatus = delivererInfoFromDatabase["Help_Status"].ToString();
                delivererToAdd.Room = delivererInfoFromDatabase["Room"].ToString();
                delivererToAdd.WorkPhone = delivererInfoFromDatabase["Work_Phone"].ToString();
                delivererToAdd.HomePhone = delivererInfoFromDatabase["Home_Phone"].ToString();
                delivererToAdd.OccupationStatus = delivererInfoFromDatabase["Occupation_Status"].ToString();
                delivererToAdd.Comments = delivererInfoFromDatabase["Comments"].ToString();
                delivererToAdd.ClientHistory = delivererInfoFromDatabase["Client_History"].ToString();
                delivererToAdd.YearLastDelivered = delivererInfoFromDatabase["Year_Last_Delivered"].ToString();
                delivererToAdd.Assigned = Convert.ToInt32(delivererInfoFromDatabase["Assigned"]);

                //Get Clients from Year_XXXX_Clients Table
                string getDelivererClientsQuery = "SELECT Clients FROM " + Main.mSelectedYear + "_Deliverers WHERE Deliverer_ID = " + dataRow["Deliverer_ID"];

                DataSet delivererClientsBeingProcessed = Main.mChristmasBasketsAccessDatabase.PerformSelectQuery(getDelivererClientsQuery, Main.mSelectedYear + "_Deliverers");

                DataRow delivererClientsFromDatabase = delivererClientsBeingProcessed.Tables[0].Rows[0];

                delivererToAdd.Clients = delivererClientsFromDatabase["Clients"].ToString();

                deliverers.Add(delivererToAdd);
            }

            //Generate all deliverer packets
            foreach(Deliverer deliverer in deliverers)
            {
                //Create the fileName
                string [] fileNames = null;
                string [] fullAddresses = null;
                string [] clients = null;
                string [] names = null;
                string [] streetAddresses = null;
                string [] cities = null;
                string [] zipCodes = null;
                string [] phoneNumbers = null;
                string [] organizations = null;
                string [] comments = null;

                //Prepare Deliverer Packet Arguments
                PrepareDelivererPacketArguments(deliverer, ref fullAddresses, ref clients, ref names, ref streetAddresses, ref cities, ref zipCodes, ref phoneNumbers, ref organizations, ref comments);

                //Determine how many clients are to process
                int numberOfFilesToGenerate = clients.Length / mMaxNumberGoogleMapsWaypoints;
                numberOfFilesToGenerate += (clients.Length % mMaxNumberGoogleMapsWaypoints == 0 ? 0 : 1);

                //Allocate number of fileNames
                fileNames = new string[numberOfFilesToGenerate];

                //Add each filename to generated files list
                for (int i = 1; i < (numberOfFilesToGenerate + 1); i++)
                {
                    string nameOfFile = "Deliverer(" + deliverer.DelivererID + ")_" + deliverer.LastName + "_" + deliverer.FirstName + "_Part(" + i.ToString() + ").htm";
                    fileNames[i - 1] = nameOfFile;
                    generatedFiles.Add(nameOfFile);
                }

                GenerateDelivererPacket(deliverer, ref fileNames, ref fullAddresses, ref clients, ref names, ref streetAddresses, ref cities, ref zipCodes, ref phoneNumbers, ref organizations, ref comments);
            }

            //Since we've created the .htm client white card files with maps
            //open them in firefox to print them

            //Get the total number of deliverers to display in firefox
            int deliverersToDisplayInFirefox = generatedFiles.Count;

            //Create a variable for the client to start from
            int startingDeliverer = 0;

            //Have a variable to use for number to print in a single session
            int sessionDelivererLimit = 100;

            while (deliverersToDisplayInFirefox != 0)
            {
                //Check for the case where deliverersToDisplayInFirefox < sessionDelivererLimit
                if (deliverersToDisplayInFirefox < sessionDelivererLimit)
                {
                    sessionDelivererLimit = deliverersToDisplayInFirefox;
                }

                //Create a new process
                System.Diagnostics.Process process = new System.Diagnostics.Process();

                //The process will be firefox
                process.StartInfo.FileName = "firefox.exe";

                //Create firefox's command argument line
                //Open all of the files we created in seperate tabs
                for (int i = startingDeliverer; i < (startingDeliverer + sessionDelivererLimit); i++)
                {
                    process.StartInfo.Arguments += "\"" + generatedFiles[i] + "\" ";
                }

                //Start Firefox
                process.Start();

                //Close our handle to Firefox
                process.Close();

                //Update deliverersToDisplayInFirefox
                deliverersToDisplayInFirefox -= sessionDelivererLimit;

                //Update startingDeliverer
                startingDeliverer += sessionDelivererLimit;
            }   
        }
        private void UpdateDeliverersDataGrid()
        {
            //Initialize variables
            string delivererIDQuery = "SELECT * FROM " + Main.mSelectedYear + "_Deliverers";

            //Clear current data in deliverers
            deliverers.Clear();

            //Get all the deliverers assigned to the year
            DataSet delivererIDs = Main.mChristmasBasketsAccessDatabase.PerformSelectQuery(delivererIDQuery, Main.mSelectedYear + "_Deliverers");

            //Process each record in the imported deliverers table
            foreach (DataRow dataRow in delivererIDs.Tables[0].Rows)
            {
                //Get the detailed information from the master Deliverer's Table in the database
                string getDelivererInfoQuery = "SELECT * FROM Deliverers WHERE Deliverer_ID = " + dataRow["Deliverer_ID"];

                DataSet delivererBeingProcessed = Main.mChristmasBasketsAccessDatabase.PerformSelectQuery(getDelivererInfoQuery, "Deliverers");

                DataRow delivererInfoFromDatabase = delivererBeingProcessed.Tables[0].Rows[0];

                Deliverer delivererToAdd = new Deliverer();

                delivererToAdd.DelivererID = Convert.ToInt32(delivererInfoFromDatabase["Deliverer_ID"]);
                delivererToAdd.FirstName = delivererInfoFromDatabase["First_Name"].ToString();
                delivererToAdd.LastName = delivererInfoFromDatabase["Last_Name"].ToString();
                delivererToAdd.Capacity = Convert.ToInt32(delivererInfoFromDatabase["Capacity"]);
                delivererToAdd.HelpStatus = delivererInfoFromDatabase["Help_Status"].ToString();
                delivererToAdd.Room = delivererInfoFromDatabase["Room"].ToString();
                delivererToAdd.WorkPhone = delivererInfoFromDatabase["Work_Phone"].ToString();
                delivererToAdd.HomePhone = delivererInfoFromDatabase["Home_Phone"].ToString();
                delivererToAdd.OccupationStatus = delivererInfoFromDatabase["Occupation_Status"].ToString();
                delivererToAdd.Comments = delivererInfoFromDatabase["Comments"].ToString();
                delivererToAdd.ClientHistory = delivererInfoFromDatabase["Client_History"].ToString();
                delivererToAdd.YearLastDelivered = delivererInfoFromDatabase["Year_Last_Delivered"].ToString();

                if (delivererInfoFromDatabase["Assigned"].ToString() == "")
                {
                    delivererToAdd.Assigned = 0;
                }
                else
                {
                    delivererToAdd.Assigned = Convert.ToInt32(delivererInfoFromDatabase["Assigned"]);
                }

                //Get Clients from Year_XXXX_Clients Table
                string getDelivererClientsQuery = "SELECT Clients FROM " + Main.mSelectedYear + "_Deliverers WHERE Deliverer_ID = " + dataRow["Deliverer_ID"];

                DataSet delivererClientsBeingProcessed = Main.mChristmasBasketsAccessDatabase.PerformSelectQuery(getDelivererClientsQuery, Main.mSelectedYear + "_Deliverers");

                DataRow delivererClientsFromDatabase = delivererClientsBeingProcessed.Tables[0].Rows[0];

                delivererToAdd.Clients = delivererClientsFromDatabase["Clients"].ToString();

                deliverers.Add(delivererToAdd);
            }

            DataContext = this;
            
            deliverersCollectionView = CollectionViewSource.GetDefaultView(deliverers);
            
            DeliverersDataGrid.Items.Refresh();
            UpdateValueLabelsAndProgressBars();
        }