﻿function toggleShipping() {
    PageMethods.GetShoppingCart(OnGetShoppingCartCompleted, OnGetShoppingCartFailed);
}

function removeItem(itemID) {
    PageMethods.RemoveItemAndRefresh(itemID, OnGetShoppingCartCompleted, OnGetShoppingCartFailed);
}

function updateItemQuantity(itemID, quantity) {
    PageMethods.UpdateItemQuantity(itemID, quantity, OnGetShoppingCartCompleted, OnUpdateItemQuantityFailed);
}

function OnGetShoppingCartCompleted(result, userContext, methodName) {
    $get("divShoppingCartStatus").style.display = 'none';
    $get("divShoppingCart").innerHTML = result;

    showHideAddressForm();
}

function OnGetShoppingCartFailed(error, userContext, methodName) {
    $get("divShoppingCartStatus").style.display = 'none';
    if (error != null)
        alert(error.get_message());
}

function OnUpdateItemQuantityFailed(error, userContext, methodName) {
    // do nothing, user likely clicked Proceed to Checkout
}

function showGettingCart() {
    $get("divShoppingCartStatus").style.display = 'block';
}

function generateCart() {
    showGettingCart();
    PageMethods.GetShoppingCart(OnGetShoppingCartCompleted, OnGetShoppingCartFailed);
}

function validateForm() {
    $get("btnProceed").disabled = true;

    var fname = $get("txtFirstName").value;
    var lname = $get("txtLastName").value;
    var add1 = $get("txtAddress1").value;
    var add2 = $get("txtAddress2").value;
    var city = $get("txtCity").value;
    var state = $get("lstState").value;
    var zip = $get("txtZip").value;
    var country = $get("lstCountry").value;
    var comments = $get("txtComments").value;
    var bypassShipping = $("#chkBypassShipping")[0].checked;

    PageMethods.ValidateForm(fname, lname, add1, add2, city, state, zip, country, comments, bypassShipping, OnValidateFormCompleted, OnValidateFormFailed);
}

// refresh the cart once more (to update shipping)
function OnValidateFormCompleted() {
    PageMethods.GetShoppingCart(OnProceedGetShoppingCartCompleted, OnGetShoppingCartFailed);
}

function OnValidateFormFailed(error, userContext, methodName) {
    if (error._message == "shopping_cart_changed") {
        alert("Could not advance to checkout page. One or more items may no longer be available.");

        PageMethods.GetShoppingCart(OnGetShoppingCartCompleted, OnGetShoppingCartFailed);
    }
    else {
        alert(error._message);
    }
    $get("btnProceed").disabled = false;
}

function showHideAddressForm() {
    if ($get("divShoppingCart").innerHTML.indexOf("There are no items") > -1)
        $get("divAddressForm").style.display = 'none';
    else
        $get("divAddressForm").style.display = 'block';
}


// everything validated, transfer values and submit to PayPal
function OnProceedGetShoppingCartCompleted() {

    if ($("#chkBypassShipping")[0].checked) {
        // add a shipping_x field for every item with shipping = 0.00
        var items = $("input[name*='item_name']");
        for (var x = 1; x <= items.length; x++) {
            $("#frmShoppingCart").append("<input type=hidden name='shipping_" + x + "' value='0.00' />");
        }
    }

    var cart = $get("frmShoppingCart");
    cart["first_name"].value = $get("txtFirstName").value;
    cart["last_name"].value = $get("txtLastName").value;
    cart["address1"].value = $get("txtAddress1").value;
    cart["address2"].value = $get("txtAddress2").value;
    cart["city"].value = $get("txtCity").value;
    cart["state"].value = $get("lstState").value;
    cart["zip"].value = $get("txtZip").value;
    cart["country"].value = $get("lstCountry").value;

    $get("frmShoppingCart").submit();
}


