﻿/// <reference name="MicrosoftAjax.js" />
/// <reference path="jquery-1.3.2.js" />
/// <reference path="jquery.timers.js" />
/// <reference path="5Grapes.js" />

FiveGrapes.Client.ShoppingCart = {};

FiveGrapes.Client.ShoppingCart.Manager = function(url, cartLinkID, checkoutLinkID)
{
    this._cartLinkID = cartLinkID;
    this._checkoutLinkID = checkoutLinkID;
    this._checkoutPostBack = null;
    this._timeOut = 0;
    this._url = url;
}

FiveGrapes.Client.ShoppingCart.Manager.prototype =
{
    get_cartLinkID: function()
    {
        return this._cartLinkID;
    },

    set_cartLinkID: function(value)
    {
        this._cartLinkID = value;
    },

    get_checkoutLinkID: function()
    {
        return this._checkoutLinkID;
    },

    set_checkoutLinkID: function(value)
    {
        this._checkoutLinkID = value;
    },

    get_checkoutPostBack: function()
    {
        return this._checkoutPostBack;
    },

    set_checkoutPostBack: function(value)
    {
        this._checkoutPostBack = value;
    },

    get_timeOut: function()
    {
        return this._timeOut;
    },

    set_timeOut: function(value)
    {
        this._timeOut = value;
    },

    get_url: function()
    {
        return this._url;
    },

    set_url: function(value)
    {
        this._url = value;
    },

    sessionEnded: function()
    {
        var message;

        message = "We apologize but due to {0} minutes of inactivity, your cart has been cleared. Please try again.";
        message = String.format(message, this.get_timeOut());

        alert(message);

        if (__eCommerce) __eCommerce.deleteCart(function()
        {
            window.location.replace(window.location.href);
        });
    },

    startTimer: function()
    {
        var me = this;
        var timeout = this.get_timeOut() * 60 * 1000;

        jQuery(document).stopTime("sessionTimer");
        jQuery(document).everyTime(timeout, "sessionTimer", function()
        {
            me.sessionEnded();
        });
    },

    update: function(sessionId, onlineRetailerId)
    {
        var me = this;
        var parameters = "{ sessionId: '" + sessionId + "', onlineRetailerId: " + onlineRetailerId + " }";
        var webservice = this.get_url() + "/GetShoppingCartTotals";

        try
        {
            jQuery.ajax(
            {
                type: "POST",
                url: webservice,
                data: parameters,
                contentType: "application/json",
                dataType: "json",

                error: function(result)
                {
                    alert("An error occurred attempting to retrieve the shopping cart totals.");
                },

                success: function(result)
                {
                    var cartLink = jQuery("#" + me.get_cartLinkID());
                    var checkoutLink = jQuery("#" + me.get_checkoutLinkID());
                    var count = 0;
                    var total = 0;

                    if (result.d)
                    {
                        count = result.d.Count;
                        total = result.d.FormattedTotal;
                    }

                    if (count > 0)
                    {
                        cartLink.html(String.format("{0} bottles ({1})", count, total));
                        checkoutLink.attr("href", "javascript:" + me.get_checkoutPostBack());
                        checkoutLink.removeAttr("disabled");
                    }
                    else
                    {
                        cartLink.html("0 bottles ($0.00)");
                        checkoutLink.attr("disabled", "disabled");
                        checkoutLink.removeAttr("href");
                    }
                }
            });
        }
        catch (e)
        {
        }

        return false;
    }
}

FiveGrapes.Client.ShoppingCart.Shipping = function(postalCodeID, stateID)
{
    this._postalCodeID = postalCodeID;
    this._postBack = null;
    this._stateID = stateID;
}

FiveGrapes.Client.ShoppingCart.Shipping.prototype =
{
    get_postalCodeID: function()
    {
        return this._postalCodeID;
    },

    set_postalCodeID: function(value)
    {
        this._postalCodeID = value;
    },

    get_postBack: function()
    {
        return this._postBack;
    },

    set_postBack: function(value)
    {
        this._postBack = value;
    },

    get_stateID: function()
    {
        return this._stateID;
    },

    set_stateID: function(value)
    {
        this._stateID = value;
    },

    findState: function()
    {
        var geocoder = new GClientGeocoder();
        var me = this;
        var state = jQuery("#" + this.get_stateID());
        var zip = jQuery("#" + this.get_postalCodeID());

        if (Page_ClientValidate("Shipping"))
        {
            geocoder.getLocations(zip.val(), function(addresses)
            {
                if (addresses.Status.code != 200)
                {
                    alert(String.format("Failed to find any information for {0}", zip.val()));
                }
                else
                {
                    if (addresses.Placemark.length == 1)
                    {
                        var placemark = addresses.Placemark[0];

                        // accuracy should be 5 zip level accuracy
                        //alert(placemark.AddressDetails.Accuracy);
                        if (placemark.AddressDetails.Country.AdministrativeArea)
                        {
                            state.val(placemark.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName);
                            eval(me.get_postBack());
                        }
                        else alert(String.format("Unable to locate state for zip/postal code of {0}\nPlease check that the entered postal code is valid", zip.val()));
                    }
                    else
                    {
                        alert("Multiple addresses found\nPlease check that the entered postal code is valid");
                    }
                }

            });
        }
    }
}
