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 SURVEY_REQUEST_SENT object.
 /// </summary>
 /// <param name="survey_request_sent_id">Initial value of the survey_request_sent_id property.</param>
 /// <param name="survey_id">Initial value of the survey_id property.</param>
 /// <param name="person_hash">Initial value of the person_hash property.</param>
 /// <param name="expiration_date">Initial value of the expiration_date property.</param>
 /// <param name="status_flag">Initial value of the status_flag property.</param>
 /// <param name="date_sent">Initial value of the date_sent property.</param>
 /// <param name="user_stamp">Initial value of the user_stamp property.</param>
 /// <param name="course_id">Initial value of the course_id property.</param>
 public static SURVEY_REQUEST_SENT CreateSURVEY_REQUEST_SENT(global::System.Int32 survey_request_sent_id, global::System.Int32 survey_id, global::System.String person_hash, global::System.DateTime expiration_date, global::System.String status_flag, global::System.DateTime date_sent, global::System.Int32 user_stamp, global::System.Int32 course_id)
 {
     SURVEY_REQUEST_SENT sURVEY_REQUEST_SENT = new SURVEY_REQUEST_SENT();
     sURVEY_REQUEST_SENT.survey_request_sent_id = survey_request_sent_id;
     sURVEY_REQUEST_SENT.survey_id = survey_id;
     sURVEY_REQUEST_SENT.person_hash = person_hash;
     sURVEY_REQUEST_SENT.expiration_date = expiration_date;
     sURVEY_REQUEST_SENT.status_flag = status_flag;
     sURVEY_REQUEST_SENT.date_sent = date_sent;
     sURVEY_REQUEST_SENT.user_stamp = user_stamp;
     sURVEY_REQUEST_SENT.course_id = course_id;
     return sURVEY_REQUEST_SENT;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the SURVEY_REQUEST_SENT EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToSURVEY_REQUEST_SENT(SURVEY_REQUEST_SENT sURVEY_REQUEST_SENT)
 {
     base.AddObject("SURVEY_REQUEST_SENT", sURVEY_REQUEST_SENT);
 }
        public ActionResult ManualEntry(int survey_id, string barcode)
        {
            //var id = Convert.ToInt32(barcode);
            var courseInfo = from c in _db.COURSEs
                             where c.barcode_number == barcode
                             select c.course_id;
            if (courseInfo.Count() == 1)
            {
                //Need to validate the user entered a survey id and a course id

                //Need to verify that the course id has been send out

                byte[] data = new byte[256];

                // This is one implementation of the abstract class SHA1.
                //result = sha.ComputeHash();
                var course = Convert.ToString(courseInfo.First());
                var date = Convert.ToString(DateTime.Now);
                var personhash = String.Concat(course, date);

                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();
                //var surveyInfo = from a in db.SURVEYs
                //                 where a.survey_id == survey_id
                //                 select a;

                //int lifetimeInDay = surveyInfo.;
                SURVEY_REQUEST_SENT UpdateSurveySent = new SURVEY_REQUEST_SENT();
                UpdateSurveySent.survey_id = survey_id;
                UpdateSurveySent.person_hash = personhash;
                UpdateSurveySent.expiration_date = DateTime.Now;
                UpdateSurveySent.status_flag = "S";
                UpdateSurveySent.date_sent = DateTime.Now;
                UpdateSurveySent.user_stamp = 1;
                UpdateSurveySent.course_id = Convert.ToInt32(course);

                //Check if the hash code is already in the table, email already sent.
                db.AddToSURVEY_REQUEST_SENT(UpdateSurveySent);
                db.SaveChanges();
                return Redirect("http://reclink.raleighnc.gov/Survey/BuildTheSurvey.aspx/" + personhash);

            }
            return View();
            //else
            //{ //Error
            //                return View();}

                //try
                //{
                //    // TODO: Add insert logic here

                //    return View();
                //}
                //catch
                //{
                //    return View();
                //}
        }