public async Task <JsonResult> RentCar(string ln, string id)
        {
            if (ModelState.IsValid)
            {
                HashSet <string>            hs1 = (HashSet <string>)Session["Veichles"];
                HashSet <string>            hs2 = (HashSet <string>)Session["Costumers"];
                Dictionary <string, string> d   = (Dictionary <string, string>)Session["RentedCars"];
                if (!hs1.Contains(ln) || !hs2.Contains(id) || (d.ContainsKey(id) && d[id].Equals(ln))) // checking if car and costumer are exist.
                {
                    return(await Task.Run(() => Json(1, JsonRequestBehavior.AllowGet)));
                }

                using (var db = new FleetMSEntities())
                {
                    try
                    {
                        var rc = new RentedCars();

                        //List<Costumer> costumerList = (List<Costumer>)Session["CostumersArray"];
                        //List<Veichle> veichleList = (List<Veichle>)Session["VeichlesArray"];
                        //rc.Costumer = Costumer.searchCostumer(costumers, id);
                        //rc.Veichle = Veichle.searchVeichles(veichles, ln);

                        Costumer[] costumers = await db.Costumer.ToArrayAsync();

                        Veichle[] veichles = await db.Veichle.ToArrayAsync();

                        rc.Costumer = Costumer.searchCostumer(costumers.ToList(), id); //find costumer
                        rc.Veichle  = Veichle.searchVeichles(veichles.ToList(), ln);   //find veichle
                        if (d.ContainsKey(rc.Costumer.Id))
                        {
                            return(await Task.Run(() => Json(2, JsonRequestBehavior.AllowGet)));
                        }

                        db.RentedCars.Add(rc);
                        await db.SaveChangesAsync();

                        //updating sessions
                        d.Add(rc.Costumer.Id, rc.Veichle.LicenseNumber);
                        Session["RentedCars"] = d;

                        return(await Task.Run(() => Json(0, JsonRequestBehavior.AllowGet)));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Source);
                    }
                }
            }
            return(await Task.Run(() => Json(1, JsonRequestBehavior.AllowGet)));
        }
        public async Task <JsonResult> RemoveCar(string data)
        {
            if (ModelState.IsValid)
            {
                HashSet <string> hs = (HashSet <string>)Session["Veichles"];
                if (!hs.Contains(data)) // checking if car exist.
                {
                    return(await Task.Run(() => Json(1, JsonRequestBehavior.AllowGet)));
                }
                Dictionary <string, string> d = (Dictionary <string, string>)Session["RentedCars"];
                if (d.ContainsValue(data))  // check if the car is rented.
                {
                    return(await Task.Run(() => Json(2, JsonRequestBehavior.AllowGet)));
                }
                using (var db = new FleetMSEntities())
                {
                    try
                    {
                        List <Veichle> list     = (List <Veichle>)Session["VeichlesArray"]; //remove car from list
                        Veichle        toRemove = Veichle.searchVeichles(list, data);


                        db.Veichle.Attach(toRemove);
                        db.Veichle.Remove(toRemove);
                        await db.SaveChangesAsync();

                        //updating sessions
                        list.Remove(toRemove);
                        Session["VeichlesArray"] = list;
                        hs.Remove(data);
                        Session["Veichles"] = hs;

                        return(await Task.Run(() => Json(0, JsonRequestBehavior.AllowGet)));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Source);
                    }
                }
            }
            return(await Task.Run(() => Json(1, JsonRequestBehavior.AllowGet)));
        }
        public async Task <JsonResult> EditCar(string name, string ln, int model, int price, int seatsNumber, string company, string hook, string box, string trucktype, string awning, string volume)
        {
            bool HookParam = false, AwningParam = false, BoxParam = false;

            if (hook.Equals("true"))
            {
                HookParam = true;
            }
            if (awning.Equals("true"))
            {
                AwningParam = true;
            }
            if (box.Equals("true"))
            {
                BoxParam = true;
            }
            if (ModelState.IsValid)
            {
                using (var db = new FleetMSEntities())
                {
                    Veichle car;
                    if (((string)Session["EditType"]).Equals("Truck")) //check if type is truck
                    {
                        car = new Truck(name, ln, model, price, seatsNumber, company, HookParam, trucktype);
                    }
                    else if (((string)Session["EditType"]).Equals("Van"))  //check if type is Van
                    {
                        car = new Van(name, ln, model, price, seatsNumber, company, int.Parse(volume), AwningParam);
                    }
                    else if (((string)Session["EditType"]).Equals("Tender"))  //check if type is Tender
                    {
                        car = new Tender(name, ln, model, price, seatsNumber, company, HookParam, BoxParam);
                    }
                    else
                    {
                        car = new PrivateCar(name, ln, model, price, seatsNumber, company);
                    }
                    try
                    {
                        List <Veichle> v        = (List <Veichle>)Session["VeichlesArray"];
                        Veichle        toRemove = Veichle.searchVeichles(v, ln);

                        db.Veichle.Attach(toRemove);
                        db.Veichle.Remove(toRemove); //remove from db
                        if (car is Truck)
                        {
                            db.Veichle.Add(((Truck)car));
                        }
                        else if (car is Tender)
                        {
                            db.Veichle.Add(((Tender)car));
                        }
                        else if (car is Van)
                        {
                            db.Veichle.Add(((Van)car));
                        }
                        else if (car is PrivateCar)
                        {
                            db.Veichle.Add(((PrivateCar)car));
                        }
                        await db.SaveChangesAsync();

                        //updating sessions
                        v.Remove(toRemove);
                        v.Add(car);
                        Session["VeichlesArray"] = v;

                        return(await Task.Run(() => Json(0, JsonRequestBehavior.AllowGet)));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Source);
                    }
                }
            }
            return(await Task.Run(() => Json(1, JsonRequestBehavior.AllowGet)));
        }