protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            using (TodoDBEntities context = new TodoDBEntities())
            {

                    int idPos = int.Parse(this.cmbListTodo.SelectedValue);
                    todo miTodo = context.todos.Single(u => u.idTodo == idPos);
                    context.todos.DeleteObject(miTodo);
                    try
                    {
                        context.SaveChanges();
                    }
                    catch (Exception error)
                    {
                        X.Msg.Show(new MessageBoxConfig
                        {
                            Title = "Error...",
                            Message = error.Message,
                            Buttons = MessageBox.Button.OK,
                            Icon = MessageBox.Icon.ERROR
                        });
                        return;
                    }
                    X.Msg.Show(new MessageBoxConfig
                    {
                        Title = "TO-DO Updated!",
                        Message = "Your TO-DO has been deleted",
                        Buttons = MessageBox.Button.OK,
                        Icon = MessageBox.Icon.INFO
                    });
            }
            Response.Redirect(Request.RawUrl);
        }
        protected void btnSubmitRegister_Click(object sender, DirectEventArgs e)
        {
            if (!this.txtRegisterPassword.Value.Equals(this.txtRegisterRepeat.Value))
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Title = "Invalid Information",
                    Message = "Password doesn't match",
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.ERROR
                });
            }
            else
            {

                    using (TodoDBEntities context = new TodoDBEntities())
                    {
                        usuario newUsuario = new usuario
                        {
                            email = txtRegisterEmail.Value.ToString(),
                            password = FormsAuthentication.HashPasswordForStoringInConfigFile(txtRegisterPassword.Value.ToString(), "SHA1"),
                        };
                        context.usuarios.AddObject(newUsuario);
                        try
                        {
                            context.SaveChanges();
                        }catch(Exception error){
                            X.Msg.Show(new MessageBoxConfig
                            {
                                Title = "Duplicated Email!",
                                Message = "The Email already exist!",
                                Buttons = MessageBox.Button.OK,
                                Icon = MessageBox.Icon.ERROR
                            });
                            return;
                        }

                    }

                X.Msg.Show(new MessageBoxConfig
                {
                    Title = "Welcome!",
                    Message = "You have been registed successfully",
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO
                });
                windowRegister.Hidden = true;
            }
        }
        protected void cmbListTodo_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Si entro cabrones");
            using (TodoDBEntities context = new TodoDBEntities())
            {
                int idPos = int.Parse(this.cmbListTodo.SelectedValue);
                var miusuario = (from u in context.todos
                                 where u.userOwner == User.Identity.Name && u.idTodo == idPos
                                 select u);
                todo temp = miusuario.First();
                this.txtName.SetValue(temp.nombre);
                this.txtDescription.SetValue(temp.descripcion);
                string fechaIn = temp.fechaI.ToShortDateString();
                string[] fechaIns = fechaIn.Split('/');
                string fechaEn = temp.fechaF.ToShortDateString();
                string[] fechaEns = fechaEn.Split('/');
                this.txtFechaInicio.Text = fechaIns[1] + "/" + fechaIns[0] + "/" + fechaIns[2];
                this.txtFechaFinal.Text = fechaEns[1] + "/" + fechaEns[0] + "/" + fechaEns[2];
                string c = temp.status;
                this.cmbStatusReal.SelectedIndex = (c.Equals("O") ? 0 : c.Equals("C") ? 1 : 2);

            }
        }
        private bool ValidateUser(string userName, string passWord)
        {
            using (TodoDBEntities context = new TodoDBEntities())
            {
                string codusuario = this.txtUserName.Value.ToString();

                /*var query=from w in context.wifispots
                    where w.owner==codusuario
                    select w;
                 */

                //otra forma
                //bool band=context.usuarios.Any(u=>u.usuario1==userName && u.clave==password);
                var miusuario = (from u in context.usuarios
                                 where u.email == codusuario
                                 select u);
                string pass = FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtPassword.Value.ToString(),"SHA1");
                if (miusuario.Count() == 0)
                    return false;
                if (pass.Equals(miusuario.First().password))
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            /*if (userName == "pedro" && passWord == "nolose")
            {
                return true;
            }
            else
            {
                return false;
            }*/
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            this.txtFechaFinal.Enabled = false;
            this.txtFechaInicio.Enabled = false;
            this.txtName.Disabled = true;
            this.txtDescription.Disabled = true;
            this.cmbStatusReal.Enabled = false;
            if(!IsPostBack){
                this.lblEmail.Text = User.Identity.Name;
                using (TodoDBEntities context = new TodoDBEntities())
                {
                    var miusuario = (from u in context.todos
                                     where u.userOwner == User.Identity.Name
                                     select u);
                    if (miusuario.Count() == 0)
                    {
                        this.cmbListTodo.Visible = false;
                        this.lblSelectTitle.Text = "You don't have any To-Do.";
                        this.btnRefresh.Disabled = true;
                    }
                    else
                    {
                        if (Session["range"] != null)
                        {
                            if ((bool)Session["range"] == true)
                            {
                                this.chFilter.Checked = true;
                                this.txtStart.Text = Session["fechaI"].ToString();
                                this.txtFinish.Text = Session["fechaF"].ToString();
                            }
                        }
                        foreach (todo u in miusuario)
                        {
                            if (Session["range"] == null || (bool)Session["range"]==false)
                            {
                                System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem(u.nombre,
                                u.idTodo.ToString());
                                this.cmbListTodo.Items.Add(item);
                            }
                            else
                            {
                                if(validateFechas(u.fechaF)){
                                    System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem(u.nombre,
                                    u.idTodo.ToString());
                                    this.cmbListTodo.Items.Add(item);
                                }
                            }
                        }
                        if (cmbListTodo.Items.Count == 0)
                        {
                            this.lblSelectTitle.Text = "There isn't any To-Do in that Date Range";
                            this.cmbListTodo.Visible = false;
                        }
                        else
                        {
                            this.cmbListTodo.SelectedIndex = 0;
                            this.cmbListTodo_SelectedIndexChanged(this, null);
                        }

                    }
                }
            }
        }
        protected void btnAddTodo_Click(object sender, DirectEventArgs e)
        {
            if (this.txtFechaInicio.Text.Equals("") ||
                this.txtFechaFinal.Text.Equals(""))
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Title = "Information Required",
                    Message = "You have to complete Date Fields",
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.ERROR
                });
            }
            else
            {
                using (TodoDBEntities context = new TodoDBEntities())
                {
                    int idPos = int.Parse(this.cmbListTodo.SelectedValue);
                    todo miTodo = context.todos.Single(u => u.idTodo==idPos);

                    string fechaIn=txtFechaInicio.Text;
                    string fechaEn=txtFechaFinal.Text;
                    string []fechaIns=fechaIn.Split('/');
                    string []fechaEns=fechaEn.Split('/');
                    int index=this.cmbStatusReal.SelectedIndex;
                    string val=(index==0?"O":index==1?"C":"D");
                    miTodo.nombre = txtName.Value.ToString();
                    miTodo.descripcion = txtDescription.Value.ToString();
                    miTodo.fechaI=new DateTime(int.Parse(fechaIns[2]),
                            int.Parse(fechaIns[0]),
                            int.Parse(fechaIns[1]));

                    miTodo.fechaF=new DateTime(int.Parse(fechaEns[2]),
                            int.Parse(fechaEns[0]),
                            int.Parse(fechaEns[1]));
                    miTodo.status=val;
                    try
                    {
                        context.SaveChanges();
                    }
                    catch (Exception error)
                    {
                        X.Msg.Show(new MessageBoxConfig
                        {
                            Title = "Error...",
                            Message = error.Message,
                            Buttons = MessageBox.Button.OK,
                            Icon = MessageBox.Icon.ERROR
                        });
                        return;
                    }

                }

                X.Msg.Show(new MessageBoxConfig
                {
                    Title = "TO-DO Updated!",
                    Message = "Your TO-DO has been updated",
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO
                });
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                System.Diagnostics.Debug.WriteLine("SI ENTRO PUES");
                this.lblEmail.Text = User.Identity.Name;

                using (TodoDBEntities context = new TodoDBEntities())
                {
                    var miusuario = (from u in context.todos
                                     where u.userOwner == User.Identity.Name
                                     select u);
                    if (miusuario.Count() == 0)
                    {
                        this.btnAddTodo.Disabled=true;
                        this.btnDelete.Disabled = true;
                        this.cmbListTodo.Visible = false;
                        this.lblSelectTitle.Text = "You don't have any To-Do.";
                    }
                    else
                    {
                        foreach (todo u in miusuario)
                        {
                            System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem(u.nombre,
                                u.idTodo.ToString());
                            this.cmbListTodo.Items.Add(item);
                        }
                        this.cmbListTodo.SelectedIndex = 0;
                        this.cmbListTodo_SelectedIndexChanged(this, null);
                    }
                }

            }
        }
        protected void btnAddTodo_Click(object sender, DirectEventArgs e)
        {
            if (this.txtFechaInicio.Text.Equals("") ||
                this.txtFechaFinal.Text.Equals(""))
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Title = "Information Required",
                    Message = "You have to complete Date Fields",
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.ERROR
                });
            }
            else
            {
                using (TodoDBEntities context = new TodoDBEntities())
                {
                    string fechaIn=txtFechaInicio.Text;
                    string fechaEn=txtFechaFinal.Text;
                    string []fechaIns=fechaIn.Split('/');
                    string []fechaEns=fechaEn.Split('/');
                    int index=this.cmbStatusReal.SelectedIndex;
                    string val=(index==0?"O":index==1?"C":"D");
                    todo newTodo=new todo{
                        nombre=txtName.Value.ToString(),
                        descripcion=txtDescription.Value.ToString(),

                        fechaI=new DateTime(int.Parse(fechaIns[2]),
                            int.Parse(fechaIns[0]),
                            int.Parse(fechaIns[1])),
                        fechaF=new DateTime(int.Parse(fechaEns[2]),
                            int.Parse(fechaEns[0]),
                            int.Parse(fechaEns[1])),
                        status=val,
                        userOwner=User.Identity.Name
                    };

                    context.todos.AddObject(newTodo);
                    try
                    {
                        context.SaveChanges();
                    }
                    catch (Exception error)
                    {
                        X.Msg.Show(new MessageBoxConfig
                        {
                            Title = "Error...",
                            Message = error.Message,
                            Buttons = MessageBox.Button.OK,
                            Icon = MessageBox.Icon.ERROR
                        });
                        return;
                    }

                }

                X.Msg.Show(new MessageBoxConfig
                {
                    Title = "TO-DO Added!",
                    Message = "Your new TO-DO has been added",
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO
                });
                resetInformation();
            }
        }