﻿/// <reference name="MicrosoftAjax.js" />
/// <reference path="jquery-1.3.2.js" />

var FiveGrapes = {};
FiveGrapes.Client = {};

FiveGrapes.getSelector = function(id)
{
    return "#" + id;
}

FiveGrapes.numberOfDaysInMonth = function(month, year) 
{
    /// <summary>
    ///
    /// </summary>
    var date = new Date(year, month + 1, 1, -1);

    return date.getDate();
}


FiveGrapes.Client.eCommerce = function(url, sessionId, onlineRetailerId)
{
    this._onlineRetailerId = onlineRetailerId;
    this._sessionId = sessionId;
    this._url = url;
}

FiveGrapes.Client.eCommerce.prototype =
{
    get_onlineRetailerId: function()
    {
        return this._onlineRetailerId;
    },

    set_onlineRetailerId: function(value)
    {
        this._onlineRetailerId = value;
    },

    get_sessionId: function()
    {
        return this._sessionId;
    },

    set_sessionId: function(value)
    {
        this._sessionId = value;
    },

    get_url: function()
    {
        return this._url;
    },

    set_url: function(value)
    {
        this._url = value;
    },

    addToCart: function(textboxId, wineBottleAttributeGroupId)
    {
        var input = $find(textboxId);
        var me = this;
        var parameters;
        var webservice = this.get_url() + "/AddToCart";
        var returnValue = false;

        try
        {
            parameters = "{ sessionId: '" + this.get_sessionId() + "'"
                + ", onlineRetailerId: " + this.get_onlineRetailerId()
                + ", wineBottleAttributeGroupId: " + wineBottleAttributeGroupId
                + ", quantity: " + input.get_value() + " }";

            jQuery.ajax(
            {
                type: "POST",
                url: webservice,
                data: parameters,
                contentType: "application/json",
                dataType: "json",

                error: function(result)
                {
                    alert("An error occurred attempting add wine to cart.");
                },

                success: function(result)
                {
                    if (result.d)
                    {
                        input.set_value("");

                        if (__cartManager)
                        {
                            __cartManager.update(me.get_sessionId(), me.get_onlineRetailerId());
                            __cartManager.startTimer();
                        }

                        //alert("Wine has been added to your cart");
                    }
                    else alert("An error occurred attempting add wine to cart.");
                }
            });
        }
        catch (e)
        {
            returnValue = false;
        }

        return returnValue;
    },

    deleteCart: function(success, error)
    {
        var me = this;
        var parameters;
        var webservice = this.get_url() + "/DeleteCart";

        try
        {
            parameters = "{ sessionId: '" + this.get_sessionId() + "'"
            + ", onlineRetailerId: " + this.get_onlineRetailerId() + " }";

            jQuery.ajax(
            {
                type: "POST",
                url: webservice,
                data: parameters,
                contentType: "application/json",
                dataType: "json",

                error: function(result)
                {
                    if (error) error();
                    alert("An error occurred attempting to delete cart.");
                },

                success: function(result)
                {
                    if (result.d)
                    {
                        if (success) success();
                    }
                    else alert("Error occurred attempting to delete cart");
                }
            });
        }
        catch (e)
        {
        }

        return false;
    },

    purchase: function(textboxId, wineBottleAttributeGroupId)
    {
        var me = this;
        var input = $find(textboxId);
        var parameters;
        var webservice = this.get_url() + "/CheckAvailability";

        if (input.get_value().length == 0 || input.get_value() == "0")
        {
            alert("Please enter a valid quantity.");
            return false;
        }

        try
        {
            this.trackSession();

            parameters = "{ sessionId: '" + this.get_sessionId() + "', onlineRetailerId: " + this.get_onlineRetailerId()
                + ", wineBottleAttributeGroupId: " + wineBottleAttributeGroupId + ", quantity: " + input.get_value() + " }";

            jQuery.ajax(
            {
                type: "POST",
                url: webservice,
                data: parameters,
                contentType: "application/json",
                dataType: "json",

                error: function(result)
                {
                    alert("An error occurred attempting add wine to cart.");
                },

                success: function(result)
                {
                    if (result.d)
                    {
                        if (result.d.HasQuantityRequested)
                        {
                            me.addToCart(textboxId, wineBottleAttributeGroupId);
                        }
                        else
                        {
                            if (result.d.AvailableQuantity <= 0)
                            {
                                alert("Wine is currently not available");
                            }
                            else
                            {
                                var message = "The number of bottles you requested is not available.\n"
                                + "Would you like to add {0} bottles to your cart?\n\n"
                                + "Click 'OK' to add to cart or click 'Cancel'";

                                message = String.format(message, result.d.AvailableQuantity);

                                if (confirm(message))
                                {
                                    input.set_value(result.d.AvailableQuantity);
                                    me.addToCart(textboxId, wineBottleAttributeGroupId);
                                }
                            }
                        }
                    }
                    else alert("Error occurred attempting to check wine inventory");
                }
            });
        }
        catch (e)
        {
        }

        return false;
    },

    trackSession: function()
    {
        var me = this;
        var parameters;
        var webservice = this.get_url() + "/TrackSession";

        try
        {
            parameters = "{ sessionId: '" + this.get_sessionId() + "'"
                + ", onlineRetailerId: " + this.get_onlineRetailerId() + " }";

            jQuery.ajax(
            {
                type: "POST",
                url: webservice,
                data: parameters,
                contentType: "application/json",
                dataType: "json",

                error: function(result)
                {
                    alert("An error occurred attempting to track session.");
                },

                success: function(result)
                {
                    // do nothing
                }
            });
        }
        catch (e)
        {
        }
    }
}
