﻿/* Availability Messaging Functions */
function skuSelector1_SelectedIndexChangedHandler(e) {
    var availMsg = (e.get_availabilityMessage() != null && e.get_availabilityMessage() != "") ? e.get_availabilityMessage() : "&nbsp;";
        
    var qtyMsg = "&nbsp;"; 

    var btnAdd = $get("addToCartDiv");
    var numavail = e.get_quantityAvailable();

    //handle availability messaging - hide at first
    var outofstock = $("#[id$='message_outofstock']");
    outofstock.hide();
    var instock = $("#[id$='message_instock']");
    instock.hide();
    var backorder = $("[id$='_message_backorder']");
    if (backorder.length > 0) backorder.hide();
    /* AT 07/28/2009*/
    // there is no data exposed to tell us the inventory mode we're in
    // so i am checking for the word "back" in the availability message which should
    // contain "Backorder" or something along those lines
    // if it does not, im going off the quantity to toggle messaging and hiding the add to cart
    if (e.get_availabilityMessage() != null) qtyMsg = e.get_availabilityMessage();
    
    if (qtyMsg.toLowerCase().indexOf("back")>0) {

        btnAdd.style.visibility = "visible";
     
    }
    else {
        if (numavail != null) {
            switch (numavail) {
                case 0:
                    qtyMsg = "Out of stock";

                    outofstock.show();
                    //hide add to cart button
                    btnAdd.style.visibility = "hidden";

                    break;
                default:
                    if (numavail == 1) {
                        qtyMsg = numavail + " in stock";
                    }
                    else if (numavail < 25) {
                        qtyMsg = numavail + " in stock";
                    } else {
                        qtyMsg = "Plenty in stock";
                    }
                    if (numavail < 0) {
                        qtyMsg = "Backorder";
                    }
                    btnAdd.style.visibility = "visible";

                    if (numavail > 0) {
                        instock.show();
                    }
                    /* will not fire..checked again below */
                    if (numavail < 0) {
                        if (backorder.length > 0) backorder.show();
                    }
                    break;
            }
        }
    }
    //if the message contains back, meaning back order or backorder...
    if (qtyMsg.toLowerCase().indexOf("back") != -1 || availMsg.toLowerCase().indexOf("back") != -1) {
        //first make sure there is a dropdown (meaning there are multiple skus to select from...)
        if ($(".AddProduct_Sku select").length > 0) {
            qtyMsg = qtyMsg.toUpperCase();
            availMsg = availMsg.toString().toUpperCase();
            if (backorder.length > 0) backorder.show();
        } else {
            backorder.addClass("SingleSkuBackorder");
        }
    }

    /* JMS Commented out since only showing backorder popup message and NOT other avail messages.
        if (qtyMsg == "&nbsp;") {
        $get("labelNumAvail").innerHTML = availMsg;
        } else {
        $get("labelNumAvail").innerHTML = qtyMsg;
    }*/

}

function AddToCart() {
    var $j = jQuery.noConflict();
    //if the single sku to be added to cart is on backorder...
    if ($j(".SingleSkuBackorder").length > 0) {
        if ($j('.SingleSkuBackorder').is(':visible')) {
            //click real add to cart button.
            clickRealButton()
        } else {
            //show the backorder message
            $j(".SingleSkuBackorder").show();

            //after a bit of time, click real add to cart button
            setTimeout('clickRealButton();', 3000);
        }
    } else {
        //click real add to cart button.
        clickRealButton()
    }
}

function clickRealButton() {
    var $j = jQuery.noConflict();
    $j("#[id$='_AddToCart_RolloverButton']").click();
}

// custom Redoxx sku fcn, look at the monogramming text field and decide how many characters there are
// so we can add the appropriate sku. THIS IS SPECIFIC TO 91003-Railroad and 92021-Railroad Rucksack PRODUCT ONLY
function setMonogrammingSku(pf_id) {
    if (pf_id == "91003-Railroad" || pf_id == "92021-Railroad Rucksack") {// ensure that we are only monogramming the railroad product
        var $j = jQuery.noConflict();
        
        //MonogramSku
        var monogramSku = $j("#[id$='_MonogramSku']");
        
        // first get the number of characters entered in the text field
        var monogramField = $j("#[id$='_textBoxMonogramming']");

        var textSize = $j.trim(monogramField.val()).length;
        // now get the correct sku and set the sku value
        monogramSku.val("");
        if (textSize == 0) monogramSku.val("");
        if (textSize > 0 && textSize <= 3) {
            monogramSku.val("92100");
        }
        if (textSize > 3) {
            monogramSku.val("92101");
        }

        //make quantity fields match
        $j("#[id$='_quantity2']").val($j("#[id$='_quantity']").val());        
    }
    
}

