﻿var CartContents = null;
var SectionContents = null;
var SectionEventID = 0;
var SectionSelected = null;
var SelectedTicketID = 0;
var SelectedSeatID = 0;
var CartDialog = null;
var SelectTicketDialog = null;
var TransDur = 1000;
var CartTickDuration = 1 * (60 * 1000);  // set for 1 minutes
var SectionTickDuration = (10 * 1000);  // set for 10 seconds
var TickSectionOn = false;
var CurrentURL = '';
var CheckoutURL = '';
var QuickBuyCheckoutURL = '';

function BlinkTopShoppingCart()
{
    $('#ShoppingCartText').effect('pulsate', { times: 1 }, 1000);
}

function RenderCartTopNumberOnly(results)
{
    var CartHTML = "<div id='ShoppingCartTopNumber' class='ShoppingCartTopNumber'>";
    CartHTML += results;
    CartHTML += "</div>";
    return CartHTML;
}

function ShowPleaseWaitCart()
{
    //    var ShoppingCartPleaseWait = document.getElementById("ShoppingCartPleaseWait");
    //    var ShoppingCartEmpty = document.getElementById("ShoppingCartEmpty");
    //    var ShoppingCartCheckout = document.getElementById("ShoppingCartCheckout");

    //    ShoppingCartEmpty.style.visibility = "hidden";
    //    ShoppingCartEmpty.style.height = "0px";

    //    ShoppingCartCheckout.style.visibility = "hidden";
    //    ShoppingCartCheckout.style.height = "0px";

    //    ShoppingCartPleaseWait.style.visibility = "visible";
    //    ShoppingCartPleaseWait.style.height = "100%";
}

// update the cart on the tick
function TickCart()
{
    if (CartContents != undefined && !CartContents.IsEmpty)
        TicketBreak.Services.ShoppingCart.GetLockTicketsSeconds();
    setTimeout(TickCart, CartTickDuration);
}

function TickSection()
{
    GetSection(SectionEventID, SectionSelected);
    setTimeout(TickSection, SectionTickDuration);
}

function AddTicketToCart(caller, IsDependant)
{
      var TicketInfo = $(caller).find("input:hidden:first").val().split(",");
    var TicketQTY = $("#buyticket_" + TicketInfo[3]).val();
    if (TicketQTY == 0)
    {
        alert("Please select a number of tickets to add to the cart.");
    } else
    {
        ShowPleaseWaitCart();
        AddTicket(TicketInfo[1], TicketInfo[2], TicketInfo[3], TicketQTY, IsDependant);
        //$(caller).effect("transfer", { to: "div.ShoppingCartContainer" }, TransDur);
    }
};

function AddMultipleBestAvailableSeatToCart(caller, IsDependant)
{
    var TicketInfo = $ (caller).find("input:hidden:first").val().split(",");
    var GroupID = TicketInfo[0];
    var TicketIDArray = new Array();
    var TicketQTYArray = new Array();
    var GroupCounter = 0;
    var TicketQuantityCheck = 0;
    $('.TicketGroupID' + GroupID).each(function ()
    {
        TicketIDArray[GroupCounter] = this.id.split("_")[1];
        TicketQTYArray[GroupCounter] = $("#" + this.id).val();
        TicketQuantityCheck += $("#" + this.id).val();
        GroupCounter++;
    });

    if (TicketQuantityCheck == 0)
    {
        alert("Please select a number of tickets to add to the cart.");
    }
    else
    {
        ShowPleaseWaitCart();
        AddMultipleBestTicket(TicketInfo[1], TicketInfo[2], TicketIDArray, TicketQTYArray, IsDependant);
    }
};


function AddBestToCart(caller, IsDependant)
{
    var TicketInfo = $(caller).find("input:hidden:first").val().split(",");
    var TicketQTY = $("#buyticket_" + TicketInfo[3]).val();
    if (TicketQTY == 0)
    {
        alert("Please select a number of tickets to add to the cart.");
    } 
    else
    {
        ShowPleaseWaitCart();
        AddBestTicket(TicketInfo[1], TicketInfo[2], TicketInfo[3], TicketQTY, IsDependant);
        //$(caller).effect("transfer", { to: "div.ShoppingCartContainer" }, TransDur);
    }
};

function AddSeatToCart(caller)
{
    var TicketInfo = caller.id.split("_");
    var SeatID = TicketInfo[1];
    var TicketID = TicketInfo[2];
    var Section = TicketInfo[3];

    // get the ticket from the list
    var ti = GetTicketFromSection(TicketID);
    if (ti != null)
    {
        // see if this ticket has children
        if (ti.Children.length > 0)
        {
            // we have children, prompt the user to select the ticketid from the list
            PromptUserForTicketID(SeatID, ti);
        } // check if the ticket is in a group
        else if (Section.length > 6 && Section.substring(0, 6) == "VIPGRP")
        {
            ShowPleaseWaitCart();
            AddSeatSection(SectionEventID, SeatID, TicketID, Section);
        }
        else
        {
            // only one ticketid, sent to cart
            ShowPleaseWaitCart();
            AddSeat(SeatID, TicketID);
            //$(caller).effect("transfer", { to: "div.ShoppingCartContainer" }, TransDur);
        }
    }
    else { alert('Could not add the seat.  Try again later.'); }
};

function GetTicketFromSection(TicketID)
{
    for (var tiPos in SectionContents.TicketItems)
    {
        var ti = SectionContents.TicketItems[tiPos];
        if (ti.TicketID == TicketID)
            return ti;
    }

    return null;
}

function RemoveSeatFromCart(caller)
{
    var TicketInfo = caller.id.split("_");
    var SeatID = TicketInfo[1];
    var TicketID = TicketInfo[2];
    var Section = TicketInfo[3];
    if (Section.length > 6 && Section.substring(0, 6) == "VIPGRP")
    {
        alert('This seat is a part of a group of seats; you are unable remove individual seats from the group.  If you would like to remove the group of seats please click remove in the shipping cart.');
    }
    else
    {
        if (confirm("Are you sure you want to remove this seat?"))
        {
            RemoveSeat(SeatID);
        }
    }
};

function BlinkCart()
{
    //$("div.ShoppingCart").effect("pulsate", { times: 1 }, 1000);
}

function GetCart()
{

    ShowPleaseWaitCart();

    TicketBreak.Services.ShoppingCart.GetCart(OnGetCartComplete);

    // if there is section information, load it
    if (SectionEventID > 0)
        GetSection(SectionEventID, SectionSelected);

}

function OnGetCartComplete(results)
{
    CartContents = results;

    $("div.ShoppingCart").html('');
    if (CartDialog != undefined)
    {
        CartDialog.html('');
        if (CartContents.IsEmpty)
        {
            CartDialog.dialog("close");
        }
    }
    //Update shopping cart item quantity display number.
    $("div.ShoppingCartNumber").html(RenderCartTopNumberOnly(CartContents.CartItemsQuantity));

    $('#linkQuickBuyCheckout').click(function (e) {
        e.preventDefault();
        if ($(this).hasClass('disabled')) {
            return false; // Do something else in here if required
        }  else
            window.location.href = $(this).attr('href');
    });

    //Update checkout shopping cart if on checkout page.
    if (document.location.href.indexOf(CheckoutURL) > -1 || document.location.href.indexOf(QuickBuyCheckoutURL) > -1)
    {
        CurrentURL = (document.location.href.indexOf(CheckoutURL) > -1) ? CheckoutURL : QuickBuyCheckoutURL;
        var CheckOutShoppingCart = document.getElementById('CheckOutShoppingCart');
        if (CheckOutShoppingCart != undefined)
        {
            if (!CartContents.IsEmpty)
            {
                $('#CheckOutShoppingCart').html(RenderCheckout());
            } else
            {
                $('#CheckOutShoppingCart').html('');
                window.location = CurrentURL;
            }
        }

    }
}

function RenderCheckout()
{
    var CartHTML = "";
    if (!CartContents.IsEmpty)
    {

        CartHTML += "<ul class='CheckOutShoppingCartContentsUL'>";
        // Checkout Contents
        for (var vPos in CartContents.VenueContainers)
        {
            var v = CartContents.VenueContainers[vPos];
            for (var epPos in v.EventParentContainers)
            {
                var ep = v.EventParentContainers[epPos];
                for (var ePos in ep.EventContainers)
                {
                    var e = ep.EventContainers[ePos];
                    for (var ciPos in e.CartItems)
                    {
                        var ci = e.CartItems[ciPos];
                        if (ci.PackageTicketID == 0)
                        {
                            CartHTML += "<li class='CheckOutShoppingCartContentsLI'>";
                            if (ep.EventParentID > 0 && e.EventID != ep.EventParentID)
                            {
                                CartHTML += "<div class='CheckoutEventParent CheckoutEventParent" + ep.EventParentID + "'>";
                                CartHTML += "<a class='CheckoutEventParentAnchor CheckoutEventParentAnchor" + ep.EventParentID + "' href='/event_details/" + ep.EventParentID + "'>";
                                CartHTML += ep.Name;
                                CartHTML += "</a>";
                                CartHTML += "</div>";
                            }

                            CartHTML += "<div class='CheckoutEvent CheckoutEvent" + e.EventID + "'>";
                            CartHTML += "<a class='CheckoutEventAnchor CheckoutEventAnchor" + e.EventID + "' href='/event_details/" + e.EventID + "'>";
                            CartHTML += e.Name;
                            CartHTML += "</a>";

                            CartHTML += "<div class='CheckoutVenue CheckoutCartVenue" + v.VenueID + " '>";
                            CartHTML += "<a class='CheckoutVenueAnchor CheckoutCartVenueAnchor" + v.VenueID + "' href='/venue_details/" + v.VenueID + "'>";
                            CartHTML += v.Name;
                            CartHTML += "</a>";
                            CartHTML += "</div>";

                            if (!e.SelectedShippingType.IsEmpty)
                            {
                                CartHTML += "<div class='CheckoutShipping CheckoutShipping" + e.EventID + "'>";
                                CartHTML += e.SelectedShippingType.Description + " " + e.SelectedShippingType.PerOrderFee.Display;
                                CartHTML += "</div>"
                            }
                            CartHTML += RenderCheckoutItem(ci);
                            CartHTML += "</li>"; //CheckOutShoppingCartContentsLI
                        }
                    }
                }
            }
        }

        CartHTML += "<ul class='CheckoutGiftCertificateTotalUL'>";
        if (CartContents.CreditAccountItems[0] != undefined)
        {
            for (var gcaiPos in CartContents.CreditAccountItems)
            {
                var gcai = CartContents.CreditAccountItems[gcaiPos];
                CartHTML += "<li class='CheckoutGiftCertificateTotalLI'>Gift Certificate " + gcai.CreditAccountNumber + " Used " + gcai.UsedValue.Display + " (Remaining " + gcai.Remaining.Display + ")</li>";
            }
        }
        CartHTML += "</ul>"; //CheckoutTotalUL

        //Checkout Total
        CartHTML += "<ul class='CheckoutTotalUL'>";
        for (var piPos in CartContents.PriceItems)
        {
            var pi = CartContents.PriceItems[piPos];
            CartHTML += "<li class='CheckoutTotalLI'>Total " + pi.DisplayWithTrailingCurrencyType + "</li>";
        }
        CartHTML += "</ul>"; //CheckoutTotalUL
        CartHTML += "</ul>"; //CheckOutShoppingCartContentsUL
    }

    return CartHTML;
}

function RenderCheckoutItem(ci)
{
    CartHTML = "";
    //CartHTML += "<li class='CheckoutCartItem CheckoutCartItem" + ci.ItemGUID + "'>";
    CartHTML += "<div class='CheckoutCartItemName'>" + ci.Name + "</div>";

    var HasSeatHeader = false;
    var HasCoupon = ci.HasCoupon;
    var Quantity = 0;
    var ItemHTML = "";
    for (var tPos in ci.TicketItems)
    {
        var t = ci.TicketItems[tPos];
        if (HasCoupon != t.HasCoupon)
        {
            ItemHTML += (HasCoupon) ? "<div class='CheckoutCartItemCoupon'>" + "Coupon " + ci.PriceCoupon.DisplayWithoutCurrencyType + "</div>" : "";
            ItemHTML += (Quantity > 0) ? "<div class='CheckoutCartItemQuantity'>" + "Quantity " + Quantity + "</div>" : "";
            HasCoupon = t.HasCoupon;
            Quantity = 0;
        }
        if (t.IsSeated)
        {
            if (!HasSeatHeader)
            {
                //ItemHTML += (t.SeatItem.Row.length > 0) ? "<div class='CheckoutCartItemSeatedItemHeader'>Section-Row-Seat</div>" : "<div class='CheckoutCartItemSeatedItemHeader'>Section-Seat</div>";
                ItemHTML += (t.SeatItem.Row.length > 0) ? "<ul class='CheckoutCartItemSeatedItemHeaderUL'><li class='CheckoutCartItemSeatedItemHeaderSectionLI'>Section</li><li class='CheckoutCartItemSeatedItemHeaderRowLI'>Row</li><li class='CheckoutCartItemSeatedItemHeaderSeatLI'>Seat</li></ul>" : "<ul class='CheckoutCartItemSeatedItemHeaderUL'><li class='CheckoutCartItemSeatedItemHeaderSectionLI'>Section</li><li class='CheckoutCartItemSeatedItemHeaderSeatLI'>Seat</li></ul>";
                HasSeatHeader = true;
            }
            //ItemHTML += (t.SeatItem.Row.length > 0) ? "<div class='CheckoutCartItemSeatedItem'>" + t.SeatItem.Section + "-" + t.SeatItem.Row + "-" + t.SeatItem.Seat + "</div>" : "<div class='CheckoutCartItemSeatedItem'>" + t.SeatItem.Section + "-" + t.SeatItem.Seat + "</div>";
            ItemHTML += (t.SeatItem.Row.length > 0) ? "<ul class='CheckoutCartItemSeatedItemItemUL'><li class='CheckoutCartItemSeatedItemSectionLI'>" + t.SeatItem.Section + "</li><li class='CheckoutCartItemSeatedItemRowLI'>" + t.SeatItem.Row + "</li><li class='CheckoutCartItemSeatedItemSeatLI'>" + t.SeatItem.Seat + "</li></ul>" : "<ul class='CheckoutCartItemSeatedItemItemUL'><li class='CheckoutCartItemSeatedItemSectionLI'>" + t.SeatItem.Section + "</li><li class='CheckoutCartItemSeatedItemSeatLI'>" + t.SeatItem.Seat + "</li></ul>";
        }
        Quantity += 1;
    }
    CartHTML += ItemHTML;

    CartHTML += (HasCoupon) ? "<div class='CheckoutCartItemCoupon'>" + "Coupon " + ci.PriceCoupon.DisplayWithoutCurrencyType + "</div>" : "";
    CartHTML += (Quantity > 0) ? "<div class='CheckoutCartItemQuantity'>" + "Quantity " + Quantity + "</div>" : "";

    CartHTML += "<div class='CheckoutCartItemPriceWrapper'>";
    CartHTML += "<div class='CheckoutCartItemTotal'>" + ci.PriceTotal.DisplayWithTrailingCurrencyType + "</div>";
    CartHTML += "<div class='CheckoutCartItemOpeningBrace'>(</div>";
    CartHTML += "<div class='CheckoutCartItemSubTotal'>" + ci.PriceSubTotal.DisplayWithoutCurrencyType + " Ticket</div>";
    CartHTML += (ci.HasExtraFees) ? "<div class='CheckoutCartItemPlusSign'> + </div>" : "";
    CartHTML += (ci.HasExtraFees) ? "<div class='CheckoutCartItemFees'>" + ci.PriceExtraFeesTotal.DisplayWithoutCurrencyType + " Fees</div>" : "";
    CartHTML += (ci.HasTaxes) ? "<div class='CheckoutCartItemPlusSign'> + </div>" : "";
    CartHTML += (ci.HasTaxes) ? "<div class='CheckoutCartItemTaxes'>" + ci.PriceTaxesTotal.DisplayWithoutCurrencyType + " Taxes</div>" : "";
    CartHTML += "<div class='CheckoutCartItemClosingBrace'>)</div>";
    CartHTML += "</div>"; //CheckoutCartItemPriceWrapper

    CartHTML += "<div class='CheckoutCartItemRemoveDiv'><a class='CheckoutCartItemRemoveAnchor' onclick='RemoveItem(\"" + ci.ItemGUID + "\");' onmouseover=\"this.style.cursor='hand';\" onmouseout=\"this.style.cursor='default';\"><span class='CheckoutCartItemRemoveSpan'>remove</span></a></div>";
    //CartHTML += "</li>"; //CheckoutCartItem

    return CartHTML;
}

function RemoveSeat(SeatID)
{
    TicketBreak.Services.ShoppingCart.RemoveSeat(SeatID, OnRemoveSeatComplete, OnRemoveSeatError);
}

function AddSeatSection(EventID, SeatID, TicketID, Section)
{
    TicketBreak.Services.ShoppingCart.AddSeatSection(EventID, SeatID, TicketID, Section, OnAddSeatSectionComplete, OnAddSeatSectionError);
}

function AddSeat(SeatID, TicketID) {
    TicketBreak.Services.ShoppingCart.AddSelectedSeat(SeatID, TicketID, null, OnAddSeatComplete, OnAddSeatError);
}

function AddBestTicket(VenueID, EventID, TicketID, Quantity, IsDependant)
{
    if (IsDependant == 'True')
    {
        TicketBreak.Services.ShoppingCart.AddBestAvailableSeat(VenueID, EventID, TicketID, Quantity, OnAddTicketCompleteDependant, OnAddTicketError);
    }
    else
    {
        TicketBreak.Services.ShoppingCart.AddBestAvailableSeat(VenueID, EventID, TicketID, Quantity, OnAddTicketComplete, OnAddTicketError);
    }
}

function AddMultipleBestTicket(VenueID, EventID, TicketID, Quantity, IsDependant)
{
    if (IsDependant == 'True')
    {
        TicketBreak.Services.ShoppingCart.AddMultipleBestAvailableSeat(VenueID, EventID, TicketID, Quantity, OnAddTicketCompleteDependant, OnAddTicketError);
    }
    else
    {
        TicketBreak.Services.ShoppingCart.AddMultipleBestAvailableSeat(VenueID, EventID, TicketID, Quantity, OnAddTicketComplete, OnAddTicketError);
    }
}

function AddTicket(VenueID, EventID, TicketID, Quantity, IsDependant)
{
    if (IsDependant == 'True')
    {
        TicketBreak.Services.ShoppingCart.AddTicket(VenueID, EventID, TicketID, Quantity, OnAddTicketCompleteDependant, OnAddTicketError);
    }
    else
    {
        TicketBreak.Services.ShoppingCart.AddTicket(VenueID, EventID, TicketID, Quantity, OnAddTicketComplete, OnAddTicketError);
    }
}

function GetSection(EventID, Section)
{
    TicketBreak.Services.ShoppingCart.GetSection(EventID, Section, OnGetSectionComplete, OnGetSectionError);
}

function OnGetSectionComplete(results)
{
    if (results != undefined)
        PaintSection(results);
}

function FormatDisplayCurrency(price)
{
    price = isNaN(price) || price === '' || price === null ? 0.00 : price;
    return parseFloat(price).toFixed(2);
}



function GetDisplayTicketInformationForLegend(SectionData, TicketID)
{
    var retVal = "";
    for (var tiPos in SectionData.TicketItems)
    {
        var ti = SectionContents.TicketItems[tiPos];
        if (ti.TicketID == TicketID)
        {
            if (isNaN(ti.Price) == false && ti.Price >= 0)
            {
                retVal = ti.DisplayName + "<br/>$" + FormatDisplayCurrency(ti.Price);
            }
            else
            {
                retVal = ti.DisplayName + "<br/>&nbsp;";
            }
            break;
        }
    }
    return retVal;
}

function PaintSection(SectionData)
{

    var HTML = '';
    if (SectionData != undefined)
    {
        SectionContents = SectionData;
        var Colours = new Array();
        Colours["notavailable"] = "#FF3333";
        Colours["available"] = new Array();
        Colours["available"]["00"] = "#009933";
        Colours["available"]["01"] = "#FFC000";
        Colours["available"]["02"] = "#A5A5A5";
        Colours["available"]["03"] = "#948B54";
        Colours["available"]["04"] = "#538ED5";
        Colours["available"]["05"] = "#D99795";
        Colours["available"]["06"] = "#6D497B";
        Colours["available"]["07"] = "#E46D0A";
        Colours["available"]["08"] = "#00B0F0";
        Colours["available"]["09"] = "#92D050";
        Colours["obstructed"] = "#E31422";
        Colours["nonseat"] = "#FFFFFF";
        Colours["yourseat"] = "#0099CC";
        Colours["wheelchair"] = "#FFFFFF";
        Colours["railing"] = "#000000";
        Colours["stairs"] = "#FF8000";

        //Generate Legend Array
        var TicketCombinationCtr = 0;
        var TicketCombinations = new Array();
        var TicketDynamicSeat = "DynamicSeat";
        var HasSeatTypeObstructedView = false;
        var HasSeatTypeWheelchairAccessible = false;
        var HasSeatTypeStairs = false;
        var HasSeatTypeRailing = false;
        for (var riPos in SectionContents.Rows)
        {
            var ri = SectionContents.Rows[riPos];
            for (var siPos in ri.Seats)
            {
                var si = ri.Seats[siPos];
                if (si.Status.substring(0, 1) == "0" && si.Status != "0U" && si.Status != "0A")
                {
                    if (TicketCombinationCtr == 0)
                    {
                        TicketCombinations[TicketCombinationCtr] = si.Status + "|" + si.TicketID + "|" + TicketDynamicSeat;
                        TicketCombinationCtr++;
                    }
                    else
                    {
                        var bNewCombinationFound = true;
                        for (var tcPos in TicketCombinations)
                        {
                            var TicketCombination = TicketCombinations[tcPos].split("|");
                            var TicketCombinationStatus = TicketCombination[0];
                            var TicketCombinationTicketID = TicketCombination[1];
                            if (si.Status == TicketCombinationStatus && si.TicketID == TicketCombinationTicketID)
                            {
                                bNewCombinationFound = false;
                                break;
                            }
                        }
                        if (bNewCombinationFound)
                        {
                            TicketCombinations[TicketCombinationCtr] = si.Status + "|" + si.TicketID + "|" + TicketDynamicSeat;
                            TicketCombinationCtr++;
                        }
                    }
                }
                else
                {
                    switch (si.Status)
                    {
                        case "0U":
                            //Obstructed View
                            HasSeatTypeObstructedView = true;
                            break;
                        case "0A":
                            //Wheelchair Accessible
                            HasSeatTypeWheelchairAccessible = true;
                            break;
                        case "NA":
                            //Stair Case
                            HasSeatTypeStairs = true;
                            break;
                        case "NR":
                            //Railing
                            HasSeatTypeRailing = true;
                            break;
                        default:
                            break;

                    }
                }
            }
        }

        HTML += FlowStart();
        HTML += "<h3>Seats for section " + SectionContents.Section + "</h3>";
        HTML += "<div class='LegendWrapper'>";
        HTML += "<ul class='LegendUL'>";

        //Add System Legend Information
        var FullLegendListCtr = 0;
        var FullLegendList = new Array();
        var SeatStatus;
        var SeatTicketID;
        var SeatHTML;
        //Your Seat
        SeatStatus = "Your Seat";
        SeatTicketID = FullLegendListCtr;
        SeatHTML = "<li class='LegendLI'><div class='LegendTopColor' style='margin-right:5px;height:32px;width:20px;background-color:" + Colours["yourseat"] + ";color:#FFFFFF'></div><br /><div class='LegendBottomImage'><img alt='' src='~/images/checked_on_sm.jpg' width='20' /></div><div class='LegendText'>Your Seat</div></li>";
        FullLegendList[FullLegendListCtr] = SeatStatus + "|" + SeatTicketID + "|" + SeatHTML;
        FullLegendListCtr++;
        //Not Available
        SeatStatus = "Taken";
        SeatTicketID = FullLegendListCtr;
        SeatHTML = "<li class='LegendLI'><div class='LegendTopColor' style='margin-right:5px;height:32px;width:20px;background-color:" + Colours["notavailable"] + ";color:#FFFFFF'></div><br /><div class='LegendBottomImage'></div><div class='LegendText'>Taken</div></li>";
        FullLegendList[FullLegendListCtr] = SeatStatus + "|" + SeatTicketID + "|" + SeatHTML;
        FullLegendListCtr++;
        //Obstructed View
        if (HasSeatTypeObstructedView)
        {
            SeatStatus = "Obstructed View";
            SeatTicketID = FullLegendListCtr;
            SeatHTML = "<li class='LegendLI'><div class='LegendTopColor' style='margin-right:5px;height:32px;width:20px;background-color:" + Colours["obstructed"] + ";color:#FFFFFF'></div><br /><div class='LegendBottomImage'><img alt='' src='~/images/checked_off_sm.jpg' width='20' /></div><div class='LegendText'>Obstructed<br />View</div></li>";
            FullLegendList[FullLegendListCtr] = SeatStatus + "|" + SeatTicketID + "|" + SeatHTML;
            FullLegendListCtr++;
        }
        //Wheelchair Accessible
        if (HasSeatTypeWheelchairAccessible)
        {
            SeatStatus = "Wheelchair Accessible";
            SeatTicketID = FullLegendListCtr;
            SeatHTML = "<li class='LegendLI'><div class='LegendTopColor' style='margin-right:5px;height:32px;width:20px;background-color:#003399;color:#FFFFFF'></div><br /><div class='LegendBottomImage'><img alt='' src='~/images/wc_logo.gif' width='20' /></div><div class='LegendText'>Wheelchair<br />Accessible</div></li>";
            FullLegendList[FullLegendListCtr] = SeatStatus + "|" + SeatTicketID + "|" + SeatHTML;
            FullLegendListCtr++;
        }
        //Railing
        if (HasSeatTypeRailing)
        {
            SeatStatus = "Railing";
            SeatTicketID = FullLegendListCtr;
            SeatHTML = "<li class='LegendLI'><div class='LegendTopColor' style='margin-right:5px;height:32px;width:20px;background-color:" + Colours["railing"] + ";color:#FFFFFF'></div><br /><div class='LegendBottomImage'></div><div class='LegendText'>Railing</div></li>";
            FullLegendList[FullLegendListCtr] = SeatStatus + "|" + SeatTicketID + "|" + SeatHTML;
            FullLegendListCtr++;
        }
        //Stairs
        if (HasSeatTypeStairs)
        {
            SeatStatus = "Stairs";
            SeatTicketID = FullLegendListCtr;
            SeatHTML = "<li class='LegendLI'><div class='LegendTopColor' style='margin-right:5px;height:32px;width:20px;background-color:" + Colours["stairs"] + ";color:#FFFFFF'></div><br /><div class='LegendBottomImage'></div><div class='LegendText'>Stairs</div></li>";
            FullLegendList[FullLegendListCtr] = SeatStatus + "|" + SeatTicketID + "|" + SeatHTML;
            FullLegendListCtr++;
        }

        for (var tcPos in TicketCombinations)
        {
            FullLegendList[FullLegendListCtr] = TicketCombinations[tcPos];
            FullLegendListCtr++;
        }

        //New Legend
        if (FullLegendList != null && FullLegendList.length > 0)
        {
            for (var tcPos in FullLegendList)
            {
                var TicketCombination = FullLegendList[tcPos].split("|");
                var TicketCombinationStatus = TicketCombination[0];
                var TicketCombinationTicketID = TicketCombination[1];
                var TicketCombinationSeatHTML = TicketCombination[2];





                if (TicketCombinationSeatHTML == TicketDynamicSeat)
                {
                    var bgcolour = Colours["available"][TicketCombinationStatus];
                    HTML += "<li class='LegendLI'><div class='LegendTopColor' style='margin-right:5px;height:32px;width:20px;background-color:" + bgcolour + ";color:#FFFFFF'></div><br /><div class='LegendBottomImage'><img alt='' src='~/images/checked_off_sm.jpg' width='20' /></div><div class='LegendText'>" + GetDisplayTicketInformationForLegend(SectionData, TicketCombinationTicketID) + "</div></li>";
                }
                else
                {
                    HTML += TicketCombinationSeatHTML;
                }

            }
        }
        HTML += "</ul>";
        HTML += "</div>";

        //End New Legend

        HTML += "<h2>Front</h2>";
        HTML += "<table cellspacing='0' cellpadding='0' border='0' width='100%'><tr><td align='center'>";
        for (var riPos in SectionContents.Rows)
        {
            var ri = SectionContents.Rows[riPos];
            HTML += "<table cellspacing='0' cellpadding='0' border='0'><tr><td style='width:20px;' align='center'>";
            HTML += "<strong>" + ri.Row + "</strong>";
            for (var siPos in ri.Seats)
            {
                var si = ri.Seats[siPos];
                var bgcolour = "";

                if (si.IsAvailable && si.Status.substring(0, 1) == "0")
                {
                    if (si.Status == "0U")
                    {
                        bgcolour = Colours["obstructed"];
                    } else if (si.Status == "0A")
                    {
                        bgcolour = Colours["wheelchair"];
                    } else
                    {
                        bgcolour = Colours["available"][si.Status];
                    }
                }
                else
                {
                    if (si.Status == "NS")
                    {
                        bgcolour = Colours["nonseat"];
                    }
                    else if (si.IsLockedByMe)
                    {
                        bgcolour = Colours["yourseat"];
                    }
                    else if (si.Status == "NA")
                    {
                        bgcolour = Colours["stairs"];
                    }
                    else if (si.Status == "NR")
                    {
                        bgcolour = Colours["railing"];
                    }
                    else
                    {
                        bgcolour = Colours["notavailable"];
                    }
                    si.IsAvailable = false;
                }

                var onClickEvent = (si.IsAvailable) ? "AddSeatToCart(this)" : "";
                onClickEvent = (si.IsLockedByMe) ? "RemoveSeatFromCart(this)" : onClickEvent;

                HTML += "<td align='center'><div id='seatdiv_" + si.SeatID + "_" + si.TicketID + "_" + si.Section + "' onclick='" + onClickEvent + "' style='color:#000000;font-weight:bold;background-color:" + bgcolour + ";width:22px;height:32px;border-color:White;border-width:1px;border-style:Solid;border-collapse:collapse;'>";
                HTML += si.Seat;
                if (si.IsAvailable)
                {
                    if (si.Status == "0A")
                    {
                        HTML += "<br /><img alt='Add to Cart' src='~/images/wc_logo.gif' width='20' />";
                    } else
                    {
                        HTML += "<br /><img alt='Add to Cart' src='~/images/checked_off_sm.jpg' width='20' />";
                    }
                }

                if (si.IsLockedByMe)
                    HTML += "<br /><img alt='Remove from Cart' src='~/images/checked_on_sm.jpg' width='20' />";

                HTML += "</div></td>";
            }

            HTML += "</td></tr></table>";
        }
        HTML += "</td></tr></table>";
        HTML += FlowStop();
    }
    $('div.SeatSection').html(HTML);
}

function OnGetSectionError(results)
{
    alert('Could not get the section information.  Try Again.');
}

function OnAddTicketCompleteDependant(results)
{
    if (results != undefined)
    {
        if (results == false)
        {
            alert('Unable to complete your request.  The ticket you are trying to add is an add-on ticket.  Please verify that you have added the ticket above it to your shopping cart.  This message is being shown because you have selected a quantity that exceeds the maximum allowed to be sold or there are not enough tickets available to complete your request.');
        }
        GetCart();
        BlinkTopShoppingCart();
    }
}

function OnAddTicketComplete(results)
{
    if (results != undefined)
    {
        if (results == false)
        {
            alert('Unable to complete your request.  Please try a lower quantity and try again.  This message is being shown because you have selected a quantity that exceeds the maximum allowed to be sold or there are not enough tickets available to complete your request.');
        }
        GetCart();
        BlinkTopShoppingCart();
    }
}

function OnAddTicketError(results)
{
    alert('Could not add ticket, please check your Quantity and try again');
    GetCart();
}

function OnRemoveSeatComplete(results)
{
    GetCart();
    BlinkTopShoppingCart();
    LeftEmptySeat();
}

function OnRemoveSeatError(results)
{
    GetSection(SectionEventID, SectionSelected);
    alert('Could not remove seat, please check your selection and try again');
    GetCart();
}

function OnAddSeatSectionComplete(results)
{
    if (results != undefined)
    {
        if (results == false)
        {


            alert('We were unable to add all the tickets in this group of seats to your shopping cart.  This can happen if you have reached your maximum tickets per event or if the seats have been reserved by someone else.');
        }
        else
        {

            alert('You have selected a seat that is a part of a group of seats.  You may only purchase these seats as a group.  All seats in the group will be added to your shopping cart.');
        }
        GetCart();
        BlinkTopShoppingCart();
    }
}

function OnAddSeatSectionError(results)
{
    GetSection(SectionEventID, SectionSelected);
    alert('Could not add seats, please check your selection and try again');
    GetCart();
}

function OnAddSeatComplete(results) {

    if (results != undefined)
    {
        if (results == false)
            alert('You have reached your limit.  Please continue to the checkout.');
        GetCart();
        LeftEmptySeat();
    }
}

function OnAddSeatError(results)
{
    GetSection(SectionEventID, SectionSelected);
    alert('Could not add seat, please check your selection and try again');
    GetCart();
}

function LeftEmptySeat() {
    TicketBreak.Services.ShoppingCart.LeftEmptySeat(OnLeftEmptySeatComplete, OnLeftEmptySeatError);

}

function CheckoutLeftEmptySeat() {
    TicketBreak.Services.ShoppingCart.LeftEmptySeat(OnCheckoutLeftEmptySeatComplete, OnLeftEmptySeatError);

}

function OnLeftEmptySeatComplete(results) {
    if (results != undefined) {
        if (results == true) {
            $("#divLeftEmptySeat").show();
            $('#linkQuickBuyCheckout').addClass('disabled')

        } else {
            $("#divLeftEmptySeat").hide();
            $('#linkQuickBuyCheckout').removeClass('disabled')

        }
        //GetCart();
    }
}

function OnCheckoutLeftEmptySeatComplete(results) {
    if (results != undefined) {
        if (results == true) {
            alert('You have now left an empty seat.  Please try your order again.');
            TicketBreak.Services.ShoppingCart.ClearShoppingCart(OnRemoveItemComplete, OnRemoveItemError);
            GetCart();
        } 
    }
}

function OnLeftEmptySeatError(results) {
    //GetSection(SectionEventID, SectionSelected);
    alert('Error with LeftEmptySeat');
    //GetCart();
}

function ApplyCoupon(VenueID, EventID, CouponCode)
{
    if (CouponCode != undefined && CouponCode != '')
    {
        TicketBreak.Services.ShoppingCart.ApplyCoupon(VenueID, EventID, CouponCode, OnApplyCouponComplete, OnApplyCouponError);
    }
}

function OnApplyCouponComplete(results)
{
    if (results != undefined)
        GetCart();
}

function OnApplyCouponError(results)
{
    alert('Could not apply coupon, please check coupon code and try again');
    GetCart();
}

function ApplyShipping(VenueID, EventID, ShippingTypeID)
{
    TicketBreak.Services.ShoppingCart.ApplyShipping(VenueID, EventID, ShippingTypeID, OnApplyShippingComplete, OnApplyShippingError);
}

function OnApplyShippingComplete(results)
{
    if (results != undefined)
        GetCart();
}

function OnApplyShippingError(results)
{
    alert('Could not apply shipping, please check your information and try again');
    GetCart();
}

function LoadAccount(AccountID)
{
    if (AccountID != undefined)
    {
        TicketBreak.Services.ShoppingCart.LoadAccount(AccountID, OnLoadAccountComplete, OnLoadAccountError);
    }
}

function OnLoadAccountComplete(results)
{
    if (results != undefined)
        GetCart();
}

function OnLoadAccountError(results)
{
    alert('Could not load account, please check your information and try again');
    GetCart();
}

function FindItem(ItemGUID)
{
    for (var vPos in CartContents.VenueContainers)
    {
        var v = CartContents.VenueContainers[vPos];
        for (var epPos in v.EventParentContainers)
        {
            var ep = v.EventParentContainers[epPos];
            for (var ePos in ep.EventContainers)
            {
                var e = ep.EventContainers[ePos];
                for (var ciPos in e.CartItems)
                {
                    var ci = e.CartItems[ciPos];
                    if (ci.ItemGUID == ItemGUID)
                        return ci;
                }
            }
        }
    }
    return null;
}

function FindTicketItem(TicketItemGUID)
{
    for (var vPos in CartContents.VenueContainers)
    {
        var v = CartContents.VenueContainers[vPos];
        for (var epPos in v.EventParentContainers)
        {
            var ep = v.EventParentContainers[epPos];
            for (var ePos in ep.EventContainers)
            {
                var e = ep.EventContainers[ePos];
                for (var ciPos in e.CartItems)
                {
                    var ci = e.CartItems[ciPos];
                    for (var tiPos in ci.TicketItems)
                    {
                        var ti = ci.TicketItems[tiPos];
                        if (ti.TicketGUID == TicketItemGUID)
                            return ti;
                    }
                }
            }
        }
    }
    return null;
}

function ClearShoppingCart()
{
    if (confirm('Are you sure you want to clear your shopping cart?'))
        TicketBreak.Services.ShoppingCart.ClearShoppingCart(OnRemoveItemComplete, OnRemoveItemError);
}


function RemoveItem(ItemGUID) {

    var ci = FindItem(ItemGUID);

    if (ci != null)
    {
        var itemInfo = ci.Name;
        if (confirm('Are you sure you want to remove the following?\n\n' + itemInfo))
            TicketBreak.Services.ShoppingCart.RemoveItem(ItemGUID, OnRemoveItemComplete, OnRemoveItemError);
    }
}

function OnRemoveItemComplete(results)
{
    if (results != undefined) {
        CheckoutLeftEmptySeat();
        GetCart();
    }
}

function OnRemoveItemError(results)
{
    alert('Could not remove, please try again');
    GetCart();
}

function FlowStart()
{
    return "<ul class='Flow'>";
}

function FlowStop()
{
    return "</ul>";
}

function FlowItem(item, align)
{

    var HTML = "<li class='Flow'><span class='FlowContainer'";
    if (typeof align != "undefined")
        HTML += " style='text-align: " + align + ";width=90%;' ";
    HTML += "><span class='FlowItem'>" + item + "</span></span></li>";
    return HTML;
}

function RenderCart(Size)
{
    Size = (typeof Size == "undefined") ? "Small" : Size;
    var CartHTML = "";
    if (!CartContents.IsEmpty)
    {
        // add the contents
        for (var vPos in CartContents.VenueContainers)
        {
            var v = CartContents.VenueContainers[vPos];
            CartHTML += "<div class='Venue " + v.VenueID + " '>" + v.Name;
            for (var epPos in v.EventParentContainers)
            {
                var ep = v.EventParentContainers[epPos];
                if (!OnlyPackagesParent(ep))
                {
                    if (ep.EventParentID > 0)
                        //CartHTML += "<div class='EventParent " + ep.EventParentID + "'>" + ep.Name;
                    CartHTML += "<div class='EventParent " + ep.EventParentID + "'>";
                    // display any credit account items
                    if (ep.CreditAccountItems[0] != undefined)
                    {
                        CartHTML += "<br /><strong>Gift Certificates</strong>";
                        for (var caiPos in ep.CreditAccountItems)
                        {
                            var cai = ep.CreditAccountItems[caiPos];
                            CartHTML += "<br/><strong>" + cai.CreditAccountNumber + "</strong> Used " + cai.UsedValue.Display;
                        }
                    }
                    for (var ePos in ep.EventContainers)
                    {
                        var e = ep.EventContainers[ePos];
                        if (!OnlyPackages(e))
                        {
                            if (e.EventID != ep.EventParentID)
                            {
                                CartHTML += "<div class='Event " + e.EventID + "'>" + e.Name;
                            }
                            if (!e.SelectedShippingType.IsEmpty)
                            {
                                CartHTML += FlowStart();
                                CartHTML += FlowItem(e.SelectedShippingType.Description + " " + e.SelectedShippingType.PerOrderFee.Display);
                                CartHTML += FlowStop();
                            }
                            for (var ciPos in e.CartItems)
                            {
                                var ci = e.CartItems[ciPos];
                                // do not display package items, we are showing the package item with totals
                                if (ci.PackageTicketID == 0)
                                    CartHTML += RenderCartItem(ci, Size);
                            }
                            if (e.EventID != ep.EventParentID)
                            {
                                CartHTML += "</div>";
                            }
                        }
                    }
                    if (ep.EventParentID > 0)
                        CartHTML += "</div>";
                }
            }
            CartHTML += "</div>";
        }

        // add the header
        CartHTML += "<div class='CartHeader'>";
        CartHTML += FlowStart();
        // display any global credit account items
        if (CartContents.CreditAccountItems[0] != undefined)
        {
            CartHTML += FlowItem("<br /><strong>Gift Certificates</strong>");
            CartHTML += "<br>";
            for (var gcaiPos in CartContents.CreditAccountItems)
            {
                var gcai = CartContents.CreditAccountItems[gcaiPos];
                CartHTML += FlowItem("<strong>" + gcai.CreditAccountNumber + "</strong> Used " + gcai.UsedValue.Display + " (Remaining " + gcai.Remaining.Display + ")<br/>");
            }
        }
        CartHTML += "<br>";
        for (var piPos in CartContents.PriceItems)
        {
            var pi = CartContents.PriceItems[piPos];
            CartHTML += FlowItem("<strong>Total " + pi.Display + "</strong>");
        }
        CartHTML += "<br>";
        if (!CartContents.Account.IsEmpty)
        {
            CartHTML += FlowItem(CartContents.Account.FullName);
            CartHTML += FlowItem(CartContents.Account.EMailAddress);
            CartHTML += FlowItem(CartContents.Account.PhoneNumber);
            CartHTML += "<br>";
            if (!CartContents.Account.Address.IsEmpty)
            {
                CartHTML += FlowItem(CartContents.Account.Address.AddressLine1);
                CartHTML += (CartContents.Account.Address.AddressLine2 != '') ? FlowItem(CartContents.Account.Address.AddressLine2) : "";
                CartHTML += FlowItem(CartContents.Account.Address.City + ", " + CartContents.Account.Address.Postal);
                CartHTML += "<br>";
            }
        }
        CartHTML += FlowStop();
        CartHTML += "<br>";
        CartHTML += "</div>";
    }

    return CartHTML;
}

function OnlyPackagesParent(ep)
{
    var Result = true;
    for (var ePos in ep.EventContainers)
    {
        var e = ep.EventContainers[ePos];
        Result = OnlyPackages(e);
        if (!Result)
            return Result;
    }
    return Result;
}

function OnlyPackages(e)
{
    for (var ciPos in e.CartItems)
    {
        var ci = e.CartItems[ciPos];
        if (ci.PackageTicketID == 0)
            return false;
    }
    return true;
}

function RenderCartItem(ci, Size)
{
    Size = (typeof Size == "undefined") ? "Small" : Size;

    CartHTML = "";
    CartHTML += "<div class='Ticket " + ci.ItemGUID + "'>";
    CartHTML += FlowStart();
    CartHTML += FlowItem(ci.Name);
    CartHTML += "<br>";

    var HasSeatHeader = false;
    var HasCoupon = ci.HasCoupon;
    var Quantity = 0;
    var ItemHTML = "";
    for (var tPos in ci.TicketItems)
    {
        var t = ci.TicketItems[tPos];
        if (HasCoupon != t.HasCoupon)
        {
            ItemHTML += (HasCoupon) ? FlowItem("Coupon " + ci.PriceCoupon.Display) : "";
            ItemHTML += (Quantity > 0) ? FlowItem("Quantity " + Quantity) + "<br>" : "";
            HasCoupon = t.HasCoupon;
            Quantity = 0;
        }
        if (t.IsSeated)
        {
            if (!HasSeatHeader)
            {
                ItemHTML += (t.SeatItem.Row.length > 0) ? FlowItem("Sec-Row-Seat") : FlowItem("Section-Seat");
                ItemHTML += "<br>";
                HasSeatHeader = true;
            }
            ItemHTML += (t.SeatItem.Row.length > 0) ? FlowItem(t.SeatItem.Section + "-" + t.SeatItem.Row + "-" + t.SeatItem.Seat) : FlowItem(t.SeatItem.Section + "-" + t.SeatItem.Seat);
            ItemHTML += "<br>";
        }
        Quantity += 1;
    }
    CartHTML += ItemHTML;
    CartHTML += (HasCoupon) ? FlowItem("Coupon " + ci.PriceCoupon.Display) + "<br>" : "";
    CartHTML += (Quantity > 0) ? FlowItem("Quantity " + Quantity) + "<br>" : "";

    CartHTML += FlowItem("Price " + ci.PriceSubTotal.Display) + "<br>";
    CartHTML += (ci.HasTaxes) ? FlowItem("Taxes " + ci.PriceTaxesTotal.Display) + "<br>" : "";
    CartHTML += (ci.HasExtraFees) ? FlowItem("Fees " + ci.PriceExtraFeesTotal.Display) + "<br>" : "";
    if (Size == "Large" || Size == "Checkout")
        CartHTML += FlowItem("Total " + ci.PriceTotal.Display) + "<br>";

    //if (Size == "Large")
    CartHTML += FlowItem("<a onclick='RemoveItem(\"" + ci.ItemGUID + "\");' onmouseover=\"this.style.cursor='hand';\" onmouseout=\"this.style.cursor='default';\">remove</a>", "right");
    CartHTML += FlowStop();
    CartHTML += "</div>";

    return CartHTML;
}

function DonePromptUserForTicketID()
{
    if (SelectedTicketID > 0)
    {
        ShowPleaseWaitCart();
        AddSeat(SelectedSeatID, SelectedTicketID);
        //$(SelectTicketDialog).effect("transfer", { to: "div.ShoppingCartContainer" }, TransDur);
        SelectTicketDialog.dialog("close");
    } else { alert('Please select a ticket type to continue.'); }
}

function SetTicketID(caller)
{
    SelectedTicketID = getCheckedValue(caller);
}

function PromptUserForTicketID(SeatID, ti)
{
    if (ti != null && ti.Children.length > 0)
    {
        SelectedTicketID = 0;
        SelectedSeatID = SeatID;
        if (SelectTicketDialog == null)
        {
            SelectTicketDialog = $("#SelectTicketDialog").dialog({
                modal: true,
                height: 200,
                width: 300,
                resizable: false,
                draggable: false,
                overlay: { opacity: 0.5, background: "black" },
                show: "slide",
                hide: "slide",
                close: function (ev, ui)
                {
                    $(this).hide();
                },
                open: function (ev, ui)
                {
                    $(this).show();
                },
                buttons: {
                    "Done": function () { DonePromptUserForTicketID(); },
                    "Close": function () { $(this).dialog("close"); }
                }
            });
        }
        else
        {
            // display the cart
            SelectTicketDialog.dialog("open");
        }

        // set the html
        var HTML = "";
        HTML += "<div align='center'>Please select the Ticket Type you would like to purchase and then click Done.<br/><br/>";
        HTML += "<table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><td align='right'><input name='TicketID' type='radio' value='" + ti.TicketID + "' onclick='SetTicketID(this);' /></td><td align='left'><strong>" + ti.DisplayName + "</strong></td></tr>";
        for (var cPos in ti.Children)
        {
            var ci = ti.Children[cPos];
            HTML += "<tr><td align='right'><input name='TicketID' type='radio' value='" + ci.TicketID + "' onclick='SetTicketID(this);' /></td><td align='left'><strong>" + ci.DisplayName + "</strong></td></tr>";
        }
        HTML += "</table></div>";

        SelectTicketDialog.html(HTML);
    }
    else
    {
        alert('Please add some tickets to your cart before you attempt to buy tickets.');
    }
}

function ShowDialog()
{
    if (!CartContents.IsEmpty)
    {
        if (CartDialog == null)
        {
            CartDialog = $("#ShoppingCartDialog").dialog({
                modal: true,
                height: 300,
                width: 500,
                resizable: false,
                draggable: false,
                overlay: { opacity: 0.5, background: "black" },
                show: "slide",
                hide: "slide",
                close: function (ev, ui)
                {
                    $(this).hide();
                },
                open: function (ev, ui)
                {
                    $(this).show();
                },
                buttons: {
                    "Checkout": function () { window.location = CheckoutURL; },
                    "Close": function () { $(this).dialog("close"); }
                }
            });
        }
        else
        {
            // display the cart
            CartDialog.dialog("open");
        }
        // set the html for the cart
        CartDialog.html(RenderCart('Large'));
    }
    else
    {
        alert('Please add some tickets to your cart before you attempt to buy tickets.');
    }
}

function RenderSection(EventID, Section)
{
    SectionEventID = EventID;
    SectionSelected = Section;

    if (!TickSectionOn)
    {
        TickSectionOn = true;
        TickSection();
    } else
    {
        GetSection(SectionEventID, SectionSelected);
    }
}

function RenderTicketSelect(HasAddToCartButton, GroupID, VenueID, EventID, TicketID, MaxQuantity, OnClickEvent, TextString)
{
    if (TextString == undefined) TextString = '';
    document.write('<div class="AddTicket">');
    if (MaxQuantity > 20) MaxQuantity = 20; //Limit pulldown list to only allow up to 20 seats per add to cart click.
    if (MaxQuantity > 0)
    {
        document.write('<select id="buyticket_' + TicketID + '" name="buyticket_' + TicketID + '" class=\"AddTicketSelect TicketGroupID'+GroupID+'\">')
        for (var j = 0; j < (MaxQuantity + 1); j++)
        {
            document.write('<option value="' + j + '">' + j + '</option>');
        }
        document.write('</select>');

        //Add to Cart Button
        if(HasAddToCartButton)
        {
            var AddTicketButtonID = "AddTicketButton" + TicketID;
            document.write('<div class="AddTicketButton" id="' + AddTicketButtonID + '"  onClick="' + OnClickEvent + "$('#" + AddTicketButtonID + "').effect('pulsate', { times: 1 }, 1000);" + '">');
            if (GroupID >= 0)
            {
                document.write('<input type="hidden" value="' + GroupID + ',' + VenueID + ',' + EventID + ',' + TicketID + '" />');
            }
            else
            {
                document.write('<input type="hidden" value="' + VenueID + ',' + EventID + ',' + TicketID + '" />');
            }
            if (TextString != '')
            {
                document.write(TextString);
            } else
            {
                document.write('<img alt=\"Add to Cart\" src=\"~/images/button-checkout-sm.jpg\" />');
            }
            document.write('</div>');
        }
    } else
    {
        document.write('<div class="AddTicketSoldOutDiv"><span class="AddTicketSoldOutSpan">Sold Out</span></div>');
    }
    document.write('</div>');
}

function getCheckedValue(radioObj)
{
    if (!radioObj)
        return "";
    var radioLength = radioObj.length;
    if (radioLength == undefined)
        if (radioObj.checked)
            return radioObj.value;
        else
            return "";
    for (var i = 0; i < radioLength; i++)
    {
        if (radioObj[i].checked)
        {
            return radioObj[i].value;
        }
    }
    return "";
}

function setCheckedValue(radioObj, newValue)
{
    if (!radioObj)
        return;
    var radioLength = radioObj.length;
    if (radioLength == undefined)
    {
        radioObj.checked = (radioObj.value == newValue.toString());
        return;
    }
    for (var i = 0; i < radioLength; i++)
    {
        radioObj[i].checked = false;
        if (radioObj[i].value == newValue.toString())
        {
            radioObj[i].checked = true;
        }
    }
}

function SelectSection_DoFSCommand(command, args)
{
    var SectionInfo = command.split("?");
    var EventID = SectionInfo[1];
    var Section = args;
    RenderSection(EventID, Section);
}

