function toggle_selection(input) {
	checked = input.checked;
	$(input.form).find('input[type=checkbox]').attr('checked', checked);
}

function async_post_form(button, element) {
	// Inicializuj, najdi hodnoty vo formulari.
	button.disabled = true;
	xajax.config.requestURI = button.form.action;
	form_name = button.form.getAttribute("name");
	post_values = xajax.getFormValues(form_name);
	
	// Zrus tlacitka.
	$(button.form).find('input[type=submit]').each(function() {
			post_values[this.name] = undefined;
	});
	
	// Pridaj stlacene.
	post_values[button.name] = button.value;
	
	// Vykonaj dotaz.
	return xajax_asynchronous_process(element, post_values);
}

function sync_post_form(button, element) {
	xajax.config.defaultMode = 'synchronous';
	ret_val = async_post_form(button, element);
	xajax.config.defaultMode = 'asynchronous';
	return ret_val;
}

function ask_question(question, ajax_element, url_relative) {
	if (!confirm(question)) {
		return false;
	}
	
	if (ajax_element != null) {
		return async_request(url_relative, ajax_element);
	} else {
		return true;
	}
}

function ask_question_submit(question, button, ajax_element) {
	 if (!confirm(question)) {
		 return false; 
	 }
	 
	 if (ajax_element != null) {
		 return async_post_form(button, ajax_element);
	 } else {
		 return true;
	 }
}

function dialog_close(element) {
	window.atteq.dialog.closing = 1;
	$("#" + element).dialog('close');
	window.atteq.dialog.closing = 0;
}

function dialog_show(url, element) {
	// Synchronny dotaz.
	xajax.config.defaultMode = 'synchronous';
	if (async_request(url, element)) {
		return true;
	}
	xajax.config.defaultMode = 'asynchronous';

	$('#' + element).dialog('open');
	return false;
}

function dialog_init(caption, element, close_callback) {
	$(document).ready(function () {
		$('#' + element).dialog({
			autoOpen: false,
			title: caption,
			modal: true,
			minWidth: 800,
			width: 800,
			close: function (event) {
                if (event.cancelable === undefined) {
                    event.cancelable = false;
                }
                close_callback(event.cancelable, element);

				// Editor zatvorime.
				close_dialog_editor();
			}
		});
	});
}

function dialog_close_click_cancel_button(cancel_button, element) {
    if (!window.atteq.dialog.closing && cancel_button != null) {
        $('#' + element + " input[name=" + cancel_button + "]").click();
    }
}

function dialog_button_clicked(parent, dialog, button) {
	// Update pre obsah pripadneho editora.
	update_dialog_editor();
	// Zatvor editor, nech mame priestor pre novy.
	close_dialog_editor();
	
	// Synchronny dotaz.
	if (sync_post_form(button, parent + "|" + dialog)) {
		return true;
	}
	
	// Test navratovej hodnoty.
	if (!window.atteq.xajax.return_value) {
		dialog_close(dialog);
	} else {
		refresh_dialog_editor();
	}
	
	// V kazdom pripade, vratime false, aby sa nevykonal standardny dotaz.
	return false;
}

function create_editor(element, config) {
	return CKEDITOR.replace(element, config);
}

function create_dialog_editor(element, config, standalone_editor) {
	close_dialog_editor();
	editor = create_editor(element, config);
	
	if (!standalone_editor) {
		window.atteq.editor.instance = editor;
		window.atteq.editor.element = element;
		window.atteq.editor.config = config;
	}
}

function update_dialog_editor() {
	if (window.atteq.editor.instance) {
		window.atteq.editor.instance.updateElement();
	}
}

function refresh_dialog_editor() {
	create_dialog_editor(window.atteq.editor.element, window.atteq.editor.config);
}

function close_dialog_editor() {
	if (window.atteq.editor.instance) {
		try {
			window.atteq.editor.instance.destroy();
		} catch (e) {
		} finally {
			window.atteq.editor.instance = null;
		}
	}
}

function fbs_click() {
	u=location.href;
	t=document.title;
	window.open(
			'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t),
			'sharer','toolbar=0,status=0,width=626,height=436'
	);
	
	return false;
}

function check_not_default_values(form) {
    $(form).find('.value-default').each(function() {
        $("ul.messages li." + this.name).show();
    });

    $(form).find('.value-filled-in').each(function() {
        $("ul.messages li." + this.name).hide();
    });

    return $(form).find('.value-default').length == 0;
}

