var puStatus = 0;
$(document).ready(function() {
    $("#bgPu").click(function() { showPopup(false); });
    $(document).keypress(function(e) { if (e.keyCode == 27 && puStatus == 1) showPopup(false); });
    $("#emailSub").click(function() {
        if (this.value == "Email Address") this.value = "";
    }).blur(function() {
        if (this.value == "") this.value = "Email Address";
    }).keypress(function(e) {
        if (e.which == 13) {
            newsletterPU(this.value);
            return false;
        }
    });
    $("#clkEmSub").click(function() {
        setNewsletter();
        return false;
    }).keydown(function(e) {
        return false;
    }).keyup(function(e) {
        if (e.which == 13) setNewsletter();
        return false;
    });
    $(".signIn").click(function() {
        if ($(this).html() == 'Sign In') {
            signInPU();
            return false;
        }
    });
    if (location.protocol == 'https:') {
        if ($(".signIn").html() == 'Sign In')
            $(".signIn").hide();
    }
});
function setNewsletter() { newsletterPU($("#emailSub").val()); }

function buildTabs() {
    $("#tabs h2").addClass('tabbed').click(function() { openTab($(this)) });
    $('#tabs fieldset').hide();
    openTab($('#tabs h2:first'));
    $('#tabInner').addClass('tabbed');
    $('#tabContent').addClass('tabbed');
    $('#tabContentBottom').addClass('tabbed');
}
function openTab(tab) {
    $('#tabContent').html(tab.next().html());
    $('#tabs h2').removeClass('selected');
    tab.addClass('selected');
}
function prdPgImg(showLrg) {
    if (showLrg) {
        $('#attrTbl').hide();
        $('#mainPic').hide();
        $('#ctl00_content_pnlFlashView').show();
        if ($('#lrgFlashObj').html() == "")
            $('#lrgFlashObj').html(printFlash('/images/viewer/viewer?itemNo=' + itemNo + '&viewType=large&lrgPics=' + lrgPics + '&baseClr=0xEEEEEE', 586, 420, 7));
        return false;
    } else {
        $('#attrTbl').show();
        $('#mainPic').show();
        $('#ctl00_content_pnlFlashView').hide();
    }
}
function recommendedClick() {
    var acc = document.getElementById("recommendedAcc");
    var btn = document.getElementById("recommendedBtn");
    var img = document.getElementById("recommendedImg");

    if (acc.style.display == "none") {
        acc.style.display = "block";
        btn.style.display = "none";
        img.src = "/images/css/recommendedHide.gif";
    } else {
        acc.style.display = "none";
        btn.style.display = "block";
        img.src = "/images/css/recommendedShow.gif";
    }
}
function chgIcon(id, dd) {
    if (dd) {
        var src = dd.value;
        if (id.indexOf("imgHead") > -1)
            src = "head" + dd.value;
        document.getElementById(id).src = '/images/prodimages/' + src + '.jpg';
    }
}
function findObject(id) {
    if (document.getElementById(id)) {
        return document.getElementById(id);
    } else {
        for (var i = 0; i < document.forms[0].elements.length; i++) {
            if (document.forms[0].elements[i].id.indexOf(id) > 0) {
                return document.getElementById(document.forms[0].elements[i].id);
            }
        }
    }
    return false;
}
function toggleMe() {
    var link = document.getElementById("morelink");
    var desc = document.getElementById("descrmore");
    if (desc.style.display == "none") {
        desc.style.display = "block";
        link.src = "/images/site/click_for_less.gif";
    } else {
        desc.style.display = "none";
        link.src = "/images/site/click_for_more.gif";
    }
}
function trim(sString) {
    while (sString.substring(0, 1) == ' ') {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length - 1, sString.length) == ' ') {
        sString = sString.substring(0, sString.length - 1);
    }
    return sString;
}
function brwsPrds(displayType) {
    document.getElementById('brws').style.display = displayType;
}
function thmSize(img, w, h) {
    img.width = w;
    img.height = h;
}
function showRow() {
    var show = 'table-row';
    if (navigator.appName == "Microsoft Internet Explorer")
        show = 'block';
    return show;
}

function validateEmail(x) {
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (filter.test(x)) {
        return true;
    } else {
        return false;
    }
}

function perPage(sel) {
    sel.disabled = true;
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + 1);
    document.cookie = "perPage=" + escape(sel.value) + ";expires=" + exdate.toGMTString();
    sel.options[sel.selectedIndex].text = "Loading...";
    var url = window.location.pathname.split("/");
    var refresh = "";
    for (x = 1; x < url.length; x++) {
        if (x == 2 && url[1] != "search") {
            refresh = refresh + "/1"
        } else if (x == 3 && url[1] == "search") {
            break;
        } else {
            refresh = refresh + "/" + url[x];
        }
    }
    window.location = refresh;
}

function searchSubmit(subType) {
    var srch = '';
    if ($('#ctl00_srch'))
        srch = $('#ctl00_srch').val();
    srch = srch.replace(/#/g, "");
    srch = jQuery.trim(srch);
    if (srch.length > 1) {
        srch = srch.replace(/\//g, "%2F");
        redirectPg(subType, "/search/" + srch);
    }
    return false;
}

function redirectPg(subType, url) {
    if (url != undefined) {
        url = url.replace(/  /g, " ").replace(/  /g, " ").replace(/  /g, " ");
        url = escape(url.replace(/ /g, "+"));
        url = url.replace(/%3F/g, "?");
        url = url.replace(/%3D/g, "=");
        url = url.replace(/%252F/g, "%2F");
        if (subType == "form") {
            var frm = document.forms[0];
            frm.__VIEWSTATE.name = "aspnetForm";
            frm.action = url;
            frm.submit();
        } else {
            window.location = url;
        }
    }
}
function cancelReturn(e) {
    var key = 0;
    if (window.event) {
        key = window.event.keyCode; //IE
    } else if (e.which) {
        key = e.which; //firefox
    }
    if (key == 13) return false;
}

function popUp(strUrl, strWinname, intWidth, intHeight, scrollbars) {
    var h = screen.height;
    if (intHeight > h) { intHeight = h }

    strFeatures = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=" + scrollbars + ",resizable=1,width=" + intWidth + ",height=" + intHeight;
    strWinname = strWinname.replace("-", "");
    sealWin = window.open(strUrl, strWinname, strFeatures);
    self.name = "MAINWINDOW";
}

function formKey(e, input) {
    var key;
    if (window.event) {
        key = window.event.keyCode; //IE
    } else if (e.which) {
        key = e.which; //firefox
    }
    if (key == 13 || key == "13") {
        if (input == "srch") {
            searchSubmit(input);
        } else if (input == "etr") {
            searchSubmit(input);
            return false;
        } else if (input == "eml") {
            //   emailSubmit(input);
            //   return false;
        }
    } else if (input == "srch") {
        sugSubmit(key);
    }
}

var xmlHttp;

function sugSubmit(key) {
    var div = document.getElementById("sg");
    var srchInput = document.getElementById("ctl00_srch");
    if (key == 188) {
        srchInput.value = srchInput.value.replace(/</, ' ');
    }
    if (div != undefined) {
        if (key == 38 || key == 40) {
            hl = sgClr();
            if (key == 38) {
                if (hl < 0) {
                    hl = div.childNodes.length - 2;
                } else {
                    hl = hl - 1;
                }
            } else {
                hl = hl + 1;
            }
            if (hl > -1 && hl < div.childNodes.length - 1) {
                var splt = div.childNodes[hl].innerHTML.split(">");
                if (splt.length > 2) {
                    srchInput.value = splt[splt.length - 1].replace(/&amp;/g, '&');
                    div.childNodes[hl].className = "hl";
                }
            }
            return;
        }

        xmlHttp = GetXmlHttpObject();
        if (xmlHttp == null) return;

        var url = "/scripts/searchSuggest.aspx?q=" + srchInput.value;
        xmlHttp.onreadystatechange = stateChanged;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);

    }
}

function sgClr() {
    var rtn = -1;
    var div = document.getElementById("sg");
    var nodes = div.childNodes.length;
    for (x = 0; x < nodes - 1; x++) {
        if (div.childNodes[x].className == "hl") {
            rtn = x;
        }
        div.childNodes[x].className = "nm";
    }
    return rtn;
}

function BuildDiv(div, rsp) {
    div.innerHTML = '';
    div.style.display = "block";
    var lines = rsp.split("\n");
    for (x = 0; x < lines.length - 1; x++) {
        var att = lines[x].split("\t");
        var lnk = escape(att[0]);
        var rslt = att[1] + " result";
        if (att[1] != 1) rslt = rslt + "s";
        lnk = lnk.replace(/%20/g, '+');
        div.innerHTML = div.innerHTML + "<a href=\"/search/" + lnk + "\" class=\"nm\"><span>" + rslt + "</span>" + att[0] + "</a>";
    }
    div.innerHTML = div.innerHTML + "<a href='javascript:closeSuggest()' class='cls'>Close</a>";
}

function closeSuggest() {
    var el = document.getElementById("sg");
    el.parentNode.removeChild(el);
}

function stateChanged() {
    if (xmlHttp.readyState == 4) {
        var div = document.getElementById("sg");
        div.style.display = "none";
        var rsp = xmlHttp.responseText;

        if (rsp != undefined) {
            if (rsp.length > 2 && rsp.indexOf("HttpUnhandledException") < 0) {
                BuildDiv(div, rsp);
            }
        }
    }
}

function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
