public GenericMPFFile CreateMPFFileObject()
    {
        if (FileType.SelectedValue.Equals("AMCND"))
        {
            HSBCMPFFile mpfFile = new HSBCMPFFile(dbConn);
            if (this.PaymentMethod.SelectedValue.Equals("B"))
            {
                mpfFile.PaymentMethod = HSBCMPFFile.PaymentMethodEnum.DIRECT_CREDIT_TO_BANK_ACC;
            }
            else if (this.PaymentMethod.SelectedValue.Equals("D"))
            {
                mpfFile.PaymentMethod = HSBCMPFFile.PaymentMethodEnum.DIRECT_DEBIT;
            }
            else if (this.PaymentMethod.SelectedValue.Equals("C"))
            {
                mpfFile.PaymentMethod = HSBCMPFFile.PaymentMethodEnum.CHEQUE;
            }
            return(mpfFile);
        }
        else if (FileType.SelectedValue.Equals("D") || FileType.SelectedValue.Equals("AMPFF"))
        {
            string RemoteProfileID = txtRemoteProfileID.Text;
            //if (Session["CompanyDBID"] != null)
            //    if (Application["MasterDBConfig"] != null)
            //    {
            //        HROne.DataAccess.DatabaseConnection masterDBConn = ((DatabaseConfig)Application["MasterDBConfig"]).CreateDatabaseConnectionObject();
            //        HROne.SaaS.Entities.ECompanyDatabase companyDatabase = new HROne.SaaS.Entities.ECompanyDatabase();
            //        companyDatabase.CompanyDBID = (int)Session["CompanyDBID"];
            //        if (HROne.SaaS.Entities.ECompanyDatabase.db.select(masterDBConn, companyDatabase))
            //        {
            //            RemoteProfileID = companyDatabase.CompanyDBClientCode;
            //        }
            //    }
            if (FileType.SelectedValue.Equals("D"))
            {
                HSBCMPFGatewayFileEncrypted mpfFile = new HSBCMPFGatewayFileEncrypted(dbConn, RemoteProfileID, "HROne " + ProductVersion.CURRENT_PROGRAM_VERSION);
                return(mpfFile);
            }
            else if (FileType.SelectedValue.Equals("AMPFF"))    //check again for safety
            {
                HSBCMPFGatewayFile mpfFile = new HSBCMPFGatewayFile(dbConn, RemoteProfileID);
                return(mpfFile);
            }
        }

        return(new GenericMPFFile(dbConn));
    }
    protected void btnGenerate_Click(object sender, EventArgs e)
    {
        PageErrors errors = PageErrors.getErrors(db, Page.Master);

        errors.clear();

        ArrayList empList = new ArrayList();

        foreach (RepeaterItem i in empRepeater.Items)
        {
            CheckBox cb = (CheckBox)i.FindControl("ItemSelect");
            if (cb.Checked)
            {
                EEmpPersonalInfo o = (EEmpPersonalInfo)EEmpPersonalInfo.db.createObject();
                WebFormUtils.GetKeys(EEmpPersonalInfo.db, o, cb);
                empList.Add(o);
            }
        }


        string   strEmpList          = string.Empty;
        string   strPayBatchList     = string.Empty;
        string   strPayPeriodRequest = string.Empty;
        DateTime dtPayPeriodFr       = new DateTime();
        DateTime dtPayPeriodTo       = new DateTime();

        if (empList.Count > 0)
        {
            foreach (EEmpPersonalInfo o in empList)
            {
                if (strEmpList == string.Empty)
                {
                    strEmpList = ((EEmpPersonalInfo)o).EmpID.ToString();
                }
                else
                {
                    strEmpList += "_" + ((EEmpPersonalInfo)o).EmpID.ToString();
                }
            }
            if (DateTime.TryParse(PayPeriodFr.Value, out dtPayPeriodFr) && DateTime.TryParse(PayPeriodTo.Value, out dtPayPeriodTo))
            {
                strPayPeriodRequest = "&PayPeriodFr=" + dtPayPeriodFr.Ticks + "&PayPeriodTo=" + dtPayPeriodTo.Ticks;
            }
            else
            {
                errors.addError("Invalid Date Format");
            }


            //            errors.addError("Complete");
            //Response.Write("<script>alert('Completed'); </script>");
        }
        else
        {
            errors.addError("Employee or Payroll Batch not selected");
        }

        if (errors.isEmpty())
        {
            GenericMPFFile mpfFileProcess = getMPFFileObject();
            if (mpfFileProcess == null)
            {
                errors.addError("Generate MPF File is not available for that MPF Scheme");
                return;
            }
            if (mpfFileProcess is HSBCMPFGatewayFileEncrypted)
            {
                string keyFolderPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BankKey");

                string keyPath             = string.Empty;
                string HSBCKeyFileName     = "HSBC.pub";
                string HangSengKeyFileName = "HASE.pub";
                string password            = "******";

                int intMPFPlanID = 0;

                if (int.TryParse(MPFPlanID.SelectedValue, out intMPFPlanID))
                {
                    EMPFPlan mpfPlan = new EMPFPlan();
                    mpfPlan.MPFPlanID = intMPFPlanID;
                    if (EMPFPlan.db.select(dbConn, mpfPlan))
                    {
                        EMPFScheme mpfScheme = new EMPFScheme();
                        mpfScheme.MPFSchemeID = mpfPlan.MPFSchemeID;
                        if (EMPFScheme.db.select(dbConn, mpfScheme))
                        {
                            if (mpfScheme.MPFSchemeTrusteeCode.Equals("HSBC"))
                            {
                                keyPath = System.IO.Path.Combine(keyFolderPath, HSBCKeyFileName);
                                if (Application["MasterDBConfig"] != null)
                                {
                                    DatabaseConnection masterDBConn = ((DatabaseConfig)Application["MasterDBConfig"]).CreateDatabaseConnectionObject();
                                    string             HSBCKeyPath  = HROne.SaaS.Entities.ESystemParameter.getParameter(masterDBConn, HROne.SaaS.Entities.ESystemParameter.PARAM_CODE_BANKKEY_HSBC_PATH);
                                    string             HSBCPassword = HROne.SaaS.Entities.ESystemParameter.getParameterWithEncryption(masterDBConn, HROne.SaaS.Entities.ESystemParameter.PARAM_CODE_BANKKEY_HSBC_PASSWORD);
                                    if (!string.IsNullOrEmpty(HSBCKeyPath) && !string.IsNullOrEmpty(HSBCPassword))
                                    {
                                        keyPath  = HSBCKeyPath;
                                        password = HSBCPassword;
                                    }
                                }
                            }
                            else if (mpfScheme.MPFSchemeTrusteeCode.Equals("HangSeng"))
                            {
                                keyPath = System.IO.Path.Combine(keyFolderPath, HangSengKeyFileName);
                                if (Application["MasterDBConfig"] != null)
                                {
                                    DatabaseConnection masterDBConn = ((DatabaseConfig)Application["MasterDBConfig"]).CreateDatabaseConnectionObject();
                                    string             HASEKeyPath  = HROne.SaaS.Entities.ESystemParameter.getParameter(masterDBConn, HROne.SaaS.Entities.ESystemParameter.PARAM_CODE_BANKKEY_HASE_PATH);
                                    string             HASEPassword = HROne.SaaS.Entities.ESystemParameter.getParameterWithEncryption(masterDBConn, HROne.SaaS.Entities.ESystemParameter.PARAM_CODE_BANKKEY_HASE_PASSWORD);
                                    if (!string.IsNullOrEmpty(HASEKeyPath) && !string.IsNullOrEmpty(HASEPassword))
                                    {
                                        keyPath  = HASEKeyPath;
                                        password = HASEPassword;
                                    }
                                }
                            }
                        }
                    }
                }

                HSBCMPFGatewayFileEncrypted mpfFileEncrypted = (HSBCMPFGatewayFileEncrypted)mpfFileProcess;

                mpfFileEncrypted.publicKeyFile     = keyPath;
                mpfFileEncrypted.publicKeyPassword = password;
            }

            HROneConfig config = HROneConfig.GetCurrentHROneConfig();
            if (config.GenerateReportAsInbox)
            {
                if (EInboxAttachment.GetTotalSize(dbConn, 0) < WebUtils.productLicense(Session).MaxInboxSizeMB * 1000 * 1000)
                {
                    HROne.TaskService.GenerateMPFContributionFileTaskFactory reportTask = new HROne.TaskService.GenerateMPFContributionFileTaskFactory(dbConn, user, lblReportHeader.Text, mpfFileProcess, empList, int.Parse(MPFPlanID.SelectedValue), dtPayPeriodFr, dtPayPeriodTo, ci);
                    AppUtils.reportTaskQueueService.AddTask(reportTask);
                    errors.addError(HROne.Translation.PageMessage.REPORT_GENERATING_TO_INBOX);
                }
                else
                {
                    errors.addError(HROne.Translation.PageMessage.INBOX_SIZE_EXCEEDED);
                }
            }
            else
            {
                try
                {
                    mpfFileProcess.LoadMPFFileDetail(empList, int.Parse(MPFPlanID.SelectedValue), dtPayPeriodFr, dtPayPeriodTo);
                    FileInfo mpfFileInfo = mpfFileProcess.GenerateMPFFile();

                    string mpfFilename = mpfFileProcess.ActualMPFFileName();
                    WebUtils.TransmitFile(Response, mpfFileInfo.FullName, mpfFilename, true);
                }
                catch (Exception ex)
                {
                    errors.addError(ex.Message);
                }
            }
        }
//        emploadData(empInfo, EEmpPayroll.db, empRepeater);
    }