﻿function color_changed(attributeValueId, productId, culture) {
    $.post("/ajax_files/ajax_changeColor.ashx", { productID: productId, attributeValueID: attributeValueId }, function(response) {
        //alert("data loaded2: " + response);
        var splitResp = response.split("::");
        document.getElementById('a' + productId).href = splitResp[0];
        document.getElementById('prodImage' + productId).src = splitResp[1];

        if (splitResp[5] != "") {
            document.getElementById('prodImage' + productId).style.top = splitResp[5] + 'px';
        }
        else
        { document.getElementById('prodImage' + productId).style.top = '0px'; }

        document.getElementById('prodImage' + productId).height = splitResp[2];
        document.getElementById('prodImage' + productId).width = splitResp[3];
        document.getElementById('prodImage' + productId).alt = splitResp[4];

        // Check product status
        var isProductInCart = splitResp[6];
        var isProductInStock = splitResp[7];
        if (isProductInStock != "True") {

            // Hide add to cart button
            $('#addToCartButton' + productId).hide();

            // Show product message
            $('#productMessage' + productId).show();
            if (culture == 'zh-CN')
                $('#productMessage' + productId).text("无现货");
            else
                $('#productMessage' + productId).text("Out of stock");
        }
        else if (isProductInCart == "True") {
            // Hide add to cart button
            $('#addToCartButton' + productId).hide();

            // Show product message
            $('#productMessage' + productId).show();
            if (culture == 'zh-CN')
                $('#productMessage' + productId).text("已添加至购物车");
            else
                $('#productMessage' + productId).text("Already in cart");

        }
        else {

            // Show add to cart button
            $('#addToCartButton' + productId).show();

            // Hide product message
            $('#productMessage' + productId).hide();
        }
    });
}

function ajaxChangeQuantity(productId, attributeValueId, quantity) {
    if (quantity > 0)
    {
        $.post("/ajax_files/ajax_changeQuantity.ashx", { ProductID: productId, AttributeValueID: attributeValueId, Quantity: quantity }, function(response) {
            if (response != "")
            {
                var splitResp = response.split("::");
                document.getElementById('divPrice' + productId + "-" + attributeValueId).innerHTML=splitResp[0];
                document.getElementById('spanProductWeight' + productId + "-" + attributeValueId).innerHTML=splitResp[5];
                document.getElementById('spanProductVolume' + productId + "-" + attributeValueId).innerHTML=splitResp[6];
                document.getElementById('spItems').innerHTML = splitResp[1];
                document.getElementById('spanOrderSubtotal').innerHTML =splitResp[2];
                document.getElementById('spanOrderWeight').innerHTML =splitResp[3];
                document.getElementById('spanOrderVolume').innerHTML =splitResp[4];
            }
        });
    }
    else
        alert("Product quantity should be positive.");
}

// Update cart ajax functions

// Add product from cart ajax
function ajaxAddToCart(productId, attributeValueId, culture) {
    //alert("productID=" + productId);
    //alert("attributeValueId=" + attributeValueId);
    //debugger;
    $.post("/ajax_files/ajax_addToCart.ashx", { productID: productId, attributeValueID: attributeValueId, wrapperId: "headerCart" }, function(response) {
        var splitResp = response.split("::");
        //alert("html: " + splitResp[0]);

        // Hide add to cart button
        $('#addToCartButton' + productId).hide();

        // Show product message
        $('#productMessage' + productId).show();
        if (culture == 'zh-CN')
            $('#productMessage' + productId).text("已添加至购物车");
        else
            $('#productMessage' + productId).text("Already in cart");

        //renew number of items in the cart
        document.getElementById('spItems').innerHTML =splitResp[1];

        //get rendered GridView from ajax file
        document.getElementById('headerCart').innerHTML = splitResp[0];
        
        //open cart
        $('#headerCart').show();
    });
}

// Delete product from cart ajax
function deleteCartProduct(productId, attributeValueId, wrapperId) {$.post("/ajax_files/DeleteCartProduct.ashx", { productId: productId, attributeValueId: attributeValueId, wrapperId: wrapperId }, refreshCart); }


function showCart(pageIndex, wrapperId) {
    //alert("wrapperId=" + wrapperId);
$.post("/ajax_files/ajax_RefreshCart.ashx", { pageIndex: pageIndex, wrapperId: wrapperId }, refreshCart); }

// Common refresh cart ajax handler
function refreshCart(response) {
     //alert("data loaded: " + response);
    
    var splitResp = response.split("::");
    //alert("html: " + splitResp[0]);
    if (null != splitResp[1]) {
    
        //renew number of items in the cart
        document.getElementById('spItems').innerHTML =splitResp[2];

         //Hide product message
        $('#productMessage' + splitResp[1]).hide();
        
        // Show add to cart button
        $('#addToCartButton' + splitResp[1]).show();
    }
    // document.getElementById("#<%= this.WrapperID %>").innerHTML =response;
    document.getElementById('headerCart').innerHTML = splitResp[0];
}
