
// asp.net prepends more stuff to the id of a control the farther it's nested within it's parents.
var Master = new Object();
Master.IDPrefix = "ctl00_";                                                     // the prefix asp.net adds to controls IDs on the master page
Master.ChildPagePrefix = "BodyContent_";                                    // the prefix that asp.net adds the control IDs that are contained in <asp:content> tags
Master.RegularPrefix = Master.IDPrefix + Master.ChildPagePrefix;                // the prefix applied to pages without any extra controls like wizards, etc.
Master.WizardPrefix = Master.IDPrefix + Master.ChildPagePrefix + "Wizard1_";    // the prefix used on controls inside a wizard (with the id Wizard1).

Master.AnimatedCart = new Object();
Master.AnimatedCart.Fade = null;               // interval to make cart fade in/out
Master.AnimatedCart.ID = "divAnimatedCart";       // id of the fading cart.

Master.Init = function() {

    try {
        function FadeIn() { StartFade(true); }
        function FadeOut() { StartFade(false); }
        Shared.AttachEvent("divAnimatedCart", "mouseover", FadeIn);
        Shared.AttachEvent("divAnimatedCart", "mouseout", FadeOut);
        Shared.AttachEvent("aCartLink", "mouseover", FadeIn);
        Shared.AttachEvent("aCartLink", "mouseout", FadeOut);
        //Shared.AttachEvent("aPrint", "click", function() { return PrintPage(); });
        var txt = document.getElementById(Master.IDPrefix + "txtSearch");
        Shared.AttachEvent(txt, "focus", function() { ToggleSearchDisplay(true); });
        Shared.AttachEvent(txt, "blur", function() { ToggleSearchDisplay(false); });
    } catch (x) { }

}

Master.CheckAll = function (chk, checks) {

    for (var i = 0; i < checks.length; i++) { if (checks[i] != chk) { checks[i].checked = chk.checked; } }

}


//try {// WRAP IN TRY CATCH, HTTPS PAGES WON'T LOAD SHARED.JS, AND WILL THROW AN ERROR.
    Shared.AddLoadEvent(Master.Init);
//} catch (x) { }


// NOT CURRENTLY USED BUT WORKS!!
function ToggleMenu(CategoryID) {

//    var className = "LeftMenuCurrent";
//    var div = document.getElementById("divCategoryMenu_" + CategoryID);
//    if(div.style.display.toLowerCase() != "none") { div.style.display = "none"; Shared.RemoveClass(div, className); }
//    else { div.style.display = "block"; Shared.AddClass(div, className); }
    //return false;

}

function PrintPage() {

    window.print();
    return false;

}

// Starts cart fade in.
function StartFade(FadeIn) {
//return;
    if(Master.AnimatedCart.Fade != null) { Master.AnimatedCart.Fade.ClearIntervals(); }
    var divCart = document.getElementById(Master.AnimatedCart.ID);
    
    if(divCart.getElementsByTagName("DIV").length > 1) {
        Master.AnimatedCart.Fade = new Fade(document.getElementById(Master.AnimatedCart.ID), false);
        Master.AnimatedCart.Fade.Start(FadeIn);
    }
}

// toggles the default text inside the search box
function ToggleSearchDisplay(bHide) {

    var txt = document.getElementById(Master.IDPrefix + "txtSearch");
    var defaultText = txt.getAttribute("title");
    if(bHide && txt.value == defaultText) { txt.value = ""; }
    else if(!bHide && txt.value == "") { txt.value = defaultText; }

}



/////////////////////////////////START METHODS FOR IN-PAGE SEARCH///////////////////////////////////////////

// Toggle the search box for the in page search
function ToggleItemSearch(bShow) {

    try {
        var div = document.getElementById("divSearchDialog");
        var txt = document.getElementById("txtItemSearch");
        var divToolTip = document.getElementById("divSearchToolTip");
        if(bShow) { 
            
            var coords = Shared.GetPosition(document.getElementById("aSearchLink"));
            coords.x -= Shared.ScrollLeft();
            coords.y -= Shared.ScrollTop();
            
            div.style.top = coords.y.toString() + "px";
            div.style.left = coords.x.toString() + "px";
            div.style.display = "block";

            divToolTip.style.top = (coords.y - 50) + "px";
            divToolTip.style.left = coords.x.toString() + "px";
            divToolTip.style.display = "block";
            txt.focus(); 
        }
        else {
            div.style.display = "none";
            divToolTip.style.display = "none"; 
            txt.value = ""; 
        }
    
    //if an error is encountered, allow link to open.
    } catch(x) { return true; }
    
    // everything executed perfectly. Don't open link.
    return false;
    
}

// Search the list of items for the specified text. 
// bKeepScrolledPosition: bool-determine whether to scroll the page back up to the top of leave it where it is.
function SearchForItem(text, bKeepScrolledPosition) {

    var originalTextLength = text.length;
    text = FormatStringForComparison(text);
    var foundOne = false;
    var validText = (text != "");

    var divParent = document.getElementById("divToSearch");
    if(divParent == null) { return; }
    var divs = divParent.getElementsByTagName("DIV")
    
    function RemoveClass(element) { Shared.RemoveClass(element, "SearchMatch"); }
    
    for(var i = 0; i < divs.length; i++) {
    
        var d = divs[i];
        if(d.className.indexOf("Inline") > -1) {
        
            if(!foundOne && validText) {
            
                var index = FormatStringForComparison(d.innerHTML).indexOf(text);
                if(index == -1) { RemoveClass(d); }
                //if (index != 0) { RemoveClass(d); }
                else {
                    Shared.AddClass(d, "SearchMatch");
                    Shared.ScrollToElement(d);
                    foundOne = true;
                }
                
            } else {
            
                RemoveClass(d);
            
            }
        
        }
    }
    
    if(!validText && !bKeepScrolledPosition) {
    
        Shared.ScrollToElement(document.getElementById("aSearchLink"));
    
    }
}


// removes characters to be ignored from a string when doing the 'in page search'
function FormatStringForComparison(s) {

    return s.toLowerCase().GlobalReplace("'", "").GlobalReplace("\"", "").GlobalReplace(" x ", "").GlobalReplace("-", "");//.GlobalReplace("\(", "").GlobalReplace("\)", "");

}

// toggles the state drop down and text box depending on selected country.
function ToggleState(Country) {

    var drp = document.getElementById(Master.RegularPrefix + "drpState");
    var txt = document.getElementById(Master.RegularPrefix + "txtState");
    
    if(Country == "USA") {
    
        drp.style.display = "inline";
        txt.style.display = "none";
    
    } else {
    
        drp.style.display = "none";
        txt.style.display = "inline";
    
    }

}

// stores the value of a drop down in it's associated text box. Used for things like state that can be a text box or drop down.
function StoreDropSelection(drop) {

    var txt = document.getElementById(drop.id.replace("drp", "txt"));
    txt.value = drop.value;

}

// disables an element
function DisableButton(btn) {

    btn.disabled = true;
    return true;

}


// functionality to open pop ups
function OpenDialog(href, top, left, height, width) {

    try {
        if(top == null) { top = 150; }
        if(left == null) { left = 230; }
        if(height == null) { height = 450; }
        if(width == null) { width = 600; }

        window.open(href, null, 'left=' + left.toString() + ',top=' + top.toString() + ',width=' + width.toString() + ',height=' + height.toString() + ',toolbar=0,titlebar=0,status=0,scrollbars=1,resizable=0,menubar=0,location=0');
        
    } catch(x) {
    
        window.location = sHref;
    
    }
    return false;
}
