﻿//  Helper Functions/////////////////////////////////////////////////////////////
//  This is a function that removes all blank spaces in front and behind
var trim = function(itemValue){
    if (itemValue == null){
        return "";
    }
    itemValue = itemValue.toString();
	//  Removes white space infront and behind value
	return itemValue.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
	//return itemValue.replace(/^\s+|\s+$/g, "");
	//return itemValue.toString();
}
///////////////////////////////////////////////////////////////////////////////////

//  Creates a namespace for this functionality
YAHOO.namespace("Current");

YAHOO.Current.App = function() {
    //  Private Members
    var carousel;
    var sourceUrl = '';
    var divName = '';

    var itemsPerPage = 3;
    var objectsPerItem = 1;
    var objectsPerPage = itemsPerPage * objectsPerItem;

    var totalObjects = 0;

    var makeMerchantAdsTag = function(imageName) {
        return "<img src=\"http://images.colorfulimages.com/cgc/images/us/local/advertisements/source/" + imageName + "?fit=150w100h\" height=\"100\" width=\"150\"/>";
    }

    var addItemsToCarousel = function(itemArray, startPos) {
        for (var i = startPos, len = itemArray.length; i < len; ++i) {
            var ad = itemArray[i];
            var adElement = makeMerchantAdsTag(ad.AdImageFileName);
            try {
                carousel.addItem(adElement);
            }
            catch (x) {
                alert(x);
            }
        }
    }

    var ajax = function(pageUrl, fn) {
        YAHOO.util.Connect.asyncRequest("GET", pageUrl, fn);
    }

    var successProcess = function(o) {
        var responseObj;
        var ads = [];
        try {
            responseObj = YAHOO.lang.JSON.parse(o.responseText);
            if (responseObj != null) {
                switch (responseObj.ID) {
                    case "Merchant":
                        if (responseObj.Page == "1") {

                            totalObjects = responseObj.TotalItems;
                            ads = responseObj.Ads;

                            // Get total number of items
                            var totalItems = Math.ceil(totalObjects / objectsPerItem);

                            // Add first page of items to carousel
                            addItemsToCarousel(ads, 0);

                            //carousel.set("numItems", totalItems);

                            // and render it
                            carousel.render();
                            carousel.show();

                            // If there are more than 1 page of objects, go add the rest
                            if (totalObjects > objectsPerPage) {
                                var remainingPagesUrl = sourceUrl + '?page=r&pageSize=' + objectsPerPage;
                                ajax(remainingPagesUrl, callback);
                            }
                        }
                        else {
                            ads = responseObj.Ads;

                            addItemsToCarousel(ads, objectsPerPage);
                        }
                        break;
                    case "GetHTML":
                        document.getElementById("plhTemp").innerHTML = responseObj.result;
                        break;
                    case "DeleteItem":
                        var item = document.getElementById("giftItemContainer" + responseObj.DeletedItem);
                        var parent = item.parentNode;
                        if (parent != null) {
                            parent.removeChild(item);
                            if (responseObj.Count == "0") {
                                item = document.getElementById("tableTotals");
                                if (item != null) {
                                    parent = item.parentNode;
                                    if (parent != null) {
                                        parent.removeChild(item);
                                        item = document.getElementById("proceedCheckout");
                                        if (item != null) {
                                            item.style.display = "none";
                                            item = document.getElementById("proceedCheckoutTop");
                                            if (item != null) {
                                                item.style.display = "none";
                                                item = document.getElementById("anotherCompleteGiftTop");
                                                if (item != null) {
                                                    item.style.display = "none";
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else {
                                item = document.getElementById("totalsBlock");
                                if (item != null) {
                                    parent = item.parentNode;
                                    if (parent != null) {
                                        parent.innerHTML = responseObj.Results;
                                    }
                                }
                            }
                            document.getElementById("shoppingbasketlinksCount").innerHTML = "Gifts in Basket:  " + responseObj.Count;
                            document.getElementById("shoppingbasketlinksSubtotal").innerHTML = "Subtotal:  $" + responseObj.SubTotal;
                            document.getElementById("GiftCardorDeliveryNotCompleteCount").value = responseObj.GiftCardorDeliveryNotCompleteCount;
                            document.getElementById("GreetingCardNotCompleteCount").value = responseObj.GreetingCardNotCompleteCount;
                            //$("#shoppingbasketlinksCount").html("Gifts in Basket:  " + responseObj.Count);
                            //$("#shoppingbasketlinksSubtotal").html("Subtotal:  $" + responseObj.SubTotal);
                        }
                        break;
                    case "DuplicateItem":
                        eval(responseObj.Results);
                        break;
                    case "StartOver":
                        eval(responseObj.Results);
                        break;
                    case "ChangeUserContact":
                        var userContactAddressSelect = document.getElementById("UserContactAddressDropDownList");
                        var count = userContactAddressSelect.options.length;
                        //remove dropdownlist
                        for (var i = count; i > 0; i--) {
                            userContactAddressSelect.removeChild(userContactAddressSelect.options[i - 1]);
                        }
                        //append dropdownlist
                        var itemArry = responseObj.ContactAddress;
                        var contactaddressCount = itemArry.length;
                        for (var i = 0; i < contactaddressCount; i++) {
                            var option = document.createElement("option");
                            var uca = itemArry[i];
                            option.innerText = uca.AddressName;
                            option.text = uca.AddressName;
                            option.value = uca.AddressId;

                            if (window.navigator.userAgent.indexOf("MSIE") > 0)
                                userContactAddressSelect.appendChild(option);
                            else
                                userContactAddressSelect.appendChild(option, null);
                        }
                        //set delivery options
                        if (responseObj.DeliveryOptions != null && responseObj.DeliveryOptions != "") {
                            $("#DeliverToName").val(responseObj.DeliveryOptions.deliveryName);
                            $("#DeliverToCompany").val(responseObj.DeliveryOptions.deliverToCompany);
                            $("#DeliverToAddress1").val(responseObj.DeliveryOptions.deliverToAddress1);
                            $("#DeliverToAddress2").val(responseObj.DeliveryOptions.deliverToAddress2);
                            $("#DeliverToCity").val(responseObj.DeliveryOptions.deliverToCity);
                            $("#DeliverToState").val(responseObj.DeliveryOptions.deliverToState);
                            $("#DeliverToPostalCode").val(responseObj.DeliveryOptions.deliverToPostalCode);
                            $("#createUserContactCheckboxText").text("Update this Contact");
                        }
                        break;
                    case "ChangeUserContactAddress":
                        //set delivery options
                        if (responseObj.DeliveryOptions != null && responseObj.DeliveryOptions != "") {
                            $("#DeliverToName").val(responseObj.DeliveryOptions.deliveryName);
                            $("#DeliverToCompany").val(responseObj.DeliveryOptions.deliverToCompany);
                            $("#DeliverToAddress1").val(responseObj.DeliveryOptions.deliverToAddress1);
                            $("#DeliverToAddress2").val(responseObj.DeliveryOptions.deliverToAddress2);
                            $("#DeliverToCity").val(responseObj.DeliveryOptions.deliverToCity);
                            $("#DeliverToState").val(responseObj.DeliveryOptions.deliverToState);
                            $("#DeliverToPostalCode").val(responseObj.DeliveryOptions.deliverToPostalCode);
                            $("#createUserContactCheckboxText").text("Update this Contact");
                        }
                        break;
                    case "ChangeUserPayment":
                        if (responseObj.UserPaymentOptions != null && responseObj.UserPaymentOptions != "") {
                            $("#AddressName").val(responseObj.UserPaymentOptions.AddressName);
                            $("#Optional").val(responseObj.UserPaymentOptions.Optional);
                            $("#City").val(responseObj.UserPaymentOptions.City);
                            $("#StateCode").val(responseObj.UserPaymentOptions.StateCode);
                            $("#PostalCode").val(responseObj.UserPaymentOptions.PostalCode);
                            $("#DayTimePhone").val(responseObj.UserPaymentOptions.DayTimePhone);
                            $("#Email").val(responseObj.UserPaymentOptions.Email);
                            var radioArray = document.getElementsByName("CCTypeRadioButton");
                            var radioLength = radioArray.length;
                            for (var i = 0; i < radioLength; i++) {
                                radioArray[i].checked = false;
                                if (radioArray[i].value == responseObj.UserPaymentOptions.CCTypeId) {
                                    radioArray[i].checked = true;
                                }
                            }
                            $("#CCTypeId").val(responseObj.UserPaymentOptions.CCTypeId);
                            $("#CCTypeName").val(responseObj.UserPaymentOptions.CCTypeName);
                            $("#NameOnCard").val(responseObj.UserPaymentOptions.NameOnCard);
                            $("#CardNumber").val(responseObj.UserPaymentOptions.CardNumber);
                            $("#VerificationNumber").val(responseObj.UserPaymentOptions.VerificationNumber);
                            $("#ExpMon").attr("value", responseObj.UserPaymentOptions.ExpMon);
                            $("#ExpYear").attr("value", responseObj.UserPaymentOptions.ExpYear);
                        }
                        break;
                }
            }
        }
        catch (exc) {
            alert("Error : " + exc); //Need to get better handler here
        }
    }
    var failureProcess = function(o) {
        alert("Ajax request failed!");
    }
    var callback = {
        success: successProcess,
        failure: failureProcess
    }
    var makeMerchantAdsCarousel = function() {
        // Get first page (success handler populates var for total items
        var firstPageUrl = sourceUrl + '?page=1&pageSize=' + objectsPerPage;
        //YAHOO.util.Connect.asyncRequest("GET", firstPageUrl, merchantAdsPage1Callback);
        ajax(firstPageUrl, callback);
    }
    var buildCarousel = function() {
        // Initial definition of carousel
        carousel = new YAHOO.widget.Carousel(divName, {
            numVisible: itemsPerPage,
            animation: { speed: 0.15 },
            scrollInc: itemsPerPage
        });
    }
    var removeChildElements = function(itemId) {
        var item = document.getElementById(itemId);
        if (item != null) {
            while (item.childNodes.length > 0) {
                item.removeChild(item.childNodes[0]);
            }
            //for (var i = 0; i < item.childNodes.length; i++){
            //    item.removeChild(item.childNodes[i]);
            //}
        }
    }
    var removeDialog = function() {
        var divItem = document.getElementById("dialogContainer");
        if (divItem != null) {
            removeChildElements(divItem.id);
            divItem.parentNode.removeChild(divItem);
            divItem = document.getElementById("dialogContainer_mask");
            if (divItem != null) {
                divItem.parentNode.removeChild(divItem);
            }
        }
    }
    return {
        //  Public Members
        init: function() {

        },
        loadCarousel: function(merchantAdsCarouselDivName, merchantAdsSourceUrl) {
            sourceUrl = merchantAdsSourceUrl;
            divName = merchantAdsCarouselDivName;
            buildCarousel();
            makeMerchantAdsCarousel();
        },
        dialog: function(containerObj, intWidth, intHeight, showModal, html) {
            if (containerObj == null) {
                containerObj = document.body;
            }
            try {
                var localStyle = containerObj.style;
                if (localStyle == null) {
                    try {
                        var item = document.getElementById(containerObj);
                        if (item == null) {
                            containerObj = document.body;
                        }
                        else {
                            containerObj = item;
                        }
                    }
                    catch (xx) {
                        containerObj = document.body;
                    }
                }
            }
            catch (x) {
                try {
                    item = document.getElementById(containerObj);
                    if (item == null) {
                        containerObj = document.body;
                    }
                }
                catch (xx) {
                    containerObj = document.body;
                }
            }
            if (intWidth == null || trim(intWidth) == "") {
                intWidth = 400;
            }
            if (intHeight == null || trim(intHeight) == "") {
                intHeight = 400;
            }
            if (showModal == null) {
                showModal = true;
            }
            if (html == null) {
                html = "";
            }
            intWidth = trim(intWidth);
            intHeight = trim(intHeight);
            var div = document.createElement("div");
            //for (var i = 0; i < 100; i++){
            var sName = "dialogContainer";
            if (document.getElementById(sName) == null) {
                div.id = sName;
                div.name = sName;
                //break;
            }
            //}
            div.style.overflow = "hidden";
            div.style.width = intWidth.toString() + "px";
            div.style.height = intHeight.toString() + "px";

            containerObj.appendChild(div);
            div.innerHTML = "<div class='bd' style='font-style:sans-serif;margin:0px;overflow:auto;height:" + (intHeight - 35).toString() + "px;'>" + html + "</div>";

            var d = new YAHOO.widget.Dialog(div.id, {
                monitorresize: true,
                constraintoviewport: true,
                fixedcenter: true,
                width: intWidth.toString(),
                height: intHeight.toString(),
                zIndex: 2000,
                close: true,
                underlay: "shadow",
                modal: showModal
            });

            d.render();
            d.show();
            YAHOO.util.Event.on(YAHOO.util.Dom.getElementsByClassName("container-close", "a", div.id), "click", function() {
                //d.destroy();
                //var item = document.getElementById("MerchantAdVideo");
                //item.parentNode.removeChild(item);
                removeDialog();
                //removeChildElements(div.id);
                //item = document.getElementById(div.id);
                //item.parentNode.removeChild(item);
            });
        },
        tabView: function(containerObj, width, height, tabInfo) {
            if (width != null && height != null) {
                containerObj.style.width = width.toString() + "px";
                containerObj.innerHTML = "";
                //containerObj.style.height = height.toString() + "px";
            }
            var name = containerObj.id + "_Tabs";
            name = new YAHOO.widget.TabView();
            if (tabInfo != null) {
                var cnt = 0;
                for (var i = 0; i < tabInfo.length; i++) {
                    if (trim(tabInfo[i].content) == "") {
                        continue;
                    }
                    if (cnt == 0) {
                        name.addTab(new YAHOO.widget.Tab({
                            label: tabInfo[i].label,
                            content: "<div style='padding:10px;'>" + tabInfo[i].content + "</div>",
                            active: true
                        }));
                    }
                    else {
                        name.addTab(new YAHOO.widget.Tab({
                            label: tabInfo[i].label,
                            content: "<div style='padding:10px;'>" + tabInfo[i].content + "</div>"
                        }));
                    }
                    cnt += 1;
                }
            }
            name.appendTo(containerObj);
            tempTabView = name;
        },
        validateNumericAmount: function(item, lowInt, highInt, value) {
            var success = true;
            if (item != null && lowInt != null && highInt != null) {
                if (value != null) {
                    var tmp = trim(value);
                    if (tmp != "") {
                        for (var j = 0; j < tmp.length; j++) {
                            try {
                                var l = parseInt(tmp.slice(j, j + 1), 10);
                                if (isNaN(l)) {
                                    YAHOO.Current.App.dialog(null, 200, 100, true, "<div style=\"text-align:center;\">Only numberic values are allowed...</div>");
                                    //alert("Only numberic values are allowed...");
                                    success = false;
                                    break;
                                }
                            }
                            catch (exc) {
                                YAHOO.Current.App.dialog(null, 200, 100, true, "<div style=\"text-align:center;\">Only numberic values are allowed...</div>");
                                //alert("Only numberic values are allowed...");
                                success = false;
                                break;
                            }
                        }
                        if (!success) {
                            item.focus();
                            return false;
                        }
                        //  First check if integer
                        var valueInt = "";
                        try {
                            valueInt = parseInt(tmp, 10);
                            if (isNaN(valueInt)) {
                                YAHOO.Current.App.dialog(null, 200, 100, true, "<div style=\"text-align:center;\">Not a valid value...</div>");
                                return false;
                            }
                        }
                        catch (ex) {
                            YAHOO.Current.App.dialog(null, 200, 100, true, "<div style=\"text-align:center;\">Numeric Values only...</div>");
                            //alert("Numeric Values only...");
                            return false;
                        }
                        //  Now check if round value
                        if (tmp.search(/^\d+$/) == -1) {
                            YAHOO.Current.App.dialog(null, 200, 100, true, "<div style=\"text-align:center;\">Gift Card amount must be a round number</div>");
                            //alert("Gift Card amount must be a round number");
                            item.focus();
                            return false;
                        }

                        if (valueInt < lowInt) {
                            YAHOO.Current.App.dialog(null, 200, 100, true, "<div style=\"text-align:center;\">Sorry, only amounts between " + "$" + lowInt.toString() + ".00 and " + "$" + highInt.toString() + ".00 are allowed...</div>");
                            //alert("Sorry, only amounts between " + "$" + lowInt.toString() + ".00 and " + "$" + highInt.toString() + ".00 are allowed...");
                            return false;
                        }
                        if (valueInt > highInt) {
                            YAHOO.Current.App.dialog(null, 200, 100, true, "<div style=\"text-align:center;\">Sorry, only amount between $" + lowInt.toString() + ".00 and $" + highInt.toString() + ".00 are allowed...</div>");
                            //alert("Sorry, only amount between $" + lowInt.toString() + ".00 and $" + highInt.toString() + ".00 are allowed...");
                            return false;
                        }
                        return true;
                    }
                    else {
                        YAHOO.Current.App.dialog(null, 200, 100, true, "<div style=\"text-align:center;\">Please Enter an Amount...</div>");
                        //alert("Please Enter an Amount...");
                        //document.getElementById("testMe").innerHTML += 1;
                        //item.focus();
                        return false;
                    }
                }
            }
        },
        completeMyOrder: function(operationType, giftCardSKU, minValue, maxValue) {
            var chk = document.getElementById('AgreeCheckBox');
            var cardValue;
            if (chk != null) {
                if (!chk.checked) {
                    YAHOO.Current.App.dialog(null, 500, 100, true, '<div style=\"text-align:center;\">Terms and Conditions Checkbox has not been selected and is required...</div>');
                    //alert('Terms and Conditions Checkbox has not been selected and is required...');
                    return false;
                }
            }
            var amtValue = document.getElementById('AmountValue');
            if (amtValue != null) {
                if (amtValue.type == "hidden") {
                    cardValue = amtValue.value;
                }
                else {
                    if (YAHOO.Current.App.validateNumericAmount(amtValue, minValue, maxValue, amtValue.value)) {
                        //document.location.href = appRoot + 'GreetingCards.mvc/All';
                        //return true;
                        cardValue = amtValue.value;
                    }
                    else {
                        amtValue.focus();
                        return false;
                    }
                }
            }
            else {
                amtValue = document.getElementById('GiftCardValueDropDownList');
                if (amtValue != null) {
                    if (amtValue[amtValue.selectedIndex].text.toLowerCase() == "select") {
                        YAHOO.Current.App.dialog(null, 200, 100, true, "<div style=\"text-align:center;\">Please select an amount...</div>");
                        //alert("Please select an amount...");
                        return false;
                    }
                    else {
                        cardValue = amtValue[amtValue.selectedIndex].value;
                    }
                }
            }
            var thisForm = document.forms[0];
            thisForm.SelectedGiftCardSKU.value = giftCardSKU;
            thisForm.SelectedGiftCardValue.value = cardValue;
            thisForm.OperationTypeHiddenBox.value = operationType;
            thisForm.action = appRoot + 'ShoppingCart/SubmitMerchantDetailForm';

            thisForm.submit();
            //document.location.href = appRoot + 'GreetingCards.mvc/All';
        },
        //        selectaGreetingCard : function(){
        //            document.location.href= appRoot + 'GreetingCards.mvc/All';
        //        },
        merchantVideo: function(videoPath) {
            if (videoPath == "") {
                return false;
            }
            YAHOO.Current.App.dialog(null, 800, 520, true, '<iframe align=\"center\" scrolling=\"auto\" width=\"775px\" height=\"470px\" id=\"MerchantAdVideo\" src=\"' + videoPath + '\"></iframe>');
        },
        SelectContactForDelivery: function() {
            var contactCheckbox = document.getElementById("DeliveryAddressToCheckBox");
            var userContactSelect = document.getElementById("UserContactDropDownList");
            var userContactAddressSelect = document.getElementById("UserContactAddressDropDownList");
            if (contactCheckbox.checked) {
                contactCheckbox.value = "1";
                userContactSelect.style.visibility = "visible";
                userContactAddressSelect.style.visibility = "visible";
                var sourceUrl = appRoot + "Jsons/ChangeUserContactAddress/" + userContactAddressSelect.value;
                ajax(sourceUrl, callback);
            }
            else {
                contactCheckbox.value = "";
                userContactSelect.style.visibility = "hidden";
                userContactAddressSelect.style.visibility = "hidden";
                document.getElementById("createUserContactCheckboxText").innerHTML = "Create As New Contact";
            }
        },
        ChangeUserContact: function() {
            var userContactSelect = document.getElementById("UserContactDropDownList");
            var sourceUrl = appRoot + "Jsons/ChangeUserContact/" + userContactSelect.value;
            ajax(sourceUrl, callback);
        },
        ChangeUserContactAddress: function() {
            var userContactAddress = document.getElementById("UserContactAddressDropDownList");
            var sourceUrl = appRoot + "Jsons/ChangeUserContactAddress/" + userContactAddress.value;
            ajax(sourceUrl, callback);
        },
        ChangeUserPayment: function() {
            var userPaymentMethod = document.getElementById("UserPaymentMethodDropDownList");
            var time = new Date();
            var sourceUrl = appRoot + "Jsons/ChangeUserPayment/" + userPaymentMethod.value + "?time=" + time.valueOf();
            ajax(sourceUrl, callback);
        },
        removeGift: function(id) {
            if (id != null && id != "") {
                var time = new Date();
                var sourceUrl = appRoot + "Jsons/DeleteItem/" + id + "?time=" + time.valueOf();
                ajax(sourceUrl, callback);
            }
        },
        duplicateGift: function(id, msg) {
            var defaultMsg = "If you need to make changes to this duplicate gift (dollar amount, personalization, name, or address),simply select \"change\" beneath the gift card, greeting card, or envelope.";
            var msgToInsert = msg == '' ? defaultMsg : msg;
            if (id != null && id != "") {
                var htmlEls = "<table align='center' style='width:100%;height:100%;border:solid 0px #cccccc;'><tr><td><div style='text-align:center;'>";
                htmlEls += msgToInsert;
                htmlEls += "</div></td></tr><tr><td><div style='text-align:center;'>";
                htmlEls += "<input type='button' value='Continue' onclick='YAHOO.Current.App.processDuplicateGift(1," + id + ");'/>";
                htmlEls += "</div></td></tr></table>";
                YAHOO.Current.App.dialog(null, 450, 150, true, htmlEls);
            }
        },
        processDuplicateGift: function(num, id) {
            removeDialog();
            if (num == 1) {
                var time = new Date();
                var sourceUrl = appRoot + "Jsons/DuplicateItem/" + id + "?time=" + time.valueOf();
                ajax(sourceUrl, callback);
            }
        },
        closeDialog: function() {
            removeDialog();
        },
        redirectHomeDialog: function() {
            removeDialog();
            //            window.location.href = appRoot.replace("https://", "http://") + "Home.mvc";
            var thisForm = document.forms[0];
            thisForm.action = appRoot + "Home";
            thisForm.submit();
        },
        addAnotherGift: function() {
            window.location.href = appRoot + "Merchants/All";
        },
        //        proceedToCheckout: function() {
        //            var giftCardorDeliveryNotCompleteCount = document.getElementById("GiftCardorDeliveryNotCompleteCount").value;
        //            if (giftCardorDeliveryNotCompleteCount > 0) {
        //                YAHOO.Current.App.dialog(null, 250, 125, true, "<div style=\"text-align:center;\">Please select gift card and shipping information for each item</div><br/><div style=\"text-align:center;\"><input type='button' value='Continue' onclick='YAHOO.Current.App.closeDialog();'/></div>");
        //                return false;
        //            }
        //            else {
        //                alert("appRoot: " + appRoot);
        //                alert(appRoot.replace("http:", "https:") + "Order.mvc/BillingAndShipping");
        //                window.location.href = appRoot.replace("http:", "https:") + "Order.mvc/BillingAndShipping";
        //            }
        //        },
        startOver: function() {
            var htmlEls = "<table align='center' style='width:100%;height:100%;border:solid 1px #cccccc;'><tr><td><div style='text-align:center;'>";
            htmlEls += "You have opted to Start Over which will remove this item from your Shopping Cart.  Are you sure this is what you want to do?";
            htmlEls += "</div></td></tr><tr><td><div style='text-align:center;'>";
            htmlEls += "<input type='button' value='Yes' onclick='YAHOO.Current.App.processStartOver(1);'/><input type='button' value='No' onclick='YAHOO.Current.App.processStartOver(0);' />";
            htmlEls += "</div></td></tr></table>";
            YAHOO.Current.App.dialog(null, 450, 150, true, htmlEls);
        },
        processStartOver: function(num) {
            //  num = 1(Yes), 0(No)
            removeDialog();
            if (num == 1) {
                //  This is when we will make a call to controller action to remove item and redirect
                var sourceUrl = appRoot + "Jsons/StartOver";
                ajax(sourceUrl, callback);
            }
        },
        activateTrackListings: function() {
            var item = document.getElementById('merchantInfoPlh');
            if (item != null) {
                tempTabView.set("activeIndex", 0);
            }
        },
        toggleMenu: function(itemNum) {
            if (itemNum != null) {
                var imageItem = document.getElementById("img_" + itemNum);
                var subItem = document.getElementsByTagName("div"); //.getElementsByClassName("sitenav-category-link-container");//.getElementById("sub_" + itemNum);
                if (imageItem != null && subItem != null) {
                    var imageItemsrc = imageItem.src;
                    if (imageItemsrc != null) {
                        var imageItemsrcExt = imageItemsrc.substring((imageItemsrc.lastIndexOf("/") + 1));
                        if (imageItemsrcExt != null) {
                            if (imageItemsrcExt.toLowerCase() == "elbow-plus-nl.gif") {
                                for (var i = 0; i < subItem.length; i++) {
                                    var subAttr = subItem[i].getAttribute("sub");
                                    if (subAttr != null) {
                                        if (subAttr == "sub_" + itemNum) {
                                            subItem[i].style.display = "";
                                        }
                                    }
                                }
                                imageItem.src = imageItemsrc.replace(imageItemsrcExt, "elbow-minus-nl.gif");
                            }
                            else {
                                for (var i = 0; i < subItem.length; i++) {
                                    var subAttr = subItem[i].getAttribute("sub");
                                    if (subAttr != null) {
                                        if (subAttr == "sub_" + itemNum) {
                                            subItem[i].style.display = "none";
                                        }
                                    }
                                }
                                imageItem.src = imageItemsrc.replace(imageItemsrcExt, "elbow-plus-nl.gif");
                                //alert(imageItemsrcExt);
                            }
                        }
                    }
                }
            }
        },
        evaluateToggle: function(display, productCategoryId) {
            if (display) {
                var imageItem = document.getElementById("img_" + productCategoryId);
                var divItems = document.getElementsByTagName("div");
                if (imageItem != null) {
                    var imageItemsrc = imageItem.src;
                    if (imageItemsrc != null) {
                        var imageItemsrcExt = imageItemsrc.substring((imageItemsrc.lastIndexOf("/") + 1));
                        if (imageItemsrcExt != null) {
                            imageItem.src = imageItemsrc.replace(imageItemsrcExt, "elbow-minus-nl.gif");
                            for (var i = 0; i < divItems.length; i++) {
                                var subItem = divItems[i].getAttribute("sub");
                                if (subItem != null) {
                                    var valueMinusSub = subItem.substring((subItem.indexOf("_") + 1));
                                    if (valueMinusSub != null) {
                                        if (valueMinusSub == productCategoryId) {
                                            divItems[i].style.display = "";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else {
                var imageItem = document.getElementById("img_" + productCategoryId);
                var divItems = document.getElementsByTagName("div");
                if (imageItem != null) {
                    var imageItemsrc = imageItem.src;
                    if (imageItemsrc != null) {
                        var imageItemsrcExt = imageItemsrc.substring((imageItemsrc.lastIndexOf("/") + 1));
                        if (imageItemsrcExt != null) {
                            imageItem.src = imageItemsrc.replace(imageItemsrcExt, "elbow-plus-nl.gif");
                            for (var i = 0; i < divItems.length; i++) {
                                var subItem = divItems[i].getAttribute("sub");
                                if (subItem != null) {
                                    var valueMinusSub = subItem.substring((subItem.indexOf("_") + 1));
                                    if (valueMinusSub != null) {
                                        if (valueMinusSub == productCategoryId) {
                                            divItems[i].style.display = "none";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
} ();
