﻿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 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) {
    var TicketInfo = $(caller).find("input:hidden:first").val().split(",");
    var TicketQTY = $("#buyticket_" + TicketInfo[2]).val();
    if (TicketQTY == 0) {
        alert("Please select a number of tickets to add to the cart.");
    } else {
        ShowPleaseWaitCart();
        AddTicket(TicketInfo[0], TicketInfo[1], TicketInfo[2], TicketQTY);
        //$(caller).effect("transfer", { to: "div.ShoppingCartContainer" }, TransDur);
    }
};

function AddBestToCart(caller) {
    var TicketInfo = $(caller).find("input:hidden:first").val().split(",");
    var TicketQTY = $("#buyticket_" + TicketInfo[2]).val();
    if (TicketQTY == 0) {
        alert("Please select a number of tickets to add to the cart.");
    } else {
        ShowPleaseWaitCart();
        AddBestTicket(TicketInfo[0], TicketInfo[1], TicketInfo[2], TicketQTY);
        //$(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");
        }
    }

    var ShoppingCartContainer = document.getElementById("ShoppingCartContainer");
    var ShoppingCartEmpty = document.getElementById("ShoppingCartEmpty");
    var ShoppingCartCheckout = document.getElementById("ShoppingCartCheckout");
    var ShoppingCartPleaseWait = document.getElementById("ShoppingCartPleaseWait");
    ShoppingCartContainer.style.visibility = "hidden";
    ShoppingCartContainer.style.height = "0px";
    ShoppingCartEmpty.style.visibility = "hidden";
    ShoppingCartEmpty.style.height = "0px";
    ShoppingCartCheckout.style.visibility = "hidden";
    ShoppingCartCheckout.style.height = "0px";
    ShoppingCartPleaseWait.style.visibility = "hidden";
    ShoppingCartPleaseWait.style.height = "0px";

    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(RenderCart('Checkout'));
            } else {
                $('#CheckOutShoppingCart').html('');
                window.location = CurrentURL;
            }
        }
    } else {
        ShoppingCartContainer.style.visibility = "visible";
        ShoppingCartContainer.style.height = "100%";
        if (!CartContents.IsEmpty) {
            ShoppingCartCheckout.style.visibility = "visible";
            ShoppingCartCheckout.style.height = "100%";
            $("div.ShoppingCart").html(RenderCart());
            if (CartDialog != undefined)
                CartDialog.html(RenderCart("Large"));
        }
        else {
            ShoppingCartEmpty.style.visibility = "visible";
            ShoppingCartEmpty.style.height = "100%";
        }
    }

    //BlinkCart();
}

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.AddSeat(SeatID, TicketID, null, OnAddSeatComplete, OnAddSeatError);
}

function AddBestTicket(VenueID, EventID, TicketID, Quantity) {
    TicketBreak.Services.ShoppingCart.AddBestAvailableSeat(VenueID, EventID, TicketID, Quantity, OnAddTicketComplete, OnAddTicketError);
}

function AddTicket(VenueID, EventID, TicketID, Quantity) {
    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 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";

        HTML += FlowStart();
        HTML += "<h3>Seats for section " + SectionContents.Section + "</h3>";
        HTML += "<div align='center'><table border='0' cellspacing='2' cellpadding='0'style='border-color:#000000;border-width:1px;border-style:Solid;border-collapse:collapse;'>";
        HTML += "<tr><td colspan='8' align='center'><strong>Legend</strong></td></tr><tr>";
//        HTML += "<td><div style='height:32px;width:20px;background-color:" + Colours["available"]["00"] + ";color:#FFFFFF'><br /><img alt='Add to Cart' src='~/images/checked_off_sm.jpg' width='20' /></div></td><td align='left' width='80px'><strong>Available</strong></td>";
        HTML += "<td><div style='height:32px;width:20px;background-color:" + Colours["yourseat"] + ";color:#FFFFFF'><br /><img alt='Remove from Cart' src='~/images/checked_on_sm.jpg' width='20' /></div></td><td align='left' width='80px'><strong>Your Seat</strong></td>";
        HTML += "<td><div style='height:32px;width:20px;background-color:" + Colours["notavailable"] + ";color:#FFFFFF'></div></td><td align='left' width='80px'><strong>Taken</strong></td>";
        HTML += "<td><div style='height:32px;width:20px;background-color:" + Colours["obstructed"] + ";color:#FFFFFF'><br /><img alt='Add to Cart' src='~/images/checked_off_sm.jpg' width='20' /></div></td><td align='left' width='100px'><strong>Obstructed View</strong></td>";
        HTML += "<td><div style='height:32px;width:20px;background-color:" + Colours["wheelchair"] + ";color:#FFFFFF'><br /><img alt='Add to Cart' src='~/images/wc_logo.gif' width='20' /></div></td><td align='left' width='100px'><strong>Wheelchair<br />Accessible</strong></td></tr>";
        HTML += "</table></div>";
        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 {
                        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 OnAddTicketComplete(results) {
    if (results != undefined) {
        if (results == false)
            alert('Unable to complete your request.  Please try a lower quantity and try again.  If problem persists, please try "Select Section and Seats" on previous page.');
        GetCart();
    }
}

function OnAddTicketError(results) {
    alert('Could not add ticket, please check your Quantity and try again');
    GetCart();
}

function OnRemoveSeatComplete(results) {
    GetCart();
}

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();
    }
}

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();
    }
}

function OnAddSeatError(results) {
    GetSection(SectionEventID, SectionSelected);
    alert('Could not add seat, please check your selection and try again');
    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 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)
        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 header
        CartHTML += "<div class='CartHeader'>";
        CartHTML += FlowStart();
        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 += "</div>";
        // 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;
                    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>";
        }
    }

    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(VenueID, EventID, TicketID, MaxQuantity, OnClickEvent, TextString) {
    if (TextString == undefined) TextString = '';
    document.write('<div class="AddTicket">');
    document.write('<table class="AddTicketTable" height="100%"><tr>');
    if (MaxQuantity > 0) {
        document.write('<td><select id="buyticket_' + TicketID + '" name="buyticket_' + TicketID + '" class=\"formfiedsdropdownprice\" onChange=\"ToggleDependants(this);\">')
        for (var j = 0; j < (MaxQuantity + 1); j++) {
            document.write('<option value="' + j + '">' + j + '</option>');
        }
        document.write('</select></td><td>');
        document.write('<div class="AddTicketButton" onClick="' + OnClickEvent + '">');
        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></td>');
    } else {
        document.write('<td nowrap="nowrap" align="center" valign="middle"><strong>SOLD OUT</strong></td>');
    }
    document.write('</tr></table></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);
}

