/**
 * opens a popup with default-options
 *
 * @param string filename the name of the file to display in the popup
 * @param string title the title of the popup
 * @param int width the width of the popup
 * @param int height the height of the popup
 */
function openDefaultPopup(filename,title,width,height)
{
	var options = 'toolbar=no,status=no,menubar=no,width=' + width + ',height=' + height;
	options += ',resizeable=yes,scrollbars=yes,left=0,top=0,screenX=0,screenY=0';
	window.open(filename,title,options);
}

/**
 * opens a popup with the given parameters
 *
 * @param string filename the name of the file to display in the popup
 * @param string title the title of the popup
 * @param string options the options of die popup
 */
function openPopup(filename,title,options)
{
	window.open(filename,title,options);
}

/**
 * performs a mod-action for some topics
 * collects the ids from the checkboxes and redirects to the given url
 *
 * @param string base_url the base_url ending with &amp;URL_ID=
 */
function performModAction(base_url)
{
	if(base_url == '')
		return;
	
	var ids = "";
	var cb = null;
	for(var i = 0;(cb = document.getElementById('id_' + i)) != null;i++)
	{
		if(cb.checked)
			ids += cb.value + ",";
	}

	if(ids == "")
		return;

	document.location.href = base_url + ids;
}

/**
 * changes the form-target to the given target and submits the form
 * this way we will quote a post
 *
 * @param string formTarget the new target of the form
 */
function quotePost(formTarget)
{
	var button = document.getElementById('quotePostButton');
	if(document.quotePostForm != null && button != null)
	{
		document.quotePostForm.action = formTarget;
		button.click();
	}
}

/**
 * claps a table-row with given id including image and cookie
 *
 * @param string row_id the id of the row
 * @param string img_id the id of the image
 * @param string cookie_name the name of the cookie where to store the current status
 * @param string root the root-path
 */
function clapTableRow(row_id,img_id,cookie_name,root)
{
	var row = document.getElementById(row_id);
	var display = (row.style.display == 'none') ? (document.all && !window.opera ? 'block' : 'table-row') : 'none';
	row.style.display = display;
	
	if(display != 'none')
		document.cookie = cookie_name + '=1';
	else
		document.cookie = cookie_name + '=0';

	var img = document.images[img_id];
	if(img != null)
		img.src = root + "images/cross" + (display != "none" ? 'open' : 'closed') + ".gif";
}

/**
 * claps the forum with given id
 *
 * @param int id the id of the forum
 * @param string imagePath the path to the images folder
 * @param string cookiePrefix the cookie-prefix
 */
function clapForum(id,imagePath,cookiePrefix)
{
	var setcookies = '';
	var subForums = document.getElementById('subForums_' + id);
	
	if(subForums.style.display == 'none')
	{
		var cookieParts = global_cookie.split(',');
		for(var i = 0;i < cookieParts.length;i++)
		{
			if(cookieParts[i] != '' && cookieParts[i] != id)
				setcookies += cookieParts[i] + ',';
		}
		
		subForums.style.display = 'block';
		imagePath += 'images/crossopen.gif';
	}
	else
	{
		setcookies = global_cookie + id + ',';
		
		subForums.style.display = 'none';
		imagePath += 'images/crossclosed.gif';
	}

	document.cookie = cookiePrefix + 'hidden_forums=' + setcookies;
	global_cookie = setcookies;
	
	var image = document.getElementById('cross_' + id);
	image.src = imagePath;
}

/**
 * scrolls to the bottom in the given textarea
 *
 * @param object textarea the textarea
 */
function scrollToBottom(textArea)
{
	textArea.scrollTop = textArea.scrollHeight;
}

/**
 * trims the given string
 *
 * @param string input the input-string
 * @return string the trim`ed string
 */
function trim(input)
{
	if(input == "")
		return "";

	// determine the first not-whitespace-character
	var i = 0;
	for(;i < input.length;i++)
	{
		if(input[i] != '\n' && input[i] != '\r' && input[i] != '\t' && input[i] != ' ')
			break;
	}

	// determine the last not-whitespace-character
	var a = input.length - 1;
	for(;a >= 0;a--)
	{
		if(input[a] != '\n' && input[a] != '\r' && input[a] != '\t' && input[a] != ' ')
			break;
	}

	return input.substr(i,a - i + 1);;
}

/**
 * inverts all checkboxes with the following id:
 * <code>&lt;prefix&gt;&lt;number&gt; , &lt;number&gt; ? {start, start + 1, ...}</code>
 *
 * @param string prefix the prefix of the checkbox-ids
 * @param int start the start-number
 * @param boolean nullNotRequired if enabled is the id "&lt;prefix&gt;0" not required
 */
function invertSelection(prefix,start,nullNotRequired)
{
	if(!start)
		start = 0;

	var f;
	for(var i = start;;i++)
	{
		f = document.getElementById(prefix + i);
		if(f == null && (!nullNotRequired || i > 0))
			return;

		if(f != null)
			f.checked = !f.checked;
	}
}

/**
 * inverts the enable-status of the agreement button at the registration
 *
 * @param object checkbox the checkbox
 * @param string submit_id the id of the button
 */
function checkAgreement(checkbox,submit_id)
{
	var f = document.getElementById(submit_id);
	if(checkbox.checked)
	{
		f.disabled = false;
		f.style.color = '#000000';
	}
	else
	{
		f.disabled = true;
		f.style.color = '#BBBBBB';
	}
}

/**
 * toggles the display-status of the div-area with given id
 *
 * @param int id the id of the div-area
 */
function toggleArea(id)
{
	var div = document.getElementById(id);
	div.style.display = div.style.display == 'none' ? 'block' : 'none';
}
