﻿var commerce = Class.create({
    initialize: function() {
    }
});


commerce.addItemToCart = function(sender, itemguid, quantityText, reloadTarget) {
    if (DataValidation.validateNumber($F(quantityText))) {
        $(quantityText).removeClassName('errorNull');
        new Ajax.Request('util/commerceShoppingCart.aspx',
    {
        method: 'post',
        parameters: { cmd: 'AddItemToCart', ItemGUID: itemguid, Quantity: $F(quantityText) },
        onSuccess: function(xhrResponse) {
            switch (sender) {
                case 'ViewItem':
                    Common.showDiv('dCheckoutContainer');
                    break;
                case 'ViewCategory':
                    Common.showDiv('dCheckoutContainer');
                    break;
                default:
                    if (reloadTarget != undefined) {
                        commerce.getCartItems(reloadTarget);
                    }
                    break;

            }


        },
        onFailure: function(xhrResponse) {
            $('sMessage').innerHTML = xhrResponse.responseText;
        }
    });
    } else {
        $(quantityText).addClassName('errorNull');
    }
}

//commerce.getCartItems = function(sCmd, sourceDiv, targetDiv)
//{
//    new Ajax.Request('util/commerceShoppingCart.aspx', {
//        method: 'post',
//        parameters: { cmd: sCmd },
//        onSuccess: function(xhrResponse)
//        {
//            var d = new Element('div').update(xhrResponse.responseText);
//            
//            $(targetDiv).innerHTML = $(d).select('div[id="' + sourceDiv + '"]')[0].innerHTML;
//        },
//        onFailure: function(xhrResponse)
//        {
//            $(targetDiv).innerHTML = xhrResponse.responseText;
//        }
//    });
//}

commerce.getCartItems = function(targetDiv) {
    new Ajax.Request('util/commerceShoppingCart.aspx', {
        method: 'post',
        parameters: { cmd: 'GetCartItems' },
        onSuccess: function(xhrResponse) {
            var d = new Element('div').update(xhrResponse.responseText);
            $(targetDiv).innerHTML = $(d).select('div[id="dShoppingCart"]')[0].innerHTML;
        },
        onFailure: function(xhrResponse) {
            alert('there was a problem');
        }
    });
}

//commerce.removeCartItem = function(index, reloadTarget)
//{
//    new Ajax.Request('util/commerceShoppingCart.aspx', {
//        method: 'post',
//        parameters: { cmd: 'RemoveCartItem', Index: index },
//        onSuccess: function(xhrResponse)
//        {
//            if(reloadTarget != undefined)
//            {
//                commerce.getCartItems('GetCartItems2', 'dCartItems2', reloadTarget);
//            }
//        },
//        onFailure: function(xhrResponse)
//        {
//        }
//    });
//}

commerce.removeCartItem = function(index, reloadTarget) {
    new Ajax.Request('util/commerceShoppingCart.aspx', {
        method: 'post',
        parameters: { cmd: 'RemoveCartItem', Index: index },
        onSuccess: function(xhrResponse) {
            if (reloadTarget != undefined) {
                commerce.getCartItems(reloadTarget);
            }
        },
        onFailure: function(xhrResponse) {
            alert('there was a problem');
        }
    });

}

commerce.removeAllCartItems = function(reloadTarget) {
    new Ajax.Request('util/commerceShoppingCart.aspx', {
        method: 'post',
        parameters: { cmd: 'RemoveAllCartItems' },
        onSuccess: function(xhrResponse) {
            if (reloadTarget != undefined) {
                commerce.getCartItems('GetCartItems', 'dCartItems', reloadTarget);
            }
        },
        onFailure: function(xhrResponse) {
        }
    });
}

commerce.applyDiscountCode = function(txtDiscountCode, reloadTarget) {
    var discountCode = $F(txtDiscountCode);
    new Ajax.Request('util/commerceShoppingCart.aspx', {
        method: 'post',
        parameters: { cmd: 'ApplyDiscountCode', DiscountCode: discountCode },
        onSuccess: function(xhrResponse) {
            if (reloadTarget != undefined) {
                commerce.getCartItems('GetCartItems2', 'dCartItems2', reloadTarget);
            }
        },
        onFailure: function(xhrResponse) {
            $(reloadTarget).innerHTML = xhrResponse.responseText;
        }
    });
}

commerce.updateQuantities = function(reloadTarget) {

    var elms = $$('.Quantities');
    if (elms.length > 0) {
        if (DataValidation.validateRequest('dShoppingCart', 'vClass', 'errorNull')) {

            var qtys = '';
            for (var i = 0; i < elms.length; i++) {
                if (DataValidation.trim(elms[i].value) != '') {
                    qtys += DataValidation.trim(elms[i].value) + ',';
                } else {
                    qtys += '0,';
                }
            }
            new Ajax.Request('util/commerceShoppingCart.aspx', {
                method: 'post',
                parameters: { cmd: 'UpdateQuantities', Quantities: qtys },
                onSuccess: function(xhrResponse) {
                    if (reloadTarget != undefined) {
                        //commerce.getCartItems('GetCartItems2', 'dCartItems2', reloadTarget);
                        commerce.getCartItems('dShoppingCart');
                    }
                },
                onFailure: function(xhrResponse) {
                    $(reloadTarget).innerHTML = xhrResponse.responseText;
                }
            });
        }
    }
}

commerce.getCartItemCount = function(targetDiv) {
    new Ajax.Request('util/commerceShoppingCart.aspx', {
        method: 'post',
        parameters: { cmd: 'GetCartItemCount' },
        onSuccess: function(xhrResponse) {
            var d = new Element('div').update(xhrResponse.responseText);

            $(targetDiv).innerHTML = $(d).select('div[id="dReturnValue"] .ReturnValue')[0].innerHTML;
        },
        onFailure: function(xhrResponse) {
        }
    });
}

commerce.validateBillingInformation = function() {
    if (DataValidation.validateRequest('billingInfoContainer', 'vClass', 'errorNull') && DataValidation.validateRequest('creditCardContainer', 'vClass', 'errorNull', $$('select[id$="cardType"]')[0].id)) {
        //$('ctl00_ContentPlaceHolder1_hfValidated').value = 'true';
        $$('input[id$="billingInfoValidated"]')[0].value = 'true';
        document.forms[0].submit();
        //document.location.href = 'CheckoutReview.aspx';
    }
    else {

    }
}

commerce.validateShippingInformation = function() {
    if (DataValidation.validateRequest('dShippingInformation', 'vClass', 'errorNull') && DataValidation.validateRequest('dUserInformation', 'vClass', 'errorNull')) {
        $('ctl00_ContentPlaceHolder1_hfValidated').value = 'true';
        document.forms[0].submit();
    }
    else {

    }
}

commerce.showCheckoutModal = function() {
    Common.showDiv('dCheckoutCOntainer');
}

commerce.setImage = function(imgPath, imgContainer, thumbHeight, thumbWidth) {

    $$('img[id$="' + imgContainer + '"]')[0].src = 'handlers/DynamicPicture.ashx?Target=' + imgPath + '&Height=' + thumbHeight + '&Width=' + thumbWidth;
    //$('sMessage').innerHTML = 'handlers/DynamicPicture.ashx?Target=' + imgPath + '&Height=125&With=125';
    //$(imgContainer).src = imgPath;

}

