﻿var buyNowText = "buy now";
var moreInfoText = "more information";
var soldOutText = "sold out";
var notOnSaleText = "tickets not on sale";
var setCalText = function(b, m, s, n) {
    buyNowText = (b.toString().length > 0) ? b : buyNowText;
    moreInfoText = (m.toString().length > 0) ? m : moreInfoText;
    soldOutText = (s.toString().length > 0) ? s : soldOutText;
    notOnSaleText = (n.toString().length > 0) ? n : notOnSaleText;
};

// hide more info divs
var initMoreInfo = function(data) {
    (function($) {
        $('.dialog').dialog({ autoOpen: false });
        $('.dialog').dialog('option', 'buttons', { 'close': function() { $(this).dialog('close'); } });
        $('.dialog').dialog('option', 'height', 'auto');
    })(jQuery);
};

// display performances per date
var viewPerfs = function(data) {

    (function($) {

        var perfs = data.split("|");
        var links = "";
        // added 6.9.2010 - improve appearance when no perfs are listed:
        if (perfs.length > 1) {
            for (x = 0; x < perfs.length - 1; x++) {
                var perf = perfs[x].split(";")
                var perfT = perf[0];
                var perfTime = perf[1];
                var imgPath = perf[6];
                var info = "<a href=\"" + perf[5] + "?p=" + perf[2] + "\" class=\"moreInfo\">" + moreInfoText + "</a>";
                var avail = perf[3];
                if (avail == "Y") {
                    buyNow = "<a href=\"" + perf[4] + "?p=" + perf[2] + "\" class=\"purchaseSmall\">" + buyNowText + "</a>";
                } else if (avail == "N") {
                    buyNow = soldOutText;
                } else if (avail == "E") {
                    buyNow = "";
                } else if (avail == "U") {
                    buyNow = notOnSaleText;
                }
                if (info.length > 0 && buyNow.length > 0) {
                    buyNow = "&nbsp;&middot;&nbsp;" + buyNow;
                }
                var image = (imgPath.length > 0) ? "<img src=\"" + imgPath + "\" width=\"50\" style=\"padding-right: 4px;\" align=\"left\">" : "";
                //links += "<p><b>" + perfT + "</b><BR>" + perfTime + "<BR>" + info + buyNow + "</p>";
                links += "<p>" + image + "<b>" + perfT + "</b><BR>" + perfTime + "<BR>" + info + buyNow + "</p>";
            }
            $("display_perfs").show();
            $("#display_perfs").html("<BR>" + links);
            //$("#calendarContainer").height($("#boxContainer").height());
            // 4.6.2010 - making common (was in Custom_TNEW.js)
            if ($("#display_perfs").outerHeight() > $("#calendarContainer").height()) {
                $("#calendarContainer").height($("#display_perfs").outerHeight());
            }
            bumpHeight();
        } else {
            $("display_perfs").hide();
        }

    })(jQuery);

};

// toggle calendar link
var toggleCalLink = function(t, l) {
    (function($) {
        $("#toggleCalendar").attr("href", "javascript:toggleCal('" + t + "');void(0);");
        $("#toggleCalendar").text("View as " + l);
    })(jQuery);
};

// toggle calendar
var toggleCal = function(v) {
    (function($) {
        if (v == "text") {
            $("#boxContainer").hide();
            $("#textContainer").show();
            toggleCalLink("box", "Calendar");
            $("#view").val("text");
            // set height 
            //$("#calendarContainer").height($("#textContainer").height());
        } else if (v == "box" || v == "") {
            $("#boxContainer").show();
            $("#textContainer").hide();
            toggleCalLink("text", "List");
            $("#view").val("box");
            // set height 
            //$("#calendarContainer").height($("#boxContainer").height());
        }
        // bumpHeight(); // added 4.6.2010
    })(jQuery);
};

function bumpHeight() {
    (function ($) {
    //alert("calendar height: " + $("#calendarContainer").outerHeight() + " vs. box height: " + $("#boxContainer").outerHeight());
            $("#calendarContainer").outerHeight (
                $("#boxContainer").outerHeight()
             );
        })(jQuery);
}

// toggle account info tabs
// toggle calendar link
var toggleAcctTab = function(a, validSummID) {
    var tab = a.parentNode.id;
    var tabCont = a.parentNode.parentNode;
    for (x = 0; x < tabCont.childNodes.length; x++) {
        if (tabCont.childNodes[x].id == tab) {
            activateTab(tabCont.childNodes[x]);
        } else {
            deactivateTab(tabCont.childNodes[x]);
        }
    }
};

var activateTab = function(t) {
    (function($) {
        if (id) {
            var id = "#" + t.id;
            $(id).removeClass("tab");
            $(id).addClass("ui-tabs-selected");
        }
    })(jQuery);
};

var deactivateTab = function(t) {
    (function($) {
        if (id) {
            var id = "#" + t.id;
            $(id).removeClass("ui-tabs-selected");
            $(id).addClass("tab");
        }
    })(jQuery);
};

// FP 61 - HDR - 2.12.10
// find anchor and determine if something needs to be preselected:
var preloadAnchor = function() {
    var url = new String(document.location);
    if (url.indexOf("#") > -1) {
        var anch = url.substring(url.indexOf("#")).replace("#fragment-", "");
        preActivateTab(anch);
    }
};

// need means of selecting tab without node reference - scraped from activateTab:
var preActivateTab = function(id) {
    (function($) {
        var id = "tab-" + id;
        $("#tabs > li").each(
            function() {
                if ($(this).attr("id") == id) {
                    activateTab(this);
                } else {
                    deactivateTab(this);
                }
            }
        );
    })(jQuery);
};

// 6.9.2010 - seeing if I can get the tab *content* to show on demand!
var acct_preActivateTab = function(id) {
    (function($) {
        var frag = id;
        var id = "tab-" + id;
        $("#tabs > li").each(
            function() {
                if ($(this).attr("id") == id) {
                    activateTab(this);
                } else {
                    deactivateTab(this);
                }
            }
        );
        $("#tabs").tabs({ selected: [frag] });
    })(jQuery);
};

// get states:
var __getStates = function(url, country, dest, sel) {
    (function($) {
        var fullURL = url + "_util/AjaxData.aspx?methName=GetStateProvinceEx&country=" + country;
        var msg = "";
        $.ajax({
            type: "GET",
            url: fullURL,
            contentType: "xml",
            success: function(txt) {
                dest.options.length = 0
                dest.options.add(new Option('', ''));
                var dom = parseXML(txt);
                var $dom = $(dom);
                $dom.find("StateProvince").each(
                    function() {
                        var id = $(this).find("id").text();
                        var desc = $(this).find("description").text();
                        var opt = new Option(desc, id);
                        if (id == sel) {
                            opt.selected = true;
                        }
                        dest.options.add(opt);
                    });
            }
        });
    })(jQuery);
};

var getStates = function(url, country, destField, sel, reqField) {
    (function($) {
        var fullURL = url + "_util/AjaxData.aspx?methName=GetStateProvinceEx&country=" + country;
        var dest = document.getElementById(destField);
        var jqStateReq = $("#stateReq");
        var msg = "";
        $.ajax({
            type: "GET",
            url: fullURL,
            contentType: "xml",
            success: function(txt) {
                dest.options.length = 0
                dest.options.add(new Option('', ''));
                var dom = parseXML(txt);
                var $dom = $(dom);
                $dom.find("StateProvince").each(
                    function() {
                        var id = $(this).find("id").text();
                        var desc = $(this).find("description").text();
                        var opt = new Option(desc, id);
                        if (id == sel) {
                            opt.selected = true;
                        }
                        dest.options.add(opt);
                    });
                // nothing came back for selected country
                // if there is validation, turn it off:
                if (reqField.toString().length > 0) {
                    if (dest.options.length <= 1) {
                        document.getElementById(reqField).enabled = false;
                        jqStateReq.hide();
                    } else if (dest.options.length > 1) {
                        document.getElementById(reqField).enabled = true;
                        jqStateReq.show();
                    }
                }
            }
        });
    })(jQuery);
};

// display zpt grid for CYO perf seats
var showZonePriceTypes = function(invNo) {
    (function($) {
        $("#" + invNo).dialog("option", "buttons", { "close": function() { $(this).dialog("close"); } });
        $("#" + invNo).dialog("option", "width", "auto");
        $("#" + invNo).dialog("option", "height", "auto");
        $("#" + invNo).dialog("open");
    })(jQuery);
};

// get performance data for pkg dialog box; id of div provided in dest
var getInvContent = function(url, invNo, id) {
    (function($) {
        var fullURL = url + "_util/AjaxData.aspx?methName=GetWebContent&invNo=" + invNo;
        var msg = "";
        var text = "";
        var img = "";
        document.body.style.cursor = "wait";
        $.ajax({
            type: "GET",
            url: fullURL,
            contentType: "xml",
            error: function() {
                document.body.style.cursor = "default";
                alert("The information you requested could not be retrieved.");
            },
            success: function(txt) {
                var dom = parseXML(txt);
                var $dom = $(dom);
                var show = false;
                $dom.find("WebContent").each(
                    function() {
                        var desc = $(this).find("content_type_desc").text();
                        var value = $(this).find("content_value").text();
                        if (desc == "TN_Express_Prod_Img") {
                            img = value;
                        } else if (desc == "TN_Express_Prod_Short_Desc") {
                            text = value;
                        }
                    });
            },
            complete: function() {
                if (text.length > 0) {
                    $("#" + id + " > #prodText > #pText").html(text); //.text(text);                
                }
                if (img.length > 0) {
                    $("#" + id + " > #prodText > #pImg").attr("src", img);
                    $("#" + id + " > #prodText > #pImg").show();
                } else if (img.length <= 0) {
                    $("#" + id + " > #prodText > #pImg").hide();
                }
                $("#" + id).dialog("option", "buttons", { "close": function() { $(this).dialog("close"); } });
                $("#" + id).dialog("option", "height", "auto");
                $("#" + id).dialog("open");
                document.body.style.cursor = "default";
            }
        });
    })(jQuery);
};


// constants for use with CYO pkg perf mgmt functions
var perfGroups = new Array();
var pkgMin = 0; var pkgMax = 0;
var chosenPerfCount = 0;
var pkgPerfReq = 0;
var pgLoaded = false;

var buildPkgPGData = function() {
    (function($) {
        $("#perfListings > option[value=-1]").each(
            function() {
                var perfG = parseInt($(this).attr("perfGroup"));
                var perfGMin = parseInt($(this).attr("perfGroupMin"));
                var perfGMax = parseInt($(this).attr("perfGroupMax"));
                pkgMin = parseInt($(this).attr("pkgMin"));
                pkgMax = parseInt($(this).attr("pkgMax"));
                var thisPerfGroup = new Array(perfG, perfGMin, perfGMax);
                if (perfGMin > 0) {
                    // if perfGMin / max exist, then use that data
                    var chosenPerfG = $("#chosenPerfs > option[perfGroup=" + perfG + "]").size();
                    if (chosenPerfG < perfGMin) {
                        pkgPerfReq += parseInt(perfGMin - chosenPerfG);
                    }
                    //} else if (perfGMin == 0 && pkgMin > 0) {
                    // if perfGMin / max dont exist, use pkg min / max
                    //     if (chosenPerfCount < pkgMin) { pkgPerfReq += parseInt(pkgMin-chosenPerfCount); }
                }
                perfGroups[perfGroups.length] = thisPerfGroup;
                pgLoaded = true;
            }
        );
        if (chosenPerfCount < pkgMin && pkgPerfReq == 0) {
            pkgPerfReq = parseInt(pkgMin - chosenPerfCount);
        }
    })(jQuery);
};

// evaluate whether or not flex pkg perf selections meet qualifications to proceed to seating
var evalNFSPerfs = function() {
    (function($) {
        var overPerfCt = 0;
        var chooseMore = 0;
        var chosenPerfCount = $("#chosenPerfs > option").size() - 1;
        if (chosenPerfCount == 0) {
            $("#chosenPerfs").width($("#perfListings").width() + 20);
        }
        // if data not yet loaded
        if (!pgLoaded) {
            buildPkgPGData();
        }
        if (pkgMax > 0 && chosenPerfCount > pkgMax) {
            overPerfCt = parseInt(chosenPerfCount - pkgMax);
        }
        if (pkgPerfReq > chosenPerfCount) {
            chooseMore = parseInt(pkgPerfReq - chosenPerfCount);
        }

        //alert("pkgMx: " + pkgMax + " // pkgPerfReq: " + pkgPerfReq + " // chooseMore: " + chooseMore + " // chosePerfCt: " + chosenPerfCount);

        if (chooseMore > 0 || overPerfCt > 0) {
            if (chooseMore > 0) {
                $("#chosenPerfs > option[value=-1]").text("Choose " + chooseMore + " more performance(s)");
            }
            if (overPerfCt > 0) {
                $("#chosenPerfs > option[value=-1]").text("Please remove " + overPerfCt + " item(s) from this list");
            }
            $("#chosenPerfs > option[value=-1]").show()
            $("#btn_continue").attr("disabled", "true");
        } else {
            document.getElementById("perfs").value = "";
            document.getElementById("perfGroups").value = "";
            if (chosenPerfCount > 0) {
                $("#chosenPerfs > option[value=-1]").text("Success! Click 'Continue' to proceed");
                $("#chosenPerfs > option[value=-1]").addClass("bgC1 w");
            } else {
                $("#chosenPerfs > option[value=-1]").text("");
                $("#chosenPerfs > option[value=-1]").removeClass("bgC1 w");
            }
            $("#chosenPerfs > option[value=-1]").hide();
            $("#btn_continue").removeAttr("disabled");
            $("#chosenPerfs > option[value!=-1]").each(
                function() {
                    document.getElementById("perfs").value += $(this).val() + ",";
                    document.getElementById("perfGroups").value += $(this).attr("perfGroup") + ",";
                }
            );
        }
    })(jQuery);
};

var evalPerfSelection = function(pg) {
    var res = true;
    var pcount = $("#chosenPerfs > option[value!=-1]").length;
    if (pkgMax > 0 && pcount == pkgMax) {
        alert("This package cannot contain more than " + pkgMax + " performance(s).\nYou must remove an existing selection before adding a new performance.");
        res = false;
    } else if (pkgMax <= 0 || pcount < pkgMax) {
        var pgMax = 0;
        var pgcount = $("#chosenPerfs > option[perfGroup=" + pg + "]").length;
        for (y = 0; y < perfGroups.length; y++) {
            if (parseInt(perfGroups[y][0]) == parseInt(pg)) {
                pgMax = perfGroups[y][2];
                break;
            }
        }
        if (pgMax > 0 && pgcount == pgMax) {
            alert("This package cannot contain more than " + pgMax + " performance(s) from this group.\nYou must remove an existing selection from this group before adding a new performance.");
            res = false;
        }
    }
    return res;
};

var evalPGMins = function() { // call at form submit
    // evaluate how sel perfs stack up against perf groups mins
    var res = true;
    for (y = 0; y < perfGroups.length; y++) {
        var pg = perfGroups[y][0];
        var pgMin = perfGroups[y][1];
        var pgcount = $("#chosenPerfs > option[perfGroup=" + pg + "]").length;
        if (parseInt(pgMin) > parseInt(pgcount)) {
            alert("One or more package performance groups have not had the minimum number of performances added to your selection.\n\nPlease review your performance selection and make additions where necessary.");
            res = false;
            break;
        }
    }
    return res;
};

var addPerf = function() {
    (function($) {
        $("#perfListings > option:selected[value>0]").each(
            function() {
                selectPerf($(this));
                /*if (Number($(this).val()) > 0) {
                if ($("#chosenPerfs > option[value=" + $(this).val() + "]").size() <= 0) {
                var par = $(this);
                var id = $(this).html();
                var desc = $(this).val();
                var perfGroup = $(this).attr("perfGroup");
                    
                if (evalPerfSelection(perfGroup)) {
                var opt = new Option(id, desc);
                opt.setAttribute("perfGroup", perfGroup);
                        
                document.getElementById("chosenPerfs").options.add(opt);

                $("#chosenPerfs").append(opt);
                $(this).css("background-color","#CCC"); 
                $(this).removeAttr("selected");       
                }             
                }
                }*/
            }
        );
        evalNFSPerfs();
    })(jQuery);
};


var selectPerf = function(option) { //where option is a jqyery object : $(this)
    (function($) {
        if (Number(option.val()) > 0) {
            if ($("#chosenPerfs > option[value=" + option.val() + "]").size() <= 0) {
                var par = option;
                var id = option.html();
                var desc = option.val();
                var perfGroup = option.attr("perfGroup");

                if (evalPerfSelection(perfGroup)) {
                    var opt = new Option(id, desc);
                    //alert(opt.value + " " + opt.text);
                    opt.setAttribute("perfGroup", perfGroup);

                    document.getElementById("chosenPerfs").options.add(opt);

                    $("#chosenPerfs").append(opt);
                    option.css("background-color", "#CCC");
                    option.removeAttr("selected");
                }
            }
        }
    })(jQuery);
};

var dropPerf = function() {
    (function($) {
        $("#chosenPerfs > option:selected[value>0]").each(
            function() {
                if (Number($(this).val()) > 0) {
                    $("#perfListings > option[value=" + $(this).val() + "]").css("background-color", "#FFF");
                    $(this).remove();
                }
            }
        );
        evalNFSPerfs();
    })(jQuery);
};

// if user returns to CYO perfs page to change perf selection, need to prepop selection at page load
var prepopNFSSel = function(perfList) {
    if (perfList != "") {
        var pl = perfList.split(",");
        for (x = 0; x < pl.length - 1; x++) {
            var option = $("#perfListings > option[value=" + pl[x] + "]");
            selectPerf(option);
        }
        evalNFSPerfs();
    }
};

// for use on NSF page ~
var viewNFSPerf = function(url) {
    (function($) {
        var invNo = new Number($("#perfListings option:selected").val());
        var pInvNo = new Number($("#chosenPerfs option:selected").val());
        //if (invNo > 0) { getInvContent(url, invNo, "000"); pInvNo = 0; }
        //if (pInvNo > 0) { getInvContent(url, pInvNo, "000"); }
        if (invNo > 0) {
            $("#" + invNo).dialog("open");
            pInvNo = 0;
        }
        if (pInvNo > 0) {
            $("#" + pInvNo).dialog("open");
        }
    })(jQuery);
};

//xml parser ~ used by ajax functions above
function parseXML( xml ) { 
    if( window.ActiveXObject && window.GetObject ) { 
        var dom = new ActiveXObject( 'Microsoft.XMLDOM' ); 
        dom.loadXML( xml ); 
        return dom; 
    } 
    if( window.DOMParser ) 
        return new DOMParser().parseFromString( xml, 'text/xml' ); 
    throw new Error( 'No XML parser available' ); 
} 


// 10.5.10 - means to kill JS forced pt dropdown similarity on pkg forms
// where there are incongruous price types offered between packages / perfs
// all same will be set to false if pts don't square up 
var allSame = true;
function fixPT(key,val) {
    (function ($) {
        if (allSame) {
            var ddlName = ".cyoqty_" + key;
            $(ddlName).val(val);
        }
    })(jQuery);
}

// 10.26.10 - restoring javascript label on promo code field
function evalPromoCode(field, action) {
    if (field.value == "promo code" && action == "clear") {
        field.value = "";
    }
}

// 2.1.12 - disable specified buttons from being receptive to enter buttons       
function bar(evt) {
    evt = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    if ((evt.keyCode == 13)) {
         return false;
    }
    return true;
}
