﻿jQuery(function () {
    // this function is called from the server control when using a non-standard clicking button without the OnClientClick.
    jQuery('[x-designercontrol-addproducttocartform-clickevent]').attr('href', 'javascript:void(0);').click(function () {
        var parentContainer = jQuery(this).parents('[x-designercontrol-addproducttocartform-container]'); // it may not be the direct parent up, depending on how the button control is rendered.
        var quantity = parentContainer.find('[x-designercontrol-addproducttocartform-quantity]').val();
        var productID = parentContainer.attr('x-designercontrol-addproducttocartform-productid');
        var checkoutNavigateUrl = parentContainer.attr('data-checkoutnavigateurl');

        if (typeof (quantity) === "undefined") {
            quantity = '1'; // default
        }

        if (designerControl_AddProductToCartForm_ValidateQuantity(quantity) == true) {
            //DesignerControl_AddProductToCartForm_AddProductToCart({ 'quantity': quantity, 'productid': productID });
            var params = { 'productid': productID, 'quantity': quantity };
            if (checkoutNavigateUrl)
                params.checkoutnavigateurl = checkoutNavigateUrl;

            var msie8 = jQuery.browser.msie && jQuery.browser.version < 8;

            var win = new jsModweb.AdvancedModalWindow({
                ascxPath: '/_framework/resources/Designer.AddProductToCartForm/AddProductToCartForm.AddWindow.ascx'
                //, title: 'Your Cart'
                , params: params
                , width: 520
                , height: msie8 ? 384 : 336
                , borderColor: 'black'
                , showTitleBar: false
                , borderWidth: '1px'
                , scrolling: false
                , overflow: 'hidden'
                , modal: true
            }).open();
        }

        /* Commented out because this will now happen in the AddProductToCartForm.AddWindow.ascx because it has to add to the cart first by its code-behind and modweb_designer_cartbar_updateAll() was being executed before the code-behind was being called, meaning the refresh didn't do anything or always delayed by one when adding multiple items to the cart after eachother.
        // update all cartbars.
        if (modweb_designer_cartbar_updateAll) {
        modweb_designer_cartbar_updateAll();
        }
        */
    });


    
    // refresh any update panels if said in the RefreshUpdatePanelOnAddControlID property of the AddProductToCartForm control.
    jQuery('[x-designercontrol-addproducttocartform]:first').each(function () { // make sure we only take 1 because there will be lots of 'x-designercontrol-addproducttocartform' on the page, but only one UpdatePanel
        var updatePanelId = jQuery(this).attr('id');
        __doPostBack(updatePanelId, 'customPostback');
        //__doPostBack('<%=UpdatePanelCartBar.ClientID%>', 'customPostback');

    });
});

function designerControl_AddProductToCartForm_ValidateQuantity(quantity) {
    if (quantity == "" || quantity == "0") {
        alert("Please enter a quantity and try again.");
        return false;
    }
    return true;
}

