$(function() {
    $('.ctbImprintColorBox').click(function() { OpenCTBImprintColors(); });

    $('#ctbImprintBlock').hide().removeClass('hide');
    $('#divCTBProgressBar').hide().removeClass('hide');

    $('.ctbColorDropDown select').change(function() {
        $('.ctbColor').val($(this).children('option:selected').val());
    });

    $('#btnCtbAddToCart').click(function() {
        var ProdID = $('.ctbProductID').val();
        var Qty = $('.ctbQuantityVal').val();
        var ColorID = $('.ctbColor').val();
        var ImprintLoc = $('.ctbImprintLocationID').val();
        var ImprintCol = $('.ctbImprintColorID').val();
        SaveClickToBuyItem(ProdID, Qty, ColorID, ImprintLoc, ImprintCol);
    });
});

function OpenCTBImprintColors() {
    $('#ctbItemChoices').slideUp('fast', function() {
        $('#ctbImprintBlock').slideDown('fast');
    });
}

function SetCTBImprintColor(Hex, ColorID) {
    $('.ctbImprintColorID').attr('value', ColorID);
    $('.ctbImprintColorBox').css('background-color', Hex);
    $('#ctbImprintBlock').slideUp('fast', function() {
        $('#ctbItemChoices').slideDown('fast');
    });
}

function ValidateClickToBuy(ColorID, ImprintColorID) {
    if (ColorID == '') {
        alert('Please select the color you would like the item in.');
        return false;
    } else if (ImprintColorID == '') {
        alert('Please select the color you would like this item imprinted with.');
        return false;
    }
}

function SaveClickToBuyItem(ProdID, Qty, ColorID, ImprintLocIDs, ImprintColorID) {
    if (ValidateClickToBuy(ColorID, ImprintColorID) != false) {
        $('#divCTBProgressBar').toggle();
        $('#ctbAddToCartSuccess').hide();
        $('#btnCtbAddToCart').attr('disabled', 'disabled');
        
        var ImprintLocs = ImprintLocIDs.split("/");

        // build the data object that will be sent to the server
        var postData = "{'ProductID':" + ProdID.toString() + ",'Quantity':" + Qty.toString() + ",'Color':'" +
            ColorID.toString() + "','ImprintLocationID':" + ImprintLocs[0] + ",'ProductImprintLocationID':" +
            ImprintLocs[1] + ",'ImprintColorID':" + ImprintColorID + ",'OneClickID':" + $('.ctbID').val() + "}";

        $.ajax({
            url: '/cart/ajaxaddtocart.aspx',
            type: 'POST',
            data: postData,
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            timeout: 10000,
            success: function(data) {
                if (data.result == true) {
                    $('#ctbAddToCartSuccess').html('<strong>This item has been added to your cart.</strong><br><a href=\'/cart\'>Click here to go your cart.</a>').show();
                } else {
                    $('#ctbAddToCartSuccess').html('We\'re sorry, but there was a problem adding this item to your cart. Please <a href=' + $('#ctbThumbCell').children('a').attr('href') + '>click here</a> to see more details on this item and try a different way.').show();
                }
                $('#divCTBProgressBar').toggle();
            },
            error: function() {
                $('#ctbAddToCartSuccess').html('We\'re sorry, but there was a problem adding this item to your cart. Please <a href=' + $('#ctbThumbCell').children('a').attr('href') + '>click here</a> to see more details on this item and try a different way.').show();
                $('#divCTBProgressBar').toggle();
            }
        });
    }
    return false;
}

function IsNumeric(value) {
    if (value.match(/^\d+$/) == null)
        return false;
    else
        return true;
}