function sfHover() {
    try {
        var sfEls = document.getElementById('nav').getElementsByTagName('LI');
        for (var i = 0; i < sfEls.length; i++) {
            sfEls[i].onmouseover = function () {
                this.className += ' sfhover';
            }
            sfEls[i].onmouseout = function () {
                this.className = this.className.replace(new RegExp(' sfhover\\b'), '');
            }
        }
    } catch (e) {}
}

function initAjaxProgress() {
    try {
        var pageHeight = (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;
        var pageWidth = $(window).width();
        var bg = document.getElementById('ajaxProgressBg');
        bg.style.height = (pageHeight + 1000) + 'px';
        bg.style.width = pageWidth + 'px';
        reposAjaxProgress();
        window.onscroll = reposAjaxProgress;
        window.onresize = reposAjaxProgress;
    } catch (e) {
        showError(e);
    }
}

function reposAjaxProgress() {
    var div = document.getElementById('ajaxProgress');
    var st = document.body.scrollTop;
    if (st == 0) {
        if (window.pageYOffset) st = window.pageYOffset;
        else st = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
    }
    div.style.top = 150 + st + "px";
    document.getElementById("ajaxProgressBg").style.left = "0px";
}

function joinEmail() {
    var address = $(".frmJoin .txtInput").val();
    if (address == "Enter Email Address" || address == "" || !validateEmail(address)) {
        alert("Please enter a valid e-mail address.");
    } else {
        window.open("http://visitor.constantcontact.com/d.jsp?ea=" + address + "&go=Join&m=1101657251422&p=oi", "_blank")
        try {
            var vt = visitorType();
            var c = getCookie("SWFA.TV");
            if (c != "" && Number(c) > 1) {
                vt += " : " + c;
            }
            pageTracker._trackEvent("Email Optin", "SWFA", vt, 1);
        } catch (e) {
            showError(e);
        }
    }
}

function validateEmail(s) {
    var regexp = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    return regexp.test(s);
}

function trapFormKeySubmitEvent(element, event) {
    if (event.keyCode == 13) {
        return false;
    } else {
        return true;
    }
}

function ajaxUpdateEndRequestHandler(sender, e) {
    $.Lightbox.reinitialize();
}

function TreePanel_onNodeSelect(sender, eventArgs) {
    try {
        var node = eventArgs.get_node();
        pageTracker._trackEvent("Category Nav", node.Text, visitorType(), 1);
    } catch (e) {
        showError(e);
    }
}

function TreePanel_Load(sender, eventArgs) {
    try {
        $(".CatTreeView > div:visible:last").css("background-image", "none");
    } catch (e) {
        showError(e);
    }
}

function visitorType() {
    var type;
    var c = getCookie("SWFA.C");
    var fv = getCookie("SWFA.FV");
    var lv = getCookie("SWFA.LV");
    if (c != "") {
        type = c;
    } else if (fv != "" && lv != "") {
        type = (fv == lv) ? "New" : "Returning";
    } else {
        type = "New";
    }
    return type;
}

function gaDeleteBasketItem(category, sku) {
    var deleteItem = confirm('Are you sure you want to remove this item from your basket?');
    if (deleteItem) {
        pageTracker._trackEvent("Delete Item", category, sku, 1);
    }
    return deleteItem;
}

function gaMoveToWishlist(category, sku) {
    pageTracker._trackEvent("Wishlist", category, sku, 1);
}

function gaClearBasket(total) {
    var choice = confirm("Are you sure you want to clear your basket?");
    if (choice) {
        pageTracker._trackEvent("Clear Cart", visitorType(), "Amount in Cart - " + total, 1);
    }
    return choice;
}

function gaShippingCalculation() {
    try {
        var zip = $(".txtShipEstimateZip").val();
        if (zip != undefined && zip != "") {
            pageTracker._trackEvent("Shipping Estimate", zip, visitorType(), 1);
        }
    } catch (e) {
        showError(e);
    }
}

function showError(e) {
    if (DEBUG) {
        var s = new String();
        s += "JavaScript Error:\n";
        for (var item in e) {
            s += item + ": " + e[item] + "\n";
        }
        alert(s);
    }
}

function getCookie(name) {
    if (document.cookie.length > 0) {
        var idxStart = document.cookie.indexOf(name + "=");
        if (idxStart != -1) {
            idxStart = idxStart + name.length + 1;
            idxEnd = document.cookie.indexOf(";", idxStart);
            if (idxEnd == -1) {
                idxEnd = document.cookie.length;
            }
            return unescape(document.cookie.substring(idxStart, idxEnd));
        }
    }
    return "";
}

function emailOptIn() {}

function validatePhotoContestAgreement(source, args) {
    args.IsValid = $(".cbPhotoContestAgreement input:checked").length;
}
var DEBUG = false;
var strSealURL = "weblogos.dnb.com";
var sealID = "41A44EDC-EFF8-46B2-ACA4-D4C9697EE558";
var strDuns = "053662094";
var strRWSPath = "smallbusiness.dnb.com";
var strCountry = "US";
var increment = "true";
var strLang = "EN";
$(function() {
    initAjaxProgress();
    if (window.attachEvent) {
        sfHover();
    }

    $(".dblink a").bind("click", function () {
        PopupWindow("https://smallbusiness.dnb.com/ePlatform/servlet/sealtarget?storeId=10001&Language=EN&sealID=41A44EDC-EFF8-46B2-ACA4-D4C9697EE558&key=053662094&country=US&cmeid=EOR100276", 500, 336);
    });

    $("#productCategoryList .txtSearch").focus(function () {
        if (this.value == "Product Search") {
            this.value = "";
        }
    });
    $("#productCategoryList .txtSearch").blur(function () {
        if (this.value == "") {
            this.value = "Product Search";
        }
    });
    $(".frmJoin .email").focus(function () {
        if (this.value == "Enter Email Address") {
            this.value = "";
        }
    });
    $(".frmJoin .email").blur(function () {
        if (this.value == "") {
            this.value = "Enter Email Address";
        }
    });
    $("#nav > ul > li").each(function (index) {
        var menuButton = jQuery(this);
        var menu = menuButton.find("ul");
        if (menu.length > 0) {
            if (menu.width() < menuButton.width()) {
                menu.css("width", menuButton.width() + "px");
            }
        }
    });
    $(".tabbednav .tabs").tabs();
    $("a[rel='external']").each(function (index) {
        jQuery(this).attr("target", "_blank");
    });
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(ajaxUpdateEndRequestHandler);
});

function PopupWindow(src, w, h) {
    try {
        var left = (screen.width / 2) - (w / 2);
        var top = (screen.height / 2) - (h / 2);
        window.open(src, null, "width=" + w + ",height=" + h + ",left=" + left + ",top=" + top + "status=yes,toolbar=no,menubar=no,location=no");
        return false;
    } catch (e) {
        return true;
    }
}

function rssSubscribe() {
    var email = $("#txtSampleListRssEmail").val();
    if (email == "") {
        alert("Please enter a valid email address");
        $("#txtSampleListRssEmail").focus();
    } else {
        var src = "http://feedburner.google.com/fb/a/mailverify?uri=TheSampleList&loc=en_US&email=" + escape(email);
        PopupWindow(src, 600, 500);
    }
    return false;
}

function showTellFriendMessage(e1) {
    var link = $(e1);
    var target = "#tafMessage";
    $(e1).hide();
    $(target).show();
    setTimeout("$('" + target + " textarea').focus()", 100);
    return false;
}
