protected void _grdZones_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        string _unitID = Request.QueryString["unitID"].ToString();
        if (e.CommandName == "Edit")
        {

            Label geofenceID = (Label)e.Item.FindControl("_lblGeofenceID");
            Session["geofenceID"] = geofenceID.Text;
            Response.Redirect("~/Map/SafetyZoneSetup.aspx?unitID=" + _unitID + "");
        }
        else if (e.CommandName == "Delete")
        {

            Geofence _gf = new Geofence();
            ProcessSafetyZone _zone = new ProcessSafetyZone(AlarmasABC.BLL.InvokeOperations.operations.DELETE);

            Label geofenceID = (Label)e.Item.FindControl("_lblGeofenceID");
            string _comID = Session["trkCompany"].ToString();

            try
            {
                _gf.UnitID = int.Parse(_unitID);
                _gf.Id = int.Parse(geofenceID.Text);
                _gf.ComID = int.Parse(_comID);

                _zone.GeoInfo = _gf;
                _zone.invoke();
                LoadZoneList(_unitID);

                _lblMessage.Text = "Safety Zone Deleted Successfully.";

                if (_grdZones.Items.Count < 1)
                {
                    _lblMessage.Text = "No Safety Zone for this Unit";
                    _grdZones.Visible = false;
                }
            }
            catch(Exception ex)
            {

            }

        }
    }
    private void saveGeofence(double _lat, double _lng, decimal _radius)
    {
        try
        {

            string _comID = Session["trkCompany"].ToString();
            string _listValue = _hListValue.Value;
            string[] _listItem = _listValue.Split(';');
            bool isActive = _rdoON.Checked;
            if (_rdUnits.Checked)
            {

                for (int i = 0; i < _listItem.Length - 1; i++)
                {
                    ProcessSafetyZone _unitInsert = new ProcessSafetyZone();
                    Geofence _geoObj = new Geofence();

                    _geoObj.ComID = int.Parse(_comID);
                    _geoObj.UnitID = int.Parse(_listItem[i].ToString());
                    _geoObj.Name = _txtZoneName.Text.ToString();
                    _geoObj.Email =_txtEmail.Text.ToString() ;
                    _geoObj.CenterLat = decimal.Parse(_lat.ToString()) ;
                    _geoObj.CenterLong =decimal.Parse(_lng.ToString() );
                    _geoObj.Radius = _radius ;
                    _geoObj.IsActive = isActive;

                    _unitInsert.GeoInfo = _geoObj;
                    _unitInsert.GeofenceUnitInsert();

                    //_lblMessage.Text = "Sucessfull";

                }

            }

            else if (_rdUnitGroup.Checked)
            {
                for (int i = 0; i < _listItem.Length - 1; i++)
                {
                    ProcessSafetyZone _unitInsert = new ProcessSafetyZone();
                    Geofence _geoObj = new Geofence();

                    _geoObj.ComID = int.Parse(_comID);
                    _geoObj.UnitGroupID = int.Parse(_listItem[i].ToString());
                    _geoObj.Name = _txtZoneName.Text.ToString();
                    _geoObj.Email = _txtEmail.Text.ToString();
                    _geoObj.CenterLat = decimal.Parse(_lat.ToString());
                    _geoObj.CenterLong = decimal.Parse(_lng.ToString());
                    _geoObj.Radius = _radius;
                    _geoObj.IsActive = isActive;

                    _unitInsert.GeoInfo = _geoObj;
                    _unitInsert.GeofenceUnitGroupInsert();
                }
            }
        }

        catch (Exception ex)
        {
            throw new Exception (" XtremeK::Map::Geofence " + ex.Message);
        }
    }
    private void updateGeofence()
    {
        try
        {
            GoogleLocation _location = GoogleLocation.Parse(_centerPoint.Value);
            Geofence _geoObj = new Geofence();

            ProcessSafetyZone _safetyZoneUpdate = new ProcessSafetyZone(AlarmasABC.BLL.InvokeOperations.operations.UPDATE);
            _geoObj.Id = int.Parse(geofenceID);
            _geoObj.ComID = int.Parse(Session["trkCompany"].ToString());
            _geoObj.Name = _txtZoneName.Text.ToString();
            _geoObj.Email = _txtEmail.Text.ToString();
            _geoObj.UnitID = int.Parse(Request.QueryString["unitID"].ToString());
            _geoObj.CenterLat = decimal.Parse(_location.Latitude.ToString());
            _geoObj.CenterLong = decimal.Parse(_location.Longitude.ToString());
            _geoObj.Radius = decimal.Parse(_Radius.Value);
            _geoObj.IsActive = _rdoON.Checked;
            _geoObj.SpeedingPhoneNum = _txtPhoneNumber.Text.Trim();
            _geoObj.IsSMS = _ckOnSMS.Checked;

            _safetyZoneUpdate.GeoInfo = _geoObj;
            _safetyZoneUpdate.invoke();

            _lblMessage.ForeColor = System.Drawing.Color.Green;
            _lblMessage.Text = "Geofence update successfully ";
            _Update.Value = "";

        }
        catch (Exception ex)
        {
            ex.Message.ToString();

            _lblMessage.ForeColor = System.Drawing.Color.Red;
            _lblMessage.Text = "Geofence update fail";

        }
    }
    private void loadGeofence(string ID)
    {
        double _lat, _lng;
        string deviceID = getDeviceID(Request.QueryString["unitID"].ToString());
        try
        {
            string _sql = "SELECT lat,long FROM tblGPRS WHERE deviceID = " + deviceID +
                          " AND recTime= (SELECT MAX(recTime) FROM tblGPRS WHERE deviceID = " + deviceID + " AND direction = 0);";

            ExecuteSQL exeSql = new ExecuteSQL();
            DataTable _dt = new DataTable();

            _dt = exeSql.ExecuteSQLQuery(_sql);

            _lng = double.Parse(_dt.Rows[0]["long"].ToString());
            _lat = double.Parse(_dt.Rows[0]["lat"].ToString());

            string _name = "";
            string _Rad = "";

            ProcessSafetyZone _pGeoData = new ProcessSafetyZone();
            Geofence _geoInput = new Geofence();

            _geoInput.ComID = int.Parse(Session["trkCompany"].ToString());
            if (ID != "")
                _geoInput.Id = int.Parse(ID);
            _geoInput.UnitID = int.Parse(Request.QueryString["unitID"].ToString());
            _pGeoData.GeoInfo = _geoInput;
            _pGeoData.LoadGeoData();

            DataSet _ds = new DataSet();
            _ds = _pGeoData.Ds;

            StringBuilder bfMarker = new StringBuilder();
            StringBuilder bfMarkerScript = new StringBuilder();

                if (_ds.Tables[0].Rows.Count > 0)
                {
                    _name = _ds.Tables[0].Rows[0]["name"].ToString();
                    _Rad = _ds.Tables[0].Rows[0]["radius"].ToString();
                    _txtEmail.Text = _ds.Tables[0].Rows[0]["email"].ToString();

                    try
                    {
                        _txtPhoneNumber.Text = _ds.Tables[0].Rows[0]["phoneNumber"].ToString();
                        bool isSMS = bool.Parse(_ds.Tables[0].Rows[0]["isSMS"].ToString());
                        if (isSMS)
                        {
                            _ckOnSMS.Checked = true;
                            _chkOffSMS.Checked = false;
                        }
                        else
                        {
                            _ckOnSMS.Checked = false;
                            _chkOffSMS.Checked = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!_ckOnSMS.Checked)
                            _chkOffSMS.Checked = true;
                    }

                    if (_ds.Tables[0].Rows.Count > 0)
                    {

                        if (_ds.Tables[0].Rows[0]["isActive"].ToString() == "True")
                            _rdoON.Checked = bool.Parse(_ds.Tables[0].Rows[0]["isActive"].ToString());
                        else
                            _rdoOFF.Checked = true;
                    }

                    _txtZoneName.Text = _name;
                    _lblRadius.Text = _Rad;

                     double _radius = ((double.Parse(_Rad) / 1.60934) * 5280) / 3.2808399;

                    _Radius.Value = _ds.Tables[0].Rows[0]["Radius"].ToString();
                    _Lat.Value = _ds.Tables[0].Rows[0]["centerLat"].ToString();
                    _Long.Value = _ds.Tables[0].Rows[0]["centerLng"].ToString();

                    string _imageName = getImageName();

                    bfMarker.Append("<script language=\"javascript\">");

                    bfMarker.Append("   myMap = new GMap2(document.getElementById('map')); ");
                    bfMarker.Append(" var rad = document.getElementById('_Radius');");
                    bfMarker.Append(" var gName = document.getElementById('_txtGeofenceName');");
                    bfMarker.Append(" var _Lat = document.getElementById('_Lat');");
                    bfMarker.Append(" var _Long = document.getElementById('_Long'); ");

                    bfMarker.Append(" myMap.addControl(new GSmallMapControl());");
                    bfMarker.Append(" myMap.addControl(new GMapTypeControl());");
                    bfMarker.Append(" myMap.enableScrollWheelZoom();");
                    bfMarker.Append(" myMap.setCenter(new GLatLng(_Lat.value,_Long.value),15,G_HYBRID_MAP);");
                    bfMarker.Append(" GEvent.addListener(myMap,'click',clickedMap);");

                    bfMarker.Append(" var _radius=((rad.value/1.60934)*5280)/3.2808399;  ");
                    bfMarker.Append(" createCircle(new GLatLng(_Lat.value,_Long.value),_radius);");
                    bfMarker.Append(" clear=true;");

                    bfMarkerScript.Append(" cPoint=new GLatLng(" + _lat + "," + _lng + ");");
                    bfMarkerScript.Append(" var icon0 = new GIcon();");
                    bfMarkerScript.Append(" icon0.image = \\'../Icon/" + _imageName + ".png\\';");
                    bfMarkerScript.Append(" icon0.iconSize = new GSize(" + iconHeight + "," + iconWidth + ");");
                    bfMarkerScript.Append(" icon0.iconAnchor = new GPoint(" + iconHeight / 2 + "," + iconWidth / 2 + ");");
                    bfMarkerScript.Append(" icon0.infoWindowAnchor = new GPoint(15, 15);");
                    bfMarkerScript.Append(" icon0.infoShadowAnchor = new GPoint(18, 25);");
                    bfMarkerScript.Append(" var marker=new GMarker(cPoint,icon0);");

                    bfMarkerScript.Append(" myMap.addOverlay(marker,icon0);");

                    bfMarker.Append(" setTimeout('evalMarker(\"" + bfMarkerScript.ToString() + "\")',1000); ");
                    bfMarker.Append("</script>");
                    Page.RegisterStartupScript("marker", bfMarker.ToString());

             }

            else
            {
                try
                {
                    string _iconName = getImageName();

                    bfMarker.Append("<script language=\"javascript\">");

                    bfMarker.Append("   myMap = new GMap2(document.getElementById('map')); ");
                    bfMarker.Append(" myMap.addControl(new GSmallMapControl()); ");
                    bfMarker.Append(" myMap.addControl(new GMapTypeControl()); ");
                    bfMarker.Append(" myMap.enableScrollWheelZoom();");
                    bfMarkerScript.Append(" myMap.setCenter(new GLatLng(" + _lat + "," + _lng + "),14,G_HYBRID_MAP);");
                    bfMarker.Append(" GEvent.addListener(myMap,'click',clickedMap);");

                    bfMarkerScript.Append(" cPoint=new GLatLng(" + _lat + "," + _lng + ");");
                    bfMarkerScript.Append(" var icon0 = new GIcon();");
                    bfMarkerScript.Append(" icon0.image = \\'../Icon/" + _iconName + ".png\\';");
                    bfMarkerScript.Append(" icon0.iconSize = new GSize(" + iconHeight + "," + iconWidth + ");");
                    bfMarkerScript.Append(" icon0.iconAnchor = new GPoint(" + iconHeight / 2 + "," + iconWidth / 2 + ");");
                    bfMarkerScript.Append(" icon0.infoWindowAnchor = new GPoint(15, 15);");
                    bfMarkerScript.Append(" icon0.infoShadowAnchor = new GPoint(18, 25);");
                    bfMarkerScript.Append(" var marker=new GMarker(cPoint,icon0);");

                    bfMarkerScript.Append(" myMap.addOverlay(marker);");

                    bfMarker.Append(" setTimeout('evalMarker(\"" + bfMarkerScript.ToString() + "\")',100); ");
                    bfMarker.Append("</script>");
                    Page.RegisterStartupScript("marker", bfMarker.ToString());

                }

                catch (Exception ex)
                {
                    throw new Exception(ex.Message.ToString());
                }

            }
        }
        catch (Exception ex)
        {
            throw new Exception("loadGeofence(): " + ex.Message.ToString());

        }
    }
    private void LoadZoneList(string _unitID)
    {
        DataSet _ds = new DataSet();
        int _comID = int.Parse(Session["trkCompany"].ToString());
        ProcessSafetyZone _zoneList = new ProcessSafetyZone(AlarmasABC.BLL.InvokeOperations.operations.SELECT);

        try
        {
            _zoneList.ComID = _comID;
            _zoneList.UnitID = int.Parse(_unitID);
            _zoneList.invoke();
            _ds = _zoneList.Ds;
            _grdZones.DataSource = _ds;
            _grdZones.DataBind();
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
        }

        if (_grdZones.Items.Count < 1)
        {
            _grdZones.Visible = false;
            _lblMessage.Text = "No zones are defined for this unit. Click the \"Create New Zone\" button to add one.";
            _lblMessage.CssClass = "";
            //Response.Redirect("~/Map/SafetyZoneSetup.aspx?unitID="+_unitID+"");
        }
    }