﻿var ShoppingCart = {
    Remove: function(index) {
        B2CPrint.G1.WebSite.ShoppingCart.Method.Remove(index, this.RemoveOnSucceeded, this.RemoveOnFailed);
    },

    RemoveOnSucceeded: function(data, args, methodName) {
        if (data.ActionSucceeded != undefined && !data.ActionSucceeded) {
            var MessageObject = {
                ErrorMessage: data.ErrorMessage,
                ErrorDescription: data.ErrorDescription,
                ErrorNumber: data.ErrorNumber,
                ErrorState: data.ErrorState,
                ErrorType: data.ErrorType,
                ErrorSource: data.ErrorSource
            };
            DisplayMessage(MessageObject);
        }
        else {
            window.location = "/ShoppingCart/";
        }
    },

    RemoveOnFailed: function(error, userContext, methodName) {
        var MessageObject = {
            ErrorMessage: error._message,
            ErrorDescription: error.timedOut ? Translate('"Common..Error.Timeout"') : error._message,
            ErrorID: error._statusCode,
            ErrorState: 1
        };
        DisplayMessage(MessageObject);
    },


    ApplyCoupon: function() {
        if (!$(".ShoppingCartCouponTable").jForm().validate()) return;
        var CouponCode = $("#fldCouponCode").val();
        B2CPrint.G1.WebSite.ShoppingCart.Method.ApplyCoupon(CouponCode, this.ApplyCouponOnSucceeded, this.ApplyCouponOnFailed);
    },

    ApplyCouponOnSucceeded: function(data, args, methodName) {
        if (data.ActionSucceeded != undefined && !data.ActionSucceeded) {
            var MessageObject = {
                ErrorMessage: data.ErrorMessage,
                ErrorDescription: data.ErrorDescription,
                ErrorNumber: data.ErrorNumber,
                ErrorState: data.ErrorState,
                ErrorType: data.ErrorType,
                ErrorSource: data.ErrorSource
            };
            DisplayMessage(MessageObject);
        }
        else {
            $('#TotalDiscountValue .pricevalue').html(parseFloat(data.DiscountPrice).formatMoney(2, '.', ','));
            $('#TotalVATValue .pricevalue').html(parseFloat(data.VAT).formatMoney(2, '.', ','));
            $('#TotalChargeValue .pricevalue').html(parseFloat(data.TotalPrice).formatMoney(2, '.', ','));
            $('#SubTotalValue .pricevalue').html(parseFloat(data.SubTotalPrice).formatMoney(2, '.', ','));
            $('.CouponDiscountValue').show();
        }
    },

    ApplyCouponOnFailed: function(error, userContext, methodName) {
        var MessageObject = {
            ErrorMessage: error._message,
            ErrorDescription: error.timedOut ? Translate('"Common..Error.Timeout"') : error._message,
            ErrorID: error._statusCode,
            ErrorState: 1
        };
        DisplayMessage(MessageObject);
    },


    UpdateStateCode: function() {
        var StateCode = $("#fldState").val();
        B2CPrint.G1.WebSite.ShoppingCart.Method.UpdateStateCode(StateCode, this.UpdateStateCodeOnSucceeded, this.UpdateStateCodeOnFailed);
    },

    UpdateStateCodeOnSucceeded: function(data, args, methodName) {
        if (data.ActionSucceeded != undefined && !data.ActionSucceeded) {
            var MessageObject = {
                ErrorMessage: data.ErrorMessage,
                ErrorDescription: data.ErrorDescription,
                ErrorNumber: data.ErrorNumber,
                ErrorState: data.ErrorState,
                ErrorType: data.ErrorType,
                ErrorSource: data.ErrorSource
            };
            DisplayMessage(MessageObject);
        }
        else {
            $('#TotalVATValue').html(parseFloat(data.VAT).formatMoney(2, '.', ','));
            $('#TotalChargeValue').html(parseFloat(data.TotalPrice).formatMoney(2, '.', ','));
            $('#SubTotalValue').html(parseFloat(data.SubTotalPrice).formatMoney(2, '.', ','));
        }
    },

    UpdateStateCodeOnFailed: function(error, userContext, methodName) {
        var MessageObject = {
            ErrorMessage: error._message,
            ErrorDescription: error.timedOut ? Translate('"Common..Error.Timeout"') : error._message,
            ErrorID: error._statusCode,
            ErrorState: 1
        };
        DisplayMessage(MessageObject);
    },

    Checkout: function() {

        var BillingDetailsValidated = false;
        var ShippingDetailsValidated = false;
        BillingDetailsValidated = $(".BillingDetails").jForm().validate();
        if ($("#fldDifferentShippingAddress:checked").length == 1) {
            ShippingDetailsValidated = $("#ShippingDetails").jForm().validate();
        }
        else {
            ShippingDetailsValidated = true;
        }

        if (!BillingDetailsValidated || !ShippingDetailsValidated) return;
        //if (!$(".BillingDetails").jForm().validate()) return;
        //if ($("#fldDifferentShippingAddress:checked").length == 1 && !$("#ShippingDetails").jForm().validate()) return;
        var ChargeData = this.CombineData();
        B2CPrint.G1.WebSite.ShoppingCart.Method.Charge(ChargeData, this.CheckoutOnSucceeded, this.CheckoutOnFailed);
    },

    CheckoutOnSucceeded: function(data, args, methodName) {
        if (data.ActionSucceeded != undefined && !data.ActionSucceeded) {
            var MessageObject = {
                ErrorMessage: data.ErrorMessage,
                ErrorDescription: data.ErrorDescription,
                ErrorNumber: data.ErrorNumber,
                ErrorState: data.ErrorState,
                ErrorType: data.ErrorType,
                ErrorSource: data.ErrorSource
            };
            DisplayMessage(MessageObject);
        }
        else {
            window.location = Constants.TEU + '?GUID=' + data.GUID + '&DBN=' + Constants.DBName;
        }
    },

    CheckoutOnFailed: function(error, userContext, methodName) {
        var MessageObject = {
            ErrorMessage: error._message,
            ErrorDescription: error.timedOut ? Translate('"Common..Error.Timeout"') : error._message,
            ErrorID: error._statusCode,
            ErrorState: 1
        };
        DisplayMessage(MessageObject);
    },

    CombineData: function() {
        var D = {};
        
        D.FirstName = $("#fldFirstName").val();
        D.LastName = $("#fldLastName").val();
        D.PhoneNumber = $("#fldPhoneNumber").val();
        D.MobilePhoneNumber = $("#fldMobilePhoneNumber").val();
        D.FaxNumber = $("#fldFaxNumber").val();
        D.Address = $("#fldAddress").val();
        D.StateCode = $("#fldState").val();
        D.City = $("#fldCity").val();
        D.ZipCode = $("#fldZIP").val();
        D.Email = $("#fldEmail").val();


        D.SignMeAsNewUser = ($("#fldSignMeAsNewUser:checked").length == 1);

        if ($("#fldDifferentShippingAddress:checked").length == 1) {
            D.ShippingAddressIsDifferent = true;
            D.ShippingFirstName = $("#fldShippingFirstName").val();
            D.ShippingLastName = $("#fldShippingLastName").val();
            D.ShippingAddress = $("#fldShippingAddress").val();
            D.ShippingCity = $("#fldShippingCity").val();
            D.ShippingStateCode = $("#fldShippingState").val();
            D.ShippingZipCode = $("#fldShippingZIP").val();
        }
        else {
            D.ShippingAddressIsDifferent = false;
        }

        return D;
    }
};
