public ActionResult SubmitApplication(int acquisitionid)
        {
            var login = ObjectBuilder.GetObject<ILoginUserPersistance>("LoginUserPersistance").GetByUserName(User.Identity.Name);
            if (null != login)
            {
                if (login.ApplicantId.HasValue)
                {
                    var applicant = ObjectBuilder.GetObject<IApplicantPersistence>("ApplicantPersistence").GetApplicant(login.ApplicantId.Value);

                    var message = string.Empty;
                    // check the criteria
                    if (!AtmHelper.ValidateHeightWeightBmi(Convert.ToDouble(applicant.Height), Convert.ToDouble(applicant.Weight), acquisitionid, applicant.GenderCd, out message))
                        return Json(new { OK = false, message = "Permohonan anda tidak berjaya dihantar." + message });

                    if (acquisitionid != 0)
                    {
                        // check based on acquisition
                        var acq = ObjectBuilder.GetObject<IAcquisitionPersistence>("AcquisitionPersistence").GetAcquisition(acquisitionid);
                        if (null != acq)
                        {
                            var acqtype = ObjectBuilder.GetObject<IReferencePersistence>("ReferencePersistence").GetAcquisitionType(acq.AcquisitionTypeCd.Value);
                            if (acqtype != null)
                            {
                                // perempuan
                                if (acq.AcquisitionTypeCd == 3)
                                    if (applicant.GenderCd == "L")
                                        return Json(new { OK = false, message = "Permohonan anda tidak berjaya dihantar kerana pengambilan ini untuk " + acqtype.AcquisitionTypeNm });
                                // lelaki
                                if (acq.AcquisitionTypeCd == 2)
                                    if (applicant.GenderCd == "P")
                                        return Json(new { OK = false, message = "Permohonan anda tidak berjaya dihantar kerana pengambilan ini untuk " + acqtype.AcquisitionTypeNm });

                                // Pegawai.. Check the selection indicator
                                if (acqtype.ServiceCd == "10")
                                {
                                    if (!applicant.SelectionTD.HasValue && !applicant.SelectionTL.HasValue && !applicant.SelectionTU.HasValue)
                                        return Json(new { OK = false, message = "Permohonan anda tidak berjaya dihantar. Sila pilih Keutamaan Perkhidmatan Pilihan" });
                                }
                            }

                        }

                        // mandatory checking on profile photo
                        var photo = ObjectBuilder.GetObject<IApplicantPersistence>("ApplicantPersistence").GetPhoto(applicant.ApplicantId);
                        if (photo == null)
                            return Json(new { OK = false, message = "Permohonan anda tidak berjaya dihantar. Sila muat naik gambar peribadi berukuran pasport (saiz tidak melebihi 500KB)." });

                        if (photo.Photo == null && string.IsNullOrWhiteSpace(photo.PhotoExt))
                            return Json(new { OK = false, message = "Permohonan anda tidak berjaya dihantar. Sila muat naik gambar peribadi berukuran pasport (saiz tidak melebihi 500KB)." });

                        // mandatory checking on basic information
                        var peribadipoint = 0.0m;
                        var edupoint = 0.0m;
                        var spopoint = 0.0m;
                        var saspoint = 0.0m;
                        var chpoint = 0.0m;
                        AtmHelper.Checklist(applicant.ApplicantId, acquisitionid, out peribadipoint, out edupoint, out spopoint, out saspoint, out chpoint);

                        if (peribadipoint != 100m)
                            return Json(new { OK = false, message = "Permohonan anda tidak berjaya dihantar. Maklumat peribadi tidak lengkap." });
                        if (edupoint != 100m)
                            return Json(new { OK = false, message = "Permohonan anda tidak berjaya dihantar. Maklumat akademik tidak lengkap." });
                        if (chpoint != 100m)
                            return Json(new { OK = false, message = "Permohonan anda tidak berjaya dihantar. Maklumat pengakuan tidak lengkap." });

                        // copy applicant to applicant submitted
                        var app = new ApplicantSubmitted()
                                  {
                                      AcquisitionId = acquisitionid,
                                      Height = applicant.Height,
                                      Weight = applicant.Weight,
                                      FullName = applicant.FullName,
                                      BMI = applicant.BMI,
                                      BirthCertNo = applicant.BirthCertNo,
                                      BirthCityCd = applicant.BirthCityCd,
                                      BirthCountryCd = applicant.BirthCountryCd,
                                      BirthPlace = applicant.BirthPlace,
                                      BirthStateCd = applicant.BirthStateCd,
                                      BirthDt = applicant.BirthDt.HasValue ? applicant.BirthDt.Value : applicant.BirthDt,
                                      CreatedBy = User.Identity.Name,
                                      CreatedDt = DateTime.Now,
                                      CorresponAddr1 = applicant.CorresponAddr1,
                                      CorresponAddr2 = applicant.CorresponAddr2,
                                      CorresponAddr3 = applicant.CorresponAddr3,
                                      CorresponAddrCityCd = applicant.CorresponAddrCityCd,
                                      CorresponAddrCountryCd = applicant.CorresponAddrCountryCd,
                                      CorresponAddrPostCd = applicant.CorresponAddrPostCd,
                                      CorresponAddrStateCd = applicant.CorresponAddrStateCd,
                                      GenderCd = applicant.GenderCd,
                                      NationalityCd = applicant.NationalityCd,
                                      NationalityCertNo = applicant.NationalityCertNo,
                                      MobilePhoneNo = applicant.MobilePhoneNo,
                                      HomePhoneNo = applicant.HomePhoneNo,
                                      DadNationalityCd = applicant.DadNationalityCd,
                                      DadName = applicant.DadName,
                                      DadICNo = applicant.DadICNo,
                                      DadOccupation = applicant.DadOccupation,
                                      DadPhoneNo = applicant.DadPhoneNo,
                                      DadSalary = applicant.DadSalary,
                                      MomName = applicant.MomName,
                                      MomNationalityCd = applicant.NationalityCd,
                                      MomICNo = applicant.MomICNo,
                                      MomOccupation = applicant.MomOccupation,
                                      MomSalary = applicant.MomSalary,
                                      MomPhoneNo = applicant.MomPhoneNo,
                                      MrtlStatusCd = applicant.MrtlStatusCd,
                                      ChildNo = applicant.ChildNo,
                                      ColorBlindInd = applicant.ColorBlindInd,
                                      EthnicCd = applicant.EthnicCd,
                                      RaceCd = applicant.RaceCd,
                                      ReligionCd = applicant.ReligionCd,
                                      Email = applicant.Email,
                                      GuardianName = applicant.GuardianName,
                                      GuardianNationalityCd = applicant.GuardianNationalityCd,
                                      GuardianOccupation = applicant.GuardianOccupation,
                                      GuardianICNo = applicant.GuardianICNo,
                                      GuardianSalary = applicant.GuardianSalary,
                                      GuardianPhoneNo = applicant.GuardianPhoneNo,
                                      NewICNo = applicant.NewICNo,
                                      ScholarshipContractStDate = applicant.ScholarshipContractStDate,
                                      CurrentOccupation = applicant.CurrentOccupation,
                                      SelectionTD = applicant.SelectionTD,
                                      SelectionTL = applicant.SelectionTL,
                                      SelectionTU = applicant.SelectionTU,
                                      ArmyServiceInd = applicant.ArmyServiceInd,
                                      ArmyServiceYrOfServ = applicant.ArmyServiceYrOfServ,
                                      ArmyServiceResignRemark = applicant.ArmyServiceResignRemark,
                                      ArmySelectionInd = applicant.ArmySelectionInd,
                                      ArmySelectionDt = applicant.ArmySelectionDt,
                                      ArmySelectionVenue = applicant.ArmySelectionVenue,
                                      ComputerICT = applicant.ComputerICT,
                                      ComputerMSExcel = applicant.ComputerMSExcel,
                                      ComputerMSPwrPoint = applicant.ComputerMSPwrPoint,
                                      ComputerMSWord = applicant.ComputerMSWord,
                                      ComputerOthers = applicant.ComputerOthers,
                                      PalapesArmyNo = applicant.PalapesArmyNo,
                                      PalapesInd = applicant.PalapesInd,
                                      PalapesInstitution = applicant.PalapesInstitution,
                                      PalapesRemark = applicant.PalapesRemark,
                                      PalapesServices = applicant.PalapesServices,
                                      PalapesTauliahEndDt = applicant.PalapesTauliahEndDt,
                                      PalapesYear = applicant.PalapesYear,
                                      CurrentOrganisation = applicant.CurrentOrganisation,
                                      CurrentSalary = applicant.CurrentSalary,
                                      ScholarshipInd = applicant.ScholarshipInd,
                                      ScholarshipBody = applicant.ScholarshipBody,
                                      ScholarshipBodyAddr = applicant.ScholarshipBodyAddr,
                                      ScholarshipNoOfYrContract = applicant.ScholarshipNoOfYrContract,
                                      EmployeeAggreeInd = applicant.EmployeeAggreeInd,
                                      CronicIlnessInd = applicant.CronicIlnessInd,
                                      CrimeInvolvement = applicant.CrimeInvolvement,
                                      DrugCaseInvolvement = applicant.DrugCaseInvolvement,
                                      NoOfSibling = applicant.NoOfSibling,
                                      NoTentera = applicant.NoTentera,
                                      SpectaclesUserInd = applicant.SpectaclesUserInd,
                                      OriginalPelepasanDocument = applicant.OriginalPelepasanDocument,
                                      PelepasanDocument = applicant.PelepasanDocument,
                                      MomNotApplicable = applicant.MomNotApplicable,
                                      DadNotApplicable = applicant.DadNotApplicable,
                                      GuardianNotApplicable = applicant.GuardianNotApplicable
                                  };

                        var idsubmitted = app.Save();
                        if (idsubmitted != 0)
                        {
                            app.ApplicantId = idsubmitted;

                            // get educations
                            var education = ObjectBuilder.GetObject<IApplicantPersistence>("ApplicantPersistence").GetEducation(applicant.ApplicantId);
                            if (null != education && education.Any())
                            {
                                foreach (var edu in education)
                                {
                                    if (!string.IsNullOrWhiteSpace(edu.OverallGrade) || edu.SKMLevel != 0 ||
                                        edu.ConfermentYr != 0)
                                    {
                                        var subedu = new ApplicantEducationSubmitted
                                                     {
                                                         ApplicantId = app.ApplicantId,
                                                         ConfermentYr = edu.ConfermentYr,
                                                         EduCertTitle = edu.EduCertTitle,
                                                         HighEduLevel = edu.HighEduLevel,
                                                         HighEduLevelCd = edu.HighEduLevelCd,
                                                         InstCd = edu.InstCd,
                                                         InstitutionName = edu.InstitutionName,
                                                         OverSeaInd = edu.OverSeaInd,
                                                         MajorMinorCd = edu.MajorMinorCd,
                                                         OverallGrade = edu.OverallGrade,
                                                         SKMLevel = edu.SKMLevel,
                                                         CreatedBy = User.Identity.Name,
                                                         CreatedDt = DateTime.Now,
                                                     };
                                        var apeduid = subedu.Save();
                                        foreach (var subject in edu.ApplicantEduSubjectCollection)
                                        {
                                            if (!string.IsNullOrWhiteSpace(subject.Grade) ||
                                                !string.IsNullOrWhiteSpace(subject.GradeCd))
                                            {
                                                var subsubject = new ApplicantEduSubjectSubmitted
                                                                 {
                                                                     GradeCd = !string.IsNullOrWhiteSpace(subject.GradeCd) ? subject.GradeCd.Trim() : subject.GradeCd,
                                                                     Grade = !string.IsNullOrWhiteSpace(subject.Grade) ? subject.Grade.Trim() : subject.Grade,
                                                                     ApplicantEduId = apeduid,
                                                                     CreatedBy = User.Identity.Name,
                                                                     CreatedDt = DateTime.Now,
                                                                     Subject = subject.Subject,
                                                                     SubjectCd = subject.SubjectCd,
                                                                 };
                                                subsubject.Save();
                                            }
                                        }
                                    }
                                }
                            }

                            // get sports
                            var sports = ObjectBuilder.GetObject<IApplicantPersistence>("ApplicantPersistence").GetSport(applicant.ApplicantId);
                            if (null != sports && sports.Any())
                            {
                                foreach (var sp in sports)
                                {
                                    if ((sp.SportAssocId.HasValue && sp.SportAssocId != 0) || !string.IsNullOrWhiteSpace(sp.Others))
                                    {
                                        var ssp = new ApplicantSportSubmitted
                                                  {
                                                      ApplicantId = app.ApplicantId,
                                                      CreatedBy = User.Identity.Name,
                                                      CreatedDt = DateTime.Now,
                                                      AchievementCd = sp.AchievementCd,
                                                      Year = sp.Year,
                                                      Others = sp.Others,
                                                      SportAssocId = sp.SportAssocId,
                                                  };
                                        ssp.Save();
                                    }
                                }
                            }

                            // get skills
                            var skills = ObjectBuilder.GetObject<IApplicantPersistence>("ApplicantPersistence").GetSkill(applicant.ApplicantId);
                            if (null != skills && skills.Any())
                            {
                                foreach (var sp in skills)
                                {
                                    var ssp = new ApplicantSkillSubmitted
                                                  {
                                                      ApplicantId = app.ApplicantId,
                                                      CreatedBy = User.Identity.Name,
                                                      CreatedDt = DateTime.Now,
                                                      LanguageSkillSpeak = sp.LanguageSkillSpeak,
                                                      LanguageSkillWrite = sp.LanguageSkillWrite,
                                                      Skill = sp.Skill,
                                                      AchievementCd = sp.AchievementCd,
                                                      Others = sp.Others,
                                                      SkillCatCd = sp.SkillCatCd,
                                                      SkillCd = sp.SkillCd,
                                                  };
                                    ssp.Save();
                                }
                            }

                            // get photo
                            if (photo.Photo != null && !string.IsNullOrWhiteSpace(photo.PhotoExt))
                            {
                                var sphoto = new ApplicantSubmittedPhoto()
                                             {
                                                 Photo = photo.Photo,
                                                 PhotoExt = photo.PhotoExt,
                                                 ApplicantId = app.ApplicantId,
                                                 CreatedBy = User.Identity.Name,
                                                 CreatedDate = DateTime.Now
                                             };

                                sphoto.Save();
                            }

                            var application = new Application()
                                              {
                                                  AcquisitionId = acquisitionid,
                                                  CreatedBy = User.Identity.Name,
                                                  CreatedDt = DateTime.Now,
                                                  ApplicantId = app.ApplicantId
                                              };
                            var id = application.Save();
                            if (id != 0)
                                return Json(new { OK = true, message = "Permohonan anda berjaya dihantar.", id = id });
                        }
                    }
                }
                return Json(new { OK = false, message = "Permohonan anda tidak berjaya dihantar kerana maklumat tidak lengkap." });
            }
            return Json(new { OK = false, message = "Permohonan anda tidak berjaya dihantar." });
        }
        public int SaveSkill(ApplicantSkillSubmitted skill)
        {
            using (var entities = new atmEntities())
            {
                var exist = (from a in entities.tblApplicantSkillSubmitteds where a.ApplicantId == skill.ApplicantId && a.SkillCd == skill.SkillCd select a).SingleOrDefault();
                if (exist != null)
                {
                    skill.ApplicantSkillId = exist.ApplicantSkillId;
                    return UpdateSkill(skill);
                }

                var s = new tblApplicantSkillSubmitted
                {
                    AchievementCd = skill.AchievementCd,
                    ApplicantId = skill.ApplicantId,
                    CreatedBy = skill.CreatedBy,
                    CreatedDt = skill.CreatedDt,
                    LanguageSkillSpeak = skill.LanguageSkillSpeak,
                    LanguageSkillWrite = skill.LanguageSkillWrite,
                    Others = skill.Others,
                    SkillCatCd = skill.SkillCatCd,
                    SkillCd = skill.SkillCd
                };
                entities.tblApplicantSkillSubmitteds.Add(s);

                if (entities.SaveChanges() > 0)
                    return s.ApplicantSkillId;

            }
            return 0;
        }
        public int UpdateSkill(ApplicantSkillSubmitted skill)
        {
            using (var entities = new atmEntities())
            {
                var exist = (from a in entities.tblApplicantSkillSubmitteds where a.ApplicantSkillId == skill.ApplicantSkillId select a).SingleOrDefault();
                if (exist != null)
                {
                    exist.AchievementCd = skill.AchievementCd;
                    exist.ApplicantId = skill.ApplicantId;
                    exist.LastModifiedBy = skill.LastModifiedBy;
                    exist.LastModifiedDt = skill.LastModifiedDt;
                    exist.LanguageSkillSpeak = skill.LanguageSkillSpeak;
                    exist.LanguageSkillWrite = skill.LanguageSkillWrite;
                    exist.Others = skill.Others;
                    exist.SkillCatCd = skill.SkillCatCd;
                    exist.SkillCd = skill.SkillCd;

                    entities.SaveChanges();
                    return exist.ApplicantSkillId;
                }
            }
            return 0;
        }
        public IEnumerable<ApplicantSkillSubmitted> GetSkill(int applicantid, int acquisitionid)
        {
            var list = new List<ApplicantSkillSubmitted>();
            using (var entities = new atmEntities())
            {
                var l = from a in entities.tblApplicantSkillSubmitteds join b in entities.tblApplicantSubmiteds on a.ApplicantId equals b.ApplicantId where a.ApplicantId == applicantid && b.AcquisitionId == acquisitionid select a;
                if (l.Any())
                {
                    foreach (var s in l)
                    {
                        var sks = new ApplicantSkillSubmitted
                        {
                            AchievementCd = s.AchievementCd,
                            CreatedBy = s.CreatedBy,
                            CreatedDt = s.CreatedDt,
                            LastModifiedBy = s.LastModifiedBy,
                            LastModifiedDt = s.LastModifiedDt,
                            ApplicantId = s.ApplicantId,
                            ApplicantSkillId = s.ApplicantSkillId,
                            LanguageSkillSpeak = s.LanguageSkillSpeak,
                            LanguageSkillWrite = s.LanguageSkillWrite,
                            Others = s.Others,
                            Skill = s.tblREFSkill.Skill,
                            SkillCatCd = !string.IsNullOrWhiteSpace(s.SkillCatCd) ? s.SkillCatCd.Trim() : s.SkillCatCd,
                            SkillCd = !string.IsNullOrWhiteSpace(s.SkillCd) ? s.SkillCd.Trim() : s.SkillCd
                        };

                        if (!string.IsNullOrWhiteSpace(sks.SkillCd) && s.tblREFSkill != null)
                            sks.Skill = s.tblREFSkill.Skill;

                        list.Add(sks);
                    }
                }
            }
            return list;
        }