
// convert all characters to lowercase to simplify testing
var agt = navigator.userAgent.toLowerCase();

// *** BROWSER VERSION ***
// Note: On IE5, these return 4, so use is_ie5up to detect IE5.
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);

// Note: Opera and WebTV spoof Navigator.  We do strict client detection.
// If you want to allow spoofing, take out the tests for opera and webtv.
var is_nav = ((agt.indexOf('mozilla') != -1) && (agt.indexOf('spoofer') == -1)
            && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera') == -1)
            && (agt.indexOf('webtv') == -1) && (agt.indexOf('hotjava') == -1));
var is_nav2 = (is_nav && (is_major == 2));
var is_nav3 = (is_nav && (is_major == 3));
var is_nav4 = (is_nav && (is_major == 4));
var is_nav4up = (is_nav && (is_major >= 4));
var is_navonly = (is_nav && ((agt.indexOf(";nav") != -1) ||
                        (agt.indexOf("; nav") != -1)));
var is_nav6 = (is_nav && (is_major == 5)); var is_nav6up = (is_nav && (is_major >= 5));
var is_gecko = (agt.indexOf('gecko') != -1);

var is_mac = (agt.indexOf("mac") != -1);

var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3 = (is_ie && (is_major < 4));
var is_ie4 = (is_ie && (is_major == 4) && (agt.indexOf("msie 5") == -1));
var is_ie4up = (is_ie && (is_major >= 4));
var is_ie5 = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0") != -1));
var is_ie5_5 = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") != -1));
var is_ie5up = (is_ie && !is_ie3 && !is_ie4);
var is_ie5_5up = (is_ie && !is_ie3 && !is_ie4 && !is_ie5);
var is_chrome = (agt.indexOf("chrome") != -1);

// Scroll Mode (crawl or jump)
var ScrollMode = new Object();
ScrollMode.CRAWL = 'crawl';
ScrollMode.JUMP = 'jump';

function showDisplayBox(id) {
    if (!id) { return; }
    var divs = document.getElementsByTagName("div");
    for (var i = 0; i <= divs.length; i++) {
        if (divs[i]) {
            if (divs[i].className == "DisplayBox") {
                divs[i].className = "hide";
                if (divs[i].style && divs[i].style.visibility) {
                    divs[i].style.visibility = "hidden";
                }
            }
        }
    }
    var showBox = document.getElementById(id);
    if (showBox) {
        showBox.className = "DisplayBox";
        if (showBox.style && showBox.style.visibility) {
            showBox.style.visibility = "visible";
        }
    }
    return false;
}

function showElement(id) {
    if (!id) { return; }
    var elem = d(id);

    if (elem) {
        elem.className = "show";
        if (elem.style && elem.style.visibility) {
            elem.style.visibility = "visible";
        }
    }
}

function hideElement(id) {
    if (!id) { return; }
    var elem = d(id);

    if (elem) {
        elem.className = "hide";
        if (elem.style && elem.style.visibility) {
            elem.style.visibility = "hidden";
        }
    }
}

function clickButton(id) {
    var button = document.getElementById(id);
    if (button) {
        if (is_gecko && button.href.indexOf("javascript") == 0) { eval(button.href); }
        else { button.click(); }
    }
    else {
        alert('Button not found: ' + id);
    }
}

// shortcut method
function d(id) {
    return document.getElementById(id);
}

// selected value
function sv(id) {
    var element = d(id);
    var options, i;
    if (element) {
        options = element.getElementsByTagName("option");
        for (i = 0; i < options.length; i++) {
            var option = element.getElementsByTagName("option")[i];
            if (option.selected) {
                return option.value;
            }
        }
    }
    return null;
}

// set html
function sh(id, content) {
    if (d(id)) { d(id).innerHTML = content; }
    //else { alert(id + ' not found'); }
}

function loadWindow() {
    if (d("ProductMoreInfoContent")) {
        jumpToHash();
    }
}

function jumpToHash() {
    setTimeout('doJumpToHash()', 30);
}

function doJumpToHash() {
    if (window.location.href.indexOf('#') != -1) {
        var hash = window.location.href.substring(window.location.href.indexOf('#'));
        var anchorName = hash.substring(1);
        showDisplayBox(anchorName + 'Box');
        if (!is_mac && !is_ie5up) {
            window.location.href = hash;
        }
        //scrollToElement(anchorName + 'Box', ScrollMode.JUMP);
    }
}

function scrollToElement(id, mode) {
    if (ScrollMode.CRAWL == mode) {
        crawlToElement(id);
    }
    else if (ScrollMode.JUMP == mode) {
        jumpToElement(id)
    }
    else {
        // default to jump
        jumpToElement(id)
    }
}

function jumpToElement(id) {
    var xPos = 0;
    var yPos = findYPos(d(id));
    window.scrollTo(xPos, yPos);
}

var lastYPos = -50;
function crawlToElement(id) {
    var xPos = 0;
    var yPos = findYPos(d(id));
    var distance = Math.abs(yPos - getCurrentYPos());
    var moveDistance = 10;
    if (distance > moveDistance) {
        if (yPos < getCurrentYPos()) {
            yPos = getCurrentYPos() - moveDistance;
        }
        else {
            yPos = getCurrentYPos() + moveDistance;
            window.scrollTo(xPos, yPos);
            if (lastYPos != getCurrentYPos()) {
                setTimeout("crawlToElement('" + id + "');", 10);
            }
            lastYPos = getCurrentYPos();
            return;
        }
    }
    window.scrollTo(xPos, yPos);
}

function scrollToPosition(xPos, yPos) {
    window.scrollTo(xPos, yPos);
}

function findXPos(obj) {
    if (!obj) { return 0; }
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findYPos(obj) {
    if (!obj) { return 0; }
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function getCurrentYPos() {
    if (document.body && document.body.scrollTop)
        return document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
        return document.documentElement.scrollTop;
    if (window.pageYOffset)
        return window.pageYOffset;
    return 0;
}

function PopupWinLg(strURL) {
    win = window.open(strURL, 'popupwinlg', 'toolbar=no,menubar=no,location=no,status=no,scrollbars=yes,resizable=yes,width=670,height=640,top=0,left=0');
    win.focus();
}

function PopupWinXtraLg(strURL) {
    win = window.open(strURL, 'popupwinlg', 'toolbar=no,menubar=no,location=no,status=no,scrollbars=yes,resizable=yes,width=700,height=550,top=20,left=20');
    win.focus();
}

function PopupWinMed(strURL) {
    win = window.open(strURL, 'popupmed', 'toolbar=no,menubar=no,location=yes,status=no,scrollbars=yes,resizable=yes,width=600,height=400');
    win.focus();
}

function PopupWinSmall(strURL) {
    win = window.open(strURL, 'popupmed', 'toolbar=no,menubar=no,location=yes,status=no,scrollbars=yes,resizable=yes,width=400,height=300');
    win.focus();
}
//try this for popup
function showPopUpCentered(el, fr, width, height) {
    var cvr = document.getElementById("cover")
    //var dlg = document.getElementById(el)
    //var frm = document.getElementById(fr)
    cvr.style.display = "block"
    //dlg.style.display = "block"
    //frm.style.display = "block"
    showdeadcenterdiv(width, height, el);
    showdeadcenterdiv(width, height, fr);

    if (document.body && (document.body.scrollWidth || document.body.scrollHeight)) {
        var pageWidth = document.body.scrollWidth + 'px';
        var pageHeight = document.body.scrollHeight + 'px';
    }
    else if (document.body.offsetWidth) {
        var pageWidth = document.body.offsetWidth + 'px';
        var pageHeight = document.body.offsetHeight + 'px';
    }
    else {
        var pageWidth = '100%';
        var pageHeight = '100%';
    }
    cvr.style.width = pageWidth;
    cvr.style.height = pageHeight;
}
//try this for popup
function showPopUp(el, fr) {
    var cvr = document.getElementById("cover")
    var dlg = document.getElementById(el)
    var frm = document.getElementById(fr)
    cvr.style.display = "block"
    dlg.style.display = "block"
    frm.style.display = "block"

    if (document.body && (document.body.scrollWidth || document.body.scrollHeight)) {
        var pageWidth = document.body.scrollWidth + 'px';
        var pageHeight = document.body.scrollHeight + 'px';
    }
    else if (document.body.offsetWidth) {
        var pageWidth = document.body.offsetWidth + 'px';
        var pageHeight = document.body.offsetHeight + 'px';
    }
    else {
        var pageWidth = '100%';
        var pageHeight = '100%';
    }
    cvr.style.width = pageWidth;
    cvr.style.height = pageHeight;
}

function closePopUp(el, fr) {
    var cvr = document.getElementById("cover")
    var dlg = document.getElementById(el)
    var frm = document.getElementById(fr)
    cvr.style.display = "none"
    dlg.style.display = "none"
    frm.style.display = "none"

    document.body.style.overflowY = "scroll"
}

function ClearTextBox(textbox, initialvalue) {
    if (textbox.value == initialvalue) {
        textbox.value = '';
    }
}

window.onload = loadWindow
function CallInternalSearch() {
    s = s_gi(s_account);
    s.linkTrackVars = "events";
    s.linkTrackEvents = "event3";
    s.events = "event3";
    s.tl(this, 'o', 'Internal Search');
}
function CallCrossSell() {
    s = s_gi(s_account);
    s.linkTrackVars = "events";
    s.linkTrackEvents = "event14";
    s.events = "event14";
    s.tl(this, 'o', 'Cross Sell');
}

function CallMailingList() {
    s = s_gi(s_account);
    s.linkTrackVars = "events";
    s.linkTrackEvents = "event25";
    s.events = "event25";
    s.tl(this, 'o', 'Mailing List');
}

function ShowHeaderHours() {
    var cvr = document.getElementById("ShowHours")
    cvr.style.display = "block"

}
function HideHeaderHours() {
    var cvr = document.getElementById("ShowHours")
    cvr.style.display = "none"

}

function CheckCompareProducts(e, productnum) {
    /* alert('got here');
    alert(e.id)
    alert(comparecount)
    alert(compareitems)*/

    if (e.checked == true) {
        if (comparecount == 4) {
            showPopUpCentered('comparedialog', 'compareFrame', 300, 200);
            e.checked = false;
        }
        else {
            comparecount++;
            if (compareitems == '') {
                compareitems = productnum;
            }
            else {
                compareitems += "|" + productnum;
            }
        }
    }
    else {
        comparecount = comparecount - 1;
        compareitems = compareitems.replace(productnum + '|', '');
        compareitems = compareitems.replace('|' + productnum, '');
        //all ways just in case
        compareitems = compareitems.replace(productnum, '');
    }
    //alert(compareitems);
    //alert(comparecount);
}

function goToCompare(domain) {
    closePopUp('comparedialog', 'compareFrame');
    if (compareitems == '' || comparecount < 2) {
        showPopUpCentered('compareerror', 'compareerrorFrame', 300, 200);
    }
    else {
        //  ClearForm;
        //  var hid = document.getElementById('ctl00$ContentPlaceHolder1$ProductsPager1$comparerefresh');
        //hid.value = "y";
        //alert(hid.value);
        var url = document.location.href;
        document.location = 'http://www' + domain + '/CompareProducts.aspx?products=' + compareitems + "&prevURL=" + url
    }
}

function checkWasCompare(cookiedomain) {
    //only for chrome - the rest do not cache the page
    // alert('here');
    if (is_chrome) {
        //check the cookie
        //  alert('cookie' + getCookie('comparepage'));
        if (getCookie('comparepage') == 'y') {
            setCookie('comparepage', '', cookiedomain);
            window.location.reload();
        }
    }
}

function showdeadcenterdiv(Xwidth, Yheight, divid) {
    // First, determine how much the visitor has scrolled 

    var scrolledX, scrolledY;
    if (self.pageYOffset) {
        scrolledX = self.pageXOffset;
        scrolledY = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        scrolledX = document.documentElement.scrollLeft;
        scrolledY = document.documentElement.scrollTop;
    } else if (document.body) {
        scrolledX = document.body.scrollLeft;
        scrolledY = document.body.scrollTop;
    }

    // Next, determine the coordinates of the center of browser's window 

    var centerX, centerY;
    if (self.innerHeight) {
        centerX = self.innerWidth;
        centerY = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        centerX = document.documentElement.clientWidth;
        centerY = document.documentElement.clientHeight;
    } else if (document.body) {
        centerX = document.body.clientWidth;
        centerY = document.body.clientHeight;
    }

    // Xwidth is the width of the div, Yheight is the height of the 
    // div passed as arguments to the function: 
    var leftOffset = scrolledX + (centerX - Xwidth) / 2;
    var topOffset = scrolledY + (centerY - Yheight) / 2;
    // The initial width and height of the div can be set in the 
    // style sheet with display:none; divid is passed as an argument to // the function 
    var o = document.getElementById(divid);
    var r = o.style;
    r.position = 'absolute';
    r.top = topOffset + 'px';
    r.left = leftOffset + 'px';
    r.display = "block";
}

function ClearForm() {
    var form = document.getElementById('form1')
    //clear the checkboxes - used for back button
    for (var i = 0; i < form.elements.length; i++) {
        alert(form.elements[i].type);
        if (form.elements[i].type == 'checkbox') {
            alert(form.elements[i].name);
            form.elements[i].checked = false;
        }
    }
}

//escape characters
function BadCharStrip(str) {
    NewStr = "";
    NewChar = "";

    for (i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) == 32) {
            //Do nothing (space)
            NewChar = str.charAt(i);
        } else if (str.charCodeAt(i) >= 48 && str.charCodeAt(i) <= 57) {
            //Do nothing (1 - 9)
            NewChar = str.charAt(i);
        } else if (str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90) {
            //Do nothing (a - z)
            NewChar = str.charAt(i);
        } else if (str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122) {
            //Do nothing (A - Z)
            NewChar = str.charAt(i);
        } else {
            //Convert to web safe ASCII code
            NewChar = "&#" + str.charCodeAt(i) + ";";
        }

        NewStr = NewStr + NewChar;
    }

    return (NewStr);
}

//set cookie

function setCookie(cookiename, cookievalue, domain) {

    if (document.cookie != document.cookie)
    { index = document.cookie.indexOf(cookiename); }
    else
    { index = -1; }

    if (index == -1) {
        document.cookie = cookiename + "=" + cookievalue + ';domain=' + domain;
    }

}

//get cookie
function getCookie(cookiename) {
    var cookievalue;
    cookievalue = '';
    if (document.cookie) {
        index = document.cookie.indexOf(cookiename);
        if (index != -1) {
            namestart = (document.cookie.indexOf("=", index) + 1);
            nameend = document.cookie.indexOf(";", index);
            if (nameend == -1) { nameend = document.cookie.length; }
            cookievalue = document.cookie.substring(namestart, nameend);
            return cookievalue;
        }
    }
}
//bookmark the website
function bookmarksite(title, url) {
    if (window.sidebar) // firefox
        window.sidebar.addPanel(title, url, "");
    else if (window.opera && window.print) { // opera
        var elem = document.createElement('a');
        elem.setAttribute('href', url);
        elem.setAttribute('title', title);
        elem.setAttribute('rel', 'sidebar');
        elem.click();
    }
    else if (document.all)// ie
        window.external.AddFavorite(url, title);
    else
        alert('This browser does not support this function.');
}



