public ActionResult Register(Customer newUser, String password_confirmation)
        {
            if (ModelState.IsValid)
            {
                if (!newUser.password.Equals(password_confirmation))
                {
                    ViewBag.confirmation = true;
                    return View();
                }
                var customerDB = new DBCustomer();

                if (!customerDB.checkEmail(newUser.email,null))
                {
                    ViewBag.email = true;
                    return View();
                }
                else
                {

                    byte[] hashedPassword = makeHash(newUser.password);
                    bool insertOK = customerDB.add(newUser, hashedPassword);
                    if (insertOK)
                    {
                        logInUser(newUser.email);
                        return RedirectToAction("PersonalSite");
                    }
                }
            }
            return View();
        }
        public bool update(int id, Customer updateUser)
        {
            var db = new DatabaseContext();
            try
            {
                Customers cust = db.Customers.FirstOrDefault(u => u.Id == id);
           
                cust.Firstname = updateUser.firstname;
                cust.Lastname = updateUser.lastname;
                cust.Address = updateUser.address;
                cust.PostalareasId = Convert.ToInt16(updateUser.postalcode);
                cust.Phonenumber = updateUser.phonenumber;
                cust.Email = updateUser.email;
            
                var existPostalcode = db.Postalareas.Find(Convert.ToInt16(updateUser.postalcode));

                if (existPostalcode == null)
                {
                    var newPostalarea = new Postalareas()
                    {
                        PostalareasId = Convert.ToInt16(updateUser.postalcode),
                        Postalarea = updateUser.postalarea
                    };
                    cust.Postalareas = newPostalarea;
                }
                db.SaveChanges();
                return true;
            }
            catch (Exception fail)
            {
                return false;
               
            }
        }
 public Customer findCustomer(String email)
 {
     var db = new DatabaseContext();
     Customers userFound =  db.Customers.FirstOrDefault(u => u.Email == email);
     Customer c = new Customer();
     c.id = userFound.Id;
     c.firstname = userFound.Firstname;
     c.lastname = userFound.Lastname;
     c.email = userFound.Email;
     c.phonenumber = userFound.Phonenumber;
     c.address = userFound.Address;
     c.postalcode = userFound.PostalareasId.ToString();
     c.postalarea = db.Postalareas.Find(userFound.PostalareasId).Postalarea;
     c.hashpassword = userFound.Password;
      return c;
 }
        public bool add(Customer inCustomer, byte[] hashedPassword)
        {
            var newCustomer = new Customers()
            {
                Firstname = inCustomer.firstname,
                Lastname = inCustomer.lastname,
                Address = inCustomer.address,
                PostalareasId = Convert.ToInt16(inCustomer.postalcode),
                Password = hashedPassword,
                Phonenumber = inCustomer.phonenumber,
                Email = inCustomer.email
            };

            var db = new DatabaseContext();
            try
            {
                var existPostalcode = db.Postalareas.Find(Convert.ToInt16(inCustomer.postalcode));

                if(existPostalcode == null )
                {
                    var newPostalarea = new Postalareas()
                    {
                        PostalareasId = Convert.ToInt16(inCustomer.postalcode),
                        Postalarea = inCustomer.postalarea
                    };
                    newCustomer.Postalareas = newPostalarea;
                }
                db.Customers.Add(newCustomer);
                db.SaveChanges();
                return true;
            }
            catch (Exception fail)
            {
                return false;
            }
        }
        public ActionResult updateUserinfo(Customer newUser)
        {

            if (ModelState.IsValid)
            {
                Customer c = (Customer)Session["loggedInUser"];
                var customerDB = new DBCustomer(); 
                if (!customerDB.checkEmail(newUser.email,c.id))
                {
                    ViewBag.ok = "email er i bruk av annen bruker, velg en annen";
                    return View();
                }
       
                  c.firstname = newUser.firstname;
                  c.lastname = newUser.lastname;
                  c.email = newUser.email;
                  c.phonenumber = newUser.phonenumber;
                  c.address = newUser.address;
                  c.postalcode = newUser.postalcode;
                  c.postalarea = newUser.postalarea;
                  
                    bool updateOK = customerDB.update(c.id, c);
                
                    if (updateOK)
                    {
                        Session["loggedInUser"] = c;
                        TempData["changed"] = "Brukerinformasjon ble oppdatert";
                        return RedirectToAction("PersonalSite"); 
                    }
                    else
                    {
                       Customer old = (Customer)Session["loggedInUser"];
                       ViewBag.ok = "klarte ikke oppdatere"; 
                       return View();
                    }
            }
            ViewBag.ok = "et felt er blankt, fyll det ut og trykk oppdater";
            return View();
        }