public void Insert()
        {
            using (var session = FluentNHibernateConfiguration.InitFactory.sessionFactory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var user = new Entities.Member { userName = "******", memberPassword = "******" };

                    var survey = new Entities.Survey { surveyDescription = "A Random Sample Survey. This is just a sample of how json can be used to render a survey.", surveyTitle = "Sample Survey", surveyQuestionList = new List<Entities.SurveyQuestion>() };
                    survey.surveyQuestionList.Add(new Entities.SurveyQuestion { surveyQuestionTitle = "What is your, favourite, color?", surveyQuestionType = 0, surveyQuestionOptionList = new List<Entities.SurveyQuestionOption>() });
                    survey.surveyQuestionList.ElementAt(0).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "White" });
                    survey.surveyQuestionList.ElementAt(0).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "Yellow" });
                    survey.surveyQuestionList.ElementAt(0).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "Blue" });
                    survey.surveyQuestionList.ElementAt(0).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "Green" });
                    survey.surveyQuestionList.ElementAt(0).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "Enter another color:", surveyQuestionOptionType = 2 });
                    survey.surveyQuestionList.Add(new Entities.SurveyQuestion { surveyQuestionTitle = "Who is the most hardworking person in our team?", surveyQuestionType = 0, surveyQuestionOptionList = new List<Entities.SurveyQuestionOption>() });
                    survey.surveyQuestionList.ElementAt(1).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "June" });
                    survey.surveyQuestionList.ElementAt(1).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "http://graph.facebook.com/weileng.peh/picture?type=large", surveyQuestionOptionTitleType = 2 });
                    survey.surveyQuestionList.ElementAt(1).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "Hong Jing" });
                    survey.surveyQuestionList.ElementAt(1).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "Xu Ai" });
                    survey.surveyQuestionList.ElementAt(1).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "Wesley" });
                    survey.surveyQuestionList.Add(new Entities.SurveyQuestion { surveyQuestionTitle = "And the best phone is", surveyQuestionType = 0, surveyQuestionOptionList = new List<Entities.SurveyQuestionOption>() });
                    survey.surveyQuestionList.ElementAt(2).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "Galaxy S 3" });
                    survey.surveyQuestionList.ElementAt(2).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "Galaxy S 2" });
                    survey.surveyQuestionList.ElementAt(2).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "Galaxy S" });
                    survey.surveyQuestionList.ElementAt(2).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "HTC One X" });
                    survey.surveyQuestionList.ElementAt(2).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "Galaxy Nexus" });
                    survey.surveyQuestionList.ElementAt(2).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "Motorola Droid Razr Maxx" });
                    survey.surveyQuestionList.Add(new Entities.SurveyQuestion { surveyQuestionTitle = "Who is the most hardworking person in our team? (You can choose more than 1 answer)", surveyQuestionType = 1, surveyQuestionOptionList = new List<Entities.SurveyQuestionOption>() });
                    survey.surveyQuestionList.ElementAt(3).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "http://graph.facebook.com/weileng.peh/picture?type=large", surveyQuestionOptionTitleType = 2 });
                    survey.surveyQuestionList.ElementAt(3).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "June" });
                    survey.surveyQuestionList.ElementAt(3).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "June" });
                    survey.surveyQuestionList.ElementAt(3).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "June" });
                    survey.surveyQuestionList.ElementAt(3).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "June" });
                    survey.surveyQuestionList.ElementAt(3).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "June" });
                    survey.surveyQuestionList.ElementAt(3).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "June" });
                    survey.surveyQuestionList.ElementAt(3).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "June" });
                    survey.surveyQuestionList.ElementAt(3).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "June" });
                    survey.surveyQuestionList.Add(new Entities.SurveyQuestion { surveyQuestionTitle = "How hardworking is June?", surveyQuestionType = 2, surveyQuestionOptionList = new List<Entities.SurveyQuestionOption>() });
                    survey.surveyQuestionList.ElementAt(4).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "Hardworkingness" , surveyQuestionOptionMaxText="Very Very Hardworking", surveyQuestionOptionMinText="Very Hardworking"});
                    survey.surveyQuestionList.Add(new Entities.SurveyQuestion { surveyQuestionTitle = "How young is this pretty young star? Numeric input.", surveyQuestionType = 3, surveyQuestionOptionList = new List<Entities.SurveyQuestionOption>() });
                    survey.surveyQuestionList.ElementAt(5).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "http://graph.facebook.com/weileng.peh/picture?type=large" });
                    survey.surveyQuestionList.Add(new Entities.SurveyQuestion { surveyQuestionTitle = "Or the birthday of this talented actor/singer? Date input.", surveyQuestionType = 4, surveyQuestionOptionList = new List<Entities.SurveyQuestionOption>() });
                    survey.surveyQuestionList.ElementAt(6).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "http://graph.facebook.com/197371292379/picture?type=large" });
                    survey.surveyQuestionList.Add(new Entities.SurveyQuestion { surveyQuestionTitle = "Which picture is the odd one out?", surveyQuestionType = 5, surveyQuestionOptionList = new List<Entities.SurveyQuestionOption>() });
                    survey.surveyQuestionList.ElementAt(7).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "http://graph.facebook.com/alexei.sourin/picture?type=square" , surveyQuestionOptionTitleType=2 });
                    survey.surveyQuestionList.ElementAt(7).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "test", surveyQuestionOptionTitleType = 0 });
                    survey.surveyQuestionList.ElementAt(7).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "http://graph.facebook.com/bengkoon.ng/picture?type=square", surveyQuestionOptionTitleType = 2 });
                    survey.surveyQuestionList.ElementAt(7).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "http://graph.facebook.com/bingsheng.he/picture?type=square", surveyQuestionOptionTitleType = 2 });
                    survey.surveyQuestionList.ElementAt(7).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "http://graph.facebook.com/limws.brandon/picture?type=square", surveyQuestionOptionTitleType = 2 });
                    survey.surveyQuestionList.Add(new Entities.SurveyQuestion { surveyQuestionTitle = "Please give your opinion of this survey in a single sentence.", surveyQuestionType = 6, surveyQuestionOptionList = new List<Entities.SurveyQuestionOption>() });
                    survey.surveyQuestionList.ElementAt(8).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "http://graph.facebook.com/197371292379/picture?type=large" });
                    survey.surveyQuestionList.Add(new Entities.SurveyQuestion { surveyQuestionTitle = "Opinon. In. A. Paragraph.", surveyQuestionType = 7, surveyQuestionOptionList = new List<Entities.SurveyQuestionOption>() });
                    survey.surveyQuestionList.ElementAt(9).surveyQuestionOptionList.Add(new Entities.SurveyQuestionOption { surveyQuestionOptionTitle = "http://graph.facebook.com/197371292379/picture?type=large" });
                    var respondent = new Entities.Respondent { respondentIPAddress = "127.0.0.1", respondentSessionID = "randomid" };

                    var responseqn0 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = 1 };
                    respondent.surveyQuestionResponseList.Add(responseqn0);
                    survey.surveyQuestionList.ElementAt(0).surveyQuestionResponseList.Add(responseqn0);
                    var responseqn1 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = 3 };
                    respondent.surveyQuestionResponseList.Add(responseqn1);
                    survey.surveyQuestionList.ElementAt(1).surveyQuestionResponseList.Add(responseqn1);
                    var responseqn2 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = 4 };
                    respondent.surveyQuestionResponseList.Add(responseqn2);
                    survey.surveyQuestionList.ElementAt(2).surveyQuestionResponseList.Add(responseqn2);
                    var responseqn30 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = 2 };
                    var responseqn31 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = 3 };
                    var responseqn32 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = 6 };
                    respondent.surveyQuestionResponseList.Add(responseqn30);
                    respondent.surveyQuestionResponseList.Add(responseqn31);
                    respondent.surveyQuestionResponseList.Add(responseqn32);
                    survey.surveyQuestionList.ElementAt(3).surveyQuestionResponseList.Add(responseqn30);
                    survey.surveyQuestionList.ElementAt(3).surveyQuestionResponseList.Add(responseqn31);
                    survey.surveyQuestionList.ElementAt(3).surveyQuestionResponseList.Add(responseqn32);
                    var responseqn4 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = 2, responseStringValue = "100" };
                    respondent.surveyQuestionResponseList.Add(responseqn4);
                    survey.surveyQuestionList.ElementAt(4).surveyQuestionResponseList.Add(responseqn4);
                    var responseqn5 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 2, responseIntegerValue = 3, responseStringValue = "345" };
                    respondent.surveyQuestionResponseList.Add(responseqn5);
                    survey.surveyQuestionList.ElementAt(5).surveyQuestionResponseList.Add(responseqn5);
                    var responseqn6 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 2, responseStringValue = "09/05/2012" };
                    respondent.surveyQuestionResponseList.Add(responseqn6);
                    survey.surveyQuestionList.ElementAt(6).surveyQuestionResponseList.Add(responseqn6);
                    var responseqn7 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = 1 };
                    respondent.surveyQuestionResponseList.Add(responseqn7);
                    survey.surveyQuestionList.ElementAt(7).surveyQuestionResponseList.Add(responseqn7);

                    survey.AddRespondent(respondent);

                    var respondent1 = new Entities.Respondent { respondentIPAddress = "127.0.0.1", respondentSessionID = "randomid1" };
                    var responseqn10 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = 2 };
                    respondent1.surveyQuestionResponseList.Add(responseqn10);
                    survey.surveyQuestionList.ElementAt(0).surveyQuestionResponseList.Add(responseqn10);
                    var responseqn11 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = 3 };
                    respondent1.surveyQuestionResponseList.Add(responseqn11);
                    survey.surveyQuestionList.ElementAt(1).surveyQuestionResponseList.Add(responseqn11);
                    var responseqn12 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = 4 };
                    respondent1.surveyQuestionResponseList.Add(responseqn12);
                    survey.surveyQuestionList.ElementAt(2).surveyQuestionResponseList.Add(responseqn12);
                    var responseqn130 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = 0 };
                    var responseqn131 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = 5 };
                    var responseqn132 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = 6 };
                    respondent1.surveyQuestionResponseList.Add(responseqn130);
                    respondent1.surveyQuestionResponseList.Add(responseqn131);
                    respondent1.surveyQuestionResponseList.Add(responseqn132);
                    survey.surveyQuestionList.ElementAt(3).surveyQuestionResponseList.Add(responseqn130);
                    survey.surveyQuestionList.ElementAt(3).surveyQuestionResponseList.Add(responseqn131);
                    survey.surveyQuestionList.ElementAt(3).surveyQuestionResponseList.Add(responseqn132);
                    var responseqn14 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = 2, responseStringValue = "10" };
                    respondent1.surveyQuestionResponseList.Add(responseqn14);
                    survey.surveyQuestionList.ElementAt(4).surveyQuestionResponseList.Add(responseqn14);
                    var responseqn15 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 2, responseIntegerValue = 3, responseStringValue = "43" };
                    respondent1.surveyQuestionResponseList.Add(responseqn15);
                    survey.surveyQuestionList.ElementAt(5).surveyQuestionResponseList.Add(responseqn15);
                    var responseqn16 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 2, responseStringValue = "09/05/2012" };
                    respondent1.surveyQuestionResponseList.Add(responseqn16);
                    survey.surveyQuestionList.ElementAt(6).surveyQuestionResponseList.Add(responseqn16);
                    var responseqn17 = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = 0 };
                    respondent1.surveyQuestionResponseList.Add(responseqn17);
                    survey.surveyQuestionList.ElementAt(7).surveyQuestionResponseList.Add(responseqn17);

                    survey.AddRespondent(respondent1);

                    user.AddSurvey(survey);
                    session.SaveOrUpdate(user);
                    transaction.Commit();

                }

            }
            //Context.Response.Write(js.Serialize(survey));
        }
        public void submitSurvey(string jsonString)
        {
            using (var session = FluentNHibernateConfiguration.InitFactory.sessionFactory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    var foo = js.Deserialize<Dictionary<string, object>>(jsonString);
                    dynamic json = JValue.Parse(jsonString);
                    JObject jsonObject = JObject.Parse(jsonString);
                    JToken jToken;
                    Entities.Survey survey;
                    if (jsonObject.TryGetValue("surveyId", out jToken))
                    {
                        int surveyID = Int32.Parse(jToken.ToString());
                        survey = Entities.Survey.GetById(session, surveyID);
                        //Context.Response.Write(jToken);
                        if (jsonObject.TryGetValue("s", out jToken))
                        {
                            string ipAddress;
                            if (!String.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"]))
                                ipAddress = HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"];
                            else if (!String.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
                                ipAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                            else ipAddress = System.Web.HttpContext.Current.Request.UserHostAddress;
                            JToken time;
                            jsonObject.TryGetValue("surveyTime", out time);
                            int timeTaken = Int32.Parse(time.ToString());

                            var respondent = new Entities.Respondent { respondentIPAddress = ipAddress, respondentSessionID = "fromworkingresponse", respondentTime = timeTaken, respondentCountryCode = jsonObject.SelectToken("surveyLocationCountryCode").ToString(), respondentBrowser = jsonObject.SelectToken("surveyBrowserBrowser").ToString(), respondentOS = jsonObject.SelectToken("surveyBrowserOS").ToString(), respondentLang = jsonObject.SelectToken("surveyLocaleLang").ToString() };
                            jsonObject = JObject.Parse(jToken.ToString());
                            int i = 0;
                            foreach (var question in survey.surveyQuestionList)
                            {
                                if (jsonObject.TryGetValue(i.ToString(), out jToken))
                                {
                                    switch (question.surveyQuestionType)
                                    {
                                        case 10:
                                        case 0:
                                        case 5:
                                        case 2:
                                        case 3:
                                            {
                                                var response = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = Int32.Parse(jToken.ToString()) };
                                                respondent.surveyQuestionResponseList.Add(response);
                                                question.surveyQuestionResponseList.Add(response);
                                                break;
                                            }
                                        case 1:
                                            {
                                                var checkboxresults = jToken.ToList();
                                                foreach (var result in checkboxresults)
                                                {
                                                    var response = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 1, responseIntegerValue = Int32.Parse(result.ToString()) };
                                                    respondent.surveyQuestionResponseList.Add(response);
                                                    question.surveyQuestionResponseList.Add(response);
                                                    Context.Response.Write(result);
                                                }

                                                break;
                                            }
                                        case 6:
                                        case 7:
                                        case 4:
                                            {
                                                var response = new Entities.SurveyQuestionResponse { responseIsAnswered = true, responseType = 2, responseStringValue = jToken.ToString() };
                                                respondent.surveyQuestionResponseList.Add(response);
                                                question.surveyQuestionResponseList.Add(response);
                                                break;
                                            }
                                    }
                                    Context.Response.Write(question.surveyQuestionTitle);

                                }
                                i++;
                            }
                            survey.AddRespondent(respondent);
                            session.SaveOrUpdate(survey);
                            transaction.Commit();
                        }
                    }
                }
            }
        }