public ViewResult sendWelcome(FormCollection col)
        {
            int survey_id = Convert.ToInt32(col["survey_id"]);

               //Get the values sent by form that are comma delimited and put them in an
               //array, this will help when the ability to select more than one course to send out.

               string s = col["item.course_id"];
               string[] values = s.Split(',');

               int id = Convert.ToInt32(values[0]);
               var person = from r in _db.REGISTRATIONs
                        where r.course_id == id && (r.registration_status_id == "A" || r.registration_status_id == "C")
                        select new
                        {
                            person_id = r.CLIENT.PERSON.person_id,
                            first_name = r.CLIENT.PERSON.first_name,
                            last_name = r.CLIENT.PERSON.last_name,
                            account_owner = r.CLIENT.ACCOUNT.title,
                            activity_title = r.COURSE.ACTIVITY.title,
                            course_title = r.COURSE.title,
                            course_id = r.COURSE.course_id,
                            email_address = r.CLIENT.PERSON.email_address,
                            account_email = r.CLIENT.ACCOUNT.email_address,
                            address_id = r.CLIENT.PERSON.address_id,
                            email_private = r.CLIENT.PERSON.email_private,
                            barcode_number = r.COURSE.barcode_number
                        };

               var personsToPrint = from p in person
                                where ((string.IsNullOrEmpty(p.email_address) && string.IsNullOrEmpty(p.account_email))) || p.email_private > 0
                                select new PeopleToManuallyMail
                                {
                                    personID = p.person_id,
                                    firstName = p.first_name,
                                    lastName = p.last_name,
                                    email = p.email_address,
                                    emailPrivate = p.email_private,
                                    barcode = p.barcode_number,
                                    courseID = p.course_id,
                                    addressID = p.address_id,
                                    accountOwner = p.account_owner,
                                    accountEmail = p.account_email

                                };

               byte[] data = new byte[256];

               // This is one implementation of the abstract class SHA1.
               //result = sha.ComputeHash();
               int EmptyAddresses = 0;
               int EmailsSent = 0; ;
               int EmailPrivacy = 0;
               string LastAccountEmail = "";
               string LastPersonalEmail = "";

               foreach (var item in person)
               {
               if ((string.IsNullOrEmpty(item.email_address)) && (string.IsNullOrEmpty(item.account_email)))
               { EmptyAddresses++; }
               else
               {
                 if (!(LastAccountEmail == item.account_email) || !(LastPersonalEmail == item.email_address))
                   {
                       if (item.email_private == 0)
                       {
                           string course;
                           string personid;
                           string personhash;
                           course = Convert.ToString(item.course_id);
                           personid = Convert.ToString(item.person_id);
                           personhash = String.Concat(course, personid);

                           SHA1 sha = new SHA1CryptoServiceProvider();

                           // This is one implementation of the abstract class SHA1.
                           data = sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(personhash));
                           personhash = BitConverter.ToString(data);

                           //strip put the dash from person
                           System.Text.StringBuilder sb = new System.Text.StringBuilder();
                           for (int i = 0; i < personhash.Length; i++)
                           {
                               if ((personhash[i] >= '0' && personhash[i] <= '9') || (personhash[i] >= 'A' && personhash[i] <= 'z'))
                                   sb.Append(personhash[i]);
                           }

                           personhash = sb.ToString();

                           string emailserver = ConfigurationManager.AppSettings["mailSettings"];
                           System.Net.Mail.MailMessage devemail = new System.Net.Mail.MailMessage();
                           System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient(emailserver);

                           string subjectline = "Parks and Recreation Survey for ";

                           //check for nulls or empty names before using Trim
                           if (!String.IsNullOrEmpty(item.activity_title))
                           { subjectline = subjectline + (item.activity_title).Trim(); }
                           if (!String.IsNullOrEmpty(item.course_title))
                           { subjectline = subjectline + ", " + (item.course_title).Trim(); }

                           string emailBody = "";
                           devemail.Subject = subjectline;

                           devemail.IsBodyHtml = true;

                           string SurveyUrl = String.Concat("http://reclink.raleighnc.gov/Survey/BuildTheSurvey.aspx/", personhash);

                           //check id participate email address is NULL, is so change the  email verbage and use the account email address.
                           var recipients = "";
                           var toPerson = "";
                           //var FromAccount = "*****@*****.**";
                           if (string.IsNullOrEmpty(item.email_address))
                           {
                               recipients = item.account_email;
                               toPerson = item.account_owner;
                           }
                           else
                           {
                               recipients = item.email_address;
                               toPerson = item.first_name;
                           }

                           emailBody = "<p> Hello " + (toPerson).Trim() + ": </p> <p></P> <p>The goal of Raleigh Parks and Recreation is to offer the best" +
                                       " programming possible. The purpose of this survey is to gather information from residents in the community concerning" +
                                       " various programs offered. We are interested in improving services and programs offered in the future and value your input." +
                                       " Please take the time to answer the following questions and be as honest as possible. All answers to this survey will" +
                                       " remain anonymous. If multiple family members participate in the same program and have the same email address, you will" +
                                       " receive only one survey.</p> </br><p> Click on the link below to begin your survey. If you can not click on the link, copy and paste" +
                                       " the link into your browser. </p> " + SurveyUrl;
                           devemail.Body = emailBody;

                           //FileReader returns an array of recipients from a text file

                           //REMOVE BEFORE SAVING UP TO GIT  TESTING ONLY
                           //recipients = "*****@*****.**";

                           devemail.To.Add(recipients);
                           devemail.From.Address.Equals("*****@*****.**");

                           mailClient.Send(devemail);

                           //Insert into tables after email is sent.
                           SURVEY_REQUEST_SENT EmailSurvey = new SURVEY_REQUEST_SENT();

                           //Get the survey lifetime from Survey
                           //Get lifetime from database is not working need to working on this

                           var surveyInfo = from a in Surveydb.SURVEYs
                                            where a.survey_id == survey_id
                                            select a;

                           //int lifetimeInDay = surveyInfo.;

                           EmailSurvey.survey_id = survey_id;
                           EmailSurvey.person_hash = personhash;
                           EmailSurvey.expiration_date = DateTime.Now.AddDays(30);
                           expDate = EmailSurvey.expiration_date;
                           EmailSurvey.status_flag = "S";
                           EmailSurvey.date_sent = DateTime.Now;
                           EmailSurvey.user_stamp = 1;
                           EmailSurvey.course_id = Convert.ToInt32(id);

                           //Check if the hash code is already in the table, email already sent.
                           Surveydb.AddToSURVEY_REQUEST_SENT(EmailSurvey);
                           Surveydb.SaveChanges();
                           EmailsSent++;
                           LastAccountEmail = item.account_email;
                           LastPersonalEmail = item.email_address;
                       }
                       else
                       {
                           EmailPrivacy++;
                       }
               }

               }

               }

               //after the emails are sent out update the Course Status table that survey for this course was sent.
               COURSE_STATUS CourseSurveySent = new COURSE_STATUS();
               CourseSurveySent.course_id = Convert.ToInt32(id);
               CourseSurveySent.course_status1 = "S";
               CourseSurveySent.survey_exp_date = expDate;
               Surveydb.AddToCOURSE_STATUS(CourseSurveySent);
               Surveydb.SaveChanges();

               //var emailview = new EmailStats();
               ViewBag.EmailCountSent = EmailsSent;
               ViewBag.NoEmailAdddress = EmptyAddresses;
               ViewBag.EmailPrivacyFlag = EmailPrivacy;
               ViewBag.courseID = id;

               //ViewBag.people = personsToPrint;
               return View(personsToPrint);
        }
 /// <summary>
 /// Create a new COURSE_STATUS object.
 /// </summary>
 /// <param name="course_status_id">Initial value of the course_status_id property.</param>
 /// <param name="course_id">Initial value of the course_id property.</param>
 /// <param name="course_status1">Initial value of the course_status1 property.</param>
 public static COURSE_STATUS CreateCOURSE_STATUS(global::System.Int32 course_status_id, global::System.Int32 course_id, global::System.String course_status1)
 {
     COURSE_STATUS cOURSE_STATUS = new COURSE_STATUS();
     cOURSE_STATUS.course_status_id = course_status_id;
     cOURSE_STATUS.course_id = course_id;
     cOURSE_STATUS.course_status1 = course_status1;
     return cOURSE_STATUS;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the COURSE_STATUS EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToCOURSE_STATUS(COURSE_STATUS cOURSE_STATUS)
 {
     base.AddObject("COURSE_STATUS", cOURSE_STATUS);
 }
        public ActionResult Index(int? course, string currentFilter, string searchString, string Display, bool? Disable, string SearchType, int? page)
        {
            //Changed on 4/10/2013 to default to the view of surveys not sent
            var FirstPass = 0;
            if (string.IsNullOrEmpty(currentFilter) && string.IsNullOrEmpty(searchString) && string.IsNullOrEmpty(Display) && string.IsNullOrEmpty(SearchType))
            {
                Display = "notSent";
                FirstPass = 1;
            }

            if (FirstPass > 0)
            {
                //go get the data
            }

            //If the checkbox is checked than the user is trying to disable this survey as  do not survey
            //need to check to see if the survey already exists in the database and act accordingly.
            var checkBox = Request.Form["applyChanges"];
            if (!String.IsNullOrEmpty(checkBox))
            {
                if (ModelState.IsValid)
                {
                    string checkbox = Request.Form["applyChanges"];
                    string[] values = checkbox.Split(',');
                    int itemCount = values.Count();
                    while (itemCount > 0)
                    {
                        if (itemCount <= values.Count())
                        {
                             itemCount = itemCount - 1;
                        }
                        int courseNum = Convert.ToInt32(values[itemCount]);

                        // var courseNum = Convert.ToInt32(checkBox);
                        Survey_DBEntities dbContext = new Survey_DBEntities();
                        var rowCount = (from s in survey_db.COURSE_STATUS where s.course_id == courseNum select s).Count();
                        if (rowCount < 1)
                        {
                            //if course id does not exist in course status table then insert
                            COURSE_STATUS courseStatusAdd = new COURSE_STATUS();
                            courseStatusAdd.course_id = courseNum;
                            courseStatusAdd.course_status1 = "N";
                            courseStatusAdd.survey_exp_date = DateTime.Now.Date;

                            dbContext.AddToCOURSE_STATUS(courseStatusAdd);
                            //dbContext.SaveChanges();
                        }
                        else
                        {
                            COURSE_STATUS statusUpdate = dbContext.COURSE_STATUS.FirstOrDefault(s => s.course_id == courseNum);
                            //check to see if the course is already disables and enable it
                            if ((statusUpdate.course_status1).Trim() == "N")
                            {
                                var removeFromCouseStatus = dbContext.COURSE_STATUS.First(s => s.course_id == courseNum);
                                dbContext.COURSE_STATUS.DeleteObject(removeFromCouseStatus);
                                //dbContext.SaveChanges();
                            }
                            else
                            {
                                statusUpdate.course_status1 = "N";
                            }
                            //dbContext.SaveChanges();
                        }
                        dbContext.SaveChanges();
                    }
                }
            }

            if (Request.HttpMethod != "GET")
            {
                page = 1;
            }

            var GreaterThan2013 = Convert.ToDateTime("2013/04/01");
            var surveyedCourses = (from sc in survey_db.COURSE_STATUS
                                   select sc.course_id).ToList();

            var query = new List<COURSE>();
            query = (from c in _db.COURSEs
                     where surveyedCourses.Contains(c.course_id) || (c.last_end_datetime > GreaterThan2013) && (c.course_status_id == "C" || (EntityFunctions.AddDays(c.last_end_datetime, 1) < GreaterThan2013)) && c.course_status_id != "X" && c.session_title_id != 9
                     orderby c.barcode_number
                     select c).ToList();
            //switch (Display)
            //{
            //    case "sent":
            //        //query = SurveysSent(AllCourses()).ToList();
            //        break;
            //    case "inProgress":
            //        //query = SurveysInProgress(AllCourses()).ToList();
            //        break;
            //    case "completed":
            //        //query = SurveysCompleted(AllCourses()).ToList();
            //        break;
            //    case "notSent":
            //        var doNotSend = (from sc in survey_db.COURSE_STATUS
            //                         where sc.course_status1 == "N"
            //                         select sc.course_id).ToList();
            //        break;
            //    case "doNotsent":
            //        query = DoNotSend(AllCourses()).ToList();
            //        break;
            //    default:
            //        break;
            //}

            var SurveyStatus = from k in survey_db.COURSE_STATUS
                               select k;

            var SurveyExp = from x in survey_db.SURVEY_REQUEST_SENT
                            select x;

            var pageNumber = page ?? 1; // if no page was specified in the querystring, default to the first page (1)
            //var CourseDetails = query.ToList();

            //ViewBag.CourseList = CourseDetails;
            ViewBag.onePageOfCourses = query.ToPagedList(pageNumber, 25); // will only contain 25 products max because of the pageSize
            ViewBag.SurveyStatus = SurveyStatus;
            ViewBag.SurveyExp = SurveyExp;
            ViewBag.searchString = searchString;
            ViewBag.display = Display;
            ViewBag.SearchType = SearchType;
            ViewBag.Disable = Disable;
            return View();
        }