/**
 * Hides forms fields of language, that is not available
 */
function processForms()
{
	tagList = Array('input', 'label', 'textarea', 'select', 'checkbox');

	for (ti = 0; ti < tagList.length; ti++) {
		objects = document.getElementsByTagName(tagList[ti]);

		for (i = 0; i < objects.length; i++) {
			var field_lang;

			if (objects[i].id)
				field_lang = objects[i].id.split('_').pop();
			else if (objects[i].htmlFor)
				field_lang = objects[i].htmlFor.split('_').pop();
			else
				continue;

			if (field_lang != 'pl' && field_lang != 'en')
				continue;

			if (LANGUAGES.indexOf(field_lang) == -1) {
				hide(objects[i]);
				objects[i].className = ''; // for codepress and jquery resize - do not mess up
			}
		}
	}
}

/**
 * Redirect to given address
 */
function goToAddress(address)
{
	location.href = address;
}

/**
 * Get element by it's ID
 */
function elById(id)
{
	return document.getElementById(id);
}

/**
 * If given param is string, returns object of that ID, otherwise returns the
 * parameter
 */
function getObj(what)
{
	if (typeof(what) == 'string')
		return elById(what);
	else
		return what;
}

/**
 * Hides object given by parameter or object with ID given by parameter
 */
function hide(what)
{
	getObj(what).style.display = 'none';
}

/**
 * Shows object given by parameter or object with ID given by parameter
 */
function show(what)
{
	getObj(what).style.display = '';
}

/**
 * Changes object visibility (hides if visible/shows if hidden)
 */
function toggleVisibility(what)
{
	var obj = getObj(what);

	if (obj.style.display)
		show(obj);
	else
		hide(obj);
}

/**
 * Enables object given by parameter or object with ID given by parameter
 */
function enable(what)
{
	getObj(what).disabled = false;
}

/**
 * Disables object given by parameter or object with ID given by parameter
 */
function disable(what)
{
	getObj(what).disabled = true;
}

/**
 * Returns label associated with given field (or field ID) as parameter, or
 * null if it does not exist
 */
function fieldLabel(field)
{
	field = getObj(field);

	var labels = document.getElementsByTagName('label');
	for (i = 0; i < labels.length; i++) {
		if (labels[i].htmlFor == field.id)
			return labels[i];
	}

	return null;
}

/**
 * Hide, show, enable or disable certain fields if needed, according to edited
 * page type
 */
function pageTypeSelected()
{
	var typeName = elById('edit_form_type_name').value;

	switch (typeName) {
		case 'link':
			hide('edit_form_page_only');
			show('edit_form_menu_link_only');

			break;

		case 'link_society':
		case 'link_journal':
			hide('edit_form_page_only');
			hide('edit_form_menu_link_only');

			break;

		case 'normal':
		case 'hidden':
		case 'invitation':
			hide('edit_form_menu_link_only');
			show('edit_form_page_only');

			show('edit_form_template_id');
			show(fieldLabel('edit_form_template_id'));

			break;

		case 'program':
		case 'promoting':
			hide('edit_form_menu_link_only');
			show('edit_form_page_only');
			
			hide('edit_form_template_id');
			hide(fieldLabel('edit_form_template_id'));
			
			break;
	}
}

/**
 * Hide, show, enable or disable certain fields if needed, according to edited
 * conference type
 */
function conferenceTypeSelected()
{
	var typeName = elById('properties_form_type_name').value;

	switch (typeName) {
		case 'archival_external':
			show('properties_form_archival_external_only');
			hide('properties_form_internal_only');

			break;

		default:
			hide('properties_form_archival_external_only');
			show('properties_form_internal_only');
			
			break;
	}
}

/**
 * Used in page edit onsubmit, confirm page deletion if requested
 */
function confirmPageDeletion()
{
	if (!elById('edit_form_delete_this').checked)
		return true;

	return confirm('UWAGA!\n\nCzy na pewno chcesz USUNĄĆ tę stronę?\nNie można jej później przywrócić!');
}

/**
 * Used in conference edit onsubmit, confirm conference deletion if requested
 */
function confirmConferenceDeletion()
{
	if (!elById('properties_form_delete_this').checked)
		return true;

	return confirm('UWAGA!\n\nCzy na pewno chcesz USUNĄĆ tę konferencję?\nNie można jej później przywrócić!');
}

/**
 * Used in sponsor edit onsubmit, confirm sponsor deletion if requested
 */
function confirmSponsorDeletion()
{
	if (!elById('edit_form_delete_this').checked)
		return true;

	return confirm('Czy na pewno chcesz USUNĄĆ tego sponsora?');
}

/**
 * Used in exhibitor edit onsubmit, confirm exhibitor deletion if requested
 */
function confirmExhibitorDeletion()
{
	if (!elById('edit_form_delete_this').checked)
		return true;

	return confirm('Czy na pewno chcesz USUNĄĆ tego wystawcę?');
}

/**
 * Used to confirm file deletion, if confirmed, redirects to proper script with parameter
 */
function deleteFile(file)
{
	if (!confirm('Czy na pewno chcesz USUNĄĆ ten plik?\n' + file))
		return;

	goToAddress('file.php?file=' + file + '&mode=delete');
}

/**
 * Used to confirm directory deletion, if confirmed, redirects to proper script with parameter
 */
function deleteDirectory(directory)
{
	if (!confirm('Czy na pewno chcesz USUNĄĆ ten katalog?\n' + directory))
		return;

	goToAddress('directory.php?directory=' + directory + '&mode=delete');
}

/**
 * Used to confirm user deletion, if confirmed, redirects to proper script with parameter
 */
function deleteUser(id, login)
{
	if (!confirm('Czy na pewno chcesz USUNĄĆ tego użytkownika?\n' + login))
		return;

	goToAddress('user.php?mode=delete&user_id=' + id);
}

/**
 * Removes 'Search...' text when focusing search fields, puts it back if still empty on focus lost
 */
function seeker(active)
{
	var seeker = elById('seeker_searched');

	if (seeker.value == SEARCH && active)
		seeker.value = '';
	else if (seeker.value == '' && !active)
		seeker.value = SEARCH;
}

/**
 * Shows/hides seeker options fieldset
 */
function seekerOptions(visible)
{
	elById('seeker_options').style.display = visible ? '' : 'none';
}

/**
 * Adds resize bar under page text editors
 */
function initHtmlEditors() {
	$('textarea.codepress').wrap('<div class="htmlEditor" />');

	$('.htmlEditor').resizable({
		handles: "se",
		minHeight: 100,
		minWidth: 500
	}).dblclick(function() {
		if (this.style.position) {
			this.style.position = '';
			this.style.left = '';
			this.style.top = '';
			this.style.width = '';
			this.style.height = '';

			$('.htmlEditor').show();
		}
		else {
			this.style.position= 'fixed';
			this.style.left = '20px';
			this.style.top = '20px';
			this.style.width = window.innerWidth - 60 + 'px';
			this.style.height = window.innerHeight - 50 + 'px';

			$('.htmlEditor').hide();
			this.style.display = 'block';
		}
	}).blur(function() {
		this.style.position = '';
		this.style.left = '';
		this.style.top = '';
		this.style.width = '';
		this.style.height = '';

		$('.htmlEditor').show();
	});

	CodePress.run();
}

function synchronizeTextEditor() {
	try {
		edit_form_text_pl.toggleEditor();
	}
	catch (ex) {
	}

	try {
		edit_form_text_en.toggleEditor();
	}
	catch (ex) {
	}

	return true;
}
