﻿function submitMe(item) {
    if ((item.options[item.selectedIndex].value).length > 0) {
        window.open(item.options[item.selectedIndex].value);
    }
}
function replaceImageMedium(source) {
    source.src = "skins/skin_1/images/imageUnavailableMedium.gif";
    // disable onerror to prevent endless loop
    source.onerror = null;
    return true;
}
function ProductTab(tab) {
    //FIRST UPDATE THE TAB
    document.getElementById("FeaturesTab").style.display = "none";
    document.getElementById("SpecificationsTab").style.display = "none";
    document.getElementById("MaterialsTab").style.display = "none";
    document.getElementById("VideoTab").style.display = "none";
    document.getElementById(tab).style.display = "block";

    //NEXT UPDATE THE IMAGE
    document.getElementById("FeaturesTabImage").src = "skins/skin_1/images/FeaturesTab.gif";
    document.getElementById("SpecificationsTabImage").src = "skins/skin_1/images/SpecificationsTab.gif";
    document.getElementById("MaterialsTabImage").src = "skins/skin_1/images/MaterialsTab.gif";
    document.getElementById("VideoTabImage").src = "skins/skin_1/images/VideoTab.gif";
    document.getElementById(tab + "Image").src = "skins/skin_1/images/" + tab + "Selected.gif";
}

function MHMProductLargeClick(pid) {
    //alert(document.getElementById("ProductPic" + pid).src);
    if (document.getElementById("ProductPic" + pid)["onclick"]) {
        document.getElementById("ProductPic" + pid)["onclick"]();
    }
}

function popuplarge(e) {
    if(e.src.toLowerCase().indexOf("imageunavailable") == -1)
    {
        var left = (screen.width/2)-300;
        var top = (screen.height/2)-300;
        window.open(e.src, "LargerImage", "left=" + left + ",top=" + top + ",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=630,height=630");
    }
}

function verify(f) {
    for (var i = 0; i < f.length; i++) {
        var e = f.elements[i];

        if (e.name == "ea") {
            if (e.value == "") {
                alert("Please enter your e-mail address.");
                f.elements[i].focus();
                return false;
            }

            var emailReg = ".+@.+\\.[a-z]+";
            var regex = new RegExp(emailReg);
            if (!regex.test(e.value)) {
                alert("The e-mail address you entered is invalid, please try again.");
                f.elements[i].focus();
                return false;
            }
        }
    }
}

function QuicktimeDetection() {
    var haveqt = false;

    if (navigator.plugins) {
        for (i = 0; i < navigator.plugins.length; i++) {
            if (navigator.plugins[i].name.indexOf("QuickTime") >= 0)
            { haveqt = true; }
        }
    }
    return haveqt;
}

function MHMChangeColorDropDown(ColorName) {
    var x = document.getElementById("Color");
    for (var i = 0; i < x.options.length; i++) {
        if (x.options[i].value.replace(/ /gi, "").toLowerCase() == ColorName + ",") {
            x.options[i].selected = true;
            break;
        } 
   }
   document.getElementById("Color").onchange();   // when you click a color image you need to refresh the size inventory  
}

function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);

    if (results == null)
        return "";
    else
        return results[1];
}


//  inventory dropdown messaging
function MHMSizeDropDown(ProductID, Size, ColorFlag) {
    if (Size.indexOf("[") != -1) {
        Size = Size.substring(0, Size.indexOf("[")) + ",";
    }

    if (document.getElementById("Color") == null) {  // set just colors and drop out if no colors exist as the message is always on the page
        var xx = document.getElementById("Size"); //  loop through array of sizes and colors
        for (ii = 0; ii < ProductID.length; ii++) {
            var test = "";
            test = String(ProductID[ii]);
            test = test.substring(1, (test.length));
            if ((test.indexOf(",0") > -1)) { // if no inventory set message
                for (yy = 1; yy < xx.length; yy++) {
                    if (xx.options[yy].text.indexOf("[") != -1) {
                        if ((test.substring(0, test.indexOf(","))) == (xx.options[yy].text.substring(0, xx.options[yy].text.indexOf(" [")))
                            && xx.options[yy].text.indexOf(" (Out of Stock)") == -1) {
                            xx.options[yy].text = String(xx.options[yy].text) + " (Out of Stock)";
                        }
                    }
                    else {
                        if ((test.substring(0, test.indexOf(","))) == (xx.options[yy].text)) {
                            xx.options[yy].text = String(xx.options[yy].text) + " (Out of Stock)";
                        }
                    }
                }
            }
        }
        return;
    }
    var z = document.getElementById("Color");      // reset Colors
    for (y = 1; y <= (z.length - 1); y++) {
        z.options[y].text = String(z.options[y].text).replace(" (Out of Stock)", "");
    }
    var zz = document.getElementById("Size");      // reset Sizes
    for (y = 1; y <= (zz.length - 1); y++) {
        zz.options[y].text = String(zz.options[y].text).replace(" (Out of Stock)", "");
    }
    if (Size.indexOf("-,-") > -1) {
        z.options[0].selected = true;
        zz.options[0].selected = true;
        //AllColorAllSize(ProductID);              
    }

    Size = Size.substring(0, Size.indexOf(",") + 1);
    var x = document.getElementById("Color"); //  loop through array of sizes and colors
    for (i = 0; i < ProductID.length; i++) {
        var test = "";
        test = String(ProductID[i]);

        if ((ColorFlag > 0) && (test.indexOf("," + Size) > -1) && (test.indexOf(",0") > -1)) { // if no inventory set message
            for (y = 1; y < x.length; y++) {
                if (x.options[y].text.indexOf("[") != -1) {
                    if ((test.substring(0, test.indexOf(","))) == (x.options[y].text.substring(0, x.options[y].text.indexOf(" [")))
                        && x.options[y].text.indexOf(" (Out of Stock)") == -1) {
                        x.options[y].text = String(x.options[y].text) + " (Out of Stock)";
                    }
                }
                else {
                    if ((test.substring(0, test.indexOf(","))) == (x.options[y].text)) {
                        x.options[y].text = String(x.options[y].text) + " (Out of Stock)";
                    }
                }
            }
        }
        else if ((ColorFlag > 0) && (test.indexOf("," + Size) > -1) && (test.indexOf(",0") == -1)) {  // if inventory set message
            for (y = 1; y <= (x.length - 1); y++) {
                if (x.options[y].text.indexOf("[") != -1) {
                    if ((test.substring(0, test.indexOf(","))) == (x.options[y].text.substring(0, x.options[y].text.indexOf(" [")))
                        && x.options[y].text.indexOf(" (Out of Stock)") == -1) {
                        x.options[y].text = String(x.options[y].text).replace(" (Out of Stock)", "");
                    }
                }
                else {
                    if ((test.substring(0, test.indexOf(","))) == (x.options[y].text)) {
                        x.options[y].text = String(x.options[y].text).replace(" (Out of Stock)", "");
                    }
                }
            }
        }
    }
}

function MHMColorDropDown(ProductID, Color, SizeFlag) {
    if (Color.indexOf("[") != -1) {
        Color = Color.substring(0, Color.indexOf("[")) + ",";
    }

    if (document.getElementById("Size") == null) {      // drop out if no sizes exist as the message is always on the page
        var xx = document.getElementById("Color");      //  loop through array of sizes and colors
        for (ii = 0; ii < ProductID.length; ii++) {
            var test = "";
            test = String(ProductID[ii]);

            if ((test.indexOf(",0") > -1)) { // if no inventory set message
                for (yy = 1; yy <= (xx.length - 1); yy++) {
                    if (xx.options[yy].text.indexOf("[") != -1) {
                        if ((test.substring(0, test.indexOf(","))) == (xx.options[yy].text.substring(0, xx.options[yy].text.indexOf(" [")))
                            && xx.options[yy].text.indexOf(" (Out of Stock)") == -1) {
                            xx.options[yy].text = String(xx.options[yy].text) + " (Out of Stock)";
                        }
                    }
                    else {
                        if ((test.substring(0, test.indexOf(","))) == (xx.options[yy].text)) {
                            xx.options[yy].text = String(xx.options[yy].text) + " (Out of Stock)";
                        }
                    }
                }
            }
        }
        return;
    }  //  END drop out if no sizes exist as the message is always on the page

    var z = document.getElementById("Color"); // reset Colors
    for (y = 1; y <= (z.length - 1); y++) {
        z.options[y].text = String(z.options[y].text).replace(" (Out of Stock)", "");
    }

    var zz = document.getElementById("Size"); // reset Sizes
    for (y = 1; y <= (zz.length - 1); y++) {
        zz.options[y].text = String(zz.options[y].text).replace(" (Out of Stock)", "");
    }
    if (Color.indexOf("-,-") > -1) {
        z.options[0].selected = true;
        zz.options[0].selected = true;
        //AllColorAllSize(ProductID);
    }

    Color = Color.substring(0, Color.indexOf(",") + 1);
    var x = document.getElementById("Size");
    for (i = 0; i < ProductID.length; i++) {   //  loop through array of sizes and colors
        var test = "";
        test = String(ProductID[i]);
        var Size = test.replace(Color, "");
        Size = Size.substring(0, Size.indexOf(","));

        if ((SizeFlag > 0) && (test.indexOf(Color) > -1) && (test.indexOf(",0") > -1)) {  // if no inventory set message            
            for (y = 1; y <= (x.length - 1); y++) {
                if (x.options[y].text.indexOf("[") != -1) {
                    if (Size == (x.options[y].text.substring(0, x.options[y].text.indexOf(" [")))
                        && x.options[y].text.indexOf(" (Out of Stock)") == -1) {
                        x.options[y].text = String(x.options[y].text) + " (Out of Stock)";
                    }
                }
                else {
                    if (Size == (x.options[y].text)) {
                        x.options[y].text = String(x.options[y].text) + " (Out of Stock)";
                    }
                }
            }
        }
        else if ((SizeFlag > 0) && (test.indexOf(Color) > -1) && (test.indexOf(",0") == -1)) {   // if inventory set message        
            for (y = 1; y <= (x.length - 1); y++) {
                if (x.options[y].text.indexOf("[") != -1) {
                    if (Size == (x.options[y].text.substring(0, x.options[y].text.indexOf(" [")))
                        && x.options[y].text.indexOf(" (Out of Stock)") == -1) {
                        x.options[y].text = String(x.options[y].text).replace(" (Out of Stock)", "");
                    }
                }
                else {
                    if (Size == (x.options[y].text)) {
                        x.options[y].text = String(x.options[y].text).replace(" (Out of Stock)", "");
                    }
                }
            }
        }
    }
}

function AllColorAllSize(ProductID) {
    var x = document.getElementById("Size");
    var SizeCount = (x.length - 1);
    var SizeZeroCount = 0;
    var z = document.getElementById("Color");
    var ColorCount = (z.length - 1);
    var ColorZeroCount = 0;

    for (y = 1; y < z.length; y++) {
        ColorZeroCount = 0;
        var ColorName = z.options[y].text;
        for (i = 0; i < ProductID.length; i++) {
            var test = "";
            test = String(ProductID[i]);
            if ((test.indexOf(ColorName + ",") != -1) && (test.indexOf(",0") != -1)) {
                ColorZeroCount = ColorZeroCount + 1;
            }
        }
        //alert(ColorZeroCount + ":" + ColorCount);
        if (SizeCount == ColorZeroCount) {
            z.options[y].text = String(z.options[y].text) + " (Out of Stock)";
        }
    }

    for (y = 1; y < x.length; y++) {
        SizeZeroCount = 0;
        var SizeName = x.options[y].text;
        for (i = 0; i < ProductID.length; i++) {
            var test = "";
            test = String(ProductID[i]);
            var SizeNameString = "," + String(SizeName) + ",0";
            //alert(SizeNameString + ":" + test);
            if ((test.indexOf(SizeNameString)) > -1) {
                SizeZeroCount = SizeZeroCount + 1;
            }
        }
        //alert(SizeZeroCount + ":" + SizeCount);
        if (ColorCount == SizeZeroCount) {
            x.options[y].text = String(x.options[y].text) + " (Out of Stock)";
        }
    }
}

function InventoryPreset() {
    var ColorExists = document.getElementById("Color");
    var SizeExists = document.getElementById("Size");
    if (SizeExists != null && ColorExists == null) {
        //alert("Size exists");
        document.getElementById("Size").onchange();
    }

    if (ColorExists != null && SizeExists == null) {
        //alert("Color exists");
        document.getElementById("Color").onchange();
    }

    if (ColorExists == null && SizeExists == null) {
        //alert("no color no size");
        OneSizeInventoryCheck();
    }
}

function OneSizeInventoryCheck() {
    if (MHMOneSizeInventory == 0) {
        //alert("no inventory");
        document.getElementById("ProductAddToCartMHM").style.display = "none";
        document.getElementById("MHMCartValidation").style.display = "block";
        document.getElementById("MHMCartValidationText").value = "This item is currently out of stock.";
    }
}

function IsNumeric(sText) {
    var ValidChars = "0123456789";
    var IsNumber = true;
    var Char;

    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}

function HomePageButtonOver() {                
    document.getElementById("HomePageSpecialsButton").src = "skins/Skin_1/images/HomeSpecialsBtnOver.jpg";
}

function HomePageButtonOut() {
    document.getElementById("HomePageSpecialsButton").src = "skins/Skin_1/images/HomeSpecialsBtn.jpg";
}


function GetDocWidth() {
    var D = document;
    return Math.max(
        $(document).width(),
        $(window).width(),
        /* For opera: */
        document.documentElement.clientWidth
        );
}

function GetDocHeight() {
    var D = document;
    return Math.max(
        $(document).height(),
        $(window).height(),
        /* For opera: */
        document.documentElement.clientHeight
        );
}

function RateIt(ProductID){

    var height = 450;
    var width = 440;
    var left = (screen.width / 2) - (width / 2);
    var top = (screen.height / 2) - (height / 2);
    var currentUrl = window.location.href;
    var url = 'rateit.aspx?Productid=' + ProductID + '&refresh=no&returnurl=' + currentUrl;
    var name = '_blank';
    var specs = 'left=' + left + ',top=' + top + ',height=' + height + ',width=' + width + ',status=no,toolbar=no,menubar=no,scrollbars=yes,location=no';
    var replace = false;
    window.open(url, name, specs, replace);
}

$(document).ready(function() {

    $(".productSwatchImage").click(function() {
        $(this).attr("data-originalUrl", $(this).parent().parent().parent().children(".productImageContainer").children("a").children("img").attr("src"));
        var color = $(this).attr("data-color");
        var urledColor = $(this).attr("data-urledColor");
        var newImageUrl = $(this).attr("data-imageUrl");
        var productIdClass = "product-" + $(this).attr("data-productid");

        // set src for all images matching this product (hover, original, additional sort, etc)
        $("." + productIdClass + " .productImageContainer a img.productImage").attr("src", newImageUrl);
        var productLink = $("." + productIdClass + " .productImageContainer a");
        var oldProductLink = productLink.attr("href");
        var newProductLink = oldProductLink.replace(/([&?])color[=].*/i, "$1color=" + urledColor);
        if (newProductLink.indexOf("&color=", 0) < 0 && newProductLink.indexOf("?color=", 0) < 0) {
            if (newProductLink.indexOf("?") < 0) {
                newProductLink = newProductLink + "?color=" + urledColor;
            } else {
                newProductLink = newProductLink + "&color=" + urledColor;
            }
        }
        productLink.attr("href", newProductLink)
        // remove selected swatch border from all swatches
        $("." + productIdClass + " .productDetailContainer .productSwatches").each(function() {
            $(this).find(".selectedSwatchBorder").each(function() {
                $(this).removeClass("selectedSwatchBorder");
                $(this).addClass("swatchBorder");
            });
        });
        // show selected swatch border for selected swatch
        $("." + productIdClass + " .productDetailContainer .productSwatches").children().each(function() {
            if ($(this).attr("data-color") == color) {
                var $swatchWeWant = $(this).find(".swatchBorder");
                $swatchWeWant.removeClass("swatchBorder");
                $swatchWeWant.addClass("selectedSwatchBorder");
            }
        });
        $(this).parent().parent().parent().children(".productImageContainer").children("a").children("img").attr("src", newImageUrl);
    });

    handleIe6();
});

function handleIe6() {
    if (isIe6()) {
        if (window.location.pathname.indexOf("/c-") < 0){
            $("#bodyWrapper").height("5000px");
        }
        $("#sideNav").css("left", "0");
        if (!hasIe6Cookie()) {
            setIe6Cookie();
            showIe6Message();
        }
    }
}

function isIe6() {
    var ieVersion = getInternetExplorerVersion();
    if (ieVersion > -1 && ieVersion < 7) {
        return true;
    }

    return false;
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

function setIe6Cookie() {
    setCookie("ie6Cookie", "ie6Cookie", 1);
}

function hasIe6Cookie() {
    var ie6Cookie = getCookie("ie6Cookie");
    
    if (ie6Cookie != null && ie6Cookie == "ie6Cookie"){
        return true;
    }

    return false;
}

function getCookie(cookieName) {
    var i, key, value, cookies = document.cookie.split(";");
    for (i = 0; i < cookies.length; i++) {
        key = cookies[i].substr(0, cookies[i].indexOf("="));
        value = cookies[i].substr(cookies[i].indexOf("=") + 1);
        key = key.replace(/^\s+|\s+$/g, "");
        if (key == cookieName) {
            return unescape(value);
        }
    }

    return null;
}

function setCookie(cookieName, value, expirationDays) {
    var expirationDate = new Date();
    expirationDate.setDate(expirationDate.getDate() + expirationDays);
    var cookieValue = escape(value) + ((expirationDays == null) ? "" : "; expires=" + expirationDate.toUTCString());
    document.cookie = cookieName + "=" + cookieValue;
}

function showIe6Message() {

    $("#ie6Callout").attr("style", "display: block;");
    
    if ($("#ie6Callout").height() < $(document).height()){
        $("#ie6Callout").attr("style", "display: block; height: " + $(document).height() + "px;");
    }
    
    $("#ie6CalloutBackground").attr("style", "height: " + $("#ie6Callout").height() + "px; width: " + $(document).width() + "px;");
    $("#ie6Callout").click(function(e) {
        if (!wasBrowserMapClicked(e, $("#browserMap"))) {
            $("#ie6Callout").attr("style", "display: none;");            
        }
    });
}

function wasBrowserMapClicked(event) {
    var eventLocation = { x: event.pageX, y: event.pageY };

    var containerOffset = $("#ie6CalloutContainer").offset();

    var browserMapBounds = {
        topLeft: {
            x: containerOffset.left + 200,
            y: containerOffset.top + 425
        },
        bottomRight: {
            x: containerOffset.left + 800,
            y: containerOffset.top + 545
        }
    }

//    alert(eventLocation.x + ", " + eventLocation.y + " | "
//    + browserMapBounds.topLeft.x + " - " + browserMapBounds.bottomRight.x 
//    + ", " + browserMapBounds.topLeft.y + " - " + browserMapBounds.bottomRight.y);

    if (eventLocation.x >= browserMapBounds.topLeft.x
        && eventLocation.x <= browserMapBounds.bottomRight.x
        && eventLocation.y >= browserMapBounds.topLeft.y
        && eventLocation.y <= browserMapBounds.bottomRight.y) {
        return true;
    }

    return false;
}

function wasElementClicked(event, $element) {
    var eventLocation = { x: event.pageX, y: event.pageY };

    var elementOffset = $element.offset();

    var elementBounds = {
        topLeft: {
            x: elementOffset.left,
            y: elementOffset.top
        },
        bottomRight: {
            x: elementOffset.left + $element.width(),
            y: elementOffset.top + $element.height()
        }
    }

    if (eventLocation.x >= elementBounds.topLeft.x
        && eventLocation.x <= elementBounds.bottomRight.x
        && eventLocation.y >= elementBounds.topLeft.y
        && eventLocation.y <= elementBounds.bottomRight.y) {
        return true;
    }

    return false;
}

// Copyright 2001, Aaron Boodman
// This code is public domain. Please use it for good, not evil.

//if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
//    document.writeln('<style type="text/css">img { visibility:hidden; } </style>');
//    window.attachEvent("onload", fnLoadPngs);
//}

function fnLoadPngs() {
    var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
    var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);

    for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
        if (itsAllGood && img.src.match(/\.png$/i) != null) {
            var src = img.src;
            var div = document.createElement("DIV");
            div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizing='scale')"
            div.style.width = img.width + "px";
            div.style.height = img.height + "px";
            img.replaceNode(div);
        }
        img.style.visibility = "visible";
    }
}

