function ApOnShoppingCart() {

}

ApOnShoppingCart.init = function()
{
    if (ApOnShoppingCart.initialized) {
        return;
    }
    ApOnShoppingCart.courtain = createFloatingElement("div", "ApOnShoppingCartCourtain");
    ApOnShoppingCart.courtain.style.position = "fixed";
    ApOnShoppingCart.courtain.style.display = "none";

    ApOnShoppingCart.lightbox = createFloatingElement("div", "ApOnShoppingCartLightbox");
    ApOnShoppingCart.lightbox.style.display = "none";
    ApOnShoppingCart.lightbox.innerHTML = "loading...";


    ApOnShoppingCart.courtain.onclick = ApOnShoppingCart.hide;

    ApOnShoppingCart.initialized = true;
}

ApOnShoppingCart.show = function(instance)
{
    //show the shopping cart
    ApOnShoppingCart.showGeneric("cart", instance);
}

ApOnShoppingCart.showGeneric = function(type, instance, data)
{
    if (instance) {
        ApOnShoppingCart.instance = instance;
    }
    ApOnShoppingCart.showType = type;
    ApOnShoppingCart.init();

    ApOnShoppingCart.courtain.style.display = "block";
    ApOnShoppingCart.lightbox.style.display = "block";
    ApOnShoppingCart.lightbox.innerHTML = "loading...";

    var maxFrame = 250;

    var animationFunction = function (frame) {
        var opacity = 0.7 * frame / maxFrame;
        if (document.all) {
            //only ie
            ApOnShoppingCart.courtain.style.filter = "alpha(opacity=" + Math.floor(opacity * 100) + ")";
        } else {
            ApOnShoppingCart.courtain.style.opacity = opacity;
        }
        var width = 700 * (frame/maxFrame);
        var height = 500 * (frame/maxFrame);
        var pageWidth = ApOnShoppingCart.courtain.offsetWidth;
        var pageHeight = ApOnShoppingCart.courtain.offsetHeight;
        setElementSizeAndPosition(ApOnShoppingCart.lightbox, (pageWidth - width) / 2, (pageHeight - height) / 2, width, height);
        if (frame == maxFrame) {
            if (ApOnShoppingCart.showType == "cart") {
                ApOnShoppingCart.loadShoppingCart();
            } else if (ApOnShoppingCart.showType == "daily delivery" || ApOnShoppingCart.showType == "daily_business delivery") {
                ApOnShoppingCart.loadDailyDelivery(data.productId);
            }
        }
    }

    var animation = new _aponAnimation(0, maxFrame, 25, "fading", animationFunction);
    animation.start();
}

ApOnShoppingCart.hide = function()
{
    ApOnShoppingCart.courtain.style.display = "none";
    ApOnShoppingCart.lightbox.style.display = "none";
}

ApOnShoppingCart.loadShoppingCart = function ()
{
    var additionalData = new Array();
    additionalData["call"] = "shopping_cart";
    ApOnWebpart.putInnerHTML("shop", "_base", ApOnShoppingCart.instance, additionalData, ApOnShoppingCart.loadShoppingCartCallback)
}

ApOnShoppingCart.loadShoppingCartCallback = function (response)
{
    ApOnShoppingCart.lightbox.style.height = "auto";
    ApOnShoppingCart.lightbox.innerHTML = response.text;
}

ApOnShoppingCart.pickMethod = function()
{
    if ($("shoppingCartEmpty")) {
        return;
    }
    ApOnShoppingCart.selectedMethod = "";
    if ($("paymentGoogleCheckout") && $("paymentGoogleCheckout").checked) {
        ApOnShoppingCart.selectedMethod = "google_checkout";
    }
    if ($("paymentPaypal") && $("paymentPaypal").checked) {
        ApOnShoppingCart.selectedMethod = "paypal";
    }
    if ($("paymentKorta") && $("paymentKorta").checked) {
        ApOnShoppingCart.selectedMethod = "korta";
    }
    if ($("paymentBankTransfer") && $("paymentBankTransfer").checked) {
        ApOnShoppingCart.selectedMethod = "bank_transfer";
    }
    if (ApOnShoppingCart.selectedMethod == "") {
        $("shoppingCartChooseMethod").style.display = "none";
        $("shoppingCartChooseMethodAlert").style.display = "inline";
        return;
    }

    $("shoppingCartChangeMethodButton").style.display = "block";
    $("shoppingCartPickMethodButton").style.display = "none";
    $("shoppingCartOnToPaymentButton").style.display = "block";

    $("shoppingCartPaymentChoices").style.display = "none";
    if (ApOnShoppingCart.selectedMethod == "google_checkout" && $("shoppingCartPaymentMethodGoogleCheckout")) {
        $("shoppingCartPaymentMethodGoogleCheckout").style.display = "block";
    } else if (ApOnShoppingCart.selectedMethod == "paypal" && $("shoppingCartPaymentMethodPaypal")) {
        $("shoppingCartPaymentMethodPaypal").style.display = "block";
    } else if (ApOnShoppingCart.selectedMethod == "korta" && $("shoppingCartPaymentMethodKorta")) {
        $("shoppingCartPaymentMethodKorta").style.display = "block";
    } else if (ApOnShoppingCart.selectedMethod == "bank_transfer" && $("shoppingCartPaymentMethodBankTransfer")) {
        $("shoppingCartPaymentMethodBankTransfer").style.display = "block";
    }
}

ApOnShoppingCart.changeMethod = function()
{
    //buttons
    $("shoppingCartChangeMethodButton").style.display = "none";
    $("shoppingCartPickMethodButton").style.display = "block";
    $("shoppingCartOnToPaymentButton").style.display = "none";
    //choices
    $("shoppingCartPaymentChoices").style.display = "block";
    //methods
    if ($("shoppingCartPaymentMethodGoogleCheckout")) {
        $("shoppingCartPaymentMethodGoogleCheckout").style.display = "none";
    }
    if ($("shoppingCartPaymentMethodPaypal")) {
        $("shoppingCartPaymentMethodPaypal").style.display = "none";
    }
    if ($("shoppingCartPaymentMethodKorta")) {
        $("shoppingCartPaymentMethodKorta").style.display = "none";
    }
    if ($("shoppingCartPaymentMethodBankTransfer")) {
        $("shoppingCartPaymentMethodBankTransfer").style.display = "none";
    }
}

ApOnShoppingCart.saveDeliveryAndProceedToPaymentGateway = function(method)
{
    ApOnShoppingCart.paymentMethod = method;
    deliveryData = {};
    deliveryData.name = $("shoppingCartDeliveryTo").value;
    if (deliveryData.name.trim() == "") {
        alert("Please enter a name to be delivered to");
        $("shoppingCartDeliveryTo").focus();
        return;
    }
    deliveryData.method = $("shoppingCartDeliveryMethod").options[$("shoppingCartDeliveryMethod").selectedIndex].value;
    if (deliveryData.method == "deliver") {
        deliveryData.address1   = $("shoppingCartDeliveryAddress1").value;
        if (deliveryData.address1.trim() == "") {
            alert("Please enter your address");
            $("shoppingCartDeliveryAddress1").focus();
            return;
        }
        deliveryData.address2   = $("shoppingCartDeliveryAddress2").value;
        deliveryData.postalCode = $("shoppingCartDeliveryPostalCode").value;
        if (deliveryData.postalCode.trim() == "") {
            alert("Please enter your postal code");
            $("shoppingCartDeliveryPostalCode").focus();
            return;
        }
        deliveryData.city       = $("shoppingCartDeliveryCity").value;
        if (deliveryData.city == "") {
            alert("Please enter your city name");
            $("shoppingCartDeliveryCity").focus();
            return;
        }
        if ($("shoppingCartDeliveryCountry").options) {
            deliveryData.country = $("shoppingCartDeliveryCountry").options[$("shoppingCartDeliveryCountry").selectedIndex].value;
            if (deliveryData.country == "") {
                alert("Please choose your country");
                return;
            }
        } else {
            deliveryData.country = $("shoppingCartDeliveryCountry").value;
        }
    }
    ApOnAjax.saveCartDelivery(deliveryData, ApOnShoppingCart.saveDeliveryAndProceedToPaymentGatewayCallback);
}

ApOnShoppingCart.saveDeliveryAndProceedToPaymentGatewayCallback = function(result)
{
    ApOnShoppingCart.proceedToPaymentGateway(ApOnShoppingCart.paymentMethod);
}

ApOnShoppingCart.proceedToPaymentGateway = function(method)
{
    if (method != "") {
        ApOnShoppingCart.selectedMethod = method;
    }
    $("shopCartDelivery").style.display = "none";

    if (ApOnShoppingCart.selectedMethod == "paypal") {
        if ($("shoppingCartPaypalForm")) {
            $("shoppingCartPaypalForm").submit();
        }
    } else if (ApOnShoppingCart.selectedMethod == "google_checkout") {
        if ($("shoppingCartGoogleCheckoutForm")) {
            $("shoppingCartGoogleCheckoutForm").submit();
        }
    } else if (ApOnShoppingCart.selectedMethod == "korta") {
        if ($("shoppingCartDetailsForKorta")) {
            $("shopCartBeforePayment").style.display = "none";
            $("shoppingCartDetailsForKorta").style.display = "block";
        }
    }
}

 ApOnShoppingCart.increment = function(id, unitPrice)
{
    //convert to integer
    $("txtCart_" + id + "_Quantity").value++;
    if ($("shoppingCartPaypal_" + id + "_Quantity")) {
        $("shoppingCartPaypal_" + id + "_Quantity").value = $("txtCart_" + id + "_Quantity").value;
    }
    if ($("shoppingCartItems")) {
        $("shoppingCartItems").innerHTML = ($("shoppingCartItems").innerHTML - 0) + 1;
    }
    ApOnShoppingCart.updatePrice(id, unitPrice);
}

ApOnShoppingCart.decrement = function(id, unitPrice)
{
    if ($("txtCart_" + id + "_Quantity").value > 0) {
        if ($("txtCart_" + id + "_Quantity").value == 1) {
            if (!confirm("Would you like to remove this item from the cart?")) {
                return;
            }
        }
        $("txtCart_" + id + "_Quantity").value--;
        if ($("shoppingCartPaypal_" + id + "_Quantity")) {
            $("shoppingCartPaypal_" + id + "_Quantity").value = $("txtCart_" + id + "_Quantity").value;
        }
        if ($("shoppingCartItems") && ($("shoppingCartItems").innerHTML - 0) > 0) {
            $("shoppingCartItems").innerHTML = $("shoppingCartItems").innerHTML - 1;
        }
        ApOnShoppingCart.updatePrice(id, unitPrice);
    }
}

ApOnShoppingCart.updatePrice = function(id, unitPrice)
{
    var oldTotalPrice = $("txtCart_" + id + "_TotalPrice").innerHTML - 0;
    unitPrice = unitPrice - 0;
    var newQuantity = $("txtCart_" + id + "_Quantity").value - 0;
    $("txtCart_" + id + "_TotalPrice").innerHTML = newQuantity * unitPrice;
    $("txtCart_TotalCartPrice").innerHTML = ($("txtCart_TotalCartPrice").innerHTML - 0) - oldTotalPrice + newQuantity * unitPrice;
    if (newQuantity == 0) {
        ApOnAjax.updateQuantityOnCart(id, newQuantity, ApOnShoppingCart.loadShoppingCart);
    } else {
        ApOnAjax.updateQuantityOnCart(id, newQuantity);
    }
}


ApOnShoppingCart.addToCart = function(productId, quantity, confirmMessage)
{
    if (confirmMessage == "" || confirm(confirmMessage)) {
        ApOnAjax.addProductToCart(productId, quantity, ApOnShoppingCart.addToCartCallback);
    }
}

ApOnShoppingCart.addToCartDeliverable = function(productId, quantity, type, max, alreadyAdded, message, instance)
{
    if (type == "once") {
        ApOnShoppingCart.addToCart(productId, quantity, message);
    } else if (type == "daily" || type == "daily_business") {
        ApOnShoppingCart.showGeneric(type + " delivery", instance, {productId: productId});
        //show daily add
    } else if (type == "weekly") {
        //show weekly add
    } else if (type == "monthly") {

    }
}

ApOnShoppingCart.addToWishlist = function(productId, confirmMessage)
{
    if (confirmMessage == "" || confirm(confirmMessage)) {
        if ($("shoppingCartItems")) {
            $("shoppingCartItems").innerHTML = ($("shoppingCartItems").innerHTML - 0) + 1;
        }
        ApOnAjax.addProductToCart(productId, 1, ApOnShoppingCart.addToCartCallback);
    }
}

ApOnShoppingCart.loadDailyDelivery = function(productId)
{
    var additionalData = new Array();
    additionalData["call"] = "delivery_daily";
    additionalData["productId"] = productId;
    ApOnWebpart.putInnerHTML("shop", "_base", ApOnShoppingCart.instance, additionalData, ApOnShoppingCart.loadDailyDeliveryCallback)
}

ApOnShoppingCart.loadDailyDeliveryCallback = function (response)
{
    ApOnShoppingCart.lightbox.style.height = "auto";
    ApOnShoppingCart.lightbox.innerHTML = response.text;
}

ApOnShoppingCart.addToCartDeliverableDaily = function(productId)
{
    var quantity =  Math.floor($('shopDeliveryDailyQuantity').value - 0);
    if (quantity <= 0) {
        alert("Please enter a positive quantity");
        $('shopDeliveryDailyQuantity').focus();
        return;
    }
    var deliveryData = {};
    deliveryData.type      = "daily";
    deliveryData.startDate = $("shopDeliveryDailyStartDate").value;
    deliveryData.endDate   = $("shopDeliveryDailyEndDate").value;
    deliveryData.weekdays = {}
    var weekdayCount = 0;
    for(var weekday = 1; weekday <= 7; weekday++) {
        deliveryData.weekdays[weekday] = $("shopDeliveryDailyWeekDay_" + weekday).checked;
        if ($("shopDeliveryDailyWeekDay_" + weekday).checked) {
            weekdayCount++;
        }
    }
    if (weekdayCount == 0) {
        alert("Please select at least one weekday");
        return;
    }
    ApOnAjax.addProductToCart(productId, quantity, deliveryData, ApOnShoppingCart.addToCartDailyCallback);
}

ApOnShoppingCart.addToCartCallback = function (result)
{
}

ApOnShoppingCart.addToCartDailyCallback = function (result)
{
    ApOnShoppingCart.hide();
}

ApOnShoppingCart.backToCart = function()
{
    $("shopCartBeforePayment").style.display = "block";
    $("shopCartDelivery").style.display = "none";
}

ApOnShoppingCart.proceedToDelivery = function()
{
    $("shopCartBeforePayment").style.display = "none";
    $("shopCartDelivery").style.display = "block";
    $("shoppingCartDeliveryTo").focus();
}

ApOnShoppingCart.pickDelivery = function(select)
{
    var location = select.options[select.selectedIndex].value;
    $("shopDeliveryDetails").style.display = location == "deliver" ? "block" : "none";
}

ApOnShoppingCart.verifyDelivery = function()
{
    var deliveryData = {};
    deliveryData.to         = $("shoppingCartDeliveryTo").value.trim();
    deliveryData.address1   = $("shoppingCartDeliveryAddress1").value.trim();
    deliveryData.address2   = $("shoppingCartDeliveryAddress2").value.trim();
    deliveryData.city       = $("shoppingCartDeliveryCity").value.trim();
    deliveryData.region     = $("shoppingCartDeliveryRegion").value.trim();
    deliveryData.postalCode = $("shoppingCartDeliveryPostalCode").value.trim();
    deliveryData.country    = $("shoppingCartDeliveryCountry").value.trim();

    if (deliveryData.to == "") {
        alert("Please enter a name to deliver to.");
        $("shoppingCartDeliveryTo").focus();
        return false;
    }

    if (deliveryData.address1 == "") {
        alert("Please enter the address to deliver to.");
        $("shoppingCartDeliveryAddress1").focus();
        return false;
    }

    if (deliveryData.city == "") {
        alert("Please enter the name of the city to deliver to.");
        $("shoppingCartDeliveryCity").focus();
        return false;
    }

    if (deliveryData.region == "") {
        alert("Please enter the name of the city to deliver to.");
        $("shoppingCartDeliveryCity").focus();
        return false;
    }

    if (deliveryData.postalCode == "") {
        alert("Please enter the postal code to deliver to.");
        $("shoppingCartDeliveryPostalCode").focus();
        return false;
    }

    if (deliveryData.country == "") {
        alert("Please enter the name of the country to deliver to.");
        $("shoppingCartDeliveryPostalCode").focus();
        return false;
    }

    return deliveryData();
}

ApOnShoppingCart.submitWishlist = function()
{
    var data = {};
    data.name = $("shopWishlistFormName").value.trim();
    if (data.name == "") {
        alert("Please enter your name.");
        $("shopWishlistFormName").focus();
        return;
    }
    data.company = $("shopWishlistFormCompany").value.trim();
    data.email = $("shopWishlistFormEmail").value.trim();
    if (!isEmail(data.email)) {
        alert("Please enter a valid contact email address.");
        $("shopWishlistFormEmail").focus();
        return;
    }
    data.message = $("shopWishlistFormMessage").value.trim();
    if (data.message == "") {
        alert("Please enter your message.");
        $("shopWishlistFormMessage").focus();
        return;
    }
    data.language = PAGE_LANGUAGE;
    //submit the data
    ApOnAjax.shopWishlistQuery(data, ApOnShoppingCart.submitWishlistCallback);
}

ApOnShoppingCart.submitWishlistCallback = function(result)
{
    $("shopWishListReview").style.display = "none";
    $("shopWishListConfirm").style.display = "block";
}
