﻿/* Sync Qty fields */
function updateQty(whichField) {
    var qty = $("[id$='_quantity']");
    var qty2 = $("[id$='_qty2']");
    if (whichField == "qty2")
        qty.attr("value", qty2.attr("value"));
    else
        qty2.attr("value", qty.attr("value"));
}

/* Availability Messaging Functions */
function skuSelector1_SelectedIndexChangedHandler(e) {
    /* Handle Availability messaging */

    var availMsg = "";
    /*(e.get_availabilityMessage() != null && e.get_availabilityMessage() != "") ? e.get_availabilityMessage() : "&nbsp;";*/

    //$get("divAvailability").innerHTML = availMsg;

    //var qtyMsg = "&nbsp;";

    var btnAdd = $get("addToCartDiv");
    var btnAdd2 = $get("addToCartDiv2");
    var btnAddOff = $get("addToCartDivOff");
    var btnAdd2Off = $get("addToCartDiv2Off");
    var btnWish = $get("addToWishDiv");
    var btnWishOff = $get("addToWishDivOff");

    if (e.get_quantityAvailable() != null) {
        //show active add to cart button
        btnAdd.style.display = "block";
        btnAdd2.style.display = "block";
        btnWish.style.display = "block";
        //hide inactive add to cart button
        btnAddOff.style.display = "none";
        btnAdd2Off.style.display = "none";
        btnWishOff.style.display = "none";
    } else {
        //hide active add to cart button
        btnAdd.style.display = "none";
        btnAdd2.style.display = "none";
        btnWish.style.display = "none";
        //show inactive add to cart buttons
        btnAddOff.style.display = "block";
        btnAdd2Off.style.display = "block";
        btnWishOff.style.display = "block";
    }
    //$get("spanNumAvail").innerHTML = qtyMsg;


    /* Handle displaying selected values */
    if (e.get_selectedSku() != null && e.get_selectedSku() != "") {
        $get("skuAsSelected").innerHTML = e.get_selectedSku();
        $get("skuAsSelected2").innerHTML = e.get_selectedSku();
        $get("ErrorNoSku").style.display = "none"; // just in case error message for no sku is displaying
        $get("ErrorNoSkuWish").style.display = "none"; // just in case error message for no sku is displaying
    }
    else {
        $get("skuAsSelected").innerHTML = "----";
        $get("skuAsSelected2").innerHTML = "";
    }

    /*Handle displaying selected manu ID */
    if (e.get_orderCode() != null && e.get_orderCode() != "") {
        $get("manuAsSelected").innerHTML = e.get_orderCode();
    }
    else {
        $get("manuAsSelected").innerHTML = "----";
    }

    //show selected price
    if (e.get_selectedSku() != null && e.get_selectedPrice() != "") {
        $get("priceAsSelected").innerHTML = "Price as Selected: <span class='price'>$" + CurrencyFormatted(e.get_selectedPrice()) + "</span>";
        $get("priceAsSelected2").innerHTML = "$" + CurrencyFormatted(e.get_selectedPrice());
        //update percentSaved
        if ($("#percentSaved")) {
            $("#percentSaved").hide();
            var perSave = "";
            if (e.get_selectedMsrpPrice() > (1.05 * e.get_selectedPrice())) perSave = "You Save " + Math.round((1 - (e.get_selectedPrice() / e.get_selectedMsrpPrice())) * 100) + "%";
            $("#percentSavedSku").html(perSave);
            $("#percentSavedSku").show();
        }
        //change tab heading text
        $("#Tab_Heading_Text").html("");
        $("#Tab_Heading_Text").show();
        $("#Tab_Heading_Link").hide();
    }
    else {
        $get("priceAsSelected").innerHTML = "";
        $get("priceAsSelected2").innerHTML = "";
        if ($("#percentSaved")) {
            $("#percentSavedSku").hide();
            $("#percentSaved").show();
        }
        //change tab heading text
        $("#Tab_Heading_Text").html("Please Make a Selection For Each Available Product Option");
        $("#Tab_Heading_Text").show();
        $("#Tab_Heading_Link").hide();
    }


    /* Not showing selected options
    
    //show selected options
    var options = "";
    if(e.get_selectedValues()) 
    {
    var sv = e.get_selectedValues();

        for(var i=0; i < sv.length; i++) 
    {
    if ((sv[i] != "" && sv[i] != "null" && sv[i] != null) && ((typeof showOptions != "undefined" && showOptions[i] != null && showOptions[i] == "True") || (typeof showOptions == "undefined")))
    options += "<li>" + sv[i] + "<\/li>";
    }

    }
    if (options != "")
    options = "<strong>Selected Options: </strong><br><ol>" + options + "<\/ol>" + "<div class='skuAvail'>" + availMsg + "</div>";
    else 
    options = "<div class='noSelectionMsg'>Select <a class='view_options_link' href='#tabs'>Product Options</a> to Purchase This Item:</div>"; //no options and no sku selected 
    $get("optionsAsSelected").innerHTML = options;
    */

    /* If already selected from each set and we don't have a sku selected -- bad combo made */
    var alreadySelected = e.get_selectedValues();
    var hasNull = false;
    for (var s = 0; s < alreadySelected.length; s++) {
        if (alreadySelected[s] == null || alreadySelected[s] == "" || alreadySelected[s] == "null")
            hasNull = true;
    }

    if ($get("ErrorBadCombo")) {
        if (hasNull == false && (e.get_selectedSku() == null || e.get_selectedSku() == "")) {
            $get("ErrorBadCombo").style.display = "block";
        } else {
            $get("ErrorBadCombo").style.display = "none";
        }
    }
}

function imageAttributeSelector1_SelectedIndexChanged(e) {
    var isIE = ((navigator.appName) == "Microsoft Internet Explorer") ? true : false;
    var eControl, pTD, pTDHtml, selDiv = null;

    //if selecting an attribute...
    if (e.get_selectedValue() != null && e.get_selectedValue() != "null" && e.get_selectedValue() != "") { /* Need to check for null and "null" because of IE */
        //get the element from the selected attribute
        eControl = e.get_selectedControl().get_element();
        //get the element's offsetParent (TD.product_details)
        pTD = eControl.offsetParent;
        
        if (pTD) {
            pTDHtml = pTD.innerHTML;
            
            //go up through parents in DOM until reach the containing TR then find the second col TD and fill with pTD.innerHTML
            if (pTD.offsetParent) {
                if (isIE == true) {
                    if (pTD.offsetParent.offsetParent.nextSibling) {
                        if (pTD.offsetParent.offsetParent.nextSibling.children[1]) selDiv = pTD.offsetParent.offsetParent.nextSibling.children[1];
                    }
                }
                else {
                    if (pTD.offsetParent.offsetParent.nextElementSibling) {
                        if (pTD.offsetParent.offsetParent.nextElementSibling.children[1]) selDiv = pTD.offsetParent.offsetParent.nextElementSibling.children[1];
                    }
                    else if (pTD.offsetParent.offsetParent.nextSibling.nextSibling) {
                        if (pTD.offsetParent.offsetParent.nextSibling.nextSibling.lastChild) selDiv = pTD.offsetParent.offsetParent.nextSibling.nextSibling.lastChild;  //For FF 3.0.15
                    }
                }
            }  
        }
    } else {//if deselecting an attribute
        //eControl = e.originalTarget;
        eControl = e.get_element();

        //get the element's offsetParent (TD.product_details)
        //themeImageUrl defined on product.aspx page
        pTDHtml = "<img src='" + themeImageUrl + "/option_no_select.gif' alt='Select option' \/>";

        //go up through parents in DOM until reach the containing TR then find the second col TD and fill with pTD.innerHTML
        if (isIE == true) {
            if (eControl.offsetParent.nextSibling) {
                if (eControl.offsetParent.nextSibling.children[1]) selDiv = eControl.offsetParent.nextSibling.children[1];
            }
        }
        else {
            if (eControl.offsetParent.nextElementSibling) {
                if (eControl.offsetParent.nextElementSibling.children[1]) selDiv = eControl.offsetParent.nextElementSibling.children[1];
            }
            else if (eControl.offsetParent.nextSibling.nextSibling) {
                if(eControl.offsetParent.nextSibling.nextSibling.lastChild) selDiv = eControl.offsetParent.nextSibling.nextSibling.lastChild;  /*For FF 3.0.15*/
            }
        }
    }

    //set selDiv's innerHTML to pTDHtml
    if (selDiv) selDiv.innerHTML = pTDHtml;
}

function CurrencyFormatted(amount) {
    var i = parseFloat(amount);
    if (isNaN(i)) { i = 0.00; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { s += '.00'; }
    if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}


/* Swatch Functions */
//this function will cause the main image to swap when a swatch is rolled over.
//you must add the following to the swatch control declaration:
//  OnClientThumbNailMouseOver="SwatchOver" 
function SwatchOver(sender, eventArgs) {
    var mainImage = document.getElementById("MainImage");
    mainImage.src = "assets/images/swatches/" + sender._swatches[eventArgs.index].Image;
}

//this function will cause the main image to swap when a swatch is rolled off.
//you must add the following to the swatch control declaration:
//  OnClientThumbnailMouseOut="SwatchOut" 
function SwatchOut(sender, eventArgs) {
    var mainImage = document.getElementById("MainImage");
    mainImage.src = mainImageSrc;
}

//this function will cause the main image to swap when a swatch is clicked.
//you must add the following to the swatch control declaration:
//  OnClientSelectedIndexChanged="SwatchClick"
function SwatchClick(sender) {
    var mainImage = document.getElementById("MainImage");
    mainImage.src = "assets/images/swatches/" + sender.get_selectedImage();
}


/* Alternate Functions */
//this function will cause the main image to swap when an alternate is rolled over.
//you must add the following to the swatch control declaration:
//  OnClientThumbNailMouseOver="SwatchOver" 
function AlternateOver(imageName) {
    var mainImage = document.getElementById("MainImage");
    mainImage.src = "assets/images/alternates/" + imageName;
}

//this function will cause the main image to swap when an alternate is rolled off.
//you must add the following to the swatch control declaration:
//  OnClientThumbnailMouseOut="SwatchOut"
function AlternateOut() {
    var mainImage = document.getElementById("MainImage");
    mainImage.src = mainImageSrc;
}

//this function will cause the main image to swap when an alternate is clicked.
//you must add the following to the swatch control declaration:
//  OnClientSelectedIndexChanged="SwatchClick"
function AlternateClick(imageName) {
    var mainImage = document.getElementById("MainImage");
    mainImage.src = "assets/images/alternates/" + imageName;

}

/* Jumbo Functions */
var winPhoto;
function makeNewWindow(path, title) {
    winPhoto = window.open("", "AltView", "height=500,width=500,status=0,scrollbars=0,location=0,resizable=1");
    winPhoto.document.open();
    winPhoto.document.write('<html>');
    winPhoto.document.write('    <head>');
    winPhoto.document.write('        <title>');
    winPhoto.document.write(title);
    winPhoto.document.write('        </title>');
    winPhoto.document.write('<sc' + 'ript lang=javascript>');
    winPhoto.document.write('var largeimage = new Image();');
    winPhoto.document.write('largeimage.src ="' + path + '"');
    winPhoto.document.write('</s' + 'cript>');
    winPhoto.document.write('    </head>');
    winPhoto.document.write('    <body marginwidth="0" marginheight="0" topmargin="0" leftmargin="0">');
    winPhoto.document.write('    <table border="0" cellspacing="0" cellpadding="3" align="center" bgcolor="#FFFFFF">');
    winPhoto.document.write('      <tr>');
    winPhoto.document.write('        <td align="center" class="dept_products_in"><img align=center src="' + path + '" onLoad="window.resizeTo(largeimage.width+60,largeimage.height+140);" ></td>');
    winPhoto.document.write('      </tr>');
    winPhoto.document.write('      <tr>');
    winPhoto.document.write('        <td align="center"><a href="javascript:window.close();" class="large_image">Close window</a><br></td>');
    winPhoto.document.write('      </tr>');
    winPhoto.document.write('     </table>	');
    winPhoto.document.write('    </body>');
    winPhoto.document.write('</html>');
    winPhoto.document.close();
    winPhoto.focus();
    return false;
}

/* Update monogramming fields and quantities*/
function updateMonogram1() {
    var msg = $("#gift_msg");
    var theValue = msg.val();
    var mono1 = $("[id$='_monogram_1']");
    var giftCk = $("[id$='_gift_wrap']");
    var q1 = $("[id$='_quantity']");
    var q2 = $("[id$='_quantity2']");

    //update monogram field if checkbox checked and message value is set
    if (theValue != "" && theValue != msg.attr("title") && giftCk.attr("checked")) {
        mono1.val("Gift Message: " + theValue.replace(/,/g, ' ') + ";"); //fill mono value
    }
    else {
        mono1.val(""); //clear mono value
    }
    //update qty field for gift wrap when checkbox checked
    if (giftCk.attr("checked")) {
        q2.val(q1.val()); //make qty = product's qty so wrap IS added to cart
        $get("GiftWrapPop").style.display = "block"; //show popup
    }
    else {
        q2.val("0"); //make qty 0 so that wrap is NOT added to cart
        $get("GiftWrapPop").style.display = "none"; //hide popup
    }
}