function PrototypeMultiItem(owner)
{
this.owner = owner;
this.on_remove = null;
this.content_container = null;
this._index = PrototypeMultiItem.items_index_counter + 1;
PrototypeMultiItem.items_index_counter++;
this.CreateControls();
this.UpdateInterface();
}
PrototypeMultiItem.prototype.ChangeControlVisibility = function(control, is_visible)
{
if (is_visible == true) {
$(control).show();
}
else {
$(control).hide();
}
}
PrototypeMultiItem.prototype.UpdateContainerID = function(container)
{
var object = this;
if (container.id) {
container.id = container.id + this._index;
}
$(container).children().each(function (index, child) {
object.UpdateContainerID(child);
});
}
PrototypeMultiItem.prototype.IsFirst = function()
{
return (this.owner.items_list.indexOf(this) == 0);
}
PrototypeMultiItem.prototype.IsLast = function()
{
return (this.owner.items_list.indexOf(this) == (this.owner.items_list.length - 1));
}
PrototypeMultiItem.prototype.HTMLPrototype = function()
{
return this.owner.html_prototype;
}
PrototypeMultiItem.prototype.ParentContainer = function()
{
return this.owner.parent_container;
}
PrototypeMultiItem.prototype.AddButton = function()
{
return this.owner.add_button;
}
PrototypeMultiItem.prototype.RemoveButton = function()
{
return this.owner.remove_button;
}
PrototypeMultiItem.prototype.CaptionLabel = function()
{
return this.owner.caption_label;
}
PrototypeMultiItem.prototype.Value = function()
{
if (this.owner.input_control) {
var element_id = this.owner.input_control + this._index;
return $(element_id).val();
}
return "";
}
PrototypeMultiItem.prototype.SetValue = function(value)
{
if (this.owner.input_control) {
var element_id = this.owner.input_control + this._index;
$(element_id).val(value);
}
}
PrototypeMultiItem.prototype.IsAddButtonVisible = function()
{
return (this.IsLast());
}
PrototypeMultiItem.prototype.IsRemoveButtonVisible = function()
{
if (this.owner.items_list.length == 1)
return (!this.IsFirst());
return true;
}
PrototypeMultiItem.prototype.CloneContainer = function ()
{
if ((this.content_container == null) &&
(this.HTMLPrototype() != ""))
{
var object = this;
this.content_container = $.parseHTML(this.HTMLPrototype())[0];
$(this.content_container).removeClass("prototype");
this.UpdateContainerID(this.content_container);
$(this.ParentContainer())[0].appendChild(this.content_container);
if (this.RemoveButton())
{
$(this.RemoveButton() + this._index)[0].onclick = function() {
object.Remove();
object.UpdateInterface();
};
}
}
}
PrototypeMultiItem.prototype.CreateControls = function ()
{
this.CloneContainer();
}
PrototypeMultiItem.prototype.UpdateCaptionLabel = function ()
{
if (this.CaptionLabel() != "")
{
var caption = $(this.CaptionLabel()).html();
if (caption != "") {
var index = this.owner.items_list.indexOf(this);
if (index > 0) {
caption += " " + (index + 1);
}
var element_id = this.CaptionLabel() + this._index;
$(element_id).html(caption);
}
}
}
PrototypeMultiItem.prototype.UpdateInterface = function ()
{
if (this.RemoveButton()) {
this.ChangeControlVisibility(this.RemoveButton() + this._index, this.IsRemoveButtonVisible());
}
if (this.AddButton()) {
this.ChangeControlVisibility(this.AddButton() + this._index, this.IsAddButtonVisible());
}
this.UpdateCaptionLabel();
}
PrototypeMultiItem.prototype.OnRemove = function (event)
{
this.on_remove = event;
}
PrototypeMultiItem.prototype.GetInputValue = function (element_id)
{
if (element_id)
{
element_id = "#" + element_id + this._index;
return $(element_id).val();
}
return "";
}
PrototypeMultiItem.prototype.SetInputValue = function (element_id, value)
{
if (element_id)
{
element_id = "#" + element_id + this._index;
$(element_id).val(value);
}
}
PrototypeMultiItem.prototype.Remove = function ()
{
if (this.content_container != null)
{
$(this.content_container).remove();
if (this.on_remove)
this.on_remove(this);
}
}
PrototypeMultiItem.items_index_counter = 0;