var cookiepage = "/sbd/content/help/cookieerror.html";
var thispage = document.location.toString();
var redirectpage = "";
if (!cookiesEnabled() && thispage.indexOf(cookiepage) < 0) {
    document.cookie = "testcookie=1; path=/; ";
    if (!document.cookie) {
        redirectpage = cookiepage;
    }
}
if ((!document.getElementById) && cookies["UnsupportedBrowser"] != "True" && redirectpage.length <= 0 && thispage.indexOf(cookiepage) < 0) {
    redirectpage = "/sbd/cre/resources/browserupgrade/index.html";
} else {
    if ((!document.getElementById) && cookies["UnsupportedBrowser"] != "True" && thispage.indexOf(cookiepage) < 0 && redirectpage.length > 0) {
        redirectpage = "/sbd/cre/resources/browserupgrade/cookies.html";
    }
}
if (redirectpage.length > 0) {
    document.location = redirectpage;
}
function cookiesEnabled() {
    var a = false;
    if (typeof navigator.cookieEnabled != "undefined") {
        var a = (navigator.cookieEnabled) ? true : false;
    } else {
        document.cookie = "testcookie=1; path=/; ";
        if (document.cookie && thispage.indexOf(cookiepage) < 0) {
            a = true;
        }
    }
    return (a);
}
function getCookies() {
    var b = new Object();
    var c = document.cookie;
    var d = /([^=\sx]+)\s*=\s*([^;]+)\s*/;
    var a;
    while (a = c.match(d)) {
        b[a[1]] = myunescape(a[2]);
        c = c.substr(a.index + a[0].length);
    }
    return b;
}
var cookies = getCookies();

function strGetCookie(a) {
    if (cookies[a]) {
        return cookies[a];
    } else {
        return null;
    }
}
function subSetCookieHelper(c, b, a) {
    if (a == null || a == "") {
        subSetSessionCookie(c, b);
    } else {
        expireDate = new Date();
        msecs = expireDate.getTime();
        expireDate.setTime(msecs + (a * 365 * 24 * 3600 * 1000));
        expireDate = expireDate.toGMTString();
        document.cookie = escape(c) + "=" + escape(b) + ";expires=" + expireDate + ";path=/";
    }
}
function subSetSessionCookie(b, a) {
    document.cookie = escape(b) + "=" + escape(a) + "; path=/";
}
function getImageCookieValue(g, a, d, b) {
    var c = "";
    if ((d == "null") && (b == "null")) {
        subSetCookieHelper(g, "on");
    } else {
        if ((d == "null") && ((b == "on") || b == "off")) {
            c = b;
        } else {
            if (d != b) {
                subSetCookieHelper(g, d);
            }
        }
    }
    return c;
}

var debugMessages = new Array(0);

function stopErrors() {
    return true;
}
function errRedirect(a) {
    window.onerror = stopErrors;
    window.location = a;
}
var globalTimeout = propertyValues.ajaxTimeout;

function getServerName() {
    var c = window.location.toString();
    var b = /\/\/([^\/]+)\/?/;
    var a = c.match(b);
    if (a != null) {
        return a[1];
    }
}
var serverName = getServerName();
var isSecure = (location.protocol === "https:");

function jsSubmitLink(a) {
    if (a.indexOf("http") != 0) {
        a = "http://" + serverName + a;
    }
    window.location = a;
    document.body.style.cursor = "wait";
    status = propertyValues.loading;
}
var isOverlay = false;
try {
    if (top.location.href != window.location.href) {
        isOverlay = true;
    }
} catch (e) {
    isOverlay = true;
}
function sgetUpHeader(c) {
	alert('setupheader');
    if (c != "overlay" && isOverlay && !(querystring.framebreak && querystring.framebreak == "false")) {
        top.location.href = window.location.href;
        return false;
    }
    var g = strGetCookie("WelcomeMsg");
    if (!isguest("StaplesUser") || (strGetCookie("STAPLES50_REENTRY") == "null" && strGetCookie("STAPLES_REENTRY_OFF") != "null")) {
        var v = $("#wmsg");
        $("#hlogout").html('<a href="/office/supplies/StaplesLogoff?langId=-1&storeId=10001&catalogId=10051" rel="nofollow"><b>' + propertyValues.logout + "</b></a>");
        var n = 0;
        if (!isguest("StaplesUserInfo") && pageId == "samhome" && propertyValues.samHeaderShowuserdataSwitch == "ON") {
            var k = '<ul><li id="liuserinfo"><b>Account Manager:</b> ';
            if (staplesUserInfo.acctmgremail && staplesUserInfo.acctmgremail !== "") {
                if (!isKiosk) {
                    k += '<a href="mailto:';
                    k += staplesUserInfo.acctmgremail;
                    k += '" id="acctmgremail">';
                    k += staplesUserInfo.acctmgremail;
                    k += "</a> ";
                } else {
                    k += staplesUserInfo.acctmgremail + " ";
                }++n;
            }
            if (staplesUserInfo.acctmgrphone && staplesUserInfo.acctmgrphone !== "") {
                k += formatUSPhone(staplesUserInfo.acctmgrphone);
                ++n;
            }
            if (staplesUserInfo.acctmgrext && staplesUserInfo.acctmgrext !== "") {
                k += " ext ";
                k += staplesUserInfo.acctmgrext;
                ++n;
            }
            k += "</li></ul>";
            if (n > 0) {
                $("#bmsging").html(k);
            }
        }
        if (n == 0) {
            if (g != null && !(g == '""')) {
                var b = propertyValues.welcomeuserloggedin.replace(/\{0\}/g, myunescape(g));
                $("#liwmsg").html('<div id="lginwmsg">' + b + "</div>");
            } else {
                v.html(propertyValues.welcomedefaultloggedin);
            }
        }
        v.attr("href", "#").click(function () {
            return false;
        });
    } else {
        $("#hlogin").html(propertyValues.login);
    }
    var l = parseCart("StaplesCart");
    var p = l["Items"].split(" ", 2);
    var h = $("#cart");
    var u = $("#carti");
    var t = $("#cartp");
    var a = u.find("a > img");
    var d = h.find("dt > a").attr("href");
    var m = propertyValues.checkoutExclusionPages.split(",");
    if (c == "overlay") {
        h.click(function () {
            parent.closeoverlay();
            return false;
        });
    } else {
        if (isValueInArray(m, pageId)) {
            h.addClass("cartclickdisabled");
            a.attr("src", "/sbd/img/ico/hdr_cart_disabled.png");
            h.click(function () {
                return false;
            });
        } else {
            if (pageId == "partner") {
                h.click(function () {
                    window.parent.location = d;
                    return false;
                });
            } else {
                h.click(function () {
                    window.location = d;
                    return false;
                });
            }
        }
    }
    $("#hcartitems").append(p[0]).after(" " + p[1]);
    fillElement("hcarttotal", l["Total"]);
    var r = t.find("a").totalWidth() + 16;
    var s = t.totalWidth();
    if (r > s) {
        var j = u.totalWidth() + r + 2;
    } else {
        var j = u.totalWidth() + s + 2;
    }
    var q = $("#hdr").superWidth() - j - $("#logo").superWidth();
    $("#searchkey").css("width", q - 174);
    $("#sform").css("width", q - 68);
    h.css("width", j);
    var o = (propertyValues.searchAutocomplete == "ON") ? true : false;
    if (o && c != "overlay" && pageId != "partner" && !isOverlay) {
        $("#searchkey").autocomplete("/autocomplete", {
            selectFirst: false,
            inputClass: "",
            loadingClass: ""
        }).result(function (x, w) {
            setAutoSearch(w);
        });
    }
    if (staplesUserInfo.mypage) {
        $("#mypage").removeClass("hide");
        $("#mccnt_mpg").removeClass("hide_simple");
    } else {
        $("#mypage").remove();
        $("#mccnt_mpg").remove();
    }
    if (isMsie7) {
        $("#chead > ul > li").hover(function () {
            $(this).children("a").addClass("ie7navitem").end().children("ul").addClass("ie7navUL");
        }, function () {
            $(this).children("a").removeClass("ie7navitem").end().children("ul").removeClass("ie7navUL");
        });
    }
}
function setAutoSearch(a) {
    $("#autocompletesearchkey").remove();
    $("#hsearch").append("<input type='hidden' id='autocompletesearchkey' name='autocompletesearchkey' value='" + a + "' />");
    formSub("hsearch");
}

var onloadfuncs = new Array();

function addLoadEvent(a) {
    if (onloadfuncs.length == 0) {
        if (typeof window.onload != "function") {
            onloadfuncs[0] = window.onload;
            onloadfuncs[1] = a;
            window.onload = null;
        } else {
            onloadfuncs[0] = a;
        }
    } else {
        onloadfuncs[onloadfuncs.length] = a;
    }
}
function onloadwindowfuncs() {
    for (var a in onloadfuncs) {
        if (onloadfuncs[a]) {
            onloadfuncs[a]();
        }
    }
}
function activateActiveX() {
    if (!document.getElementsByTagName || !document.body.outerHTML || !document.compatMode) {
        return false;
    }
    var b = new Array("object", "applet");
    var d = b.length;
    while (d-- > 0) {
        var g = document.getElementsByTagName(b[d]);
        var c = g.length;
        while (c-- > 0) {
            var h = "";
            var a = g[c].childNodes.length;
            while (a-- > 0) {
                h += g[c].childNodes[a].outerHTML;
            }
            g[c].outerHTML = g[c].outerHTML.replace("</" + b[d].toUpperCase() + ">", h + "</" + b[d].toUpperCase() + ">");
        }
    }
}
addLoadEvent(activateActiveX);
$(document).ready(onloadwindowfuncs);

function myunescape(b) {
    b = "" + b;
    while (true) {
        var a = b.indexOf("+");
        if (a < 0) {
            break;
        }
        b = b.substring(0, a) + "%20" + b.substring(a + 1, b.length);
    }
    return unescape(b);
}
function getArgs() {
    var b = new Object();
    var h = location.search.substring(1);
    var g = h.split("&");
    for (var c = 0; c < g.length; c++) {
        var j = g[c].indexOf("=");
        if (j == -1) {
            continue;
        }
        var a = g[c].substring(0, j);
        var d = g[c].substring(j + 1);
        b[a] = myunescape(d);
    }
    return b;
}
var querystring = getArgs();
addLoadEvent(function () {
    if (querystring.debugmsg) {
        $("#debug").append("<br />DEBUGMSG: " + querystring.debugmsg);
    }
});

function parseCart(h) {
    var b = new Object();
    try {
        var c = cookies[h];
        var d = /^([^\;]+)\;([^\;]+)\;([^\;]+)$/;
        var a = c.match(d);
        if (a != null) {
            b["OrderID"] = a[1];
            b["Items"] = a[2];
            b["Total"] = a[3];
        } else {
            b["OrderID"] = "0";
            b["Items"] = "0 items";
            b["Total"] = "$0.00";
        }
    } catch (g) {
        b["OrderID"] = "0";
        b["Items"] = "0 items";
        b["Total"] = "$0.00";
    }
    return b;
}
function isguest(a) {
    try {
        if (cookies[a] != null && cookies[a] != "" && cookies[a] != "guest") {
            return false;
        } else {
            return true;
        }
    } catch (b) {}
}
function fillStaplesUserInfo(cookieName) {
    var returnObject = new Object();
    var jsonStr = strGetCookie(cookieName);
    if (jsonStr) {
        returnObject = eval("(" + jsonStr + ")");
    }
    return returnObject;
}
function setHomePage() {
    var c;
    var a = false;
    if ($("#makehomepage").is(":checked")) {
        a = true;
        c = "/office/supplies/StaplesMemberUnsecure?change=startingPage&reqStartingPage=samhome&URL=makehomepage&errorUrl=makehomepage&storeId=10001&langId=-1&catalogId=10051";
    } else {
        c = "/office/supplies/StaplesMemberUnsecure?change=startingPage&reqStartingPage=home&URL=makehomepage&errorUrl=makehomepage&storeId=10001&langId=-1&catalogId=10051";
    }
    $("#makehomepage").attr("disabled", true);
    try {
        $.ajax({
            url: c,
            type: "GET",
            dataType: "html",
            error: function () {
                $("#makehomepage").removeAttr("disabled");
                a ? $("#makehomepage").removeAttr("checked") : $("#makehomepage").attr("checked", true);
            },
            success: function (d) {
                $("#makehomepage").removeAttr("disabled");
            }
        });
    } catch (b) {}
}
var staplesUserInfo = new Object();
staplesUserInfo = fillStaplesUserInfo("StaplesUserInfo");
addLoadEvent(function () {
    $("#searchkey").focus();
});
function setUpSamHead() {
    var d = "";
    if (propertyValues.samHeaderMakestartpageSwitch == "ON") {
        if (staplesUserInfo.HOMEPAGE == "samhome") {
            chkBoxHtml = '<input type="checkbox" name="makehomepage" class="checkbox" checked="checked"  id="makehomepage" onclick="javascript:setHomePage();">';
        } else {
            chkBoxHtml = '<input type="checkbox" name="makehomepage" class="checkbox"  id="makehomepage" onclick="javascript:setHomePage();">';
        }
        var c = '<li id="mtmshp">';
        c += '<form name="homepageform1" id="homepageform1">';
        c += chkBoxHtml;
        c += '<label for="makehomepage" class="checklabel">Make this my Staples start page</label>';
        c += "</form></li>";
        $("#bosts").replaceWith(c);
    } else {
        d = "width:392px";
    }
    if (propertyValues.samHeaderShowuserdataSwitch == "ON") {
        var a = strGetCookie("DirectCustomerNumber");
        var g = staplesUserInfo.tier;
        var b = '<li id="usrreward" ';
        b += 'class="' + g + '" ';
        if (d != "") {
            b += 'style="' + d + '" ';
        }
        b += '><span class="usrinfo">';
        if (staplesUserInfo.custfname && staplesUserInfo.custfname !== 0) {
            b += '<span class="usrpgnme">';
            b += staplesUserInfo.custfname;
            b += "&rsquo;s Page</span>";
        }
        if (a != null) {
            b += '<span class="rewardnum"><strong>Rewards #: </strong>' + a + "</span>";
        }
        b += "</span></li>";
        $("#ffd").replaceWith(b);
    }
}

var sidenavOpenState = false;
var sidenavFlyoutOpenState = false;
var showap;
var showallprodsUl;
var leftnavLi;

function setUpSAP() {
    var a = $("#showallprods");
    showallprodsUl = $("#showallprods > ul");
    leftnavLi = $("#showallprods > ul > li");
    showap = $("#showap");
    $("#showap").click(function () {
    	return false;
    });
/*    } else {
        if (pageId == "partner") {} else {
            showap.click(function () {
                if (sidenavOpenState) {
                    closeShowAllProducts();
                } else {
                    sidenavOpenState = true;
                    $(this).addClass("showapOn");
                    showallprodsUl.addClass("showul");
                    if (isMsie6) {
                        $("select").removeClass("visible").addClass("invisible");
                    }
                    $(document).bind("click.showap", function (g) {
                        var c = $(g.target);
                        var d = c.parents();
                        var b = true;
                        d.each(function () {
                            if (this.tagName == "DIV" && $(this).attr("id") == "showallprods") {
                                b = false;
                                return false;
                            }
                        });
                        if (b) {
                            closeShowAllProducts();
                        }
                    });
                }
                return false;
            });
        }
    }*/
}
var ajaxFlyouts = false;
//var ajaxFlyouts = ((pageId == "home" && propertyValues.homePageFlyoutAjaxSwitch == "ON") || (pageId != "partner" && pageId != "home" && propertyValues.nonHomePageFlyoutAjaxSwitch == "ON"));
if (ajaxFlyouts) {
    addLoadEvent(setupLeftNav);
}
var footerNavHolder;

function setupLeftNav() {
    footerNavHolder = $("#sideNavLinks");
    if (footerNavHolder) {
        var a = "/office/supplies/StaplesUSB2CPAS/gen/allproductsnavinc.jsp?pageMode=standalone&DC_lang=-1&storeId=10001&catalogId=10051";
        try {
            $.ajax({
                url: a,
                type: "GET",
                dataType: "html",
                error: function () {},
                success: function (c) {
                    footerNavHolder.append(c);
                }
            });
        } catch (b) {}
    }
}
function setUpSidenav() {
    if (typeof(leftnavLi) == "undefined") {
        setUpSAP();
    }
    var a = $("#showallprods");
    showallprodsUl = $("#showallprods > ul");
    showap = $("#showap");
    leftnavLi.hoverIntent({
        sensitivity: 1,
        interval: 1,
        over: function () {
            openFlyout($(this));
        },
        timeout: 0,
        out: function () {
            closeFlyout($(this));
        }
    });
}
function openFlyout(d) {
    var a = $(d);
    a.addClass("selected");
    var g = a.data("flyout");
    if (ajaxFlyouts) {
        if (!g) {
            var c = (a.attr("id").replace(/(FO)/g, "div"));
            var b = $("#" + c);
            a.data("flyout", b);
            a.append(b);
        } else {
            a.append(g);
        }
        showFlyOut(a);
    } else {
        if (!g) {
            a.data("flyout", a.find("div.flyout"));
        }
        if (!$(g)[0]) {} else {
            showFlyOut(a);
        }
    }
}
function closeShowAllProducts() {
    if (!sidenavFlyoutOpenState) {
        sidenavOpenState = false;
        if (isMsie6 && !$(".showallprods > ul > li > div.flyout:not(.hide)")[0]) {
            $("select").removeClass("invisible").addClass("visible");
        }
        showap.removeClass("showapOn");
        showallprodsUl.removeClass("showul");
        $(document).unbind("click.showap");
    }
}
function closeFlyout(b) {
    b.removeClass("selected");
    var a = b.data("flyout");
    if (a) {
        a.addClass("hide");
        if (ajaxFlyouts) {
            footerNavHolder.append(a);
        }
        sidenavFlyoutOpenState = false;
    } else {
        sidenavFlyoutOpenState = false;
    }
}
function showFlyOut(g) {
    sidenavFlyoutOpenState = true;
    leftnavLi.find("div.flyout").addClass("hide");
    var j = g.data("flyout");
    if (!g.data("lazyloaded")) {
        j.find("img[src$='/sbd/img/bg/blank.gif'][name]").each(function () {
            $(this).attr("src", $(this).attr("name"));
            $(this).removeAttr("name");
        });
        g.data("lazyloaded", true);
    }
    if (!g.data("positioninfo")) {
        var a = {
            offsetTop: g.offset().top,
            positionTop: g.position().top,
            totalHeight: g.totalHeight(),
            flyoutDivTotalHeight: j.totalHeight(),
            divitObj: j.find("span.divit")
        };
        g.data("positioninfo", a);
    } else {
        var a = g.data("positioninfo");
    }
    var d = 41;
    var c = -1;
    var h = $(window).scrollTop() - a.offsetTop + a.positionTop;
    if (h + 10 > c) {
        c = h + 10;
    }
    if (a.positionTop < c) {
        c = a.positionTop - 10;
    }
    if ((c + a.flyoutDivTotalHeight) < (a.totalHeight + a.positionTop + 6)) {
        c = (a.totalHeight + a.positionTop + 0) - a.flyoutDivTotalHeight;
    }
    var b = a.positionTop - c - 10 + (a.totalHeight / 2);
    a.divitObj.attr("style", "top:" + b + "px");
    j.attr("style", "top:" + c + "px").attr("class", "flyout");
    if (isMsie6 || isMsie7) {
        g.find("a.closer").attr("style", "right:5px");
    }
}
function formskeypress(e) {
    if (window.event) {
        if (window.event.keyCode == 13) {
            window.event.returnValue = false;
            if (window.event.srcElement.form.action) {
                if (!window.event.srcElement.form.enteroverride) {
                    formSub(window.event.srcElement.form.name);
                } else {
                    eval(window.event.srcElement.form.enteroverride.value);
                }
            }
        }
    } else {
        if (e.which == 13) {
            e.preventDefault();
            if (e.target.form.action) {
                if (!e.target.form.enteroverride) {
                    formSub(e.target.form.name);
                } else {
                    eval(e.target.form.enteroverride.value);
                }
            }
        }
    }
}
function formsenterhook() {
    if (document.getElementById) {
        if (document.captureEvents) {
            document.captureEvents(Event.KEYPRESS);
        }
        for (f = 0; f < document.forms.length; f++) {
            for (i = 0; i < document.forms[f].elements.length; i++) {
                if (document.forms[f].elements[i].type != "textarea") {
                    document.forms[f].elements[i].onkeypress = formskeypress;
                }
            }
        }
        var b = document.getElementsByTagName("a");
        for (i = 0; i < b.length; i++) {
            if (b[i].className.match(/^d\d+$/)) {
                b[i].onkeypress = buttonskeypress;
            }
        }
    }
}
//addLoadEvent(formsenterhook);

function buttonskeypress(a) {
    if (window.event) {
        if (window.event.keyCode == 32) {
            window.event.returnValue = false;
            if (window.event.srcElement.href > "") {
                window.location = window.event.srcElement.href;
            }
        }
    } else {
        if (a.which == 32) {
            a.preventDefault();
            if (a.target.href > "") {
                window.location = a.target.href;
            }
        }
    }
}
var isKiosk = false;
/*if (cookies["KIOSK_NBR"] || cookies["STORE_NBR"]) {
    isKiosk = true;
    document.write("<scr" + 'ipt src="/sbd/js/kiosk.js" type="text/javascript"></scr' + "ipt>");
}*/
function qtyOne(a) {
    if (a.value == "") {
        a.value = "1";
    }
    a.select();
}
function autoclear(b, a) {
    if (b.value == a) {
        b.value = "";
    }
}
function formReset(fName) {
    eval("document." + fName + ".reset();");
}
var submittedformcount = 0;
function setFormAction(a) {
    if (this != null && a.action != "") {
        a.action = "";
    }
}
function disableSrcForElement(a) {
    if (a != null && a.src != "") {
        a.src = "";
    }
}
function redirect(a) {
    location.replace(a);
}
function clearElementValue(a) {
    if (a != null) {
        a.value = "";
    }
}
var userAgent = navigator.userAgent.toLowerCase();
var isMsie = /msie/.test(userAgent) && !/opera/.test(userAgent);
var isMsie6 = /msie 6/.test(userAgent) && !/opera/.test(userAgent);
var isMsie7 = /msie 7/.test(userAgent) && !/opera/.test(userAgent);
var isFF2 = /firefox/.test(userAgent) && /2\.0/.test(userAgent);

if (isMsie) {
    addLoadEvent(function () {
        $("div.p01 select").hover(function () {
            $(this).css("width", "auto");
        }, function () {
            $(this).css("width", "");
        });
    });
}
jQuery.fn.superWidth = function () {
    if (isMsie6 && false) {
        var c = parseInt(this.css("border-left-width"));
        var b = parseInt(this.css("border-right-width"));
        var d = parseInt(this.css("padding-left"));
        var a = parseInt(this.css("padding-right"));
        c = c ? c : 0;
        b = b ? b : 0;
        d = d ? d : 0;
        a = a ? a : 0;
        return this.width() + c + b + d + a;
    } else {
        return this.width();
    }
};
jQuery.fn.superHeight = function () {
    if (isMsie6 && false) {
        return this.totalHeight();
    } else {
        return this.height();
    }
};
jQuery.fn.totalWidth = function () {
    var c = parseInt(this.css("border-left-width"));
    var b = parseInt(this.css("border-right-width"));
    var d = parseInt(this.css("padding-left"));
    var a = parseInt(this.css("padding-right"));
    c = c ? c : 0;
    b = b ? b : 0;
    d = d ? d : 0;
    a = a ? a : 0;
    return this.width() + c + b + d + a;
};
jQuery.fn.totalHeight = function () {
    var a = parseInt(this.css("border-top-width"));
    var b = parseInt(this.css("border-bottom-width"));
    var c = parseInt(this.css("padding-top"));
    var d = parseInt(this.css("padding-bottom"));
    a = a ? a : 0;
    b = b ? b : 0;
    c = c ? c : 0;
    d = d ? d : 0;
    return this.height() + a + b + c + d;
};
function s1trip(b, d) {
    var a = "";
    for (var c = 0; c < b.length; c++) {
        if (d.indexOf(b.charAt(c)) == -1) {
            a += b.charAt(c);
        }
    }
    return a;
}
function s1eparate(b, d) {
    b = "" + b;
    var a = "";
    for (var c = 0; c < b.length; c++) {
        if (c != 0 && (b.length - c) % 3 == 0) {
            a += d;
        }
        a += b.charAt(c);
    }
    return a;
}
