[VauctionAuthorize, Compress, HttpPost, ValidateAntiForgeryTokenWrapper(HttpVerbs.Post)] // AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)
    public ActionResult PlaceBid(long? id, bool? ProxyBidding, decimal? BidAmount, decimal? RealBidAmount)
    {
      if (!id.HasValue) return RedirectToAction("Category", "Auction");
      AuctionDetail auction = AuctionRepository.GetAuctionDetail(id.GetValueOrDefault(-1), true);
      if (auction == null) return RedirectToAction("Category", "Auction");
      if (!auction.IsCurrentEvent || auction.Status == Consts.AuctionStatus.Closed || auction.DateEnd.CompareTo(DateTime.Now) < 0 || (!ProxyBidding.HasValue || !BidAmount.HasValue))
        return RedirectToAction("AuctionDetail", "Auction", new { @id = auction.LinkParams.ID });

      SessionUser cuser = AppHelper.CurrentUser;

      if (!AuctionRepository.IsUserWatchItem(cuser.ID, auction.LinkParams.ID))
        AuctionRepository.AddItemToWatchList(cuser.ID, auction.LinkParams.ID);

      PreviewBid previewBid = new PreviewBid
                                {
                                  LinkParams = auction.LinkParams,
                                  IsProxy = ProxyBidding.Value,
                                  Amount = BidAmount.Value,
                                  Quantity = 1,
                                  RealAmount = RealBidAmount.GetValueOrDefault(0)
                                };

      byte result;
      BidCurrent currentBid = new BidCurrent
      {
        Amount = BidAmount.GetValueOrDefault(0),
        Auction_ID = id.Value,
        DateMade = DateTime.Now,
        IP = Consts.UsersIPAddress,
        IsActive = true,
        IsProxy = ProxyBidding.GetValueOrDefault(false),
        MaxBid = BidAmount.GetValueOrDefault(0),
        Quantity = 1,
        User_ID = cuser.ID
      };
      BidCurrent previousBid, loserBid, winnerBid;

      result = BidRepository.BiddingForSingleAuction(auction, currentBid, out previousBid, out loserBid, out winnerBid);

      if (result == 3) return RedirectToAction("AuctionDetail", new { id.Value });

      AuctionRepository.RemoveAuctionResultsCache(id.Value);

      BidRepository.RemoveTopBidForItemCache(id.Value);

      BidRepository.UpdateUsersTopBidCache(id.Value, cuser.ID, currentBid);

      if (result == 1)
      {
        previewBid.Amount = winnerBid.Amount;
        if (cuser.IsRecievingOutBidNotice && !String.IsNullOrEmpty(cuser.Email) && !cuser.IsHouseBidder)
          Mail.SendOutBidLetter(cuser.FirstName, cuser.LastName, cuser.Email, auction.LinkParams.Lot.ToString(), auction.LinkParams.Title, winnerBid.Amount.GetCurrency(), auction.EventDateEnd > DateTime.Now ? auction.EventDateEnd.ToString() : DateTime.Now.Date.ToShortDateString(), auction.LinkParams.AuctionDetailUrl);
        return View("OutBid", previewBid);
      }

      if (cuser.IsRecievingBidConfirmation && !String.IsNullOrEmpty(cuser.Email) && !cuser.IsHouseBidder)
      {
        if (result == 2)
          Mail.SendSuccessfulBidUpdateLetter(cuser.FirstName, cuser.LastName, cuser.Email, auction.LinkParams.Lot.ToString(), auction.LinkParams.Title, currentBid.Amount.GetCurrency(), currentBid.MaxBid.GetCurrency(), auction.EventDateEnd > DateTime.Now ? auction.EventDateEnd.ToString() : DateTime.Now.Date.ToShortDateString(), auction.LinkParams.AuctionDetailUrl, currentBid.MaxBid > previousBid.MaxBid);
        else
          Mail.SendSuccessfulBidLetter(cuser.FirstName, cuser.LastName, cuser.Email, auction.LinkParams.Lot.ToString(), auction.LinkParams.Title, currentBid.Amount.GetCurrency(), BidAmount.GetCurrency(), auction.EventDateEnd > DateTime.Now ? auction.EventDateEnd.ToString() : DateTime.Now.Date.ToShortDateString(), auction.LinkParams.AuctionDetailUrl);
      }

      if (loserBid != null && loserBid.User_ID != cuser.ID)
      {
        User usr = dataProvider.UserRepository.GetUser(loserBid.User_ID, true);
        if (usr.IsRecievingOutBidNotice && !String.IsNullOrEmpty(usr.Email) && !usr.IsHouseBidder)
        {
          AddressCard ac = dataProvider.UserRepository.GetAddressCard(usr.Billing_AddressCard_ID.GetValueOrDefault(-1), true);
          Mail.SendOutBidLetter(ac.FirstName, ac.LastName, usr.Email, auction.LinkParams.Lot.ToString(), auction.LinkParams.Title, winnerBid.Amount.GetCurrency(), auction.EventDateEnd > DateTime.Now ? auction.EventDateEnd.ToString() : DateTime.Now.Date.ToShortDateString(), auction.LinkParams.AuctionDetailUrl);
        }
      }
      /*

      BidCurrent prevBid = BidRepository.GetUserTopBidForItem(id.Value, cuser.ID, false);
      if (prevBid != null && (prevBid.IsProxy == ProxyBidding) && prevBid.MaxBid >= BidAmount) return RedirectToAction("AuctionDetail", "Auction", new { @id = id });
      
      BidCurrent lastTop = BidRepository.GetTopBidForItem(id.Value);
      decimal lastMaxBid = (lastTop == null) ? 0 : lastTop.MaxBid;
      decimal lastamount = (lastTop == null) ? 0 : lastTop.Amount;

      BiddingObject placedBid = BidRepository.PlaceSingleBid(id.Value, ProxyBidding.Value, BidAmount.Value, cuser.ID, 1, (lastTop != null && lastTop.User_ID == cuser.ID && ProxyBidding.Value), auction.Price, prevBid, lastTop);      

      List<BidLogCurrent> newblogs = new List<BidLogCurrent>();
      BidRepository.ResolveProxyBiddingSituation(id.Value, cuser.ID, ProxyBidding.Value, placedBid, lastTop, auction.Price, newblogs);

      BidCurrent currentTop = BidRepository.GetTopBidForItem(id.Value);      
      BidRepository.UpdateUsersTopBid(id.Value, AppSession.CurrentUser.ID, placedBid.Bid);
      bool IsOutBidden = (lastTop != null && currentTop.MaxBid <= lastTop.MaxBid && currentTop.User_ID != cuser.ID);
      if (IsOutBidden)
      {
        if (placedBid.Bid.Amount >= currentTop.Amount)
        {
          currentTop.Amount = placedBid.Bid.Amount;
          BidRepository.UpdateCurrentBid(currentTop);
        }        
        if (lastamount < currentTop.Amount && newblogs.Where(BL=>BL.User_ID ==currentTop.User_ID && BL.Amount ==currentTop.Amount && BL.MaxBid==currentTop.MaxBid && BL.IsProxy == currentTop.IsProxy).Count()==0)
          BidRepository.AddBidLogCurrent(id.Value, currentTop.Quantity, currentTop.User_ID, currentTop.IsProxy, currentTop.Amount, currentTop.MaxBid, false, currentTop.IP);
        previewBid.Amount = currentTop.Amount;
        try
        {
          if (cuser.IsRecievingOutBidNotice && !String.IsNullOrEmpty(cuser.Email) && !cuser.IsHouseBidder)
            Mail.SendOutBidLetter(cuser.FirstName, cuser.LastName, cuser.Email, auction.LinkParams.Lot.ToString(), auction.LinkParams.Title, currentTop.Amount.GetCurrency(), auction.EventDateEnd > DateTime.Now ? auction.EventDateEnd.ToString() : DateTime.Now.Date.ToShortDateString(), auction.LinkParams.AuctionDetailUrl);
        }
        catch (Exception ex)
        {
          Utils.Lib.Logger.LogException(ex);
        }
        AuctionRepository.UpdateAuctionBiddingResult(id.Value, currentTop.User_ID, currentTop.Amount, currentTop.MaxBid);
        return View("OutBid", previewBid);
      }
      AuctionRepository.UpdateAuctionBiddingResult(id.Value, currentTop.User_ID, currentTop.Amount, currentTop.MaxBid);
      if (lastTop != null && lastTop.User_ID!=cuser.ID)
      {
        User usr = dataProvider.UserRepository.GetUser(lastTop.User_ID, true);
        AddressCard ac = dataProvider.UserRepository.GetAddressCard(usr.Billing_AddressCard_ID.GetValueOrDefault(-1), true);
        if (usr.IsRecievingOutBidNotice && !String.IsNullOrEmpty(usr.Email) && !usr.IsHouseBidder)
        {
          try
          {
            Mail.SendOutBidLetter(ac.FirstName, ac.LastName, usr.Email, auction.LinkParams.Lot.ToString(), auction.LinkParams.Title, placedBid.Bid.Amount.GetCurrency(), auction.EventDateEnd > DateTime.Now ? auction.EventDateEnd.ToString() : DateTime.Now.Date.ToShortDateString(), auction.LinkParams.AuctionDetailUrl);
          }
          catch (Exception ex)
          {
            Utils.Lib.Logger.LogException(ex);
          }
        }
      }

      try
      {
        if (cuser.IsRecievingBidConfirmation && !String.IsNullOrEmpty(cuser.Email) && !cuser.IsHouseBidder)
        {
          if (lastTop == null || lastTop.User_ID != cuser.ID)
          {
            Mail.SendSuccessfulBidLetter(cuser.FirstName, cuser.LastName, cuser.Email, auction.LinkParams.Lot.ToString(), auction.LinkParams.Title, placedBid.Bid.Amount.GetCurrency(), BidAmount.GetCurrency(), auction.EventDateEnd > DateTime.Now ? auction.EventDateEnd.ToString() : DateTime.Now.Date.ToShortDateString(), auction.LinkParams.AuctionDetailUrl);
          }
          else
          {
            Mail.SendSuccessfulBidUpdateLetter(cuser.FirstName, cuser.LastName, cuser.Email, auction.LinkParams.Lot.ToString(), auction.LinkParams.Title, currentTop.Amount.GetCurrency(), currentTop.MaxBid.GetCurrency(), auction.EventDateEnd > DateTime.Now ? auction.EventDateEnd.ToString() : DateTime.Now.Date.ToShortDateString(), auction.LinkParams.AuctionDetailUrl, (lastMaxBid < currentTop.MaxBid));
          }
        }
      }
      catch (Exception ex)
      {
        Utils.Lib.Logger.LogException(ex);
      }*/
      return View("SuccessfulBid", auction);
    }
    public ActionResult AuctionDetail(long? id, bool? ProxyBidding, decimal? BidAmount, decimal? OutBidAmount)
    {
      if (!id.HasValue) return RedirectToAction("Category", "Auction");
      AuctionDetail auction = AuctionRepository.GetAuctionDetail(id.GetValueOrDefault(-1), true);
      if (auction == null) return RedirectToAction("Category", "Auction");
      if (!auction.IsCurrentEvent || auction.Status == Consts.AuctionStatus.Closed || auction.DateEnd.CompareTo(DateTime.Now) < 0 || (!ProxyBidding.HasValue || !BidAmount.HasValue))
        return RedirectToAction("AuctionDetail", "Auction", new { @id = auction.LinkParams.ID });

      PreviewBid previewBid = new PreviewBid
                                {
                                  IsProxy = ProxyBidding.Value,
                                  Amount = BidAmount.Value,
                                  LinkParams = auction.LinkParams,
                                  Quantity = 1
                                };

      BidCurrent prevMaxBid = BidRepository.GetTopBidForItem(auction.LinkParams.ID);
      if (prevMaxBid != null)
      {
        previewBid.IsOutBid = previewBid.Amount <= prevMaxBid.Amount && previewBid.Amount <= prevMaxBid.MaxBid;
        if (prevMaxBid.User_ID == AppHelper.CurrentUser.ID)
        {
          previewBid.IsUpdate = true;
          previewBid.PreviousAmount = prevMaxBid.Amount;
          previewBid.PreviousMaxBid = prevMaxBid.MaxBid;
          previewBid.RealAmount = (previewBid.IsProxy) ? prevMaxBid.Amount : previewBid.Amount;
          previewBid.Amount = (previewBid.Amount < prevMaxBid.MaxBid) ? prevMaxBid.MaxBid : previewBid.Amount;
        }
        else
          previewBid.RealAmount = (!previewBid.IsProxy) ? previewBid.Amount : (((!OutBidAmount.HasValue) ? prevMaxBid.Amount : OutBidAmount.Value) + Consts.GetIncrement(prevMaxBid.Amount));
      }
      else
      {
        previewBid.RealAmount = (previewBid.IsProxy) ? auction.Price : previewBid.Amount;
      }
      return View("PreviewBid", previewBid);
    }