/*
 * @fileoverview Functions for interactive with the Macallan shop web services via js
 *
 * @author Rolo M
 * 	
 */
webServiceUrl = '/services/ShopService.asmx';

    function getWebServiceUrl(operation) {
        return webServiceUrl + '/' + operation;
    };

    function addToBasket(productCode) {
        // increment the basket immediately so it looks instane in the front end
        incrementBasketItemText();
        var operation = 'AddToBasket'
        $.post(getWebServiceUrl(operation), { 'code': productCode },
        function(data) {
            // now set the basket count in the front end to the actual basket value
            var itemCount = data.childNodes[0].textContent
            setBasketItemText(itemCount);
        });
    };

    function updateBasketItemCount() {
        var operation = 'BasketItemCount'
        $.post(getWebServiceUrl(operation),
        function(data) {
            var itemCount = data.childNodes[0].textContent
            setBasketItemText(itemCount);
        });
    };

    function setBasketItemText(newAmount) {
        $('#basketCount').text(newAmount);
    };

    function getBasketItemText(newAmount) {
        return parseInt($('#basketCount').text());
    };

    function incrementBasketItemText(newAmount) {
        setBasketItemText(getBasketItemText() + 1);
    };

    function getProductStatus(productCode) {
        var operation = 'ProductStatus'
        $.post(getWebServiceUrl(operation), { 'code': productCode },
        function(data) {
            window.alert(data.childNodes[0].textContent);
        });
    };
