////////////////////////////////////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////////////////////////////////////

var __CFGIFT_ERROR_OFFER_FORM_ID = "invalid CFGiftOfferForm id";

////////////////////////////////////////////////////////////////////////////////
// Static Variables
////////////////////////////////////////////////////////////////////////////////

var __cfGiftOfferFormMap = {};
var __cfGiftWishListItems = new Array();

////////////////////////////////////////////////////////////////////////////////
// Classes
////////////////////////////////////////////////////////////////////////////////

function CFGiftOfferForm(id, nameId, categoryId)
{
    CFWidget.call(this, id);
    var nameSelect = cfExtendedSelectGet(nameId);
    this.__categorySelect = cfMultiSelectGet(categoryId);
    this.__descriptionBox = this.getElement().elements["description"];
    this.__nameSelect = nameSelect;
    __cfGiftOfferFormMap[id] = this;
    var f = cfEventHandlerCreate(this.__handleNameUpdate.bind(this));
    nameSelect.addEventHandler(CFWIDGET_EVENT_USER_UPDATE, f);
    this.__handleNameUpdate();
}

CFGiftOfferForm.extendClasses(CFWidget);

CFGiftOfferForm.prototype.__handleNameUpdate = function()
{
    var index = this.__nameSelect.getSelectedIndex();
    if (index > 0) {
        var item = __cfGiftWishListItems[index - 1];
        this.__categorySelect.setValue(item.getCategory(),
                                       item.getSubcategory());
        this.__descriptionBox.value = item.getDescription();
    }
}

// CFGiftWishListItem

function CFGiftWishListItem(name, quantity, description, category, subcategory)
{
    this.__category = category;
    this.__description = description;
    this.__name = name;
    this.__quantity = quantity;
    this.__subcategory = subcategory;
    __cfGiftWishListItems.push(this);
}

CFGiftWishListItem.prototype.getCategory = function()
{
    return this.__category;
}

CFGiftWishListItem.prototype.getDescription = function()
{
    return this.__description;
}

CFGiftWishListItem.prototype.getName = function()
{
    return this.__name;
}

CFGiftWishListItem.prototype.getQuantity = function()
{
    return this.__quantity;
}

CFGiftWishListItem.prototype.getSubcategory = function()
{
    return this.__subcategory;
}

////////////////////////////////////////////////////////////////////////////////
// Public API
////////////////////////////////////////////////////////////////////////////////

function cfGiftOfferFormCreate(arg)
{
    return new CFGiftOfferForm(arg.id, arg.nameId, arg.categoryId);
}

function cfGiftOfferFormGet(id)
{
    var form = __cfGiftOfferFormMap[id];
    if (! form) {
        return cfErrorTrigger("cfGiftOfferFormGet: '" + id + "': " +
                              __CFGIFT_ERROR_OFFER_FORM_ID);
    }
    return form;
}

function cfGiftWishListItemCreate(arg)
{
    return new CFGiftWishListItem(arg.name, arg.quantity, arg.description,
                                  arg.category, arg.subcategory);
}
