var ErrorsHandle = function() {
	this.elements = [];
	this.handleElement = function(element) {
		if (element.size() == 0)
			return;
		this.elements.push(element);
		var children = element.children();
		var currentObject = this;
		if (children.size() > 0) {
			jQuery.each(children, function() {
				currentObject.handleElement(jQuery(this));
			});
		}
		var id = element.attr("id");
		if (id != null && id != "") {
			var label = jQuery("label[for=" + id + "]");
			this.elements.push(label);
			label.addClass("error");
		}
		element.addClass("error");
	}
	this.clearErrors = function() {
		jQuery.each(this.elements, function() {
			this.removeClass("error");
		});
		this.elements = [];
	}
	this.handle = function(errors) {
		this.clearErrors();
		var currentObject = this;
		jQuery.each(errors, function(){
			var object = jQuery("#" + this.id)
			currentObject.handleElement(object);
		});
	}
}
var errorsHandler = new ErrorsHandle();
function bindFjErrors(errors) {
	if (errors.errors)
		errorsHandler.handle(errors.errors);
	var form = jQuery("#" + errors.form);
	form.removeClass("busy");
	var scrollTop = form.offset().top;
	if (errors.message) {
		var errorsContainer = form.find(".formerrors");
		errorsContainer.empty();
		errorsContainer.append(jQuery(errors.message));
		var scrollTop = errorsContainer.offset().top;
	}
	jQuery("html,body").animate({scrollTop: scrollTop});
}
function bindFJFormOk(message) {
	errorsHandler.clearErrors();
	var form = jQuery("#" + message.form);
	form.removeClass("busy");
	var scrollTop = form.offset().top;
	if (message.message && message.message != "") {
		var messageContainer = jQuery(message.message).insertBefore(form);
		var scrollTop = messageContainer.offset().top;
		form.remove();
	}
	jQuery("html,body").animate({scrollTop: scrollTop});
}
jQuery(function() {
	jQuery(".addcontainer button").each(function() {
		var current = jQuery(this);
		var addContainer = current.parents(".addcontainer");
		current.click(function() {
			var values = jQuery("[name=" + current.attr("name") + "]");
			jQuery.post(document.location.href, {_reqtype:"htmlpart", request: "addcontainer", "size": values.size(), "id": addContainer.attr("id"), "name":current.attr("name"), value: current.attr("value")}, function(data, textStatus) {
				jQuery(data).insertBefore(addContainer);
			},"html");
			return false;
		});
	});
	var ID= "_____" + jQuery("*").size() + "___fo___";
	var iName = ID + "_item__";
	var formIframe = jQuery('<iframe id="'+ID+'" name="'+iName+'"></iframe>').appendTo(jQuery("body"));
	formIframe.hide();
	jQuery("form._formcreator_form").each(function() {
		var formElement = jQuery(this);
		formElement.submit(function() {
			formElement.addClass("busy");
		});
		try {
			var action = this.action;
			if (action == null || action == "") {
				action = document.location.href;
			}
			if (action.indexOf("?") > 0) {
				action += "&";
			} else {
				action += "?";
			}
			action += "_reqtype=htmlpart";
			this.action = action;
			this.target = iName;
		} catch (e) {
			jQuery(e).debug();
		}
	});
});
/*
jQuery(function() {
	var fieldsets = [];
	jQuery("fieldset").each(function() {
		var current = jQuery(this);
		if (current.parent("fieldset").size() == 0) {
			var children = current.children(":not(h1)+:not(legend)");
			var h1 = current.children("h1");
			fieldsets.push({"field": current, "head":h1, "children": children});
		}
	});
	jQuery.each(fieldsets, function(i, val) {
		if (i> 0) {
			val.children.hide();
		}
		val.head.click(function(){
			jQuery.each(fieldsets, function(vi, vval) {
				vval.children.hide();
			});
			val.children.show();
		});
	});
});
*/
