;function ARIElementsCloner(id, options, clonerOptions, data) {
	var cloner = jQuery("#" + id).elementsCloner(clonerOptions, data);

	var saveData = function() {
		var data = cloner.getData(),
			fixedData = [];

		if (options.keyField) {
			var keyField = options.keyField;
			for (var i = 0; i < data.length; i++) {
				var dataItem = data[i];
				if (dataItem[keyField])
					fixedData.push(dataItem);
			}
		}

		$(options.hiddenId).value = JSON.stringify(fixedData.length > 0 ? fixedData : null);
	};

    if (typeof(Joomla) != "undefined" && typeof(Joomla.submitbutton) != "undefined") {
        var oldHandler = Joomla.submitbutton;
        Joomla.submitbutton = function() {
            saveData();

            oldHandler.apply(this, arguments);
        }
    } else {
        $(document.body).getElements('form').addEvent("submit", (function (e) {
            saveData();
        }).bind(this));
    }
};