//GET
        public ActionResult MainSite(int id = 0)
        {
            Customer customer = db.Customers.Find(id);
            CustomerSite site = new CustomerSite();
            ViewBag.customerName = customer.customerName;
            site.visitingStreet = customer.visitingStreet;
            site.visitingCity = customer.visitingCity;
            site.visitingPostal = customer.visitingPostal;
            site.visitingCountry = customer.visitingCountry;

            site.postalStreet = customer.postalStreet;
            site.postalCity = customer.postalCity;
            site.postalPostal = customer.postalPostal;
            site.postalCountry = customer.postalCountry;

            site.contactAddressStreet = customer.contactAddressStreet;
            site.contactAddressCountry = customer.contactAddressCountry;
            site.contactAddressPostal = customer.contactAddressPostal;
            site.contactAddressCity = customer.contactAddressCity;

            site.contactEmail = customer.contactEmail;
            site.contactName = customer.contactName;
            site.contactPhone = customer.contactPhone;
            site.contactTitle = customer.contactTitle;

            ViewBag.customerSiteStatus = new SelectList(db.CustomerStatus, "customerStatusNo", "customerStatusText");
            ViewBag.wayOfDelivery = new SelectList(db.WayOfDeliveries, "wodNo", "wodText");

            return View("Create", site);
        }
        public ActionResult Create(CustomerSite customersite)
        {
            var customer = db.Customers.First(x => x.customerNo == customersite.customerNo);
            customer.customerNo = customersite.customerNo;
            if (customersite.visitingStreet == null)
            {
                customersite.visitingStreet = customersite.postalStreet;
            }
            if (customersite.visitingPostal == null)
            {
                customersite.visitingPostal = customersite.postalPostal;
            }
            if (customersite.visitingCity == null)
            {
                customersite.visitingCity = customersite.postalCity;
            }
            if (customersite.visitingCountry == null)
            {
                customersite.visitingCountry = customersite.postalCountry;  
            }
            customersite.paymentDisruption = 0;
            customersite.customerSiteStatus = 1;
            customersite.dateOfTheLastSale = null;
            customersite.sales = 0;

            if (ModelState.IsValid)
            {
                db.CustomerSites.Add(customersite);
                db.SaveChanges();
                return RedirectToAction("Details", new { id = customersite.customerSiteNo });
            }
            ViewBag.customerNo = customer.customerNo;
            ViewBag.customerName = customer.customerName;
            ViewBag.customerSiteStatus = new SelectList(db.CustomerStatus, "customerStatusNo", "customerStatusText",
                customersite.customerSiteStatus);
            ViewBag.paymentDisruption = new SelectList(db.PaymentDisruptions, "paymentDisruptionNo",
                "paymentDisruptionText", customersite.paymentDisruption);
            ViewBag.wayOfDelivery = new SelectList(db.WayOfDeliveries, "wodNo", "wodText", customersite.wayOfDelivery);
            return View(customersite);
        }
        public ActionResult Edit(CustomerSite customersite)
        {
            if (ModelState.IsValid)
            {
                db.Entry(customersite).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Details", new {id = customersite.customerSiteNo });
            }
            List<SelectListItem> dropDownItems;

            //get all countries and put them into list of select list items
            dropDownItems = GetAllCountries();
            // sort list into alphabetical order
            var items = from i in dropDownItems orderby i.Text == customersite.visitingCountry, i.Text ascending select i;
            //transfer country data into view using viewbag 
            ViewBag.countries = items;
            ViewBag.customer = new SelectList(db.Customers, "customerNo", "alphabeticID", customersite.customerNo);
            ViewBag.customerSiteStatus = new SelectList(db.CustomerStatus, "customerStatusNo", "customerStatusText",
                customersite.customerSiteStatus);
            ViewBag.paymentDisruption = new SelectList(db.PaymentDisruptions, "paymentDisruptionNo",
                "paymentDisruptionText", customersite.paymentDisruption);
            ViewBag.wayOfDelivery = new SelectList(db.WayOfDeliveries, "wodNo", "wodText", customersite.wayOfDelivery);
            return View(customersite);
        }
        public ActionResult Create(Customer customer)
        {
            // Remove customerNo from modelstate so it allows customerNo to be added last.         
            // Problem was that the modelstate was still expecting the customerNo to be added at the create new customer page
            ModelState.Remove("customerNo");
            if (customer.visitingStreet == null)
            {
                customer.visitingStreet = customer.postalStreet;
            }
            if (customer.visitingPostal == null)
            {
                customer.visitingPostal = customer.postalPostal;
            }
            if (customer.visitingCity == null)
            {
                customer.visitingCity = customer.postalCity;
            }
            if (customer.visitingCountry == null)
            {
                customer.visitingCountry = customer.postalCountry;
            }

            if (ModelState.IsValid)
            {
                
                // generate new customer id
                var lastId = db.Customers.Max(item => item.customerNo);
                var newId = lastId + 1;
                customer.customerNo = newId;

                CustomerSite site = new CustomerSite();
                site = TempData["siteNumber"] as CustomerSite;

                if (site != null)
                {
                    site.customerNo = customer.customerNo;
                    db.Customers.Add(customer);
                    db.SaveChanges();

                    db.Entry(site).State = EntityState.Modified;
                    db.SaveChanges();

                    return RedirectToAction("Index");
                }

                db.Customers.Add(customer);
                db.SaveChanges();
              
                return RedirectToAction("Index");
            }

            ViewBag.customerGroupCode = new SelectList(db.CustomerGroups, "customerGroupCode", "customerGroupName",
                customer.customerGroupCode);
            ViewBag.customerStatus = new SelectList(db.CustomerStatus, "customerStatusNo", "customerStatusText",
                customer.customerStatus);
            ViewBag.termsOfDelivery = new SelectList(db.TermsOfDeliveries, "todNo", "todText", customer.termsOfDelivery);
            ViewBag.termsOfPayment = new SelectList(db.TermsOfPayments, "topNo", "topText", customer.termsOfPayment);

            //get all countries and put them into list of select list items
            var dropDownItems2 = GetAllCountries();
            // sort list into alphabetical order
            var items2 = from i in dropDownItems2 orderby i.Text select i;
            //transfer country data into view using viewbag 
            ViewBag.countries = items2;
            return View(customer);
        }