﻿// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  RECALCULATE CART AJAX
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


function ClearCart() {
    if (confirm('Are you sure you want to remove all items from your shopping cart?')) {
        // a comma-seperated list of all item Ids in the cart
        var itemList_CS = document.getElementById("ItemList_CS").value;
        var itemList = itemList_CS.split(",");

        // set all quantities to 0
        var requestList = '';
        for (x = 0; x < itemList.length; x++) {
            if (requestList.length > 0) {
                requestList = requestList + ',';
            }
            requestList = requestList + 'QTY_' + itemList[x] + '=0';
        }

        // hide buttons
        var shoppingCartButttons = document.getElementById("ShoppingCartButtons_Client");
        if (shoppingCartButttons) {
            shoppingCartButttons.style.display = "none";
        }

        alert('calling webservice now');

        // call webservice
        ret = ShoppingCart_Recalculated.GetRecalculatedCart(requestList, OnRecalcCartComplete, OnRecalcCartError, OnRecalcCartTimeout);
    }
}

function RecalculateCart() {
    // a comma-seperated list of all item Ids in the cart
    var itemList_CS = document.getElementById("ItemList_CS").value;
    var itemList = itemList_CS.split(",");

    // validate quantity values
    var isValid = true;
    var requestList = '';
    for(x = 0;x < itemList.length; x++){
        var thisQty = document.getElementById('QTY_' + itemList[x]).value;
        if (isNaN(thisQty)) {
            alert('Please enter a valid number for the Quantity (' + thisQty + ')');
            isValid = false;
        }
        else {
            //concatenate list that will be sent to WebService
            thisQty = parseInt(thisQty);
            if (requestList.length > 0) {
                requestList = requestList + ',';
            }
            requestList = requestList + 'QTY_' + itemList[x] + '=' + thisQty;
        }
    }

    if (isValid == true) {
        // Call Webservice
        ret = ShoppingCart_Recalculated.GetRecalculatedCart(requestList, OnRecalcCartComplete, OnRecalcCartError, OnRecalcCartTimeout);
    }
}

function OnRecalcCartComplete(arg) {
    if (arg == "ERROR") {
        alert('There was a problem recalculating the shopping cart. If this problem continues please contact Dataforth customer service and we will assist you with your order.');
    }
    else {
        // refresh the HTML in the shopping cart div
        document.getElementById("shoppingCartContents").innerHTML = arg;
    }
}

function OnRecalcCartError(arg) {
    alert('Error recalculating Cart: ' + arg);
}

function OnRecalcCartTimeout(arg) {
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  ADD TO CART AJAX
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function AddItemToCart(itemID) {
    
    var qty = document.getElementById("INDEX_ID" + itemID).value;
    if (isNaN(qty)) {
        alert('Please enter a valid number for the Quanity');
    }
    else {
        // Call webservice 
        qty = parseInt(qty);
        ret = AddToCart.AddItemToCart(itemID, qty, OnAddToCartComplete, OnAddToCartError, OnAddToCartTimeout);
        // Add/remove the checkMark next to the item
        var checkMark = document.getElementById("cartCheck_" + itemID);
        if (checkMark) {
            if (qty > 0) {
                checkMark.src = "g/Checkmark.png";
            }
            else {
                checkMark.src = "g/blank.gif";
            }
        }
    }

    
    
    AddToCartIsNotInProcess = false;
}

function OnAddToCartComplete(arg) {
    // Determine the Id of the Cart Preview and set it's content
    var cartPreviewId = document.getElementById("CartPreviewId").value;
    document.getElementById(cartPreviewId).innerHTML = arg;

    // Finish up
    AddToCartIsNotInProcess = true;
}

function OnAddToCartTimeout(arg) {
    alert('Timeout on AddToCart');
    AddToCartIsNotInProcess = true;
}

function OnAddToCartError(arg) {
    alert('Error on AddToCart: ' + arg);
    AddToCartIsNotInProcess = true;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
