//private void btnGeocode_Click(object sender, RoutedEventArgs e)
        //{
        //    SaveScreenToDealer();
        //    YahooGeoCoder.GeocodeDealer(_selectedDealer);
        //    txtLatitude.Text = _selectedDealer.Latitude.ToString();
        //    txtLongitude.Text = _selectedDealer.Longitude.ToString();
        //}

        // center markers on load

        //void MainMap_MouseEnter(object sender, MouseEventArgs e)
        //{
        //    MainMap.Focus();
        //}

        //void MainMap_Loaded(object sender, RoutedEventArgs e)
        //{
        //    MainMap.ZoomAndCenterMarkers(null);
        //}

        //void MainMap_OnMapTypeChanged(MapType type)
        //{
        //    sliderZoom.Minimum = MainMap.MinZoom;
        //    sliderZoom.Maximum = MainMap.MaxZoom;
        //}

        //void MainMap_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        //{
        //    System.Windows.Point p = e.GetPosition(MainMap);
        //    currentMarker.Position = MainMap.FromLocalToLatLng((int)p.X, (int)p.Y);
        //}

        //// move current marker with left holding
        //void MainMap_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        //{
        //    if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
        //    {
        //        System.Windows.Point p = e.GetPosition(MainMap);
        //        currentMarker.Position = MainMap.FromLocalToLatLng((int)p.X, (int)p.Y);
        //    }
        //}

        //// zoo max & center markers
        //private void button13_Click(object sender, RoutedEventArgs e)
        //{
        //    MainMap.ZoomAndCenterMarkers(null);
        //}

        //// tile louading starts
        //void MainMap_OnTileLoadStart()
        //{
        //    System.Windows.Forms.MethodInvoker m = delegate()
        //    {
        //        //progressBar1.Visibility = Visibility.Visible;
        //    };

        //    try
        //    {
        //        this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, m);
        //    }
        //    catch
        //    {
        //    }
        //}

        //// tile loading stops
        //void MainMap_OnTileLoadComplete(long ElapsedMilliseconds)
        //{
        //    MainMap.ElapsedMilliseconds = ElapsedMilliseconds;

        //    System.Windows.Forms.MethodInvoker m = delegate()
        //    {
        //        //progressBar1.Visibility = Visibility.Hidden;
        //        //groupBox3.Header = "loading, last in " + MainMap.ElapsedMilliseconds + "ms";
        //    };

        //    try
        //    {
        //        this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, m);
        //    }
        //    catch
        //    {
        //    }
        //}

        //// current location changed
        //void MainMap_OnCurrentPositionChanged(PointLatLng point)
        //{
        //    //mapgroup.Header = "gmap: " + point;
        //}

        private void SaveDealer()
        {
            if (txtLatitude.Text.Length == 0 || txtLatitude.Text == "0")
            {
                //MapQuestGeoCoder.GeocodeDealer(_selectedDealer);
                GoogleGeocoder.GeocodeDealer(_selectedDealer);
            }
            //TODO put in transation

            DealerService.Save(_selectedDealer);

            //delete all
            DealerService.DeleteDealerZipCodes(_selectedDealer.ID);

            //then add back
            if (_dealerZipCodes != null)
            {
                foreach (ZipGeoCode _zipGeoCode in _dealerZipCodes)
                {
                    DealerService.AddDealerZipCode(new DealerZipCode()
                    {
                        DealerID = _selectedDealer.ID, ZipGeoCodeID = _zipGeoCode.ID
                    });
                }
            }
        }
        private void btnGeoCodeDealers_Click(object sender, RoutedEventArgs e)
        {
            List <Dealer> _dealers = DealerService.GetAll();

            foreach (Dealer _dealer in _dealers)
            {
                BingGeoCoder.GeocodeDealer(_dealer);
                DealerService.Save(_dealer);
            }
        }
        private void LoadDealers()
        {
            Mouse.OverrideCursor = Cursors.Wait;

            List <Dealer> _dealers = DealerService.GetFiltered(txtDealerName.Text, (bool)rdoActiveOnly.IsChecked);

            GridViewDealers.ItemsSource = _dealers;

            foreach (Dealer _dealer in _dealers)
            {
                if (_dealer.Latitude == null)
                {
                    BingGeoCoder.GeocodeDealer(_dealer);
                    DealerService.Save(_dealer);
                }
            }

            Mouse.OverrideCursor = Cursors.Arrow;
        }
        private void btn2StepProcessCreateCSV_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (txtNumberMailings.Text.Length > 0)
                {
                    try
                    {
                        _dealer.DefaultNumberMailings = Int32.Parse(txtNumberMailings.Text);
                    }
                    catch
                    {
                        MessageBox.Show("A numeric number or mailings is required!");
                        return;
                    }
                }
                else
                {
                    return;
                }

                //save the dealer...
                DealerService.Save(_dealer);

                if (Int32.Parse(txtTotalUnsoldCases.Text) >= Int32.Parse(txtNumberMailings.Text))
                {
                    _dealerMailingList = new DealerMailingList()
                    {
                        DealerID = _dealer.ID, FiledCasesOnly = (bool)rdoFiledOnly.IsChecked, CreationDate = DateTime.Now, MarkedAsSold = (bool)chkMarkAsSold.IsChecked, StartFilterDate = (DateTime)rdpStartSelectDate.SelectedDate, EndFilterDate = (DateTime)rdpEndSelectDate.SelectedDate, TemplateFilePath = txtFilePath.Text, NumberCases = Int32.Parse(txtNumberMailings.Text)
                    };
                }
                else
                {
                    MessageBox.Show("The number of available unsold cases is less that the Selected Number of Mailings");
                    return;
                }

                Mouse.OverrideCursor = Cursors.Wait;

                MailMergeHelper _helper = new MailMergeHelper(_dealer, _dealerMailingList);
                //process but don't merge
                try
                {
                    _helper.ProcessMailingList(false);
                    MessageBox.Show("Mail Merge Created!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error Processing Mail merge: " + ex.ToString(), "Error");
                }

                Mouse.OverrideCursor = Cursors.Arrow;
                LoadPreviousMailings();
                LoadMailingTotals();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                Mouse.OverrideCursor = Cursors.Arrow;
            }
        }