private TransactionDTO PlaceBetAllowMaxBet(bool sendOrNOT, MatchDTO ibetMatch, MatchDTO sbobetMatch, eOddType oddType, string ibetOddID, string sbobetOddID, string ibetOddType, string sbobetOddType, string ibetOddValue, string sbobetOddValue, int ibetStake, int sbobetStake, bool ibetHomeGive, IBetEngine ibetEngine, SbobetEngine sbobetEngine, string sbobetOdd)
        {
            this._betting = true;
            TransactionDTO transactionDTO = new TransactionDTO();
            
            //transactionDTO.OddType = current2.Type.ToString() + " - Home / Away";
            string text = "";
            string text2 = "";
            string text3 = "";
            bool flag = false;
            bool flag2 = false;
            int num = 0;
            int num2 = 0;
            string text4 = "";
            string text5 = "";
            string text6 = "";
            bool flag3 = false;
            bool flag4 = false;
            bool sBOBETReTrade = false;
            int num3 = 0;
            string text7 = "";
            string betCount = "";
            string text8 = "";
            bool flag5 = false;

            float ibetOddValue_ = float.Parse(ibetOddValue);
            float sboOddValue_ = float.Parse(sbobetOddValue);

            if (ibetOddValue_ > 0.84 || (ibetOddValue_ > 0f && txtAddValue.Value.ToString() == "0.01"))
            {
                float valueAdd = ibetStake * (float)txtAddValue.Value;
                ibetStake = ibetStake + (int)valueAdd;
            }
            if (sboOddValue_ > 0.84 || (sboOddValue_ > 0f && txtAddValue.Value.ToString() == "0.01"))
            {
                float valueAdd = sbobetStake * (float)txtAddValue.Value;
                sbobetStake = sbobetStake + (int)valueAdd;
            }

            bool ibetGoFirst = true;// who is Away or Under

            if (checkEdit11.Checked) //safe bet
            {
                if (sbobetOddType == "a") //  ibet  vs sbo
                {
                    if (oddType == eOddType.FirstHalfOverUnder || oddType == eOddType.FulltimeOverUnder)//neu bong over under
                    {
                        ibetGoFirst = false;
                    }
                    else // bong handicap
                    {
                        if (ibetHomeGive) // neu away la doi cua duoi
                        {
                            ibetGoFirst = false;
                        }
                    }
                }
                else //  sbo vs ibet
                {
                    if (oddType == eOddType.FirstHalfOverUnder || oddType == eOddType.FulltimeOverUnder)//neu bong over under
                    {
                        ibetGoFirst = true;
                    }
                    else // bong handicap
                    {
                        if (ibetHomeGive) // neu away la doi cua duoi
                        {
                            ibetGoFirst = true;
                        }
                    }
                }
            }

            if (MatchDTO.IsSameMatch(ibetMatch.HomeTeamName.ToLower(), sbobetMatch.HomeTeamName.ToLower(), ibetMatch.AwayTeamName.ToLower(), sbobetMatch.AwayTeamName.ToLower()))
            {
                try
                {
                    ibetEngine.PrepareBet(ibetOddID, ibetOddType, ibetOddValue, ibetStake.ToString(), out flag, out num, out num2, out text4, out text2, out text3);
                    sbobetEngine.PrepareBet(sbobetOddID, sbobetOddValue, sbobetOddType, out betCount, out num3, out flag3, out text7, out text5, out text6);
                    //object result = this.CallJavascriptFunction("bet", new object[] { 0, ibetMatch.ID.Remove(0, 2), ibetOddID, ibetOddType, ibetOddValue });
                    
                    if (MatchDTO.IsSameMatch(text2, text5, text3, text6)) // ten hom - away giong nhau
                    {                        
                        float num4 = 0f;
                        float num5 = 0f;
                        if (flag && flag3 && float.TryParse(text4, out num4) && float.TryParse(text7, out num5))
                        {
                            if ((checkEdit13.Checked || checkEdit15.Checked) && sendOrNOT)//gui lenh
                            {
                                var taskA = new Task(() => this.AddOddToLocalCommunity(ibetMatch, sbobetMatch, oddType, sbobetOdd, sbobetOdd, ibetOddType, sbobetOddType, ibetOddValue, sbobetOddValue, ibetHomeGive));
                                taskA.Start();
                            }

                            //try parse de tranh hien tuong mat Odd 1 ben
                            //float num4 = float.Parse(text4); 
                            //float num5 = float.Parse(text7);
                            if (num4 + num5 == 0f || num4 == num5)
                            {
                                int num6 = 0;
                                if (num2 >= num3) // num2 maxbet ibet
                                {
                                    num6 = num3; // num3 maxbet sbo
                                }
                                else
                                {
                                    if (num2 <= num3)
                                    {
                                        num6 = num2;
                                    }
                                }
                                if (ibetStake >= num6)
                                {
                                    ibetStake = num6;
                                }
                                if (sbobetStake >= num6)
                                {
                                    sbobetStake = num6;
                                }
                                try
                                {
                                    float currentCredit = 0;
                                    float currentCredit2 = 0;
                                    if (checkEdit8.Checked)
                                    {
                                        currentCredit = ibetEngine.GetCurrentCredit();
                                        currentCredit2 = sbobetEngine.GetCurrentCredit();
                                    }
                                    else
                                    {
                                        currentCredit = ibetEngine._currentCredit;
                                        currentCredit2 = sbobetEngine._currentCredit;
                                    }

                                    if ((float)ibetStake <= currentCredit && (float)sbobetStake <= currentCredit2)
                                    {
                                        try
                                        {
                                            if (ibetGoFirst)
                                            {
                                                
#if TESTMODE
                                                #region IBET_FIRTST_THEN_SBO
                                                if (!checkEdit12.Checked || (checkEdit12.Checked && ibetOddType == "a" && (oddType == eOddType.FirstHalfOverUnder || oddType == eOddType.FulltimeOverUnder)))
                                                    ibetEngine.ConfirmBet(oddType, ibetOddValue, ibetStake.ToString(), num.ToString(), num2.ToString(), out flag2);
                                                if (flag2 && !checkEdit12.Checked)
                                                {
                                                    sbobetEngine.ConfirmBet(sbobetOddID, sbobetOddValue, sbobetOddType, sbobetStake.ToString(), betCount, out text8, out flag4);
                                                    if (!flag4)
                                                    {
                                                        try
                                                        {
                                                            sbobetEngine.PrepareBet(sbobetOddID, text8, sbobetOddType, out betCount, out num3, out flag5, out text7, out text5, out text6);
                                                            if (MatchDTO.IsSameMatch(text2, text5, text3, text6))
                                                            {
                                                                if (!float.TryParse(text7, out num5)) //text 7 = BetKindValue
                                                                {
                                                                    Thread.Sleep(3000);
                                                                    sbobetEngine.PrepareBet(sbobetOddID, text8, sbobetOddType, out betCount, out num3, out flag5, out text7, out text5, out text6);
                                                                    float.TryParse(text7, out num5);
                                                                }

                                                                if (num4 + num5 == 0f || num4 == num5)
                                                                {
                                                                    text = "Retrade. IBET: " + ibetOddValue + " -  SBOBET: " + text8;
                                                                    //text = "No retrade in this version please trade manually.";
                                                                    sbobetEngine.ConfirmBet(sbobetOddID, text8, sbobetOddType, sbobetStake.ToString(), betCount, out text8, out sBOBETReTrade);
                                                                    this._lastTransactionTime = System.DateTime.Now;
                                                                    playSound(false);
                                                                    
                                                                }
                                                                else
                                                                {
                                                                    text = "Invalid Odd in SBOBET. IBET Odd: " + text4 + " - SBOBET Odd:" + text7;
                                                                    playSound(false);
                                                                }
                                                            }
                                                            else
                                                            {
                                                                text = string.Concat(new string[]
															    {
																    "Not Same Match - Retrade. IBET: ", 
																    text2, 
																    " / ", 
																    text3, 
																    " - SBOBET: ", 
																    text5, 
																    " / ", 
																    text6
															    });
                                                            }
                                                        }
                                                        catch (System.Exception ex)
                                                        {
                                                            text = "Error while Retrade. Details: " + ex.Message;
                                                            playSound(false);
                                                        }
                                                    }
                                                    else
                                                    {
                                                        playSound(true);
                                                    }

                                                    this._lastTransactionTime = System.DateTime.Now;
                                                    object obj = text;
                                                    text = string.Concat(new object[]
												    {
													    obj, 
													    " - Success Transaction. Half: ", 
													    ibetMatch.Half, 
													    " - Minute: ", 
													    ibetMatch.Minute, 
													    " - Halftime: ", 
													    ibetMatch.IsHalfTime
												    });
                                                    ibetEngine.GetCurrentCredit();
                                                    sbobetEngine.GetCurrentCredit();

                                                }
                                                
                                                #endregion
#endif
                                            }

                                            else
                                            {
                                                text = "No more SBOBET comes first.";
                                                //#region SBO_BET_FIRST_THEN_IBET

                                                //sbobetEngine.ConfirmBet(sbobetOddID, sbobetOddValue, sbobetOddType, sbobetStake.ToString(), betCount, out text8, out flag4);
                                                //if (flag4)
                                                //{ 
                                                //    ibetEngine.ConfirmBet(oddType, ibetOddValue, ibetStake.ToString(), num.ToString(), num2.ToString(), out flag2);
                                                //    if (!flag2)
                                                //    {
                                                //        text = "Invalid Odd while Retrade.";// IBET Odd: " + text4 + " - SBOBET Odd:" + text7;
                                                //        sbobetEngine.GetCurrentCredit();
                                                //        playSound(false);
                                                //    }
                                                //    else
                                                //    {
                                                //        this._lastTransactionTime = System.DateTime.Now;
                                                //        object obj = text;
                                                //        text = string.Concat(new object[]
                                                //        {
                                                //            obj, 
                                                //            " - Success Transaction. Half: ", 
                                                //            ibetMatch.Half, 
                                                //            " - Minute: ", 
                                                //            ibetMatch.Minute, 
                                                //            " - Halftime: ", 
                                                //            ibetMatch.IsHalfTime
                                                //        });
                                                //        ibetEngine.GetCurrentCredit();
                                                //        sbobetEngine.GetCurrentCredit();
                                                //        playSound(true);
                                                //    }
                                                //}

                                                //#endregion
                                            }

                                        }
                                        catch (System.Exception ex)
                                        {
                                            text = "Error while Trading. Details: " + ex.Message;
                                        }
                                    }
                                    else
                                    {
                                        text = string.Concat(new object[]
										{
											"Out of Cash. IBET Credit: ", 
											currentCredit, 
											" -  SBOBET Credit: ", 
											currentCredit2
										});
                                    }
                                }
                                catch (System.Exception ex)
                                {
                                    text = "Error while getting Credit. Details: " + ex.Message;
                                }
                            }
                            else
                            {
                                text = "Invalid Odd while Preparing Ticket. IBET Odd: " + text4 + " - SBOBET Odd:" + text7;
                            }
                        }
                    }                        
                    else
                    {
                        text = string.Concat(new string[]
						{
							"Not Same Match - Preparing Ticket. IBET: ", 
							text2, 
							" / ", 
							text3, 
							" - SBOBET: ", 
							text5, 
							" / ", 
							text6
						});
                    }                    
                }
                catch (System.Exception ex)
                {
                    text = "Error while Preparing Trade. Details: " + ex.Message;
                }
            }
            else
            {
                text = string.Concat(new string[]
				{
					"Not Same Match - Comparing. IBET: ", 
					ibetMatch.HomeTeamName, 
					" / ", 
					ibetMatch.AwayTeamName, 
					" - SBOBET: ", 
					sbobetMatch.HomeTeamName, 
					" / ", 
					sbobetMatch.AwayTeamName
				});
            }
            transactionDTO.HomeTeamName = ibetMatch.HomeTeamName + ":" + ibetMatch.HomeScore + " / " + sbobetMatch.HomeTeamName;
            transactionDTO.AwayTeamName = ibetMatch.AwayTeamName + ":" + ibetMatch.AwayScore + " / " + sbobetMatch.AwayTeamName;
            transactionDTO.Odd = text4 + " / " + text7;
            transactionDTO.OddKindValue = text4 + " / " + text7;
            transactionDTO.OddValue = ibetOddValue + " / " + sbobetOddValue;
            transactionDTO.Stake = ibetStake + " / " + sbobetStake;
            transactionDTO.IBETAllow = flag;
            transactionDTO.IBETTrade = flag2;
            transactionDTO.IBETReTrade = false;
            transactionDTO.SBOBETAllow = flag3;
            transactionDTO.SBOBETTrade = flag4;
            transactionDTO.SBOBETReTrade = sBOBETReTrade;
            //transactionDTO.OddType = oddType.ToString() + ;
            transactionDTO.OddType = oddType.ToString() + " - " + ibetOddType.Replace("h", "Home").Replace("a", "Away") + " / " + sbobetOddType.Replace("h", "Home").Replace("a", "Away");
            transactionDTO.Note = text;
            transactionDTO.DateTime = System.DateTime.Now.ToLocalTime();

            transactionDTO.HomeTeamSBOBET = sbobetMatch.HomeTeamName;
            transactionDTO.AwayTeamSBOBET = sbobetMatch.AwayTeamName;
            
            this._betting = false;
            return transactionDTO;            
        }        
		private TransactionDTO PlaceBet(
            MatchDTO ibetMatch,
            MatchDTO threein1betMatch,
            eOddType oddType,
            string ibetOddID,
            string ibetOddType,
            string ibetOddValue,
            OddDTO threein1Odd,
            string threein1OddType,
            int ibetStake,
            int threein1betStake,
            IBetEngine ibetEngine,
            ThreeIn1BetEngine threein1betEngine)
		{
			TransactionDTO transactionDTO = new TransactionDTO();
            string text = string.Empty;
            string text2 = string.Empty;
            string text3 = string.Empty;
			bool flag = false;
			bool flag2 = false;
			int num = 0;
			int num2 = 0;
            string text4 = string.Empty;
            string text5 = string.Empty;
            string text6 = string.Empty;
			bool flag3 = false;
			bool flag4 = false;
			bool sBOBETReTrade = false;
			int num3 = 0;
            
            string text7 = string.Empty;
            string betCount = string.Empty;
            
            string text8 = string.Empty;
			

            bool threein1betAllowance = false;
            int threein1betminStake = 0;
            int threein1betmaxStake = 0;
            string threein1betKindValue = string.Empty;
            string threein1betHomeTeamName = string.Empty;
            string threein1betAwayTeamName = string.Empty;
            string postBODY = string.Empty;  

            if (MatchDTO.IsSameMatch(ibetMatch.HomeTeamName.ToLower(), threein1betMatch.HomeTeamName.ToLower(), ibetMatch.AwayTeamName.ToLower(), threein1betMatch.AwayTeamName.ToLower()))
			{
				try
				{
					ibetEngine.PrepareBet(ibetOddID, ibetOddType, ibetOddValue, ibetStake.ToString(), out flag, out num, out num2, out text4, out text2, out text3);
                    threein1betEngine.PrepareBet(threein1Odd.ID, threein1Odd.Away.ToString(), threein1Odd.Type.ToString().Replace("FulltimeHandicap", "Hdp").Replace("", ""), "Away", threein1Odd.IsHomeGive.ToString().Replace("True", "1").Replace("False", "0"), threein1Odd.Odd, threein1betMatch.HomeScore, threein1betMatch.AwayScore, threein1betMatch.ID, threein1betMatch.IsHalfTime.ToString().Replace("True", "1").Replace("False", "0"), threein1betStake.ToString(),
                        out threein1betAllowance, out threein1betminStake, out threein1betmaxStake, out threein1betKindValue, out threein1betHomeTeamName, out threein1betAwayTeamName, out postBODY);
                    
                    if (MatchDTO.IsSameMatch(text2, text5, text3, text6))
					{
						if (flag && flag3)
						{
							float num4 = float.Parse(text4);
							float num5 = float.Parse(text7);
							if (num4 + num5 == 0f || num4 == num5)
							{
								if (ibetStake <= num2 && threein1betStake <= num3)
								{
									try
									{
										float currentCredit = ibetEngine.GetCurrentCredit();
                                        float currentCredit2 = threein1betEngine.GetCurrentCredit();
										if ((float)ibetStake <= currentCredit && (float)threein1betStake <= currentCredit2)
										{
											try
											{
												ibetEngine.ConfirmBet(oddType, ibetOddValue, ibetStake.ToString(), num.ToString(), num2.ToString(), out flag2);
												if (flag2)
												{
                                                    //threeIn1BetEngine.ConfirmBet(oddType,
                                                    //    threein1betOddValue,
                                                    //    threein1betStake.ToString(), 
                                                    //    betCount, 
                                                    //    text8, 
                                                    //    out flag4);
													if (!flag4)
													{
														try
														{
                                                            //
														}
														catch (System.Exception ex)
														{
															text = "Error while Retrade. Details: " + ex.Message;
														}
													}
													this._lastTransactionTime = System.DateTime.Now;
													object obj = text;
													text = string.Concat(new object[]
													{
														obj, 
														" - Success Transaction. Half: ", 
														ibetMatch.Half, 
														" - Minute: ", 
														ibetMatch.Minute, 
														" - Halftime: ", 
														ibetMatch.IsHalfTime
													});
												}
											}
											catch (System.Exception ex)
											{
												text = "Error while Trading. Details: " + ex.Message;
											}
										}
										else
										{
											text = string.Concat(new object[]
											{
												"Out of Cash. IBET Credit: ", 
												currentCredit, 
												" -  3in1BET Credit: ", 
												currentCredit2
											});
										}
									}
									catch (System.Exception ex)
									{
										text = "Error while getting Credit. Details: " + ex.Message;
									}
								}
								else
								{
									text = string.Concat(new object[]
									{
										"Max Bet. IBET: ", 
										num2, 
										" - 3in1BET: ", 
										num3
									});
								}
							}
							else
							{
								text = "Invalid Odd while Preparing Ticket. IBET Odd: " + text4 + " - 3in1BET Odd:" + text7;
							}
						}
					}
					else
					{
						text = string.Concat(new string[]
						{
							"Not Same Match - Preparing Ticket. IBET: ", 
							text2, 
							" / ", 
							text3, 
							" - 3in1BET: ", 
							text5, 
							" / ", 
							text6
						});
					}
				}
				catch (System.Exception ex)
				{
					text = "Error while Preparing Trade. Details: " + ex.Message;
				}
			}
			else
			{
				text = string.Concat(new string[]
				{
					"Not Same Match - Comparing. IBET: ", 
					ibetMatch.HomeTeamName, 
					" / ", 
					ibetMatch.AwayTeamName, 
					" - 3in1BET: ", 
					threein1betMatch.HomeTeamName, 
					" / ", 
					threein1betMatch.AwayTeamName
				});
			}
            transactionDTO.HomeTeamName = ibetMatch.HomeTeamName + " / " + threein1betMatch.HomeTeamName;
            transactionDTO.AwayTeamName = ibetMatch.AwayTeamName + " / " + threein1betMatch.AwayTeamName;
			transactionDTO.Odd = text4 + " / " + text7;
			transactionDTO.OddKindValue = text4 + " / " + text7;
			transactionDTO.OddValue = ibetOddValue + " / " + threein1Odd.Away;
			transactionDTO.Stake = ibetStake + " / " + threein1betStake;
			transactionDTO.IBETAllow = flag;
			transactionDTO.IBETTrade = flag2;
			transactionDTO.IBETReTrade = false;
			transactionDTO.THREEIN1Allow = flag3;
			transactionDTO.THREEIN1Trade = flag4;
			transactionDTO.THREEIN1ReTrade = sBOBETReTrade;
			transactionDTO.OddType = oddType.ToString();
			transactionDTO.Note = text;
			transactionDTO.DateTime = System.DateTime.Now;
			return transactionDTO;
		}
        private TransactionDTO PlaceBet(bool sendOrNOT, MatchDTO ibetMatch, MatchDTO sbobetMatch, eOddType oddType, string ibetOddID, string sbobetOddID, string ibetOddType, string sbobetOddType, string ibetOddValue, string sbobetOddValue, int ibetStake, int sbobetStake, IBetEngine ibetEngine, SbobetEngine sbobetEngine, string sbobetOdd, bool ibetHomeGive)
        {
            this._betting = true;
            TransactionDTO transactionDTO = new TransactionDTO();
            //transactionDTO.OddType = oddType.ToString() + " - " + ibetOddType.Replace("h", "Home").Replace("a", "Away") + " / " + sbobetOddType.Replace("h", "Home").Replace("a", "Away");
            string text = "";
            string text2 = "";
            string text3 = "";
            bool flag = false;
            bool flag2 = false;
            int num = 0;
            int num2 = 0;
            string text4 = "";
            string text5 = "";
            string text6 = "";
            bool flag3 = false;
            bool flag4 = false;
            bool sBOBETReTrade = false;
            int num3 = 0;
            string text7 = "";
            string betCount = "";
            string text8 = "";
            bool flag5 = false;

            float ibetOddValue_ = float.Parse(ibetOddValue);
            float sboOddValue_ = float.Parse(sbobetOddValue);

            if (ibetOddValue_ > 0.84 || (ibetOddValue_ > 0f && txtAddValue.Value.ToString() == "0.01"))
            {
                float valueAdd = ibetStake * (float)txtAddValue.Value;
                ibetStake = ibetStake + (int)valueAdd;
            }
            if (sboOddValue_ > 0.84 || (sboOddValue_ > 0f && txtAddValue.Value.ToString() == "0.01"))
            {
                float valueAdd = sbobetStake * (float)txtAddValue.Value;
                sbobetStake = sbobetStake + (int)valueAdd;
            }

            if (MatchDTO.IsSameMatch(ibetMatch.HomeTeamName.ToLower(), sbobetMatch.HomeTeamName.ToLower(), ibetMatch.AwayTeamName.ToLower(), sbobetMatch.AwayTeamName.ToLower()))
            {
                try
                {
                    ibetEngine.PrepareBet(ibetOddID, ibetOddType, ibetOddValue, ibetStake.ToString(), out flag, out num, out num2, out text4, out text2, out text3);
                    sbobetEngine.PrepareBet(sbobetOddID, sbobetOddValue, sbobetOddType, out betCount, out num3, out flag3, out text7, out text5, out text6);
                    if (MatchDTO.IsSameMatch(text2, text5, text3, text6))
                    {
                        if (flag && flag3)
                        {
                            if ((checkEdit13.Checked || checkEdit15.Checked) && sendOrNOT)//gui lenh
                            {
                                var taskA = new Task(() => this.AddOddToLocalCommunity(ibetMatch, sbobetMatch, oddType, sbobetOdd, sbobetOdd, ibetOddType, sbobetOddType, ibetOddValue, sbobetOddValue, ibetHomeGive));
                                taskA.Start();
                            }
                            float num4 = float.Parse(text4);
                            float num5 = float.Parse(text7);
                            if (num4 + num5 == 0f || num4 == num5)
                            {
                                if (ibetStake <= num2 && sbobetStake <= num3)
                                {
                                    try
                                    {                                        
                                        float currentCredit = 0;
                                        float currentCredit2 = 0;
                                        if (checkEdit8.Checked)
                                        {
                                            currentCredit = ibetEngine.GetCurrentCredit();
                                            currentCredit2 = sbobetEngine.GetCurrentCredit();
                                        }
                                        else
                                        {
                                            currentCredit = ibetEngine._currentCredit;
                                            currentCredit2 = sbobetEngine._currentCredit;
                                        }

                                        if ((float)ibetStake <= currentCredit && (float)sbobetStake <= currentCredit2)
                                        {
                                            try
                                            {
                                                
                                                ibetEngine.ConfirmBet(oddType, ibetOddValue, ibetStake.ToString(), num.ToString(), num2.ToString(), out flag2);
                                                if (flag2)
                                                {
                                                    sbobetEngine.ConfirmBet(sbobetOddID, sbobetOddValue, sbobetOddType, sbobetStake.ToString(), betCount, out text8, out flag4);
                                                    if (!flag4)
                                                    {
                                                        try
                                                        {
                                                            sbobetEngine.PrepareBet(sbobetOddID, text8, sbobetOddType, out betCount, out num3, out flag5, out text7, out text5, out text6);
                                                            if (MatchDTO.IsSameMatch(text2, text5, text3, text6))
                                                            {
                                                                num5 = float.Parse(text7);
                                                                if (num4 + num5 == 0f || num4 == num5)
                                                                {
                                                                    text = "Retrade. IBET: " + ibetOddValue + " -  SBOBET: " + text8;
                                                                    sbobetEngine.ConfirmBet(sbobetOddID, text8, sbobetOddType, sbobetStake.ToString(), betCount, out text8, out sBOBETReTrade);
                                                                    this._lastTransactionTime = System.DateTime.Now;
                                                                }
                                                                else
                                                                {
                                                                    text = "Invalid Odd while Retrade. IBET Odd: " + text4 + " - SBOBET Odd:" + text7;
                                                                }
                                                            }
                                                            else
                                                            {
                                                                text = string.Concat(new string[]
																{
																	"Not Same Match - Retrade. IBET: ", 
																	text2, 
																	" / ", 
																	text3, 
																	" - SBOBET: ", 
																	text5, 
																	" / ", 
																	text6
																});
                                                            }
                                                        }
                                                        catch (System.Exception ex)
                                                        {
                                                            text = "Error while Retrade. Details: " + ex.Message;
                                                        }
                                                    }
                                                    this._lastTransactionTime = System.DateTime.Now;
                                                    object obj = text;
                                                    text = string.Concat(new object[]
													{
														obj, 
														" - Success Transaction. Half: ", 
														ibetMatch.Half, 
														" - Minute: ", 
														ibetMatch.Minute, 
														" - Halftime: ", 
														ibetMatch.IsHalfTime
													});
                                                }
                                                
                                            }
                                            catch (System.Exception ex)
                                            {
                                                text = "Error while Trading. Details: " + ex.Message;
                                            }
                                        }
                                        else
                                        {
                                            text = string.Concat(new object[]
											{
												"Out of Cash. IBET Credit: ", 
												currentCredit, 
												" -  SBOBET Credit: ", 
												currentCredit2
											});
                                        }
                                    }
                                    catch (System.Exception ex)
                                    {
                                        text = "Error while getting Credit. Details: " + ex.Message;
                                    }
                                }
                                else
                                {
                                    text = string.Concat(new object[]
									{
										"Max Bet. IBET: ", 
										num2, 
										" - SBOBET: ", 
										num3
									});
                                }
                            }
                            else
                            {
                                text = "Invalid Odd while Preparing Ticket. IBET Odd: " + text4 + " - SBOBET Odd:" + text7;
                            }
                        }
                    }
                    else
                    {
                        text = string.Concat(new string[]
						{
							"Not Same Match - Preparing Ticket. IBET: ", 
							text2, 
							" / ", 
							text3, 
							" - SBOBET: ", 
							text5, 
							" / ", 
							text6
						});
                    }
                }
                catch (System.Exception ex)
                {
                    text = "Error while Preparing Trade. Details: " + ex.Message;
                }
            }
            else
            {
                text = string.Concat(new string[]
				{
					"Not Same Match - Comparing. IBET: ", 
					ibetMatch.HomeTeamName, 
					" / ", 
					ibetMatch.AwayTeamName, 
					" - SBOBET: ", 
					sbobetMatch.HomeTeamName, 
					" / ", 
					sbobetMatch.AwayTeamName
				});
            }
            transactionDTO.HomeTeamName = ibetMatch.HomeTeamName + " / " + sbobetMatch.HomeTeamName;
            transactionDTO.AwayTeamName = ibetMatch.AwayTeamName + " / " + sbobetMatch.AwayTeamName;
            transactionDTO.Odd = text4 + " / " + text7;
            transactionDTO.OddKindValue = text4 + " / " + text7;
            transactionDTO.OddValue = ibetOddValue + " / " + sbobetOddValue;
            transactionDTO.Stake = ibetStake + " / " + sbobetStake;
            transactionDTO.IBETAllow = flag;
            transactionDTO.IBETTrade = flag2;
            transactionDTO.IBETReTrade = false;
            transactionDTO.SBOBETAllow = flag3;
            transactionDTO.SBOBETTrade = flag4;
            transactionDTO.SBOBETReTrade = sBOBETReTrade;
            transactionDTO.OddType = oddType.ToString();
            transactionDTO.Note = text;
            transactionDTO.DateTime = System.DateTime.Now;
            transactionDTO.OddType = oddType.ToString() + " - " + ibetOddType.Replace("h", "Home").Replace("a", "Away") + " / " + sbobetOddType.Replace("h", "Home").Replace("a", "Away");
            this._betting = false;
            return transactionDTO;
            
        }
		private TransactionDTO PlaceBetAllowMaxBet(
            MatchDTO ibetMatch, 
            MatchDTO threein1betMatch, 
            eOddType oddType, 
            string ibetOddID,
            string ibetOddType,             
            string ibetOddValue, 
            OddDTO threein1Odd,
            string threein1OddType,
            int ibetStake, 
            int threein1betStake, 
            IBetEngine ibetEngine, 
            ThreeIn1BetEngine threein1betEngine)
		{
            
			TransactionDTO transactionDTO = new TransactionDTO(); //khoi tao transaction
            string text = string.Empty;			
            bool flagIBETOK = false;
            bool flag3IN1OK = false;
			
			bool threein1ReTrade = false;            
            string betCount = string.Empty;
            
            bool flagibetAllowance = false;
            int ibetminStake = 0;
            int ibetmaxStake = 0;
            string ibetBetKindValue = string.Empty;
            string ibetHomeTeamName = string.Empty;
            string ibetAwayTeamNam = string.Empty;

            bool ibetGoFirst = true;// who is Away or Under
            string threein1OddValue = string.Empty;
            if (threein1OddType == "Away") //neu 3in1 o vi tri away
            {
                threein1OddValue = threein1Odd.Away.ToString();
                if (threein1Odd.Type == eOddType.FirstHalfOverUnder || threein1Odd.Type == eOddType.FulltimeOverUnder) //neu 3in1 la Under
                {
                    ibetGoFirst = false; // cho 3in1 Confirm truoc
                }
                else // bong handicap
                {
                    if (threein1Odd.IsHomeGive) // neu away la doi cua duoi
                    {
                        ibetGoFirst = false;
                    }
                }
            }
            else if (threein1OddType == "Home") // neu 3in1 o vi tri home - ibet away
            {
                threein1OddValue = threein1Odd.Home.ToString();
                if (threein1Odd.Type == eOddType.FirstHalfOverUnder || threein1Odd.Type == eOddType.FulltimeOverUnder) //neu 3in1 la Over
                {
                    ibetGoFirst = true; // cho ibet Confirm truoc (under)
                }
                else // bong handicap
                {
                    if (!threein1Odd.IsHomeGive) // neu 3in1 la doi cua duoi
                    {
                        ibetGoFirst = false;
                    }
                }
            }

            bool flagthreein1betAllowance = false;
            int threein1betminStake = 0;
            int threein1betmaxStake = 0;
            string threein1betKindValue = string.Empty;
            string threein1betHomeTeamName = string.Empty;
            string threein1betAwayTeamName = string.Empty;
            
            string postBODY = string.Empty;            

            if (MatchDTO.IsSameMatch(ibetMatch.HomeTeamName.ToLower(), threein1betMatch.HomeTeamName.ToLower(), ibetMatch.AwayTeamName.ToLower(), threein1betMatch.AwayTeamName.ToLower()))
			{
				try
				{
                    ibetEngine.PrepareBet(ibetOddID, ibetOddType, ibetOddValue, ibetStake.ToString(), 
                        out flagibetAllowance, out ibetminStake, out ibetmaxStake, out ibetBetKindValue, out ibetHomeTeamName, out ibetAwayTeamNam);
                    
                    threein1betEngine.PrepareBet(
                        threein1Odd.ID,
                        threein1OddValue,//odd price
                        threein1Odd.Type.ToString(), 
                        threein1OddType, // "Away"
                        threein1Odd.IsHomeGive.ToString().Replace("True", "1").Replace("False", "0"),
                        threein1Odd.Odd, 
                        threein1betMatch.HomeScore, 
                        threein1betMatch.AwayScore, 
                        threein1betMatch.ID, 
                        threein1betMatch.IsHalfTime.ToString().Replace("True", "1").Replace("False", "0"), 
                        threein1betStake.ToString(),
                        out flagthreein1betAllowance, 
                        out threein1betminStake, 
                        out threein1betmaxStake, 
                        out threein1betKindValue, 
                        out threein1betHomeTeamName, 
                        out threein1betAwayTeamName,
                        out postBODY);
#if DEBUG
                    Utilities.WriteLog.Write("Calling threein1betEngine.PrepareBet");
#endif

                    if (MatchDTO.IsSameMatch(ibetHomeTeamName, threein1betHomeTeamName, ibetAwayTeamNam, threein1betAwayTeamName))
                    {
                        if (flagibetAllowance && flagthreein1betAllowance)
                        {
                            float numIbet = float.Parse(ibetBetKindValue);
                            float num3in1bet = float.Parse(threein1betKindValue);

                            if (numIbet + num3in1bet == 0f || numIbet == num3in1bet)
                            {
#if DEBUG
                                Utilities.WriteLog.Write("+ Valid bet, system is now going to confirm:" + ibetHomeTeamName + " - " + threein1betAwayTeamName);
#endif
                                int numMax = 0; // get minimum of Maximum bet allowed of two system
                                if (ibetmaxStake >= threein1betmaxStake)
                                {
                                    numMax = threein1betmaxStake;                                    
                                }
                                else
                                {
                                    numMax = ibetmaxStake;
                                }

                                if (ibetStake >= numMax) //compare stake with numMax
                                {
                                    ibetStake = numMax;
                                }
                                if (threein1betStake >= numMax)
                                {
                                    threein1betStake = numMax;
                                }

                                try
                                {
                                    float currentCredit = 0;
                                    float currentCredit2 = 0;
                                    if (checkEdit8.Checked)
                                    {
                                        currentCredit = ibetEngine.GetCurrentCredit();
                                        currentCredit2 = threein1betEngine.GetCurrentCredit();
                                    }
                                    else
                                    {
                                        currentCredit = ibetEngine._currentCredit;
                                        currentCredit2 = threein1betEngine._currentCredit;
                                    }
                                    
                                    
                                    if ((float)ibetStake <= currentCredit && (float)threein1betStake <= currentCredit2)
                                    {
                                        if (ibetGoFirst)
                                        {
                                            #region Confirm_IBET_first_then_3in1
#if DEBUG
                                            Utilities.WriteLog.Write("IBET GO FIRST!");
#endif
                                            try
                                            {
                                                ibetEngine.ConfirmBet(oddType, ibetOddValue, ibetStake.ToString(), ibetminStake.ToString(), ibetmaxStake.ToString(), out flagIBETOK);
                                                if (flagIBETOK)// iBet Confirm thanh cong
                                                {
                                                    string odd3in1NEW = string.Empty;
                                                    threein1betEngine.ConfirmBet(postBODY, threein1betStake.ToString(), out flag3IN1OK, out odd3in1NEW);
                                                    if (!flag3IN1OK)
                                                    {
                                                        try
                                                        {
                                                            if (odd3in1NEW != "changed")
                                                            {
                                                                //threein1betEngine.ConfirmBet(postBODY, threein1betStake.ToString(), out flag3IN1OK, out odd3in1NEW);
                                                                this._lastTransactionTime = System.DateTime.Now;
                                                                playSound(false);
                                                                text = "Odd 3in1 changed";
                                                                this.lblIbetCurrentCredit.Caption = this._ibetEngine.GetCurrentCredit().ToString();
                                                                
                                                            }
                                                            else
                                                            {
                                                                text = "Invalid Odd while Retrade.";// IBET Odd: " +text4 + " - SBOBET Odd:" + text7;
                                                                this.lblIbetCurrentCredit.Caption = this._ibetEngine.GetCurrentCredit().ToString();
                                                                playSound(false);
                                                            }
                                                        }
                                                        catch (System.Exception ex)
                                                        {
                                                            text = "Error while Retrade. Details: " + ex.Message;
                                                            playSound(false);

#if DEBUG
                                                            Utilities.WriteLog.Write(text);
#endif
                                                        }
                                                    }
                                                    else
                                                    {
                                                        this.lbl3in1betCurrentCredit.Caption = this._3in1Engine.GetCurrentCredit().ToString();
                                                        this.lblIbetCurrentCredit.Caption = this._ibetEngine.GetCurrentCredit().ToString();
                                                        playSound(true);
                                                    }
                                                    this._lastTransactionTime = System.DateTime.Now;
                                                    object obj = text;
                                                    text = string.Concat(new object[]
												    {
													    obj, 
													    " - Success Transaction. Half: ", 
													    ibetMatch.Half, 
													    " - Minute: ", 
													    ibetMatch.Minute, 
													    " - Halftime: ", 
													    ibetMatch.IsHalfTime
												    });                                                    
                                                }
                                            }
                                            catch (System.Exception ex)
                                            {
                                                text = "Error while Trading. Details: " + ex.Message;
#if DEBUG
                                                Utilities.WriteLog.Write(text);
#endif
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Confirm_3in1_first_then_IBET
#if DEBUG
                                            Utilities.WriteLog.Write("3IN1BET GO FIRST!");
#endif
                                            try
                                            {
                                                string odd3in1NEW = string.Empty;
                                                threein1betEngine.ConfirmBet(postBODY, threein1betStake.ToString(), out flag3IN1OK, out odd3in1NEW);
                                                if (flag3IN1OK)
                                                {
                                                    ibetEngine.ConfirmBet(oddType, ibetOddValue, ibetStake.ToString(), ibetminStake.ToString(), ibetmaxStake.ToString(), out flagIBETOK);
                                                    if (!flagIBETOK)// iBet Confirm thanh cong
                                                    {
                                                        try
                                                        {
                                                            playSound(false);
#if DEBUG
                                                            Utilities.WriteLog.Write("iBet confirm failed.");
#endif
                                                        }

                                                        catch (System.Exception ex)
                                                        {
                                                            text = "Error while Retrade. Details: " + ex.Message;
                                                            playSound(false);
#if DEBUG
                                                            Utilities.WriteLog.Write(text);
#endif
                                                        }
                                                    }
                                                    else
                                                    {
                                                        this.lbl3in1betCurrentCredit.Caption = this._3in1Engine.GetCurrentCredit().ToString();
                                                        this.lblIbetCurrentCredit.Caption = this._ibetEngine.GetCurrentCredit().ToString();
                                                        playSound(true);
                                                    }
                                                    this._lastTransactionTime = System.DateTime.Now;
                                                    object obj = text;
                                                    text = string.Concat(new object[]
												    {
													    obj, 
													    " - Success Transaction. Half: ", 
													    ibetMatch.Half, 
													    " - Minute: ", 
													    ibetMatch.Minute, 
													    " - Halftime: ", 
													    ibetMatch.IsHalfTime
												    });
                                                }
                                            }
                                            catch (System.Exception ex)
                                            {
                                                text = "Error while Trading. Details: " + ex.Message;
#if DEBUG
                                                Utilities.WriteLog.Write(text);
#endif
                                            }
                                            #endregion
                                        }
                                    }
                                    else
                                    {
                                        text = string.Concat(new object[]
										{
											"Out of Cash. IBET Credit: ", 
											currentCredit, 
											" -  SBOBET Credit: ", 
											currentCredit2
										});
#if DEBUG
                                        Utilities.WriteLog.Write(text);
#endif
                                    }
                                }
                                catch (System.Exception ex)
                                {
                                    text = "Error while getting Credit. Details: " + ex.Message;
#if DEBUG
                                    Utilities.WriteLog.Write(text);
#endif
                                }



                            }
                            else
                            {
                                text = "Invalid Odd while Preparing Ticket. IBET Odd: " + ibetBetKindValue + " - SBOBET Odd:" + threein1betKindValue;
#if DEBUG
                                Utilities.WriteLog.Write(text);
#endif
                            }
                        }
                        else
                        { 
#if DEBUG
                            if (!flagibetAllowance)
                                Utilities.WriteLog.Write("IBET DIDN'T ALLOW");
                            if (!flagthreein1betAllowance)
                                Utilities.WriteLog.Write("3IN1 DIDN'T ALLOW");
#endif

                        }
                    }

                    else
                    {
                        text = string.Concat(new string[]
						{
							"Not Same Match - Preparing Ticket. IBET: ", 
							ibetHomeTeamName, 
							" / ", 
							ibetAwayTeamNam, 
							" - 3in1BET: ", 
							threein1betHomeTeamName, 
							" / ", 
							threein1betAwayTeamName
						});
#if DEBUG
                        Utilities.WriteLog.Write(text);
#endif
                    }

                        
				}
				catch (System.Exception ex)
				{
					text = "Error while Preparing Trade. Details: " + ex.Message;
#if DEBUG
                    WriteLog.Write(" *** " + text);
#endif 
				}
			}
			else
			{
				text = string.Concat(new string[]
				{
					"Not Same Match - Comparing. IBET: ", 
					ibetMatch.HomeTeamName, 
					" / ", 
					ibetMatch.AwayTeamName, 
					" - 3in1BET: ", 
					threein1betMatch.HomeTeamName, 
					" / ", 
					threein1betMatch.AwayTeamName
				});
			}
            transactionDTO.HomeTeamName = ibetMatch.HomeTeamName + " / " + threein1betMatch.HomeTeamName;
            transactionDTO.AwayTeamName = ibetMatch.AwayTeamName + " / " + threein1betMatch.AwayTeamName;
            transactionDTO.Odd = ibetBetKindValue + " / " + threein1betKindValue;
			transactionDTO.OddKindValue = ibetBetKindValue + " / " + threein1betKindValue;
            transactionDTO.OddValue = ibetOddValue + " / " + threein1OddValue;
			transactionDTO.Stake = ibetStake + " / " + threein1betStake;
			transactionDTO.IBETAllow = flagibetAllowance;
            transactionDTO.IBETTrade = flagIBETOK;
			transactionDTO.IBETReTrade = false;
            transactionDTO.THREEIN1Allow = flagthreein1betAllowance;
            transactionDTO.THREEIN1Trade = flag3IN1OK;
			transactionDTO.THREEIN1ReTrade = threein1ReTrade;
			transactionDTO.OddType = oddType.ToString();
			transactionDTO.Note = text;
			transactionDTO.DateTime = System.DateTime.Now;
			return transactionDTO;
		}