public static string encodePrintOptions(TerminalParameters HicapsParams, string strHexin)
        {
            // Receipt Prompting parameters.
            // Documentation says MY Field been added Bit 0-1 Print Merchant Copy, Bit 1-1 Print Customer Copy
            // Big 2-1 Pronpt before printing customer copy.
            //
            // Jp note.... Sample PMS.exe code does not seem to use this logic... in the end I've just
            // added code for all possibilities copying what the PMS.exe sends to the terminal.
            // ?
            // Only valid for pmskey 6730429
            if (HicapsParams.PmsKey != "6730429")
            {
                return(strHexin);
            }
            bool fieldAdded = false;

            if ((!HicapsParams.PrintCustomerReceipt) && (!HicapsParams.PrintMerchantReceipt))
            {
                strHexin   = addSubHexStringField(encodeAlpha("MY", "00"), strHexin);
                fieldAdded = true;
            }
            if ((!HicapsParams.PrintCustomerReceipt) && (HicapsParams.PrintMerchantReceipt))
            {
                strHexin   = addSubHexStringField(encodeAlpha("MY", "80"), strHexin);
                fieldAdded = true;
            }
            if (HicapsParams.PrintCustomerReceipt && !fieldAdded)
            {
                if (HicapsParams.PrintMerchantReceipt && HicapsParams.PrintCustomerReceiptPrompt)
                {
                    strHexin   = addSubHexStringField(encodeAlpha("MY", "E0"), strHexin);
                    fieldAdded = true;
                }
                if (!HicapsParams.PrintMerchantReceipt && !HicapsParams.PrintCustomerReceiptPrompt)
                {
                    strHexin   = addSubHexStringField(encodeAlpha("MY", "40"), strHexin);
                    fieldAdded = true;
                }
                if (!HicapsParams.PrintMerchantReceipt && HicapsParams.PrintCustomerReceiptPrompt)
                {
                    strHexin   = addSubHexStringField(encodeAlpha("MY", "60"), strHexin);
                    fieldAdded = true;
                }
                if (HicapsParams.PrintMerchantReceipt && !HicapsParams.PrintCustomerReceiptPrompt)
                {
                    strHexin   = addSubHexStringField(encodeAlpha("MY", "C0"), strHexin);
                    fieldAdded = true;
                }
                if (!fieldAdded)
                {
                    strHexin   = addSubHexStringField(encodeAlpha("MY", "80"), strHexin);
                    fieldAdded = true;
                }
                //
            }
            return(strHexin);
        }
        public static string encodeM1Field(TerminalParameters HicapsParams)
        {
            string m1Field    = "";
            string m1SubField = "";
            //TODO extract details out of             HicapsParams.ClaimData

            //  int linecount = 0;
            int m1FieldLen = 0;

            // Each Sub Claim Line
            for (int i = 0; i < HicapsParams.ClaimData.Length; i++)
            {
                //        6          6          8                  2            6           5            1                       6
                //data = itemNum + feeamount + dateofservice + itemOverrideCde + lspNum + equipmentId + selfDeemedCde + contribPatientAmountStr;
                //00002301000021072009        000000
                //00002301000021072009              000000
                string data                 = HicapsParams.ClaimData[i];
                string itemNumber           = data.Substring(0, 6).Trim();   //= "12345";
                string chargeAmount         = data.Substring(6, 6).Trim();;  //= "0000060";
                string dateofservice        = data.Substring(12, 8).Trim();; //= "01072009";
                string itemOverrideCode     = data.Substring(20, 2).Trim();  //= "AP"; //"AP"
                string lspn                 = data.Substring(22, 6).Trim();  //= "123456"; //123456
                string equipmentId          = data.Substring(28, 5).Trim();  //= "12345"; //12345
                string selfDeemedCde        = data.Substring(33, 2).Trim();  //= "Y";  //Y
                string contribPatientAmount = data.Substring(35, 6).Trim();  //= "000000"; // "0000000";
                string spcid                = data.Substring(41, 4).Trim();  // = "    "

                m1FieldLen = 0;
                m1Field    = "";
                // D1 = Charge Amount
                if (!string.IsNullOrEmpty(chargeAmount) && chargeAmount != "000000")
                {
                    // can only be size 7
                    int    amount   = Convert.ToInt32(chargeAmount);
                    string hexField = Right("000000" + amount.ToString("X"), 6);
                    m1Field     = m1Field + "D1 03 " + hexField.Substring(0, 2) + " " + hexField.Substring(2, 2) + " " + hexField.Substring(4, 2) + " ";
                    m1FieldLen += 3;
                }
                // D9 = Date of Service
                if (!string.IsNullOrEmpty(dateofservice))
                {
                    // can only be size 8
                    m1Field     = m1Field + "D9 04 " + dateofservice.Substring(0, 2) + " " + dateofservice.Substring(2, 2) + " " + dateofservice.Substring(4, 2) + " " + dateofservice.Substring(6, 2) + " ";
                    m1FieldLen += 4;
                }
                // D2 = ItemNumber AlphsNumeric
                if (!string.IsNullOrEmpty(itemNumber))
                {
                    // Can only be size 6
                    m1Field     = m1Field + encodeMedicareAlpha("D2", itemNumber) + " ";
                    m1FieldLen += itemNumber.Length;
                }
                // D3 = Item OVerride Code ?
                if (!string.IsNullOrEmpty(itemOverrideCode))
                {
                    // itemOverrideCode can only be 2.
                    m1Field     = m1Field + encodeMedicareAlpha("D3", itemOverrideCode) + " ";
                    m1FieldLen += itemOverrideCode.Length;
                }
                // D4 = Patient Contributon amount
                if (!string.IsNullOrEmpty(contribPatientAmount) && contribPatientAmount != "000000")
                {
                    // Can only be most size 7, 9999999
                    int    contribAmount = Convert.ToInt32(contribPatientAmount);
                    string hexField2     = Right("000000" + contribAmount.ToString("X"), 6);
                    m1Field     = m1Field + "D4 03 " + hexField2.Substring(0, 2) + " " + hexField2.Substring(2, 2) + " " + hexField2.Substring(4, 2) + " ";
                    m1FieldLen += 3;
                }

                // D8 = LSPN
                if (!string.IsNullOrEmpty(lspn))
                {
                    // can only be len 6
                    lspn        = Right("000000" + lspn, 6);
                    m1Field     = m1Field + "D8 03 " + lspn.Substring(0, 2) + " " + lspn.Substring(2, 2) + " " + lspn.Substring(4, 2) + " ";
                    m1FieldLen += 3;
                }

                // DA selfDeemedCde Id
                if (!string.IsNullOrEmpty(selfDeemedCde))
                {
                    // can only be len 2
                    m1Field     = m1Field + encodeMedicareAlpha("DA", selfDeemedCde) + " ";
                    m1FieldLen += selfDeemedCde.Length;
                }

                // DB Equipment Id
                if (!string.IsNullOrEmpty(equipmentId))
                {
                    // Can only be len 5
                    m1Field     = m1Field + encodeMedicareAlpha("DB", equipmentId) + " ";
                    m1FieldLen += equipmentId.Length;
                }
                // DC spcid
                if (!string.IsNullOrEmpty(spcid))
                {
                    // Can only be len 4
                    m1Field     = m1Field + encodeMedicareAlpha("DC", spcid) + " ";
                    m1FieldLen += equipmentId.Length;
                }
                m1Field = m1Field.Trim();
                // fix length
                m1FieldLen = m1Field.Replace(" ", "").Length / 2;
                if (HicapsParams.ClaimType != 4)
                {
                    m1Field = "DF 16 " + m1FieldLen.ToString("X2") + " " + m1Field;
                }
                else
                {
                    // Bulk Bill
                    m1Field = "DF 45 " + m1FieldLen.ToString("X2") + " " + m1Field;
                }
                m1SubField += m1Field + " ";

                // End of repeat line
            }
            m1Field = m1SubField.Trim();
            int    fieldlen  = m1Field.Replace(" ", "").Length / 2;
            string fieldsize = fieldlen.ToString("D4");

            m1Field = "4D 31 " + fieldsize.Substring(0, 2) + " " + fieldsize.Substring(2, 2) + " " + m1Field + " 1C";
            return(m1Field);
        }
        public static string encodeM0Field(TerminalParameters HicapsParams)
        {
            string m0Field    = "";
            int    sizm0Field = 0;

            //DF 03 09 30 30 30 30 30 30 30 30 30
            if (!string.IsNullOrEmpty(HicapsParams.AccountReferenceId))
            {
                m0Field     = m0Field + "DF " + encodeMedicareAlpha("03", HicapsParams.AccountReferenceId) + " ";
                sizm0Field += HicapsParams.AccountReferenceId.Length;
            }
            //DF 04 01 4E
            if (HicapsParams.ClaimType != 4)
            {
                if (HicapsParams.AccountPaidInd == "N")
                {
                    m0Field = m0Field + "DF 04 01 4E "; sizm0Field += 1;
                }
                else
                {
                    m0Field = m0Field + "DF 04 01 59 "; sizm0Field += 1;
                }
            }
            // Do not send down claimant details for Bulk Bill
            if (HicapsParams.ClaimType != 4)
            {
                //DF 05 10 12 34 56 78 90 12 34 56 78 90 ClaimantMedicareCardNum
                if (!string.IsNullOrEmpty(HicapsParams.ClaimantMedicareCardNum))
                {
                    string field = HicapsParams.ClaimantMedicareCardNum;
                    m0Field = m0Field + "DF 05 05 " + field.Substring(0, 2) + " "
                              + field.Substring(2, 2) + " "
                              + field.Substring(4, 2) + " "
                              + field.Substring(6, 2) + " "
                              + field.Substring(8, 2) + " ";
                    sizm0Field += 5;
                }
                // DF 06 01 31 ClaimantIRN
                if (!string.IsNullOrEmpty(HicapsParams.ClaimantIRN))
                {
                    m0Field     = m0Field + "DF " + encodeMedicareAlpha("06", HicapsParams.ClaimantIRN) + " ";
                    sizm0Field += HicapsParams.ClaimantIRN.Length;
                }
            }
            //DF 07 10 12 34 56 78 90 12 34 56 78 90 PatientMedicareCardNum
            if (!string.IsNullOrEmpty(HicapsParams.PatientMedicareCardNum))
            {
                string field = HicapsParams.PatientMedicareCardNum;
                m0Field = m0Field + "DF 07 05 " + field.Substring(0, 2) + " "
                          + field.Substring(2, 2) + " "
                          + field.Substring(4, 2) + " "
                          + field.Substring(6, 2) + " "
                          + field.Substring(8, 2) + " ";
                sizm0Field += 5;
            }
            // DF 08 01 31 PatientIRN
            if (!string.IsNullOrEmpty(HicapsParams.PatientIRN))
            {
                m0Field     = m0Field + "DF " + encodeMedicareAlpha("08", HicapsParams.PatientIRN) + " ";
                sizm0Field += HicapsParams.PatientIRN.Length;
            }
            // DF 41 CEV Request indicator
            if (!string.IsNullOrEmpty(HicapsParams.CevRequestInd))
            {
                HicapsParams.CevRequestInd = HicapsParams.CevRequestInd.ToUpper();
                m0Field     = m0Field + "DF " + encodeMedicareAlpha("41", HicapsParams.CevRequestInd) + " ";
                sizm0Field += HicapsParams.CevRequestInd.Length;
            }

            // DF 42 08 30 31 32 33 34 35 36 37  PayeeProviderNum
            //
            if (!string.IsNullOrEmpty(HicapsParams.PayeeProviderNum))
            {
                m0Field     = m0Field + "DF " + encodeMedicareAlpha("42", HicapsParams.PayeeProviderNum) + " ";
                sizm0Field += HicapsParams.PayeeProviderNum.Length;
            }
            // DF 0A 08 30 31 32 33 34 35 36 37 ServicingProviderNum
            //
            if (!string.IsNullOrEmpty(HicapsParams.ServicingProviderNum))
            {
                m0Field     = m0Field + "DF " + encodeMedicareAlpha("0A", HicapsParams.ServicingProviderNum) + " ";
                sizm0Field += HicapsParams.ServicingProviderNum.Length;
            }
            // This is correct
            sizm0Field = m0Field.Replace(" ", "").Length / 2;
            // DF 0C  ?? PseudoProviderNum
            #region Second Level Data Elementa
            //Second Level Data Elements
            string secondLevelm0Field = "";

            //DF 0D 04 01 12 20 09
            if (HicapsParams.ReferralIssueDate != null && HicapsParams.ReferralIssueDate > new DateTime(2000, 1, 1))
            {
                string dateTimeField = HicapsParams.ReferralIssueDate.Day.ToString("D2") + " " + HicapsParams.ReferralIssueDate.Month.ToString("D2") + " " + HicapsParams.ReferralIssueDate.Year.ToString().Substring(0, 2) + " " + HicapsParams.ReferralIssueDate.Year.ToString().Substring(2, 2);
                secondLevelm0Field = secondLevelm0Field + "DF 0D 04 " + dateTimeField + " ";
            }
            // DF 0E 01 53
            if (!string.IsNullOrEmpty(HicapsParams.ReferralPeriodTypeCde))
            {
                secondLevelm0Field = secondLevelm0Field + "DF " + encodeMedicareAlpha("0E", HicapsParams.ReferralPeriodTypeCde.Substring(0, 1)) + " ";
            }
            // DF 0F 01 53
            if (!string.IsNullOrEmpty(HicapsParams.ReferralOverrideTypeCde))
            {
                secondLevelm0Field = secondLevelm0Field + "DF " + encodeMedicareAlpha("0F", HicapsParams.ReferralOverrideTypeCde.Substring(0, 1)) + " ";
            }
            // DF 10 08 32 31 30 32 31 32 32 31
            if (!string.IsNullOrEmpty(HicapsParams.ReferringProviderNum))
            {
                secondLevelm0Field = secondLevelm0Field + "DF " + encodeMedicareAlpha("10", HicapsParams.ReferringProviderNum) + " ";
            }
            #region Requesting part
            //DF 11 04 01 12 20 09
            if (HicapsParams.RequestIssueDate != null && HicapsParams.RequestIssueDate > new DateTime(2000, 1, 1))
            {
                string dateTimeField = HicapsParams.RequestIssueDate.Day.ToString("D2") + " " + HicapsParams.RequestIssueDate.Month.ToString("D2") + " " + HicapsParams.RequestIssueDate.Year.ToString().Substring(0, 2) + " " + HicapsParams.RequestIssueDate.Year.ToString().Substring(2, 2);
                secondLevelm0Field = secondLevelm0Field + "DF 11 04 " + dateTimeField + " ";
            }
            // DF 12 01 53
            if (!string.IsNullOrEmpty(HicapsParams.RequestOverrideTypeCde) && string.IsNullOrEmpty(HicapsParams.RequestingProviderNum))
            {
                secondLevelm0Field = secondLevelm0Field + "DF " + encodeMedicareAlpha("12", HicapsParams.RequestOverrideTypeCde.Substring(0, 1)) + " ";
            }
            // DF 13 01 53
            if (!string.IsNullOrEmpty(HicapsParams.RequestTypeCde) && !string.IsNullOrEmpty(HicapsParams.RequestingProviderNum))
            {
                secondLevelm0Field = secondLevelm0Field + "DF " + encodeMedicareAlpha("13", HicapsParams.RequestTypeCde.Substring(0, 1)) + " ";
            }
            // DF 14 08 32 31 30 32 31 32 32 31
            if (!string.IsNullOrEmpty(HicapsParams.RequestingProviderNum))
            {
                secondLevelm0Field = secondLevelm0Field + "DF " + encodeMedicareAlpha("14", HicapsParams.RequestingProviderNum) + " ";
            }
            #endregion
            // DF 15 01 4F ServiceType Cde
            // ServiceTypeCde not valid for Bulk Bill
            if (HicapsParams.ClaimType != 4)
            {
                if (!string.IsNullOrEmpty(HicapsParams.ServiceTypeCde))
                {
                    //  secondLevelm0Field = secondLevelm0Field + "DF 15 01 " + HicapsParams.ServiceTypeCde.Substring(0, 1) + " ";
                    secondLevelm0Field = secondLevelm0Field + "DF " + encodeMedicareAlpha("15", HicapsParams.ServiceTypeCde.Substring(0, 1)) + " ";
                }
            }
            // Add DF 0B to start of secondLevel Data Elemetnts
            string secondFieldSize = (secondLevelm0Field.Replace(" ", "").Length / 2).ToString("X2");
            if (secondFieldSize != "00")
            {
                if (HicapsParams.ClaimType != 4)
                {
                    // Patient Claim using DF 0B
                    m0Field = m0Field + "DF 0B " + secondFieldSize + " " + secondLevelm0Field;
                }
                else
                {
                    // Bulk Bill using DF 43
                    m0Field = m0Field + "DF 43 " + secondFieldSize + " " + secondLevelm0Field;
                }
            }
            #endregion
            // Add DF02/DF40 to start.
            // Get length of fields.
            if (HicapsParams.ClaimType != 4)
            {
                m0Field = m0Field.Trim();
                m0Field = "DF 02 " + sizm0Field.ToString("X2") + " " + m0Field;
            }
            else
            {
                // Bulk Bull using DF 40 field
                m0Field = m0Field.Trim();
                m0Field = "DF 40 " + sizm0Field.ToString("X2") + " " + m0Field;
            }
            //encryptSubField(HicapsParams, ref m0Field);
            int    fieldlen  = m0Field.Replace(" ", "").Length / 2;
            string fieldsize = fieldlen.ToString("D4");

            m0Field = "4D 30 " + fieldsize.Substring(0, 2) + " " + fieldsize.Substring(2, 2) + " " + m0Field + " 1C";
            return(m0Field);
            // throw new NotImplementedException();
        }