﻿//EditMultItemsBehavior
//Para agregar a un form

Type.registerNamespace('dev54.OPCS.eCommerce');

dev54.OPCS.eCommerce.Carrito = function() {
    dev54.OPCS.eCommerce.Carrito.initializeBase(this);

    //Miembros   
    this._CarritoContainer = null;

    this._events = null;

    this._ServiceBussy = false;
    this._ServiceLastResult = "";
    this._ServiceLastError = "";

}

dev54.OPCS.eCommerce.Carrito.prototype = {
    //*********************************************************************
    //Publicos
    set_ArticulosContainer: function(value) { this._ArticulosContainer = value; },
    get_ArticulosContainer: function() { return this._ArticulosContainer; },


    initialize: function() {
        dev54.OPCS.eCommerce.Carrito.callBaseMethod(this, 'initialize');
        this._atachEvents();
    },

    dispose: function() {
        this._detachEvents();
        dev54.OPCS.eCommerce.Carrito.callBaseMethod(this, 'dispose');
    },

    _atachEvents: function() {

    },

    _detachEvents: function() {

    },

    //*********************************************************************
    //Event Handlers


    //*********************************************************************
    //Mis eventos    
    get_events: function() {
        if (!this._events) {
            this._events = new Sys.EventHandlerList();
        }
        return this._events;
    },
    add_beginChange: function(handler) {
        this.get_events().addHandler('beginChange', handler);
    },
    remove_beginChange: function(handler) {
        this.get_events().removeHandler('beginChange', handler);
    },
    add_endChange: function(handler) {
        this.get_events().addHandler('endChange', handler);
    },
    remove_endChange: function(handler) {
        this.get_events().removeHandler('endChange', handler);
    },
    _raiseEvent: function(eventName, eventArgs) {
        var handler = this.get_events().getHandler(eventName);

        var theEventArgs = null;
        if (handler) {
            if (!eventArgs) {
                theEventArgs = Sys.EventArgs.Empty;
            }
            else {
                theEventArgs = eventArgs;
            }

            handler(this, theEventArgs);
        }
    },

    //Metodos
    AddItem: function(ArticuloID, Cantidad) {
        this._beginAddItem(ArticuloID, Cantidad);
    },
    EditItem: function(ItemID, ArticuloID, Cantidad) {
        this._beginEditItem(ItemID, ArticuloID, Cantidad);
    },
    RemoveItem: function(ItemID) {
        this._beginRemoveItem(ItemID);
    },

    Clear: function() {
        this._beginClear();
    },

    ActualizarInterface: function(data) {
        var html = data[0];
        if (this._ArticulosContainer) this._ArticulosContainer.innerHTML = html;

        var carritoCount = parseInt(data[1]);

        var cmdShowCarritoPedido = $get("cmdShowCarritoPedido");
        if (cmdShowCarritoPedido) {
            if (carritoCount == 1)
                cmdShowCarritoPedido.innerHTML = "Carrito de Compras (1 producto)";
            else if (carritoCount > 1)
                cmdShowCarritoPedido.innerHTML = "Carrito de Compras (" + carritoCount + " productos)";
            else
                cmdShowCarritoPedido.innerHTML = "Carrito de Compras (ningún producto)";
        }
        else
            alert(cmdShowCarritoPedido);
    },

    _beginAddItem: function(ArticuloID, Cantidad) {

        this._raiseEvent('beginChange', null);

        //Invocamos el servicio
        var ServiceData = new Array();
        ServiceData.Requester = this;
        ServiceData.ArticuloID = ArticuloID;
        ServiceData.Cantidad = Cantidad;

        OnlinePub.CarritoService.AddItem(ArticuloID, Cantidad,
            this._oncarritoServiceAddItemSuccess, this._oncarritoServiceError,
            ServiceData);
    },
    _endAddItem: function(result) {
        this.ActualizarInterface(result);
        this._raiseEvent('endChange', null);
    },

    //Servicio Web
    _oncarritoServiceAddItemSuccess: function(result, context, methodName) {
        context.Requester._endAddItem(result)
    },
    _oncarritoServiceError: function(error, context, methodName) {
        //No hay nada por hacer
        alert("CarritoService Error: " + error.get_message());
    },

    _beginEditItem: function(ItemID, ArticuloID, Cantidad) {

        this._raiseEvent('beginChange', null);

        //Invocamos el servicio
        var ServiceData = new Array();
        ServiceData.Requester = this;
        ServiceData.ArticuloID = ArticuloID;
        ServiceData.Cantidad = Cantidad;
        ServiceData.ItemID = ItemID;
        
        OnlinePub.CarritoService.EditItem(ItemID, ArticuloID, Cantidad,
            this._oncarritoServiceEditItemSuccess, this._oncarritoServiceError,
            ServiceData);
    },
    _endEditItem: function(result) {
        this.ActualizarInterface(result);
        this._raiseEvent('endChange', null);

        alert("El item fue actualizado con exito");
    },

    //Servicio Web
    _oncarritoServiceEditItemSuccess: function(result, context, methodName) {
        context.Requester._endEditItem(result)
    },
    _oncarritoServiceError: function(error, context, methodName) {
        //No hay nada por hacer
        alert("CarritoService Error: " + error.get_message());
    },


    _beginClear: function() {

        this._raiseEvent('beginChange', null);

        //Invocamos el servicio
        var ServiceData = new Array();
        ServiceData.Requester = this;

        OnlinePub.CarritoService.Clear(
            this._oncarritoServiceClearSuccess, this._oncarritoServiceError,
            ServiceData);
    },
    _endClear: function(result) {
        this.ActualizarInterface(result);
        this._raiseEvent('endChange', null);
    },

    //Servicio Web
    _oncarritoServiceClearSuccess: function(result, context, methodName) {
        context.Requester._endAddItem(result)
    },


    _beginRemoveItem: function(ItemID) {

        this._raiseEvent('beginChange', null);

        //Invocamos el servicio
        var ServiceData = new Array();
        ServiceData.Requester = this;
        ServiceData.ItemID = ItemID;

        OnlinePub.CarritoService.RemoveItem(ItemID,
            this._oncarritoServiceRemoveItemSuccess, this._oncarritoServiceError,
            ServiceData);
    },

    _endRemoveItem: function(result) {
        this.ActualizarInterface(result);
        this._raiseEvent('endChange', null);
    },

    //Servicio Web
    _oncarritoServiceRemoveItemSuccess: function(result, context, methodName) {
        context.Requester._endRemoveItem(result);
    }
}

dev54.OPCS.eCommerce.Carrito.registerClass('dev54.OPCS.eCommerce.Carrito', Sys.Component);
