// POST api/PossibleAnswer
        public HttpResponseMessage PostPossibleAnswer(PossibleAnswer possibleanswer)
        {
            if (ModelState.IsValid)
            {
                db.PossibleAnswers.Add(possibleanswer);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, possibleanswer);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = possibleanswer.PossibleAnswerID }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
        // PUT api/PossibleAnswer/5
        public HttpResponseMessage PutPossibleAnswer(int id, PossibleAnswer possibleanswer)
        {
            if (ModelState.IsValid && id == possibleanswer.PossibleAnswerID)
            {
                db.Entry(possibleanswer).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
        public ActionResult EditQuestions(FormCollection collection)
        {
            Enquete e = db.Enquetes.Find(int.Parse(collection["Enquete_id"]));
            if (!db.Enrollments.Where(en => en.UserID == WebSecurity.CurrentUserId && en.role == EnrollmentRole.Owner).Select(en => en.EnqueteID).Contains(e.EnqueteID))
            {
                return RedirectToAction("Index", "EnqueteBuilder");
            }
            foreach(Question q in e.Questions)
            {
                if (q.Type == QuestionType.Checkbox || q.Type == QuestionType.MultipleChoice)
                {
                    for (int a = q.PossibleAnswers.Count - 1; a >= 0;--a)
                    {
                        db.PossibleAnswers.Remove(q.PossibleAnswers.ElementAt(a));
                    }
                }
            }
            db.SaveChanges();
            var questionCount = int.Parse(collection["questionCount"]);
            questionCount--;
            var questions = new Dictionary<string,Question>();
            for (var a = 1;a <= questionCount;++a)
            {
                var c = e.Questions.Where(qu => qu.QuestionNumber == a);
                Question q;
                if (c.Count() == 0)
                {
                    q = new Question();
                }
                else
                {
                    q = c.First();
                    db.Entry(q).State = EntityState.Modified;
                }
                q.EnqueteID = e.EnqueteID;
                q.Enquete = e;
                q.QuestionNumber = a;
                q.Text = collection["Question_" + a + "_Text"]; //Question_1_Text
                q.Type = (QuestionType)Enum.Parse(typeof(QuestionType),collection["Question_" + a + "_Type"]); //Question_1_Type
                questions.Add(a.ToString(), q);
            }
            var allPossibleAnswers = new Dictionary<int, ICollection<PossibleAnswer>>();
            for (var a = 1; a <= questionCount; ++a)//Question_1_Next
            {
                var q = questions[a.ToString()];
                int tmp;
                if (int.TryParse(collection["Question_" + a + "_Next"],out tmp))
                {
                    q.NextQuestionID = questions[tmp+""].QuestionID;
                    q.NextQuestion = questions[tmp+""];
                }
                if (q.Type == QuestionType.MultipleChoice || q.Type == QuestionType.Checkbox) // type == multiplechoice
                {
                    var answerCount = int.Parse(collection["Question_" + a + "_AnswerCount"]);
                    List<PossibleAnswer> possibleAnswers = new List<PossibleAnswer>();
                    for (var b = 1; b <= answerCount; ++b) //Question_1_AnswerCount
                    {
                        var possibleAnswer = new PossibleAnswer();
                        possibleAnswer.Text = collection["Question_" + a + "_Answer_" + b + "_Text"]; //Question_1_Answer_1_Text
                        if (int.TryParse(collection["Question_" + a + "_Answer_" + b + "_Next"], out tmp))
                        {
                            possibleAnswer.NextQuestionID = questions[tmp + ""].QuestionID;
                            possibleAnswer.NextQuestion = questions[tmp + ""];
                        }
                        else
                        {
                            possibleAnswer.NextQuestionID = q.NextQuestionID;
                            possibleAnswer.NextQuestion = q;
                        }
                        possibleAnswer.QuestionID = q.QuestionID;
                        possibleAnswer.Question = q;
                        possibleAnswers.Add(possibleAnswer);
                    }
                    q.PossibleAnswers = possibleAnswers;
                    allPossibleAnswers[q.QuestionNumber] = possibleAnswers;
                }
            }
            var questionList = new List<Question>(questionCount);
            for (var a = 1; a <= questionCount; ++a)
            {
                questionList.Insert(a - 1, questions[a.ToString()]);
                e.Questions = questionList;
            }
            if (ModelState.IsValid)

            {
                db.Entry(e).State = EntityState.Modified;
                db.SaveChanges();
            }
            ViewBag.PossibleAnswers = allPossibleAnswers;
            ViewBag.QuestionTypes = Enum.GetValues(typeof(QuestionType));
            return View(e);
        }