//public static ServiceSheetViewModel createSubmissionForXml(XElement submissionXml)
        //{
        //    ServiceSheetViewModel retval = new ServiceSheetViewModel();
        //    ServiceSheet serviceSheetReturn = new ServiceSheet();
        //    serviceSheetReturn.SubmissionNumber = Int32.Parse(submissionXml.Element(SUBMISSION_NUMBER).Value);
        //    //RT 11/8/16 - Adding in the response id
        //    serviceSheetReturn.CanvasResponseId = submissionXml.Element(RESPONSE_ID).Value;
        //    //Adding response date time
        //    string responseDateStr = submissionXml.Element(RESPONSE_DATE_TIME).Value;
        //    serviceSheetReturn.DtResponse = Convert.ToDateTime(responseDateStr);
        //    //Adding device date
        //    string deviceDateStr = submissionXml.Element(DEVICE_DATE_TIME).Value;
        //    serviceSheetReturn.DtDevice = Convert.ToDateTime(deviceDateStr);
        //    //Submission version is in the form element
        //    XElement formDetailsXml = submissionXml.Element(FORM);
        //    serviceSheetReturn.SubmissionFormVersion = Int32.Parse(formDetailsXml.Element(SUBMISSION_VERSION).Value);
        //    serviceSheetReturn.Username = submissionXml.Element(USERNAME).Value;
        //    serviceSheetReturn.UserFirstName = submissionXml.Element(FIRST_NAME).Value;
        //    serviceSheetReturn.UserSurname = submissionXml.Element(SURNAME).Value;

        //    XElement sectionsXml = submissionXml.Element(SECTIONS);
        //    // Loop through the sections
        //    foreach (XElement sectionXml in sectionsXml.Elements())
        //    {
        //        string sectionName = sectionXml.Element(SECTION_NAME).Value;
        //        if (sectionName.Equals(JOB_DETAILS))
        //        {
        //            XElement screensXml = sectionXml.Element(SCREENS);
        //            XElement screenXml = screensXml.Element(SCREEN);
        //            XElement responsesXml = screenXml.Element(RESPONSES);
        //            //Parse the job details

        //            serviceSheetReturn.Customer = xmlResult(CUSTOMER, responsesXml);
        //            serviceSheetReturn.AddressLine1 = xmlResult(ADDRESS_1, responsesXml);
        //            serviceSheetReturn.AddressLine2 = xmlResult(ADDRESS_2, responsesXml);
        //            serviceSheetReturn.TownCity = xmlResult(TOWN_CITY, responsesXml);
        //            serviceSheetReturn.Postcode = xmlResult(POSTCODE, responsesXml);
        //            serviceSheetReturn.CustomerContact = xmlResult(CUSTOMER_CONTACT, responsesXml);
        //            serviceSheetReturn.CustomerPhoneNo = xmlResult(CUSTOMER_PHONE, responsesXml);
        //            serviceSheetReturn.MachineMakeModel = xmlResult(MACHINE_MAKE_MODEL, responsesXml);
        //            //RT 11/8/16 - Adding serial number
        //            serviceSheetReturn.MachineSerial = xmlResult(SERIAL_NUMBER, responsesXml);

        //            serviceSheetReturn.CncControl = xmlResult(MACHINE_CONTROL, responsesXml);
        //            string jobStartStr = xmlResult(JOB_START_DATE, responsesXml);
        //            serviceSheetReturn.DtJobStart = Convert.ToDateTime(jobStartStr);
        //            serviceSheetReturn.CustomerOrderNo = xmlResult(CUSTOMER_ORDER, responsesXml);
        //            serviceSheetReturn.MttJobNumber = xmlResult(MTT_JOB_NO, responsesXml);
        //            serviceSheetReturn.JobDescription = xmlResult(JOB_DESC, responsesXml);

        //        }
        //        else if (sectionName.Equals(TIME_SHEET))
        //        {
        //            XElement screensXml = sectionXml.Element(SCREENS);
        //            XElement screenXml = screensXml.Element(SCREEN);
        //            XElement responseGroupsXml = screenXml.Element(RESPONSE_GROUPS);
        //            //retval.ServiceTimesheets = ServiceDay.createDays(responseGroupsXml);
        //            serviceSheetReturn.ServiceDays= createDays(responseGroupsXml);
        //        }
        //        else if (sectionName.Equals(JOB_SIGNOFF))
        //        {
        //            XElement screensXml = sectionXml.Element(SCREENS);
        //            XElement screenXml = screensXml.Element(SCREEN);
        //            XElement responsesXml = screenXml.Element(RESPONSES);

        //            serviceSheetReturn.JobTotalTimeOnsite = Convert.ToDouble(xmlResult(JOB_TOTAL_TIME_ONSITE, responsesXml));
        //            serviceSheetReturn.JobTotalTravelTime = Convert.ToDouble(xmlResult(TOTAL_TRAVEL_TIME, responsesXml));

        //            //RT 13/10/16 - Mileage, daily/overnight and BP are int, although canvas returns a string like a double.  There will never be a decimal point in them
        //            double mileageRead = Convert.ToDouble(xmlResult(TOTAL_MILEAGE, responsesXml));
        //            serviceSheetReturn.JobTotalMileage = (int)mileageRead;
        //            double totalDaRead = Convert.ToDouble(xmlResult(TOTAL_DAILY_ALLOWANCES, responsesXml));
        //            serviceSheetReturn.TotalDailyAllowances = (int)totalDaRead;
        //            double totalOaRead = Convert.ToDouble(xmlResult(TOTAL_OVERNIGHT_ALLOWANCES, responsesXml));
        //            serviceSheetReturn.TotalOvernightAllowances = (int)totalOaRead;
        //            double totalBpRead = Convert.ToDouble(xmlResult(TOTAL_BARRIER_PAYMENTS, responsesXml));
        //            serviceSheetReturn.TotalBarrierPayments = (int)totalBpRead;
        //            serviceSheetReturn.JobStatus = xmlResult(JOB_STATUS, responsesXml);
        //            serviceSheetReturn.FinalJobReport = xmlResult(FINAL_JOB_REPORT, responsesXml);
        //            serviceSheetReturn.AdditionalFaults = xmlResult(ADDITIONAL_FAULTS_FOUND, responsesXml);
        //            serviceSheetReturn.QuoteRequired = Convert.ToBoolean(xmlResult(QUOTE_REQUIRED, responsesXml));
        //            serviceSheetReturn.FollowUpPartsRequired = xmlResult(FOLLOWUP_PARTS, responsesXml);
        //            serviceSheetReturn.Image1Url = xmlResult(IMAGE_1_URL, responsesXml);
        //            serviceSheetReturn.Image2Url = xmlResult(IMAGE_2_URL, responsesXml);
        //            serviceSheetReturn.Image3Url = xmlResult(IMAGE_3_URL, responsesXml);
        //            serviceSheetReturn.Image4Url = xmlResult(IMAGE_4_URL, responsesXml);
        //            serviceSheetReturn.Image5Url = xmlResult(IMAGE_5_URL, responsesXml);
        //            serviceSheetReturn.CustomerSignatureUrl = xmlResult(CUSTOMER_SIGNATURE, responsesXml);
        //            serviceSheetReturn.CustomerName = xmlResult(CUSTOMER_NAME, responsesXml);
        //            serviceSheetReturn.DtSigned = Convert.ToDateTime(xmlResult(DATE_SIGNED, responsesXml));
        //            serviceSheetReturn.MttEngSignatureUrl = xmlResult(MTT_ENG_SIGNATURE, responsesXml);
        //        }
        //        else
        //        {
        //            new Exception("Unknown Canvas Data Section");
        //        }

        //    }
        //    retval.ServiceSubmission = serviceSheetReturn;
        //    return retval;
        //}

        //RT 29/11/16 - Rewiting to use new MVVM methods
        public static AllServiceDayViewModels createDays(XElement allDays)
        {
            AllServiceDayViewModels retval = new AllServiceDayViewModels();

            foreach (XElement responseGroupXml in allDays.Elements())
            {
                string dtServiceStr = xmlResult(DATE, responseGroupXml);
                DateTime dtReport = Convert.ToDateTime(dtServiceStr);
                XElement sectionXml = responseGroupXml.Element(SECTION);
                XElement screensXml = sectionXml.Element(SCREENS);
                XElement screenXml = screensXml.Element(SCREEN);
                XElement responsesXml = screenXml.Element(RESPONSES);

                string travelStartStr = xmlResult(TRAVEL_START, responsesXml);
                DateTime TravelStartTime = Convert.ToDateTime(dtServiceStr + " " + travelStartStr);
                string arrivalOnsiteStr = xmlResult(ARRIVE_ONSITE, responsesXml);
                DateTime ArrivalOnsiteTime = Convert.ToDateTime(dtServiceStr + " " + arrivalOnsiteStr);
                string departSiteStr = xmlResult(DEPART_SITE, responsesXml);
                DateTime DepartureSiteTime = Convert.ToDateTime(dtServiceStr + " " + departSiteStr);
                string travelEndStr = xmlResult(TRAVEL_END, responsesXml);
                DateTime TravelEndTime = Convert.ToDateTime(dtServiceStr + " " + travelEndStr);
                int Mileage = Convert.ToInt32(xmlResult(MILEAGE, responsesXml));
                string currentXmlResult = xmlResult(DAILY_ALLOWANCE, responsesXml);
                int DailyAllowance = Convert.ToInt32(currentXmlResult);
                bool da;
                if (DailyAllowance == 1)
                {
                    da = true;
                }
                else
                {
                    da = false;
                }
                currentXmlResult = xmlResult(OVERNIGHT_ALLOWANCE, responsesXml);
                int OvernightAllowance = Convert.ToInt32(currentXmlResult);
                bool oN;
                if (OvernightAllowance == 1)
                {
                    oN = true;
                }
                else
                {
                    oN = false;
                }
                currentXmlResult = xmlResult(BARRIER_PAYMENT, responsesXml);
                int BarrierPayment = Convert.ToInt32(currentXmlResult);
                bool bP;
                if (BarrierPayment == 1)
                {
                    bP = true;
                }
                else
                {
                    bP = false;
                }
                double TravelToSiteTime = Convert.ToDouble(xmlResult(TRAVEL_TO_SITE, responsesXml));
                double TravelFromSiteTime = Convert.ToDouble(xmlResult(TRAVEL_FROM_SITE, responsesXml));
                double TotalTravelTime = Convert.ToDouble(xmlResult(TOTAL_TRAVEL, responsesXml));
                double TotalOnsiteTime = Convert.ToDouble(xmlResult(TOTAL_TIME_ONSITE, responsesXml));
                string DailyReport = xmlResult(DAILY_REPORT, responsesXml);
                string PartsSuppliedToday = xmlResult(PARTS_SUPPLIED, responsesXml);

                ServiceDayViewModel sd = new ServiceDayViewModel(TravelStartTime, ArrivalOnsiteTime, DepartureSiteTime, TravelEndTime, Mileage, da, oN,
                    bP, TravelToSiteTime, TravelFromSiteTime, TotalTravelTime, TotalOnsiteTime, DailyReport, PartsSuppliedToday, dtReport, null);

                retval.AddServiceDay(sd);
            }

            //Need to sort the observable collection
            ObservableCollection<ServiceDayViewModel> sortedDays = new ObservableCollection<ServiceDayViewModel>(retval.AllServiceDayVMs.OrderBy(a => a.DtReport));
            retval.AllServiceDayVMs = sortedDays;

            return retval;
        }
        //Creator for the VM
        public ServiceSheetViewModel(int submissionNoEntered, string appNameEntered, string userfirstNameEntered, string userSurnameEntered, string canvasResponseIdEntered, DateTime dtResponseEntered, 
            DateTime dtDeviceEntered, string submissionFormNameEntered, int submissionVersionEntered, string customerEntered, string address1Entered, string address2Entered, string townCityEntered,
            string postcodeEntered, string contactEntered, string phoneNumberEntered, string makeModelEntered, string serialNumberEntered, string cncControlEntered, DateTime dtJobStartEntered, 
            string customerOrderEntered, string mttJobNumberEntered, string jobDescEntered, double totalTimeOnsiteEntered, double totalTravelTimeEntered, int totalMileageEntered,
            int totalDAEntered, int totalOAEntered, int totalBPEntered, string jobStatusEntered, string finalJobReportEntered, string additionalFaultsEntered, bool quoteEntered, string followupPartsEntered,
            string image1UrlEntered, string image2UrlEntered, string image3UrlEntered, string image4UrlEntered, string image5UrlEntered, string customerSignatureUrlEntered, string custSignedNameEntered,
            DateTime dtSignedEntered, string mttEngSignatureUrlEntered, AllServiceDayViewModels serviceDaysEntered, string usernameEntered)
        {
            //ServiceSheetViewModel retval = new ServiceSheetViewModel();
            this.SubmissionNumber = submissionNoEntered;
            this.AppName = appNameEntered;
            this.UserFirstName = userfirstNameEntered;
            this.UserSurname = userSurnameEntered;
            this.Username = usernameEntered;
            this.CanvasResponseId = canvasResponseIdEntered;
            this.DtResponse = dtResponseEntered;
            this.DtDevice = dtDeviceEntered;
            this.SubmissionFormName = submissionFormNameEntered;
            this.SubmissionFormVersion = submissionVersionEntered;
            this.Customer = customerEntered;
            this.AddressLine1 = address1Entered;
            this.AddressLine2 = address2Entered;
            this.TownCity = townCityEntered;
            this.Postcode = postcodeEntered;
            this.CustomerContact = contactEntered;
            this.CustomerPhoneNo = phoneNumberEntered;
            this.MachineMakeModel = makeModelEntered;
            this.MachineSerial = serialNumberEntered;
            this.CncControl = cncControlEntered;
            this.DtJobStart = dtJobStartEntered;
            this.CustomerOrderNo = customerOrderEntered;
            this.MttJobNumber = mttJobNumberEntered;
            this.JobDescription = jobDescEntered;
            this.JobTotalTimeOnsite = totalTimeOnsiteEntered;
            this.JobTotalTravelTime = totalTravelTimeEntered;
            this.JobTotalMileage = totalMileageEntered;
            this.TotalDailyAllowances = totalDAEntered;
            this.TotalOvernightAllowances = totalOAEntered;
            this.TotalBarrierPayments = totalBPEntered;
            this.JobStatus = jobStatusEntered;
            this.FinalJobReport = finalJobReportEntered;
            this.AdditionalFaults = additionalFaultsEntered;
            this.QuoteRequired = quoteEntered;
            this.FollowUpPartsRequired = followupPartsEntered;
            this.Image1Url = image1UrlEntered;
            this.Image2Url = image2UrlEntered;
            this.Image3Url = image3UrlEntered;
            this.Image4Url = image4UrlEntered;
            this.Image5Url = image5UrlEntered;
            this.CustomerSignatureUrl = customerSignatureUrlEntered;
            this.CustomerName = custSignedNameEntered;
            this.DtSigned = dtSignedEntered;
            this.MttEngSignatureUrl = mttEngSignatureUrlEntered;
            //We need to set the service sheet reference on the day
            //We also need to set the service days on the sheet
            this.ServiceSubmission.ServiceDays = new List<ServiceDay>();

            foreach (ServiceDayViewModel sd in serviceDaysEntered.AllServiceDayVMs)
            {
                this.ServiceSubmission.ServiceDays.Add(sd.ServiceDayModel);
                sd.ParentServiceSheetVM = this;
            }
            this.AllServiceDays = serviceDaysEntered;
        }