function CaseControlModel(JSObj, user) {
        var self = this;
        self.Age = ko.observable("");
        self.NumberOfChildren = ko.observable("");
        self.HighestLevelOfEducation = ko.observable("");
        self.IsSingletonPregnancy = ko.observable("true");
        self.WasConceivedWithFertilityTreatment = ko.observable("false");
        self.LivingLocation = ko.observable("");
        self.LivingLocationOther = ko.observable("");
        self.IsTakingPartInFetalStudy = ko.observable("false");
        self.CrlTakenBefore14weeks = ko.observable("false");
        self.EarliestCrlMeasurementDate = ko.observable("");
        self.IsTheExactCRLMeasurementKnown = ko.observable("true");
        self.CrlMeasurementAtTime = ko.observable("");
        self.EstimatedGAatCrlWeeks = ko.observable("");
        self.EstimatedGAatCrlDays = ko.observable("");
        self.HcTakenBefore24weeks = ko.observable("false");
        self.EarliestHcMeasurementDate = ko.observable("");
        self.IsTheExactHCMeasurementKnown = ko.observable("true");
        self.HcMeasurementAtTime = ko.observable("");
        self.EstimatedGAatHcWeeks = ko.observable("");
        self.EstimatedGAatHcDays = ko.observable("");
        self.DateOfDelivery = ko.observable("");
        self.BirthWeight = ko.observable("");
        self.NameOfResearcher = ko.observable("");
        self.ResearcherCode = ko.observable("");
        self.Id = ko.observable("");
        self.CurrentPage = ko.observable(1);
        self.DateCreated = ko.observable(new Date().getTime());
        self.DatabaseId = ko.observable(0);
        self.GaveConsent = ko.observable("false");
        self.PTID = ko.observable("");
        self.HospitalNr = ko.observable("");
        self.Included = ko.observable(null);
        self.WomensName = ko.observable("");
        self.WillCollectSamples = ko.observable("true");
        self.ReasonsWhySamplesHaveNotBeenTaken = ko.observable("");
        self.LocalId = ko.observable(-1);
        self.QuotaId = ko.observable(-1);
        self.NewbornSex = ko.observable("");

        var mappProperties = ['Age', 'NumberOfChildren', 'HighestLevelOfEducation', 'IsSingletonPregnancy', 'WasConceivedWithFertilityTreatment',
                                'LivingLocation', 'IsTakingPartInFetalStudy', 'CrlTakenBefore14weeks', 'EarliestCrlMeasurementDate',
                                'CrlMeasurementAtTime', 'EstimatedGAatCrlWeeks', 'EstimatedGAatCrlDays', 'HcTakenBefore24weeks',
                                'EarliestHcMeasurementDate', 'HcMeasurementAtTime', 'EstimatedGAatHcWeeks', 'EstimatedGAatHcDays',
                                'DateOfDelivery', 'BirthWeight', 'NameOfResearcher', 'ResearcherCode',
                                'Id', 'CurrentPage', 'DateCreated', 'DatabaseId', 'GaveConsent', 'PTID', 'HospitalNr', 'Included', 'WomensName',
                                'WillCollectSamples', 'ReasonsWhySamplesHaveNotBeenTaken', 'IsTheExactCRLMeasurementKnown', 'IsTheExactHCMeasurementKnown',
                                'LivingLocationOther', 'LocalId', 'QuotaId', 'NewbornSex'
                            ];

        self.User = user;

        /* computed */
        self.IsLivingLocationOtherRequired = ko.computed(function () {
            return self.LivingLocation() == "46" || self.LivingLocation() == "47";
        });

        self.DateCreatedAsDate = function () {
            return new Date(self.DateCreated());
        }

        self.DateCreatedUTC = ko.computed(function () {
            return self.DateCreatedAsDate();
        });

        self.DateCreatedFormatted = ko.computed(function () {
            var date = self.DateCreatedAsDate();
            return date.toLocaleDateString();
        });

        self.Page1 = ko.computed(function () {
            var notEligibleLocation = ['2', '5', '9', '50', '52'];
            return (self.Age() >= 18 && self.IsSingletonPregnancy() == "true" && self.IsTakingPartInFetalStudy() == "false" && self.WasConceivedWithFertilityTreatment() == "false" && jQuery.inArray(self.LivingLocation(), notEligibleLocation) < 0);
        });

        self.Page2 = ko.computed(function () {
            return (self.CrlTakenBefore14weeks() == "true" || self.HcTakenBefore24weeks() == "true");
        });

        self.IsEligible = function () {
            if (self.CurrentPage() == 1)
                return self.Page1();
            else if (self.CurrentPage() == 2)
                return self.Page2();
            else if (self.CurrentPage() == 3)
                return self.Page3();
            else
                return self.Page1() && self.Page2() && self.Page3();
        }

        self.GetPreviousPage = function () {
            var page = 1;
            if (self.Page1() && self.CurrentPage() > 2)
                page = 2;
            if (self.Page1() && self.Page2() && self.CurrentPage() > 3)
                page = 3;
            if (self.Page1() && self.Page2() && self.Page3() && self.CurrentPage() > 4) {
                if (self.IsNotPretermCase())
                    page = 4;
                else
                    page = 3;
            }

            self.QuotaId(-1); //reset quota

            return page;
        }

        self.GetCurrentDateMinus9Months = function () {
            var date = Date.today().add(-9).months();

            return self.GetFormattedDate(date);
        }

        self.GetCurrentDateMinus7Days = function () {
            var date = Date.today().add(-7).days();

            return self.GetFormattedDate(date);
        }

        self.GetFormattedDate = function (date) {
            if (date == null)
                date = new Date();
            
            var yyyy = date.getFullYear().toString();
            var mm = (date.getMonth() + 1).toString(); // getMonth() is zero-based
            var dd = date.getDate().toString();
            return yyyy + "-" + (mm[1] ? mm : "0" + mm[0]) + "-" + (dd[1] ? dd : "0" + dd[0]); // padding
        };

        self.GetCurrentDate = ko.computed(function () {
            self.CurrentPage(); //force update when current page changes
            return self.GetFormattedDate(Date.today());
        });

        self.IsTheExactCRLMeasurementKnownCalculated = ko.computed(function () {
            if (self.Page1() && self.CrlTakenBefore14weeks() == "true")
                return (self.IsTheExactCRLMeasurementKnown() == "true");
            else
                return false;
        });

        self.IsTheExactHCMeasurementKnownCalculated = ko.computed(function () {
            if (self.Page1() && self.HcTakenBefore24weeks() == "true" && self.CrlTakenBefore14weeks() == "false")
                return (self.IsTheExactHCMeasurementKnown() == "true");
            else
                return false;
        });

        self.AreEstimatedGaCrlRequired = ko.computed(function () {
            if (self.Page1() && self.CrlTakenBefore14weeks() == "true")
                return !(self.IsTheExactCRLMeasurementKnown() == "true");
            else
                return false;
        });

        self.AreEstimatedGaHcRequired = ko.computed(function () {
            if (self.Page1() && self.HcTakenBefore24weeks() == "true" && self.CrlTakenBefore14weeks() == "false")
                return !(self.IsTheExactHCMeasurementKnown() == "true");
            else
                return false;
        });

       function GetDateFromDateString(dateString) {
           return Date.UTC(parseInt(dateString.substring(0, 4), 10), parseInt(dateString.substring(5, 7), 10) - 1, parseInt(dateString.substring(8, 10), 10));
       }

       function GetDateDifferenceInDays(laterDateString, earlierDateString) {
           var laterDate = GetDateFromDateString(laterDateString);
           var earlierDate = GetDateFromDateString(earlierDateString);

           return (laterDate - earlierDate) / 86400000;
       }

       function GetGestationalAge(laterDateString, earlierDateString, measurementType, measurement, gaWeeks, gaDays) {
           var gaInDays = 0;
           if (measurementType == 'CRL') {
               gaInDays = Math.round(40.9041 + 3.21585 * Math.sqrt(measurement) + 0.348956 * measurement); //new CRL formula 
           }
           else if (measurementType == 'HC') {
               //HC is in cm, but formula is for mm
               measurement = measurement * 10;
               var ga = Math.exp(0.010611 * measurement - 0.000030321 * Math.pow(measurement, 2) + 0.43498 * Math.pow(10, -7) * Math.pow(measurement, 3) + 1.848); //HC formula

               gaInDays = Math.floor(ga) * 7 + Math.round((ga - Math.floor(ga)) * 7);
           }
           else
               gaInDays = parseInt(gaWeeks, 10) * 7 + parseInt(gaDays, 10);
               
           var days = GetDateDifferenceInDays(laterDateString, earlierDateString);

           return days + gaInDays;
       }

       self.GestationalAgeInDays = ko.computed(function () {
           var result = 0;
           if (self.CrlTakenBefore14weeks() == 'true' && self.CurrentPage() > 2)
               result = GetGestationalAge(self.DateOfDelivery(), self.EarliestCrlMeasurementDate(), self.IsTheExactCRLMeasurementKnown() == 'true' ? 'CRL' : '', self.CrlMeasurementAtTime(), self.EstimatedGAatCrlWeeks(), self.EstimatedGAatCrlDays());
           else if (self.HcTakenBefore24weeks() == 'true' && self.CurrentPage() > 2)
               result = GetGestationalAge(self.DateOfDelivery(), self.EarliestHcMeasurementDate(), self.IsTheExactHCMeasurementKnown() == 'true' ? 'HC' : '', self.HcMeasurementAtTime(), self.EstimatedGAatHcWeeks(), self.EstimatedGAatHcDays());

           return result;
       });

       self.GestationalAgeWeeks = ko.computed(function () {
           var ga = self.GestationalAgeInDays();
           if (isNaN(ga))
               return null;
           else
               return Math.floor(ga / 7);
       });

       self.GestationalAgeDays = ko.computed(function () {
           var ga = self.GestationalAgeInDays();
           if (isNaN(ga))
               return null;
           else
               return ga % 7;
       });

       self.Page3 = ko.computed(function () {
           return self.GestationalAgeInDays() < (42 * 7 + 0) && self.GestationalAgeInDays() > (23 * 7 + 6); //> 23w6d and < 24w0 d;
       });

        self.CaseControlType = ko.computed(function () {
            var days = self.GestationalAgeInDays();
            var sex = self.NewbornSex();
            var weight = self.BirthWeight();

            //for Nairobi - different rules what to do with <36w babies because all preterms recruited
            if (self.User.CountryId == 5 && (weight == '' || days >= (30 * 7 + 0) && days <= (35 * 7 + 6)))
            {
                //if birthweight is not entered yet, return as a possible SGA to always ask for BirthWeight/Gender if all preterms recruited
                if (weight == '')
                    return 'P3';

                if (days >= (30 * 7 + 0) && days <= (30 * 7 + 6)) {
                    if (sex == 2) //Female
                    {
                        if (weight < 470)
                            return 'P3';
                        else if (weight < 590)
                            return 'P3-P9.9';
                        else
                            return 'Case';
                    }
                    else //Male
                    {
                        if (weight < 460)
                            return 'P3';
                        else if (weight < 610)
                            return 'P3-P9.9';
                        else
                            return 'Case';
                    }
                }
                else if (days >= (31 * 7 + 0) && days <= (31 * 7 + 6)) {
                    if (sex == 2) //Female
                    {
                        if (weight < 810)
                            return 'P3';
                        else if (weight < 930)
                            return 'P3-P9.9';
                        else
                            return 'Case';
                    }
                    else //Male
                    {
                        if (weight < 800)
                            return 'P3';
                        else if (weight < 950)
                            return 'P3-P9.9';
                        else
                            return 'Case';
                    }
                }
                else if (days >= (32 * 7 + 0) && days <= (32 * 7 + 6)) {
                    if (sex == 2) //Female
                    {
                        if (weight < 1120)
                            return 'P3';
                        else if (weight < 1250)
                            return 'P3-P9.9';
                        else
                            return 'Case';
                    }
                    else //Male
                    {
                        if (weight < 1110)
                            return 'P3';
                        else if (weight < 1270)
                            return 'P3-P9.9';
                        else
                            return 'Case';
                    }
                }
                else if (days >= (33 * 7 + 0) && days <= (33 * 7 + 6)) {
                    if (sex == 2) //Female
                    {
                        if (weight < 1400)
                            return 'P3';
                        else if (weight < 1530)
                            return 'P3-P9.9';
                        else
                            return 'Case';
                    }
                    else //Male
                    {
                        if (weight < 1400)
                            return 'P3';
                        else if (weight < 1550)
                            return 'P3-P9.9';
                        else
                            return 'Case';
                    }
                }
                else if (days >= (34 * 7 + 0) && days <= (34 * 7 + 6)) {
                    if (sex == 2) //Female
                    {
                        if (weight < 1660)
                            return 'P3';
                        else if (weight < 1790)
                            return 'P3-P9.9';
                        else
                            return 'Case';
                    }
                    else //Male
                    {
                        if (weight < 1660)
                            return 'P3';
                        else if (weight < 1810)
                            return 'P3-P9.9';
                        else
                            return 'Case';
                    }
                }
                else if (days >= (35 * 7 + 0) && days <= (35 * 7 + 6)) {
                    if (sex == 2) //Female
                    {
                        if (weight < 1890)
                            return 'P3';
                        else if (weight < 2020)
                            return 'P3-P9.9';
                        else
                            return 'Case';
                    }
                    else //Male
                    {
                        if (weight < 1900)
                            return 'P3';
                        else if (weight < 2050)
                            return 'P3-P9.9';
                        else
                            return 'Case';
                    }
                }
            }

            if (days < 36 * 7)
                return 'Case';
            else {
                if (days >= (36 * 7 + 0) && days <= (36 * 7 + 6)) {
                    if (sex == 2) //Female
                    {
                        if (weight < 2090)
                            return 'P3';
                        else if (weight < 2230)
                            return 'P3-P9.9';
                        else
                            return 'Case';
                    }
                    else //Male
                    {
                        if (weight < 2120)
                            return 'P3';
                        else if (weight < 2270)
                            return 'P3-P9.9';
                        else
                            return 'Case';
                    }
                }
                else if (days >= (37 * 7 + 0) && days <= (37 * 7 + 6)) {
                    if (sex == 2) //Female
                    {
                        if (weight < 2270)
                            return 'P3';
                        else if (weight < 2410)
                            return 'P3-P9.9';
                        else
                            return 'Case';
                    }
                    else //Male
                    {
                        if (weight < 2320)
                            return 'P3';
                        else if (weight < 2470)
                            return 'P3-P9.9';
                        else
                            return 'Case';
                    }
                }
                else if (days >= (38 * 7 + 0) && days <= (38 * 7 + 6)) {
                    if (sex == 2) //Female
                    {
                        if (weight < 2430)
                            return 'P3';
                        else if (weight < 2570)
                            return 'P3-P9.9';
                        else
                            return 'Control';
                    }
                    else //Male
                    {
                        if (weight < 2490)
                            return 'P3';
                        else if (weight < 2640)
                            return 'P3-P9.9';
                        else
                            return 'Control';
                    }
                }
                else if (days >= (39 * 7 + 0) && days <= (39 * 7 + 6)) {
                    if (sex == 2) //Female
                    {
                        if (weight < 2570)
                            return 'P3';
                        else if (weight < 2710)
                            return 'P3-P9.9';
                        else
                            return 'Control';
                    }
                    else //Male
                    {
                        if (weight < 2650)
                            return 'P3';
                        else if (weight < 2800)
                            return 'P3-P9.9';
                        else
                            return 'Control';
                    }
                }
                else if (days >= (40 * 7 + 0) && days <= (40 * 7 + 6)) {
                    if (sex == 2) //Female
                    {
                        if (weight < 2690)
                            return 'P3';
                        else if (weight < 2830)
                            return 'P3-P9.9';
                        else
                            return 'Control';
                    }
                    else //Male
                    {
                        if (weight < 2790)
                            return 'P3';
                        else if (weight < 2940)
                            return 'P3-P9.9';
                        else
                            return 'Control';
                    }
                }
                else if (days >= (41 * 7 + 0) && days <= (41 * 7 + 6)) {
                    if (sex == 2) //Female
                    {
                        if (weight < 2790)
                            return 'P3';
                        else if (weight < 2930)
                            return 'P3-P9.9';
                        else
                            return 'Control';
                    }
                    else //Male
                    {
                        if (weight < 2910)
                            return 'P3';
                        else if (weight < 3060)
                            return 'P3-P9.9';
                        else
                            return 'Control';
                    }
                }
            }
        });

        self.IsCase = ko.computed(function () {
            var type = self.CaseControlType();

            return (type == 'Case' || type == 'P3' || type == 'P3-P9.9');
        });

        self.IsNotPretermCase = function (quotaId) {
            if (typeof (quotaId) == "undefined")
                quotaId = self.QuotaId();

            return self.Page2() && !(quotaId == 0 || quotaId == 1 || quotaId == 2);
        };

        self.Eligible = ko.computed(function () {
            return self.IsEligible();
        });

        self.EnrolRequired = ko.computed(function () {
            return self.IsEligible() && self.GaveConsent() == "true" && self.WillCollectSamples() == "true";
        });

        self.ReasonRequired = ko.computed(function () {
            return self.IsEligible() && self.GaveConsent() == "true" && self.WillCollectSamples() == "false";
        });

        self.Load = function (obj) {
            LoadFromJson(obj);
            self.CurrentPage.notifySubscribers(); //force update of current date
        }

        LoadFromJson(JSObj);
   }

   var self = this;

   self.User = (userInformation) ? JSON.parse(userInformation) : {};

   if (self.User.ForceOnlineMode)
       self.DataProvider = new OnlineStoreProvider(self.User);
   else
       self.DataProvider = new OfflineStoreProvider(self.User, localStorageDomain, localStoragePrefix, texts);

   self.DataStore = self.DataProvider.DataStore;
   self.Unfinished = self.DataProvider.Unfinished;
   self.Unsynced = self.DataProvider.Unsynced;
   self.ItemCount = self.DataProvider.ItemCount;
   self.GetNew = function () { return self.DataProvider.GetNew(); }
   self.Get = function (id) { return self.DataProvider.Get(id); }
   self.CalculateInclusion = function (model) { self.DataProvider.CalculateInclusion(model); }
   self.IsUniquePTID = function (PTID) { return self.DataProvider.IsUniquePTID(PTID); }
   self.UpdateQuota = function (model) { self.DataProvider.UpdateQuota(model); }
   self.save = function (model) { return self.DataProvider.save(model); }
   self.saveFormFields = function (serializedString, model) { return self.DataProvider.saveFormFields(serializedString, model); }
   self.SyncWithDatabase = function () { self.DataProvider.SyncWithDatabase(); }
   self.GetQuotaId = function (model, ignoreBirthWeight) { return self.DataProvider.GetQuotaId(model, ignoreBirthWeight); }
   self.LoadUnfinishedScreenings = function () { self.DataProvider.LoadUnfinishedScreenings(); }

   function OfflineStoreProvider(user, localStorageDomain, localStoragePrefix, texts) {
   var self = this;
   self.DataStore = ko.observableArray();
   self.User = user;
   self.DataStoreName = self.User.CountryId + (self.User.HospitalCode ? '-' + self.User.HospitalCode : '');
   self.LocalStorageDomain = localStorageDomain;
   self.LocalStoragePrefix = localStoragePrefix;

    self.SyncedItemsCount = ko.observable(0);
    self.SyncedPTIDs = [];
    self.PTIDs = []; //list of all PTIDs for unique checks
    self.SerializedForms = {};

    self.NewId = function () {
        return ko.utils.arrayFilter(self.DataStore(), function (item) {
            return item.Id > self.MaxId
        }).length + 1 + self.MaxId;
    }

    self.Unfinished = ko.computed(function () {
        return ko.utils.arrayFilter(self.DataStore(), function (item) {
            return item.CurrentPage < 6;
        });
    });

    self.IsUnsynced = function (item) {
        return item.CurrentPage == 6 && item.DatabaseId == 0;
    }

    self.Unsynced = ko.computed(function () {
        return ko.utils.arrayFilter(self.DataStore(), function (item) {
            return self.IsUnsynced(item);
        });
    });

    self.Quota = [];
    self.MaxId = 0;
    self.Version = 8;
    self.TotalRecruitedSGAInDatabase = 0;
    self.TotalRecruitedPretermInDatabase = 0;

    function LoadUnfinished() {
        self.Quota[0] = { total: 0, included: 0 };   //<36 weeks (that is, up to 35+6) = 100% (Preterm case)
        self.Quota[1] = { total: 0, included: 0 };   //36+0 to 36+6 = 50% (Preterm case)
        self.Quota[2] = { total: 0, included: 0 };   //37+0 to 37+6 = 5% (Preterm case)
        self.Quota[3] = { total: 0, included: 0 };   //BW/GA <P3 = 100% (SGA case)
        self.Quota[4] = { total: 0, included: 0 };   //BW/GA P3-P9.96 = 5% (SGA case)
        self.Quota[5] = { total: 0, included: 0 };   //GA 38+0 to 41+6 weeks and BW / GA >= P10 (controls)

        var data = local.GetData();

        for (var key in data) {
            var model = MapModel(data[key]);
            self.DataStore.push(ko.toJS(model));//revert to regular object to prevent object change tracking
        }
    };

    self.ItemCount = ko.computed(function() {
        return self.DataStore().length + self.SyncedItemsCount();
    });

    self.GetNew = function () {
        var model = new CaseControlModel(null, self.User);
        model.Id(self.NewId());
        return model;
    }

    self.Get = function (id) {
        if (typeof (id) == "undefined")
            id = -1;

        var itemCount = self.DataStore().length;
        if (id >= 0 && itemCount > 0 && id <= (itemCount - 1)) {
            var modelObject = self.DataStore()[id]; //JS object, not Knockout object
            modelObject.LocalId = id;
            return modelObject;
        }

        return ko.toJS(self.GetNew());
    };

    self.GetQuotaId = function (model, ignoreBirthWeight) {
        var quotaId = -1;
        var birthWeight = model.BirthWeight();
        if (ignoreBirthWeight)
            model.BirthWeight("");
        //for GA between 36 and 38 weeks and back/next button behavior we need to ignore birthWeight when calculating case/controls
        var caseControlType = model.CaseControlType();
        if (ignoreBirthWeight)
            model.BirthWeight(birthWeight);

        //for Nairobi, different quota calculation as all preterms have been recruited
        if (self.User.CountryId == 5)
        {
            //always ask for birthweight
            if (birthWeight == '')
                quotaId = 3;
            else
            {
                if (caseControlType == 'P3') //BW/GA <P3
                    quotaId = 3;
                else if (caseControlType == 'P3-P9.9') //BW/GA P3-P9.9
                    quotaId = 4;
                else if (caseControlType == 'Case') //Preterm case
                    quotaId = -1;
                else 
                    quotaId = 5; //control
            }

            return quotaId;
        }

        if (model.GestationalAgeInDays() < 36 * 7) //< 36w
            quotaId = 0;
        else if (model.GestationalAgeInDays() < 37 * 7) {
            //check if according to algorithm this would be included as Preterm

            //change as of 2012-11-26 - include all 36 weekers as preterms
//            var included = self.Quota[1].included;
//            var notIncluded = self.Quota[1].total - self.Quota[1].included;

//            if (included > notIncluded) {
//                //not preterm case - get SGA case type
//                if (caseControlType == 'P3')
//                    quotaId = 3;
//                else if (caseControlType == 'P3-P9.9')
//                    quotaId = 4;
//                else
//                    quotaId = 1;
//            }
//            else
                quotaId = 1;
        }
        else if (model.GestationalAgeInDays() < 38 * 7) { //<38w 
            //check if according to algorithm this would be included as Preterm
            var included = self.Quota[2].included;
            var notIncluded = self.Quota[2].total - self.Quota[2].included;

            //change as of 2012-11-26 - include 20% (instead of 5)
            if (included * 5 > self.Quota[2].total) {
                //not preterm case - get SGA case type
                if (caseControlType == 'P3')
                    quotaId = 3;
                else if (caseControlType == 'P3-P9.9')
                    quotaId = 4;
                else
                    quotaId = 2;
            }
            else
                quotaId = 2;
        }
        else if (caseControlType == 'P3') //BW/GA <P3
            quotaId = 3;
        else if (caseControlType == 'P3-P9.9') //BW/GA P3-P9.9
            quotaId = 4;
        else
            quotaId = 5; //control

        return quotaId;
    }

    self.CalculateInclusion = function (model) {
        if (!model.Eligible()) {
            model.Included(false);
            return;
        }

        var quotaId = self.GetQuotaId(model, false);
        model.QuotaId(quotaId);

        //a patient could be eligible but quotaID not determined for Nairobi when recruiting preterms as SGA, but birthweight cut-off values are not satisfied
        if (quotaId == -1)
        {
            model.Included(false);
            return;
        }

        var included = self.Quota[quotaId].included;
        var notIncluded = self.Quota[quotaId].total - self.Quota[quotaId].included;

        if (self.User.CountryId == 11 && !model.IsCase()) //Thailand - no longer recruit controls, requested by Rachel 2015-11-11
        {
            model.Included(false);
            return;
        }

        if (quotaId == 0)
            model.Included(true);
        else if (quotaId == 1)
            model.Included(true); //change of as 2012-11-26 - include all
        else if (quotaId == 2)
            model.Included(!(included * 5 > self.Quota[quotaId].total)); //change of as 2012-11-26 - include 20%
        else if (quotaId == 3)
        {
            if (self.IsRecruitmentCompleted(quotaId))
            {
                //reset quota to -1 to not include in statistics
                model.QuotaId(-1);
                model.Included(false);
            }
            else
                model.Included(true);
        }
        else if (quotaId == 4) {
            if (self.IsRecruitmentCompleted(quotaId))
                model.Included(false);
            else
                model.Included(!(included > notIncluded));
        }
        else if (quotaId == 5 && !model.IsCase()) {
            var includedCases = self.Quota[0].included + self.Quota[1].included + self.Quota[2].included + self.Quota[3].included + self.Quota[4].included;

            //special case for Brazil - requested by Rachel 2016-06-22
            //recruit 9 additional controls for site 02 instead of site 04
            if (self.User.CountryId == 1 && self.User.HospitalCode == "02")
                includedCases = includedCases + 9;

            model.Included(included < includedCases);
        }
    }

    self.IsRecruitmentCompleted = function(quotaId)
    {
        if (self.User.CountryId == 11 && (quotaId == 3 || quotaId == 4)) //Thailand - SGA 230
        {
            return (self.TotalRecruitedSGAInDatabase + self.Quota[3].included + self.Quota[4].included) >= 230;
        }
        else if (self.User.CountryId == 5 && (quotaId == 3 || quotaId == 4)) //Nairobi - SGA 250
        {
            return (self.TotalRecruitedSGAInDatabase + self.Quota[3].included + self.Quota[4].included) >= 250;
        }

        return false;
    };

    self.UpdateQuota = function (model) {
        if (model.IsEligible() && model.QuotaId() >= 0) { //quotaID could be -1 for Nairobi when preterms are recruited as SGAs but do not satisfy cut-off values
            var quotaId = model.QuotaId();
            var ga = model.GestationalAgeInDays();

            //If she is eligible but then does not consent or give samples do not add this women to total number of patients and calculate percentage from this total number
            if (!model.Included() || (model.Included() && model.GaveConsent() == 'true' && model.WillCollectSamples() == 'true'))
            {
                self.Quota[quotaId].total++;
                //if this is preterm case but chosen as SGA, add "missing" (increase total) in Quota
                //For Nairobi, do not include SGAs in Preterms statistics (e.g. 36w, but quotaId=3)
                if (ga >= 36 * 7 && ga < 38 * 7 && quotaId != 1 && quotaId != 2 && self.User.CountryId != 5) {
                    if (ga < 37 * 7)
                        self.Quota[1].total++;
                    else
                        self.Quota[2].total++;
                }
            }

            //change 2014-10-30 for possible SGAs that did not consent/samples not collected, increase totals
            //for the quotas of “37 weeks” and “BW/GA P5-P9.9” only
            if (model.Included() && (model.GaveConsent() == 'false' || model.WillCollectSamples() == 'false'))
            {
                if (quotaId == 2 || ga >= 37 * 7 && ga < 38 * 7)
                    self.Quota[2].total++;
                else if (quotaId == 4)
                    self.Quota[4].total++;
            }

            if (model.Included() && model.GaveConsent() == 'true' && model.WillCollectSamples() == 'true')
                self.Quota[quotaId].included++;
        }
    };
}